Bug 1772943 - Port osfile.jsm usage to IOUtils in toolkit/mozapps/ r=Gijs,mixedpuppy

Differential Revision: https://phabricator.services.mozilla.com/D148967
This commit is contained in:
Barret Rennie 2022-07-10 02:04:13 +00:00
Родитель 05ea948f3c
Коммит 8a72738ef5
7 изменённых файлов: 47 добавлений и 139 удалений

Просмотреть файл

@ -4,10 +4,6 @@ const { AddonTestUtils } = ChromeUtils.import(
"resource://testing-common/AddonTestUtils.jsm"
);
const { ProductAddonCheckerTestUtils } = ChromeUtils.import(
"resource://gre/modules/addons/ProductAddonChecker.jsm"
);
AddonTestUtils.initMochitest(this);
const testServer = AddonTestUtils.createHttpServer();
@ -69,9 +65,9 @@ add_task(async function test_management_install() {
},
});
let themeXPIFileHash = await ProductAddonCheckerTestUtils.computeHash(
"sha256",
themeXPIFile.path
let themeXPIFileHash = await IOUtils.computeHexDigest(
themeXPIFile.path,
"sha256"
);
const otherXPIFile = AddonTestUtils.createTempWebExtensionFile({

Просмотреть файл

@ -947,10 +947,7 @@ async function test_checkForAddons_installAddon(
let data = "e~=0.5772156649";
let zipFile = createNewZipFile(zipFileName, data);
let hashFunc = "sha256";
let expectedDigest = await ProductAddonCheckerTestUtils.computeHash(
hashFunc,
zipFile.path
);
let expectedDigest = await IOUtils.computeHexDigest(zipFile.path, hashFunc);
let fileSize = zipFile.fileSize;
if (wantInstallReject) {
fileSize = 1;

Просмотреть файл

@ -28,7 +28,6 @@ const { XPCOMUtils } = ChromeUtils.import(
const { EventEmitter } = ChromeUtils.import(
"resource://gre/modules/EventEmitter.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const lazy = {};
@ -658,13 +657,14 @@ var AddonTestUtils = {
let loadedData = {};
let fileSuffix = "extensions";
const fileName = `${prefix}-${fileSuffix}.json`;
let jsonStr = await OS.File.read(OS.Path.join(dir.path, fileName), {
encoding: "UTF-8",
}).catch(() => {});
if (jsonStr) {
try {
loadedData[fileSuffix] = await IOUtils.readJSON(
PathUtils.join(dir.path, fileName)
);
this.info(`Loaded ${fileName}`);
loadedData[fileSuffix] = JSON.parse(jsonStr);
}
} catch (e) {}
return this.loadBlocklistRawData(loadedData);
},
@ -934,32 +934,6 @@ var AddonTestUtils = {
this.addonsList = new AddonsList(this.addonStartup);
},
/**
* Recursively create all directories up to and including the given
* path, if they do not exist.
*
* @param {string} path The path of the directory to create.
* @returns {Promise} Resolves when all directories have been created.
*/
recursiveMakeDir(path) {
let paths = [];
for (
let lastPath;
path != lastPath;
lastPath = path, path = OS.Path.dirname(path)
) {
paths.push(path);
}
return Promise.all(
paths
.reverse()
.map(path =>
OS.File.makeDir(path, { ignoreExisting: true }).catch(() => {})
)
);
},
/**
* Writes the given data to a file in the given zip file.
*
@ -1009,7 +983,7 @@ var AddonTestUtils = {
},
async promiseWriteFilesToZip(zip, files, flags) {
await this.recursiveMakeDir(OS.Path.dirname(zip));
await IOUtils.makeDirectory(PathUtils.parent(zip));
this.writeFilesToZip(zip, files, flags);
@ -1017,7 +991,7 @@ var AddonTestUtils = {
},
async promiseWriteFilesToDir(dir, files) {
await this.recursiveMakeDir(dir);
await IOUtils.makeDirectory(dir);
for (let [path, data] of Object.entries(files)) {
path = path.split("/");
@ -1026,21 +1000,19 @@ var AddonTestUtils = {
// Create parent directories, if necessary.
let dirPath = dir;
for (let subDir of path) {
dirPath = OS.Path.join(dirPath, subDir);
await OS.File.makeDir(dirPath, { ignoreExisting: true });
dirPath = PathUtils.join(dirPath, subDir);
await PathUtils.makeDirectory(dirPath);
}
const leafPath = PathUtils.join(dirPath, leafName);
if (
typeof data == "object" &&
ChromeUtils.getClassName(data) == "Object"
) {
data = JSON.stringify(data);
await IOUtils.writeJSON(leafPath, data);
} else if (typeof data == "string") {
await IOUtils.writeUTF8(leafPath, data);
}
if (typeof data == "string") {
data = new TextEncoder("utf-8").encode(data);
}
await OS.File.writeAtomic(OS.Path.join(dirPath, leafName), data);
}
return nsFile(dir);
@ -1048,12 +1020,12 @@ var AddonTestUtils = {
promiseWriteFilesToExtension(dir, id, files, unpacked = this.testUnpacked) {
if (unpacked) {
let path = OS.Path.join(dir, id);
let path = PathUtils.join(dir, id);
return this.promiseWriteFilesToDir(path, files);
}
let xpi = OS.Path.join(dir, `${id}.xpi`);
let xpi = PathUtils.join(dir, `${id}.xpi`);
return this.promiseWriteFilesToZip(xpi, files);
},
@ -1269,18 +1241,16 @@ var AddonTestUtils = {
async promiseSetExtensionModifiedTime(path, time) {
await IOUtils.setModificationTime(path, time);
let iterator = new OS.File.DirectoryIterator(path);
const children = await IOUtils.getChildren(path);
try {
await iterator.forEach(entry => {
return this.promiseSetExtensionModifiedTime(entry.path, time);
});
await Promise.all(
children.map(entry => this.promiseSetExtensionModifiedTime(entry, time))
);
} catch (ex) {
if (ex instanceof OS.File.Error) {
if (DOMException.isInstance(ex)) {
return;
}
throw ex;
} finally {
iterator.close().catch(() => {});
}
},
@ -1793,10 +1763,7 @@ var AddonTestUtils = {
this.tempXPIs.push(file);
let manifest = Services.io.newFileURI(file);
await OS.File.writeAtomic(
file.path,
new TextEncoder().encode(JSON.stringify(data))
);
await IOUtils.writeJSON(file.path, data);
this.overrideEntry = lazy.aomStartup.registerChrome(manifest, [
[
"override",

Просмотреть файл

@ -13,7 +13,6 @@ const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
const { CertUtils } = ChromeUtils.import(
"resource://gre/modules/CertUtils.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const lazy = {};
@ -37,8 +36,6 @@ logger.manageLevelFromPref("extensions.logging.productaddons.level");
* ensure that we fail cleanly in such case.
*/
const TIMEOUT_DELAY_MS = 20000;
// How much of a file to read into memory at a time for hashing
const HASH_CHUNK_SIZE = 8192;
/**
* Gets the status of an XMLHttpRequest either directly or from its underlying
@ -425,13 +422,13 @@ function downloadFile(url, options = { httpsOnlyNoUpgrade: false }) {
return;
}
(async function() {
let f = await OS.File.openUnique(
OS.Path.join(OS.Constants.Path.tmpDir, "tmpaddon")
const path = await IOUtils.createUniqueFile(
PathUtils.osTempDir,
"tmpaddon"
);
let path = f.path;
logger.info(`Downloaded file will be saved to ${path}`);
await f.file.close();
await OS.File.writeAtomic(path, new Uint8Array(sr.response));
await IOUtils.write(path, new Uint8Array(sr.response));
return path;
})().then(resolve, reject);
};
@ -467,51 +464,6 @@ function downloadFile(url, options = { httpsOnlyNoUpgrade: false }) {
});
}
/**
* Convert a string containing binary values to hex.
*/
function binaryToHex(input) {
let result = "";
for (let i = 0; i < input.length; ++i) {
let hex = input.charCodeAt(i).toString(16);
if (hex.length == 1) {
hex = "0" + hex;
}
result += hex;
}
return result;
}
/**
* Calculates the hash of a file.
*
* @param hashFunction
* The type of hash function to use, must be supported by nsICryptoHash.
* @param path
* The path of the file to hash.
* @return a promise that resolves to hash of the file or rejects with a JS
* exception in case of error.
*/
var computeHash = async function(hashFunction, path) {
let file = await OS.File.open(path, { existing: true, read: true });
try {
let hasher = Cc["@mozilla.org/security/hash;1"].createInstance(
Ci.nsICryptoHash
);
hasher.initWithString(hashFunction);
let bytes;
do {
bytes = await file.read(HASH_CHUNK_SIZE);
hasher.update(bytes, bytes.length);
} while (bytes.length == HASH_CHUNK_SIZE);
return binaryToHex(hasher.finish(false));
} finally {
await file.close();
}
};
/**
* Verifies that a downloaded file matches what was expected.
*
@ -525,7 +477,7 @@ var computeHash = async function(hashFunction, path) {
*/
var verifyFile = async function(properties, path) {
if (properties.size !== undefined) {
let stat = await OS.File.stat(path);
let stat = await IOUtils.stat(path);
if (stat.size != properties.size) {
throw new Error(
"Downloaded file was " +
@ -539,7 +491,7 @@ var verifyFile = async function(properties, path) {
if (properties.hashFunction !== undefined) {
let expectedDigest = properties.hashValue.toLowerCase();
let digest = await computeHash(properties.hashFunction, path);
let digest = await IOUtils.computeHexDigest(path, properties.hashFunction);
if (digest != expectedDigest) {
throw new Error(
"Hash was `" + digest + "` but expected `" + expectedDigest + "`."
@ -610,7 +562,7 @@ const ProductAddonChecker = {
await verifyFile(addon, path);
return path;
} catch (e) {
await OS.File.remove(path);
await IOUtils.remove(path);
throw e;
}
},
@ -618,8 +570,6 @@ const ProductAddonChecker = {
// For test use only.
const ProductAddonCheckerTestUtils = {
computeHash,
/**
* Used to override ServiceRequest calls with a mock request.
* @param mockRequest The mocked ServiceRequest object.

Просмотреть файл

@ -21,8 +21,6 @@ const {
ExtensionUtils: { promiseEvent, promiseObserved },
} = ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
AddonTestUtils.initMochitest(this);
// The response to the discovery API, as documented at:
@ -44,7 +42,7 @@ async function readAPIResponseFixture(
amoTestHost,
fixtureFilePath = DISCOAPI_DEFAULT_FIXTURE
) {
let apiText = await OS.File.read(fixtureFilePath, { encoding: "utf-8" });
let apiText = await IOUtils.readUTF8(fixtureFilePath);
apiText = apiText.replace(/\bhttps?:\/\/[^"]+(?=")/g, url => {
try {
url = new URL(url);

Просмотреть файл

@ -1,5 +1,4 @@
let { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
let { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
let { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
const RELATIVE_PATH = "browser/toolkit/mozapps/extensions/test/xpinstall";
@ -51,7 +50,7 @@ function handleRequest(aRequest, aResponse) {
LOG("Completing download");
try {
// Doesn't seem to be a sane way to read using OS.File and write to an
// Doesn't seem to be a sane way to read using IOUtils and write to an
// nsIOutputStream so here we are.
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.initWithPath(xpiFile);
@ -80,18 +79,17 @@ function handleRequest(aRequest, aResponse) {
aResponse.processAsync();
OS.File.getCurrentDirectory().then(dir => {
xpiFile = OS.Path.join(dir, ...RELATIVE_PATH.split("/"), params.file);
const dir = Services.dirsvc.get("CurWorkD", Ci.nsIFile).path;
xpiFile = PathUtils.join(dir, ...RELATIVE_PATH.split("/"), params.file);
LOG("Starting slow download of " + xpiFile);
OS.File.stat(xpiFile).then(info => {
IOUtils.stat(xpiFile).then(info => {
aResponse.setHeader("Content-Type", "binary/octet-stream");
aResponse.setHeader("Content-Length", info.size.toString());
LOG("Download paused");
waitForComplete.then(complete_download);
});
});
} else if (params.continue) {
dump(
"slowinstall.sjs: Received signal to complete all current downloads.\n"

Просмотреть файл

@ -18,6 +18,8 @@ module.exports = {
ContentTask: false,
ContentTaskUtils: false,
EventUtils: false,
IOUtils: false,
PathUtils: false,
PromiseDebugging: false,
SpecialPowers: false,
TestUtils: false,