Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit ca2ad16

Browse files
committed
GH-853 fs.fchmod and fs.fchown
1 parent da9b334 commit ca2ad16

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

lib/fs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ fs.unlinkSync = function(path) {
432432
return binding.unlink(path);
433433
};
434434

435+
fs.fchmod = function(fd, mode, callback) {
436+
binding.fchmod(fd, modeNum(mode), callback || noop);
437+
};
438+
439+
fs.fchmodSync = function(fd, mode) {
440+
return binding.fchmod(fd, modeNum(mode));
441+
};
442+
435443
fs.chmod = function(path, mode, callback) {
436444
binding.chmod(path, modeNum(mode), callback || noop);
437445
};
@@ -440,6 +448,14 @@ fs.chmodSync = function(path, mode) {
440448
return binding.chmod(path, modeNum(mode));
441449
};
442450

451+
fs.fchown = function(fd, uid, gid, callback) {
452+
binding.fchown(fd, uid, gid, callback || noop);
453+
};
454+
455+
fs.fchownSync = function(fd, uid, gid) {
456+
return binding.fchown(fd, uid, gid);
457+
};
458+
443459
fs.chown = function(path, uid, gid, callback) {
444460
binding.chown(path, uid, gid, callback || noop);
445461
};

src/node_file.cc

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ static int After(eio_req *req) {
126126
case EIO_LINK:
127127
case EIO_SYMLINK:
128128
case EIO_CHMOD:
129+
case EIO_FCHMOD:
129130
case EIO_CHOWN:
131+
case EIO_FCHOWN:
130132
// These, however, don't.
131133
argc = 1;
132134
break;
@@ -792,7 +794,7 @@ static Handle<Value> Read(const Arguments& args) {
792794
}
793795

794796

795-
/* fs.chmod(fd, mode);
797+
/* fs.chmod(path, mode);
796798
* Wrapper for chmod(1) / EIO_CHMOD
797799
*/
798800
static Handle<Value> Chmod(const Arguments& args) {
@@ -808,16 +810,38 @@ static Handle<Value> Chmod(const Arguments& args) {
808810
ASYNC_CALL(chmod, args[2], *path, mode);
809811
} else {
810812
int ret = chmod(*path, mode);
811-
if (ret != 0) return ThrowException(ErrnoException(errno, NULL, "", *path));
813+
if (ret != 0) return ThrowException(ErrnoException(errno, "chmod", "", *path));
812814
return Undefined();
813815
}
814816
}
815817

816818

817-
/* fs.chown(fd, uid, gid);
818-
* Wrapper for chown(1) / EIO_CHOWN
819+
/* fs.fchmod(fd, mode);
820+
* Wrapper for fchmod(1) / EIO_FCHMOD
819821
*/
822+
static Handle<Value> FChmod(const Arguments& args) {
823+
HandleScope scope;
824+
825+
if(args.Length() < 2 || !args[0]->IsInt32() || !args[1]->IsInt32()) {
826+
return THROW_BAD_ARGS;
827+
}
828+
int fd = args[0]->Int32Value();
829+
mode_t mode = static_cast<mode_t>(args[1]->Int32Value());
830+
831+
if(args[2]->IsFunction()) {
832+
ASYNC_CALL(fchmod, args[2], fd, mode);
833+
} else {
834+
int ret = fchmod(fd, mode);
835+
if (ret != 0) return ThrowException(ErrnoException(errno, "fchmod", "", 0));
836+
return Undefined();
837+
}
838+
}
839+
840+
820841
#ifdef __POSIX__
842+
/* fs.chown(path, uid, gid);
843+
* Wrapper for chown(1) / EIO_CHOWN
844+
*/
821845
static Handle<Value> Chown(const Arguments& args) {
822846
HandleScope scope;
823847

@@ -837,7 +861,37 @@ static Handle<Value> Chown(const Arguments& args) {
837861
ASYNC_CALL(chown, args[3], *path, uid, gid);
838862
} else {
839863
int ret = chown(*path, uid, gid);
840-
if (ret != 0) return ThrowException(ErrnoException(errno, NULL, "", *path));
864+
if (ret != 0) return ThrowException(ErrnoException(errno, "chown", "", *path));
865+
return Undefined();
866+
}
867+
}
868+
#endif // __POSIX__
869+
870+
871+
#ifdef __POSIX__
872+
/* fs.fchown(fd, uid, gid);
873+
* Wrapper for fchown(1) / EIO_FCHOWN
874+
*/
875+
static Handle<Value> FChown(const Arguments& args) {
876+
HandleScope scope;
877+
878+
if (args.Length() < 3 || !args[0]->IsInt32()) {
879+
return THROW_BAD_ARGS;
880+
}
881+
882+
if (!args[1]->IsInt32() || !args[2]->IsInt32()) {
883+
return ThrowException(Exception::Error(String::New("User and Group IDs must be an integer.")));
884+
}
885+
886+
int fd = args[0]->Int32Value();
887+
uid_t uid = static_cast<uid_t>(args[1]->Int32Value());
888+
gid_t gid = static_cast<gid_t>(args[2]->Int32Value());
889+
890+
if (args[3]->IsFunction()) {
891+
ASYNC_CALL(fchown, args[3], fd, uid, gid);
892+
} else {
893+
int ret = fchown(fd, uid, gid);
894+
if (ret != 0) return ThrowException(ErrnoException(errno, "fchown", "", 0));
841895
return Undefined();
842896
}
843897
}
@@ -948,8 +1002,13 @@ void File::Initialize(Handle<Object> target) {
9481002
NODE_SET_METHOD(target, "write", Write);
9491003

9501004
NODE_SET_METHOD(target, "chmod", Chmod);
1005+
NODE_SET_METHOD(target, "fchmod", FChmod);
9511006
#ifdef __POSIX__
1007+
//NODE_SET_METHOD(target, "lchmod", LChmod);
1008+
9521009
NODE_SET_METHOD(target, "chown", Chown);
1010+
NODE_SET_METHOD(target, "fchown", FChown);
1011+
//NODE_SET_METHOD(target, "lchown", LChown);
9531012
#endif // __POSIX__
9541013

9551014
NODE_SET_METHOD(target, "utimes", UTimes);

test/simple/test-fs-chmod.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,28 @@ fs.chmod(file, '0777', function(err) {
4141
}
4242
});
4343

44+
fs.open(file, 'a', function(err, fd) {
45+
if (err) {
46+
got_error = true;
47+
console.error(err.stack);
48+
return;
49+
}
50+
fs.fchmod(fd, '0777', function(err) {
51+
if (err) {
52+
got_error = true;
53+
} else {
54+
console.log(fs.fstatSync(fd).mode);
55+
assert.equal(0777, fs.fstatSync(fd).mode & 0777);
56+
57+
fs.fchmodSync(fd, 0644);
58+
assert.equal(0644, fs.fstatSync(fd).mode & 0777);
59+
success_count++;
60+
}
61+
});
62+
});
63+
4464
process.addListener('exit', function() {
45-
assert.equal(1, success_count);
65+
assert.equal(2, success_count);
4666
assert.equal(false, got_error);
4767
});
4868

0 commit comments

Comments
 (0)