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
This commit is contained in:
mats.palmgren%bredband.net 2009-11-11 03:50:20 +00:00
parent 1b1e85dcaa
commit 64d8d66e6f
2 changed files with 23 additions and 19 deletions

View File

@ -21,6 +21,7 @@
* *
* Contributor(s): * Contributor(s):
* Alec Flett <alecf@netscape.com> * Alec Flett <alecf@netscape.com>
* Mats Palmgren <matspal@gmail.com>
* *
* Alternatively, the contents of this file may be used under the terms of * 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 * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -85,10 +86,6 @@ static nsresult pref_InitInitialObjects(void);
*/ */
nsPrefService::nsPrefService() nsPrefService::nsPrefService()
: mDontWriteUserPrefs(PR_FALSE)
#if MOZ_PROFILESHARING
, mDontWriteSharedUserPrefs(PR_FALSE)
#endif
{ {
} }
@ -367,10 +364,22 @@ nsresult nsPrefService::UseUserPrefFile()
nsresult nsPrefService::MakeBackupPrefFile(nsIFile *aFile) nsresult nsPrefService::MakeBackupPrefFile(nsIFile *aFile)
{ {
// Example: this copies "prefs.js" to "Invalidprefs.js" in the same directory. // 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; nsAutoString newFilename;
nsresult rv = aFile->GetLeafName(newFilename); nsresult rv = aFile->GetLeafName(newFilename);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
newFilename.Insert(NS_LITERAL_STRING("Invalid"), 0); 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); rv = aFile->CopyTo(nsnull, newFilename);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
return rv; return rv;
@ -395,7 +404,10 @@ nsresult nsPrefService::ReadAndOwnUserPrefFile(nsIFile *aFile)
if (exists) { if (exists) {
rv = openPrefFile(mCurrentFile); rv = openPrefFile(mCurrentFile);
if (NS_FAILED(rv)) { 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 { } else {
rv = NS_ERROR_FILE_NOT_FOUND; rv = NS_ERROR_FILE_NOT_FOUND;
@ -424,7 +436,10 @@ nsresult nsPrefService::ReadAndOwnSharedUserPrefFile(nsIFile *aFile)
nsresult rv = openPrefFile(mCurrentSharedFile); nsresult rv = openPrefFile(mCurrentSharedFile);
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) { 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 #ifdef MOZ_PROFILESHARING
@ -494,16 +509,6 @@ nsresult nsPrefService::WritePrefFile(nsIFile* aFile)
if (!gHashTable.ops) if (!gHashTable.ops)
return NS_ERROR_NOT_INITIALIZED; 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 // execute a "safe" save by saving through a tempfile
rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink), rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStreamSink),
aFile, aFile,

View File

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