From 9df1541c9059388f8bca6bc85f0b3db1035f1125 Mon Sep 17 00:00:00 2001 From: "dietrich@mozilla.com" Date: Sun, 6 Jan 2008 22:28:24 -0800 Subject: [PATCH] Bug 408751 ? nsNavHistory::Init is fragile (r=sspitzer) --- .../components/places/src/nsNavHistory.cpp | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/toolkit/components/places/src/nsNavHistory.cpp b/toolkit/components/places/src/nsNavHistory.cpp index dc6c103aa11..df346cb70fb 100644 --- a/toolkit/components/places/src/nsNavHistory.cpp +++ b/toolkit/components/places/src/nsNavHistory.cpp @@ -446,16 +446,27 @@ nsNavHistory::BackupDBFile() nsCOMPtr profDir; nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profDir)); - + + // create unique file nsCOMPtr corruptBackup; rv = profDir->Clone(getter_AddRefs(corruptBackup)); NS_ENSURE_SUCCESS(rv, rv); - rv = corruptBackup->Append(DB_CORRUPT_FILENAME); NS_ENSURE_SUCCESS(rv, rv); rv = corruptBackup->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); NS_ENSURE_SUCCESS(rv, rv); - return mDBFile->MoveTo(profDir, DB_CORRUPT_FILENAME); + + // get unique name, and remove tmp file + nsAutoString backupName; + rv = corruptBackup->GetLeafName(backupName); + NS_ENSURE_SUCCESS(rv, rv); + rv = corruptBackup->Remove(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + // copy contents of db file to new uniquely-named backup file + rv = mDBFile->CopyTo(profDir, backupName); + NS_ENSURE_SUCCESS(rv, rv); + return NS_OK; } // nsNavHistory::InitDBFile @@ -473,15 +484,24 @@ nsNavHistory::InitDBFile(PRBool aForceInit) NS_ENSURE_SUCCESS(rv, rv); // if forcing, backup and remove the old file + PRBool dbExists; if (aForceInit) { + if (mDBConn) { + // close db connection if open + rv = mDBConn->Close(); + NS_ENSURE_SUCCESS(rv, rv); + } rv = BackupDBFile(); NS_ENSURE_SUCCESS(rv, rv); + rv = mDBFile->Remove(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + dbExists = PR_FALSE; + } + else { + // file exists? + rv = mDBFile->Exists(&dbExists); + NS_ENSURE_SUCCESS(rv, rv); } - - // file exists? - PRBool dbExists; - rv = mDBFile->Exists(&dbExists); - NS_ENSURE_SUCCESS(rv, rv); // open the database mDBService = do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID, &rv);