Skip to content

fs: remove ability to call truncate with fd #57567

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

Merged
merged 1 commit into from
Mar 22, 2025
Merged
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
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1832,12 +1832,15 @@ and replaced with an identical, public `path.toNamespacedPath()` method.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57567
description: End-of-Life.
- version: v9.0.0
pr-url: https://github.com/nodejs/node/pull/15990
description: Runtime deprecation.
-->

Type: Runtime
Type: End-of-Life

`fs.truncate()` `fs.truncateSync()` usage with a file descriptor is
deprecated. Please use `fs.ftruncate()` or `fs.ftruncateSync()` to work with
Expand Down
20 changes: 0 additions & 20 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const {

const permission = require('internal/process/permission');

let truncateWarn = true;
let fs;

// Lazy loaded
Expand All @@ -167,16 +166,6 @@ let ReadFileContext;
let FileReadStream;
let FileWriteStream;

function showTruncateDeprecation() {
if (truncateWarn) {
process.emitWarning(
'Using fs.truncate with a file descriptor is deprecated. Please use ' +
'fs.ftruncate with a file descriptor instead.',
'DeprecationWarning', 'DEP0081');
truncateWarn = false;
}
}

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
Expand Down Expand Up @@ -1031,10 +1020,6 @@ function renameSync(oldPath, newPath) {
* @returns {void}
*/
function truncate(path, len, callback) {
if (typeof path === 'number') {
showTruncateDeprecation();
return fs.ftruncate(path, len, callback);
}
if (typeof len === 'function') {
callback = len;
len = 0;
Expand Down Expand Up @@ -1064,11 +1049,6 @@ function truncate(path, len, callback) {
* @returns {void}
*/
function truncateSync(path, len) {
if (typeof path === 'number') {
// legacy
showTruncateDeprecation();
return fs.ftruncateSync(path, len);
}
if (len === undefined) {
len = 0;
}
Expand Down
27 changes: 0 additions & 27 deletions test/parallel/test-fs-truncate-fd.js

This file was deleted.

7 changes: 2 additions & 5 deletions test/parallel/test-fs-truncate-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const filename = path.resolve(tmp, 'truncate-sync-file.txt');

fs.writeFileSync(filename, 'hello world', 'utf8');

const fd = fs.openSync(filename, 'r+');
fs.truncateSync(filename, 5);
assert(fs.readFileSync(filename).equals(Buffer.from('hello')));

fs.truncateSync(fd, 5);
assert(fs.readFileSync(fd).equals(Buffer.from('hello')));

fs.closeSync(fd);
fs.unlinkSync(filename);
7 changes: 0 additions & 7 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ tmpdir.refresh();

let stat;

const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
' Please use fs.ftruncate with a file descriptor instead.';

// Check truncateSync
fs.writeFileSync(filename, data);
stat = fs.statSync(filename);
Expand Down Expand Up @@ -64,10 +61,6 @@ fs.ftruncateSync(fd);
stat = fs.statSync(filename);
assert.strictEqual(stat.size, 0);

// truncateSync
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
fs.truncateSync(fd);

fs.closeSync(fd);

// Async tests
Expand Down
Loading