Skip to content

Commit 1976284

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 5d15eb1 commit 1976284

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
@@ -6771,7 +6771,11 @@ operations.
67716771
67726772
The following constants are exported by `fs.constants`.
67736773
6774-
Not every constant will be available on every operating system.
6774+
Not every constant will be available on every operating system;
6775+
this is especially important for Windows, where many of the POSIX specific
6776+
definitions are not available.
6777+
For portable applications it is recommended to check for their presence
6778+
before use.
67756779
67766780
To use more than one constant, use the bitwise OR `|` operator.
67776781
@@ -6824,6 +6828,8 @@ The following constants are meant for use as the `mode` parameter passed to
68246828
</tr>
68256829
</table>
68266830
6831+
The definitions are also available on Windows.
6832+
68276833
##### File copy constants
68286834
68296835
The following constants are meant for use with [`fs.copyFile()`][].
@@ -6852,6 +6858,8 @@ The following constants are meant for use with [`fs.copyFile()`][].
68526858
</tr>
68536859
</table>
68546860
6861+
The definitions are also available on Windows.
6862+
68556863
##### File open constants
68566864
68576865
The following constants are meant for use with `fs.open()`.
@@ -6946,6 +6954,9 @@ The following constants are meant for use with `fs.open()`.
69466954
</tr>
69476955
</table>
69486956
6957+
On Windows, only `O_APPEND`, `O_CREAT`, `O_EXCL`, `O_RDONLY`, `O_RDWR`,
6958+
`O_TRUNC`, `O_WRONLY` and `UV_FS_O_FILEMAP` are available.
6959+
69496960
##### File type constants
69506961
69516962
The following constants are meant for use with the {fs.Stats} object's
@@ -6990,6 +7001,9 @@ The following constants are meant for use with the {fs.Stats} object's
69907001
</tr>
69917002
</table>
69927003
7004+
On Windows, only `S_IFCHR`, `S_IFDIR`, `S_IFLNK`, `S_IFMT`, and `S_IFREG`,
7005+
are available.
7006+
69937007
##### File mode constants
69947008
69957009
The following constants are meant for use with the {fs.Stats} object's
@@ -7050,6 +7064,8 @@ The following constants are meant for use with the {fs.Stats} object's
70507064
</tr>
70517065
</table>
70527066
7067+
On Windows, only `S_IRUSR` and `S_IWUSR` are available.
7068+
70537069
## Notes
70547070
70557071
### 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)