Skip to content

Commit 168a555

Browse files
committed
fs: fix naming of truncate/ftruncate functions
For backwards compatibility, fs.truncate(<number>) will proxy to fs.ftruncate. Fix #3805
1 parent 0414e14 commit 168a555

File tree

5 files changed

+201
-11
lines changed

5 files changed

+201
-11
lines changed

doc/api/fs.markdown

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,24 @@ to the completion callback.
7171

7272
Synchronous rename(2).
7373

74-
## fs.truncate(fd, len, [callback])
74+
## fs.ftruncate(fd, len, [callback])
7575

7676
Asynchronous ftruncate(2). No arguments other than a possible exception are
7777
given to the completion callback.
7878

79-
## fs.truncateSync(fd, len)
79+
## fs.ftruncateSync(fd, len)
8080

8181
Synchronous ftruncate(2).
8282

83+
## fs.truncate(path, len, [callback])
84+
85+
Asynchronous truncate(2). No arguments other than a possible exception are
86+
given to the completion callback.
87+
88+
## fs.truncateSync(path, len)
89+
90+
Synchronous truncate(2).
91+
8392
## fs.chown(path, uid, gid, [callback])
8493

8594
Asynchronous chown(2). No arguments other than a possible exception are given

lib/fs.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,60 @@ fs.renameSync = function(oldPath, newPath) {
440440
pathModule._makeLong(newPath));
441441
};
442442

