diff --git a/doc/api.txt b/doc/api.txt index 93bb67196a..a3e5082254 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -632,7 +632,11 @@ fs.rename("/tmp/hello", "/tmp/world").addCallback(function () { - on success: no parameters. - on error: no parameters. - ++fs.chmod(path, mode)+ :: + See chmod(1) + - on success: no parameters. + - on error: no parameters. + +fs.stat(path)+ :: See stat(2). - on success: Returns +fs.Stats+ object. It looks like this: diff --git a/src/node.js b/src/node.js index 302790f41d..4506bd8eaa 100644 --- a/src/node.js +++ b/src/node.js @@ -769,6 +769,17 @@ var fsModule = createInternalModule("fs", function (exports) { return content; }; + + exports.chmod = function(path, mode){ + var promise = new events.Promise(); + process.fs.chmod(path, mode, callback(promise)); + return promise; + }; + + exports.chmodSync = function(path, mode){ + return process.fs.chmod(path, mode); + }; + }); var fs = fsModule.exports; diff --git a/src/node_file.cc b/src/node_file.cc index 16f404591e..adc0057dd9 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -54,6 +54,7 @@ static int After(eio_req *req) { case EIO_RMDIR: case EIO_MKDIR: case EIO_FTRUNCATE: + case EIO_CHMOD: argc = 0; break; @@ -428,6 +429,28 @@ static Handle Read(const Arguments& args) { } } +/* node.fs.chmod(fd, mode); + * Wrapper for chmod(1) / EIO_CHMOD + */ +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 { + int ret = chmod(*path, mode); + if (ret != 0) return ThrowException(errno_exception(errno)); + return Undefined(); + } +} + + void File::Initialize(Handle target) { HandleScope scope; @@ -443,6 +466,8 @@ void File::Initialize(Handle target) { NODE_SET_METHOD(target, "stat", Stat); NODE_SET_METHOD(target, "unlink", Unlink); NODE_SET_METHOD(target, "write", Write); + + NODE_SET_METHOD(target, "chmod", Chmod); errno_symbol = NODE_PSYMBOL("errno"); encoding_symbol = NODE_PSYMBOL("node:encoding"); diff --git a/test/mjsunit/test-fs-chmod.js b/test/mjsunit/test-fs-chmod.js new file mode 100644 index 0000000000..e4a012c5b8 --- /dev/null +++ b/test/mjsunit/test-fs-chmod.js @@ -0,0 +1,27 @@ +process.mixin(require("./common")); + +var got_error = false; +var success_count = 0; + +var __file = path.join(fixturesDir, "a.js"); + +var promise = fs.chmod(__file, 0777); + +promise.addCallback(function () { + puts(fs.statSync(__file).mode); + assert.equal("777", (fs.statSync(__file).mode & 0777).toString(8)); + + fs.chmodSync(__file, 0644); + assert.equal("644", (fs.statSync(__file).mode & 0777).toString(8)); + success_count++; +}); + +promise.addErrback(function () { + got_error = true; +}); + +process.addListener("exit", function () { + assert.equal(1, success_count); + assert.equal(false, got_error); +}); +