Skip to content

Commit 6d12faf

Browse files
ilg-uldanielleadams
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 dc143a7 commit 6d12faf

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/api/fs.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6485,7 +6485,11 @@ operations.
64856485
64866486
The following constants are exported by `fs.constants`.
64876487
6488-
Not every constant will be available on every operating system.
6488+
Not every constant will be available on every operating system;
6489+
this is especially important for Windows, where many of the POSIX specific
6490+
definitions are not available.
6491+
For portable applications it is recommended to check for their presence
6492+
before use.
64896493
64906494
To use more than one constant, use the bitwise OR `|` operator.
64916495
@@ -6538,6 +6542,8 @@ The following constants are meant for use as the `mode` parameter passed to
65386542
</tr>
65396543
</table>
65406544
6545+
The definitions are also available on Windows.
6546+
65416547
##### File copy constants
65426548
65436549
The following constants are meant for use with [`fs.copyFile()`][].
@@ -6566,6 +6572,8 @@ The following constants are meant for use with [`fs.copyFile()`][].
65666572
</tr>
65676573
</table>
65686574
6575+
The definitions are also available on Windows.
6576+
65696577
##### File open constants
65706578
65716579
The following constants are meant for use with `fs.open()`.
@@ -6660,6 +6668,9 @@ The following constants are meant for use with `fs.open()`.
66606668
</tr>
66616669
</table>
66626670
6671+
On Windows, only `O_APPEND`, `O_CREAT`, `O_EXCL`, `O_RDONLY`, `O_RDWR`,
6672+
`O_TRUNC`, `O_WRONLY` and `UV_FS_O_FILEMAP` are available.
6673+
66636674
##### File type constants
66646675
66656676
The following constants are meant for use with the {fs.Stats} object's
@@ -6704,6 +6715,9 @@ The following constants are meant for use with the {fs.Stats} object's
67046715
</tr>
67056716
</table>
67066717
6718+
On Windows, only `S_IFCHR`, `S_IFDIR`, `S_IFLNK`, `S_IFMT`, and `S_IFREG`,
6719+
are available.
6720+
67076721
##### File mode constants
67086722
67096723
The following constants are meant for use with the {fs.Stats} object's
@@ -6764,6 +6778,8 @@ The following constants are meant for use with the {fs.Stats} object's
67646778
</tr>
67656779
</table>
67666780
6781+
On Windows, only `S_IRUSR` and `S_IWUSR` are available.
6782+
67676783
## Notes
67686784
67696785
### Ordering of callback and promise-based operations

src/node_constants.cc

Lines changed: 10 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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)