Skip to content

Commit 1281a48

Browse files
ilg-ultargos
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 <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c4f7e93 commit 1281a48

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
@@ -6526,7 +6526,11 @@ operations.
65266526
65276527
The following constants are exported by `fs.constants`.
65286528
6529-
Not every constant will be available on every operating system.
6529+
Not every constant will be available on every operating system;
6530+
this is especially important for Windows, where many of the POSIX specific
6531+
definitions are not available.
6532+
For portable applications it is recommended to check for their presence
6533+
before use.
65306534
65316535
To use more than one constant, use the bitwise OR `|` operator.
65326536
@@ -6579,6 +6583,8 @@ The following constants are meant for use as the `mode` parameter passed to
65796583
</tr>
65806584
</table>
65816585
6586+
The definitions are also available on Windows.
6587+
65826588
##### File copy constants
65836589
65846590
The following constants are meant for use with [`fs.copyFile()`][].
@@ -6607,6 +6613,8 @@ The following constants are meant for use with [`fs.copyFile()`][].
66076613
</tr>
66086614
</table>
66096615
6616+
The definitions are also available on Windows.
6617+
66106618
##### File open constants
66116619
66126620
The following constants are meant for use with `fs.open()`.
@@ -6701,6 +6709,9 @@ The following constants are meant for use with `fs.open()`.
67016709
</tr>
67026710
</table>
67036711
6712+
On Windows, only `O_APPEND`, `O_CREAT`, `O_EXCL`, `O_RDONLY`, `O_RDWR`,
6713+
`O_TRUNC`, `O_WRONLY` and `UV_FS_O_FILEMAP` are available.
6714+
67046715
##### File type constants
67056716
67066717
The following constants are meant for use with the {fs.Stats} object's
@@ -6745,6 +6756,9 @@ The following constants are meant for use with the {fs.Stats} object's
67456756
</tr>
67466757
</table>
67476758
6759+
On Windows, only `S_IFCHR`, `S_IFDIR`, `S_IFLNK`, `S_IFMT`, and `S_IFREG`,
6760+
are available.
6761+
67486762
##### File mode constants
67496763
67506764
The following constants are meant for use with the {fs.Stats} object's
@@ -6805,6 +6819,8 @@ The following constants are meant for use with the {fs.Stats} object's
68056819
</tr>
68066820
</table>
68076821
6822+
On Windows, only `S_IRUSR` and `S_IWUSR` are available.
6823+
68086824
## Notes
68096825
68106826
### 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)