ทำงานกับระบบไฟล์ WASI
เรียนรู้ว่า WASI เปิดเผยระบบไฟล์ของโฮสต์ให้โมดูลในแซนด์บ็อกซ์ผ่านไดเรกทอรีที่เปิดไว้ล่วงหน้าและการเข้าถึงไฟล์ตามความสามารถอย่างไร
ทำงานกับระบบไฟล์ WASI เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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.
คำถามที่พบบ่อย
บทเรียน “ทำงานกับระบบไฟล์ WASI” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ทำงานกับระบบไฟล์ WASI” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทำงานกับระบบไฟล์ WASI”
เรียนรู้ว่า WASI เปิดเผยระบบไฟล์ของโฮสต์ให้โมดูลในแซนด์บ็อกซ์ผ่านไดเรกทอรีที่เปิดไว้ล่วงหน้าและการเข้าถึงไฟล์ตามความสามารถอย่างไร คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ทำงานกับระบบไฟล์ WASI” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ WASI และเป้าหมาย
- การสร้างและเรียกใช้โมดูล WASI
- ความสามารถและอนาคตของ WASI
- ทำงานกับระบบไฟล์ WASI