diff --git a/doc/api.txt b/doc/api.txt index 12e61b02a6..757ca9c8fd 100644 --- a/doc/api.txt +++ b/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). diff --git a/src/node.js b/src/node.js index 76ac289860..d65b8be1c7 100644 --- a/src/node.js +++ b/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)); diff --git a/src/node_file.cc b/src/node_file.cc index 522903fd1a..153de7516d 100644 --- a/src/node_file.cc +++ b/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 Rename(const Arguments& args) { } } +static Handle 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 Unlink(const Arguments& args) { HandleScope scope; @@ -412,6 +432,7 @@ void File::Initialize(Handle 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);