Skip to content

Commit ba67d7d

Browse files
authored
[WasmFS] Implement [f]statfs64 syscalls (#16971)
1 parent b0f2f98 commit ba67d7d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ jobs:
371371
- run-tests-linux:
372372
# also add a little select testing for wasm2js in -O3
373373
# also add a little select wasmfs testing
374-
test_targets: "core3 wasm2js3.test_memorygrowth_2 wasmfs.test_hello_world wasmfs.test_hello_world_standalone wasmfs.test_unistd_links* wasmfs.test_atexit_standalone wasmfs.test_emscripten_get_now wasmfs.test_dyncall_specific_minimal_runtime core2ss.test_pthread_dylink wasmfs.test_utime wasmfs.test_unistd_unlink wasmfs.test_unistd_access wasmfs.test_unistd_close wasmfs.test_unistd_truncate wasmfs.test_readdir wasmfs.test_unistd_pipe wasmfs.test_dlfcn_self wasmfs.test_dlfcn_unique_sig wasmfs.test_dylink_basics wasmfs.test_exit_status wasmfs.test_minimal_runtime_memorygrowth wasmfs.test_getcwd_with_non_ascii_name"
374+
test_targets: "core3 wasm2js3.test_memorygrowth_2 wasmfs.test_hello_world wasmfs.test_hello_world_standalone wasmfs.test_unistd_links* wasmfs.test_atexit_standalone wasmfs.test_emscripten_get_now wasmfs.test_dyncall_specific_minimal_runtime core2ss.test_pthread_dylink wasmfs.test_utime wasmfs.test_unistd_unlink wasmfs.test_unistd_access wasmfs.test_unistd_close wasmfs.test_unistd_truncate wasmfs.test_readdir wasmfs.test_unistd_pipe wasmfs.test_dlfcn_self wasmfs.test_dlfcn_unique_sig wasmfs.test_dylink_basics wasmfs.test_exit_status wasmfs.test_minimal_runtime_memorygrowth wasmfs.test_getcwd_with_non_ascii_name other.test_unistd_fstatfs_wasmfs"
375375
test-wasm2js1:
376376
executor: bionic
377377
steps:

system/lib/wasmfs/syscalls.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <stdlib.h>
1717
#include <sys/ioctl.h>
1818
#include <sys/stat.h>
19+
#include <sys/statfs.h>
1920
#include <syscall_arch.h>
2021
#include <unistd.h>
2122
#include <utility>
@@ -611,7 +612,7 @@ __wasi_errno_t __wasi_fd_seek(__wasi_fd_t fd,
611612
return __WASI_ERRNO_SUCCESS;
612613
}
613614

614-
int doChdir(std::shared_ptr<File>& file) {
615+
static int doChdir(std::shared_ptr<File>& file) {
615616
auto dir = file->dynCast<Directory>();
616617
if (!dir) {
617618
return -ENOTDIR;
@@ -1389,6 +1390,45 @@ int __syscall_fcntl64(int fd, int cmd, ...) {
13891390
}
13901391
}
13911392

1393+
static int doStatFS(std::shared_ptr<File>& file, size_t size, struct statfs* buf) {
1394+
if (size != sizeof(struct statfs)) {
1395+
// We only know how to write to a standard statfs, not even a truncated one.
1396+
return -EINVAL;
1397+
}
1398+
1399+
// NOTE: None of the constants here are true. We're just returning safe and
1400+
// sane values, that match the long-existing JS FS behavior (except for
1401+
// the inode number, where we can do better).
1402+
buf->f_type = 0;
1403+
buf->f_bsize = 4096;
1404+
buf->f_frsize = 4096;
1405+
buf->f_blocks = 1000000;
1406+
buf->f_bfree = 500000;
1407+
buf->f_bavail = 500000;
1408+
buf->f_files = file->getIno();
1409+
buf->f_ffree = 1000000;
1410+
buf->f_fsid = {0, 0};
1411+
buf->f_flags = ST_NOSUID;
1412+
buf->f_namelen = 255;
1413+
return 0;
1414+
}
1415+
1416+
int __syscall_statfs64(intptr_t path, size_t size, intptr_t buf) {
1417+
auto parsed = path::parseFile((char*)path);
1418+
if (auto err = parsed.getError()) {
1419+
return err;
1420+
}
1421+
return doStatFS(parsed.getFile(), size, (struct statfs*)buf);
1422+
}
1423+
1424+
int __syscall_fstatfs64(int fd, size_t size, intptr_t buf) {
1425+
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
1426+
if (!openFile) {
1427+
return -EBADF;
1428+
}
1429+
return doStatFS(openFile->locked().getFile(), size, (struct statfs*)buf);
1430+
}
1431+
13921432
// Stubs (at least for now)
13931433

13941434
int __syscall_accept4(int sockfd,

tests/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11629,6 +11629,7 @@ def test_unistd_login(self):
1162911629
def test_unistd_sleep(self):
1163011630
self.do_run_in_out_file_test('unistd/sleep.c')
1163111631

11632+
@also_with_wasmfs
1163211633
def test_unistd_fstatfs(self):
1163311634
self.do_run_in_out_file_test('unistd/fstatfs.c')
1163411635

0 commit comments

Comments
 (0)