Skip to content

Commit bcc032e

Browse files
miksagory
authored andcommitted
Adding interface between node and libeio for Chmod.
1 parent cb32883 commit bcc032e

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎doc/api.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,11 @@ fs.rename("/tmp/hello", "/tmp/world").addCallback(function () {
632632
- on success: no parameters.
633633
- on error: no parameters.
634634

635-
635+
+fs.chmod(path, mode)+ ::
636+
See chmod(1)
637+
- on success: no parameters.
638+
- on error: no parameters.
639+
636640
+fs.stat(path)+ ::
637641
See stat(2).
638642
- on success: Returns +fs.Stats+ object. It looks like this:

‎src/node.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,17 @@ var fsModule = createInternalModule("fs", function (exports) {
769769

770770
return content;
771771
};
772+
773+
exports.chmod = function(path, mode){
774+
var promise = new events.Promise();
775+
process.fs.chmod(path, mode, callback(promise));
776+
return promise;
777+
};
778+
779+
exports.chmodSync = function(path, mode){
780+
return process.fs.chmod(path, mode);
781+
};
782+
772783
});
773784

774785
var fs = fsModule.exports;

‎src/node_file.cc‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static int After(eio_req *req) {
5454
case EIO_RMDIR:
5555
case EIO_MKDIR:
5656
case EIO_FTRUNCATE:
57+
case EIO_CHMOD:
5758
argc = 0;
5859
break;
5960

@@ -428,6 +429,28 @@ static Handle<Value> Read(const Arguments& args) {
428429
}
429430
}
430431

432+
/* node.fs.chmod(fd, mode);
433+
* Wrapper for chmod(1) / EIO_CHMOD
434+
*/
435+
static Handle<Value> Chmod(const Arguments& args){
436+
HandleScope scope;
437+
438+
if(args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) {
439+
return THROW_BAD_ARGS;
440+
}
441+
String::Utf8Value path(args[0]->ToString());
442+
mode_t mode = static_cast<mode_t>(args[1]->Int32Value());
443+
444+
if(args[2]->IsFunction()) {
445+
ASYNC_CALL(chmod, args[2], *path, mode);
446+
} else {
447+
int ret = chmod(*path, mode);
448+
if (ret != 0) return ThrowException(errno_exception(errno));
449+
return Undefined();
450+
}
451+
}
452+
453+
431454
void File::Initialize(Handle<Object> target) {
432455
HandleScope scope;
433456

@@ -443,6 +466,8 @@ void File::Initialize(Handle<Object> target) {
443466
NODE_SET_METHOD(target, "stat", Stat);
444467
NODE_SET_METHOD(target, "unlink", Unlink);
445468
NODE_SET_METHOD(target, "write", Write);
469+
470+
NODE_SET_METHOD(target, "chmod", Chmod);
446471

447472
errno_symbol = NODE_PSYMBOL("errno");
448473
encoding_symbol = NODE_PSYMBOL("node:encoding");

‎test/mjsunit/test-fs-chmod.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
process.mixin(require("./common"));
2+
3+
var got_error = false;
4+
var success_count = 0;
5+
6+
var __file = path.join(fixturesDir, "a.js");
7+
8+
var promise = fs.chmod(__file, 0777);
9+
10+
promise.addCallback(function () {
11+
puts(fs.statSync(__file).mode);
12+
assert.equal("777", (fs.statSync(__file).mode & 0777).toString(8));
13+
14+
fs.chmodSync(__file, 0644);
15+
assert.equal("644", (fs.statSync(__file).mode & 0777).toString(8));
16+
success_count++;
17+
});
18+
19+
promise.addErrback(function () {
20+
got_error = true;
21+
});
22+
23+
process.addListener("exit", function () {
24+
assert.equal(1, success_count);
25+
assert.equal(false, got_error);
26+
});
27+

0 commit comments

Comments
 (0)