Skip to content

Commit f69de13

Browse files
mildsunriseaddaleax
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 PR-URL: #32006 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 6f0ec79 commit f69de13

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

lib/fs.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,9 +1248,9 @@ function futimesSync(fd, atime, mtime) {
12481248
handleErrorFromBinding(ctx);
12491249
}
12501250

1251-
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
1251+
function writeAll(fd, isUserFd, buffer, offset, length, callback) {
12521252
// write(fd, buffer, offset, length, position, callback)
1253-
fs.write(fd, buffer, offset, length, position, (writeErr, written) => {
1253+
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
12541254
if (writeErr) {
12551255
if (isUserFd) {
12561256
callback(writeErr);
@@ -1268,10 +1268,7 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
12681268
} else {
12691269
offset += written;
12701270
length -= written;
1271-
if (position !== null) {
1272-
position += written;
1273-
}
1274-
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
1271+
writeAll(fd, isUserFd, buffer, offset, length, callback);
12751272
}
12761273
});
12771274
}
@@ -1288,7 +1285,7 @@ function writeFile(path, data, options, callback) {
12881285

12891286
if (isFd(path)) {
12901287
const isUserFd = true;
1291-
writeAll(path, isUserFd, data, 0, data.byteLength, null, callback);
1288+
writeAll(path, isUserFd, data, 0, data.byteLength, callback);
12921289
return;
12931290
}
12941291

@@ -1297,8 +1294,7 @@ function writeFile(path, data, options, callback) {
12971294
callback(openErr);
12981295
} else {
12991296
const isUserFd = false;
1300-
const position = /a/.test(flag) ? null : 0;
1301-
writeAll(fd, isUserFd, data, 0, data.byteLength, position, callback);
1297+
writeAll(fd, isUserFd, data, 0, data.byteLength, callback);
13021298
}
13031299
});
13041300
}
@@ -1318,15 +1314,11 @@ function writeFileSync(path, data, options) {
13181314

13191315
let offset = 0;
13201316
let length = data.byteLength;
1321-
let position = (/a/.test(flag) || isUserFd) ? null : 0;
13221317
try {
13231318
while (length > 0) {
1324-
const written = fs.writeSync(fd, data, offset, length, position);
1319+
const written = fs.writeSync(fd, data, offset, length);
13251320
offset += written;
13261321
length -= written;
1327-
if (position !== null) {
1328-
position += written;
1329-
}
13301322
}
13311323
} finally {
13321324
if (!isUserFd) fs.closeSync(fd);

0 commit comments

Comments
 (0)