From 64d8d66e6f197c31f3b51eb8e073a383ffa7f289 Mon Sep 17 00:00:00 2001 From: "mats.palmgren%bredband.net" Date: Wed, 11 Nov 2009 03:50:20 +0000 Subject: [PATCH] Corrupt prefs.js file not removed if backup file (invalidprefs.js) exists. b=495735 r=benjamin a1.9.0.16=dveditz git-svn-id: svn://10.0.0.236/trunk@258961 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/libpref/src/nsPrefService.cpp | 37 +++++++++++-------- mozilla/modules/libpref/src/nsPrefService.h | 5 +-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/mozilla/modules/libpref/src/nsPrefService.cpp b/mozilla/modules/libpref/src/nsPrefService.cpp index 27ad9cddf32..8e43f35b2f5 100644 --- a/mozilla/modules/libpref/src/nsPrefService.cpp +++ b/mozilla/modules/libpref/src/nsPrefService.cpp @@ -21,6 +21,7 @@ * * Contributor(s): * Alec Flett + * Mats Palmgren * * 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 @@ -85,10 +86,6 @@ static nsresult pref_InitInitialObjects(void); */ nsPrefService::nsPrefService() -: mDontWriteUserPrefs(PR_FALSE) -#if MOZ_PROFILESHARING - , mDontWriteSharedUserPrefs(PR_FALSE) -#endif { } @@ -367,10 +364,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 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; @@ -395,7 +404,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; @@ -424,7 +436,10 @@ nsresult nsPrefService::ReadAndOwnSharedUserPrefFile(nsIFile *aFile) nsresult rv = openPrefFile(mCurrentSharedFile); if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) { - mDontWriteSharedUserPrefs = NS_FAILED(MakeBackupPrefFile(mCurrentSharedFile)); + // 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(mCurrentSharedFile); } #ifdef MOZ_PROFILESHARING @@ -494,16 +509,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; -#if MOZ_PROFILESHARING - if (mDontWriteSharedUserPrefs && aFile == mCurrentSharedFile) - return NS_OK; -#endif - // execute a "safe" save by saving through a tempfile rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink), aFile, diff --git a/mozilla/modules/libpref/src/nsPrefService.h b/mozilla/modules/libpref/src/nsPrefService.h index d6bc1fb61eb..aaa99f3b110 100644 --- a/mozilla/modules/libpref/src/nsPrefService.h +++ b/mozilla/modules/libpref/src/nsPrefService.h @@ -21,6 +21,7 @@ * * Contributor(s): * Brian Nesse + * Mats Palmgren * * 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 @@ -79,11 +80,9 @@ protected: private: nsCOMPtr mRootBranch; - nsCOMPtr mCurrentFile; - PRPackedBool mDontWriteUserPrefs; + nsCOMPtr mCurrentFile; #if MOZ_PROFILESHARING nsCOMPtr mCurrentSharedFile; - PRPackedBool mDontWriteSharedUserPrefs; #endif };