Skip to content

Commit 22e3eb6

Browse files
cjihrigtargos
authored andcommitted
fs: add flush option to writeFile() functions
This commit adds a 'flush' option to the fs.writeFile family of functions. Refs: #49886 PR-URL: #50009 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent 88c739b commit 22e3eb6

File tree

4 files changed

+210
-14
lines changed

4 files changed

+210
-14
lines changed

doc/api/fs.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,9 @@ All the [caveats][] for `fs.watch()` also apply to `fsPromises.watch()`.
17181718
<!-- YAML
17191719
added: v10.0.0
17201720
changes:
1721+
- version: REPLACEME
1722+
pr-url: https://github.com/nodejs/node/pull/50009
1723+
description: The `flush` option is now supported.
17211724
- version:
17221725
- v15.14.0
17231726
- v14.18.0
@@ -1741,6 +1744,9 @@ changes:
17411744
* `encoding` {string|null} **Default:** `'utf8'`
17421745
* `mode` {integer} **Default:** `0o666`
17431746
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
1747+
* `flush` {boolean} If all data is successfully written to the file, and
1748+
`flush` is `true`, `filehandle.sync()` is used to flush the data.
1749+
**Default:** `false`.
17441750
* `signal` {AbortSignal} allows aborting an in-progress writeFile
17451751
* Returns: {Promise} Fulfills with `undefined` upon success.
17461752
@@ -4849,6 +4855,9 @@ details.
48494855
<!-- YAML
48504856
added: v0.1.29
48514857
changes:
4858+
- version: REPLACEME
4859+
pr-url: https://github.com/nodejs/node/pull/50009
4860+
description: The `flush` option is now supported.
48524861
- version: v19.0.0
48534862
pr-url: https://github.com/nodejs/node/pull/42796
48544863
description: Passing to the `string` parameter an object with an own
@@ -4906,6 +4915,9 @@ changes:
49064915
* `encoding` {string|null} **Default:** `'utf8'`
49074916
* `mode` {integer} **Default:** `0o666`
49084917
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
4918+
* `flush` {boolean} If all data is successfully written to the file, and
4919+
`flush` is `true`, `fs.fsync()` is used to flush the data.
4920+
**Default:** `false`.
49094921
* `signal` {AbortSignal} allows aborting an in-progress writeFile
49104922
* `callback` {Function}
49114923
* `err` {Error|AggregateError}
@@ -6131,6 +6143,9 @@ this API: [`fs.utimes()`][].
61316143
<!-- YAML
61326144
added: v0.1.29
61336145
changes:
6146+
- version: REPLACEME
6147+
pr-url: https://github.com/nodejs/node/pull/50009
6148+
description: The `flush` option is now supported.
61346149
- version: v19.0.0
61356150
pr-url: https://github.com/nodejs/node/pull/42796
61366151
description: Passing to the `data` parameter an object with an own
@@ -6165,8 +6180,9 @@ changes:
61656180
* `encoding` {string|null} **Default:** `'utf8'`
61666181
* `mode` {integer} **Default:** `0o666`
61676182
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
6168-
6169-
Returns `undefined`.
6183+
* `flush` {boolean} If all data is successfully written to the file, and
6184+
`flush` is `true`, `fs.fsyncSync()` is used to flush the data.
6185+
Returns `undefined`.
61706186

61716187
The `mode` option only affects the newly created file. See [`fs.open()`][]
61726188
for more details.

lib/fs.js

+50-9
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ function lutimesSync(path, atime, mtime) {
22142214
handleErrorFromBinding(ctx);
22152215
}
22162216

