@@ -679,19 +679,19 @@ Tests a user's permissions for the file or directory specified by `path`.
679
679
The ` mode ` argument is an optional integer that specifies the accessibility
680
680
checks to be performed. The following constants define the possible values of
681
681
` mode ` . It is possible to create a mask consisting of the bitwise OR of two or
682
- more values.
682
+ more values (e.g. ` fs.constants.W_OK | fs.constants.R_OK ` ) .
683
683
684
- - ` fs.constants.F_OK ` - ` path ` is visible to the calling process. This is useful
684
+ * ` fs.constants.F_OK ` - ` path ` is visible to the calling process. This is useful
685
685
for determining if a file exists, but says nothing about ` rwx ` permissions.
686
686
Default if no ` mode ` is specified.
687
- - ` fs.constants.R_OK ` - ` path ` can be read by the calling process.
688
- - ` fs.constants.W_OK ` - ` path ` can be written by the calling process.
689
- - ` fs.constants.X_OK ` - ` path ` can be executed by the calling process. This has
687
+ * ` fs.constants.R_OK ` - ` path ` can be read by the calling process.
688
+ * ` fs.constants.W_OK ` - ` path ` can be written by the calling process.
689
+ * ` fs.constants.X_OK ` - ` path ` can be executed by the calling process. This has
690
690
no effect on Windows (will behave like ` fs.constants.F_OK ` ).
691
691
692
692
The final argument, ` callback ` , is a callback function that is invoked with
693
693
a possible error argument. If any of the accessibility checks fail, the error
694
- argument will be populated . The following example checks if the file
694
+ argument will be an ` Error ` object . The following example checks if the file
695
695
` /etc/passwd ` can be read and written by the current process.
696
696
697
697
``` js
@@ -783,7 +783,7 @@ The "not recommended" examples above check for accessibility and then use the
783
783
file; the "recommended" examples are better because they use the file directly
784
784
and handle the error, if any.
785
785
786
- In general, check for the accessibility of a file only if the file won’t be
786
+ In general, check for the accessibility of a file only if the file will not be
787
787
used directly, for example when its accessibility is a signal from another
788
788
process.
789
789
@@ -799,9 +799,33 @@ changes:
799
799
800
800
* ` path ` {string|Buffer|URL}
801
801
* ` mode ` {integer} ** Default:** ` fs.constants.F_OK `
802
+ * Returns: ` undefined `
802
803
803
- Synchronous version of [ ` fs.access() ` ] [ ] . This throws if any accessibility
804
- checks fail, and does nothing otherwise.
804
+ Synchronously tests a user's permissions for the file or directory specified by
805
+ ` path ` . The ` mode ` argument is an optional integer that specifies the
806
+ accessibility checks to be performed. The following constants define the
807
+ possible values of ` mode ` . It is possible to create a mask consisting of the
808
+ bitwise OR of two or more values (e.g. ` fs.constants.W_OK | fs.constants.R_OK ` ).
809
+
810
+ * ` fs.constants.F_OK ` - ` path ` is visible to the calling process. This is useful
811
+ for determining if a file exists, but says nothing about ` rwx ` permissions.
812
+ Default if no ` mode ` is specified.
813
+ * ` fs.constants.R_OK ` - ` path ` can be read by the calling process.
814
+ * ` fs.constants.W_OK ` - ` path ` can be written by the calling process.
815
+ * ` fs.constants.X_OK ` - ` path ` can be executed by the calling process. This has
816
+ no effect on Windows (will behave like ` fs.constants.F_OK ` ).
817
+
818
+ If any of the accessibility checks fail, an ` Error ` will be thrown. Otherwise,
819
+ the method will return ` undefined ` .
820
+
821
+ ``` js
822
+ try {
823
+ fs .accessSync (' etc/passwd' , fs .constants .R_OK | fs .constants .W_OK );
824
+ console .log (' can read/write' );
825
+ } catch (err) {
826
+ console .error (' no access!' );
827
+ }
828
+ ```
805
829
806
830
## fs.appendFile(file, data[ , options] , callback)
807
831
<!-- YAML
@@ -828,8 +852,8 @@ changes:
828
852
* ` callback ` {Function}
829
853
* ` err ` {Error}
830
854
831
- Asynchronously append data to a file, creating the file if it does not yet exist.
832
- ` data ` can be a string or a buffer .
855
+ Asynchronously append data to a file, creating the file if it does not yet
856
+ exist. ` data ` can be a string or a [ ` Buffer ` ] [ ] .
833
857
834
858
Example:
835
859
@@ -846,10 +870,21 @@ If `options` is a string, then it specifies the encoding. Example:
846
870
fs .appendFile (' message.txt' , ' data to append' , ' utf8' , callback);
847
871
```
848
872
849
- Any specified file descriptor has to have been opened for appending.
873
+ The ` file ` may be specified as a numeric file descriptor that has been opened
874
+ for appending (using ` fs.open() ` or ` fs.openSync() ` ). The file descriptor will
875
+ not be closed automatically.
850
876
851
- * Note* : If a file descriptor is specified as the ` file ` , it will not be closed
852
- automatically.
877
+ ``` js
878
+ fs .open (' message.txt' , ' a' , (err , fd ) => {
879
+ if (err) throw err;
880
+ fs .appendFile (fd, ' data to append' , ' utf8' , (err ) => {
881
+ fs .close (fd, (err ) => {
882
+ if (err) throw err;
883
+ });
884
+ if (err) throw err;
885
+ });
886
+ });
887
+ ```
853
888
854
889
## fs.appendFileSync(file, data[ , options] )
855
890
<!-- YAML
@@ -870,7 +905,43 @@ changes:
870
905
* ` mode ` {integer} ** Default:** ` 0o666 `
871
906
* ` flag ` {string} ** Default:** ` 'a' `
872
907
873
- The synchronous version of [ ` fs.appendFile() ` ] [ ] . Returns ` undefined ` .
908
+ Synchronously append data to a file, creating the file if it does not yet
909
+ exist. ` data ` can be a string or a [ ` Buffer ` ] [ ] .
910
+
911
+ Example:
912
+
913
+ ``` js
914
+ try {
915
+ fs .appendFileSync (' message.txt' , ' data to append' );
916
+ console .log (' The "data to append" was appended to file!' );
917
+ } catch (err) {
918
+ /* Handle the error */
919
+ }
920
+ ```
921
+
922
+ If ` options ` is a string, then it specifies the encoding. Example:
923
+
924
+ ``` js
925
+ fs .appendFileSync (' message.txt' , ' data to append' , ' utf8' );
926
+ ```
927
+
928
+ The ` file ` may be specified as a numeric file descriptor that has been opened
929
+ for appending (using ` fs.open() ` or ` fs.openSync() ` ). The file descriptor will
930
+ not be closed automatically.
931
+
932
+ ``` js
933
+ let fd;
934
+
935
+ try {
936
+ fd = fs .openSync (' message.txt' , ' a' );
937
+ fs .appendFileSync (fd, ' data to append' , ' utf8' );
938
+ } catch (err) {
939
+ /* Handle the error */
940
+ } finally {
941
+ if (fd !== undefined )
942
+ fs .closeSync (fd);
943
+ }
944
+ ```
874
945
875
946
## fs.chmod(path, mode, callback)
876
947
<!-- YAML
@@ -896,6 +967,47 @@ possible exception are given to the completion callback.
896
967
897
968
See also: chmod(2)
898
969
970
+ ### File modes
971
+
972
+ The ` mode ` argument used in both the ` fs.chmod() ` and ` fs.chmodSync() `
973
+ methods is a numeric bitmask created using a logical OR of the following
974
+ constants:
975
+
976
+ | Constant | Octal | Description |
977
+ | ---------------------- | ------- | ------------------------ |
978
+ | ` fs.constants.S_IRUSR ` | ` 0o400 ` | read by owner |
979
+ | ` fs.constants.S_IWUSR ` | ` 0o200 ` | write by owner |
980
+ | ` fs.constants.S_IXUSR ` | ` 0o100 ` | execute/search by owner |
981
+ | ` fs.constants.S_IRGRP ` | ` 0o40 ` | read by group |
982
+ | ` fs.constants.S_IWGRP ` | ` 0o20 ` | write by group |
983
+ | ` fs.constants.S_IXGRP ` | ` 0o10 ` | execute/search by group |
984
+ | ` fs.constants.S_IROTH ` | ` 0o4 ` | read by others |
985
+ | ` fs.constants.S_IWOTH ` | ` 0o2 ` | write by others |
986
+ | ` fs.constants.S_IXOTH ` | ` 0o1 ` | execute/search by others |
987
+
988
+ An easier method of constructing the ` mode ` is to use a sequence of three
989
+ octal digits (e.g. ` 765 ` ). The left-most digit (` 7 ` in the example), specifies
990
+ the permissions for the file owner. The middle digit (` 6 ` in the example),
991
+ specifies permissions for the group. The right-most digit (` 5 ` in the example),
992
+ specifies the permissions for others.
993
+
994
+ | Number | Description |
995
+ | ------- | ------------------------ |
996
+ | ` 7 ` | read, write, and execute |
997
+ | ` 6 ` | read and write |
998
+ | ` 5 ` | read and execute |
999
+ | ` 4 ` | read only |
1000
+ | ` 3 ` | write and execute |
1001
+ | ` 2 ` | write only |
1002
+ | ` 1 ` | execute only |
1003
+ | ` 0 ` | no permission |
1004
+
1005
+ For example, the octal value ` 0o765 ` means:
1006
+
1007
+ * The owner may read, write and execute the file.
1008
+ * The group may read and write the file.
1009
+ * Others may read and execute the file.
1010
+
899
1011
## fs.chmodSync(path, mode)
900
1012
<!-- YAML
901
1013
added: v0.6.7
@@ -3345,7 +3457,6 @@ The following constants are meant for use with the [`fs.Stats`][] object's
3345
3457
[ `fs.FSWatcher` ] : #fs_class_fs_fswatcher
3346
3458
[ `fs.Stats` ] : #fs_class_fs_stats
3347
3459
[ `fs.access()` ] : #fs_fs_access_path_mode_callback
3348
- [ `fs.appendFile()` ] : fs.html#fs_fs_appendfile_file_data_options_callback
3349
3460
[ `fs.chmod()` ] : #fs_fs_chmod_path_mode_callback
3350
3461
[ `fs.chown()` ] : #fs_fs_chown_path_uid_gid_callback
3351
3462
[ `fs.exists()` ] : fs.html#fs_fs_exists_path_callback
0 commit comments