diff --git a/src/node_file.cc b/src/node_file.cc index fa33a243df..f5f8ac47b3 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -10,6 +10,8 @@ #include #include +#include + namespace node { using namespace v8; @@ -304,6 +306,10 @@ static Handle SendFile(const Arguments& args) { } } +static inline int scandir_one(struct dirent *unused) { + return 1; +} + static Handle ReadDir(const Arguments& args) { HandleScope scope; @@ -316,9 +322,26 @@ static Handle ReadDir(const Arguments& args) { if (args[1]->IsFunction()) { ASYNC_CALL(readdir, args[1], *path, 0 /*flags*/) } else { - // TODO - return ThrowException(Exception::Error( - String::New("synchronous readdir() not yet supported."))); + struct dirent **eps; + int n = scandir(*path, &eps, scandir_one, alphasort); + + if ( n >= 0) { + int cnt; + char *name; + + Local 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)); + } } } diff --git a/test/mjsunit/test-readdir.js b/test/mjsunit/test-readdir.js index f740560a55..4ded485571 100644 --- a/test/mjsunit/test-readdir.js +++ b/test/mjsunit/test-readdir.js @@ -2,28 +2,37 @@ process.mixin(require("./common")); var got_error = false; -fs.readdir(fixturesDir, function (err, files) { +var files = ['a.js' + , 'b' + , 'cycles' + , 'echo.js' + , 'multipart.js' + , 'nested-index' + , 'print-chars.js' + , 'test_ca.pem' + , 'test_cert.pem' + , 'test_key.pem' + , 'throws_error.js' + , 'x.txt' + ]; + + +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(files); - assert.deepEqual(['a.js' - , 'b' - , 'cycles' - , 'echo.js' - , 'multipart.js' - , 'nested-index' - , 'print-chars.js' - , 'test_ca.pem' - , 'test_cert.pem' - , 'test_key.pem' - , 'throws_error.js' - , 'x.txt' - ], files.sort()); + p(f); + assert.deepEqual(files, f.sort()); } }); -puts("readdir " + fixturesDir); process.addListener("exit", function () { assert.equal(false, got_error);