Skip to content
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

lib: deprecate fd usage for fs.truncate and fs.truncateSync #15990

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,17 @@ The internal `path._makeLong()` was not intended for public use. However,
userland modules have found it useful. The internal API has been deprecated
and replaced with an identical, public `path.toNamespacedPath()` method.

<a id="DEP00XX"></a>
### DEP0081: fs.truncate()

Type: Runtime

`fs.truncate()` `fs.truncateSync()` usage with
a file descriptor has been deprecated.

*Note*: Please use `fs.ftruncate()` or `fs.ftruncateSync()`
to work with file descriptors.


[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
Expand Down
6 changes: 6 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,9 @@ Asynchronous truncate(2). No arguments other than a possible exception are
given to the completion callback. A file descriptor can also be passed as the
first argument. In this case, `fs.ftruncate()` is called.

*Note*: Passing a file descriptor is deprecated and may result in an error
being thrown in the future.

## fs.truncateSync(path[, len])
<!-- YAML
added: v0.8.6
Expand All @@ -2279,6 +2282,9 @@ added: v0.8.6
Synchronous truncate(2). Returns `undefined`. A file descriptor can also be
passed as the first argument. In this case, `fs.ftruncateSync()` is called.

*Note*: Passing a file descriptor is deprecated and may result in an error
being thrown in the future.

## fs.unlink(path, callback)
<!-- YAML
added: v0.0.2
Expand Down
14 changes: 14 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ const isWindows = process.platform === 'win32';
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
const errnoException = util._errnoException;

let truncateWarn = true;

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', 'DEP00XX');
truncateWarn = false;
}
}

function getOptions(options, defaultOptions) {
if (options === null || options === undefined ||
typeof options === 'function') {
Expand Down Expand Up @@ -783,6 +795,7 @@ fs.renameSync = function(oldPath, newPath) {

fs.truncate = function(path, len, callback) {
if (typeof path === 'number') {
showTruncateDeprecation();
return fs.ftruncate(path, len, callback);
}
if (typeof len === 'function') {
Expand All @@ -808,6 +821,7 @@ fs.truncate = function(path, len, callback) {
fs.truncateSync = function(path, len) {
if (typeof path === 'number') {
// legacy
showTruncateDeprecation();
return fs.ftruncateSync(path, len);
}
if (len === undefined) {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-fs-truncate-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ 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);
fs.truncate(fd, 5, common.mustCall(function(err) {
assert.ok(!err);
assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello');
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ common.refreshTmpDir();

let stat;

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

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

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

fs.closeSync(fd);

// async tests
Expand Down