2217-
function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
2217+
function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback) {
22182218
if (signal?.aborted) {
22192219
const abortError = new AbortError(undefined, { cause: signal?.reason });
22202220
if (isUserFd) {
@@ -2237,15 +2237,33 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
22372237
});
22382238
}
22392239
} else if (written === length) {
2240-
if (isUserFd) {
2241-
callback(null);
2240+
if (!flush) {
2241+
if (isUserFd) {
2242+
callback(null);
2243+
} else {
2244+
fs.close(fd, callback);
2245+
}
22422246
} else {
2243-
fs.close(fd, callback);
2247+
fs.fsync(fd, (syncErr) => {
2248+
if (syncErr) {
2249+
if (isUserFd) {
2250+
callback(syncErr);
2251+
} else {
2252+
fs.close(fd, (err) => {
2253+
callback(aggregateTwoErrors(err, syncErr));
2254+
});
2255+
}
2256+
} else if (isUserFd) {
2257+
callback(null);
2258+
} else {
2259+
fs.close(fd, callback);
2260+
}
2261+
});
22442262
}
22452263
} else {
22462264
offset += written;
22472265
length -= written;
2248-
writeAll(fd, isUserFd, buffer, offset, length, signal, callback);
2266+
writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback);
22492267
}
22502268
});
22512269
}
@@ -2259,14 +2277,23 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
22592277
* mode?: number;
22602278
* flag?: string;
22612279
* signal?: AbortSignal;
2280+
* flush?: boolean;
22622281
* } | string} [options]
22632282
* @param {(err?: Error) => any} callback
22642283
* @returns {void}
22652284
*/
22662285
function writeFile(path, data, options, callback) {
22672286
callback = maybeCallback(callback || options);
2268-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
2287+
options = getOptions(options, {
2288+
encoding: 'utf8',
2289+
mode: 0o666,
2290+
flag: 'w',
2291+
flush: false,
2292+
});
22692293
const flag = options.flag || 'w';
2294+
const flush = options.flush ?? false;
2295+
2296+
validateBoolean(flush, 'options.flush');
22702297

22712298
if (!isArrayBufferView(data)) {
22722299
validateStringAfterArrayBufferView(data, 'data');
@@ -2276,7 +2303,7 @@ function writeFile(path, data, options, callback) {
22762303
if (isFd(path)) {
22772304
const isUserFd = true;
22782305
const signal = options.signal;
2279-
writeAll(path, isUserFd, data, 0, data.byteLength, signal, callback);
2306+
writeAll(path, isUserFd, data, 0, data.byteLength, signal, flush, callback);
22802307
return;
22812308
}
22822309

@@ -2289,7 +2316,7 @@ function writeFile(path, data, options, callback) {
22892316
} else {
22902317
const isUserFd = false;
22912318
const signal = options.signal;
2292-
writeAll(fd, isUserFd, data, 0, data.byteLength, signal, callback);
2319+
writeAll(fd, isUserFd, data, 0, data.byteLength, signal, flush, callback);
22932320
}
22942321
});
22952322
}
@@ -2302,11 +2329,21 @@ function writeFile(path, data, options, callback) {
23022329
* encoding?: string | null;
23032330
* mode?: number;
23042331
* flag?: string;
2332+
* flush?: boolean;
23052333
* } | string} [options]
23062334
* @returns {void}
23072335
*/
23082336
function writeFileSync(path, data, options) {
2309-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
2337+
options = getOptions(options, {
2338+
encoding: 'utf8',
2339+
mode: 0o666,
2340+
flag: 'w',
2341+
flush: false,
2342+
});
2343+
2344+
const flush = options.flush ?? false;
2345+
2346+
validateBoolean(flush, 'options.flush');
23102347

23112348
if (!isArrayBufferView(data)) {
23122349
validateStringAfterArrayBufferView(data, 'data');
@@ -2326,6 +2363,10 @@ function writeFileSync(path, data, options) {
23262363
offset += written;
23272364
length -= written;
23282365
}
2366+
2367+
if (flush) {
2368+
fs.fsyncSync(fd);
2369+
}
23292370
} finally {
23302371
if (!isUserFd) fs.closeSync(fd);
23312372
}

lib/internal/fs/promises.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ async function handleFdClose(fileOpPromise, closeFunc) {
413413
);
414414
}
415415

