Skip to content

Commit 1c2783b

Browse files
jasnellgibfahn
authored andcommitted
doc: fs doc improvements
PR-URL: #17831 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 3ae37b2 commit 1c2783b

File tree

1 file changed

+127
-16
lines changed

1 file changed

+127
-16
lines changed

doc/api/fs.md

+127-16
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,19 @@ Tests a user's permissions for the file or directory specified by `path`.
457457
The `mode` argument is an optional integer that specifies the accessibility
458458
checks to be performed. The following constants define the possible values of
459459
`mode`. It is possible to create a mask consisting of the bitwise OR of two or
460-
more values.
460+
more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
461461

462-
- `fs.constants.F_OK` - `path` is visible to the calling process. This is useful
462+
* `fs.constants.F_OK` - `path` is visible to the calling process. This is useful
463463
for determining if a file exists, but says nothing about `rwx` permissions.
464464
Default if no `mode` is specified.
465-
- `fs.constants.R_OK` - `path` can be read by the calling process.
466-
- `fs.constants.W_OK` - `path` can be written by the calling process.
467-
- `fs.constants.X_OK` - `path` can be executed by the calling process. This has
465+
* `fs.constants.R_OK` - `path` can be read by the calling process.
466+
* `fs.constants.W_OK` - `path` can be written by the calling process.
467+
* `fs.constants.X_OK` - `path` can be executed by the calling process. This has
468468
no effect on Windows (will behave like `fs.constants.F_OK`).
469469

470470
The final argument, `callback`, is a callback function that is invoked with
471471
a possible error argument. If any of the accessibility checks fail, the error
472-
argument will be populated. The following example checks if the file
472+
argument will be an `Error` object. The following example checks if the file
473473
`/etc/passwd` can be read and written by the current process.
474474

475475
```js
@@ -561,7 +561,7 @@ The "not recommended" examples above check for accessibility and then use the
561561
file; the "recommended" examples are better because they use the file directly
562562
and handle the error, if any.
563563

564-
In general, check for the accessibility of a file only if the file won’t be
564+
In general, check for the accessibility of a file only if the file will not be
565565
used directly, for example when its accessibility is a signal from another
566566
process.
567567

@@ -577,9 +577,33 @@ changes:
577577

578578
* `path` {string|Buffer|URL}
579579
* `mode` {integer} **Default:** `fs.constants.F_OK`
580+
* Returns: `undefined`
580581

581-
Synchronous version of [`fs.access()`][]. This throws if any accessibility
582-
checks fail, and does nothing otherwise.
582+
Synchronously tests a user's permissions for the file or directory specified by
583+
`path`. The `mode` argument is an optional integer that specifies the
584+
accessibility checks to be performed. The following constants define the
585+
possible values of `mode`. It is possible to create a mask consisting of the
586+
bitwise OR of two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
587+
588+
* `fs.constants.F_OK` - `path` is visible to the calling process. This is useful
589+
for determining if a file exists, but says nothing about `rwx` permissions.
590+
Default if no `mode` is specified.
591+
* `fs.constants.R_OK` - `path` can be read by the calling process.
592+
* `fs.constants.W_OK` - `path` can be written by the calling process.
593+
* `fs.constants.X_OK` - `path` can be executed by the calling process. This has
594+
no effect on Windows (will behave like `fs.constants.F_OK`).
595+
596+
If any of the accessibility checks fail, an `Error` will be thrown. Otherwise,
597+
the method will return `undefined`.
598+
599+
```js
600+
try {
601+
fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
602+
console.log('can read/write');
603+
} catch (err) {
604+
console.error('no access!');
605+
}
606+
```
583607

584608
## fs.appendFile(file, data[, options], callback)
585609
<!-- YAML
@@ -606,8 +630,8 @@ changes:
606630
* `callback` {Function}
607631
* `err` {Error}
608632

609-
Asynchronously append data to a file, creating the file if it does not yet exist.
610-
`data` can be a string or a buffer.
633+
Asynchronously append data to a file, creating the file if it does not yet
634+
exist. `data` can be a string or a [`Buffer`][].
611635

612636
Example:
613637

@@ -624,10 +648,21 @@ If `options` is a string, then it specifies the encoding. Example:
624648
fs.appendFile('message.txt', 'data to append', 'utf8', callback);
625649
```
626650

627-
Any specified file descriptor has to have been opened for appending.
651+
The `file` may be specified as a numeric file descriptor that has been opened
652+
for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
653+
not be closed automatically.
628654

