Browse Source

Add node.fs.readdir()

v0.7.4-release
Ryan 16 years ago
parent
commit
9b3e2ae192
  1. 38
      src/file.cc
  2. 10
      src/file.h
  3. 25
      test/mjsunit/test-readdir.js
  4. 6
      website/api.txt

38
src/file.cc

@ -147,6 +147,30 @@ EIOPromise::After (eio_req *req)
break;
}
case EIO_READDIR:
{
char *namebuf = static_cast<char*>(req->ptr2);
int nnames = req->result;
Local<Array> names = Array::New(nnames);
for (int i = 0; i < nnames; i++) {
Local<String> 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<Value>
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<Value>
Open (const Arguments& args)
{
@ -332,6 +369,7 @@ File::Initialize (Handle<Object> 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);

10
src/file.h

@ -93,6 +93,16 @@ class EIOPromise : public Promise {
return p->handle_;
}
static v8::Handle<v8::Object>
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:

25
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");
});

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

Loading…
Cancel
Save