From 9b3e2ae192e73f7ecc55e1686965a66c9a0107ff Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 3 Sep 2009 21:29:20 +0200 Subject: [PATCH] Add node.fs.readdir() --- src/file.cc | 38 ++++++++++++++++++++++++++++++++++++ src/file.h | 10 ++++++++++ test/mjsunit/test-readdir.js | 25 ++++++++++++++++++++++++ website/api.txt | 6 ++++++ 4 files changed, 79 insertions(+) create mode 100644 test/mjsunit/test-readdir.js diff --git a/src/file.cc b/src/file.cc index a79e59d699..c5fbbb3755 100644 --- a/src/file.cc +++ b/src/file.cc @@ -147,6 +147,30 @@ EIOPromise::After (eio_req *req) break; } + case EIO_READDIR: + { + char *namebuf = static_cast(req->ptr2); + int nnames = req->result; + + Local names = Array::New(nnames); + + for (int i = 0; i < nnames; i++) { + Local name = String::New(namebuf); + names->Set(Integer::New(i), name); +#ifndef NDEBUG + namebuf += strlen(namebuf); + assert(*namebuf == '\0'); + namebuf += 1; +#else + namebuf += strlen(namebuf) + 1; +#endif + } + + argc = 1; + argv[0] = names; + break; + } + default: assert(0 && "Unhandled eio response"); } @@ -227,6 +251,19 @@ RMDir (const Arguments& args) return scope.Close(EIOPromise::RMDir(*path)); } +static Handle +ReadDir (const Arguments& args) +{ + HandleScope scope; + + if (args.Length() < 1 || !args[0]->IsString()) { + return ThrowException(BAD_ARGUMENTS); + } + + String::Utf8Value path(args[0]->ToString()); + return scope.Close(EIOPromise::ReadDir(*path)); +} + static Handle Open (const Arguments& args) { @@ -332,6 +369,7 @@ File::Initialize (Handle target) NODE_SET_METHOD(target, "read", Read); NODE_SET_METHOD(target, "rename", Rename); NODE_SET_METHOD(target, "rmdir", RMDir); + NODE_SET_METHOD(target, "readdir", ReadDir); NODE_SET_METHOD(target, "stat", Stat); NODE_SET_METHOD(target, "unlink", Unlink); NODE_SET_METHOD(target, "write", Write); diff --git a/src/file.h b/src/file.h index 02da720b30..3eb4bd53ef 100644 --- a/src/file.h +++ b/src/file.h @@ -93,6 +93,16 @@ class EIOPromise : public Promise { return p->handle_; } + static v8::Handle + ReadDir (const char *path) + { + EIOPromise *p = Create(); + // By using EIO_READDIR_DENTS (or other flags), more complex results can + // be had from eio_readdir. Doesn't seem that we need that though. + p->req_ = eio_readdir(path, 0, EIO_PRI_DEFAULT, After, p); + return p->handle_; + } + static EIOPromise* Create (void); protected: diff --git a/test/mjsunit/test-readdir.js b/test/mjsunit/test-readdir.js new file mode 100644 index 0000000000..1b11a02e84 --- /dev/null +++ b/test/mjsunit/test-readdir.js @@ -0,0 +1,25 @@ + +include("mjsunit.js"); + +var dirname = node.path.dirname(__filename); +var fixtures = node.path.join(dirname, "fixtures"); + +var got_error = false; + +var promise = node.fs.readdir(fixtures); +puts("readdir " + fixtures); + +promise.addCallback(function (files) { + p(files); + assertArrayEquals(["b","a.js","x.txt"], files); +}); + +promise.addErrback(function () { + puts("error"); + got_error = true; +}); + +process.addListener("exit", function () { + assertFalse(got_error); + puts("exit"); +}); diff --git a/website/api.txt b/website/api.txt index f99cf00103..cf615e84d5 100644 --- a/website/api.txt +++ b/website/api.txt @@ -453,6 +453,12 @@ node.fs.stat("/tmp/world").addCallback(function (stats) { - on success: no parameters. - on error: no parameters. ++node.fs.readdir(path)+ :: + Reads the contents of a directory. + - on success: One argument, an array containing the names (strings) of the + files in the directory (excluding "." and ".."). + - on error: no parameters. + +node.fs.close(fd)+ :: See close(2)