Sign in Join
Wiki copytemporary

WebAssembly/Files: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: WebAssembly/Files

OldNewLine
1+The WASI target supports filesystem access and this is implemented in the Free Pascal RTL. However, there are some caveats, due to the way WASI is designed.
2+
3+## File access using wasmtime
4+
5+By default wasmtime disallows any file access, only standard input and output. This is because WebAssembly can be used for sandboxing and running untrusted code. To enable access to a directory, you need to run your program like that:
6+
7+`wasmtime run --dir `*`<DIRECTORY>`*` program.wasm`
8+
9+You can also specify the way the directory is visible inside the program, like this:
10+
11+`wasmtime run --mapdir `*`<GUEST_DIR::HOST_DIR>`*` program.wasm`
12+
13+For example, if you want */home/blabla/tralala* on your host system to be visible as the root directory in your WebAssembly program, you can do:
14+
15+` wasmtime run --mapdir /::/home/blabla/tralala program.wasm`
16+
17+## Caveats
18+
19+### Current directory
20+
21+There is no WASI API function to obtain or change the current directory. All the Free Pascal RTL functions for dealing with the current directory are actually emulated inside the RTL. However, there's no way to specify the initial directory, so, for example, if you run your program like that:
22+
23+`wasmtime run --dir / program.wasm`
24+
25+To grant it access to the root directory and therefore to the entire filesystem, you would expect that your program would also inherit the current directory, however that's not the case. Your program will assume the current directory is /.
26+
27+### Relative paths
28+
29+If you give access to a directory, using a relative path to wasmtime, e.g.:
30+
31+`wasmtime run --dir . program.wasm`
32+
33+Then the full path will not be known to your program. It will simply see the current directory as ".". Some programs might not expect this, so it's better to use --mapdir in this case, e.g.:
34+
35+`wasmtime run --mapdir /::. program.wasm`
36+
37+This will make the current directory to be visible as / in your program.
38+
39+---
40+*From the Free Pascal wiki, page "WebAssembly/Files", as it stood on 2022-06-21 after a change by Nickysn, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
41+Category: WebAssembly

Back to history