Created
February 24, 2010 12:46
-
-
Save ThisIsMissEm/313398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/node.js b/src/node.js | |
index 32f3b6d..f321e43 100644 | |
--- a/src/node.js | |
+++ b/src/node.js | |
@@ -537,6 +537,16 @@ var fsModule = createInternalModule("fs", function (exports) { | |
exports.chmodSync = function (path, mode) { | |
return process.fs.chmod(path, mode); | |
}; | |
+ | |
+ exports.glob = function(pattern, callback) { | |
+ process.fs.glob(pattern, callback || noop); | |
+ }; | |
+ | |
+ exports.globSync = function(pattern) { | |
+ var result = process.fs.glob(pattern); | |
+ process.stdio.writeError(JSON.stringify(result) + "\n"); | |
+ return result; | |
+ }; | |
function writeAll (fd, data, encoding, callback) { | |
exports.write(fd, data, 0, encoding, function (writeErr, written) { | |
diff --git a/src/node_file.cc b/src/node_file.cc | |
index ee2c2d6..6d00017 100644 | |
--- a/src/node_file.cc | |
+++ b/src/node_file.cc | |
@@ -11,6 +11,7 @@ | |
#include <string.h> | |
#include <errno.h> | |
#include <limits.h> | |
+#include <glob.h> | |
/* used for readlink, AIX doesn't provide it */ | |
#ifndef PATH_MAX | |
@@ -562,6 +563,100 @@ static Handle<Value> Chmod(const Arguments& args){ | |
} | |
} | |
+/* node.fs.glob(pattern); | |
+ * Wrapper for glob(1) / EIO_custom around glob | |
+ */ | |
+typedef struct eio_req_glob eio_req_glob; | |
+struct eio_req_glob { | |
+ void *callback; | |
+ glob_t g; | |
+ char pattern[1]; | |
+}; | |
+ | |
+static int After_Glob (eio_req *req) { | |
+ HandleScope scope; | |
+ | |
+ struct eio_req_glob * rreq = (struct eio_req_glob *)(req->data); | |
+ | |
+ Persistent<Function> *callback = reinterpret_cast<Persistent<Function>*>(rreq->callback); | |
+ //assert((*callback)->IsFunction()); | |
+ | |
+ ev_unref(EV_DEFAULT_UC); | |
+ | |
+ int argc = 0; | |
+ Local<Value> argv[6]; // 6 is the maximum number of args | |
+ | |
+ if (req->errorno != 0) { | |
+ argc = 1; | |
+ argv[0] = errno_exception(req->errorno); | |
+ } else { | |
+ // Note: the error is always given the first argument of the callback. | |
+ // If there is no error then then the first argument is null. | |
+ argc = 2; | |
+ argv[0] = Local<Value>::New(Null()); | |
+ argv[1] = Integer::New(req->result); | |
+ } | |
+ | |
+ TryCatch try_catch; | |
+ | |
+ (*callback)->Call(Context::GetCurrent()->Global(), argc, argv); | |
+ | |
+ if (try_catch.HasCaught()) { | |
+ FatalException(try_catch); | |
+ } | |
+ | |
+ // Dispose of the persistent handle | |
+ callback->Dispose(); | |
+ delete callback; | |
+ | |
+ return 0; | |
+} | |
+ | |
+static int eio_glob (eio_req *req) { | |
+ struct eio_req_glob * rreq = (struct eio_req_glob *)(req->data); | |
+ | |
+ req->result = glob(rreq->pattern, 0, NULL, &rreq->g); | |
+ | |
+ return 0; | |
+} | |
+ | |
+static Handle<Value> Glob(const Arguments& args) { | |
+ HandleScope scope; | |
+ | |
+ if(args.Length() < 1 || !args[0]->IsString()) { | |
+ return THROW_BAD_ARGS; | |
+ } | |
+ | |
+ String::Utf8Value pattern(args[0]->ToString()); | |
+ | |
+ if(args[1]->IsFunction()) { | |
+ eio_req *req; | |
+ req = (eio_req *)calloc (1, sizeof *req); | |
+ | |
+ struct eio_req_glob * rreq = (struct eio_req_glob *) calloc(1, sizeof *req); | |
+ | |
+ if(!rreq) { | |
+ V8::LowMemoryNotification(); | |
+ return ThrowException(Exception::Error(String::New("Could not allocate enough memory"))); | |
+ } | |
+ | |
+ strcpy(rreq->pattern, *pattern); | |
+ | |
+ | |
+ rreq->callback = persistent_callback(args[1]); | |
+ | |
+ req->data = rreq; | |
+ eio_custom(eio_glob, EIO_PRI_DEFAULT, After_Glob, req); | |
+ | |
+ assert(req); | |
+ ev_ref(EV_DEFAULT_UC); | |
+ | |
+ return Undefined(); | |
+ } else { | |
+ return ThrowException(Exception::Error(String::New("synchronous glob() not yet supported."))); | |
+ } | |
+} | |
+ | |
void File::Initialize(Handle<Object> target) { | |
HandleScope scope; | |
@@ -584,6 +679,7 @@ void File::Initialize(Handle<Object> target) { | |
NODE_SET_METHOD(target, "write", Write); | |
NODE_SET_METHOD(target, "chmod", Chmod); | |
+ NODE_SET_METHOD(target, "glob", Glob); | |
errno_symbol = NODE_PSYMBOL("errno"); | |
encoding_symbol = NODE_PSYMBOL("node:encoding"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment