| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c094dda commit 05d6da6
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ | |||
| 10 | 10 | #include <string.h> | |
| 11 | 11 | #include <errno.h> | |
| 12 | 12 | ||
| 13 | + #include <dirent.h> | ||
| 14 | + | ||
| 13 | 15 | namespace node { | |
| 14 | 16 | ||
| 15 | 17 | using namespace v8; | |
@@ -304,6 +306,10 @@ static Handle<Value> SendFile(const Arguments& args) { | |||
| 304 | 306 | } | |
| 305 | 307 | } | |
| 306 | 308 | ||
| 309 | + static inline int scandir_one(struct dirent *unused) { | ||
| 310 | + return 1; | ||
| 311 | + } | ||
| 312 | + | ||
| 307 | 313 | static Handle<Value> ReadDir(const Arguments& args) { | |
| 308 | 314 | HandleScope scope; | |
| 309 | 315 | ||
@@ -316,9 +322,26 @@ static Handle<Value> ReadDir(const Arguments& args) { | |||
| 316 | 322 | if (args[1]->IsFunction()) { | |
| 317 | 323 | ASYNC_CALL(readdir, args[1], *path, 0 /*flags*/) | |
| 318 | 324 | } else { | |
| 319 | - // TODO | ||
| 320 | - return ThrowException(Exception::Error( | ||
| 321 | - String::New("synchronous readdir() not yet supported."))); | ||
| 325 | + struct dirent **eps; | ||
| 326 | + int n = scandir(*path, &eps, scandir_one, alphasort); | ||
| 327 | + | ||
| 328 | + if ( n >= 0) { | ||
| 329 | + int cnt; | ||
| 330 | + char *name; | ||
| 331 | + | ||
| 332 | + Local<Array> res = Array::New(n); | ||
| 333 | + | ||
| 334 | + for(cnt = 0; cnt < n; ++cnt) { | ||
| 335 | + name = eps[cnt]->d_name; | ||
| 336 | + | ||
| 337 | + if (name [0] != '.') { | ||
| 338 | + res->Set(Integer::New(cnt), String::New(name)); | ||
| 339 | + } | ||
| 340 | + } | ||
| 341 | + return scope.Close(res); | ||
| 342 | + } else { | ||
| 343 | + return ThrowException(errno_exception(errno)); | ||
| 344 | + } | ||
| 322 | 345 | } | |
| 323 | 346 | } | |
| 324 | 347 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,28 +2,37 @@ process.mixin(require("./common")); | |||
| 2 | 2 | ||
| 3 | 3 | var got_error = false; | |
| 4 | 4 | ||
| 5 | - fs.readdir(fixturesDir, function (err, files) { | ||
| 5 | + var files = ['a.js' | ||
| 6 | + , 'b' | ||
| 7 | + , 'cycles' | ||
| 8 | + , 'echo.js' | ||
| 9 | + , 'multipart.js' | ||
| 10 | + , 'nested-index' | ||
| 11 | + , 'print-chars.js' | ||
| 12 | + , 'test_ca.pem' | ||
| 13 | + , 'test_cert.pem' | ||
| 14 | + , 'test_key.pem' | ||
| 15 | + , 'throws_error.js' | ||
| 16 | + , 'x.txt' | ||
| 17 | + ]; | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + puts('readdirSync ' + fixturesDir); | ||
| 21 | + var f = fs.readdirSync(fixturesDir); | ||
| 22 | + p(f); | ||
| 23 | + assert.deepEqual(files, f.sort()); | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + puts("readdir " + fixturesDir); | ||
| 27 | + fs.readdir(fixturesDir, function (err, f) { | ||
| 6 | 28 | if (err) { | |
| 7 | 29 | puts("error"); | |
| 8 | 30 | got_error = true; | |
| 9 | 31 | } else { | |
| 10 | - p(files); | ||
| 11 | - assert.deepEqual(['a.js' | ||
| 12 | - , 'b' | ||
| 13 | - , 'cycles' | ||
| 14 | - , 'echo.js' | ||
| 15 | - , 'multipart.js' | ||
| 16 | - , 'nested-index' | ||
| 17 | - , 'print-chars.js' | ||
| 18 | - , 'test_ca.pem' | ||
| 19 | - , 'test_cert.pem' | ||
| 20 | - , 'test_key.pem' | ||
| 21 | - , 'throws_error.js' | ||
| 22 | - , 'x.txt' | ||
| 23 | - ], files.sort()); | ||
| 32 | + p(f); | ||
| 33 | + assert.deepEqual(files, f.sort()); | ||
| 24 | 34 | } | |
| 25 | 35 | }); | |
| 26 | - puts("readdir " + fixturesDir); | ||
| 27 | 36 | ||
| 28 | 37 | process.addListener("exit", function () { | |
| 29 | 38 | assert.equal(false, got_error); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments