Sign in Join
Wiki copytemporary

WebAssembly/Files: what changed

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

OldNewLine
11The 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.
22
33## File access using wasmtime
44
55By 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:
66
7-`wasmtime run --dir `*`<DIRECTORY>`*` program.wasm`
7+``` pascal
8+wasmtime run --dir <DIRECTORY> program.wasm
9+```
810
911You can also specify the way the directory is visible inside the program, like this:
1012
11-`wasmtime run --mapdir `*`<GUEST_DIR::HOST_DIR>`*` program.wasm`
13+``` pascal
14+wasmtime run --mapdir <GUEST_DIR::HOST_DIR> program.wasm
15+```
1216
1317For 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:
1418
15-` wasmtime run --mapdir /::/home/blabla/tralala program.wasm`
19+``` pascal
20+ wasmtime run --mapdir /::/home/blabla/tralala program.wasm
21+```
1622
1723## Caveats
1824
1925### Current directory
2026
2127There 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:
2228
23-`wasmtime run --dir / program.wasm`
29+``` pascal
30+wasmtime run --dir / program.wasm
31+```
2432
2533To 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 /.
2634
2735### Relative paths
2836
2937If you give access to a directory, using a relative path to wasmtime, e.g.:
3038
31-`wasmtime run --dir . program.wasm`
39+``` pascal
40+wasmtime run --dir . program.wasm
41+```
3242
3343Then 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.:
3444
35-`wasmtime run --mapdir /::. program.wasm`
45+``` pascal
46+wasmtime run --mapdir /::. program.wasm
47+```
3648
3749This will make the current directory to be visible as / in your program.
3850
3951---
4052*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).*
4153Category: WebAssembly

Back to history