Skip to content

Add nodefs readdir handling for directories that contain exotic entries #22925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -695,9 +695,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 @@ -711,7 +711,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 @@ -727,7 +737,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
12 changes: 12 additions & 0 deletions test/fs/test_nodefs_readdir.wasm2js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
listing contents of dir=/
.
..
tmp
home
dev
proc
listing contents of dir=/working
existing
stdout
test_nodefs_readdir.js
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: 11 additions & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5779,14 +5779,24 @@ 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')
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('existing/a')
self.emcc_args += ['-lnodefs.js']
self.do_runf('fs/test_nodefs_readdir.c', 'success')
suffix = ''
if self.get_setting('WASMFS'):
suffix = '.wasmfs'
elif self.is_wasm2js():
suffix = ".wasm2js"
self.do_run_in_out_file_test('fs/test_nodefs_readdir.c', out_suffix=suffix)

@requires_node
@crossplatform
Expand Down