Browse Source

Adding interface between node and libeio for Chmod.

v0.7.4-release
Micheil Smith 15 years ago
committed by Ryan Dahl
parent
commit
bcc032e43a
  1. 6
      doc/api.txt
  2. 11
      src/node.js
  3. 25
      src/node_file.cc
  4. 27
      test/mjsunit/test-fs-chmod.js

6
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:

11
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;

25
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<Value> Read(const Arguments& args) {
}
}
/* node.fs.chmod(fd, mode);
* Wrapper for chmod(1) / EIO_CHMOD
*/
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 {
int ret = chmod(*path, mode);
if (ret != 0) return ThrowException(errno_exception(errno));
return Undefined();
}
}
void File::Initialize(Handle<Object> target) {
HandleScope scope;
@ -443,6 +466,8 @@ void File::Initialize(Handle<Object> 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");

27
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);
});
Loading…
Cancel
Save