Is it possible to create a file in a directory? (webassembly)

wasm files have limited access to the file system.

I can create a file.
fs::write("test.txt", "context").expect("error");

But I can't create a file in the directory.
fs::write("myDirectory/test.txt", "context").expect("error");

Also I can't create a directory.
fs::create_dir("newDirectory").expect("error");

I get an error - Unable to write file: Os { code: 76, kind: Uncategorized, message: "Capabilities insufficient" }

Is it possible to create a file in a directory?
I need to create many files. I want to group them in a separate directory

WASM by itself can't do anything, including anything related to files. No file operations at all. It all depends on the host that runs the web assembly.

So it depends how you're running it. If you're using an environment supporting WASI, then there are WASI functions for creating directories.

If you're running WASM in a browser, then you're at mercy of whatever browsers and JavaScript support, and standard filesystem access doesn't really exist there. There's a relatively new File System Access API, but it's more oriented towards loading and saving documents from user-selected folder than an equivalent of std::fs APIs.

3 Likes