Skip to content

Commit 55a3c87

Browse files
authored
[WasmFS] Implement the fchmod and newfstatat syscalls (#16987)
Implementing these + the recent PRs that landed allow us to enable and pass test_stat_chmod which tests both chmod and stat operations. The only part that must be disabled there is symlink support.
1 parent aa31673 commit 55a3c87

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

system/lib/wasmfs/syscalls.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ int __syscall_fstatat64(int dirfd, intptr_t path, intptr_t buf, int flags) {
337337
// system. Specific values were chosen to match existing library_fs.js
338338
// values.
339339
// ID of device containing file: Hardcode 1 for now, no meaning at the
340-
buffer->st_dev = 1;
341340
// moment for Emscripten.
341+
buffer->st_dev = 1;
342342
buffer->st_mode = lockedFile.getMode();
343343
buffer->st_ino = file->getIno();
344344
// The number of hard links is 1 since they are unsupported.
@@ -1052,6 +1052,17 @@ int __syscall_chmod(intptr_t path, int mode) {
10521052
return __syscall_fchmodat(AT_FDCWD, path, mode, 0);
10531053
}
10541054

1055+
int __syscall_fchmod(int fd, int mode) {
1056+
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
1057+
if (!openFile) {
1058+
return -EBADF;
1059+
}
1060+
auto lockedFile = openFile->locked().getFile()->locked();
1061+
lockedFile.setMode(mode);
1062+
lockedFile.setCTime(time(NULL));
1063+
return 0;
1064+
}
1065+
10551066
int __syscall_fchownat(
10561067
int dirfd, intptr_t path, int owner, int group, int flags) {
10571068
// Only accept valid flags.
@@ -1432,6 +1443,17 @@ int __syscall_fstatfs64(int fd, size_t size, intptr_t buf) {
14321443
return doStatFS(openFile->locked().getFile(), size, (struct statfs*)buf);
14331444
}
14341445

1446+
int __syscall_newfstatat(int dirfd, intptr_t path, intptr_t buf, int flags) {
1447+
if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW)) {
1448+
return -EINVAL;
1449+
}
1450+
auto parsed = path::getFileAt(dirfd, (char*)path, flags);
1451+
if (auto err = parsed.getError()) {
1452+
return err;
1453+
}
1454+
return doStatFS(parsed.getFile(), sizeof(struct statfs), (struct statfs*)buf);
1455+
}
1456+
14351457
// Stubs (at least for now)
14361458

14371459
int __syscall_accept4(int sockfd,

tests/stat/test_chmod.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void test() {
113113
assert(s.st_mode == (0300 | S_IFDIR));
114114
assert(s.st_ctime != lastctime);
115115

116+
#ifndef WASMFS // TODO https://github.com/emscripten-core/emscripten/issues/15948
116117
//
117118
// chmod a symlink's target
118119
//
@@ -136,6 +137,7 @@ void test() {
136137
// make sure the file it references didn't change
137138
stat("file-link", &s);
138139
assert(s.st_mode == (0400 | S_IFREG));
140+
#endif // WASMFS
139141

140142
puts("success");
141143
}

tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5599,6 +5599,7 @@ def test_stat(self):
55995599
def test_fstatat(self):
56005600
self.do_runf(test_file('stat/test_fstatat.c'), 'success')
56015601

5602+
@also_with_wasmfs
56025603
def test_stat_chmod(self):
56035604
self.do_runf(test_file('stat/test_chmod.c'), 'success')
56045605

0 commit comments

Comments
 (0)