1 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/.
10 * IOUtils is a simple, efficient interface for performing file I/O from a
11 * privileged chrome-only context. All asynchronous I/O tasks are run on
12 * a background thread.
14 * Pending I/O tasks will block shutdown at the |profileBeforeChange| phase.
15 * During this shutdown phase, no additional I/O tasks will be accepted --
16 * method calls to this interface will reject once shutdown has entered this
19 * IOUtils methods may reject for any number of reasons. Reasonable attempts
20 * have been made to map each common operating system error to a |DOMException|.
21 * Most often, a caller only needs to check if a given file wasn't found by
22 * catching the rejected error and checking if |ex.name === 'NotFoundError'|.
23 * In other cases, it is likely sufficient to allow the error to be caught and
26 [ChromeOnly, Exposed=(Window, Worker)]
29 * Reads up to |opts.maxBytes| of the file at |path| according to |opts|.
31 * NB: The maximum file size that can be read is UINT32_MAX.
33 * @param path An absolute file path.
35 * @return Resolves with an array of unsigned byte values read from disk,
36 * otherwise rejects with a DOMException.
39 Promise<Uint8Array> read(DOMString path, optional ReadOptions opts = {});
41 * Reads the UTF-8 text file located at |path| and returns the decoded
42 * contents as a |DOMString|. If a UTF-8 byte order marker (BOM) is
43 * present, it will be stripped from the returned string.
45 * NB: The maximum file size that can be read is UINT32_MAX.
47 * @param path An absolute file path.
49 * @return Resolves with the file contents encoded as a string, otherwise
50 * rejects with a DOMException.
53 Promise<UTF8String> readUTF8(DOMString path, optional ReadUTF8Options opts = {});
55 * Read the UTF-8 text file located at |path| and return the contents
56 * parsed as JSON into a JS value.
58 * NB: The maximum file size that can be read is UINT32_MAX.
60 * @param path An absolute path.
62 * @return Resolves with the contents of the file parsed as JSON.
65 Promise<any> readJSON(DOMString path, optional ReadUTF8Options opts = {});
67 * Attempts to safely write |data| to a file at |path|.
69 * This operation can be made atomic by specifying the |tmpPath| option. If
70 * specified, then this method ensures that the destination file is not
71 * modified until the data is entirely written to the temporary file, after
72 * which point the |tmpPath| is moved to the specified |path|.
74 * The target file can also be backed up to a |backupFile| before any writes
75 * are performed to prevent data loss in case of corruption.
77 * @param path An absolute file path.
78 * @param data Data to write to the file at path.
80 * @return Resolves with the number of bytes successfully written to the file,
81 * otherwise rejects with a DOMException.
84 Promise<unsigned long long> write(DOMString path, Uint8Array data, optional WriteOptions options = {});
86 * Attempts to encode |string| to UTF-8, then safely write the result to a
87 * file at |path|. Works exactly like |write|.
89 * @param path An absolute file path.
90 * @param string A string to write to the file at path.
91 * @param options Options for writing the file.
93 * @return Resolves with the number of bytes successfully written to the file,
94 * otherwise rejects with a DOMException.
97 Promise<unsigned long long> writeUTF8(DOMString path, UTF8String string, optional WriteOptions options = {});
99 * Attempts to serialize |value| into a JSON string and encode it as into a
100 * UTF-8 string, then safely write the result to a file at |path|. Works
101 * exactly like |write|.
103 * @param path An absolute file path
104 * @param value The value to be serialized.
105 * @param options Options for writing the file. The "append" mode is not supported.
107 * @return Resolves with the number of bytes successfully written to the file,
108 * otherwise rejects with a DOMException.
111 Promise<unsigned long long> writeJSON(DOMString path, any value, optional WriteOptions options = {});
113 * Moves the file from |sourcePath| to |destPath|, creating necessary parents.
114 * If |destPath| is a directory, then the source file will be moved into the
115 * destination directory.
117 * @param sourcePath An absolute file path identifying the file or directory
119 * @param destPath An absolute file path identifying the destination
120 * directory and/or file name.
122 * @return Resolves if the file is moved successfully, otherwise rejects with
126 Promise<undefined> move(DOMString sourcePath, DOMString destPath, optional MoveOptions options = {});
128 * Removes a file or directory at |path| according to |options|.
130 * @param path An absolute file path identifying the file or directory to
133 * @return Resolves if the file is removed successfully, otherwise rejects
134 * with a DOMException.
137 Promise<undefined> remove(DOMString path, optional RemoveOptions options = {});
139 * Creates a new directory at |path| according to |options|.
141 * @param path An absolute file path identifying the directory to create.
143 * @return Resolves if the directory is created successfully, otherwise
144 * rejects with a DOMException.
147 Promise<undefined> makeDirectory(DOMString path, optional MakeDirectoryOptions options = {});
149 * Obtains information about a file, such as size, modification dates, etc.
151 * @param path An absolute file path identifying the file or directory to
154 * @return Resolves with a |FileInfo| object for the file at path, otherwise
155 * rejects with a DOMException.
160 Promise<FileInfo> stat(DOMString path);
162 * Copies a file or directory from |sourcePath| to |destPath| according to
165 * @param sourcePath An absolute file path identifying the source file to be
167 * @param destPath An absolute file path identifying the location for the
170 * @return Resolves if the file was copied successfully, otherwise rejects
171 * with a DOMException.
174 Promise<undefined> copy(DOMString sourcePath, DOMString destPath, optional CopyOptions options = {});
176 * Updates the access time for the file at |path|.
178 * @param path An absolute file path identifying the file whose
179 * modification time is to be set. This file must exist
180 * and will not be created.
181 * @param modification An optional access time for the file expressed in
182 * milliseconds since the Unix epoch
183 * (1970-01-01T00:00:00Z). The current system time is used
184 * if this parameter is not provided.
186 * @return Resolves with the updated access time time expressed in
187 * milliseconds since the Unix epoch, otherwise rejects with a
191 Promise<long long> setAccessTime(DOMString path, optional long long access);
193 * Updates the modification time for the file at |path|.
195 * @param path An absolute file path identifying the file whose
196 * modification time is to be set. This file must exist
197 * and will not be created.
198 * @param modification An optional modification time for the file expressed in
199 * milliseconds since the Unix epoch
200 * (1970-01-01T00:00:00Z). The current system time is used
201 * if this parameter is not provided.
203 * @return Resolves with the updated modification time expressed in
204 * milliseconds since the Unix epoch, otherwise rejects with a
208 Promise<long long> setModificationTime(DOMString path, optional long long modification);
210 * Retrieves a (possibly empty) list of immediate children of the directory at
213 * @param path An absolute file path.
215 * @return Resolves with a sequence of absolute file paths representing the
216 * children of the directory at |path|, otherwise rejects with a
220 Promise<sequence<DOMString>> getChildren(DOMString path, optional GetChildrenOptions options = {});
222 * Set the permissions of the file at |path|.
224 * Windows does not make a distinction between user, group, and other
225 * permissions like UNICES do. If a permission flag is set for any of user,
226 * group, or other has a permission, then all users will have that
227 * permission. Additionally, Windows does not support setting the
228 * "executable" permission.
230 * @param path An absolute file path
231 * @param permissions The UNIX file mode representing the permissions.
232 * @param honorUmask If omitted or true, any UNIX file mode value is
233 * modified by the process umask. If false, the exact value
234 * of UNIX file mode will be applied. This value has no effect
237 * @return Resolves if the permissions were set successfully, otherwise
238 * rejects with a DOMException.
241 Promise<undefined> setPermissions(DOMString path, unsigned long permissions, optional boolean honorUmask = true);
243 * Return whether or not the file exists at the given path.
245 * @param path An absolute file path.
247 * @return A promise that resolves to whether or not the given file exists.
250 Promise<boolean> exists(DOMString path);
253 * Create a file with a unique name and return its path.
255 * @param parent An absolute path to the directory where the file is to be
257 * @param prefix A prefix for the filename.
259 * @return A promise that resolves to a unique filename.
262 Promise<DOMString> createUniqueFile(DOMString parent, DOMString prefix, optional unsigned long permissions = 0644);
265 * Create a directory with a unique name and return its path.
267 * @param parent An absolute path to the directory where the file is to be
269 * @param prefix A prefix for the directory name.
271 * @return A promise that resolves to a unique directory name.
274 Promise<DOMString> createUniqueDirectory(DOMString parent, DOMString prefix, optional unsigned long permissions = 0755);
277 * Compute the hash of a file as a hex digest.
279 * @param path The absolute path of the file to hash.
280 * @param method The hashing method to use.
282 * @return A promise that resolves to the hex digest of the file's hash in lowercase.
285 Promise<UTF8String> computeHexDigest(DOMString path, HashAlgorithm method);
289 * Return the Windows-specific file attributes of the file at the given path.
291 * @param path An absolute file path.
293 * @return A promise that resolves to the Windows-specific file attributes.
296 Promise<WindowsFileAttributes> getWindowsAttributes(DOMString path);
299 * Set the Windows-specific file attributes of the file at the given path.
301 * @param path An absolute file path.
302 * @param attrs The attributes to set. Attributes will only be set if they are
303 * |true| or |false| (i.e., |undefined| attributes are not
306 * @return A promise that resolves is the attributes were set successfully.
309 Promise<undefined> setWindowsAttributes(DOMString path, optional WindowsFileAttributes attrs = {});
310 #elif defined(XP_MACOSX)
312 * Return whether or not the file has a specific extended attribute.
314 * @param path An absolute path.
315 * @param attr The attribute to check for.
317 * @return A promise that resolves to whether or not the file has an extended
318 * attribute, or rejects with an error.
321 Promise<boolean> hasMacXAttr(DOMString path, UTF8String attr);
323 * Return the value of an extended attribute for a file.
325 * @param path An absolute path.
326 * @param attr The attribute to get the value of.
328 * @return A promise that resolves to the value of the extended attribute, or
329 * rejects with an error.
332 Promise<Uint8Array> getMacXAttr(DOMString path, UTF8String attr);
334 * Set the extended attribute on a file.
336 * @param path An absolute path.
337 * @param attr The attribute to set.
338 * @param value The value of the attribute to set.
340 * @return A promise that resolves to whether or not the file has an extended
341 * attribute, or rejects with an error.
344 Promise<undefined> setMacXAttr(DOMString path, UTF8String attr, Uint8Array value);
346 * Delete the extended attribute on a file.
348 * @param path An absolute path.
349 * @param attr The attribute to delete.
351 * @return A promise that resolves if the attribute was deleted, or rejects
355 Promise<undefined> delMacXAttr(DOMString path, UTF8String attr);
359 * Return a nsIFile whose parent directory exists. The parent directory of the
360 * file will be created off main thread if it does not already exist.
362 * @param components The path components. The first component must be an
365 * @return A promise that resolves to an nsIFile for the requested file.
368 Promise<nsIFile> getFile(DOMString... components);
371 * Return an nsIFile corresponding to a directory. It will be created
372 * off-main-thread if it does not already exist.
374 * @param components The path components. The first component must be an
377 * @return A promise that resolves to an nsIFile for the requested directory.
380 Promise<nsIFile> getDirectory(DOMString... components);
384 partial namespace IOUtils {
386 * The async shutdown client for the profile-before-change shutdown phase.
389 readonly attribute any profileBeforeChange;
392 * The async shutdown client for the profile-before-change-telemetry shutdown
395 * ONLY telemetry should register blockers on this client.
398 readonly attribute any sendTelemetry;
402 partial namespace IOUtils {
404 * Synchronously opens the file at |path|. This API is only available in workers.
406 * @param path An absolute file path.
408 * @return A |SyncReadFile| object for the file.
411 SyncReadFile openFileForSyncReading(DOMString path);
415 * Launch a child process; uses `base::LaunchApp` from IPC. (This WebIDL
416 * binding is currently Unix-only; it could also be supported on Windows
417 * but it would use u16-based strings, so it would basically be a separate
418 * copy of the bindings.)
420 * This interface was added for use by `Subprocess.sys.mjs`; other would-be
421 * callers may want to just use Subprocess instead of calling this directly.
423 * @param argv The command to run and its arguments.
424 * @param options Various parameters about how the child process is launched
425 * and its initial environment.
427 * @return The process ID. Note that various errors (e.g., the
428 * executable to be launched doesn't exist) may not be
429 * encountered until after the process is created, so a
430 * successful return doesn't necessarily imply a successful
434 unsigned long launchProcess(sequence<UnixString> argv, LaunchOptions options);
439 * An object representing an open file, allowing parts of the file contents to be
440 * read synchronously. Only available in workers.
442 [ChromeOnly, Exposed=Worker]
443 interface SyncReadFile {
445 * The file size, in bytes.
447 readonly attribute long long size;
450 * Synchronously read |dest.length| bytes at offset |offset| into |dest|.
451 * Throws if the file has been closed already or if the read would be out-of-bounds.
453 * @param dest A Uint8Array whose entire contents will be overwritten with
454 * bytes read from the file.
455 * @param offset The file offset at which the read range begins. (The length of the
456 * range is given by |dest.length|.)
459 undefined readBytesInto(Uint8Array dest, long long offset);
462 * Close the file. Subsequent calls to readBytesInto will throw.
463 * If the file is not closed manually, it will be closed once this object is GC'ed.
469 * Options to be passed to the |IOUtils.readUTF8| method.
471 dictionary ReadUTF8Options {
473 * If true, this option indicates that the file to be read is compressed with
474 * LZ4-encoding, and should be decompressed before the data is returned to
477 boolean decompress = false;
481 * Options to be passed to the |IOUtils.read| method.
483 dictionary ReadOptions : ReadUTF8Options {
485 * The offset into the file to read from. If unspecified, the file will be read
488 unsigned long long offset = 0;
491 * The max bytes to read from the file at path. If unspecified, the entire
492 * file will be read. This option is incompatible with |decompress|.
494 unsigned long? maxBytes = null;
498 * Modes for writing to a file.
502 * Overwrite the contents of the file.
504 * The file will be created if it does not exist.
508 * Append to the end of the file.
510 * This mode will refuse to create the file if it does not exist.
514 * Append to the end of the file, or create it if it does not exist.
520 * This mode will refuse to overwrite an existing file.
526 * Options to be passed to the |IOUtils.write| and |writeUTF8|
529 dictionary WriteOptions {
531 * If specified, backup the destination file to this path before writing.
533 DOMString backupFile;
535 * If specified, write the data to a file at |tmpPath| instead of directly to
536 * the destination. Once the write is complete, the destination will be
537 * overwritten by a move. Specifying this option will make the write a little
538 * slower, but also safer.
542 * The mode used to write to the file.
544 WriteMode mode = "overwrite";
546 * If true, force the OS to write its internal buffers to the disk.
547 * This is considerably slower for the whole system, but safer in case of
548 * an improper system shutdown (e.g. due to a kernel panic) or device
549 * disconnection before the buffers are flushed.
551 boolean flush = false;
553 * If true, compress the data with LZ4-encoding before writing to the file.
555 boolean compress = false;
559 * Options to be passed to the |IOUtils.move| method.
561 dictionary MoveOptions {
563 * If true, fail if the destination already exists.
565 boolean noOverwrite = false;
569 * Options to be passed to the |IOUtils.remove| method.
571 dictionary RemoveOptions {
573 * If true, no error will be reported if the target file is missing.
575 boolean ignoreAbsent = true;
577 * If true, and the target is a directory, recursively remove files.
579 boolean recursive = false;
582 * If true, a failed delete on a readonly file will be retried by first
583 * removing the readonly attribute.
585 * Only has an effect on Windows.
587 boolean retryReadonly = false;
591 * Options to be passed to the |IOUtils.makeDirectory| method.
593 dictionary MakeDirectoryOptions {
595 * If true, create the directory and all necessary ancestors if they do not
596 * already exist. If false and any ancestor directories do not exist,
597 * |makeDirectory| will reject with an error.
599 boolean createAncestors = true;
601 * If true, succeed even if the directory already exists (default behavior).
602 * Otherwise, fail if the directory already exists.
604 boolean ignoreExisting = true;
606 * The file mode to create the directory with.
608 * This is ignored on Windows.
610 unsigned long permissions = 0755;
615 * Options to be passed to the |IOUtils.copy| method.
617 dictionary CopyOptions {
619 * If true, fail if the destination already exists.
621 boolean noOverwrite = false;
623 * If true, copy the source recursively.
625 boolean recursive = false;
629 * Options to be passed to the |IOUtils.getChildren| method.
631 dictionary GetChildrenOptions {
633 * If true, no error will be reported if the target file is missing.
635 boolean ignoreAbsent = false;
639 * Types of files that are recognized by the |IOUtils.stat| method.
641 enum FileType { "regular", "directory", "other" };
644 * Basic metadata about a file.
646 dictionary FileInfo {
648 * The absolute path to the file on disk, as known when this file info was
654 * Identifies if the file at |path| is a regular file, directory, or something
660 * If this represents a regular file, the size of the file in bytes.
666 * The timestamp of file creation, represented in milliseconds since Epoch
667 * (1970-01-01T00:00:00.000Z).
669 * This is only available on MacOS and Windows.
671 long long creationTime;
674 * The timestmp of last file accesss, represented in milliseconds since Epoch
675 * (1970-01-01T00:00:00.000Z).
677 long long lastAccessed;
680 * The timestamp of the last file modification, represented in milliseconds
681 * since Epoch (1970-01-01T00:00:00.000Z).
683 long long lastModified;
686 * The permissions of the file, expressed as a UNIX file mode.
688 * NB: Windows does not make a distinction between user, group, and other
689 * permissions like UNICES do. The user, group, and other parts will always
690 * be identical on Windows.
692 unsigned long permissions;
696 * The supported hash algorithms for |IOUtils.hashFile|.
698 enum HashAlgorithm { "sha256", "sha384", "sha512" };
702 * Windows-specific file attributes.
704 dictionary WindowsFileAttributes {
706 * Whether or not the file is read-only.
710 * Whether or not the file is hidden.
714 * Whether or not the file is classified as a system file.
722 * Used where the POSIX API allows an arbitrary byte string but in
723 * practice it's usually UTF-8, so JS strings are accepted for
726 typedef (UTF8String or Uint8Array) UnixString;
729 * Options for the `launchApp` method. See also `base::LaunchOptions`
732 dictionary LaunchOptions {
734 * The environment variables, as a sequence of `NAME=value` strings.
735 * (The underlying C++ code can also inherit the current environment
736 * with optional changes; that feature could be added here if needed.)
738 required sequence<UnixString> environment;
741 * The initial current working directory.
746 * File descriptors to pass to the child process. Any fds not
747 * mentioned here, other than stdin/out/err, will not be inherited
748 * even if they aren't marked close-on-exec.
750 sequence<FdMapping> fdMap;
753 * On macOS 10.14+, disclaims responsibility for the child process
754 * with respect to privacy/security permission prompts and
755 * decisions. Ignored if not supported by the OS.
757 boolean disclaim = false;
761 * Describes a file descriptor to give to the child process.
763 dictionary FdMapping {
765 * The fd in the parent process to pass. This must remain open during
766 * the call to `launchApp` but can be closed after it returns (or throws).
768 required unsigned long src;
771 * The fd number to map it to in the child process.
773 required unsigned long dst;