Skip to content

Commit 1c4f4cc

Browse files
mildsunriseMylesBorins
authored andcommitted
fs: fix writeFile[Sync] for non-seekable files
Completely disables the use of positioned writes at writeFile and writeFileSync, which allows it to work with non-seekable files. Fixes: #31926 Backport-PR-URL: #32172 PR-URL: #32006 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1e05ddf commit 1c4f4cc

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

lib/fs.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -1240,9 +1240,9 @@ function futimesSync(fd, atime, mtime) {
12401240
handleErrorFromBinding(ctx);
12411241
}
12421242

1243-
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
1243+
function writeAll(fd, isUserFd, buffer, offset, length, callback) {
12441244
// write(fd, buffer, offset, length, position, callback)
1245-
fs.write(fd, buffer, offset, length, position, (writeErr, written) => {
1245+
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
12461246
if (writeErr) {
12471247
if (isUserFd) {
12481248
callback(writeErr);
@@ -1260,10 +1260,7 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
12601260
} else {
12611261
offset += written;
12621262
length -= written;
1263-
if (position !== null) {
1264-
position += written;
1265-
}
1266-
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
1263+
writeAll(fd, isUserFd, buffer, offset, length, callback);
12671264
}
12681265
});
12691266
}
@@ -1289,9 +1286,8 @@ function writeFile(path, data, options, callback) {
12891286
function writeFd(fd, isUserFd) {
12901287
const buffer = isArrayBufferView(data) ?
12911288
data : Buffer.from('' + data, options.encoding || 'utf8');
1292-
const position = (/a/.test(flag) || isUserFd) ? null : 0;
12931289

1294-
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, position, callback);
1290+
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, callback);
12951291
}
12961292
}
12971293

@@ -1307,15 +1303,11 @@ function writeFileSync(path, data, options) {
13071303
}
13081304
let offset = 0;
13091305
let length = data.byteLength;
1310-
let position = (/a/.test(flag) || isUserFd) ? null : 0;
13111306
try {
13121307
while (length > 0) {
1313-
const written = fs.writeSync(fd, data, offset, length, position);
1308+
const written = fs.writeSync(fd, data, offset, length);
13141309
offset += written;
13151310
length -= written;
1316-
if (position !== null) {
1317-
position += written;
1318-
}
13191311
}
13201312
} finally {
13211313
if (!isUserFd) fs.closeSync(fd);

0 commit comments

Comments
 (0)