Bug 1772942 - Port osfile.jsm usage to IOUtils in toolkit/modules/ r=Gijs,preferences-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D149304
This commit is contained in:
Barret Rennie 2022-09-02 20:00:04 +00:00
parent 30c7a556bf
commit 449d91d319
11 changed files with 71 additions and 120 deletions

View file

@ -16,7 +16,6 @@ const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
Log: "resource://gre/modules/Log.jsm",
OS: "resource://gre/modules/osfile.jsm",
});
// Define our prefs
@ -35,11 +34,6 @@ const LOGGER_NAME = "asanreporter";
let logger;
XPCOMUtils.defineLazyGetter(lazy, "asanDumpDir", () => {
let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
return lazy.OS.Path.join(profileDir.path, "asan");
});
const AsanReporter = {
init() {
if (this.initialized) {
@ -80,67 +74,33 @@ const AsanReporter = {
},
};
function processDirectory() {
let iterator = new lazy.OS.File.DirectoryIterator(lazy.asanDumpDir);
let results = [];
async function processDirectory() {
const asanDumpDir = PathUtils.join(PathUtils.profileDir, "asan");
const children = await IOUtils.getChildren(asanDumpDir);
// Scan the directory for any ASan logs that we haven't
// submitted yet. Store the filenames in an array so we
// can close the iterator early.
iterator
.forEach(entry => {
if (
entry.name.indexOf("ff_asan_log.") == 0 &&
!entry.name.includes("submitted")
) {
results.push(entry);
}
})
.then(
() => {
iterator.close();
logger.info("Processing " + results.length + " reports...");
const results = children.filter(
entry => entry.startsWith("ff_asan_log.") && !entry.includes("submitted")
);
// Sequentially submit all reports that we found. Note that doing this
// with Promise.all would not result in a sequential ordering and would
// cause multiple requests to be sent to the server at once.
let requests = Promise.resolve();
results.forEach(result => {
requests = requests.then(
// We return a promise here that already handles any submit failures
// so our chain is not interrupted if one of the reports couldn't
// be submitted for some reason.
() =>
submitReport(result.path).then(
() => {
logger.info("Successfully submitted " + result.path);
},
e => {
logger.error(
"Failed to submit " + result.path + ". Reason: " + e
);
}
)
);
});
logger.info(`Processing ${results.length} reports...`);
for (const result of results) {
try {
await submitReport(result);
logger.info(`Successfully submitted ${result.path}`);
} catch (e) {
logger.error(`Failed to submit ${result.path}. Reason: ${e}`);
}
}
requests.then(() => logger.info("Done processing reports."));
},
e => {
iterator.close();
logger.error("Error while iterating over report files: " + e);
}
);
logger.info("Done processing reports.");
}
function submitReport(reportFile) {
async function submitReport(reportFile) {
logger.info("Processing " + reportFile);
return lazy.OS.File.read(reportFile)
.then(submitToServer)
.then(() => {
// Mark as submitted only if we successfully submitted it to the server.
return lazy.OS.File.move(reportFile, reportFile + ".submitted");
});
const data = await IOUtils.read(reportFile);
await submitToServer(data);
// Mark as submitted only if we successfully submitted it to the server.
await IOUtils.move(reportFile, `${reportFile}.submitted`);
}
function submitToServer(data) {

View file

@ -5,6 +5,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
if (typeof Components !== "undefined") {
/* global OS */
Cc["@mozilla.org/net/osfileconstantsservice;1"]
.getService(Ci.nsIOSFileConstantsService)
.init();
}
/* exported LIBC, libc */
// This file is loaded into the same scope as subprocess_unix.jsm

View file

@ -5,6 +5,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
if (typeof Components !== "undefined") {
/* global OS */
Cc["@mozilla.org/net/osfileconstantsservice;1"]
.getService(Ci.nsIOSFileConstantsService)
.init();
}
/* exported LIBC, Win, createPipe, libc, win32 */
// This file is loaded into the same scope as subprocess_win.jsm

View file

@ -12,7 +12,6 @@
var EXPORTED_SYMBOLS = ["SubprocessImpl", "libc"];
const { ctypes } = ChromeUtils.import("resource://gre/modules/ctypes.jsm");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { BaseProcess, PromiseWorker } = ChromeUtils.import(
"resource://gre/modules/subprocess/subprocess_common.jsm"
);
@ -108,7 +107,7 @@ var SubprocessUnix = {
*getEnvironment() {
let environ;
if (OS.Constants.Sys.Name == "Darwin") {
if (Services.appinfo.OS === "Darwin") {
environ = libc._NSGetEnviron().contents;
} else {
environ = libc.environ;
@ -142,16 +141,16 @@ var SubprocessUnix = {
},
isExecutableFile: async function isExecutable(path) {
if (!OS.Path.split(path).absolute) {
if (!PathUtils.isAbsolute(path)) {
return false;
}
try {
let info = await OS.File.stat(path);
let info = await IOUtils.stat(path);
// FIXME: We really want access(path, X_OK) here, but OS.File does not
// FIXME: We really want access(path, X_OK) here, but IOUtils does not
// support it.
return !info.isDir && info.unixMode & 0o111;
return info.type !== "directory" && info.permissions & 0o111;
} catch (e) {
return false;
}
@ -174,8 +173,7 @@ var SubprocessUnix = {
* @returns {Promise<string>}
*/
async pathSearch(bin, environment) {
let split = OS.Path.split(bin);
if (split.absolute) {
if (PathUtils.isAbsolute(bin)) {
if (await this.isExecutableFile(bin)) {
return bin;
}
@ -192,7 +190,7 @@ var SubprocessUnix = {
}
for (let dir of dirs) {
let path = OS.Path.join(dir, bin);
let path = PathUtils.join(dir, bin);
if (await this.isExecutableFile(path)) {
return path;

View file

@ -15,7 +15,6 @@ const { AppConstants } = ChromeUtils.import(
"resource://gre/modules/AppConstants.jsm"
);
const { ctypes } = ChromeUtils.import("resource://gre/modules/ctypes.jsm");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
@ -119,13 +118,15 @@ var SubprocessWin = {
},
async isExecutableFile(path) {
if (!OS.Path.split(path).absolute) {
if (!PathUtils.isAbsolute(path)) {
return false;
}
try {
let info = await OS.File.stat(path);
return !(info.isDir || info.isSymlink);
let info = await IOUtils.stat(path);
// On Windows, a FileType of "other" indicates it is a reparse point
// (i.e., a link).
return info.type !== "directory" && info.type !== "other";
} catch (e) {
return false;
}
@ -148,8 +149,7 @@ var SubprocessWin = {
* @returns {Promise<string>}
*/
async pathSearch(bin, environment) {
let split = OS.Path.split(bin);
if (split.absolute) {
if (PathUtils.isAbsolute(bin)) {
if (await this.isExecutableFile(bin)) {
return bin;
}
@ -170,7 +170,7 @@ var SubprocessWin = {
}
for (let dir of dirs) {
let path = OS.Path.join(dir, bin);
let path = PathUtils.join(dir, bin);
if (await this.isExecutableFile(path)) {
return path;

View file

@ -9,6 +9,5 @@ const { AppConstants } = ChromeUtils.import(
// eslint-disable-next-line no-unused-vars
XPCOMUtils.defineLazyModuleGetters(this, {
OS: "resource://gre/modules/osfile.jsm",
Subprocess: "resource://gre/modules/Subprocess.jsm",
});

View file

@ -35,8 +35,8 @@ let readAll = async function(pipe) {
add_task(async function setup() {
PYTHON = await Subprocess.pathSearch(env.get("PYTHON"));
PYTHON_BIN = OS.Path.basename(PYTHON);
PYTHON_DIR = OS.Path.dirname(PYTHON);
PYTHON_BIN = PathUtils.filename(PYTHON);
PYTHON_DIR = PathUtils.parent(PYTHON);
});
add_task(async function test_subprocess_io() {
@ -527,11 +527,8 @@ add_task(async function test_subprocess_pathSearch() {
});
add_task(async function test_subprocess_workdir() {
let procDir = await OS.File.getCurrentDirectory();
let tmpDirFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
tmpDirFile.initWithPath(OS.Constants.Path.tmpDir);
tmpDirFile.normalize();
let tmpDir = tmpDirFile.path;
let procDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile).path;
let tmpDir = PathUtils.normalize(PathUtils.osTempDir);
notEqual(
procDir,
@ -571,13 +568,6 @@ add_task(async function test_subprocess_workdir() {
tmpDir,
"Process should launch in the directory specified in `workdir`"
);
dir = await OS.File.getCurrentDirectory();
equal(
dir,
procDir,
"`workdir` should not change the working directory of the current process"
);
});
add_task(async function test_subprocess_term() {

View file

@ -6,11 +6,11 @@ let envService = Cc["@mozilla.org/process/environment;1"].getService(
const PYTHON = envService.get("PYTHON");
const PYTHON_BIN = OS.Path.basename(PYTHON);
const PYTHON_DIR = OS.Path.dirname(PYTHON);
const PYTHON_BIN = PathUtils.filename(PYTHON);
const PYTHON_DIR = PathUtils.parent(PYTHON);
const DOES_NOT_EXIST = OS.Path.join(
OS.Constants.Path.tmpDir,
const DOES_NOT_EXIST = PathUtils.join(
PathUtils.osTempDir,
"ThisPathDoesNotExist"
);

View file

@ -10,7 +10,6 @@ ChromeUtils.defineModuleGetter(
"AsyncShutdown",
"resource://gre/modules/AsyncShutdown.jsm"
);
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(
this,
"JSONFile",
@ -150,7 +149,7 @@ add_task(async function test_load_string_predefined() {
let string = '{"number":123,"string":"test","object":{"prop1":1,"prop2":2}}';
await OS.File.writeAtomic(store.path, new TextEncoder().encode(string), {
await IOUtils.writeUTF8(store.path, string, {
tmpPath: store.path + ".tmp",
});
@ -169,15 +168,15 @@ add_task(async function test_load_string_malformed() {
let string = '{"number":123,"string":"test","object":{"prop1":1,';
await OS.File.writeAtomic(store.path, new TextEncoder().encode(string), {
await IOUtils.writeUTF8(store.path, string, {
tmpPath: store.path + ".tmp",
});
await store.load();
// A backup file should have been created.
Assert.ok(await OS.File.exists(store.path + ".corrupt"));
await OS.File.remove(store.path + ".corrupt");
Assert.ok(await IOUtils.exists(store.path + ".corrupt"));
await IOUtils.remove(store.path + ".corrupt");
// The store should be ready to accept new data.
Assert.ok(store.dataReady);
@ -195,15 +194,15 @@ add_task(async function test_load_string_malformed_sync() {
let string = '{"number":123,"string":"test","object":{"prop1":1,';
await OS.File.writeAtomic(store.path, new TextEncoder().encode(string), {
await IOUtils.writeUTF8(store.path, string, {
tmpPath: store.path + ".tmp",
});
store.ensureDataReady();
// A backup file should have been created.
Assert.ok(await OS.File.exists(store.path + ".corrupt"));
await OS.File.remove(store.path + ".corrupt");
Assert.ok(await IOUtils.exists(store.path + ".corrupt"));
await IOUtils.remove(store.path + ".corrupt");
// The store should be ready to accept new data.
Assert.ok(store.dataReady);
@ -217,11 +216,9 @@ add_task(async function test_overwrite_data() {
let string = `{"number":456,"string":"tset","object":{"prop1":3,"prop2":4}}`;
await OS.File.writeAtomic(
storeForSave.path,
new TextEncoder().encode(string),
{ tmpPath: storeForSave.path + ".tmp" }
);
await IOUtils.writeUTF8(storeForSave.path, string, {
tmpPath: storeForSave.path + ".tmp",
});
Assert.ok(!storeForSave.dataReady);
storeForSave.data = TEST_DATA;

View file

@ -1,6 +1,5 @@
"use strict";
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Region } = ChromeUtils.import("resource://gre/modules/Region.jsm");
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
const { TestUtils } = ChromeUtils.import(
@ -11,12 +10,6 @@ XPCOMUtils.defineLazyModuleGetters(this, {
RegionTestUtils: "resource://testing-common/RegionTestUtils.jsm",
});
async function readFile(file) {
let decoder = new TextDecoder();
let data = await OS.File.read(file.path);
return decoder.decode(data);
}
function setLocation(location) {
Services.prefs.setCharPref(
"geo.provider.network.url",
@ -25,7 +18,7 @@ function setLocation(location) {
}
async function stubMap(obj, path, fun) {
let map = await readFile(do_get_file(path));
let map = await IOUtils.readUTF8(do_get_file(path).path);
sinon.stub(obj, fun).resolves(JSON.parse(map));
}
@ -67,7 +60,9 @@ add_task(async function test_local_basic() {
});
add_task(async function test_mls_results() {
let data = await readFile(do_get_file("regions/mls-lookup-results.csv"));
let data = await IOUtils.readUTF8(
do_get_file("regions/mls-lookup-results.csv").path
);
for (const row of data.split("\n")) {
let [lat, lng, expectedRegion] = row.split(",");
setLocation({ lng: parseFloat(lng), lat: parseFloat(lat) });

View file

@ -5,8 +5,6 @@
do_get_profile();
ChromeUtils.import("resource://gre/modules/osfile.jsm");
// OS.File doesn't like to be first imported during shutdown
const { Sqlite } = ChromeUtils.import("resource://gre/modules/Sqlite.jsm");
const { AsyncShutdown } = ChromeUtils.import(
"resource://gre/modules/AsyncShutdown.jsm"