Skip to content

Commit 0108148

Browse files
mildsunrisetargos
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 8d1eeb1 commit 0108148

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
@@ -1220,9 +1220,9 @@ function futimesSync(fd, atime, mtime) {
12201220
handleErrorFromBinding(ctx);
12211221
}
12221222

1223-
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
1223+
function writeAll(fd, isUserFd, buffer, offset, length, callback) {
12241224
// write(fd, buffer, offset, length, position, callback)
1225-
fs.write(fd, buffer, offset, length, position, (writeErr, written) => {
1225+
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
12261226
if (writeErr) {
12271227
if (isUserFd) {
12281228
callback(writeErr);
@@ -1240,10 +1240,7 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
12401240
} else {
12411241
offset += written;
12421242
length -= written;
1243-
if (position !== null) {
1244-
position += written;
1245-
}
1246-
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
1243+
writeAll(fd, isUserFd, buffer, offset, length, callback);
12471244
}
12481245
});
12491246
}
@@ -1269,9 +1266,8 @@ function writeFile(path, data, options, callback) {
12691266
function writeFd(fd, isUserFd) {
12701267
const buffer = isArrayBufferView(data) ?
12711268
data : Buffer.from('' + data, options.encoding || 'utf8');
1272-
const position = (/a/.test(flag) || isUserFd) ? null : 0;
12731269

1274-
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, position, callback);
1270+
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, callback);
12751271
}
12761272
}
12771273

@@ -1287,15 +1283,11 @@ function writeFileSync(path, data, options) {
12871283
}
12881284
let offset = 0;
12891285
let length = data.byteLength;
1290-
let position = (/a/.test(flag) || isUserFd) ? null : 0;
12911286
try {
12921287
while (length > 0) {
1293-
const written = fs.writeSync(fd, data, offset, length, position);
1288+
const written = fs.writeSync(fd, data, offset, length);
12941289
offset += written;
12951290
length -= written;
1296-
if (position !== null) {
1297-
position += written;
1298-
}
12991291
}
13001292
} finally {
13011293
if (!isUserFd) fs.closeSync(fd);

0 commit comments

Comments
 (0)