443-
fs.truncate = function(fd, len, callback) {
444-
binding.truncate(fd, len, makeCallback(callback));
443+
fs.truncate = function(path, len, callback) {
444+
if (typeof path === 'number') {
445+
// legacy
446+
return fs.ftruncate(path, len, callback);
447+
}
448+
if (typeof len === 'function') {
449+
callback = len;
450+
len = 0;
451+
} else if (typeof len === 'undefined') {
452+
len = 0;
453+
}
454+
fs.open(path, 'w', function(er, fd) {
455+
if (er) return callback(er);
456+
binding.ftruncate(fd, len, function(er) {
457+
fs.close(fd, function(er2) {
458+
callback(er || er2);
459+
});
460+
});
461+
});
445462
};
446463

447-
fs.truncateSync = function(fd, len) {
448-
return binding.truncate(fd, len);
464+
fs.truncateSync = function(path, len) {
465+
if (typeof path === 'number') {
466+
// legacy
467+
return fs.ftruncateSync(path, len, callback);
468+
}
469+
if (typeof len === 'undefined') {
470+
len = 0;
471+
}
472+
// allow error to be thrown, but still close fd.
473+
var fd = fs.openSync(path, 'w');
474+
try {
475+
var ret = fs.ftruncateSync(fd, len);
476+
} finally {
477+
fs.closeSync(fd);
478+
}
479+
return ret;
480+
};
481+
482+
fs.ftruncate = function(fd, len, callback) {
483+
if (typeof len === 'function') {
484+
callback = len;
485+
len = 0;
486+
} else if (typeof len === 'undefined') {
487+
len = 0;
488+
}
489+
binding.ftruncate(fd, len, makeCallback(callback));
490+
};
491+
492+
fs.ftruncateSync = function(fd, len) {
493+
if (typeof len === 'undefined') {
494+
len = 0;
495+
}
496+
return binding.ftruncate(fd, len);
449497
};
450498

451499
fs.rmdir = function(path, callback) {

src/node_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static Handle<Value> Rename(const Arguments& args) {
486486
}
487487
}
488488

489-
static Handle<Value> Truncate(const Arguments& args) {
489+
static Handle<Value> FTruncate(const Arguments& args) {
490490
HandleScope scope;
491491

492492
if (args.Length() < 2 || !args[0]->IsInt32()) {
@@ -941,7 +941,7 @@ void File::Initialize(Handle<Object> target) {
941941
NODE_SET_METHOD(target, "fdatasync", Fdatasync);
942942
NODE_SET_METHOD(target, "fsync", Fsync);
943943
NODE_SET_METHOD(target, "rename", Rename);
944-
NODE_SET_METHOD(target, "truncate", Truncate);
944+
NODE_SET_METHOD(target, "ftruncate", FTruncate);
945945
NODE_SET_METHOD(target, "rmdir", RMDir);
946946
NODE_SET_METHOD(target, "mkdir", MKDir);
947947
NODE_SET_METHOD(target, "sendfile", SendFile);

test/fixtures/create-file.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@ var fs = require('fs');
2424
var file_name = process.argv[2];
2525
var file_size = parseInt(process.argv[3]);
2626

27-
var fd = fs.openSync(file_name, 'w');
28-
fs.truncateSync(fd, file_size);
29-
fs.closeSync(fd);
27+
fs.truncateSync(file_name, file_size);

test/simple/test-fs-truncate.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var path = require('path');
25+
var fs = require('fs');
26+
var tmp = common.tmpDir;
27+
var filename = path.resolve(tmp, 'truncate-file.txt');
28+
var data = new Buffer(1024 * 16);
29+
data.fill('x');
30+
31+
var stat;
32+
33+
// truncateSync
34+
fs.writeFileSync(filename, data);
35+
stat = fs.statSync(filename);
36+
assert.equal(stat.size, 1024 * 16);
37+
38+
fs.truncateSync(filename, 1024);
39+
stat = fs.statSync(filename);
40+
assert.equal(stat.size, 1024);
41+
42+
fs.truncateSync(filename);
43+
stat = fs.statSync(filename);
44+
assert.equal(stat.size, 0);
45+
46+
// ftruncateSync
47+
fs.writeFileSync(filename, data);
48+
var fd = fs.openSync(filename, 'a');
49+
50+
stat = fs.statSync(filename);
51+
assert.equal(stat.size, 1024 * 16);
52+
53+
fs.ftruncateSync(fd, 1024);
54+
stat = fs.statSync(filename);
55+
assert.equal(stat.size, 1024);
56+
57+
fs.ftruncateSync(fd);
58+
stat = fs.statSync(filename);
59+
assert.equal(stat.size, 0);
60+
61+
fs.closeSync(fd);
62+
63+
// async tests
64+
var success = 0;
65+
testTruncate(function(er) {
66+
if (er) throw er;
67+
success++;
68+
testFtruncate(function(er) {
69+
if (er) throw er;
70+
success++;
71+
});
72+
});
73+
74+
process.on('exit', function() {
75+
assert.equal(success, 2);
76+
console.log('ok');
77+
});
78+
79+
function testTruncate(cb) {
80+
fs.writeFile(filename, data, function(er) {
81+
if (er) return cb(er);
82+
fs.stat(filename, function(er, stat) {
83+
if (er) return cb(er);
84+
assert.equal(stat.size, 1024 * 16);
85+
86+
fs.truncate(filename, 1024, function(er) {
87+
if (er) return cb(er);
88+
fs.stat(filename, function(er, stat) {
89+
if (er) return cb(er);
90+
assert.equal(stat.size, 1024);
91+
92+
fs.truncate(filename, function(er) {
93+
if (er) return cb(er);
94+
fs.stat(filename, function(er, stat) {
95+
if (er) return cb(er);
96+
assert.equal(stat.size, 0);
97+
cb();
98+
});
99+
});
100+
});
101+
});
102+
});
103+
});
104+
}
105+
106+
107+
function testFtruncate(cb) {
108+
fs.writeFile(filename, data, function(er) {
109+
if (er) return cb(er);
110+
fs.stat(filename, function(er, stat) {
111+
if (er) return cb(er);
112+
assert.equal(stat.size, 1024 * 16);
113+
114+
fs.open(filename, 'w', function(er, fd) {
115+
if (er) return cb(er);
116+
fs.ftruncate(fd, 1024, function(er) {
117+
if (er) return cb(er);
118+
fs.stat(filename, function(er, stat) {
119+
if (er) return cb(er);
120+
assert.equal(stat.size, 1024);
121+
122+
fs.ftruncate(fd, function(er) {
123+
if (er) return cb(er);
124+
fs.stat(filename, function(er, stat) {
125+
if (er) return cb(er);
126+
assert.equal(stat.size, 0);
127+
fs.close(fd, cb);
128+
});
129+
});
130+
});
131+
});
132+
});
133+
});
134+
});
135+
}

0 commit comments

Comments
 (0)