Skip to content

Commit 6c5e4cb

Browse files
committed
[WasmFS] Create public wasmfs_mount function
We already had `wasmfs_unmount` so it seems logical we should have this one too. This is designed to replace `wasmfs_create_directory` which has a rather misleading name. I have left `wasmfs_create_directory` around for now but is simply combination of `mkdir` + `mount` (which is essentially what is was doing already).
1 parent 8af2200 commit 6c5e4cb

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

system/include/emscripten/wasmfs.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ backend_t wasmfs_get_backend_by_fd(int fd);
2929
// TODO: Remove this function so that only directories can be mounted.
3030
int wasmfs_create_file(const char* pathname __attribute__((nonnull)), mode_t mode, backend_t backend);
3131

32-
// Creates a new directory using a specific backend.
33-
// Returns 0 on success like `mkdir`, or a negative value on error.
34-
// TODO: Add an alias with wasmfs_mount.
32+
// Legacy function. This function works like `mkdir` + `wasmfs_mount`.
3533
int wasmfs_create_directory(const char* path __attribute__((nonnull)), mode_t mode, backend_t backend);
3634

35+
// Mount a backend at a given location in the filesystem
36+
// `target` must be a valid existing directory.
37+
int wasmfs_mount(const char* path __attribute__((nonnull)), backend_t backend);
38+
3739
// Unmounts the directory (Which must be a valid mountpoint) at a specific path.
3840
// Returns 0 on success, or a negative value on error.
3941
int wasmfs_unmount(const char* path __attribute__((nonnull)));

system/lib/wasmfs/syscalls.cpp

+42-26
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,7 @@ int __syscall_mknodat(int dirfd, intptr_t path, int mode, int dev) {
597597
OpenReturnMode::Nothing);
598598
}
599599

600-
static int
601-
doMkdir(path::ParsedParent parsed, int mode, backend_t backend = NullBackend) {
600+
static int doMkdir(path::ParsedParent parsed, int mode) {
602601
if (auto err = parsed.getError()) {
603602
return err;
604603
}
@@ -624,41 +623,58 @@ doMkdir(path::ParsedParent parsed, int mode, backend_t backend = NullBackend) {
624623
return -EACCES;
625624
}
626625

627-
// By default, the backend that the directory is created in is the same as
628-
// the parent directory. However, if a backend is passed as a parameter,
629-
// then that backend is used.
630-
if (!backend) {
631-
backend = parent->getBackend();
626+
if (!lockedParent.insertDirectory(childName, mode)) {
627+
// TODO Receive a specific error code, and report it here. For now, report
628+
// a generic error.
629+
return -EIO;
632630
}
633631

634-
if (backend == parent->getBackend()) {
635-
if (!lockedParent.insertDirectory(childName, mode)) {
636-
// TODO Receive a specific error code, and report it here. For now, report
637-
// a generic error.
638-
return -EIO;
639-
}
640-
} else {
641-
auto created = backend->createDirectory(mode);
642-
if (!created) {
643-
// TODO Receive a specific error code, and report it here. For now, report
644-
// a generic error.
645-
return -EIO;
646-
}
647-
[[maybe_unused]] bool mounted = lockedParent.mountChild(childName, created);
648-
assert(mounted);
632+
// TODO: Check that the insertion is successful.
633+
634+
return 0;
635+
}
636+
637+
int wasmfs_mount(backend_t backend, const char* path) {
638+
path::ParsedParent parsed = path::parseParent(path);
639+
if (auto err = parsed.getError()) {
640+
return err;
649641
}
642+
auto& [parent, childNameView] = parsed.getParentChild();
643+
auto lockedParent = parent->locked();
644+
std::string childName(childNameView);
650645

651-
// TODO: Check that the insertion is successful.
646+
// Child must exist and must be directory
647+
auto child = lockedParent.getChild(childName);
648+
if (!child) {
649+
return -EEXIST;
650+
}
651+
if (!child->dynCast<Directory>()) {
652+
return -ENOTDIR;
653+
}
654+
655+
auto created = backend->createDirectory(0777);
656+
if (!created) {
657+
// TODO Receive a specific error code, and report it here. For now, report
658+
// a generic error.
659+
return -EIO;
660+
}
661+
[[maybe_unused]] bool mounted = lockedParent.mountChild(childName, created);
662+
assert(mounted);
652663

653664
return 0;
654665
}
655666

656667
// This function is exposed to users and allows users to specify a particular
657668
// backend that a directory should be created within.
658-
int wasmfs_create_directory(char* path, int mode, backend_t backend) {
659-
static_assert(std::is_same_v<decltype(doMkdir(0, 0, 0)), int>,
669+
int wasmfs_create_directory(const char* path, int mode, backend_t backend) {
670+
static_assert(std::is_same_v<decltype(doMkdir(0, 0)), int>,
660671
"unexpected conversion from result of doMkdir to int");
661-
return doMkdir(path::parseParent(path), mode, backend);
672+
int rtn = doMkdir(path::parseParent(path), mode);
673+
if (rtn != 0) {
674+
return rtn;
675+
}
676+
677+
return wasmfs_mount(backend, path);
662678
}
663679

664680
// TODO: Test this.

0 commit comments

Comments
 (0)