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

Use the NODEFS filesystem in statfs when available #23912

Closed
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
13 changes: 13 additions & 0 deletions test/setup_nodefs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const statfsSync = require('fs').statfsSync;

const DEFAULT_BLOCKS = 1e6;

Module['preRun'] = () => {
try {
const statfs = statfsSync('/');
ENV.EXPECTED_BLOCKS = statfs.blocks.toString();
} catch (e) {
// Older versions of Node don't support statfsSync
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only needed for the one test I would just do this in fstatfs.c itself. You can just do it in an EM_ASM or EM_JS block on startup?


Module['onRuntimeInitialized'] = () => {
out('mounting node filesystem under /nodefs');
FS.mkdir('/nodefs');
Expand Down
40 changes: 37 additions & 3 deletions test/unistd/fstatfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,53 @@
#include <sys/vfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h> // for strerror
#include <errno.h> // for errno#include <filesystem>

#define DEFAULT_BLOCKS 1000000

int main() {
struct statfs buf;
const char *expected_blocks_str = getenv("EXPECTED_BLOCKS");
long expected_blocks = expected_blocks_str ? atol(expected_blocks_str) : DEFAULT_BLOCKS;
int fstatfs_rtn;

#if NODEF || NODERAWFSS
fstatfs_rtn = statfs("/nodefs", &buf);
printf("f_type: %ld\n", buf.f_type);
assert(fstatfs_rtn == 0);
printf("f_blocks: %d\n", buf.f_blocks);
printf("expected_blocks: %ld\n", expected_blocks);
assert(buf.f_blocks == expected_blocks);
#endif

int rtn;
assert(fstatfs(STDOUT_FILENO, &buf) == 0);
fstatfs_rtn = fstatfs(STDOUT_FILENO, &buf);
printf("f_type: %ld\n", buf.f_type);
assert(fstatfs_rtn == 0);
printf("f_blocks: %d\n", buf.f_blocks);
assert(buf.f_blocks == DEFAULT_BLOCKS);

int f = open("file", O_RDWR | O_CREAT);
assert(fstatfs(f, &buf) == 0);
fstatfs_rtn = fstatfs(f, &buf);
printf("f_type: %ld\n", buf.f_type);
printf("f_blocks: %d\n", buf.f_blocks);
#if NODEFS || NODERAWFS
assert(fstatfs_rtn == 0);
assert(buf.f_blocks != 0);
#else
assert(fstatfs_rtn == 0);
assert(buf.f_blocks == DEFAULT_BLOCKS);
#endif

assert(statfs("file", &buf) == 0);
printf("f_type: %ld\n", buf.f_type);
printf("f_blocks: %d\n", buf.f_blocks);
#if NODEFS || NODERAWFS
assert(buf.f_blocks != 0);
#else
assert(buf.f_blocks == DEFAULT_BLOCKS);
#endif

return 0;
}