From 927cf8d6a3707cb00dab9d41a34b29fde900c79d Mon Sep 17 00:00:00 2001 From: r1cebank Date: Fri, 6 Oct 2017 11:06:35 -0700 Subject: [PATCH 1/2] lib: deprecate fd usage for fs.truncate(Sync) --- lib/fs.js | 8 ++++++++ test/parallel/test-fs-truncate-fd.js | 4 ++++ test/parallel/test-fs-truncate.js | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/lib/fs.js b/lib/fs.js index b7a2bbba0ce82e..8d395a643772dd 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -783,6 +783,10 @@ fs.renameSync = function(oldPath, newPath) { fs.truncate = function(path, len, callback) { if (typeof path === 'number') { + process.emitWarning( + 'Using fs.truncate with file descriptor deprecated. In the future, ' + + 'use fs.ftruncate with file descriptor', + 'DeprecationWarning', 'DEP00XX'); return fs.ftruncate(path, len, callback); } if (typeof len === 'function') { @@ -808,6 +812,10 @@ fs.truncate = function(path, len, callback) { fs.truncateSync = function(path, len) { if (typeof path === 'number') { // legacy + process.emitWarning( + 'Using fs.truncate with file descriptor deprecated. In the future, ' + + 'use fs.ftruncate with file descriptor', + 'DeprecationWarning', 'DEP00XX'); return fs.ftruncateSync(path, len); } if (len === undefined) { diff --git a/test/parallel/test-fs-truncate-fd.js b/test/parallel/test-fs-truncate-fd.js index 526612870d9f73..244fad8ac50a97 100644 --- a/test/parallel/test-fs-truncate-fd.js +++ b/test/parallel/test-fs-truncate-fd.js @@ -9,6 +9,10 @@ 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 file descriptor deprecated.' + + ' In the future, ' + + 'use fs.ftruncate with file descriptor'; +common.expectWarning('DeprecationWarning', msg); fs.truncate(fd, 5, common.mustCall(function(err) { assert.ok(!err); assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello'); diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js index a56a1a054ca718..a8a2767521012b 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -32,6 +32,10 @@ common.refreshTmpDir(); let stat; +const msg = 'Using fs.truncate with file descriptor deprecated.' + + ' In the future, ' + + 'use fs.ftruncate with file descriptor'; + // truncateSync fs.writeFileSync(filename, data); stat = fs.statSync(filename); @@ -60,6 +64,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 From 2ff66b98b969c98d0495de64b3b3ffa1c2300cc8 Mon Sep 17 00:00:00 2001 From: r1cebank Date: Fri, 6 Oct 2017 11:22:12 -0700 Subject: [PATCH 2/2] doc: deprecation note for fs.truncate(Sync) --- doc/api/deprecations.md | 11 +++++++++++ doc/api/fs.md | 6 ++++++ lib/fs.js | 22 ++++++++++++++-------- test/parallel/test-fs-truncate-fd.js | 8 +++++--- test/parallel/test-fs-truncate.js | 5 ++--- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 76165638839e38..59c41b77d4fd16 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -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. + +### 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 diff --git a/doc/api/fs.md b/doc/api/fs.md index 0c91ec7b352722..5e74d16daf7a6d 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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])