Browse Source

Add fs.readdirSync()

v0.7.4-release
Micheil Smith 15 years ago
committed by Ryan Dahl
parent
commit
05d6da6c4a
  1. 29
      src/node_file.cc
  2. 27
      test/mjsunit/test-readdir.js

29
src/node_file.cc

@ -10,6 +10,8 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <dirent.h>
namespace node { namespace node {
using namespace v8; using namespace v8;
@ -304,6 +306,10 @@ static Handle<Value> SendFile(const Arguments& args) {
} }
} }
static inline int scandir_one(struct dirent *unused) {
return 1;
}
static Handle<Value> ReadDir(const Arguments& args) { static Handle<Value> ReadDir(const Arguments& args) {
HandleScope scope; HandleScope scope;
@ -316,9 +322,26 @@ static Handle<Value> ReadDir(const Arguments& args) {
if (args[1]->IsFunction()) { if (args[1]->IsFunction()) {
ASYNC_CALL(readdir, args[1], *path, 0 /*flags*/) ASYNC_CALL(readdir, args[1], *path, 0 /*flags*/)
} else { } else {
// TODO struct dirent **eps;
return ThrowException(Exception::Error( int n = scandir(*path, &eps, scandir_one, alphasort);
String::New("synchronous readdir() not yet supported.")));
if ( n >= 0) {
int cnt;
char *name;
Local<Array> res = Array::New(n);
for(cnt = 0; cnt < n; ++cnt) {
name = eps[cnt]->d_name;
if (name [0] != '.') {
res->Set(Integer::New(cnt), String::New(name));
}
}
return scope.Close(res);
} else {
return ThrowException(errno_exception(errno));
}
} }
} }

27
test/mjsunit/test-readdir.js

@ -2,13 +2,7 @@ process.mixin(require("./common"));
var got_error = false; var got_error = false;
fs.readdir(fixturesDir, function (err, files) { var files = ['a.js'
if (err) {
puts("error");
got_error = true;
} else {
p(files);
assert.deepEqual(['a.js'
, 'b' , 'b'
, 'cycles' , 'cycles'
, 'echo.js' , 'echo.js'
@ -20,10 +14,25 @@ fs.readdir(fixturesDir, function (err, files) {
, 'test_key.pem' , 'test_key.pem'
, 'throws_error.js' , 'throws_error.js'
, 'x.txt' , 'x.txt'
], files.sort()); ];
puts('readdirSync ' + fixturesDir);
var f = fs.readdirSync(fixturesDir);
p(f);
assert.deepEqual(files, f.sort());
puts("readdir " + fixturesDir);
fs.readdir(fixturesDir, function (err, f) {
if (err) {
puts("error");
got_error = true;
} else {
p(f);
assert.deepEqual(files, f.sort());
} }
}); });
puts("readdir " + fixturesDir);
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(false, got_error); assert.equal(false, got_error);

Loading…
Cancel
Save