416+
async function handleFdSync(fileOpPromise, handle) {
417+
return PromisePrototypeThen(
418+
fileOpPromise,
419+
(result) => PromisePrototypeThen(
420+
handle.sync(),
421+
() => result,
422+
(syncError) => PromiseReject(syncError),
423+
),
424+
(opError) => PromiseReject(opError),
425+
);
426+
}
427+
416428
async function fsCall(fn, handle, ...args) {
417429
assert(handle[kRefs] !== undefined,
418430
'handle must be an instance of FileHandle');
@@ -1003,8 +1015,16 @@ async function mkdtemp(prefix, options) {
10031015
}
10041016

10051017
async function writeFile(path, data, options) {
1006-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
1018+
options = getOptions(options, {
1019+
encoding: 'utf8',
1020+
mode: 0o666,
1021+
flag: 'w',
1022+
flush: false,
1023+
});
10071024
const flag = options.flag || 'w';
1025+
const flush = options.flush ?? false;
1026+
1027+
validateBoolean(flush, 'options.flush');
10081028

10091029
if (!isArrayBufferView(data) && !isCustomIterable(data)) {
10101030
validateStringAfterArrayBufferView(data, 'data');
@@ -1018,8 +1038,13 @@ async function writeFile(path, data, options) {
10181038
checkAborted(options.signal);
10191039

10201040
const fd = await open(path, flag, options.mode);
1021-
return handleFdClose(
1022-
writeFileHandle(fd, data, options.signal, options.encoding), fd.close);
1041+
let writeOp = writeFileHandle(fd, data, options.signal, options.encoding);
1042+
1043+
if (flush) {
1044+
writeOp = handleFdSync(writeOp, fd);
1045+
}
1046+
1047+
return handleFdClose(writeOp, fd.close);
10231048
}
10241049

10251050
function isCustomIterable(obj) {
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const assert = require('node:assert');
5+
const fs = require('node:fs');
6+
const fsp = require('node:fs/promises');
7+
const test = require('node:test');
8+
const data = 'foo';
9+
let cnt = 0;
10+
11+
function nextFile() {
12+
return tmpdir.resolve(`${cnt++}.out`);
13+
}
14+
15+
tmpdir.refresh();
16+
17+
test('synchronous version', async (t) => {
18+
await t.test('validation', (t) => {
19+
for (const v of ['true', '', 0, 1, [], {}, Symbol()]) {
20+
assert.throws(() => {
21+
fs.writeFileSync(nextFile(), data, { flush: v });
22+
}, { code: 'ERR_INVALID_ARG_TYPE' });
23+
}
24+
});
25+
26+
await t.test('performs flush', (t) => {
27+
const spy = t.mock.method(fs, 'fsyncSync');
28+
const file = nextFile();
29+
fs.writeFileSync(file, data, { flush: true });
30+
const calls = spy.mock.calls;
31+
assert.strictEqual(calls.length, 1);
32+
assert.strictEqual(calls[0].result, undefined);
33+
assert.strictEqual(calls[0].error, undefined);
34+
assert.strictEqual(calls[0].arguments.length, 1);
35+
assert.strictEqual(typeof calls[0].arguments[0], 'number');
36+
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
37+
});
38+
39+
await t.test('does not perform flush', (t) => {
40+
const spy = t.mock.method(fs, 'fsyncSync');
41+
42+
for (const v of [undefined, null, false]) {
43+
const file = nextFile();
44+
fs.writeFileSync(file, data, { flush: v });
45+
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
46+
}
47+
48+
assert.strictEqual(spy.mock.calls.length, 0);
49+
});
50+
});
51+
52+
test('callback version', async (t) => {
53+
await t.test('validation', (t) => {
54+
for (const v of ['true', '', 0, 1, [], {}, Symbol()]) {
55+
assert.throws(() => {
56+
fs.writeFileSync(nextFile(), data, { flush: v });
57+
}, { code: 'ERR_INVALID_ARG_TYPE' });
58+
}
59+
});
60+
61+
await t.test('performs flush', (t, done) => {
62+
const spy = t.mock.method(fs, 'fsync');
63+
const file = nextFile();
64+
fs.writeFile(file, data, { flush: true }, common.mustSucceed(() => {
65+
const calls = spy.mock.calls;
66+
assert.strictEqual(calls.length, 1);
67+
assert.strictEqual(calls[0].result, undefined);
68+
assert.strictEqual(calls[0].error, undefined);
69+
assert.strictEqual(calls[0].arguments.length, 2);
70+
assert.strictEqual(typeof calls[0].arguments[0], 'number');
71+
assert.strictEqual(typeof calls[0].arguments[1], 'function');
72+
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
73+
done();
74+
}));
75+
});
76+
77+
await t.test('does not perform flush', (t, done) => {
78+
const values = [undefined, null, false];
79+
const spy = t.mock.method(fs, 'fsync');
80+
let cnt = 0;
81+
82+
for (const v of values) {
83+
const file = nextFile();
84+
85+
fs.writeFile(file, data, { flush: v }, common.mustSucceed(() => {
86+
assert.strictEqual(fs.readFileSync(file, 'utf8'), data);
87+
cnt++;
88+
89+
if (cnt === values.length) {
90+
assert.strictEqual(spy.mock.calls.length, 0);
91+
done();
92+
}
93+
}));
94+
}
95+
});
96+
});
97+
98+
test('promise based version', async (t) => {
99+
await t.test('validation', async (t) => {
100+
for (const v of ['true', '', 0, 1, [], {}, Symbol()]) {
101+
await assert.rejects(() => {
102+
return fsp.writeFile(nextFile(), data, { flush: v });
103+
}, { code: 'ERR_INVALID_ARG_TYPE' });
104+
}
105+
});
106+
107+
await t.test('success path', async (t) => {
108+
for (const v of [undefined, null, false, true]) {
109+
const file = nextFile();
110+
await fsp.writeFile(file, data, { flush: v });
111+
assert.strictEqual(await fsp.readFile(file, 'utf8'), data);
112+
}
113+
});
114+
});

0 commit comments

Comments
 (0)