From 0896af783f83398416e4a3e919f158c5f336493d Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Wed, 25 Sep 2024 19:50:15 +0700 Subject: [PATCH 01/14] Add statfs function to FS --- src/library_fs.js | 24 ++++++++++++++++++++++++ src/library_nodefs.js | 16 ++++++++++++++++ src/library_syscall.js | 22 +++++++++++----------- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index d3f70f83f45a..bc03a19378a4 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -672,6 +672,30 @@ FS.staticInit(); } return parent.node_ops.mknod(parent, name, mode, dev); }, + statfs(path) { + var lookup = FS.lookupPath(path, { follow: true }); + var parent = lookup.node + + // Error handling + var defaults = { + bsize: 4096, + frsize: 4096, + blocks: 1e6, + bfree: 5e5, + bavail: 5e5, + files: FS.nextInode, + ffree: 1e6, + fsid: 42, + flags: 2, + namelen: 255 + }; + + if (typeof parent.node_ops?.statfs === 'function') { + return { ...defaults, ...parent.node_ops.statfs(parent.mount.opts.root) }; + } else { + return defaults; + } + }, // helpers to create specific types of nodes create(path, mode) { mode = mode !== undefined ? mode : 438 /* 0666 */; diff --git a/src/library_nodefs.js b/src/library_nodefs.js index d30c69815c13..cf99b3ccbd28 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -118,6 +118,9 @@ addToLibrary({ return newFlags; }, + statfs: function(path) { + return fs.statfsSync(path); + }, node_ops: { getattr(node) { var path = NODEFS.realPath(node); @@ -214,6 +217,19 @@ addToLibrary({ var path = NODEFS.realPath(node); return NODEFS.tryFSOperation(() => fs.readlinkSync(path)); }, + statfs(path) { + var stats = fs.statfsSync(path); + return { + bsize: stats.bsize, + frsize: stats.bsize, + blocks: stats.blocks, + bfree: stats.bfree, + bavail: stats.bavail, + files: stats.files, + ffree: stats.ffree, + fsid: stats.type + } + } }, stream_ops: { open(stream) { diff --git a/src/library_syscall.js b/src/library_syscall.js index 2ae0b8f23652..11a8297cf677 100644 --- a/src/library_syscall.js +++ b/src/library_syscall.js @@ -802,22 +802,22 @@ var SyscallsLibrary = { }, __syscall_statfs64: (path, size, buf) => { - path = SYSCALLS.getStr(path); #if ASSERTIONS assert(size === {{{ C_STRUCTS.statfs.__size__ }}}); #endif + var defaults = FS.statfs(SYSCALLS.getStr(path)); // NOTE: None of the constants here are true. We're just returning safe and // sane values. - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bsize, '4096', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_frsize, '4096', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_blocks, '1000000', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bfree, '500000', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bavail, '500000', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_files, 'FS.nextInode', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_ffree, '1000000', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_fsid, '42', 'i32') }}}; - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_flags, '2', 'i32') }}}; // ST_NOSUID - {{{ makeSetValue('buf', C_STRUCTS.statfs.f_namelen, '255', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bsize, 'defaults.bsize', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_frsize, 'defaults.bsize', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_blocks, 'defaults.blocks', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bfree, 'defaults.bfree', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bavail, 'defaults.bavail', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_files, 'defaults.files', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_ffree, 'defaults.ffree', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_fsid, 'defaults.fsid', 'i32') }}}; + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_flags, 'defaults.flags', 'i32') }}}; // ST_NOSUID + {{{ makeSetValue('buf', C_STRUCTS.statfs.f_namelen, 'defaults.namelen', 'i32') }}}; return 0; }, __syscall_fstatfs64__deps: ['__syscall_statfs64'], From 5770db85c107c0c598b6ff1dba921e78616d841f Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Wed, 25 Sep 2024 20:39:53 +0700 Subject: [PATCH 02/14] Remove fsid and keep it hardcoded --- src/library_fs.js | 2 +- src/library_nodefs.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index bc03a19378a4..7496be0ad2a3 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -687,7 +687,7 @@ FS.staticInit(); ffree: 1e6, fsid: 42, flags: 2, - namelen: 255 + namelen: 255, }; if (typeof parent.node_ops?.statfs === 'function') { diff --git a/src/library_nodefs.js b/src/library_nodefs.js index cf99b3ccbd28..3a304ac7da00 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -219,6 +219,7 @@ addToLibrary({ }, statfs(path) { var stats = fs.statfsSync(path); + console.log(stats.type) return { bsize: stats.bsize, frsize: stats.bsize, @@ -227,7 +228,7 @@ addToLibrary({ bavail: stats.bavail, files: stats.files, ffree: stats.ffree, - fsid: stats.type + type: stats.type } } }, From a18edbe2fc04b147b525e399572d28501f257c49 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 20:47:36 +0700 Subject: [PATCH 03/14] Refactoring and add test --- src/library_fs.js | 3 ++- src/library_nodefs.js | 15 ++++--------- src/library_syscall.js | 2 -- test/fs/test_nodefs_statfs.c | 42 ++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 test/fs/test_nodefs_statfs.c diff --git a/src/library_fs.js b/src/library_fs.js index 7496be0ad2a3..d91b76b65353 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -676,7 +676,8 @@ FS.staticInit(); var lookup = FS.lookupPath(path, { follow: true }); var parent = lookup.node - // Error handling + // NOTE: None of the defaults here are true. We're just returning safe and + // sane values. var defaults = { bsize: 4096, frsize: 4096, diff --git a/src/library_nodefs.js b/src/library_nodefs.js index 3a304ac7da00..3418244092be 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -218,18 +218,11 @@ addToLibrary({ return NODEFS.tryFSOperation(() => fs.readlinkSync(path)); }, statfs(path) { - var stats = fs.statfsSync(path); - console.log(stats.type) + var stats = NODEFS.tryFSOperation(() => fs.statfsSync(path)); return { - bsize: stats.bsize, - frsize: stats.bsize, - blocks: stats.blocks, - bfree: stats.bfree, - bavail: stats.bavail, - files: stats.files, - ffree: stats.ffree, - type: stats.type - } + ...stats, + frsize: stats.bsize + }; } }, stream_ops: { diff --git a/src/library_syscall.js b/src/library_syscall.js index 11a8297cf677..ca9a147c7367 100644 --- a/src/library_syscall.js +++ b/src/library_syscall.js @@ -806,8 +806,6 @@ var SyscallsLibrary = { assert(size === {{{ C_STRUCTS.statfs.__size__ }}}); #endif var defaults = FS.statfs(SYSCALLS.getStr(path)); - // NOTE: None of the constants here are true. We're just returning safe and - // sane values. {{{ makeSetValue('buf', C_STRUCTS.statfs.f_bsize, 'defaults.bsize', 'i32') }}}; {{{ makeSetValue('buf', C_STRUCTS.statfs.f_frsize, 'defaults.bsize', 'i32') }}}; {{{ makeSetValue('buf', C_STRUCTS.statfs.f_blocks, 'defaults.blocks', 'i32') }}}; diff --git a/test/fs/test_nodefs_statfs.c b/test/fs/test_nodefs_statfs.c new file mode 100644 index 000000000000..c11382c18f20 --- /dev/null +++ b/test/fs/test_nodefs_statfs.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +void test_statfs(const char *path) { + printf("Testing statfs for path: %s\n", path); + struct statvfs st; + int result = statvfs(path, &st); + + assert(result == 0 && "statvfs should succeed"); + + printf("File system block size: %lu\n", st.f_bsize); + printf("Total blocks: %lu\n", st.f_blocks); + printf("Free blocks: %lu\n", st.f_bfree); + printf("Available blocks: %lu\n", st.f_bavail); + printf("Total inodes: %lu\n", st.f_files); + printf("Free inodes: %lu\n", st.f_ffree); + + // Basic sanity checks + assert(st.f_bsize > 0 && "Block size should be positive"); + assert(st.f_blocks > 0 && "Total blocks should be positive"); + assert(st.f_bfree <= st.f_blocks && "Free blocks should not exceed total blocks"); + assert(st.f_bavail <= st.f_bfree && "Available blocks should not exceed free blocks"); + assert(st.f_files > 0 && "Total inodes should be positive"); + assert(st.f_ffree <= st.f_files && "Free inodes should not exceed total inodes"); +} + +int main() { + // Test the root filesystem (which should be MEMFS by default) + test_statfs("/"); + + // Mount NODEFS and test it + EM_ASM( + FS.mkdir('/working'); + FS.mount(NODEFS, { root: '.' }, '/working'); + ); + + test_statfs("/working"); + + puts("success"); +} From c3c0c5f4e74a2148b97ac664673ae6a2bd8b976e Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 21:17:04 +0700 Subject: [PATCH 04/14] Fix statvfs test --- src/library_fs.js | 10 +++++----- test/core/test_statvfs.c | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index d91b76b65353..f10a63cace9b 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -673,8 +673,6 @@ FS.staticInit(); return parent.node_ops.mknod(parent, name, mode, dev); }, statfs(path) { - var lookup = FS.lookupPath(path, { follow: true }); - var parent = lookup.node // NOTE: None of the defaults here are true. We're just returning safe and // sane values. @@ -691,11 +689,13 @@ FS.staticInit(); namelen: 255, }; - if (typeof parent.node_ops?.statfs === 'function') { - return { ...defaults, ...parent.node_ops.statfs(parent.mount.opts.root) }; - } else { + var lookup = FS.lookupPath(path, {follow: true}); + var parent = lookup.node; + if (typeof parent.node_ops?.statfs !== 'function') { return defaults; } + + return { ...defaults, ...parent.node_ops.statfs(parent.mount.opts.root) }; }, // helpers to create specific types of nodes create(path, mode) { diff --git a/test/core/test_statvfs.c b/test/core/test_statvfs.c index 2de11485b03f..2b310f85e846 100644 --- a/test/core/test_statvfs.c +++ b/test/core/test_statvfs.c @@ -8,10 +8,13 @@ #include #include #include +#include +#include int main() { struct statvfs s; + mkdir("/test", S_IRWXU | S_IRWXG | S_IRWXO); printf("result: %d\n", statvfs("/test", &s)); printf("errno: %d\n", errno); From d2e8f4c04bca8b2eb58ba03af777104ce194eeb2 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 21:34:53 +0700 Subject: [PATCH 05/14] Add test and fix cases where the default free inodes is larger than total inodes --- src/library_fs.js | 3 ++- test/fs/test_nodefs_statfs.c | 7 +------ test/test_core.py | 9 +++++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index f10a63cace9b..07973b0b49de 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -683,7 +683,8 @@ FS.staticInit(); bfree: 5e5, bavail: 5e5, files: FS.nextInode, - ffree: 1e6, + // Free inodes can not be larger than total inodes so calculate a reasonable value here + ffree: () => Math.max(0, Math.floor(this.files * 0.9) - this.files), fsid: 42, flags: 2, namelen: 255, diff --git a/test/fs/test_nodefs_statfs.c b/test/fs/test_nodefs_statfs.c index c11382c18f20..956d3bd18b8f 100644 --- a/test/fs/test_nodefs_statfs.c +++ b/test/fs/test_nodefs_statfs.c @@ -10,12 +10,7 @@ void test_statfs(const char *path) { assert(result == 0 && "statvfs should succeed"); - printf("File system block size: %lu\n", st.f_bsize); - printf("Total blocks: %lu\n", st.f_blocks); - printf("Free blocks: %lu\n", st.f_bfree); - printf("Available blocks: %lu\n", st.f_bavail); - printf("Total inodes: %lu\n", st.f_files); - printf("Free inodes: %lu\n", st.f_ffree); + // Basic sanity checks assert(st.f_bsize > 0 && "Block size should be positive"); diff --git a/test/test_core.py b/test/test_core.py index 6c38e9fede6d..907ba01790bc 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5779,6 +5779,15 @@ def test_fs_nodefs_readdir(self): self.emcc_args += ['-lnodefs.js'] self.do_runf('fs/test_nodefs_readdir.c', 'success') + @requires_node + def test_fs_nodefs_statfs(self): + # externally setup an existing folder structure: existing/a + if self.get_setting('WASMFS'): + self.set_setting('FORCE_FILESYSTEM') + os.makedirs(os.path.join(self.working_dir, 'existing', 'a')) + self.emcc_args += ['-lnodefs.js'] + self.do_runf('fs/test_nodefs_statfs.c', 'success') + @no_windows('no symlink support on windows') @requires_node def test_fs_noderawfs_nofollow(self): From 5adafc1b38dfb171101313f10a52313d06b4d150 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 21:36:17 +0700 Subject: [PATCH 06/14] Remove unnecessary variable --- src/library_fs.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index 07973b0b49de..3abd77e9b88c 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -690,8 +690,7 @@ FS.staticInit(); namelen: 255, }; - var lookup = FS.lookupPath(path, {follow: true}); - var parent = lookup.node; + var parent = FS.lookupPath(path, {follow: true}).node; if (typeof parent.node_ops?.statfs !== 'function') { return defaults; } From d96e4611d8e9c6605f0882b252b862afd20c86a1 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 21:45:23 +0700 Subject: [PATCH 07/14] Remove old statfs addition --- src/library_nodefs.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/library_nodefs.js b/src/library_nodefs.js index 3418244092be..3480add03ead 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -117,10 +117,6 @@ addToLibrary({ } return newFlags; }, - - statfs: function(path) { - return fs.statfsSync(path); - }, node_ops: { getattr(node) { var path = NODEFS.realPath(node); From e7569a81650a29be75fd73ab737c8272a46319d8 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 21:45:49 +0700 Subject: [PATCH 08/14] Add newline --- src/library_nodefs.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/library_nodefs.js b/src/library_nodefs.js index 3480add03ead..fbd0faf698cd 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -117,6 +117,7 @@ addToLibrary({ } return newFlags; }, + node_ops: { getattr(node) { var path = NODEFS.realPath(node); From 46de52e0bd1f55f3c80406deb4437811d636f03f Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Thu, 26 Sep 2024 22:05:31 +0700 Subject: [PATCH 09/14] Fix free inodes value and update test --- src/library_fs.js | 4 ++-- test/core/test_statvfs.c | 4 ++-- test/core/test_statvfs.out | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index 3abd77e9b88c..bcbd448f7629 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -676,6 +676,7 @@ FS.staticInit(); // NOTE: None of the defaults here are true. We're just returning safe and // sane values. + var ffree = FS.nextInode - 1; // Free inodes can not be larger than total inodes var defaults = { bsize: 4096, frsize: 4096, @@ -683,8 +684,7 @@ FS.staticInit(); bfree: 5e5, bavail: 5e5, files: FS.nextInode, - // Free inodes can not be larger than total inodes so calculate a reasonable value here - ffree: () => Math.max(0, Math.floor(this.files * 0.9) - this.files), + ffree, fsid: 42, flags: 2, namelen: 255, diff --git a/test/core/test_statvfs.c b/test/core/test_statvfs.c index 2b310f85e846..7b1baddd8aa9 100644 --- a/test/core/test_statvfs.c +++ b/test/core/test_statvfs.c @@ -24,8 +24,8 @@ int main() { printf("f_bfree: %u\n", s.f_bfree); printf("f_bavail: %u\n", s.f_bavail); printf("f_files: %d\n", s.f_files > 5); - printf("f_ffree: %u\n", s.f_ffree); - printf("f_favail: %u\n", s.f_favail); + printf("f_ffree: %u\n", s.f_ffree <= s.f_files && s.f_ffree > 0); + printf("f_favail: %u\n", s.f_favail <= s.f_files && s.f_favail > 0); printf("f_fsid: %lu\n", s.f_fsid); printf("f_flag: %lu\n", s.f_flag); printf("f_namemax: %lu\n", s.f_namemax); diff --git a/test/core/test_statvfs.out b/test/core/test_statvfs.out index 8c414d0949dd..169df0a92be7 100644 --- a/test/core/test_statvfs.out +++ b/test/core/test_statvfs.out @@ -6,8 +6,8 @@ f_blocks: 1000000 f_bfree: 500000 f_bavail: 500000 f_files: 1 -f_ffree: 1000000 -f_favail: 1000000 +f_ffree: 1 +f_favail: 1 f_fsid: 42 f_flag: 2 f_namemax: 255 From d96eb8aafb26d6b124c9572b2826c9dce381b526 Mon Sep 17 00:00:00 2001 From: Jeroen P Date: Thu, 26 Sep 2024 22:13:56 +0700 Subject: [PATCH 10/14] Update test/fs/test_nodefs_statfs.c Co-authored-by: Carlos Garcia --- test/fs/test_nodefs_statfs.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/fs/test_nodefs_statfs.c b/test/fs/test_nodefs_statfs.c index 956d3bd18b8f..fa416a0d372a 100644 --- a/test/fs/test_nodefs_statfs.c +++ b/test/fs/test_nodefs_statfs.c @@ -10,8 +10,6 @@ void test_statfs(const char *path) { assert(result == 0 && "statvfs should succeed"); - - // Basic sanity checks assert(st.f_bsize > 0 && "Block size should be positive"); assert(st.f_blocks > 0 && "Total blocks should be positive"); From a9d68cdb2a55f8582ab693be527a3167a7a6e303 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Fri, 27 Sep 2024 12:58:37 +0700 Subject: [PATCH 11/14] refactor statfs --- src/library_fs.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index bcbd448f7629..b8a28d8affa9 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -676,26 +676,24 @@ FS.staticInit(); // NOTE: None of the defaults here are true. We're just returning safe and // sane values. - var ffree = FS.nextInode - 1; // Free inodes can not be larger than total inodes - var defaults = { + var rtn = { bsize: 4096, frsize: 4096, blocks: 1e6, bfree: 5e5, bavail: 5e5, files: FS.nextInode, - ffree, + ffree: FS.nextInode - 1, fsid: 42, flags: 2, namelen: 255, }; var parent = FS.lookupPath(path, {follow: true}).node; - if (typeof parent.node_ops?.statfs !== 'function') { - return defaults; + if (typeof parent.node_ops?.statfs) { + Object.assign(rtn, parent.node_ops.statfs(parent.mount.opts.root)); } - - return { ...defaults, ...parent.node_ops.statfs(parent.mount.opts.root) }; + return rtn; }, // helpers to create specific types of nodes create(path, mode) { From df511ea10818161aa8927613b35eeb7cb460e49c Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Fri, 27 Sep 2024 13:03:38 +0700 Subject: [PATCH 12/14] Fix check for statfs on nodeops --- src/library_fs.js | 2 +- test/fs/test_nodefs_statfs.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index b8a28d8affa9..468000d58b2b 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -690,7 +690,7 @@ FS.staticInit(); }; var parent = FS.lookupPath(path, {follow: true}).node; - if (typeof parent.node_ops?.statfs) { + if (parent.node_ops.statfs) { Object.assign(rtn, parent.node_ops.statfs(parent.mount.opts.root)); } return rtn; diff --git a/test/fs/test_nodefs_statfs.c b/test/fs/test_nodefs_statfs.c index fa416a0d372a..84ea269da85f 100644 --- a/test/fs/test_nodefs_statfs.c +++ b/test/fs/test_nodefs_statfs.c @@ -19,16 +19,19 @@ void test_statfs(const char *path) { assert(st.f_ffree <= st.f_files && "Free inodes should not exceed total inodes"); } +void setup() { + EM_ASM( + FS.mkdir('/working'); + FS.mount(NODEFS, { root: '.' }, '/working'); + ); +} + int main() { // Test the root filesystem (which should be MEMFS by default) - test_statfs("/"); - // Mount NODEFS and test it - EM_ASM( - FS.mkdir('/working'); - FS.mount(NODEFS, { root: '.' }, '/working'); - ); + test_statfs("/"); + setup(); test_statfs("/working"); puts("success"); From 7601bd8833372a949a4d71e2a7cf019d99606c27 Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Fri, 27 Sep 2024 13:13:09 +0700 Subject: [PATCH 13/14] Rename statfs to statvfs and add a comment to explain assigning bsize to frsize --- src/library_nodefs.js | 8 ++++---- .../{test_nodefs_statfs.c => test_nodefs_statvfs.c} | 11 ++++------- test/test_core.py | 9 +++++---- 3 files changed, 13 insertions(+), 15 deletions(-) rename test/fs/{test_nodefs_statfs.c => test_nodefs_statvfs.c} (91%) diff --git a/src/library_nodefs.js b/src/library_nodefs.js index fbd0faf698cd..a9b91f8d37a9 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -216,10 +216,10 @@ addToLibrary({ }, statfs(path) { var stats = NODEFS.tryFSOperation(() => fs.statfsSync(path)); - return { - ...stats, - frsize: stats.bsize - }; + // Node.js doesn't provide frsize (fragment size). Set it to bsize (block size) + // as they're often the same in many file systems. May not be accurate for all. + stats.frsize = stats.bsize; + return stats; } }, stream_ops: { diff --git a/test/fs/test_nodefs_statfs.c b/test/fs/test_nodefs_statvfs.c similarity index 91% rename from test/fs/test_nodefs_statfs.c rename to test/fs/test_nodefs_statvfs.c index 84ea269da85f..c1f3ba0d846e 100644 --- a/test/fs/test_nodefs_statfs.c +++ b/test/fs/test_nodefs_statvfs.c @@ -3,7 +3,7 @@ #include #include -void test_statfs(const char *path) { +void test_statvfs(const char *path) { printf("Testing statfs for path: %s\n", path); struct statvfs st; int result = statvfs(path, &st); @@ -27,12 +27,9 @@ void setup() { } int main() { - // Test the root filesystem (which should be MEMFS by default) - - test_statfs("/"); - setup(); - test_statfs("/working"); - + // Test the root filesystem (which should be MEMFS by default) + test_statvfs("/"); + test_statvfs("/working"); puts("success"); } diff --git a/test/test_core.py b/test/test_core.py index 907ba01790bc..7a7c47f3d836 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5775,18 +5775,19 @@ def test_fs_nodefs_readdir(self): # externally setup an existing folder structure: existing/a if self.get_setting('WASMFS'): self.set_setting('FORCE_FILESYSTEM') - os.makedirs(os.path.join(self.working_dir, 'existing', 'a')) + os.makedirs('existing/a') self.emcc_args += ['-lnodefs.js'] self.do_runf('fs/test_nodefs_readdir.c', 'success') @requires_node - def test_fs_nodefs_statfs(self): + @crossplatform + def test_fs_nodefs_statvfs(self): # externally setup an existing folder structure: existing/a if self.get_setting('WASMFS'): self.set_setting('FORCE_FILESYSTEM') - os.makedirs(os.path.join(self.working_dir, 'existing', 'a')) + os.makedirs('existing/a') self.emcc_args += ['-lnodefs.js'] - self.do_runf('fs/test_nodefs_statfs.c', 'success') + self.do_runf('fs/test_nodefs_statvfs.c', 'success') @no_windows('no symlink support on windows') @requires_node From 3a5a16729579ebb344f802a123f3372606005bdc Mon Sep 17 00:00:00 2001 From: Jeroen Pfeil Date: Mon, 30 Sep 2024 11:35:36 +0700 Subject: [PATCH 14/14] Fix test and use 2-space indentation --- test/fs/test_nodefs_statvfs.c | 40 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test/fs/test_nodefs_statvfs.c b/test/fs/test_nodefs_statvfs.c index c1f3ba0d846e..b455affb6749 100644 --- a/test/fs/test_nodefs_statvfs.c +++ b/test/fs/test_nodefs_statvfs.c @@ -4,32 +4,32 @@ #include void test_statvfs(const char *path) { - printf("Testing statfs for path: %s\n", path); - struct statvfs st; - int result = statvfs(path, &st); + printf("Testing statfs for path: %s\n", path); + struct statvfs st; + int result = statvfs(path, &st); - assert(result == 0 && "statvfs should succeed"); + assert(result == 0 && "statvfs should succeed"); - // Basic sanity checks - assert(st.f_bsize > 0 && "Block size should be positive"); - assert(st.f_blocks > 0 && "Total blocks should be positive"); - assert(st.f_bfree <= st.f_blocks && "Free blocks should not exceed total blocks"); - assert(st.f_bavail <= st.f_bfree && "Available blocks should not exceed free blocks"); - assert(st.f_files > 0 && "Total inodes should be positive"); - assert(st.f_ffree <= st.f_files && "Free inodes should not exceed total inodes"); + // Basic sanity checks + assert(st.f_bsize > 0 && "Block size should be positive"); + assert(st.f_blocks > 0 && "Total blocks should be positive"); + assert(st.f_bfree <= st.f_blocks && "Free blocks should not exceed total blocks"); + assert(st.f_bavail <= st.f_bfree && "Available blocks should not exceed free blocks"); + assert(st.f_files >= 0 && "Total inodes should be 0 or positive"); + assert(st.f_ffree <= st.f_files && "Free inodes should not exceed total inodes"); } void setup() { - EM_ASM( - FS.mkdir('/working'); - FS.mount(NODEFS, { root: '.' }, '/working'); - ); + EM_ASM( + FS.mkdir('/working'); + FS.mount(NODEFS, { root: '.' }, '/working'); + ); } int main() { - setup(); - // Test the root filesystem (which should be MEMFS by default) - test_statvfs("/"); - test_statvfs("/working"); - puts("success"); + setup(); + // Test the root filesystem (which should be MEMFS by default) + test_statvfs("/"); + test_statvfs("/working"); + puts("success"); }