Skip to content

Commit d89de07

Browse files
miksagory
authored andcommitted
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.
1 parent 29e867a commit d89de07

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

lib/fs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ fs.chmodSync = function (path, mode) {
349349
return binding.chmod(path, mode);
350350
};
351351

352+
fs.chown = function(path, uid, gid, callback) {
353+
binding.chown(path, uid, gid, callback || noop);
354+
};
355+
356+
fs.chownSync = function(path, uid, gid) {
357+
return binding.chown(path, uid, gid);
358+
};
359+
352360
function writeAll (fd, data, encoding, callback) {
353361
fs.write(fd, data, 0, encoding, function (writeErr, written) {
354362
if (writeErr) {

src/node_file.cc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static int After(eio_req *req) {
5151
case EIO_READLINK:
5252
case EIO_OPEN:
5353
case EIO_CHMOD:
54+
case EIO_CHOWN:
5455
case EIO_MKDIR:
5556
argv[0] = ErrnoException(req->errorno, NULL, "", static_cast<const char*>(req->ptr1));
5657
break;
@@ -73,6 +74,7 @@ static int After(eio_req *req) {
7374
case EIO_LINK:
7475
case EIO_SYMLINK:
7576
case EIO_CHMOD:
77+
case EIO_CHOWN:
7678
argc = 0;
7779
break;
7880

@@ -630,18 +632,19 @@ static Handle<Value> Read(const Arguments& args) {
630632
}
631633
}
632634

635+
633636
/* fs.chmod(fd, mode);
634637
* Wrapper for chmod(1) / EIO_CHMOD
635638
*/
636-
static Handle<Value> Chmod(const Arguments& args){
639+
static Handle<Value> Chmod(const Arguments& args) {
637640
HandleScope scope;
638-
641+
639642
if(args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
640643
return THROW_BAD_ARGS;
641644
}
642645
String::Utf8Value path(args[0]->ToString());
643646
mode_t mode = static_cast<mode_t>(args[1]->Int32Value());
644-
647+
645648
if(args[2]->IsFunction()) {
646649
ASYNC_CALL(chmod, args[2], *path, mode);
647650
} else {
@@ -652,6 +655,33 @@ static Handle<Value> Chmod(const Arguments& args){
652655
}
653656

654657

658+
/* fs.chown(fd, uid, gid);
659+
* Wrapper for chown(1) / EIO_CHOWN
660+
*/
661+
static Handle<Value> Chown(const Arguments& args) {
662+
HandleScope scope;
663+
664+
if (args.Length() < 3 || !args[0]->IsString()) {
665+
return THROW_BAD_ARGS;
666+
}
667+
668+
if (!args[1]->IsInt32() || !args[2]->IsInt32()) {
669+
return ThrowException(Exception::Error(String::New("User and Group IDs must be an integer.")));
670+
}
671+
672+
String::Utf8Value path(args[0]->ToString());
673+
uid_t uid = static_cast<uid_t>(args[1]->Int32Value());
674+
gid_t gid = static_cast<gid_t>(args[2]->Int32Value());
675+
676+
if (args[3]->IsFunction()) {
677+
ASYNC_CALL(chown, args[3], *path, uid, gid);
678+
} else {
679+
int ret = chown(*path, uid, gid);
680+
if (ret != 0) return ThrowException(ErrnoException(errno, NULL, "", *path));
681+
return Undefined();
682+
}
683+
}
684+
655685
void File::Initialize(Handle<Object> target) {
656686
HandleScope scope;
657687

@@ -676,6 +706,7 @@ void File::Initialize(Handle<Object> target) {
676706
NODE_SET_METHOD(target, "write", Write);
677707

678708
NODE_SET_METHOD(target, "chmod", Chmod);
709+
NODE_SET_METHOD(target, "chown", Chown);
679710

680711
errno_symbol = NODE_PSYMBOL("errno");
681712
encoding_symbol = NODE_PSYMBOL("node:encoding");

0 commit comments

Comments
 (0)