Skip to content

Commit

Permalink
fs: adjust truncate mode from r+ to a
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed Sep 11, 2022
1 parent 8bf7754 commit 375f0a7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
13 changes: 12 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ try {
```
If the file previously was shorter than `len` bytes, it is extended, and the
extended part is filled with null bytes (`'\0'`):
extended part is filled with null bytes (`'\0'`).
If `len` is negative then `0` will be used.
Expand Down Expand Up @@ -1481,6 +1481,10 @@ automatically be normalized to absolute path.
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43315
description: Creates a file if it didn't exist.
-->
* `path` {string|Buffer|URL}
Expand Down Expand Up @@ -4073,6 +4077,9 @@ $ tree .
<!-- YAML
added: v0.8.6
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43315
description: Creates a file if it didn't exist.
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the `callback` argument
Expand Down Expand Up @@ -5781,6 +5788,10 @@ this API: [`fs.symlink()`][].
<!-- YAML
added: v0.8.6
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43315
description: Creates a file if it didn't exist.
-->
* `path` {string|Buffer|URL}
Expand Down
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ function truncate(path, len, callback) {
validateInteger(len, 'len');
len = MathMax(0, len);
callback = maybeCallback(callback);
fs.open(path, 'r+', (er, fd) => {
fs.open(path, 'a', (er, fd) => {
if (er) return callback(er);
const req = new FSReqCallback();
req.oncomplete = function oncomplete(er) {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ function truncateSync(path, len) {
len = 0;
}
// Allow error to be thrown, but still close fd.
const fd = fs.openSync(path, 'r+');
const fd = fs.openSync(path, 'a');
let ret;

try {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ async function rename(oldPath, newPath) {
}

async function truncate(path, len = 0) {
const fd = await open(path, 'r+');
const fd = await open(path, 'a');
return handleFdClose(ftruncate(fd, len), fd.close);
}

Expand Down
48 changes: 48 additions & 0 deletions test/parallel/test-fs-truncate-nonreadable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const common = require('../common');

// This test ensures that truncate works on non-readable files

const assert = require('assert');
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const expected = Buffer.from('good');

function checkContents(filepath) {
fs.chmodSync(filepath, 0o400);
assert.deepStrictEqual(
fs.readFileSync(filepath),
expected
);
}

(async () => {
const MODE = 0o200;
{
const filepath = path.resolve(tmpdir.path, 'promises.txt');
fs.writeFileSync(filepath, 'good bad');
fs.chmodSync(filepath, MODE);
await fsPromises.truncate(filepath, 4);
checkContents(filepath);
}
{
const filepath = path.resolve(tmpdir.path, 'callback.txt');
fs.writeFileSync(filepath, 'good bad');
fs.chmodSync(filepath, MODE);
fs.truncate(filepath, 4, common.mustSucceed(() => {
checkContents(filepath);
}));
}
{
const filepath = path.resolve(tmpdir.path, 'synchronous.txt');
fs.writeFileSync(filepath, 'good bad');
fs.chmodSync(filepath, MODE);
fs.truncateSync(filepath, 4);
checkContents(filepath);
}
})().then(common.mustCall());
22 changes: 11 additions & 11 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,17 @@ function testFtruncate(cb) {
}

{
const file8 = path.resolve(tmp, 'non-existent-truncate-file.txt');
const validateError = (err) => {
assert.strictEqual(file8, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, open '${file8}'`);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'open');
return true;
};
fs.truncate(file8, 0, common.mustCall(validateError));
const file8 = path.resolve(tmp, 'non-existent-truncate-file-1.txt');
fs.truncate(file8, 0, common.mustSucceed(() => {
assert(fs.readFileSync(file8).equals(Buffer.from('')));
}));
}

{
const file9 = path.resolve(tmp, 'non-existent-truncate-file-2.txt');
fs.truncate(file9, 2, common.mustSucceed(() => {
assert(fs.readFileSync(file9).equals(Buffer.from('\u0000\u0000')));
}));
}

['', false, null, {}, []].forEach((input) => {
Expand Down

0 comments on commit 375f0a7

Please sign in to comment.