Skip to content

Commit 1dcbf73

Browse files
ilg-uljuanarbol
authored andcommitted
src: define fs.constants.S_IWUSR & S_IRUSR for Win
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: #41591 PR-URL: #42757 Refs: #41591 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6190552 commit 1dcbf73

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/api/fs.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -6486,7 +6486,11 @@ operations.
64866486
64876487
The following constants are exported by `fs.constants`.
64886488
6489-
Not every constant will be available on every operating system.
6489+
Not every constant will be available on every operating system;
6490+
this is especially important for Windows, where many of the POSIX specific
6491+
definitions are not available.
6492+
For portable applications it is recommended to check for their presence
6493+
before use.
64906494
64916495
To use more than one constant, use the bitwise OR `|` operator.
64926496
@@ -6539,6 +6543,8 @@ The following constants are meant for use as the `mode` parameter passed to
65396543
</tr>
65406544
</table>
65416545
6546+
The definitions are also available on Windows.
6547+
65426548
##### File copy constants
65436549
65446550
The following constants are meant for use with [`fs.copyFile()`][].
@@ -6567,6 +6573,8 @@ The following constants are meant for use with [`fs.copyFile()`][].
65676573
</tr>
65686574
</table>
65696575
6576+
The definitions are also available on Windows.
6577+
65706578
##### File open constants
65716579
65726580
The following constants are meant for use with `fs.open()`.
@@ -6661,6 +6669,9 @@ The following constants are meant for use with `fs.open()`.
66616669
</tr>
66626670
</table>
66636671
6672+
On Windows, only `O_APPEND`, `O_CREAT`, `O_EXCL`, `O_RDONLY`, `O_RDWR`,
6673+
`O_TRUNC`, `O_WRONLY` and `UV_FS_O_FILEMAP` are available.
6674+
66646675
##### File type constants
66656676
66666677
The following constants are meant for use with the {fs.Stats} object's
@@ -6705,6 +6716,9 @@ The following constants are meant for use with the {fs.Stats} object's
67056716
</tr>
67066717
</table>
67076718
6719+
On Windows, only `S_IFCHR`, `S_IFDIR`, `S_IFLNK`, `S_IFMT`, and `S_IFREG`,
6720+
are available.
6721+
67086722
##### File mode constants
67096723
67106724
The following constants are meant for use with the {fs.Stats} object's
@@ -6765,6 +6779,8 @@ The following constants are meant for use with the {fs.Stats} object's
67656779
</tr>
67666780
</table>
67676781
6782+
On Windows, only `S_IRUSR` and `S_IWUSR` are available.
6783+
67686784
## Notes
67696785
67706786
### Ordering of callback and promise-based operations

src/node_constants.cc

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
#include <dlfcn.h>
4848
#endif
4949

50+
#if defined(_WIN32)
51+
#include <io.h> // _S_IREAD _S_IWRITE
52+
#ifndef S_IRUSR
53+
#define S_IRUSR _S_IREAD
54+
#endif // S_IRUSR
55+
#ifndef S_IWUSR
56+
#define S_IWUSR _S_IWRITE
57+
#endif // S_IWUSR
58+
#endif
59+
5060
#include <cerrno>
5161
#include <csignal>
5262
#include <limits>

test/parallel/test-fs-constants.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
const fs = require('fs');
4+
const assert = require('assert');
5+
6+
// Check if the two constants accepted by chmod() on Windows are defined.
7+
assert.notStrictEqual(fs.constants.S_IRUSR, undefined);
8+
assert.notStrictEqual(fs.constants.S_IWUSR, undefined);

0 commit comments

Comments
 (0)