Followup checkin for Bug 423154 - off-by-one error for browser.bookmarks.max_backups (r=mano, a=beltzner)

This commit is contained in:
dietrich@mozilla.com 2008-03-20 10:49:12 -07:00
parent 9864e10f4f
commit fab91284d2
3 changed files with 1 additions and 120 deletions

View File

@ -66,7 +66,7 @@ interface nsIPlacesImportExportService: nsISupports
void exportHTMLToFile(in nsILocalFile aFile);
/**
* Backup and archive the bookmarks.html file.
* Backup the bookmarks.html file.
*/
void backupBookmarksFile();
};

View File

@ -2482,120 +2482,3 @@ nsPlacesImportExportService::BackupBookmarksFile()
return NS_OK;
}
/**
* ArchiveBookmarksFile()
*
* Creates a dated backup once a day in <profile>/bookmarkbackups
*
* PRInt32 numberOfBackups - the maximum number of backups to keep
*
* PRBool forceArchive - forces creating an archive even if one was
* already created that day (overwrites)
*/
nsresult
nsPlacesImportExportService::ArchiveBookmarksFile(PRInt32 numberOfBackups,
PRBool forceArchive)
{
nsCOMPtr<nsIFile> bookmarksBackupDir;
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(bookmarksBackupDir));
NS_ENSURE_SUCCESS(rv, rv);
nsDependentCString dirName("bookmarkbackups");
rv = bookmarksBackupDir->AppendNative(dirName);
NS_ENSURE_SUCCESS(rv, rv);
PRBool exists;
rv = bookmarksBackupDir->Exists(&exists);
if (NS_FAILED(rv) || !exists) {
rv = bookmarksBackupDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
// if there's no backup folder, there's no backup, fail
NS_ENSURE_SUCCESS(rv, rv);
}
// construct the new leafname
PRTime now64 = PR_Now();
PRExplodedTime nowInfo;
PR_ExplodeTime(now64, PR_LocalTimeParameters, &nowInfo);
PR_NormalizeTime(&nowInfo, PR_LocalTimeParameters);
char timeString[128];
// Use YYYY-MM-DD (ISO 8601) as it doesn't contain illegal characters
// and makes the alphabetical order of multiple backup files more useful.
PR_FormatTime(timeString, 128, "bookmarks-%Y-%m-%d.html", &nowInfo);
nsAutoString backupFilenameString = NS_ConvertUTF8toUTF16((timeString));
nsCOMPtr<nsIFile> backupFile;
if (forceArchive) {
// if we have a backup from today, nuke it
nsCOMPtr<nsIFile> currentBackup;
rv = bookmarksBackupDir->Clone(getter_AddRefs(currentBackup));
NS_ENSURE_SUCCESS(rv, rv);
rv = currentBackup->Append(backupFilenameString);
NS_ENSURE_SUCCESS(rv, rv);
rv = currentBackup->Exists(&exists);
if (NS_SUCCEEDED(rv) && exists) {
rv = currentBackup->Remove(PR_FALSE);
NS_ENSURE_SUCCESS(rv, rv);
}
} else {
nsCOMPtr<nsISimpleEnumerator> existingBackups;
rv = bookmarksBackupDir->GetDirectoryEntries(getter_AddRefs(existingBackups));
NS_ENSURE_SUCCESS(rv, rv);
nsStringArray backupFileNames;
PRBool hasMoreElements = PR_FALSE;
PRBool hasCurrentBackup = PR_FALSE;
while (NS_SUCCEEDED(existingBackups->HasMoreElements(&hasMoreElements)) &&
hasMoreElements)
{
rv = existingBackups->GetNext(getter_AddRefs(backupFile));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString backupName;
rv = backupFile->GetLeafName(backupName);
NS_ENSURE_SUCCESS(rv, rv);
// the backup for today exists, do not create later
if (backupName == backupFilenameString) {
hasCurrentBackup = PR_TRUE;
continue;
}
// mark the rest for possible removal
if (Substring(backupName, 0, 10) == NS_LITERAL_STRING("bookmarks-"))
backupFileNames.AppendString(backupName);
}
if (numberOfBackups > 0 && backupFileNames.Count() >= numberOfBackups) {
PRInt32 numberOfBackupsToDelete = backupFileNames.Count() - numberOfBackups + 1;
backupFileNames.Sort();
while (numberOfBackupsToDelete--) {
(void)bookmarksBackupDir->Clone(getter_AddRefs(backupFile));
(void)backupFile->Append(*backupFileNames[0]);
(void)backupFile->Remove(PR_FALSE);
backupFileNames.RemoveStringAt(0);
}
}
if (hasCurrentBackup)
return NS_OK;
}
nsCOMPtr<nsIFile> bookmarksFile;
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
getter_AddRefs(bookmarksFile));
NS_ENSURE_SUCCESS(rv, rv);
rv = bookmarksFile->CopyTo(bookmarksBackupDir, backupFilenameString);
// at least dump something out in case this fails in a debug build
NS_ENSURE_SUCCESS(rv, rv);
return rv;
}

View File

@ -48,8 +48,6 @@ class nsPlacesImportExportService : public nsIPlacesImportExportService,
nsresult WriteSeparator(nsINavHistoryResultNode* aItem, const nsACString& aIndent, nsIOutputStream* aOutput);
nsresult WriteDescription(PRInt64 aId, PRInt32 aType, nsIOutputStream* aOutput);
nsresult ArchiveBookmarksFile(PRInt32 aNumberOfBackups, PRBool aForceArchive);
inline nsresult EnsureServiceState() {
NS_ENSURE_STATE(mHistoryService);
NS_ENSURE_STATE(mFaviconService);