Skip to content

Fix for getdents (readdir) under NODEFS. #16124

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 1 commit into from
Jan 28, 2022
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
3 changes: 0 additions & 3 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,13 +1061,10 @@ FS.staticInit();` +
var stream = FS.createStream({
node: node,
path: FS.getPath(node), // we want the absolute path to the node
id: node.id,
flags: flags,
mode: node.mode,
seekable: true,
position: 0,
stream_ops: node.stream_ops,
node_ops: node.node_ops,
// used by the file family libc calls (fopen, fwrite, ferror, etc.)
ungotten: [],
error: false
Expand Down
9 changes: 7 additions & 2 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ mergeInto(LibraryManager.library, {
}`,
$NODERAWFS: {
lookup: function(parent, name) {
#if ASSERTIONS
assert(parent)
assert(parent.path)
#endif
return FS.lookupPath(parent.path + '/' + name).node;
},
lookupPath: function(path, opts) {
Expand All @@ -38,7 +42,7 @@ mergeInto(LibraryManager.library, {
}
var st = fs.lstatSync(path);
var mode = NODEFS.getMode(path);
return { path: path, node: { id: st.ino, mode: mode }};
return { path: path, node: { id: st.ino, mode: mode, node_ops: NODERAWFS, path: path }};
},
createStandardStreams: function() {
FS.streams[0] = { fd: 0, nfd: 0, position: 0, path: '', flags: 0, tty: true, seekable: false };
Expand Down Expand Up @@ -90,7 +94,8 @@ mergeInto(LibraryManager.library, {
}
var newMode = NODEFS.getMode(pathTruncated);
var fd = suggestFD != null ? suggestFD : FS.nextfd(nfd);
var stream = { fd: fd, nfd: nfd, position: 0, path: path, id: st.ino, flags: flags, mode: newMode, node_ops: NODERAWFS, seekable: true };
var node = { id: st.ino, mode: newMode, node_ops: NODERAWFS, path: path }
var stream = { fd: fd, nfd: nfd, position: 0, path: path, flags: flags, node: node, seekable: true };
FS.streams[fd] = stream;
return stream;
},
Expand Down
4 changes: 2 additions & 2 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ var SyscallsLibrary = {
var type;
var name = stream.getdents[idx];
if (name === '.') {
id = stream.id;
id = stream.node.id;
type = 4; // DT_DIR
}
else if (name === '..') {
Expand All @@ -867,7 +867,7 @@ var SyscallsLibrary = {
type = 4; // DT_DIR
}
else {
var child = FS.lookupNode(stream, name);
var child = FS.lookupNode(stream.node, name);
id = child.id;
type = FS.isChrdev(child.mode) ? 2 : // DT_CHR, character device.
FS.isDir(child.mode) ? 4 : // DT_DIR, directory.
Expand Down
9 changes: 6 additions & 3 deletions tests/dirent/test_readdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ void test() {
for (i = 0; i < 3; i++) {
errno = 0;
ent = readdir(dir);
//printf("ent, errno: %p, %d\n", ent, errno);
assert(ent);
//printf("%d file: %s (%d : %d)\n", i, ent->d_name, ent->d_reclen, sizeof(*ent));
if (ent) {
fprintf(stderr, "%d file: %s (%d : %lu)\n", i, ent->d_name, ent->d_reclen, sizeof(*ent));
} else {
fprintf(stderr, "ent: %p, errno: %d\n", ent, errno);
assert(ent);
}
assert(ent->d_reclen == sizeof(*ent));
if (!seen[0] && !strcmp(ent->d_name, ".")) {
assert(ent->d_type & DT_DIR);
Expand Down
33 changes: 33 additions & 0 deletions tests/fs/test_nodefs_readdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <assert.h>
#include <dirent.h>
#include <emscripten.h>
#include <stdio.h>

void list_dir(const char *path) {
printf("listing contents of dir=%s\n", path);
struct dirent* entry;
DIR* dir = opendir(path);
assert(dir);
int n = 0;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
++n;
}
assert(n);
closedir(dir);
}

int main(int argc, char * argv[]) {
list_dir("/");

// mount the current folder as a NODEFS instance
// inside of emscripten and create folders nodefs/a
// in mounted directory
EM_ASM(
FS.mkdir('/working');
FS.mount(NODEFS, { root: '.' }, '/working');
);

list_dir("/working");
puts("success");
}
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5433,6 +5433,12 @@ def test_fs_nodefs_nofollow(self):
self.emcc_args += ['-lnodefs.js']
self.do_runf(test_file('fs/test_nodefs_nofollow.c'), 'success', js_engines=[config.NODE_JS])

def test_fs_nodefs_readdir(self):
# externally setup an existing folder structure: existing/a
os.makedirs(os.path.join(self.working_dir, 'existing', 'a'))
self.emcc_args += ['-lnodefs.js']
self.do_runf(test_file('fs/test_nodefs_readdir.c'), 'success')

@no_windows('no symlink support on windows')
def test_fs_noderawfs_nofollow(self):
self.set_setting('NODERAWFS')
Expand Down