Skip to content
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

Ensure the ino we give to readdir matches the ino we give to stat #23139

Merged
merged 19 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 13 additions & 13 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,23 +697,22 @@ var SyscallsLibrary = {

var startIdx = Math.floor(off / struct_size);
var endIdx = Math.min(stream.getdents.length, startIdx + Math.floor(count/struct_size))
var node;
for (var idx = startIdx; idx < endIdx; idx++) {
var id;
var type;
var name = stream.getdents[idx];
if (name === '.') {
id = stream.node.id;
node = stream.node;
type = 4; // DT_DIR
}
else if (name === '..') {
var lookup = FS.lookupPath(stream.path, { parent: true });
id = lookup.node.id;
node = lookup.node;
type = 4; // DT_DIR
}
else {
var child;
try {
child = FS.lookupNode(stream.node, name);
node = 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.
Expand All @@ -722,16 +721,17 @@ var SyscallsLibrary = {
}
throw e;
}
id = child.id;
type = FS.isChrdev(child.mode) ? 2 : // DT_CHR, character device.
FS.isDir(child.mode) ? 4 : // DT_DIR, directory.
FS.isLink(child.mode) ? 10 : // DT_LNK, symbolic link.
type = FS.isChrdev(node.mode) ? 2 : // DT_CHR, character device.
FS.isDir(node.mode) ? 4 : // DT_DIR, directory.
FS.isLink(node.mode) ? 10 : // DT_LNK, symbolic link.
8; // DT_REG, regular file.
}
#if ASSERTIONS
assert(id);
#endif
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_ino, 'id', 'i64') }}};
// We use getattr to decide which ino we return to stat, so make sure to
// use it to decide which ino we return to readdir too. noderawfs puts the
// native inode into node.id and doesn't define node_ops.getattr so use
// that as a fallback.
var ino = node.node_ops.getattr?.(node).ino ?? node.id;
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_ino, 'ino', 'i64') }}};
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_off, '(idx + 1) * struct_size', 'i64') }}};
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_reclen, C_STRUCTS.dirent.__size__, 'i16') }}};
{{{ makeSetValue('dirp + pos', C_STRUCTS.dirent.d_type, 'type', 'i8') }}};
Expand Down
68 changes: 68 additions & 0 deletions test/fs/test_fs_readdir_ino_matches_stat_ino.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>


#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif

void makedir(const char *dir) {
int rtn = mkdir(dir, 0777);
assert(rtn == 0);
}

void changedir(const char *dir) {
int rtn = chdir(dir);
assert(rtn == 0);
}

void setup() {
#if defined(__EMSCRIPTEN__) && defined(NODEFS)
makedir("working");
emscripten_debugger();
EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working'));
changedir("working");
#endif
}

int main() {
setup();
int res = open("b", O_CREAT, 0777);
assert(res >= 0);
assert(close(res) == 0);
assert(symlink("b", "a") == 0);
int dirfd = open(".", O_RDONLY);
assert(dirfd > 0);
DIR *dirp;
dirp = fdopendir(dirfd);
assert(dirp != NULL);
struct dirent *ep;
int a_ino = -1;
int b_ino = -1;
while ((ep = readdir(dirp))) {
if (strcmp(ep->d_name, "a") == 0) {
a_ino = ep->d_ino;
}
if (strcmp(ep->d_name, "b") == 0) {
b_ino = ep->d_ino;
}
}
assert(a_ino >= 0);
assert(b_ino >= 0);
struct stat sta, stb;
assert(lstat("a", &sta) == 0);
assert(lstat("b", &stb) == 0);
printf("readdir a_ino: %d, b_ino: %d\n", a_ino, b_ino);
printf("stat a_ino: %llu, b_ino: %llu\n", sta.st_ino, stb.st_ino);
assert(a_ino == sta.st_ino);
assert(b_ino == stb.st_ino);
printf("success\n");
}
11 changes: 11 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5900,6 +5900,17 @@ def test_fs_rename_on_existing(self, args):
self.set_setting('FORCE_FILESYSTEM')
self.do_runf('fs/test_fs_rename_on_existing.c', 'success', emcc_args=args)

@parameterized({
'': ([],),
'nodefs': (['-DNODEFS', '-lnodefs.js'],),
'noderawfs': (['-sNODERAWFS'],)
})
def test_fs_readdir_ino_matches_stat_ino(self, args):
if self.get_setting('WASMFS'):
self.set_setting('FORCE_FILESYSTEM')
self.do_runf('fs/test_fs_readdir_ino_matches_stat_ino.c', 'success', emcc_args=args)


hoodmane marked this conversation as resolved.
Show resolved Hide resolved
def test_sigalrm(self):
self.do_runf('test_sigalrm.c', 'Received alarm!')
self.set_setting('EXIT_RUNTIME')
Expand Down
Loading