diff --git a/toolkit/components/places/src/nsNavHistory.cpp b/toolkit/components/places/src/nsNavHistory.cpp index dc6c103aa11a..df346cb70fb3 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);