Praca z systemem plików WASI
Dowiedz się, jak WASI udostępnia system plików hosta modułowi działającemu w sandboxie za pomocą wstępnie otwartych katalogów i dostępu do plików opartego na uprawnieniach.
Praca z systemem plików WASI to bezpłatna lekcja WebAssembly (WASM) for High Performance Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej WebAssembly (WASM) for High Performance Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs WebAssembly (WASM) for High Performance Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się WebAssembly (WASM) for High Performance Apps dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Praca z systemem plików WASI” jest bezpłatna?
Tak — pełny tekst „Praca z systemem plików WASI” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu WebAssembly (WASM) for High Performance Apps, przejdź na CoddyKit PRO. Kurs WebAssembly (WASM) for High Performance Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Praca z systemem plików WASI”?
Dowiedz się, jak WASI udostępnia system plików hosta modułowi działającemu w sandboxie za pomocą wstępnie otwartych katalogów i dostępu do plików opartego na uprawnieniach. Ćwiczysz WebAssembly (WASM) for High Performance Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć WebAssembly (WASM) for High Performance Apps?
Nie wymagamy żadnego doświadczenia. WebAssembly (WASM) for High Performance Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Praca z systemem plików WASI”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji WebAssembly (WASM) for High Performance Apps?
Tak. Każda lekcja WebAssembly (WASM) for High Performance Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do WASI i jego celów
- Budowanie i uruchamianie modułów WASI
- Możliwości WASI i przyszłość
- Praca z systemem plików WASI