Skip to content

WasmFS JS API: Implement mount and unmount - #19825

Merged
kripken merged 31 commits into
emscripten-core:mainfrom
jameshu15869:library-wasmfs-mount-acct
Aug 1, 2023
Merged

kripken merged 31 commits into
emscripten-core:mainfrom
jameshu15869:library-wasmfs-mount-acct

Conversation

@jameshu15869

@jameshu15869 jameshu15869 commented Jul 10, 2023 •

Copy link
Copy Markdown
Contributor

This PR implements FS.mount and FS.unmount. There were existing tests, so I just added some additional test cases to those files. Tests that now pass in WasmFS mode were also added to CI.

There are some differences in the behavior of the WasmFS mount and the legacy API. For example, WasmFS mount will create a new directory to mount to if one does not already exist. Existing directories must be nonempty, since WasmFS will unlink the directory before creating a new directory with the specified backend.

Currently, WasmFS does not support mounting IDBFS, WORKERFS, and PROXYFS, although this may be added in the future using a bridge to legacy backends (#18935)

commit 73033df
Author: jameshu111 <[email protected]>
Date:   Mon Jul 10 14:47:25 2023 -0400

    clean up

commit 7a68467
Author: jameshu111 <[email protected]>
Date:   Mon Jul 10 13:03:34 2023 -0400

    Squashed commit of the following:

    commit 844906b
    Author: jameshu111 <[email protected]>
    Date:   Mon Jul 10 10:47:15 2023 -0400

        clean up

    commit 6dc44f4
    Author: jameshu111 <[email protected]>
    Date:   Mon Jul 10 10:33:11 2023 -0400

        add virtual backends and cleanup

    commit 8d2ae99
    Author: jameshu111 <[email protected]>
    Date:   Mon Jul 10 09:28:44 2023 -0400

        pass tests

    commit bb50c62
    Author: jameshu111 <[email protected]>
    Date:   Fri Jul 7 16:26:31 2023 -0400

        working split design

    commit 74eadbe
    Author: jameshu111 <[email protected]>
    Date:   Fri Jul 7 15:29:14 2023 -0400

        passing mount tests with first implementation

    commit f63c39c
    Merge: 66a4fa5 da84fe7
    Author: jameshu111 <[email protected]>
    Date:   Fri Jul 7 09:17:41 2023 -0400

        Merge branch 'main' of https://github.com/jameshu15869/emscripten

    commit 66a4fa5
    Author: jameshu111 <[email protected]>
    Date:   Fri Jul 7 09:17:39 2023 -0400

        before sync

    commit f0a6a24
    Merge: 019bb29 9e66122
    Author: jameshu111 <[email protected]>
    Date:   Wed Jun 28 15:57:04 2023 -0400

        Merge branch 'main' of https://github.com/jameshu15869/emscripten

    commit 019bb29
    Author: jameshu111 <[email protected]>
    Date:   Wed Jun 28 15:56:28 2023 -0400

        temp
Comment thread .circleci/config.yml
wasmfs.test_fs_nodefs_cloexec_rawfs
wasmfs.test_fs_errorstack
wasmfs.test_fs_errorstack_rawfs
wasmfs.test_fs_emptyPath

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/library_wasmfs.js Resolved
Comment thread src/library_wasmfs.js Outdated
$MEMFS: {
createBackend: (opts) => (_wasmfs_create_memory_backend())
},
$ICASE: {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we give all the these globals names with FS in them.. how about ICASEFS, and FETCHFS?

Also, so we need to even other with creating these globals for non-legacy filesystems? Maybe can skip it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with ICASEFS, etc.

I think having the globals for the non-legacy filesystems like NODEFS made the implementation a little easier, since I could match the legacy JS API of FS.mount(NODEFS) (passing an object), then give the object a createBackend() method to make the correct backend to pass to wasmfs_mount. Would it be better to not use globals for each FS and try a different approach?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess I was thinking the JS API only needs to support enough for compatibility with the legacy FS.

Maybe for new filesystems there doesn't need to be an API for mounting from JS at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see, would this mean that the new JS API won't support using the new filesystems from JS at all? i.e. the new backends can only be used in C/C++?

@jameshu15869

Copy link
Copy Markdown
Contributor Author

It seems like I'm getting a closure compiler error for browser.test_modularize_and_preload_files_wasmfs. The error message is below.

building:ERROR: Closure compiler run failed:

building:ERROR: C:/Users/james/AppData/Local/Temp/emscripten_temp_iwqbpr6a/a.out.js.mem.jso1.js:1678:3: ERROR - [JSC_UNDEFINED_VARIABLE] variable _emscripten_proxy_execute_queue is undeclared
  1678|    _emscripten_proxy_execute_queue(queue);
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 error(s), 0 warning(s)

While I see emscripten_proxy_execute_queue(queue); in library_wasmfs.js, I don't think any of the code I changed should affect this. @tlively Could this be related to why backend constructors were passed in to the virtual backends instead of actual pointers (Since I'm guessing the issue is related to proxying)?

Comment thread emcc.py Outdated
'wasmfs_create_opfs_backend',
'wasmfs_create_js_file_backend',
'wasmfs_create_fetch_backend',
'wasmfs_create_icase_backend_from_pointer',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I actually hope we can avoid including all the backends like this. We should be able to tell from things like -lnodefs.js that the user wants NODEFS, for example, which would imply wasmfs_create_node_backend.

Specifically, I think -lnodefs.js will include library_nodefs.js. That file, in WasmFS mode, can then have an extra dep here ..__deps: ['wasmfs_create_node_backend', ..]

@jameshu15869 jameshu15869 Jul 11, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should putting something in the __deps be expected to automatically import it?

I've tried a few different variations, such as __deps: ['wasmfs_create_node_backend'], __deps: ['_wasmfs_create_node_backend'], and __deps: ['$wasmfs_create_node_backend'], but none of them correctly imported the function unless I also added wasmfs_create_node_backend to REQUIRED_EXPORTS in emcc.py.

As a side note, it looks like the library_wasmfs_*.js files for the other backends are included on every execution. I added the globals to those files, but I'm wondering if it might be better to directly create library_opfs.js etc. to use with options like -lopfs.js and move the globals there. Basically, would that work as expected?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that adding native functions to __deps only currently works when the JS function is called/needed by that native code.

If you JS function in question is only called from JS, or included via DEFAULT_LIBRARY_FUNCS_TO_INCLUDE then this mechanism won't work. We should really fix that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. Is there a way for me to conditionally add the function to exports in emcc.py if -lnodefs.js is set on the command line?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found the bug we have open for this: #19759

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jameshu15869

Is there a way for me to conditionally add the function to exports in emcc.py if -lnodefs.js is set on the command line?

Technically that information is accessible:

diff --git a/emcc.py b/emcc.py
index 5e347e303..d577ebda4 100755
--- a/emcc.py
+++ b/emcc.py
@@ -4238,6 +4240,7 @@ def process_libraries(state, linker_inputs, embind_emit_tsd):
     new_flags.append((i, flag))
 
   settings.JS_LIBRARIES += libraries
+  print('debug', settings.JS_LIBRARIES)
 
   # At this point processing JS_LIBRARIES is finished, no more items will be added to it.
   # Sort the input list from (order, lib_name) pairs to a flat array in the right order.

But that is at the link stage, which is after the compile flags stuff that has the main WasmFS logic that sets up the required exports. So putting it here would be a little odd.

@sbc100 do you have a better idea for a workaround?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not really I'm afraid. I can work on #19759 so maybe we can just take the code size hit until that fix lands.

BTW, You even you put the dependencies here because they don't work in the __deps .. we can/should still add them to the __deps since its a good failsafe and it expresses the dependency in the correct place.

Comment thread src/library_nodefs.js Resolved
Comment thread src/library_wasmfs.js Outdated
#endif
'$OPFS',
'$JSFILEFS',
'$FETCHFS',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OPFS etc. can have the same check that NODEFS has, to not include them unnecessarily.

Also, I think these new deps can be behind #ifdef FORCE_FILESYSTEM as we don't need them otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with the #ifdef FORCE_FILESYSTEM. Since library_wasmfs_opfs.js is included for every WasmFS compilation, does it still make sense to use LibraryManager to check if library_wasmfs_opfs.js is present?

@kripken kripken Jul 12, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since library_wasmfs_opfs.js is included for every WasmFS compilation, does it still make sense to use LibraryManager to check if library_wasmfs_opfs.js is present?

Correct, that doesn't make sense.

I think the idea we were discussing when we met is that we'd check for a new file, library_opfs.jsto mirror library_nodefs.js etc. Those files are conditionally included, so checking for them makes sense. (Whereas the _wasmfs_ ones are always included since non-JS code might use them.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it. Should there be a check and/or warning if the user tries to link -lopfs.js when using the legacy API? If so, would that go somewhere in emcc.py?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good idea! Yeah, this can help users. Maybe library_opfs.js can end in

if (!WASMFS) {
  error("using -lopfs.js requires using WasmFS (-sWASMFS)");
}

@kripken

kripken commented Jul 11, 2023

Copy link
Copy Markdown
Member

About that closure error, it looks like a missing dependency. Does this tiny PR fix it for you? #19836

Comment thread src/library_wasmfs.js Outdated
Comment on lines +377 to +379
if (!type) {
throw new Error("FS is not valid.");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are so many other ways this could go wrong (like if someone passes a string as type) that I wonder whether it's worth trying to do any error checking here at all. Could we just let the JS runtime throw an error if type.createBackend(opts) doesn't work out?

Similar comment for the opts.backend check in the ICASEFS implementation, although there at least the common case would be for opts.backend to be undefined. (I think the current check might be intended to test that, but I don't think it does. You would want to do typeof opts.backend === "undefined" instead.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree. On a side note, one thing I noticed was that in the legacy JS API trying to pass NODEFS without -lnodefs.js would give a string message telling the user that -lnodefs.js should be used. However in WasmFS, I would directly get a NODEFS is not defined error. From what I can tell, parseTools.js should be responsible for finding the global uses and replacing them with the string message. Should WasmFS also use this behavior?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kripken do you have thoughts about that -lnodefs.js error message?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, those error messages are very useful for people, I think, since "X is not defined" doesn't point the user to the right fix. Hopefully it's a simple matter to get that working in parseTools?

Comment thread system/include/emscripten/wasmfs.h Resolved
extern "C" {

__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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 wasmfs_unmount to emscripten/wasmfs.h as well.

@jameshu15869 jameshu15869 Jul 12, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Directly using #include <emscripten/wasmfs.h> ends up giving me a few weird errors like

C:\Users\james\Documents\GitHub\emscripten\system\lib\wasmfs\js_api.cpp:266:31: error: reference to 'backend_t' is
      ambiguous
  266 | int _wasmfs_mount(char* path, backend_t created_backend) {
      |                               ^
C:\Users\james\Documents\GitHub\emscripten\cache\sysroot/include\emscripten/wasmfs.h:17:25: note: candidate found by
      name lookup is 'backend_t'
   17 | typedef struct Backend* backend_t;
      |                         ^
C:\Users\james\Documents\GitHub\emscripten\system\lib\wasmfs/file.h:35:7: note: candidate found by name lookup is
      'wasmfs::backend_t'
   35 | using backend_t = Backend*;
      |       ^
C:\Users\james\Documents\GitHub\emscripten\system\lib\wasmfs\js_api.cpp:292:10: error: no matching function for call to
      'wasmfs_create_directory'
  292 |   return wasmfs_create_directory(path, 0777, created_backend);
      |          ^~~~~~~~~~~~~~~~~~~~~~~
C:\Users\james\Documents\GitHub\emscripten\cache\sysroot/include\emscripten/wasmfs.h:33:5: note: candidate function not
      viable: no known conversion from 'backend_t' (aka 'wasmfs::Backend *') to 'backend_t' (aka 'Backend *') for 3rd
      argument
   33 | int wasmfs_create_directory(const char* path __attribute__((nonnull)), mode_t mode, backend_t backend);
      |     ^
2 errors generated..

It seems like backend_t is redefined in emscripten/wasmfs.h using typedef struct Backend* backend_t without a namespace, while there is using namespace wasmfs in js_api.cpp. Would it make sense to change all the usages of backend_t in emscripten/wasmfs.h to wasmfs::backend_t? I'm still new to C++ so this might not be the best approach.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 namespace wasmfs { ... } around the typedef only when using C++ and not when using C by using the preprocessor. You can see an example of this in emscripten/proxying.h.

But for this PR, keeping the forward declarations in this file to avoid all these issues seems fine as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've tried using the ifdef as shown below

#ifdef __cplusplus
namespace wasmfs {
#endif
typedef struct Backend* backend_t;
#ifdef __cplusplus
} // namespace wasmfs
#endif

However, it still looks like I'm getting an error with backend_t being ambiguous.

candidate function not
      viable: no known conversion from 'wasmfs::backend_t' (aka 'wasmfs::Backend *') to 'backend_t' (aka 'Backend *') for
      3rd argument
   35 | int wasmfs_create_directory(const char* path __attribute__((nonnull)), mode_t mode, backend_t backend);

Am I using the preprocessor incorrectly? Would it make more sense to use something like typedef wasmfs::backend_t backend_t inside the C++ ifdef, then typedef struct Backend* backend_t if C++ is not defined? Based on my understanding, doesn't the problem come from having one backend_t defined inside the namespace and one that isn't (Instead defined in wasmfs.h)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was able to solve the problem by wrapping namespace wasmfs {} around the entire section, although I'm not sure if this might cause any problems in the future.

}

int _wasmfs_mount(char* path, backend_t created_backend) {
int err = __syscall_rmdir((intptr_t)path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Comment thread system/lib/wasmfs/js_api.cpp Resolved
Comment thread system/lib/wasmfs/syscalls.cpp Resolved
Comment thread system/lib/wasmfs/syscalls.cpp Resolved
Comment thread system/lib/wasmfs/syscalls.cpp Outdated
Comment on lines +912 to +913
// Create a new directory to replace the old mount.
return wasmfs_create_directory((char*)path, 0777, parent->getBackend());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The basic unmount operation should not do this, although perhaps it needs to be done at a higher level for compatibility with the legacy JS API. If so, we should presumably only create this replacement directory if we are overwriting a directory in the first place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it might make sense to move the creation of the directory to js_api.cpp. Would there be a way to tell if the mountpoint was created by overwriting a directory? Or would it be simpler to directly create a new directory on each call to unmount?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whether a previous directory existed could be determined from the error code returned by the rmdir we do, and specifically we know there was a previous directory only if the rmdir call returns 0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the unlink operation in wasmfs_unmount might always think a previous directory existed, since the mounted child that we want to unmount will always exist. One idea I had was to add a variable indicating if a previous directory existed to each mounted directory at mount, since we can see the result of rmdir there, though I'm not sure this is the best approach. Do you have any suggestions for other ways to track whether or not a mount directory "covers" a previous directory?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh right, I was getting mount and unmount mixed up. Sorry about that. Yes, just unconditionally creating the replacement directory at the JS API level might make sense, if that matches the legacy API behavior.

Perhaps we should talk about whether there is a feasible path toward "fixing" this legacy behavior next time we meet because it really is extremely surprising IMO.

Comment thread test/fs/test_mount.c
}
assert(ex instanceof FS.ErrnoError && ex.errno === 44); // ENOENT
#if !defined(WASMFS)
// WasmFS readFile aborts on failure, instead throwing an ErrnoError.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this something we should fix in a follow-up PR? If so, it might be good to have a TODO here and maybe even file an issue about it if it's not going to be fixed soon.

Comment thread test/fs/test_mount.c
FS.unmount("/jsfile");

// test ICASE backend
FS.mount(ICASEFS, { backend: MEMFS }, "/icase");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the API you've designed here 👍

@sbc100

sbc100 commented Jul 12, 2023 •

Copy link
Copy Markdown
Collaborator

Now that #19843 as landed it should be possible to add native deps to library functions that are only included via DEFAULT_LIBRARY_FUNCS_TO_INCLUDE

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Amazing work here! This looks really promising.

Comment thread src/library_opfs.js Resolved
Comment thread src/library_wasmfs.js Resolved
Comment thread system/include/emscripten/wasmfs.h Outdated

#ifdef __cplusplus
namespace wasmfs {
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IIUC, as a result of this, C++ users need to write wasmfs::wasmfs_mount for example, which is redundant. Why do we need the namespace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The namespace issue came from trying to directly include #include <emscripten/wasmfs.h> in js_api.cpp instead of forward declaring methods like wasmfs_create_directory. Since emscripten/wasmfs.h also had to function as a normal C header, backend_t was redefined here. This meant using backend_t in js_api.cpp was ambiguous, since there was backend_t and wasmfs::backend_t. Based on #19825 (comment) I think it might be easier to use forward declarations for now and add a TODO.

Comment thread system/include/emscripten/wasmfs.h Outdated

// Unmounts the directory at a specific path.
// Returns 0 on success, or a negative value on error.
int wasmfs_unmount(int dirfd, intptr_t path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is slightly odd to have unmount but no mount, so at least a comment about that might make sense.

I wonder, though, if we have create_directory instead of mount, perhaps we could have remove_directory instead of unmount..? (With comments that they both do something like a mount/unmount if the backend differs from the parent dir perhaps.) Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it possible that adding remove_directory would add more confusion since we already have __syscall_rmdir and __syscall_unlinkat? I think the main reason we chose to add wasmfs_unmount here was because we were essentially removing the directory while handling a few special cases (Verifying that the child is actually a mountpoint, allowing unmounts/unlinks of nonempty directories, and not doing any path parsing).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In hindsight, I wish we had named wasmfs_create_directory wasmfs_mount instead (and relatedly we should get rid of wasmfs_create_file and only allow mounting directories.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this something that would be difficult to change? If so, would it be worth the effort to manually go through and change every usage?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The problem is that this is the public API for WasmFS, so simply renaming the existing function would break users. We can perhaps create an alias with the new name and mark the old version as deprecated and remove it after a transition period, but we should consider doing that as a separate PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we think we might add an alias wasmfs_mount later than leaving this as "unmount" for now sgtm. Maybe add a comment and a TODO though.

Comment thread system/lib/wasmfs/js_api.cpp Resolved
Comment thread test/fs/test_mount.c Resolved
Comment thread test/test_core.py Resolved
Comment thread test/wasmfs/get_backend.h Resolved
Comment thread src/library_fetchfs.js
},
});

if (!WASMFS) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should these checks be #if preprocessor checks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did consider using #if, but was a little unsure how to trigger the error if the library_fetchfs.js file actually got linked in. Does calling error() inside a top-level #if (As below) have the potential to cause any problems?

#if !WASMFS
error("using -lfetchfs is not currently supported in WasmFS."
#endif

@kripken kripken Jul 26, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 preamble.js which is in the output, this is a JS library file which is executed. So it's ok to use normal JS execution to check things like if (..) {..

Comment thread src/library_wasmfs.js
Comment on lines +377 to +379
// The filesystem was not included, and instead we have an error
// message stored in the variable.
throw type;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Where does the error message stored in type come from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think parseTools.js (Specifically makeRemovedFSAssert()) will scan through and try to find any usages of globals like NODEFS that are used when library_nodefs.js isn't linked in and convert the variable value to a string.

Comment thread system/include/emscripten/wasmfs.h Resolved
Comment thread system/include/emscripten/wasmfs.h Outdated
Comment on lines +101 to +103
#ifdef __cplusplus
} // namespace wasmfs
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets put this namespace only around the backend_t definition to avoid the complexity of having the functions be namespaced or not depending on the language. Now that I think about it, this conditional namespacing seems similarly complicated for the typedef as well...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it makes the most sense to use forward declarations for now and add a TODO based on #19825 (comment).

Comment thread system/lib/wasmfs/js_api.cpp Outdated
Comment on lines +26 to +28
// __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);
// int wasmfs_unmount(int dirfd, intptr_t path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the choice between including emscripten/wasmfs.h and dealing with conflicting backend_t typedefs or repeating the function declarations here? Maybe it would be better to repeate the declarations here and add a TODO about resolving the conflicting typedef and including emscripten/wasmfs.h instead. Sorry for the back-and-forth here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think trying to just wrap the typedef struct Backend* backend_t was causing some issues elsewhere where the functions themselves couldn't access backend_t without using wasmfs::backend_t, so I ended up trying to wrap the whole thing in namespace wasmfs { ... }, which caused a few other concerns in this PR. Going with forward declarations might be the simplest approach for this PR and I can try to go back and play with the namespacing in the future.

Comment thread system/lib/wasmfs/syscalls.cpp Outdated

// wasmfs_unmount is similar to __syscall_unlinkat, but assumes AT_REMOVEDIR is true
// and will only unlink mountpoints (Empty and nonempty).
int wasmfs_unmount(int dirfd, intptr_t path) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should either call this wasmfs_unmountat or remove the dirfd arg, which only appears in "at" versions of syscalls.

@jameshu15869 jameshu15869 Jul 26, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the dirfd arg might not be necessary, but I like wasmfs_unmountat to kind of differentiate the definition in syscalls.cpp from _wasmfs_unmount in js_api.cpp. Is it reasonable to continue using dirfd just for the sake of the name?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should probably figure out what functionality we want to provide and let the names follow from that. Also, making sure the public API makes sense on its own is much more important than considering how it makes sense in conjunction with the internal implementation; we can always rename internal things.

Given that none of the other functions here take a dirfd argument, I would probably lean toward removing it and keeping the unmount name.

Comment thread system/lib/wasmfs/js_api.cpp Resolved
Comment thread src/library_opfs.js Outdated
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2013 The Emscripten Authors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copyrights should all have the current year.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should this be all files that I modify or only files that I create in the current year?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only the year in which the file was first created. No need to update older years on files you modify.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great to me aside from the copyright years.

We have some open questions about the shape of the C API in the long-term, but for now I think this is good.

@tlively did you want to take another look?

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

Comment thread system/lib/wasmfs/js_api.cpp Resolved
@kripken
kripken merged commit 48e8a04 into emscripten-core:main Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants