From 168a5557803b40b4930c3b4f9587b67a6d566b13 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sat, 4 Aug 2012 12:39:11 -0700 Subject: [PATCH] fs: fix naming of truncate/ftruncate functions For backwards compatibility, fs.truncate() will proxy to fs.ftruncate. Fix #3805 --- doc/api/fs.markdown | 13 ++- lib/fs.js | 56 ++++++++++++- src/node_file.cc | 4 +- test/fixtures/create-file.js | 4 +- test/simple/test-fs-truncate.js | 135 ++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 test/simple/test-fs-truncate.js diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index 3186a334a6..d1579dca0a 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -71,15 +71,24 @@ to the completion callback. Synchronous rename(2). -## fs.truncate(fd, len, [callback]) +## fs.ftruncate(fd, len, [callback]) Asynchronous ftruncate(2). No arguments other than a possible exception are given to the completion callback. -## fs.truncateSync(fd, len) +## fs.ftruncateSync(fd, len) Synchronous ftruncate(2). +## fs.truncate(path, len, [callback]) + +Asynchronous truncate(2). No arguments other than a possible exception are +given to the completion callback. + +## fs.truncateSync(path, len) + +Synchronous truncate(2). + ## fs.chown(path, uid, gid, [callback]) Asynchronous chown(2). No arguments other than a possible exception are given diff --git a/lib/fs.js b/lib/fs.js index 31e36908fd..b71cff6c67 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -440,12 +440,60 @@ fs.renameSync = function(oldPath, newPath) { pathModule._makeLong(newPath)); }; -fs.truncate = function(fd, len, callback) { - binding.truncate(fd, len, makeCallback(callback)); +fs.truncate = function(path, len, callback) { + if (typeof path === 'number') { + // legacy + return fs.ftruncate(path, len, callback); + } + if (typeof len === 'function') { + callback = len; + len = 0; + } else if (typeof len === 'undefined') { + len = 0; + } + fs.open(path, 'w', function(er, fd) { + if (er) return callback(er); + binding.ftruncate(fd, len, function(er) { + fs.close(fd, function(er2) { + callback(er || er2); + }); + }); + }); }; -fs.truncateSync = function(fd, len) { - return binding.truncate(fd, len); +fs.truncateSync = function(path, len) { + if (typeof path === 'number') { + // legacy + return fs.ftruncateSync(path, len, callback); + } + if (typeof len === 'undefined') { + len = 0; + } + // allow error to be thrown, but still close fd. + var fd = fs.openSync(path, 'w'); + try { + var ret = fs.ftruncateSync(fd, len); + } finally { + fs.closeSync(fd); + } + return ret; +}; + +fs.ftruncate = function(fd, len, callback) { + if (typeof len === 'function') { + callback = len; + len = 0; + } else if (typeof len === 'undefined') { + len = 0; + } + binding.ftruncate(fd, len, makeCallback(callback)); +}; + +fs.ftruncateSync = function(fd, len) { + if (typeof len === 'undefined') { + len = 0; + } + return binding.ftruncate(fd, len); }; fs.rmdir = function(path, callback) { diff --git a/src/node_file.cc b/src/node_file.cc index 7e8badf5c2..1fdf9a4c94 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -486,7 +486,7 @@ static Handle Rename(const Arguments& args) { } } -static Handle Truncate(const Arguments& args) { +static Handle FTruncate(const Arguments& args) { HandleScope scope; if (args.Length() < 2 || !args[0]->IsInt32()) { @@ -941,7 +941,7 @@ void File::Initialize(Handle target) { NODE_SET_METHOD(target, "fdatasync", Fdatasync); NODE_SET_METHOD(target, "fsync", Fsync); NODE_SET_METHOD(target, "rename", Rename); - NODE_SET_METHOD(target, "truncate", Truncate); + NODE_SET_METHOD(target, "ftruncate", FTruncate); NODE_SET_METHOD(target, "rmdir", RMDir); NODE_SET_METHOD(target, "mkdir", MKDir); NODE_SET_METHOD(target, "sendfile", SendFile); diff --git a/test/fixtures/create-file.js b/test/fixtures/create-file.js index 1e87bc92df..c711da671a 100644 --- a/test/fixtures/create-file.js +++ b/test/fixtures/create-file.js @@ -24,6 +24,4 @@ var fs = require('fs'); var file_name = process.argv[2]; var file_size = parseInt(process.argv[3]); -var fd = fs.openSync(file_name, 'w'); -fs.truncateSync(fd, file_size); -fs.closeSync(fd); +fs.truncateSync(file_name, file_size); diff --git a/test/simple/test-fs-truncate.js b/test/simple/test-fs-truncate.js new file mode 100644 index 0000000000..5786449a7a --- /dev/null +++ b/test/simple/test-fs-truncate.js @@ -0,0 +1,135 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var path = require('path'); +var fs = require('fs'); +var tmp = common.tmpDir; +var filename = path.resolve(tmp, 'truncate-file.txt'); +var data = new Buffer(1024 * 16); +data.fill('x'); + +var stat; + +// truncateSync +fs.writeFileSync(filename, data); +stat = fs.statSync(filename); +assert.equal(stat.size, 1024 * 16); + +fs.truncateSync(filename, 1024); +stat = fs.statSync(filename); +assert.equal(stat.size, 1024); + +fs.truncateSync(filename); +stat = fs.statSync(filename); +assert.equal(stat.size, 0); + +// ftruncateSync +fs.writeFileSync(filename, data); +var fd = fs.openSync(filename, 'a'); + +stat = fs.statSync(filename); +assert.equal(stat.size, 1024 * 16); + +fs.ftruncateSync(fd, 1024); +stat = fs.statSync(filename); +assert.equal(stat.size, 1024); + +fs.ftruncateSync(fd); +stat = fs.statSync(filename); +assert.equal(stat.size, 0); + +fs.closeSync(fd); + +// async tests +var success = 0; +testTruncate(function(er) { + if (er) throw er; + success++; + testFtruncate(function(er) { + if (er) throw er; + success++; + }); +}); + +process.on('exit', function() { + assert.equal(success, 2); + console.log('ok'); +}); + +function testTruncate(cb) { + fs.writeFile(filename, data, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 1024 * 16); + + fs.truncate(filename, 1024, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 1024); + + fs.truncate(filename, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 0); + cb(); + }); + }); + }); + }); + }); + }); +} + + +function testFtruncate(cb) { + fs.writeFile(filename, data, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 1024 * 16); + + fs.open(filename, 'w', function(er, fd) { + if (er) return cb(er); + fs.ftruncate(fd, 1024, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 1024); + + fs.ftruncate(fd, function(er) { + if (er) return cb(er); + fs.stat(filename, function(er, stat) { + if (er) return cb(er); + assert.equal(stat.size, 0); + fs.close(fd, cb); + }); + }); + }); + }); + }); + }); + }); +}