629-
*Note*: If a file descriptor is specified as the `file`, it will not be closed
630-
automatically.
655+
```js
656+
fs.open('message.txt', 'a', (err, fd) => {
657+
if (err) throw err;
658+
fs.appendFile(fd, 'data to append', 'utf8', (err) => {
659+
fs.close(fd, (err) => {
660+
if (err) throw err;
661+
});
662+
if (err) throw err;
663+
});
664+
});
665+
```
631666

632667
## fs.appendFileSync(file, data[, options])
633668
<!-- YAML
@@ -648,7 +683,43 @@ changes:
648683
* `mode` {integer} **Default:** `0o666`
649684
* `flag` {string} **Default:** `'a'`
650685

651-
The synchronous version of [`fs.appendFile()`][]. Returns `undefined`.
686+
Synchronously append data to a file, creating the file if it does not yet
687+
exist. `data` can be a string or a [`Buffer`][].
688+
689+
Example:
690+
691+
```js
692+
try {
693+
fs.appendFileSync('message.txt', 'data to append');
694+
console.log('The "data to append" was appended to file!');
695+
} catch (err) {
696+
/* Handle the error */
697+
}
698+
```
699+
700+
If `options` is a string, then it specifies the encoding. Example:
701+
702+
```js
703+
fs.appendFileSync('message.txt', 'data to append', 'utf8');
704+
```
705+
706+
The `file` may be specified as a numeric file descriptor that has been opened
707+
for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
708+
not be closed automatically.
709+
710+
```js
711+
let fd;
712+
713+
try {
714+
fd = fs.openSync('message.txt', 'a');
715+
fs.appendFileSync(fd, 'data to append', 'utf8');
716+
} catch (err) {
717+
/* Handle the error */
718+
} finally {
719+
if (fd !== undefined)
720+
fs.closeSync(fd);
721+
}
722+
```
652723

653724
## fs.chmod(path, mode, callback)
654725
<!-- YAML
@@ -674,6 +745,47 @@ possible exception are given to the completion callback.
674745

675746
See also: chmod(2)
676747

748+
### File modes
749+
750+
The `mode` argument used in both the `fs.chmod()` and `fs.chmodSync()`
751+
methods is a numeric bitmask created using a logical OR of the following
752+
constants:
753+
754+
| Constant | Octal | Description |
755+
| ---------------------- | ------- | ------------------------ |
756+
| `fs.constants.S_IRUSR` | `0o400` | read by owner |
757+
| `fs.constants.S_IWUSR` | `0o200` | write by owner |
758+
| `fs.constants.S_IXUSR` | `0o100` | execute/search by owner |
759+
| `fs.constants.S_IRGRP` | `0o40` | read by group |
760+
| `fs.constants.S_IWGRP` | `0o20` | write by group |
761+
| `fs.constants.S_IXGRP` | `0o10` | execute/search by group |
762+
| `fs.constants.S_IROTH` | `0o4` | read by others |
763+
| `fs.constants.S_IWOTH` | `0o2` | write by others |
764+
| `fs.constants.S_IXOTH` | `0o1` | execute/search by others |
765+
766+
An easier method of constructing the `mode` is to use a sequence of three
767+
octal digits (e.g. `765`). The left-most digit (`7` in the example), specifies
768+
the permissions for the file owner. The middle digit (`6` in the example),
769+
specifies permissions for the group. The right-most digit (`5` in the example),
770+
specifies the permissions for others.
771+
772+
| Number | Description |
773+
| ------- | ------------------------ |
774+
| `7` | read, write, and execute |
775+
| `6` | read and write |
776+
| `5` | read and execute |
777+
| `4` | read only |
778+
| `3` | write and execute |
779+
| `2` | write only |
780+
| `1` | execute only |
781+
| `0` | no permission |
782+
783+
For example, the octal value `0o765` means:
784+
785+
* The owner may read, write and execute the file.
786+
* The group may read and write the file.
787+
* Others may read and execute the file.
788+
677789
## fs.chmodSync(path, mode)
678790
<!-- YAML
679791
added: v0.6.7
@@ -3040,7 +3152,6 @@ The following constants are meant for use with the [`fs.Stats`][] object's
30403152
[`fs.FSWatcher`]: #fs_class_fs_fswatcher
30413153
[`fs.Stats`]: #fs_class_fs_stats
30423154
[`fs.access()`]: #fs_fs_access_path_mode_callback
3043-
[`fs.appendFile()`]: fs.html#fs_fs_appendfile_file_data_options_callback
30443155
[`fs.chmod()`]: #fs_fs_chmod_path_mode_callback
30453156
[`fs.chown()`]: #fs_fs_chown_path_uid_gid_callback
30463157
[`fs.exists()`]: fs.html#fs_fs_exists_path_callback

0 commit comments

Comments
 (0)