Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThisIsMissEm/306662 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/306662 to your computer and use it in GitHub Desktop.
From 02781bb4d71e90d50fae65a56becff3686220966 Mon Sep 17 00:00:00 2001
From: Micheil Smith <micheil@brandedcode.com>
Date: Thu, 18 Feb 2010 19:02:55 +1100
Subject: [PATCH] Adding interface between node and libeio for Chmod.
---
doc/api.txt | 6 +++++-
src/node.js | 11 +++++++++++
src/node_file.cc | 24 ++++++++++++++++++++++++
test/mjsunit/test-fs-chmod.js | 27 +++++++++++++++++++++++++++
4 files changed, 67 insertions(+), 1 deletions(-)
create mode 100644 test/mjsunit/test-fs-chmod.js
diff --git a/doc/api.txt b/doc/api.txt
index 93bb671..a3e5082 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 302790f..4506bd8 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 16f4045..b1d4fe7 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -428,6 +428,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 +465,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");
diff --git a/test/mjsunit/test-fs-chmod.js b/test/mjsunit/test-fs-chmod.js
new file mode 100644
index 0000000..e4a012c
--- /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);
+});
+
--
1.6.5.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment