Bug 658544 - Add support for cleanup of profile folder. r=lina

Differential Revision: https://phabricator.services.mozilla.com/D70644

--HG--
extra : moz-landing-system : lando
This commit is contained in:
jayati 2020-04-15 20:38:49 +00:00
parent 7870c996a2
commit c758dad718
4 changed files with 99 additions and 0 deletions

View File

@ -5,6 +5,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const BYTES_PER_MEBIBYTE = 1048576;
const MS_PER_DAY = 86400000;
// Threshold value for removeOldCorruptDBs.
// Corrupt DBs older than this value are removed.
const CORRUPT_DB_RETAIN_DAYS = 14;
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
@ -44,6 +48,7 @@ var PlacesDBUtils = {
this._refreshUI,
this.originFrecencyStats,
this.incrementalVacuum,
this.removeOldCorruptDBs,
];
let telemetryStartTime = Date.now();
let taskStatusMap = await PlacesDBUtils.runTasks(tasks);
@ -1260,6 +1265,56 @@ var PlacesDBUtils = {
}
},
/**
* Remove old and useless places.sqlite.corrupt files.
*
* @resolves to an array of logs for this task.
*
*/
async removeOldCorruptDBs() {
let logs = [];
logs.push(
"> Cleanup profile from places.sqlite.corrupt files older than " +
CORRUPT_DB_RETAIN_DAYS +
" days."
);
let re = /^places\.sqlite(-\d)?\.corrupt$/;
let currentTime = Date.now();
let iterator = new OS.File.DirectoryIterator(OS.Constants.Path.profileDir);
try {
await iterator.forEach(async entry => {
let lastModificationDate;
if (!entry.isDir && !entry.isSymLink && re.test(entry.name)) {
if ("winLastWriteDate" in entry) {
// Under Windows, additional information allows us to sort files immediately
// without having to perform additional I/O.
lastModificationDate = entry.winLastWriteDate.getTime();
} else {
// Under other OSes, we need to call OS.File.stat
let info = await OS.File.stat(entry.path);
lastModificationDate = info.lastModificationDate.getTime();
}
try {
// Convert milliseconds to days.
let days = Math.ceil(
(currentTime - lastModificationDate) / MS_PER_DAY
);
if (days >= CORRUPT_DB_RETAIN_DAYS || days < 0) {
await OS.File.remove(entry.path);
}
} catch (error) {
logs.push("Could not remove file: " + entry.path, error);
}
}
});
} catch (error) {
logs.push("removeOldCorruptDBs failed", error);
} finally {
iterator.close();
}
return logs;
},
/**
* Runs a list of tasks, returning a Map when done.
*

View File

@ -41,6 +41,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
TestUtils: "resource://testing-common/TestUtils.jsm",
AppConstants: "resource://gre/modules/AppConstants.jsm",
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
PlacesDBUtils: "resource://gre/modules/PlacesDBUtils.jsm",
});
XPCOMUtils.defineLazyGetter(this, "SMALLPNG_DATA_URI", function() {

View File

@ -0,0 +1,42 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const TEMP_FILES_TO_CREATE = 5;
const LAST_MODIFICATION_DAY = [5, 10, 15, 20, 25];
const TEST_CURRENT_TIME = Date.now();
const MS_PER_DAY = 86400000;
const RETAIN_DAYS = 14;
async function createfiles() {
for (let i = 0; i < TEMP_FILES_TO_CREATE; i++) {
let setTime = TEST_CURRENT_TIME;
setTime -= LAST_MODIFICATION_DAY[i] * MS_PER_DAY;
let fileName = "places.sqlite" + (i > 0 ? "-" + i : "") + ".corrupt";
let filePath = OS.Path.join(OS.Constants.Path.profileDir, fileName);
await OS.File.writeAtomic(filePath, "test-file-delete-me", {
tmpPath: fileName + ".tmp",
});
Assert.ok(await OS.File.exists(filePath), "file created: " + filePath);
await OS.File.setDates(filePath, setTime, setTime);
}
}
add_task(async function removefiles() {
await createfiles();
await PlacesDBUtils.runTasks([PlacesDBUtils.removeOldCorruptDBs]);
for (let i = 0; i < TEMP_FILES_TO_CREATE; i++) {
let fileName = "places.sqlite" + (i > 0 ? "-" + i : "") + ".corrupt";
let filePath = OS.Path.join(OS.Constants.Path.profileDir, fileName);
if (LAST_MODIFICATION_DAY[i] >= RETAIN_DAYS) {
Assert.ok(
!(await OS.File.exists(filePath)),
"Old corrupt file has been removed" + filePath
);
} else {
Assert.ok(
await OS.File.exists(filePath),
"Files that are not old are not removed" + filePath
);
}
}
});

View File

@ -88,6 +88,7 @@ support-files = noRoot.sqlite
[test_origins_parsing.js]
[test_pageGuid_bookmarkGuid.js]
[test_frecency_observers.js]
[test_PlacesDBUtils_removeOldCorruptDBs.js]
[test_placeURIs.js]
[test_PlacesUtils_invalidateCachedGuidFor.js]
[test_PlacesUtils_invalidateCachedGuids.js]