Adding experimental support for fs.chown and fs.chownSync.
authorMicheil Smith <micheil@brandedcode.com>
Wed, 26 May 2010 17:49:01 +0000 (03:49 +1000)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 26 May 2010 20:37:45 +0000 (13:37 -0700)
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
src/node_file.cc

index ad6b977457e5e866bac7d03bdcd2670bd5433279..72302440570db10e9868d4616fe6021e74195934 100644 (file)
--- 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) {
index 49ba894dcfc93f7f32d440304baaaaed695a405d..dde27402dfa2b5c1f19632cc5640aa7e8b0fba73 100644 (file)
@@ -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<const char*>(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<Value> Read(const Arguments& args) {
   }
 }
 
+
 /* fs.chmod(fd, mode);
  * Wrapper for chmod(1) / EIO_CHMOD
  */
-static Handle<Value> Chmod(const Arguments& args){
+static Handle<Value> 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<mode_t>(args[1]->Int32Value());
-  
+
   if(args[2]->IsFunction()) {
     ASYNC_CALL(chmod, args[2], *path, mode);
   } else {
@@ -652,6 +655,33 @@ static Handle<Value> Chmod(const Arguments& args){
 }
 
 
+/* fs.chown(fd, uid, gid);
+ * Wrapper for chown(1) / EIO_CHOWN
+ */
+static Handle<Value> 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<uid_t>(args[1]->Int32Value());
+  gid_t gid = static_cast<gid_t>(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<Object> target) {
   HandleScope scope;
 
@@ -676,6 +706,7 @@ void File::Initialize(Handle<Object> 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");