Skip to content

Commit

Permalink
src: define fs.constants.S_IWUSR & S_IRUSR for Win
Browse files Browse the repository at this point in the history
On Windows, most of the POSIX file mode definitions are not available.
However, functionally equivalent read/write definitions exists, and
chmod() can use them. This patch defines two aliases, so that these
definintions are issued in fs.constants.

fixes: nodejs/node#41591

PR-URL: nodejs/node#42757
Refs: nodejs/node#41591
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
ilg-ul authored and guangwong committed Oct 10, 2022
1 parent 31580e4 commit 2f8a2de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
18 changes: 17 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6526,7 +6526,11 @@ operations.
The following constants are exported by `fs.constants`.
Not every constant will be available on every operating system.
Not every constant will be available on every operating system;
this is especially important for Windows, where many of the POSIX specific
definitions are not available.
For portable applications it is recommended to check for their presence
before use.
To use more than one constant, use the bitwise OR `|` operator.
Expand Down Expand Up @@ -6579,6 +6583,8 @@ The following constants are meant for use as the `mode` parameter passed to
</tr>
</table>
The definitions are also available on Windows.
##### File copy constants
The following constants are meant for use with [`fs.copyFile()`][].
Expand Down Expand Up @@ -6607,6 +6613,8 @@ The following constants are meant for use with [`fs.copyFile()`][].
</tr>
</table>
The definitions are also available on Windows.
##### File open constants
The following constants are meant for use with `fs.open()`.
Expand Down Expand Up @@ -6701,6 +6709,9 @@ The following constants are meant for use with `fs.open()`.
</tr>
</table>
On Windows, only `O_APPEND`, `O_CREAT`, `O_EXCL`, `O_RDONLY`, `O_RDWR`,
`O_TRUNC`, `O_WRONLY` and `UV_FS_O_FILEMAP` are available.
##### File type constants
The following constants are meant for use with the {fs.Stats} object's
Expand Down Expand Up @@ -6745,6 +6756,9 @@ The following constants are meant for use with the {fs.Stats} object's
</tr>
</table>
On Windows, only `S_IFCHR`, `S_IFDIR`, `S_IFLNK`, `S_IFMT`, and `S_IFREG`,
are available.
##### File mode constants
The following constants are meant for use with the {fs.Stats} object's
Expand Down Expand Up @@ -6805,6 +6819,8 @@ The following constants are meant for use with the {fs.Stats} object's
</tr>
</table>
On Windows, only `S_IRUSR` and `S_IWUSR` are available.
## Notes
### Ordering of callback and promise-based operations
Expand Down
10 changes: 10 additions & 0 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
#include <dlfcn.h>
#endif

#if defined(_WIN32)
#include <io.h> // _S_IREAD _S_IWRITE
#ifndef S_IRUSR
#define S_IRUSR _S_IREAD
#endif // S_IRUSR
#ifndef S_IWUSR
#define S_IWUSR _S_IWRITE
#endif // S_IWUSR
#endif

#include <cerrno>
#include <csignal>
#include <limits>
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
require('../common');
const fs = require('fs');
const assert = require('assert');

// Check if the two constants accepted by chmod() on Windows are defined.
assert.notStrictEqual(fs.constants.S_IRUSR, undefined);
assert.notStrictEqual(fs.constants.S_IWUSR, undefined);

0 comments on commit 2f8a2de

Please sign in to comment.