Skip to content

Commit 6c9f575

Browse files
authored
Merge pull request #115 from skhamis/update-os-file
Update areas using OS.File to the recommended IOUtils API
2 parents 6548758 + 9567c4d commit 6c9f575

4 files changed

Lines changed: 40 additions & 37 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"applications": {
1212
"gecko": {
1313
"id": "aboutsync@mhammond.github.com",
14-
"strict_min_version": "72.0a1"
14+
"strict_min_version": "114.0a1"
1515
}
1616
},
1717

src/CollectionsViewer.jsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
22

33
const React = require("react");
4-
const ReactDOM = require("react-dom");
5-
const DOM = require("react-dom-factories");
64
const { TabView, TabPanel } = require("./TabView");
75

8-
const { Fetching, ObjectInspector, ErrorDisplay, arrayCloneWithoutJank, importLocal } = require("./common");
6+
const { Fetching, ObjectInspector, ErrorDisplay, arrayCloneWithoutJank } = require("./common");
97
const { TableInspector } = require("./AboutSyncTableInspector");
108
const { AboutSyncRecordEditor } = require("./AboutSyncRecordEditor");
119
const { EngineActions } = require("./EngineActions");
@@ -15,10 +13,10 @@ const { BookmarkValidator } = require("./bookmarkValidator");
1513

1614
const validation = require("./validation");
1715

18-
const { Weave } = importLocal("resource://services-sync/main.js");
19-
const { AddonValidator } = importLocal("resource://services-sync/engines/addons.js");
20-
const { PasswordValidator } = importLocal("resource://services-sync/engines/passwords.js");
21-
const { FormValidator } = importLocal("resource://services-sync/engines/forms.js");
16+
const { Weave } = ChromeUtils.importESModule("resource://services-sync/main.sys.mjs");
17+
const { AddonValidator } = ChromeUtils.importESModule("resource://services-sync/engines/addons.sys.mjs");
18+
const { PasswordValidator } = ChromeUtils.importESModule("resource://services-sync/engines/passwords.sys.mjs");
19+
const { FormValidator } = ChromeUtils.importESModule("resource://services-sync/engines/forms.sys.mjs");
2220

2321
// takes an array of objects who have no real properties but have a bunch of
2422
// getters on their prototypes, and returns an array of new objects that contain
@@ -93,7 +91,7 @@ const collectionComponentBuilders = {
9391
},
9492

