Browse Source

fs: fix naming of truncate/ftruncate functions

For backwards compatibility, fs.truncate(<number>) will proxy to
fs.ftruncate.

Fix #3805
v0.9.1-release
isaacs 12 years ago
parent
commit
168a555780
  1. 13
      doc/api/fs.markdown
  2. 56
      lib/fs.js
  3. 4
      src/node_file.cc
  4. 4
      test/fixtures/create-file.js
  5. 135
      test/simple/test-fs-truncate.js

13
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

56
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) {

4
src/node_file.cc

@ -486,7 +486,7 @@ static Handle<Value> Rename(const Arguments& args) {
}
}
static Handle<Value> Truncate(const Arguments& args) {
static Handle<Value> FTruncate(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2 || !args[0]->IsInt32()) {
@ -941,7 +941,7 @@ void File::Initialize(Handle<Object> 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);

4
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);

135
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);
});
});
});
});
});
});
});
}
Loading…
Cancel
Save