Repository navigation
WasmFS JS API: Implement mount and unmount #19825
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
Changes from all commits
0710775
5b79521
7cbbcbb
56478ad
82cede3
e23e1ad
069f629
95cfd17
a7aeef1
830a150
4c5224c
2a13102
0997caf
9741d47
3ee5bd2
5e3661c
2c862c0
87412b5
6cf9a9b
d6e3168
bedaeaf
9f974c2
babf15a
5c89b3f
650e784
2b6b01c
8616a70
275afdf
2649933
8a950aa
dc262e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 The Emscripten Authors | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $FETCHFS__deps: ['$stringToUTF8OnStack', 'wasmfs_create_fetch_backend'], | ||
| $FETCHFS: { | ||
| createBackend(opts) { | ||
| return _wasmfs_create_fetch_backend(stringToUTF8OnStack(opts.base_url)); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (!WASMFS) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these checks be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did consider using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ok as normal JS. We use the preprocessor to filter what code is emitted in the output JS, but this code is not in the output JS - unlike say |
||
| error("using -lfetchfs.js requires using WasmFS (-sWASMFS)"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 The Emscripten Authors | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $ICASEFS__deps: ['wasmfs_create_icase_backend'], | ||
| $ICASEFS: { | ||
| createBackend(opts) { | ||
| if (typeof opts.backend === "undefined") { | ||
| throw new Error("Underlying backend is not valid."); | ||
| } | ||
| var underlyingBackend = opts.backend.createBackend(opts); | ||
| return _wasmfs_create_icase_backend(underlyingBackend); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (!WASMFS) { | ||
| error("using -licasefs.js requires using WasmFS (-sWASMFS)"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 The Emscripten Authors | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $JSFILEFS__deps: ['wasmfs_create_js_file_backend'], | ||
| $JSFILEFS: { | ||
| createBackend(opts) { | ||
| return _wasmfs_create_js_file_backend(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (!WASMFS) { | ||
| error("using -ljsfile.js requires using WasmFS (-sWASMFS)"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 The Emscripten Authors | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $OPFS__deps: ['wasmfs_create_opfs_backend'], | ||
| $OPFS: { | ||
| createBackend(opts) { | ||
| return _wasmfs_create_opfs_backend(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| if (!WASMFS) { | ||
| error("using -lopfs.js requires using WasmFS (-sWASMFS)"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,12 @@ | |
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $MEMFS__deps: ['wasmfs_create_memory_backend'], | ||
| $MEMFS: { | ||
| createBackend(opts) { | ||
| return _wasmfs_create_memory_backend(); | ||
| } | ||
| }, | ||
| $wasmFSPreloadedFiles: [], | ||
| $wasmFSPreloadedDirs: [], | ||
| // We must note when preloading has been "flushed", that is, the time at which | ||
|
|
@@ -18,6 +24,7 @@ FS.init(); | |
| FS.createPreloadedFile = FS_createPreloadedFile; | ||
| `, | ||
| $FS__deps: [ | ||
| '$MEMFS', | ||
| '$wasmFSPreloadedFiles', | ||
| '$wasmFSPreloadedDirs', | ||
| '$wasmFSPreloadingFlushed', | ||
|
|
@@ -35,6 +42,21 @@ FS.createPreloadedFile = FS_createPreloadedFile; | |
| // up requiring all of our code | ||
| // here. | ||
| '$FS_modeStringToFlags', | ||
| #if LibraryManager.has('library_icasefs.js') | ||
| '$ICASEFS', | ||
| #endif | ||
| #if LibraryManager.has('library_nodefs.js') | ||
| '$NODEFS', | ||
| #endif | ||
| #if LibraryManager.has('library_opfs.js') | ||
| '$OPFS', | ||
| #endif | ||
| #if LibraryManager.has('library_jsfilefs.js') | ||
| '$JSFILEFS', | ||
| #endif | ||
| #if LibraryManager.has('library_fetchfs.js') | ||
| '$FETCHFS', | ||
| #endif | ||
| 'malloc', | ||
| 'free', | ||
| #endif | ||
|
|
@@ -349,8 +371,20 @@ FS.createPreloadedFile = FS_createPreloadedFile; | |
| __wasmfs_readdir_finish(state); | ||
| return entries; | ||
| }), | ||
| // TODO: mount | ||
| // TODO: unmount | ||
| mount: (type, opts, mountpoint) => { | ||
| #if ASSERTIONS | ||
| if (typeof type == 'string') { | ||
| // The filesystem was not included, and instead we have an error | ||
| // message stored in the variable. | ||
| throw type; | ||
|
Comment on lines
+377
to
+379
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the error message stored in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| } | ||
| #endif | ||
| var backendPointer = type.createBackend(opts); | ||
| return FS.handleError(withStackSave(() => __wasmfs_mount(stringToUTF8OnStack(mountpoint), backendPointer))); | ||
| }, | ||
| unmount: (mountpoint) => ( | ||
| FS.handleError(withStackSave(() => __wasmfs_unmount(stringToUTF8OnStack(mountpoint)))) | ||
|
kripken marked this conversation as resolved.
Resolved
|
||
| ), | ||
| // TODO: lookup | ||
| mknod(path, mode, dev) { | ||
| return FS.handleError(withStackSave(() => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,16 @@ using namespace wasmfs; | |
|
|
||
| extern "C" { | ||
|
|
||
| // TODO: Replace forward declarations with #include <emscripten/wasmfs.h> and | ||
| // resolve wasmfs::backend_t namespace conflicts. | ||
| __wasi_fd_t wasmfs_create_file(char* pathname, mode_t mode, backend_t backend); | ||
| int wasmfs_create_directory(char* path, int mode, backend_t backend); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably include emscripten/wasmfs.h rather than forward-declaring functions here. Including the header will make sure that the function definitions and function users are always working with the same signature. Having this standalone forward declarations runs the risk of the signatures getting out of sync. We can add
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Directly using It seems like
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, adding the namespace in emscripten/wasmfs.h would fix this. Unfortunately emscripten/wasmfs.h also has to work as a plain C header, though, so it's not entirely straightforward. You would have to insert But for this PR, keeping the forward declarations in this file to avoid all these issues seems fine as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried using the ifdef as shown below However, it still looks like I'm getting an error with Am I using the preprocessor incorrectly? Would it make more sense to use something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was able to solve the problem by wrapping |
||
| int wasmfs_unmount(intptr_t path); | ||
|
|
||
| // Copy the file specified by the pathname into JS. | ||
| // Return a pointer to the JS buffer in HEAPU8. | ||
| // The buffer will also contain the file length. | ||
| // TODO: Use WasmFS ErrnoError handling instead of aborting on failure. | ||
| void* _wasmfs_read_file(char* path) { | ||
| static_assert(sizeof(off_t) == 8, "File offset type must be 64-bit"); | ||
|
|
||
|
|
@@ -207,7 +212,7 @@ int _wasmfs_llseek(int fd, off_t offset, int whence) { | |
|
|
||
| int _wasmfs_rename(char* oldpath, char* newpath) { | ||
| return __syscall_renameat(AT_FDCWD, (intptr_t)oldpath, AT_FDCWD, (intptr_t)newpath); | ||
| }; | ||
| } | ||
|
|
||
| int _wasmfs_read(int fd, void *buf, size_t count) { | ||
| __wasi_iovec_t iovs[1]; | ||
|
|
@@ -265,6 +270,24 @@ int _wasmfs_lstat(char* path, struct stat* statBuf) { | |
| return __syscall_lstat64((intptr_t)path, (intptr_t)statBuf); | ||
| } | ||
|
|
||
| // The legacy JS API requires a mountpoint to already exist, so WasmFS will attempt to remove | ||
| // the target directory if it exists before replacing it with a mounted directory. | ||
| int _wasmfs_mount(char* path, wasmfs::backend_t created_backend) { | ||
| int err = __syscall_rmdir((intptr_t)path); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add a comment explaining that we do this for compatibility with the legacy JS API, because otherwise this seems like nonsense. |
||
|
|
||
| // The legacy JS API mount requires the directory to already exist, but we will also allow it to be missing. | ||
| if (err && err != -ENOENT) { | ||
| return err; | ||
| } | ||
|
|
||
| return wasmfs_create_directory(path, 0777, created_backend); | ||
| } | ||
|
|
||
| // WasmFS will always remove the mounted directory, regardless of if the directory existed before. | ||
| int _wasmfs_unmount(char* path) { | ||
| return wasmfs_unmount((intptr_t)path); | ||
| } | ||
|
|
||
| // Helper method that identifies what a path is: | ||
| // ENOENT - if nothing exists there | ||
| // EISDIR - if it is a directory | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just put
wasmfs.test_fs_*here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all the
test_fs_*tests are passing yet, since some are failing because the functions haven't been implemented yet.