Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: track file offset in writeFile of fs/promises #23136

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,19 @@ async function writeFileHandle(filehandle, data, options) {
data : Buffer.from('' + data, options.encoding || 'utf8');
let remaining = buffer.length;
if (remaining === 0) return;
// If the file is opened in APPEND mode set position as null, so that the
// data will be always written at the end of the file, otherwise start at 0.
let position = (options.flag || '').includes('a') ? null : 0;

do {
const { bytesWritten } =
await write(filehandle, buffer, 0,
Math.min(16384, buffer.length));
Math.min(16384, buffer.length), position);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positional writes don't work on streams, try running this variant of writeFile to stdout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, if we cannot seek the position in fd, it will fail. Wouldn't that be the expected behaviour in this case?

remaining -= bytesWritten;
buffer = buffer.slice(bytesWritten);
if (typeof position === 'number') {
position += bytesWritten;
}
} while (remaining > 0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

// This tests if the writeFile automatically adjusts the file offset even if
// the file is truncated.

const common = require('../common');
const fsPromises = require('fs').promises;
const path = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const tmpDir = tmpdir.path;
const Data = 'FS Promises';

tmpdir.refresh();

(async function() {
assert(!/A/.test(Data));
const fileName = path.resolve(tmpDir, 'temp.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.truncate();
await fileHandle.writeFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());

(async function() {
// In this case there is no truncate call, but still the contents of the file
// should be truncated before the new string is written.
assert(!/A/.test(Data));
const fileName = path.resolve(tmpDir, 'temp.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.writeFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());

(async function() {
// This tests appendFile as well, as appendFile internally uses writeFile
const fileName = path.resolve(tmpDir, 'temp-1.txt');
const fileHandle = await fsPromises.open(fileName, 'w');
await fileHandle.writeFile('A'.repeat(Data.length * 2));
await fileHandle.truncate();
await fileHandle.appendFile(Data);
await fileHandle.close();
assert.deepStrictEqual(await fsPromises.readFile(fileName, 'UTF-8'), Data);
})().then(common.mustCall());
18 changes: 18 additions & 0 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,23 @@ async function validateWriteFile() {
assert.deepStrictEqual(buffer, readFileData);
}

async function validateLargeWriteFile() {
const fileName = path.resolve(tmpDir, 'large.txt');
const handle = await open(fileName, 'w');
// 16385 is written as (16384 + 1) because, 16384 is the max chunk size used
// in the writeFile, the max chunk size is searchable, and the boundary
// testing is also done.
const buffer = Buffer.from(
Array.apply(null, { length: (16384 + 1) * 3 })
.map(Math.random)
.map((number) => (number * (1 << 8)))
);

await handle.writeFile(buffer);
await handle.close();
assert.deepStrictEqual(fs.readFileSync(fileName), buffer);
}

validateWriteFile()
.then(validateLargeWriteFile)
.then(common.mustCall());
17 changes: 17 additions & 0 deletions test/parallel/test-fs-promises-writefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ async function doWrite() {
assert.deepStrictEqual(data, buffer);
}

async function doLargeWrite() {
// 16385 is written as (16384 + 1) because, 16384 is the max chunk size used
// in the writeFile, the max chunk size is searchable, and the boundary
// testing is also done.
const buffer = Buffer.from(
Array.apply(null, { length: (16384 + 1) * 3 })
.map(Math.random)
.map((number) => (number * (1 << 8)))
);
const dest = path.resolve(tmpDir, 'large.txt');

await fsPromises.writeFile(dest, buffer);
const data = fs.readFileSync(dest);
assert.deepStrictEqual(data, buffer);
}

async function doAppend() {
await fsPromises.appendFile(dest, buffer2);
const data = fs.readFileSync(dest);
Expand All @@ -41,6 +57,7 @@ async function doReadWithEncoding() {
}

doWrite()
.then(doLargeWrite)
.then(doAppend)
.then(doRead)
.then(doReadWithEncoding)
Expand Down