0Pricing
WebAssembly (WASM) for High Performance Apps · レッスン

WASIファイルシステムを扱う

WASIがpreopened directoryとケイパビリティベースのファイルアクセスを通じて、サンドボックス化されたモジュールにホストのファイルシステムを公開する仕組みを学びます。

「WASIファイルシステムを扱う」はCoddyKit上の無料WebAssembly (WASM) for High Performance Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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ファイルシステムを扱う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebAssembly (WASM) for High Performance Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebAssembly (WASM) for High Performance Appsコースには全4レッスンが含まれています。

「WASIファイルシステムを扱う」で何を学びますか?

WASIがpreopened directoryとケイパビリティベースのファイルアクセスを通じて、サンドボックス化されたモジュールにホストのファイルシステムを公開する仕組みを学びます。 ブラウザで直接実行するハンズオンコードでWebAssembly (WASM) for High Performance Appsを演習し、24時間対応の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に戻る