Implement fs.readdirSync()
This commit is contained in:
parent
49cd1bbf84
commit
05ae932a0e
@ -635,6 +635,10 @@ Asynchronous readdir(3). Reads the contents of a directory.
|
|||||||
The callback gets two arguments +(err, files)+ where +files+ is an array of
|
The callback gets two arguments +(err, files)+ where +files+ is an array of
|
||||||
the names of the files in the directory excluding +"."+ and +".."+.
|
the names of the files in the directory excluding +"."+ and +".."+.
|
||||||
|
|
||||||
|
+fs.readdir(path, callback)+ ::
|
||||||
|
Synchronous readdir(3). Returns an array of filenames excluding +"."+ and
|
||||||
|
+".."+.
|
||||||
|
|
||||||
|
|
||||||
+fs.close(fd, callback)+ ::
|
+fs.close(fd, callback)+ ::
|
||||||
Asynchronous close(2).
|
Asynchronous close(2).
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -336,9 +337,27 @@ 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
|
DIR *dir = opendir(*path);
|
||||||
return ThrowException(Exception::Error(
|
if (!dir) return ThrowException(errno_exception(errno));
|
||||||
String::New("synchronous readdir() not yet supported.")));
|
|
||||||
|
struct dirent *ent;
|
||||||
|
|
||||||
|
Local<Array> files = Array::New();
|
||||||
|
char *name;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (ent = readdir(dir)) {
|
||||||
|
name = ent->d_name;
|
||||||
|
|
||||||
|
if (name[0] != '.' || (name[1] && (name[1] != '.' || name[2]))) {
|
||||||
|
files->Set(Integer::New(i), String::New(name));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
|
||||||
|
return scope.Close(files);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,28 +2,37 @@ process.mixin(require("./common"));
|
|||||||
|
|
||||||
var got_error = false;
|
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) {
|
if (err) {
|
||||||
puts("error");
|
puts("error");
|
||||||
got_error = true;
|
got_error = true;
|
||||||
} else {
|
} else {
|
||||||
p(files);
|
p(f);
|
||||||
assert.deepEqual(['a.js'
|
assert.deepEqual(files, f.sort());
|
||||||
, '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());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
puts("readdir " + fixturesDir);
|
|
||||||
|
|
||||||
process.addListener("exit", function () {
|
process.addListener("exit", function () {
|
||||||
assert.equal(false, got_error);
|
assert.equal(false, got_error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user