0Pricing
WebAssembly (WASM) for High Performance Apps · 课时

使用 WASI 文件系统

学习 WASI 如何通过预开放目录和基于能力的文件访问,将主机文件系统提供给沙盒模块。

使用 WASI 文件系统 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.wasm

Reading 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.c

Listing 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 preopen
  • ENOTCAPABLE — path is outside any granted directory
  • EACCES — 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.wasm

Best 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 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

「使用 WASI 文件系统」这节课中我会学到什么?

学习 WASI 如何通过预开放目录和基于能力的文件访问,将主机文件系统提供给沙盒模块。 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebAssembly (WASM) for High Performance Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebAssembly (WASM) for High Performance Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 WASI 文件系统」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebAssembly (WASM) for High Performance Apps 课中编写并运行代码吗?

能。每节 WebAssembly (WASM) for High Performance Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. WASI 简介与目标
  2. 构建并运行 WASI 模块
  3. WASI 的能力与未来
  4. 使用 WASI 文件系统
← 返回 WebAssembly (WASM) for High Performance Apps