9593
async clients(provider, serverRecords) {
96-
const { fxAccounts: legacyfxAccounts, getFxAccountsSingleton } = importLocal("resource://gre/modules/FxAccounts.jsm");
94+
const { fxAccounts: legacyfxAccounts, getFxAccountsSingleton } = ChromeUtils.importESModule("resource://gre/modules/FxAccounts.sys.mjs");
9795
const fxAccounts = legacyfxAccounts || getFxAccountsSingleton();
9896
let fxaDevices = [];
9997
if (typeof fxAccounts.device == "object" && "recentDeviceList" in fxAccounts.device) {

src/config.jsx

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
88

99
const Services =
1010
globalThis.Services ||
11-
Cu.import("resource://gre/modules/Services.jsm").Services;
12-
const { Log } = importLocal("resource://gre/modules/Log.jsm");
13-
const { Preferences } = importLocal("resource://gre/modules/Preferences.jsm");
14-
const { FileUtils } = importLocal("resource://gre/modules/FileUtils.jsm");
15-
const { OS } = importLocal("resource://gre/modules/osfile.jsm");
11+
Cu.import("resource://gre/modules/Services.sys.mjs").Services;
12+
const { Log } = ChromeUtils.importESModule("resource://gre/modules/Log.sys.mjs");
13+
const { Preferences } = ChromeUtils.importESModule("resource://gre/modules/Preferences.sys.mjs");
14+
const { FileUtils } = ChromeUtils.importESModule("resource://gre/modules/FileUtils.sys.mjs");
1615
const { Downloads } = importLocal("resource://gre/modules/Downloads.jsm");
1716
const { Config } = importLocal("chrome://aboutsync/content/config.js");
1817
const { Panel, PanelGroup } = require("./panel");
19-
const { Weave } = importLocal("resource://services-sync/main.js");
18+
const { Weave } = ChromeUtils.importESModule("resource://services-sync/main.sys.mjs");
2019

2120
// For our "Sync Preferences" support.
2221
// A "log level" <select> element.
@@ -244,10 +243,10 @@ class LogFilesComponent extends React.Component {
244243

245244
async downloadFile(sourceFileURI, targetFilename) {
246245
let downloadsDir = await Downloads.getPreferredDownloadsDirectory();
247-
let filename = await OS.Path.join(downloadsDir, targetFilename);
246+
let filename = await PathUtils.join(downloadsDir, targetFilename);
248247
// need to nuke an existing file first.
249-
if ((await OS.File.exists(filename))) {
250-
await OS.File.remove(filename);
248+
if ((await IOUtils.exists(filename))) {
249+
await IOUtils.remove(filename);
251250
}
252251
let download = await Downloads.createDownload({
253252
source: sourceFileURI,
@@ -287,14 +286,17 @@ class LogFilesComponent extends React.Component {
287286
}
288287
});
289288

290-
let tmpFileInfo = await OS.File.openUnique(
291-
OS.Path.join(OS.Constants.Path.tmpDir, "aboutsync-combined-log.txt"))
289+
let combinedFilePath = PathUtils.join(PathUtils.tempDir, "aboutsync-combined-log.txt");
290+
// need to nuke an existing file first.
291+
if ((await IOUtils.exists(combinedFilePath))) {
292+
await IOUtils.remove(combinedFilePath);
293+
}
292294

293295
try {
294296
let textEncoder = new TextEncoder();
295297
// as in cstdio
296298
async function puts(string) {
297-
return tmpFileInfo.file.write(textEncoder.encode(string + "\n"));
299+
return await IOUtils.write(combinedFilePath, textEncoder.encode(string + "\n"), {mode: "appendOrCreate"});
298300
}
299301

300302
await puts(`Processing ${files.length} files`);
@@ -308,7 +310,7 @@ class LogFilesComponent extends React.Component {
308310
total: files.length
309311
}
310312
});
311-
let entireFile = await OS.File.read(entry.path, { encoding: "UTF-8" });
313+
let entireFile = await IOUtils.readUTF8(entry.path);
312314
let entireFileLines = entireFile.split("\n");
313315

314316
if (entireFileLines.length == 0) {
@@ -336,8 +338,8 @@ class LogFilesComponent extends React.Component {
336338
}
337339
await puts(outLines.join("\n"));
338340
}
339-
} finally {
340-
await tmpFileInfo.file.close();
341+
} catch (err) {
342+
console.error("Error combining logs: ", err);
341343
}
342344
this.setState({
343345
downloadingCombined: {
@@ -346,7 +348,7 @@ class LogFilesComponent extends React.Component {
346348
}
347349
});
348350

349-
await this.downloadFile(OS.Path.toFileURI(tmpFileInfo.path), "aboutsync-combined-log.txt");
351+
await this.downloadFile(PathUtils.toFileURI(combinedFilePath), "aboutsync-combined-log.txt");
350352
}
351353

352354
async downloadCombined(event) {
@@ -369,23 +371,27 @@ class LogFilesComponent extends React.Component {
369371
});
370372
}
371373

372-
componentDidMount() {
374+
async componentDidMount() {
373375
// find all our log-files.
374376
let logDir = FileUtils.getDir("ProfD", ["weave", "logs"]);
375-
let iterator = new OS.File.DirectoryIterator(logDir.path);
376-
377377
let result = {
378378
entries: [],
379379
numErrors: 0,
380380
}
381-
iterator.forEach(entry => {
382-
result.entries.push(entry);
383-
result.numErrors += entry.name.startsWith("error-") ? 1 : 0;
384-
}).then(() => {
385-
this.setState({ logFiles: result });
386-
}).catch(err => {
381+
try {
382+
let iterator = await IOUtils.getChildren(logDir.path);
383+
iterator.forEach(entry => {
384+
// IOUtils gives us the absolute path, but LOG_FILE_RE
385+
// and other operations operate on the filename
386+
let fileName = /[^/]*$/.exec(entry)[0];
387+
result.entries.push({name: fileName, path: entry});
388+
result.numErrors += fileName.startsWith("error-") ? 1 : 0;
389+
});
390+
} catch(err) {
387391
console.error("Failed to fetch the logfiles", err);
388-
});
392+
}
393+
// Update the state
394+
this.setState({ logFiles: result });
389395
}
390396

391397
_processing(kind, {current, total}) {

src/provider.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const { PrefCheckbox } = require("./config");
1313
const { Weave } = importLocal("resource://services-sync/main.js");
1414

1515
const { CryptoWrapper, Collection } = importLocal("resource://services-sync/record.js");
16-
const { OS } = importLocal("resource://gre/modules/osfile.jsm");
1716
const { PlacesUtils } = importLocal("resource://gre/modules/PlacesUtils.jsm");
1817

1918
const React = require("react");
@@ -191,7 +190,7 @@ class LocalProvider extends Provider {
191190
this.anonymize(ob);
192191
}
193192
let json = JSON.stringify(ob, undefined, 2); // pretty!
194-
return OS.File.writeAtomic(path, json, {encoding: "utf-8", tmpPath: path + ".tmp"});
193+
return IOUtils.writeUTF8(path, json);
195194
}
196195

197196
/* Perform a quick-and-nasty anonymization of the data. Replaces many

0 commit comments

Comments
 (0)