Skip to content

Commit 6d60352

Browse files
authored
src: define O_SYNC, O_DSYNC and O_DIRECT on Windows
Signed-off-by: Kirill Saied <sayed.kirill@gmail.com> PR-URL: #64707 Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fd5b135 commit 6d60352

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎doc/api/fs.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8789,12 +8789,14 @@ The following constants are meant for use with `fs.open()`.
87898789
<tr>
87908790
<td><code>O_SYNC</code></td>
87918791
<td>Flag indicating that the file is opened for synchronized I/O with write
8792-
operations waiting for file integrity.</td>
8792+
operations waiting for file integrity. On Windows, this maps to
8793+
<code>FILE_FLAG_WRITE_THROUGH</code>.</td>
87938794
</tr>
87948795
<tr>
87958796
<td><code>O_DSYNC</code></td>
87968797
<td>Flag indicating that the file is opened for synchronized I/O with write
8797-
operations waiting for data integrity.</td>
8798+
operations waiting for data integrity. On Windows, this maps to
8799+
<code>FILE_FLAG_WRITE_THROUGH</code>.</td>
87988800
</tr>
87998801
<tr>
88008802
<td><code>O_SYMLINK</code></td>
@@ -8804,7 +8806,7 @@ The following constants are meant for use with `fs.open()`.
88048806
<tr>
88058807
<td><code>O_DIRECT</code></td>
88068808
<td>When set, an attempt will be made to minimize caching effects of file
8807-
I/O.</td>
8809+
I/O. On Windows, this maps to <code>FILE_FLAG_NO_BUFFERING</code>.</td>
88088810
</tr>
88098811
<tr>
88108812
<td><code>O_NONBLOCK</code></td>

‎src/node_constants.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,22 @@ void DefineFsConstants(Local<Object> target) {
11701170

11711171
#ifdef O_SYNC
11721172
NODE_DEFINE_CONSTANT(target, O_SYNC);
1173+
#elif UV_FS_O_SYNC != 0
1174+
// Windows lacks the POSIX O_SYNC macro, but libuv maps UV_FS_O_SYNC to
1175+
// FILE_FLAG_WRITE_THROUGH, so expose it under the portable name.
1176+
#define O_SYNC UV_FS_O_SYNC
1177+
NODE_DEFINE_CONSTANT(target, O_SYNC);
1178+
#undef O_SYNC
11731179
#endif
11741180

11751181
#ifdef O_DSYNC
11761182
NODE_DEFINE_CONSTANT(target, O_DSYNC);
1183+
#elif UV_FS_O_DSYNC != 0
1184+
// Windows lacks the POSIX O_DSYNC macro, but libuv maps UV_FS_O_DSYNC to
1185+
// FILE_FLAG_WRITE_THROUGH, so expose it under the portable name.
1186+
#define O_DSYNC UV_FS_O_DSYNC
1187+
NODE_DEFINE_CONSTANT(target, O_DSYNC);
1188+
#undef O_DSYNC
11771189
#endif
11781190

11791191

@@ -1183,6 +1195,12 @@ void DefineFsConstants(Local<Object> target) {
11831195

11841196
#ifdef O_DIRECT
11851197
NODE_DEFINE_CONSTANT(target, O_DIRECT);
1198+
#elif UV_FS_O_DIRECT != 0
1199+
// Windows lacks the POSIX O_DIRECT macro, but libuv maps UV_FS_O_DIRECT to
1200+
// FILE_FLAG_NO_BUFFERING, so expose it under the portable name.
1201+
#define O_DIRECT UV_FS_O_DIRECT
1202+
NODE_DEFINE_CONSTANT(target, O_DIRECT);
1203+
#undef O_DIRECT
11861204
#endif
11871205

11881206
#ifdef O_NONBLOCK

‎test/parallel/test-fs-constants.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const fs = require('fs');
44
const assert = require('assert');
55

66
// Check if the two constants accepted by chmod() on Windows are defined.
77
assert.notStrictEqual(fs.constants.S_IRUSR, undefined);
88
assert.notStrictEqual(fs.constants.S_IWUSR, undefined);
99

10+
// O_SYNC, O_DSYNC and O_DIRECT have no POSIX macro on Windows, but libuv
11+
// honors them via FILE_FLAG_WRITE_THROUGH / FILE_FLAG_NO_BUFFERING, so they
12+
// are exposed under their portable names with libuv's flag values.
13+
if (common.isWindows) {
14+
assert.strictEqual(fs.constants.O_SYNC, 0x08000000);
15+
assert.strictEqual(fs.constants.O_DSYNC, 0x04000000);
16+
assert.strictEqual(fs.constants.O_DIRECT, 0x02000000);
17+
}
18+
1019
// Check null prototype.
1120
assert.strictEqual(Object.getPrototypeOf(fs.constants), null);
1221

0 commit comments

Comments
 (0)