Bug 923406 - Remove localizedFilenamePrefix from PlacesBackups.js r=mak

This commit is contained in:
Raymond Lee 2014-02-09 11:14:39 +01:00
parent e3376add57
commit 85848089fd
3 changed files with 65 additions and 91 deletions

View File

@ -30,15 +30,9 @@ XPCOMUtils.defineLazyGetter(this, "localFileCtor",
this.PlacesBackups = {
get _filenamesRegex() {
// Get the localized backup filename, will be used to clear out
// old backups with a localized name (bug 445704).
let localizedFilename =
PlacesUtils.getFormattedString("bookmarksArchiveFilename", [new Date()]);
let localizedFilenamePrefix =
localizedFilename.substr(0, localizedFilename.indexOf("-"));
delete this._filenamesRegex;
return this._filenamesRegex =
new RegExp("^(bookmarks|" + localizedFilenamePrefix + ")-([0-9-]+)(_[0-9]+)*\.(json|html)");
new RegExp("^(bookmarks)-([0-9-]+)(_[0-9]+)*\.(json|html)");
},
get folder() {
@ -319,9 +313,7 @@ this.PlacesBackups = {
if (aMaxBackups !== undefined && aMaxBackups > -1) {
let backupFiles = yield this.getBackupFiles();
numberOfBackupsToDelete = backupFiles.length - aMaxBackups;
}
if (numberOfBackupsToDelete > 0) {
// If we don't have today's backup, remove one more so that
// the total backups after this operation does not exceed the
// number specified in the pref.
@ -329,15 +321,15 @@ this.PlacesBackups = {
!this._isFilenameWithSameDate(OS.Path.basename(mostRecentBackupFile),
newBackupFilename))
numberOfBackupsToDelete++;
}
while (numberOfBackupsToDelete--) {
this._entries.pop();
if (!this._backupFiles) {
yield this.getBackupFiles();
}
let oldestBackup = this._backupFiles.pop();
yield OS.File.remove(oldestBackup);
while (numberOfBackupsToDelete--) {
this._entries.pop();
if (!this._backupFiles) {
yield this.getBackupFiles();
}
let oldestBackup = this._backupFiles.pop();
yield OS.File.remove(oldestBackup);
}
// Do nothing if we already have this backup or we don't want backups.

View File

@ -9,17 +9,17 @@
*/
const PREFIX = "bookmarks-";
// The localized prefix must be "bigger" and associated to older backups.
const LOCALIZED_PREFIX = "segnalibri-";
const SUFFIX = ".json";
const NUMBER_OF_BACKUPS = 10;
function run_test() {
do_test_pending();
run_next_test();
}
add_task(function () {
// Generate random dates.
var dateObj = new Date();
var dates = [];
let dateObj = new Date();
let dates = [];
while (dates.length < NUMBER_OF_BACKUPS) {
// Use last year to ensure today's backup is the newest.
let randomDate = new Date(dateObj.getFullYear() - 1,
@ -32,74 +32,63 @@ function run_test() {
// Sort dates from oldest to newest.
dates.sort();
Task.spawn(function() {
// Get and cleanup the backups folder.
let backupFolderPath = yield PlacesBackups.getBackupFolder();
let bookmarksBackupDir = new FileUtils.File(backupFolderPath);
// Get and cleanup the backups folder.
let backupFolderPath = yield PlacesBackups.getBackupFolder();
let bookmarksBackupDir = new FileUtils.File(backupFolderPath);
// Fake backups are created backwards to ensure we won't consider file
// creation time.
// Create fake backups for the newest dates.
for (let i = dates.length - 1; i >= 0; i--) {
let backupFilename;
if (i > Math.floor(dates.length/2))
backupFilename = PREFIX + dates[i] + SUFFIX;
else
backupFilename = LOCALIZED_PREFIX + dates[i] + SUFFIX;
let backupFile = bookmarksBackupDir.clone();
backupFile.append(backupFilename);
backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
do_check_true(backupFile.exists());
}
// Fake backups are created backwards to ensure we won't consider file
// creation time.
// Create fake backups for the newest dates.
for (let i = dates.length - 1; i >= 0; i--) {
let backupFilename = PREFIX + dates[i] + SUFFIX;
let backupFile = bookmarksBackupDir.clone();
backupFile.append(backupFilename);
backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));
do_log_info("Creating fake backup " + backupFile.leafName);
if (!backupFile.exists())
do_throw("Unable to create fake backup " + backupFile.leafName);
}
// Replace PlacesUtils getFormattedString so that it will return the localized
// string we want.
PlacesUtils.getFormattedString = function (aKey, aValue) {
return LOCALIZED_PREFIX + aValue;
}
yield PlacesBackups.create(NUMBER_OF_BACKUPS);
// Add today's backup.
dates.push(dateObj.toLocaleFormat("%Y-%m-%d"));
yield PlacesBackups.create(Math.floor(dates.length/2));
// Add today's backup.
dates.push(dateObj.toLocaleFormat("%Y-%m-%d"));
// Check backups.
for (var i = 0; i < dates.length; i++) {
let backupFilename;
let shouldExist;
let backupFile;
if (i > Math.floor(dates.length/2)) {
let files = bookmarksBackupDir.directoryEntries;
let rx = new RegExp("^" + PREFIX + dates[i] + "(_[0-9]+){0,1}" + SUFFIX + "$");
while (files.hasMoreElements()) {
let entry = files.getNext().QueryInterface(Ci.nsIFile);
if (entry.leafName.match(rx)) {
backupFilename = entry.leafName;
backupFile = entry;
break;
}
// Check backups. We have 11 dates but we the max number is 10 so the
// oldest backup should have been removed.
for (let i = 0; i < dates.length; i++) {
let backupFilename;
let shouldExist;
let backupFile;
if (i > 0) {
let files = bookmarksBackupDir.directoryEntries;
let rx = new RegExp("^" + PREFIX + dates[i] + "(_[0-9]+){0,1}" + SUFFIX + "$");
while (files.hasMoreElements()) {
let entry = files.getNext().QueryInterface(Ci.nsIFile);
if (entry.leafName.match(rx)) {
backupFilename = entry.leafName;
backupFile = entry;
break;
}
shouldExist = true;
}
else {
backupFilename = LOCALIZED_PREFIX + dates[i] + SUFFIX;
backupFile = bookmarksBackupDir.clone();
backupFile.append(backupFilename);
shouldExist = false;
}
if (backupFile.exists() != shouldExist)
do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename);
shouldExist = true;
}
// Cleanup backups folder.
// XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens
// on WIN XP.
let files = bookmarksBackupDir.directoryEntries;
while (files.hasMoreElements()) {
let entry = files.getNext().QueryInterface(Ci.nsIFile);
entry.remove(false);
else {
backupFilename = PREFIX + dates[i] + SUFFIX;
backupFile = bookmarksBackupDir.clone();
backupFile.append(backupFilename);
shouldExist = false;
}
do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements());
if (backupFile.exists() != shouldExist)
do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename);
}
do_test_finished();
});
}
// Cleanup backups folder.
// XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens
// on WIN XP.
let files = bookmarksBackupDir.directoryEntries;
while (files.hasMoreElements()) {
let entry = files.getNext().QueryInterface(Ci.nsIFile);
entry.remove(false);
}
do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements());
});

View File

@ -24,13 +24,6 @@ finduri-MonthYear=%1$S %2$S
# This is used to generate local files container when history is grouped by site
localhost=(local files)
# LOCALIZATION NOTE (bookmarksArchiveFilename):
# Do not change this string! It's used only to
# detect older localized bookmark archives from
# before bug 445704 was fixed. It will be removed
# in a subsequent release.
bookmarksArchiveFilename=bookmarks-%S.json
# LOCALIZATION NOTE
# The string is used for showing file size of each backup in the "fileRestorePopup" popup
# %1$S is the file size