summaryrefslogtreecommitdiffstats
path: root/dom/docs/ioutils_migration.md
blob: c9f959b56483f7b64054cdb7197f39507c0f585a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# IOUtils Migration Guide

**Improving performance through a new file API**

---

## What is IOUtils?

`IOUtils` is a privileged JavaScript API for performing file I/O in the Firefox frontend.
It was developed as a replacement for `OS.File`, addressing
[bug 1231711](https://bugzilla.mozilla.org/show_bug.cgi?id=1231711).
It is *not to be confused* with the unprivileged
[DOM File API](https://developer.mozilla.org/en-US/docs/Web/API/File).

`IOUtils` provides a minimal API surface to perform common
I/O tasks via a collection of static methods inspired from `OS.File`.
It is implemented in C++, and exposed to JavaScript via WebIDL bindings.

The most up-to-date API can always be found in
[IOUtils.webidl](https://searchfox.org/mozilla-central/source/dom/chrome-webidl/IOUtils.webidl).

## Differences from `OS.File`

`IOUtils` has a similar API to `OS.File`, but one should keep in mind some key differences.

### No `File` instances (except `SyncReadFile` in workers)

Most of the `IOUtils` methods only operate on absolute path strings, and don't expose a file handle to the caller.
The exception to this rule is the `openFileForSyncReading` API, which is only available in workers.

Furthermore, `OS.File` was exposing platform-specific file descriptors through the
[`fd`](https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.File_for_workers#Attributes)
attribute. `IOUtils` does not expose file descriptors.

### WebIDL has no `Date` type

`IOUtils` is written in C++ and exposed to JavaScript through WebIDL.
Many uses of `OS.File` concern themselves with obtaining or manipulating file metadata,
like the last modified time, however the `Date` type does not exist in WebIDL.
Using `IOUtils`,
these values are returned to the caller as the number of milliseconds since
`1970-01-01T00:00:00Z`.
`Date`s can be safely constructed from these values if needed.

For example, to obtain the last modification time of a file and update it to the current time:

```js
let { lastModified } = await IOUtils.stat(path);

let lastModifiedDate = new Date(lastModified);

let now = new Date();

await IOUtils.touch(path, now.valueOf());
```

### Some methods are not implemented

For various reasons
(complexity, safety, availability of underlying system calls, usefulness, etc.)
the following `OS.File` methods have no analogue in IOUtils.
They also will **not** be implemented.

-   void unixSymlink(in string targetPath, in string createPath)
-   string getCurrentDirectory(void)
-   void setCurrentDirectory(in string path)
-   object open(in string path)
-   object openUnique(in string path)

### Errors are reported as `DOMException`s

When an `OS.File` method runs into an error,
it will throw/reject with a custom
[`OS.File.Error`](https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/OSFile.jsm/OS.File.Error).
These objects have custom attributes that can be checked for common error cases.

`IOUtils` has similar behaviour, however its methods consistently reject with a
[`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException)
whose name depends on the failure:

| Exception Name | Reason for exception |
| -------------- | -------------------- |
| `NotFoundError` | A file at the specified path could not be found on disk. |
| `NotAllowedError` | Access to a file at the specified path was denied by the operating system. |
| `NotReadableError` | A file at the specified path could not be read for some reason. It may have been too big to read, or it was corrupt, or some other reason. The exception message should have more details. |
| `ReadOnlyError` | A file at the specified path is read only and could not be modified. |
| `NoModificationAllowedError` | A file already exists at the specified path and could not be overwritten according to the specified options. The exception message should have more details. |
| `OperationError` | Something went wrong during the I/O operation. E.g. failed to allocate a buffer. The exception message should have more details. |
| `UnknownError` | An unknown error occurred in the implementation. An nsresult error code should be included in the exception message to assist with debugging and improving `IOUtils` internal error handling. |

### `IOUtils` is mostly async-only

`OS.File` provided an asynchronous front-end for main-thread consumers,
and a synchronous front-end for workers.
`IOUtils` only provides an asynchronous API for the vast majority of its API surface.
These asynchronous methods can be called from both the main thread and from chrome-privileged worker threads.

The one exception to this rule is `openFileForSyncReading`, which allows synchronous file reading in workers.

## `OS.File` vs `IOUtils`

Some methods and options of `OS.File` keep the same name and underlying behaviour in `IOUtils`,
but others have been renamed.
The following is a detailed comparison with examples of the methods and options in each API.

### Reading a file

`IOUtils` provides the following methods to read data from a file. Like
`OS.File`, they accept an `options` dictionary.

Note: The maximum file size that can be read is `UINT32_MAX` bytes. Attempting
to read a file larger will result in a `NotReadableError`.

```idl
Promise<Uint8Array> read(DOMString path, ...);

Promise<DOMString> readUTF8(DOMString path, ...);

Promise<any> readJSON(DOMString path, ...);

// Workers only:
SyncReadFile openFileForSyncReading(DOMString path);
```

#### Options

| `OS.File` option   | `IOUtils` option             | Description                                                                                                               |
| ------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| bytes: number?     | maxBytes: number?            | If specified, read only up to this number of bytes. Otherwise, read the entire file. Default is null.                         |
| compression: 'lz4' | decompress: boolean          | If true, read the file and return the decompressed LZ4 stream. Otherwise, just read the file byte-for-byte. Default is false. |
| encoding: 'utf-8'  | N/A; use `readUTF8` instead. | Interprets the file as UTF-8 encoded text, and returns a string to the caller.                                                |

#### Examples

##### Read raw (unsigned) byte values

**`OS.File`**
```js
let bytes = await OS.File.read(path); // Uint8Array
```
**`IOUtils`**
```js
let bytes = await IOUtils.read(path); // Uint8Array
```

##### Read UTF-8 encoded text

**`OS.File`**
```js
let utf8 = await OS.File.read(path, { encoding: 'utf-8' }); // string
```
**`IOUtils`**
```js
let utf8 = await IOUtils.readUTF8(path); // string
```

##### Read JSON file

**`IOUtils`**
```js
let obj = await IOUtils.readJSON(path); // object
```

##### Read LZ4 compressed file contents

**`OS.File`**
```js
// Uint8Array
let bytes = await OS.File.read(path, { compression: 'lz4' });
// string
let utf8 = await OS.File.read(path, {
    encoding: 'utf-8',
    compression: 'lz4',
});
```
**`IOUtils`**
```js
let bytes = await IOUtils.read(path, { decompress: true }); // Uint8Array
let utf8 = await IOUtils.readUTF8(path, { decompress: true }); // string
```

##### Synchronously read a fragment of a file into a buffer, from a worker

**`OS.File`**
```js
// Read 64 bytes at offset 128, workers only:
let file = OS.File.open(path, { read: true });
file.setPosition(128);
let bytes = file.read({ bytes: 64 }); // Uint8Array
file.close();
```
**`IOUtils`**
```js
// Read 64 bytes at offset 128, workers only:
let file = IOUtils.openFileForSyncReading(path);
let bytes = new Uint8Array(64);
file.readBytesInto(bytes, 128);
file.close();
```

### Writing to a file

IOUtils provides the following methods to write data to a file. Like
OS.File, they accept an options dictionary.

```idl
Promise<unsigned long long> write(DOMString path, Uint8Array data, ...);

Promise<unsigned long long> writeUTF8(DOMString path, DOMString string, ...);

Promise<unsigned long long> writeJSON(DOMString path, any value, ...);
```

#### Options

| `OS.File` option     | `IOUtils` option              | Description                                                                                                                                                               |
| -------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| backupTo: string?    | backupFile: string?           | Identifies the path to backup the target file to before performing the write operation. If unspecified, no backup will be performed. Default is null.                     |
| tmpPath: string?     | tmpPath: string?              | Identifies a path to write to first, before performing a move to overwrite the target file. If unspecified, the target file will be written to directly. Default is null. |
| noOverwrite: boolean | mode: 'overwrite' or 'create' | 'create' mode will refuse to overwrite an existing file. Default is 'overwrite'.                                                                                          |
| flush: boolean       | flush: boolean                | If true, force the OS to flush its internal buffers to disk. Default is false.                                                                                            |
| encoding: 'utf-8'    | N/A; use `writeUTF8` instead. | Allows the caller to supply a string to be encoded as utf-8 text on disk.                                                                                                 |

#### Examples
##### Write raw (unsigned) byte values

**`OS.File`**
```js
let bytes = new Uint8Array();
await OS.File.writeAtomic(path, bytes);
```

**`IOUtils`**
```js
let bytes = new Uint8Array();
await IOUtils.write(path, bytes);
```

##### Write UTF-8 encoded text

**`OS.File`**
```js
let str = "";
await OS.File.writeAtomic(path, str, { encoding: 'utf-8' });
```

**`IOUtils`**
```js
let str = "";
await IOUtils.writeUTF8(path, str);
```

##### Write A JSON object

**`IOUtils`**
```js
let obj = {};
await IOUtils.writeJSON(path, obj);
```

##### Write with LZ4 compression

**`OS.File`**
```js
let bytes = new Uint8Array();
await OS.File.writeAtomic(path, bytes, { compression: 'lz4' });
let str = "";
await OS.File.writeAtomic(path, str, {
    compression: 'lz4',
});
```

**`IOUtils`**
```js
let bytes = new Uint8Array();
await IOUtils.write(path, bytes, { compress: true });
let str = "";
await IOUtils.writeUTF8(path, str, { compress: true });
```

### Move a file

`IOUtils` provides the following method to move files on disk.
Like `OS.File`, it accepts an options dictionary.

```idl
Promise<void> move(DOMString sourcePath, DOMString destPath, ...);
```

#### Options

| `OS.File` option     | `IOUtils` option                    | Description                                                                                                                                                               |
| -------------------- | ----------------------------------- | ---------------------------------------------------------------------------- |
| noOverwrite: boolean | noOverwrite: boolean                | If true, fail if the destination already exists. Default is false.           |
| noCopy: boolean      | N/A; will not be implemented        | This option is not implemented in `IOUtils`, and will be ignored if provided |

#### Example

**`OS.File`**
```js
await OS.File.move(srcPath, destPath);
```

**`IOUtils`**
```js
await IOUtils.move(srcPath, destPath);
```

### Remove a file

`IOUtils` provides *one* method to remove files from disk.
`OS.File` provides several methods.

```idl
Promise<void> remove(DOMString path, ...);
```

#### Options

| `OS.File` option                                           | `IOUtils` option      | Description                                                                                                      |
| ---------------------------------------------------------- | --------------------- | ---------------------------------------------------------------------------------------------------------------- |
| ignoreAbsent: boolean                                      | ignoreAbsent: boolean | If true, and the destination does not exist, then do not raise an error. Default is true.                        |
| N/A; `OS.File` has dedicated methods for directory removal | recursive: boolean    | If true, and the target is a directory, recursively remove the directory and all its children. Default is false. |

#### Examples

##### Remove a file

**`OS.File`**
```js
await OS.File.remove(path, { ignoreAbsent: true });
```

**`IOUtils`**
```js
await IOUtils.remove(path);
```

##### Remove a directory and all its contents

**`OS.File`**
```js
await OS.File.removeDir(path, { ignoreAbsent: true });
```

**`IOUtils`**
```js
await IOUtils.remove(path, { recursive: true });
```

##### Remove an empty directory

**`OS.File`**
```js
await OS.File.removeEmptyDir(path); // Will throw an exception if `path` is not empty.
```

**`IOUtils`**
```js
await IOUtils.remove(path); // Will throw an exception if `path` is not empty.
```

### Make a directory

`IOUtils` provides the following method to create directories on disk.
Like `OS.File`, it accepts an options dictionary.

```idl
Promise<void> makeDirectory(DOMString path, ...);
```

#### Options

| `OS.File` option        | `IOUtils` option           | Description                                                                                                                                                                                                     |
| ----------------------- | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ignoreExisting: boolean | ignoreExisting: boolean    | If true, succeed even if the target directory already exists. Default is true.                                                                                                                                  |
| from: string            | createAncestors: boolean   | If true, `IOUtils` will create all missing ancestors in a path. Default is true. This option differs from `OS.File`, which requires the caller to specify a root path from which to create missing directories. |
| unixMode: number        | permissions: unsigned long | The file mode to create the directory with. Ignored on Windows. Default is 0755.                                                                                                                                |
| winSecurity             | N/A                        | `IOUtils` does not support setting custom directory security settings on Windows.                                                                                                                               |

#### Example

**`OS.File`**
```js
await OS.File.makeDir(srcPath, destPath);
```
**`IOUtils`**
```js
await IOUtils.makeDirectory(srcPath, destPath);
```

### Update a file's modification time

`IOUtils` provides the following method to update a file's modification time.

```idl
Promise<void> setModificationTime(DOMString path, optional long long modification);
```

#### Example

**`OS.File`**
```js
await OS.File.setDates(path, new Date(), new Date());
```

**`IOUtils`**
```js
await IOUtils.setModificationTime(path, new Date().valueOf());
```

### Get file metadata

`IOUtils` provides the following method to query file metadata.

```idl
Promise<void> stat(DOMString path);
```

#### Example

**`OS.File`**
```js
let fileInfo = await OS.File.stat(path);
```

**`IOUtils`**
```js
let fileInfo = await IOUtils.stat(path);
```

### Copy a file

`IOUtils` provides the following method to copy a file on disk.
Like `OS.File`, it accepts an options dictionary.

```idl
Promise<void> copy(DOMString path, ...);
```

#### Options

| `OS.File` option                                                    | `IOUtils` option     | Description                                                    |
| ------------------------------------------------------------------- | -------------------- | -------------------------------------------------------------------|
| noOverwrite: boolean                                                | noOverwrite: boolean | If true, fail if the destination already exists. Default is false. |
| N/A; `OS.File` does not appear to support recursively copying files | recursive: boolean   | If true, copy the source recursively.                              |

#### Examples

##### Copy a file

**`OS.File`**
```js
await OS.File.copy(srcPath, destPath);
```

**`IOUtils`**
```js
await IOUtils.copy(srcPath, destPath);
```

##### Copy a directory recursively

**`OS.File`**
```js
// Not easy to do.
```

**`IOUtils`**
```js
await IOUtils.copy(srcPath, destPath, { recursive: true });
```

### Iterate a directory

At the moment, `IOUtils` does not have a way to expose an iterator for directories.
This is blocked by
[bug 1577383](https://bugzilla.mozilla.org/show_bug.cgi?id=1577383).
As a stop-gap for this functionality,
one can get all the children of a directory and iterate through the returned path array using the following method.

```idl
Promise<sequence<DOMString>> getChildren(DOMString path);
```

#### Example

**`OS.File`**
```js
for await (const { path } of new OS.FileDirectoryIterator(dirName)) {</p>
  ...
}
```

**`IOUtils`**
```js
for (const path of await IOUtils.getChildren(dirName)) {
  ...
}
```

### Check if a file exists

`IOUtils` provides the following method analogous to the `OS.File` method of the same name.

```idl
Promise<boolean> exists(DOMString path);
```

#### Example

**`OS.File`**
```js
if (await OS.File.exists(path)) {
  ...
}
```

**`IOUtils`**
```js
if (await IOUtils.exists(path)) {
  ...
}
```

### Set the permissions of a file

`IOUtils` provides the following method analogous to the `OS.File` method of the same name.

```idl
Promise<void> setPermissions(DOMString path, unsigned long permissions, optional boolean honorUmask = true);
```

#### Options

| `OS.File` option        | `IOUtils` option           | Description                                                                                                                          |
| ----------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| unixMode: number        | permissions: unsigned long | The UNIX file mode representing the permissions. Required in IOUtils.                                                                |
| unixHonorUmask: boolean | honorUmask: boolean        | If omitted or true, any UNIX file mode is modified by the permissions. Otherwise the exact value of the permissions will be applied. |

#### Example

**`OS.File`**
```js
await OS.File.setPermissions(path, { unixMode: 0o600 });
```

**`IOUtils`**
```js
await IOUtils.setPermissions(path, 0o600);
```

## FAQs

**Why should I use `IOUtils` instead of `OS.File`?**

[Bug 1231711](https://bugzilla.mozilla.org/show_bug.cgi?id=1231711)
provides some good context, but some reasons include:
* reduced cache-contention,
* faster startup, and
* less memory usage.

Additionally, `IOUtils` benefits from a native implementation,
which assists in performance-related work for
[Project Fission](https://hacks.mozilla.org/2021/05/introducing-firefox-new-site-isolation-security-architecture/).

We are actively working to migrate old code usages of `OS.File`
to analogous `IOUtils` calls, so new usages of `OS.File`
should not be introduced at this time.

**Do I need to import anything to use this API?**

Nope! It's available via the `IOUtils` global in JavaScript (`ChromeOnly` context).

**Can I use this API from C++ or Rust?**

Currently usage is geared exclusively towards JavaScript callers,
and all C++ methods are private except for the Web IDL bindings.
However given sufficient interest,
it should be easy to expose ergonomic public methods for C++ and/or Rust.

**Why isn't `IOUtils` written in Rust?**

At the time of writing,
support for Web IDL bindings was more mature for C++ oriented tooling than it was for Rust.

**Is `IOUtils` feature complete? When will it be available?**

`IOUtils` is considered feature complete as of Firefox 83.