From 1dcbf73552fe5774e58293cfef341208e7d9ab6b Mon Sep 17 00:00:00 2001 From: Liviu Ionescu Date: Thu, 21 Apr 2022 07:29:31 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/41591 PR-URL: https://github.com/nodejs/node/pull/42757 Refs: https://github.com/nodejs/node/issues/41591 Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- doc/api/fs.md | 18 +++++++++++++++++- src/node_constants.cc | 10 ++++++++++ test/parallel/test-fs-constants.js | 8 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-fs-constants.js diff --git a/doc/api/fs.md b/doc/api/fs.md index c6e5acb874c52d..c360c7a35e1921 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -6486,7 +6486,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. @@ -6539,6 +6543,8 @@ The following constants are meant for use as the `mode` parameter passed to +The definitions are also available on Windows. + ##### File copy constants The following constants are meant for use with [`fs.copyFile()`][]. @@ -6567,6 +6573,8 @@ The following constants are meant for use with [`fs.copyFile()`][]. +The definitions are also available on Windows. + ##### File open constants The following constants are meant for use with `fs.open()`. @@ -6661,6 +6669,9 @@ The following constants are meant for use with `fs.open()`. +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 @@ -6705,6 +6716,9 @@ The following constants are meant for use with the {fs.Stats} object's +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 @@ -6765,6 +6779,8 @@ The following constants are meant for use with the {fs.Stats} object's +On Windows, only `S_IRUSR` and `S_IWUSR` are available. + ## Notes ### Ordering of callback and promise-based operations diff --git a/src/node_constants.cc b/src/node_constants.cc index 38c8f2738b4bad..3269e3003acd4d 100644 --- a/src/node_constants.cc +++ b/src/node_constants.cc @@ -47,6 +47,16 @@ #include #endif +#if defined(_WIN32) +#include // _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 #include #include diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js new file mode 100644 index 00000000000000..49bcabd80873b4 --- /dev/null +++ b/test/parallel/test-fs-constants.js @@ -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);