lib: deprecate fd usage for fs.truncate(Sync)

PR-URL: https://github.com/nodejs/node/pull/15990
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
r1cebank 2017-10-06 11:06:35 -07:00 committed by James M Snell
parent af49e58211
commit c885ea727d
5 changed files with 42 additions and 0 deletions

View File

@ -719,6 +719,15 @@ The internal `path._makeLong()` was not intended for public use. However,
userland modules have found it useful. The internal API has been deprecated userland modules have found it useful. The internal API has been deprecated
and replaced with an identical, public `path.toNamespacedPath()` method. and replaced with an identical, public `path.toNamespacedPath()` method.
<a id="DEP0081"></a>
### DEP0081: fs.truncate() using a file descriptor
Type: Runtime
`fs.truncate()` `fs.truncateSync()` usage with a file descriptor has been
deprecated. Please use `fs.ftruncate()` or `fs.ftruncateSync()` to work with
file descriptors.
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array

View File

@ -2266,6 +2266,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 given to the completion callback. A file descriptor can also be passed as the
first argument. In this case, `fs.ftruncate()` is called. 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]) ## fs.truncateSync(path[, len])
<!-- YAML <!-- YAML
added: v0.8.6 added: v0.8.6
@ -2277,6 +2280,9 @@ added: v0.8.6
Synchronous truncate(2). Returns `undefined`. A file descriptor can also be Synchronous truncate(2). Returns `undefined`. A file descriptor can also be
passed as the first argument. In this case, `fs.ftruncateSync()` is called. 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) ## fs.unlink(path, callback)
<!-- YAML <!-- YAML
added: v0.0.2 added: v0.0.2

View File

@ -63,6 +63,18 @@ const isWindows = process.platform === 'win32';
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG); const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
const errnoException = util._errnoException; 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', 'DEP0081');
truncateWarn = false;
}
}
function getOptions(options, defaultOptions) { function getOptions(options, defaultOptions) {
if (options === null || options === undefined || if (options === null || options === undefined ||
typeof options === 'function') { typeof options === 'function') {
@ -783,6 +795,7 @@ fs.renameSync = function(oldPath, newPath) {
fs.truncate = function(path, len, callback) { fs.truncate = function(path, len, callback) {
if (typeof path === 'number') { if (typeof path === 'number') {
showTruncateDeprecation();
return fs.ftruncate(path, len, callback); return fs.ftruncate(path, len, callback);
} }
if (typeof len === 'function') { if (typeof len === 'function') {
@ -808,6 +821,7 @@ fs.truncate = function(path, len, callback) {
fs.truncateSync = function(path, len) { fs.truncateSync = function(path, len) {
if (typeof path === 'number') { if (typeof path === 'number') {
// legacy // legacy
showTruncateDeprecation();
return fs.ftruncateSync(path, len); return fs.ftruncateSync(path, len);
} }
if (len === undefined) { if (len === undefined) {

View File

@ -9,6 +9,12 @@ const filename = path.resolve(tmp, 'truncate-file.txt');
fs.writeFileSync(filename, 'hello world', 'utf8'); fs.writeFileSync(filename, 'hello world', 'utf8');
const fd = fs.openSync(filename, 'r+'); 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) { fs.truncate(fd, 5, common.mustCall(function(err) {
assert.ok(!err); assert.ok(!err);
assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello'); assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello');

View File

@ -32,6 +32,9 @@ common.refreshTmpDir();
let stat; let stat;
const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
' Please use fs.ftruncate with a file descriptor instead.';
// truncateSync // truncateSync
fs.writeFileSync(filename, data); fs.writeFileSync(filename, data);
stat = fs.statSync(filename); stat = fs.statSync(filename);
@ -60,6 +63,10 @@ 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);
fs.truncateSync(fd);
fs.closeSync(fd); fs.closeSync(fd);
// async tests // async tests