Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ var SyscallsLibrary = {
var pos = 0;
var off = FS.llseek(stream, 0, {{{ cDefs.SEEK_CUR }}});

var idx = Math.floor(off / struct_size);

while (idx < stream.getdents.length && pos + struct_size <= count) {
var startIdx = Math.floor(off / struct_size);
var endIdx = Math.min(stream.getdents.length, startIdx + Math.floor(count/struct_size))
for (var idx = startIdx; idx < endIdx; idx++) {
var id;
var type;
var name = stream.getdents[idx];
Expand All @@ -717,7 +717,17 @@ var SyscallsLibrary = {
type = 4; // DT_DIR
}
else {
var child = FS.lookupNode(stream.node, name);
var child;
try {
child = FS.lookupNode(stream.node, name);
} catch (e) {
// If the entry is not a directory, file, or symlink, nodefs
// lookupNode will raise EINVAL. Skip these and continue.
if (e?.errno === {{{ cDefs.EINVAL }}}) {
continue;
}
throw e;
}
id = child.id;
type = FS.isChrdev(child.mode) ? 2 : // DT_CHR, character device.
FS.isDir(child.mode) ? 4 : // DT_DIR, directory.
Expand All @@ -733,7 +743,6 @@ var SyscallsLibrary = {
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_type, 'type', 'i8') }}};
stringToUTF8(name, dirp + pos + {{{ C_STRUCTS.dirent.d_name }}}, 256);
pos += struct_size;
idx += 1;
}
FS.llseek(stream, idx * struct_size, {{{ cDefs.SEEK_SET }}});
return pos;
Expand Down
13 changes: 13 additions & 0 deletions test/fs/test_nodefs_readdir.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
listing contents of dir=/
.
..
tmp
home
dev
proc
listing contents of dir=/working
existing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange that we have . and .. at the root but not in the subdirectory. I guess that just a bug.

stdout
test_nodefs_readdir.js
test_nodefs_readdir.wasm
success
14 changes: 14 additions & 0 deletions test/fs/test_nodefs_readdir.wasmfs.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
listing contents of dir=/
.
..
dev
tmp
listing contents of dir=/working
.
..
existing
named_pipe
stdout
test_nodefs_readdir.js
test_nodefs_readdir.wasm
success
12 changes: 10 additions & 2 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5757,14 +5757,22 @@ def test_fs_nodefs_nofollow(self):
self.emcc_args += ['-lnodefs.js']
self.do_runf('fs/test_nodefs_nofollow.c', 'success')

@crossplatform
@requires_node
def test_fs_nodefs_readdir(self):
# externally setup an existing folder structure: existing/a
if self.get_setting('WASMFS'):
self.set_setting('FORCE_FILESYSTEM')
os.makedirs(os.path.join(self.working_dir, 'existing', 'a'))
if not WINDOWS:
# Add an entry that isn't a directory, file, or link to test that we handle
# it correctly.
os.mkfifo('named_pipe')
os.makedirs(os.path.join('existing', 'a'))
self.emcc_args += ['-lnodefs.js']
self.do_runf('fs/test_nodefs_readdir.c', 'success')
suffix = ''
if self.get_setting('WASMFS'):
suffix = '.wasmfs'
self.do_run_in_out_file_test('fs/test_nodefs_readdir.c', out_suffix=suffix)

@no_windows('no symlink support on windows')
@requires_node
Expand Down