fs: remove ability to call truncate with fd

PR-URL: https://github.com/nodejs/node/pull/57567
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Yagiz Nizipli 2025-03-22 15:39:44 -04:00 committed by GitHub
parent 4a7d50122b
commit a4f556fc36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 6 additions and 60 deletions

View File

@ -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

View File

@ -149,7 +149,6 @@ const {
const permission = require('internal/process/permission');
let truncateWarn = true;
let fs;
// Lazy loaded
@ -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.
@ -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;
@ -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;
}

View File

@ -1,27 +0,0 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const tmp = tmpdir.path;
tmpdir.refresh();
const filename = path.resolve(tmp, 'truncate-file.txt');
fs.writeFileSync(filename, 'hello world', 'utf8');
const fd = fs.openSync(filename, 'r+');
const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
' Please use fs.ftruncate with a file descriptor instead.';
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
fs.truncate(fd, 5, common.mustSucceed(() => {
assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello');
}));
process.once('beforeExit', () => {
fs.closeSync(fd);
fs.unlinkSync(filename);
console.log('ok');
});

View File

@ -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);

View File

@ -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);
@ -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