Skip to content

Commit ad3a583

Browse files
committed
fs: add flush option to writeFile() functions
This commit adds a 'flush' option to the fs.writeFile family of functions. Refs: #49886
1 parent 47b2883 commit ad3a583

File tree

4 files changed

+209
-13
lines changed

4 files changed

+209
-13
lines changed

doc/api/fs.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,9 @@ All the [caveats][] for `fs.watch()` also apply to `fsPromises.watch()`.
17421742
<!-- YAML
17431743
added: v10.0.0
17441744
changes:
1745+
- version: REPLACEME
1746+
pr-url: https://github.com/nodejs/node/pull/50009
1747+
description: The `flush` option is now supported.
17451748
- version:
17461749
- v15.14.0
17471750
- v14.18.0
@@ -1765,6 +1768,9 @@ changes:
17651768
* `encoding` {string|null} **Default:** `'utf8'`
17661769
* `mode` {integer} **Default:** `0o666`
17671770
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
1771+
* `flush` {boolean} If all data is successfully written to the file, and
1772+
`flush` is `true`, `filehandle.sync()` is used to flush the data.
1773+
**Default:** `false`.
17681774
* `signal` {AbortSignal} allows aborting an in-progress writeFile
17691775
* Returns: {Promise} Fulfills with `undefined` upon success.
17701776
@@ -4879,6 +4885,9 @@ details.
48794885
<!-- YAML
48804886
added: v0.1.29
48814887
changes:
4888+
- version: REPLACEME
4889+
pr-url: https://github.com/nodejs/node/pull/50009
4890+
description: The `flush` option is now supported.
48824891
- version: v19.0.0
48834892
pr-url: https://github.com/nodejs/node/pull/42796
48844893
description: Passing to the `string` parameter an object with an own
@@ -4936,6 +4945,9 @@ changes:
49364945
* `encoding` {string|null} **Default:** `'utf8'`
49374946
* `mode` {integer} **Default:** `0o666`
49384947
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
4948+
* `flush` {boolean} If all data is successfully written to the file, and
4949+
`flush` is `true`, `fs.fsync()` is used to flush the data.
4950+
**Default:** `false`.
49394951
* `signal` {AbortSignal} allows aborting an in-progress writeFile
49404952
* `callback` {Function}
49414953
* `err` {Error|AggregateError}
@@ -6167,6 +6179,9 @@ this API: [`fs.utimes()`][].
61676179
<!-- YAML
61686180
added: v0.1.29
61696181
changes:
6182+
- version: REPLACEME
6183+
pr-url: https://github.com/nodejs/node/pull/50009
6184+
description: The `flush` option is now supported.
61706185
- version: v19.0.0
61716186
pr-url: https://github.com/nodejs/node/pull/42796
61726187
description: Passing to the `data` parameter an object with an own
@@ -6201,7 +6216,8 @@ changes:
62016216
* `encoding` {string|null} **Default:** `'utf8'`
62026217
* `mode` {integer} **Default:** `0o666`
62036218
* `flag` {string} See [support of file system `flags`][]. **Default:** `'w'`.
6204-
6219+
* `flush` {boolean} If all data is successfully written to the file, and
6220+
`flush` is `true`, `fs.fsyncSync()` is used to flush the data.
62056221
Returns `undefined`.
62066222

62076223
The `mode` option only affects the newly created file. See [`fs.open()`][]

lib/fs.js

+50-9
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,7 @@ function lutimesSync(path, atime, mtime) {
22172217
handleErrorFromBinding(ctx);
22182218
}
22192219

