Browse Source

Add posix.truncate()

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
76c1805c67
  1. 5
      doc/api.txt
  2. 10
      src/node.js
  3. 21
      src/node_file.cc

5
doc/api.txt

@ -653,6 +653,11 @@ sys.puts("stats: " + JSON.stringify(stats));
- on success: no parameters.
- on error: no parameters.
+posix.truncate(fd, len)+ ::
See ftruncate(2).
- on success: no parameters.
- on error: no parameters.
+posix.stat(path)+ ::
See stat(2).

10
src/node.js

@ -571,6 +571,16 @@ var posixModule = createInternalModule("posix", function (exports) {
return process.fs.rename(oldPath, newPath);
};
exports.truncate = function (fd, len) {
var promise = new events.Promise();
process.fs.truncate(fd, len, callback(promise));
return promise;
};
exports.truncateSync = function (fd, len) {
return process.fs.truncate(fd, len);
};
exports.rmdir = function (path) {
var promise = new events.Promise();
process.fs.rmdir(path, callback(promise));

21
src/node_file.cc

@ -53,6 +53,7 @@ static int After(eio_req *req) {
case EIO_UNLINK:
case EIO_RMDIR:
case EIO_MKDIR:
case EIO_FTRUNCATE:
argc = 0;
break;
@ -202,6 +203,25 @@ static Handle<Value> Rename(const Arguments& args) {
}
}
static Handle<Value> Truncate(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2 || !args[0]->IsInt32()) {
return THROW_BAD_ARGS;
}
int fd = args[0]->Int32Value();
off_t len = args[1]->Uint32Value();
if (args[2]->IsFunction()) {
ASYNC_CALL(ftruncate, args[2], fd, len)
} else {
int ret = ftruncate(fd, len);
if (ret != 0) return ThrowException(errno_exception(errno));
return Undefined();
}
}
static Handle<Value> Unlink(const Arguments& args) {
HandleScope scope;
@ -412,6 +432,7 @@ void File::Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "open", Open);
NODE_SET_METHOD(target, "read", Read);
NODE_SET_METHOD(target, "rename", Rename);
NODE_SET_METHOD(target, "truncate", Truncate);
NODE_SET_METHOD(target, "rmdir", RMDir);
NODE_SET_METHOD(target, "mkdir", MKDir);
NODE_SET_METHOD(target, "sendfile", SendFile);

Loading…
Cancel
Save