mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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: https://github.com/nodejs/node/issues/31926 Backport-PR-URL: https://github.com/nodejs/node/pull/32172 PR-URL: https://github.com/nodejs/node/pull/32006 Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
1 parent
8d1eeb1ae5
commit
010814856a
1 file changed
+5
-13
@@ -1220,9 +1220,9 @@ function futimesSync(fd, atime, mtime) {
|
||||
handleErrorFromBinding(ctx);
|
||||
}
|
||||
|
||||
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
|
||||
function writeAll(fd, isUserFd, buffer, offset, length, callback) {
|
||||
// write(fd, buffer, offset, length, position, callback)
|
||||
fs.write(fd, buffer, offset, length, position, (writeErr, written) => {
|
||||
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
|
||||
if (writeErr) {
|
||||
if (isUserFd) {
|
||||
callback(writeErr);
|
||||
@@ -1240,10 +1240,7 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
|
||||
} else {
|
||||
offset += written;
|
||||
length -= written;
|
||||
if (position !== null) {
|
||||
position += written;
|
||||
}
|
||||
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
|
||||
writeAll(fd, isUserFd, buffer, offset, length, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1269,9 +1266,8 @@ function writeFile(path, data, options, callback) {
|
||||
function writeFd(fd, isUserFd) {
|
||||
const buffer = isArrayBufferView(data) ?
|
||||
data : Buffer.from('' + data, options.encoding || 'utf8');
|
||||
const position = (/a/.test(flag) || isUserFd) ? null : 0;
|
||||
|
||||
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, position, callback);
|
||||
writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, callback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1287,15 +1283,11 @@ function writeFileSync(path, data, options) {
|
||||
}
|
||||
let offset = 0;
|
||||
let length = data.byteLength;
|
||||
let position = (/a/.test(flag) || isUserFd) ? null : 0;
|
||||
try {
|
||||
while (length > 0) {
|
||||
const written = fs.writeSync(fd, data, offset, length, position);
|
||||
const written = fs.writeSync(fd, data, offset, length);
|
||||
offset += written;
|
||||
length -= written;
|
||||
if (position !== null) {
|
||||
position += written;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (!isUserFd) fs.closeSync(fd);
|
||||
|
||||
Reference in new issue
Block a user