Lavorare con il filesystem WASI
Imparate come WASI espone il filesystem dell’host a un modulo in sandbox tramite directory preaperte e accesso ai file basato sulle capability.
Lavorare con il filesystem WASI è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Lavorare con il filesystem WASI» è gratuita?
Sì — il testo completo di «Lavorare con il filesystem WASI» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Cosa imparerò in «Lavorare con il filesystem WASI»?
Imparate come WASI espone il filesystem dell’host a un modulo in sandbox tramite directory preaperte e accesso ai file basato sulle capability. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?
Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Lavorare con il filesystem WASI»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?
Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Introduzione a WASI e ai suoi obiettivi
- Creare ed eseguire moduli WASI
- Funzionalità e futuro di WASI
- Lavorare con il filesystem WASI