Skip to content

Commit

Permalink
Bug 1649597, remove OS.File usage from CrashManager.jsm r=barret
Browse files Browse the repository at this point in the history
  • Loading branch information
Emma Malysz committed Dec 11, 2020
1 parent 755e251 commit c2d6d67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
76 changes: 30 additions & 46 deletions toolkit/components/crashes/CrashManager.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const myScope = this;

ChromeUtils.import("resource://gre/modules/Log.jsm", this);
ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
const { PromiseUtils } = ChromeUtils.import(
"resource://gre/modules/PromiseUtils.jsm"
);
Expand Down Expand Up @@ -217,23 +216,23 @@ CrashManager.prototype = Object.freeze({

_lazyGetDir(field, path, leaf) {
delete this[field];
let value = OS.Path.join(path, leaf);
let value = PathUtils.join(path, leaf);
Object.defineProperty(this, field, { value });
return value;
},

get _crDir() {
return this._lazyGetDir(
"_crDir",
OS.Constants.Path.userApplicationDataDir,
Services.dirsvc.get("UAppData", Ci.nsIFile).path,
"Crash Reports"
);
},

get _storeDir() {
return this._lazyGetDir(
"_storeDir",
OS.Constants.Path.profileDir,
Services.dirsvc.get("ProfD", Ci.nsIFile).path,
"crashes"
);
},
Expand All @@ -249,8 +248,8 @@ CrashManager.prototype = Object.freeze({
get _eventsDirs() {
delete this._eventsDirs;
let value = [
OS.Path.join(this._crDir, "events"),
OS.Path.join(this._storeDir, "events"),
PathUtils.join(this._crDir, "events"),
PathUtils.join(this._storeDir, "events"),
];
Object.defineProperty(this, "_eventsDirs", { value });
return value;
Expand Down Expand Up @@ -364,7 +363,7 @@ CrashManager.prototype = Object.freeze({
);
}
} catch (ex) {
if (ex instanceof OS.File.Error) {
if (ex instanceof DOMException) {
this._log.warn("I/O error reading " + entry.path, ex);
} else {
// We should never encounter an exception. This likely represents
Expand All @@ -389,7 +388,7 @@ CrashManager.prototype = Object.freeze({

for (let path of deletePaths) {
try {
await OS.File.remove(path);
await IOUtils.remove(path);
} catch (ex) {
this._log.warn("Error removing event file (" + path + ")", ex);
}
Expand Down Expand Up @@ -614,7 +613,7 @@ CrashManager.prototype = Object.freeze({
// See docs/crash-events.rst for the file format specification.
_processEventFile(entry) {
return (async () => {
let data = await OS.File.read(entry.path);
let data = await IOUtils.read(entry.path);
let store = await this._getStore();

let decoder = new TextDecoder();
Expand Down Expand Up @@ -787,39 +786,25 @@ CrashManager.prototype = Object.freeze({
*/
_getDirectoryEntries(path, re) {
return (async function() {
try {
await OS.File.stat(path);
} catch (ex) {
if (!(ex instanceof OS.File.Error) || !ex.becauseNoSuchFile) {
throw ex;
}
return [];
}

let it = new OS.File.DirectoryIterator(path);
let children = await IOUtils.getChildren(path);
let entries = [];

try {
await it.forEach((entry, index, it) => {
if (entry.isDir) {
return undefined;
}

let match = re.exec(entry.name);
if (!match) {
return undefined;
}
for (const entry of children) {
let stat = await IOUtils.stat(entry);
if (stat.type == "directory") {
continue;
}

return OS.File.stat(entry.path).then(info => {
entries.push({
path: entry.path,
id: match[1],
date: info.lastModificationDate,
});
});
let filename = PathUtils.filename(entry);
let match = re.exec(filename);
if (!match) {
continue;
}
entries.push({
path: entry,
id: match[1],
date: stat.lastModified,
});
} finally {
it.close();
}

entries.sort((a, b) => {
Expand All @@ -838,9 +823,8 @@ CrashManager.prototype = Object.freeze({
return (this._getStoreTask = (async () => {
try {
if (!this._store) {
await OS.File.makeDir(this._storeDir, {
ignoreExisting: true,
unixMode: OS.Constants.libc.S_IRWXU,
await IOUtils.makeDirectory(this._storeDir, {
permissions: 0o700,
});

let store = new CrashStore(
Expand Down Expand Up @@ -951,7 +935,7 @@ function CrashStore(storeDir, telemetrySizeKey) {
this._storeDir = storeDir;
this._telemetrySizeKey = telemetrySizeKey;

this._storePath = OS.Path.join(storeDir, "store.json.mozlz4");
this._storePath = PathUtils.join(storeDir, "store.json.mozlz4");

// Holds the read data from disk.
this._data = null;
Expand Down Expand Up @@ -991,7 +975,7 @@ CrashStore.prototype = Object.freeze({

try {
let decoder = new TextDecoder();
let data = await OS.File.read(this._storePath, { compression: "lz4" });
let data = await IOUtils.read(this._storePath, { decompress: true });
data = JSON.parse(decoder.decode(data));

if (data.corruptDate) {
Expand Down Expand Up @@ -1070,7 +1054,7 @@ CrashStore.prototype = Object.freeze({
}
} catch (ex) {
// Missing files (first use) are allowed.
if (!(ex instanceof OS.File.Error) || !ex.becauseNoSuchFile) {
if (!(ex instanceof DOMException) || ex.name != "NotFoundError") {
// If we can't load for any reason, mark a corrupt date in the instance
// and swallow the error.
//
Expand Down Expand Up @@ -1138,9 +1122,9 @@ CrashStore.prototype = Object.freeze({

let encoder = new TextEncoder();
let data = encoder.encode(JSON.stringify(normalized));
let size = await OS.File.writeAtomic(this._storePath, data, {
let size = await IOUtils.write(this._storePath, data, {
tmpPath: this._storePath + ".tmp",
compression: "lz4",
compress: true,
});
if (this._telemetrySizeKey) {
Services.telemetry.getHistogramById(this._telemetrySizeKey).add(size);
Expand Down
9 changes: 4 additions & 5 deletions toolkit/components/crashes/tests/xpcshell/test_crash_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var { CrashManager, CrashStore, dateToDays } = ChromeUtils.import(
"resource://gre/modules/CrashManager.jsm",
null
);
ChromeUtils.import("resource://gre/modules/osfile.jsm", this);

const DUMMY_DATE = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
DUMMY_DATE.setMilliseconds(0);
Expand Down Expand Up @@ -39,9 +38,9 @@ var STORE_DIR_COUNT = 0;
function getStore() {
return (async function() {
let storeDir = do_get_tempdir().path;
storeDir = OS.Path.join(storeDir, "store-" + STORE_DIR_COUNT++);
storeDir = PathUtils.join(storeDir, "store-" + STORE_DIR_COUNT++);

await OS.File.makeDir(storeDir, { unixMode: OS.Constants.libc.S_IRWXU });
await IOUtils.makeDirectory(storeDir, { permissions: 0o700 });

let s = new CrashStore(storeDir);
await s.load();
Expand All @@ -51,7 +50,7 @@ function getStore() {
}

add_task(async function test_constructor() {
let s = new CrashStore("/some/path");
let s = new CrashStore(do_get_tempdir().path);
Assert.ok(s instanceof CrashStore);
});

Expand Down Expand Up @@ -120,7 +119,7 @@ add_task(async function test_corrupt_json() {
let s = await getStore();

let buffer = new TextEncoder().encode("{bad: json-file");
await OS.File.writeAtomic(s._storePath, buffer, { compression: "lz4" });
await IOUtils.write(s._storePath, buffer, { compress: true });

await s.load();
Assert.ok(s.corruptDate, "Corrupt date is defined.");
Expand Down

0 comments on commit c2d6d67

Please sign in to comment.