Corrupt prefs.js file not removed if backup file (invalidprefs.js) exists. b=495735 r=dietrich r=benjamin

This commit is contained in:
Mats Palmgren 2009-11-03 20:05:31 +01:00
parent 688e01bef4
commit 632cf29816
2 changed files with 19 additions and 10 deletions

View File

@ -21,6 +21,7 @@
*
* Contributor(s):
* Alec Flett <alecf@netscape.com>
* Mats Palmgren <matspal@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -76,7 +77,6 @@ static nsresult pref_InitInitialObjects(void);
*/
nsPrefService::nsPrefService()
: mDontWriteUserPrefs(PR_FALSE)
{
}
@ -305,10 +305,22 @@ nsresult nsPrefService::UseUserPrefFile()
nsresult nsPrefService::MakeBackupPrefFile(nsIFile *aFile)
{
// Example: this copies "prefs.js" to "Invalidprefs.js" in the same directory.
// "Invalidprefs.js" is removed if it exists, prior to making the copy.
nsAutoString newFilename;
nsresult rv = aFile->GetLeafName(newFilename);
NS_ENSURE_SUCCESS(rv, rv);
newFilename.Insert(NS_LITERAL_STRING("Invalid"), 0);
nsCOMPtr<nsIFile> newFile;
rv = aFile->GetParent(getter_AddRefs(newFile));
NS_ENSURE_SUCCESS(rv, rv);
rv = newFile->Append(newFilename);
NS_ENSURE_SUCCESS(rv, rv);
PRBool exists = PR_FALSE;
newFile->Exists(&exists);
if (exists) {
rv = newFile->Remove(PR_FALSE);
NS_ENSURE_SUCCESS(rv, rv);
}
rv = aFile->CopyTo(nsnull, newFilename);
NS_ENSURE_SUCCESS(rv, rv);
return rv;
@ -328,7 +340,10 @@ nsresult nsPrefService::ReadAndOwnUserPrefFile(nsIFile *aFile)
if (exists) {
rv = openPrefFile(mCurrentFile);
if (NS_FAILED(rv)) {
mDontWriteUserPrefs = NS_FAILED(MakeBackupPrefFile(mCurrentFile));
// Save a backup copy of the current (invalid) prefs file, since all prefs
// from the error line to the end of the file will be lost (bug 361102).
// TODO we should notify the user about it (bug 523725).
MakeBackupPrefFile(mCurrentFile);
}
} else {
rv = NS_ERROR_FILE_NOT_FOUND;
@ -388,12 +403,6 @@ nsresult nsPrefService::WritePrefFile(nsIFile* aFile)
if (!gHashTable.ops)
return NS_ERROR_NOT_INITIALIZED;
// Don't save user prefs if there was an error reading them and we failed
// to make a backup copy, since all prefs from the error line to the end of
// the file would be lost (bug 361102).
if (mDontWriteUserPrefs && aFile == mCurrentFile)
return NS_OK;
// execute a "safe" save by saving through a tempfile
rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink),
aFile,

View File

@ -21,6 +21,7 @@
*
* Contributor(s):
* Brian Nesse <bnesse@netscape.com>
* Mats Palmgren <matspal@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -77,8 +78,7 @@ protected:
private:
nsCOMPtr<nsIPrefBranch2> mRootBranch;
nsCOMPtr<nsIFile> mCurrentFile;
PRPackedBool mDontWriteUserPrefs;
nsCOMPtr<nsIFile> mCurrentFile;
};
#endif // nsPrefService_h__