Working with the WASI Filesystem
Learn how WASI exposes the host filesystem to a sandboxed module through preopened directories and capability-based file access.
Working with the WASI Filesystem is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Files Need Special Handling
A WASM module has no ambient authority — it cannot open any file by default. WASI grants file access only through preopened directories handed to the module at startup.
- No path traversal outside what was granted
- Access is capability-based, not permission-based
Preopened Directories
When a runtime launches a module it can map a host folder to a guest path. The module receives a file descriptor for each mapping.
Example with wasmtime: wasmtime --dir=. app.wasm grants the current directory.
wasmtime run --dir=./data app.wasmReading a File from WASI
In a WASI language SDK the standard library file APIs are wired to WASI calls. In C, fopen works only for preopened paths.
#include <stdio.h>
int main() {
FILE *f = fopen("data/hello.txt", "r");
if (!f) { perror("open"); return 1; }
char buf[128];
while (fgets(buf, sizeof buf, f)) fputs(buf, stdout);
fclose(f);
return 0;
}Writing Files Safely
Writing also requires a preopened directory with the right rights. Attempting to write outside it returns an error such as ENOTCAPABLE.
#include <stdio.h>
int main() {
FILE *f = fopen("data/out.txt", "w");
if (!f) return 1;
fputs("written via WASI\n", f);
fclose(f);
return 0;
}File Descriptors & Rights
Each WASI file descriptor carries a set of rights (read, write, seek, etc.). A directory descriptor can be more restrictive than its host counterpart, enabling least-privilege design.
Path Resolution Rules
WASI resolves paths relative to a preopened descriptor. The runtime picks the longest matching preopen. There is no global root; / is meaningless unless mapped.
The wasi-libc Layer
Languages like C compiled with wasi-sdk use wasi-libc, which translates POSIX calls into WASI syscalls. This is why familiar APIs just work inside the sandbox.
clang --target=wasm32-wasi -o app.wasm app.cListing a Directory
Directory iteration uses fd_readdir under the hood. High-level languages expose this as normal directory listing once a directory is preopened.
Common Errors
Typical filesystem errors when learning WASI:
ENOENT— file not found within the preopenENOTCAPABLE— path is outside any granted directoryEACCES— descriptor lacks the required right
Multiple Preopens
You can grant several directories, each with a distinct guest alias.
wasmtime run --dir=./in::input --dir=./out::output app.wasmBest Practices
Grant the narrowest directory possible, prefer read-only preopens, and never assume host absolute paths exist inside the module.
Quick Check
Test your understanding of WASI file access.
Recap
You learned that WASI uses preopened directories and capability-based descriptors to expose files safely. Paths resolve relative to preopens, rights enforce least privilege, and wasi-libc bridges POSIX APIs to WASI syscalls.
Frequently asked questions
Is the “Working with the WASI Filesystem” lesson free?
Yes — the full text of “Working with the WASI Filesystem” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.
What will I learn in “Working with the WASI Filesystem”?
Learn how WASI exposes the host filesystem to a sandboxed module through preopened directories and capability-based file access. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebAssembly (WASM) for High Performance Apps?
No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Working with the WASI Filesystem” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?
Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to WASI & Its Goals
- Building & Running WASI Modules
- WASI Capabilities & Future
- Working with the WASI Filesystem