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:
parent
4a7d50122b
commit
a4f556fc36
@ -1832,12 +1832,15 @@ and replaced with an identical, public `path.toNamespacedPath()` method.
|
|||||||
|
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
changes:
|
changes:
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/57567
|
||||||
|
description: End-of-Life.
|
||||||
- version: v9.0.0
|
- version: v9.0.0
|
||||||
pr-url: https://github.com/nodejs/node/pull/15990
|
pr-url: https://github.com/nodejs/node/pull/15990
|
||||||
description: Runtime deprecation.
|
description: Runtime deprecation.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
Type: Runtime
|
Type: End-of-Life
|
||||||
|
|
||||||
`fs.truncate()` `fs.truncateSync()` usage with a file descriptor is
|
`fs.truncate()` `fs.truncateSync()` usage with a file descriptor is
|
||||||
deprecated. Please use `fs.ftruncate()` or `fs.ftruncateSync()` to work with
|
deprecated. Please use `fs.ftruncate()` or `fs.ftruncateSync()` to work with
|
||||||
|
20
lib/fs.js
20
lib/fs.js
@ -149,7 +149,6 @@ const {
|
|||||||
|
|
||||||
const permission = require('internal/process/permission');
|
const permission = require('internal/process/permission');
|
||||||
|
|
||||||
let truncateWarn = true;
|
|
||||||
let fs;
|
let fs;
|
||||||
|
|
||||||
// Lazy loaded
|
// Lazy loaded
|
||||||
@ -167,16 +166,6 @@ let ReadFileContext;
|
|||||||
let FileReadStream;
|
let FileReadStream;
|
||||||
let FileWriteStream;
|
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
|
// Ensure that callbacks run in the global context. Only use this function
|
||||||
// for callbacks that are passed to the binding layer, callbacks that are
|
// for callbacks that are passed to the binding layer, callbacks that are
|
||||||
// invoked from JS already run in the proper scope.
|
// invoked from JS already run in the proper scope.
|
||||||
@ -1031,10 +1020,6 @@ function renameSync(oldPath, newPath) {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function truncate(path, len, callback) {
|
function truncate(path, len, callback) {
|
||||||
if (typeof path === 'number') {
|
|
||||||
showTruncateDeprecation();
|
|
||||||
return fs.ftruncate(path, len, callback);
|
|
||||||
}
|
|
||||||
if (typeof len === 'function') {
|
if (typeof len === 'function') {
|
||||||
callback = len;
|
callback = len;
|
||||||
len = 0;
|
len = 0;
|
||||||
@ -1064,11 +1049,6 @@ function truncate(path, len, callback) {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function truncateSync(path, len) {
|
function truncateSync(path, len) {
|
||||||
if (typeof path === 'number') {
|
|
||||||
// legacy
|
|
||||||
showTruncateDeprecation();
|
|
||||||
return fs.ftruncateSync(path, len);
|
|
||||||
}
|
|
||||||
if (len === undefined) {
|
if (len === undefined) {
|
||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
|
@ -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');
|
|
||||||
});
|
|
@ -12,10 +12,7 @@ const filename = path.resolve(tmp, 'truncate-sync-file.txt');
|
|||||||
|
|
||||||
fs.writeFileSync(filename, 'hello world', 'utf8');
|
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);
|
fs.unlinkSync(filename);
|
||||||
|
@ -33,9 +33,6 @@ tmpdir.refresh();
|
|||||||
|
|
||||||
let stat;
|
let stat;
|
||||||
|
|
||||||
const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
|
|
||||||
' Please use fs.ftruncate with a file descriptor instead.';
|
|
||||||
|
|
||||||
// Check truncateSync
|
// Check truncateSync
|
||||||
fs.writeFileSync(filename, data);
|
fs.writeFileSync(filename, data);
|
||||||
stat = fs.statSync(filename);
|
stat = fs.statSync(filename);
|
||||||
@ -64,10 +61,6 @@ fs.ftruncateSync(fd);
|
|||||||
stat = fs.statSync(filename);
|
stat = fs.statSync(filename);
|
||||||
assert.strictEqual(stat.size, 0);
|
assert.strictEqual(stat.size, 0);
|
||||||
|
|
||||||
// truncateSync
|
|
||||||
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
|
|
||||||
fs.truncateSync(fd);
|
|
||||||
|
|
||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
|
|
||||||
// Async tests
|
// Async tests
|
||||||
|
Loading…
x
Reference in New Issue
Block a user