2220-
function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
2220+
function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback) {
22212221
if (signal?.aborted) {
22222222
const abortError = new AbortError(undefined, { cause: signal?.reason });
22232223
if (isUserFd) {
@@ -2240,15 +2240,33 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
22402240
});
22412241
}
22422242
} else if (written === length) {
2243-
if (isUserFd) {
2244-
callback(null);
2243+
if (!flush) {
2244+
if (isUserFd) {
2245+
callback(null);
2246+
} else {
2247+
fs.close(fd, callback);
2248+
}
22452249
} else {
2246-
fs.close(fd, callback);
2250+
fs.fsync(fd, (syncErr) => {
2251+
if (syncErr) {
2252+
if (isUserFd) {
2253+
callback(syncErr);
2254+
} else {
2255+
fs.close(fd, (err) => {
2256+
callback(aggregateTwoErrors(err, syncErr));
2257+
});
2258+
}
2259+
} else if (isUserFd) {
2260+
callback(null);
2261+
} else {
2262+
fs.close(fd, callback);
2263+
}
2264+
});
22472265
}
22482266
} else {
22492267
offset += written;
22502268
length -= written;
2251-
writeAll(fd, isUserFd, buffer, offset, length, signal, callback);
2269+
writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback);
22522270
}
22532271
});
22542272
}
@@ -2262,14 +2280,23 @@ function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
22622280
* mode?: number;
22632281
* flag?: string;
22642282
* signal?: AbortSignal;
2283+
* flush?: boolean;
22652284
* } | string} [options]
22662285
* @param {(err?: Error) => any} callback
22672286
* @returns {void}
22682287
*/
22692288
function writeFile(path, data, options, callback) {
22702289
callback = maybeCallback(callback || options);
2271-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
2290+
options = getOptions(options, {
2291+
encoding: 'utf8',
2292+
mode: 0o666,
2293+
flag: 'w',
2294+
flush: false,
2295+
});
22722296
const flag = options.flag || 'w';
2297+
const flush = options.flush ?? false;
2298+
2299+
validateBoolean(flush, 'options.flush');
22732300

22742301
if (!isArrayBufferView(data)) {
22752302
validateStringAfterArrayBufferView(data, 'data');
@@ -2279,7 +2306,7 @@ function writeFile(path, data, options, callback) {
22792306
if (isFd(path)) {
22802307
const isUserFd = true;
22812308
const signal = options.signal;
2282-
writeAll(path, isUserFd, data, 0, data.byteLength, signal, callback);
2309+
writeAll(path, isUserFd, data, 0, data.byteLength, signal, flush, callback);
22832310
return;
22842311
}
22852312

@@ -2292,7 +2319,7 @@ function writeFile(path, data, options, callback) {
22922319
} else {
22932320
const isUserFd = false;
22942321
const signal = options.signal;
2295-
writeAll(fd, isUserFd, data, 0, data.byteLength, signal, callback);
2322+
writeAll(fd, isUserFd, data, 0, data.byteLength, signal, flush, callback);
22962323
}
22972324
});
22982325
}
@@ -2305,11 +2332,21 @@ function writeFile(path, data, options, callback) {
23052332
* encoding?: string | null;
23062333
* mode?: number;
23072334
* flag?: string;
2335+
* flush?: boolean;
23082336
* } | string} [options]
23092337
* @returns {void}
23102338
*/
23112339
function writeFileSync(path, data, options) {
2312-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
2340+
options = getOptions(options, {
2341+
encoding: 'utf8',
2342+
mode: 0o666,
2343+
flag: 'w',
2344+
flush: false,
2345+
});
2346+
2347+
const flush = options.flush ?? false;
2348+
2349+
validateBoolean(flush, 'options.flush');
23132350

23142351
if (!isArrayBufferView(data)) {
23152352
validateStringAfterArrayBufferView(data, 'data');
@@ -2329,6 +2366,10 @@ function writeFileSync(path, data, options) {
23292366
offset += written;
23302367
length -= written;
23312368
}
2369+
2370+
if (flush) {
2371+
fs.fsyncSync(fd);
2372+
}
23322373
} finally {
23332374
if (!isUserFd) fs.closeSync(fd);
23342375
}

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');
@@ -1006,8 +1018,16 @@ async function mkdtemp(prefix, options) {
10061018
}
10071019

10081020
async function writeFile(path, data, options) {
1009-
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
1021+
options = getOptions(options, {
1022+
encoding: 'utf8',
1023+
mode: 0o666,
1024+
flag: 'w',
1025+
flush: false,
1026+
});
10101027
const flag = options.flag || 'w';
1028+
const flush = options.flush ?? false;
1029+
1030+
validateBoolean(flush, 'options.flush');
10111031

10121032
if (!isArrayBufferView(data) && !isCustomIterable(data)) {
10131033
validateStringAfterArrayBufferView(data, 'data');
@@ -1021,8 +1041,13 @@ async function writeFile(path, data, options) {
10211041
checkAborted(options.signal);
10221042

10231043
const fd = await open(path, flag, options.mode);
1024-
return handleFdClose(
1025-
writeFileHandle(fd, data, options.signal, options.encoding), fd.close);
1044+
let writeOp = writeFileHandle(fd, data, options.signal, options.encoding);
1045+
1046+
if (flush) {
1047+
writeOp = handleFdSync(writeOp, fd);
1048+
}
1049+
1050+
return handleFdClose(writeOp, fd.close);
10261051
}
10271052

10281053
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)