2018-10-17 14:40:08 +05:30
|
|
|
'use strict';
|
|
|
|
|
|
2020-10-03 13:01:57 -07:00
|
|
|
// This test makes sure that `writeFile()` always writes from the current
|
|
|
|
|
// position of the file, instead of truncating the file, when used with file
|
|
|
|
|
// descriptors.
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
const assert = require('assert');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* writeFileSync() test. */
|
2023-08-15 22:45:14 +09:00
|
|
|
const filename = tmpdir.resolve('test.txt');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
|
/* Open the file descriptor. */
|
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
2021-05-26 18:44:53 +03:00
|
|
|
try {
|
|
|
|
|
/* Write only five characters, so that the position moves to five. */
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.writeSync(fd, 'Hello'), 5);
|
|
|
|
|
assert.strictEqual(fs.readFileSync(filename).toString(), 'Hello');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
2021-05-26 18:44:53 +03:00
|
|
|
/* Write some more with writeFileSync(). */
|
|
|
|
|
fs.writeFileSync(fd, 'World');
|
2019-07-30 07:50:21 +01:00
|
|
|
|
2021-05-26 18:44:53 +03:00
|
|
|
/* New content should be written at position five, instead of zero. */
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
|
2021-05-26 18:44:53 +03:00
|
|
|
} finally {
|
|
|
|
|
fs.closeSync(fd);
|
|
|
|
|
}
|
2018-10-17 14:40:08 +05:30
|
|
|
}
|
|
|
|
|
|
2021-05-26 18:44:53 +03:00
|
|
|
const fdsToCloseOnExit = [];
|
|
|
|
|
process.on('beforeExit', common.mustCall(() => {
|
|
|
|
|
for (const fd of fdsToCloseOnExit) {
|
|
|
|
|
try {
|
|
|
|
|
fs.closeSync(fd);
|
|
|
|
|
} catch {
|
|
|
|
|
// Failed to close, ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
2018-10-17 14:40:08 +05:30
|
|
|
{
|
|
|
|
|
/* writeFile() test. */
|
2023-08-15 22:45:14 +09:00
|
|
|
const file = tmpdir.resolve('test1.txt');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
|
/* Open the file descriptor. */
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.open(file, 'w', common.mustSucceed((fd) => {
|
2021-05-26 18:44:53 +03:00
|
|
|
fdsToCloseOnExit.push(fd);
|
2018-10-17 14:40:08 +05:30
|
|
|
/* Write only five characters, so that the position moves to five. */
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.write(fd, 'Hello', common.mustSucceed((bytes) => {
|
2018-10-17 14:40:08 +05:30
|
|
|
assert.strictEqual(bytes, 5);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(file).toString(), 'Hello');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
|
/* Write some more with writeFile(). */
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.writeFile(fd, 'World', common.mustSucceed(() => {
|
2018-10-17 14:40:08 +05:30
|
|
|
/* New content should be written at position five, instead of zero. */
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(file).toString(), 'HelloWorld');
|
2018-10-17 14:40:08 +05:30
|
|
|
}));
|
|
|
|
|
}));
|
|
|
|
|
}));
|
|
|
|
|
}
|
2021-05-09 02:35:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test read-only file descriptor
|
|
|
|
|
{
|
2023-08-15 22:45:14 +09:00
|
|
|
const file = tmpdir.resolve('test.txt');
|
2021-05-09 02:35:53 +08:00
|
|
|
|
|
|
|
|
fs.open(file, 'r', common.mustSucceed((fd) => {
|
2021-05-26 18:44:53 +03:00
|
|
|
fdsToCloseOnExit.push(fd);
|
2022-01-06 21:45:31 -05:00
|
|
|
fs.writeFile(fd, 'World', common.expectsError(/EBADF/));
|
2021-05-09 02:35:53 +08:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test with an AbortSignal
|
|
|
|
|
{
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const signal = controller.signal;
|
2023-08-15 22:45:14 +09:00
|
|
|
const file = tmpdir.resolve('test.txt');
|
2021-05-09 02:35:53 +08:00
|
|
|
|
|
|
|
|
fs.open(file, 'w', common.mustSucceed((fd) => {
|
2021-05-26 18:44:53 +03:00
|
|
|
fdsToCloseOnExit.push(fd);
|
2021-05-09 02:35:53 +08:00
|
|
|
fs.writeFile(fd, 'World', { signal }, common.expectsError({
|
|
|
|
|
name: 'AbortError'
|
|
|
|
|
}));
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
controller.abort();
|
|
|
|
|
}
|