From d89de077582b6458d7586a29107f86fe4a4508a0 Mon Sep 17 00:00:00 2001 From: Micheil Smith Date: Thu, 27 May 2010 03:49:01 +1000 Subject: [PATCH] Adding experimental support for fs.chown and fs.chownSync. Notes: - Currently only accepts numeric user and group ids. - No tests, as tests depend on getpwuid and getgrgid. - No documentation, as there is no tests and this is experimental. --- lib/fs.js | 8 ++++++++ src/node_file.cc | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index ad6b977457e..72302440570 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -349,6 +349,14 @@ fs.chmodSync = function (path, mode) { return binding.chmod(path, mode); }; +fs.chown = function(path, uid, gid, callback) { + binding.chown(path, uid, gid, callback || noop); +}; + +fs.chownSync = function(path, uid, gid) { + return binding.chown(path, uid, gid); +}; + function writeAll (fd, data, encoding, callback) { fs.write(fd, data, 0, encoding, function (writeErr, written) { if (writeErr) { diff --git a/src/node_file.cc b/src/node_file.cc index 49ba894dcfc..dde27402dfa 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -51,6 +51,7 @@ static int After(eio_req *req) { case EIO_READLINK: case EIO_OPEN: case EIO_CHMOD: + case EIO_CHOWN: case EIO_MKDIR: argv[0] = ErrnoException(req->errorno, NULL, "", static_cast(req->ptr1)); break; @@ -73,6 +74,7 @@ static int After(eio_req *req) { case EIO_LINK: case EIO_SYMLINK: case EIO_CHMOD: + case EIO_CHOWN: argc = 0; break; @@ -630,18 +632,19 @@ static Handle Read(const Arguments& args) { } } + /* fs.chmod(fd, mode); * Wrapper for chmod(1) / EIO_CHMOD */ -static Handle Chmod(const Arguments& args){ +static Handle Chmod(const Arguments& args) { HandleScope scope; - + if(args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) { return THROW_BAD_ARGS; } String::Utf8Value path(args[0]->ToString()); mode_t mode = static_cast(args[1]->Int32Value()); - + if(args[2]->IsFunction()) { ASYNC_CALL(chmod, args[2], *path, mode); } else { @@ -652,6 +655,33 @@ static Handle Chmod(const Arguments& args){ } +/* fs.chown(fd, uid, gid); + * Wrapper for chown(1) / EIO_CHOWN + */ +static Handle Chown(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 3 || !args[0]->IsString()) { + return THROW_BAD_ARGS; + } + + if (!args[1]->IsInt32() || !args[2]->IsInt32()) { + return ThrowException(Exception::Error(String::New("User and Group IDs must be an integer."))); + } + + String::Utf8Value path(args[0]->ToString()); + uid_t uid = static_cast(args[1]->Int32Value()); + gid_t gid = static_cast(args[2]->Int32Value()); + + if (args[3]->IsFunction()) { + ASYNC_CALL(chown, args[3], *path, uid, gid); + } else { + int ret = chown(*path, uid, gid); + if (ret != 0) return ThrowException(ErrnoException(errno, NULL, "", *path)); + return Undefined(); + } +} + void File::Initialize(Handle target) { HandleScope scope; @@ -676,6 +706,7 @@ void File::Initialize(Handle target) { NODE_SET_METHOD(target, "write", Write); NODE_SET_METHOD(target, "chmod", Chmod); + NODE_SET_METHOD(target, "chown", Chown); errno_symbol = NODE_PSYMBOL("errno"); encoding_symbol = NODE_PSYMBOL("node:encoding");