0Pricing
WebAssembly (WASM) for High Performance Apps · Lección

Trabajar con el sistema de archivos de WASI

Aprenda cómo WASI expone el sistema de archivos del host a un módulo aislado mediante directorios preabiertos y acceso a archivos basado en capacidades.

Trabajar con el sistema de archivos de WASI es una lección gratuita de WebAssembly (WASM) for High Performance Apps en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de WebAssembly (WASM) for High Performance Apps, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de WebAssembly (WASM) for High Performance Apps incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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.

Preguntas frecuentes

¿La lección «Trabajar con el sistema de archivos de WASI» es gratis?

Sí — el texto completo de «Trabajar con el sistema de archivos de WASI» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de WebAssembly (WASM) for High Performance Apps, actualiza a CoddyKit PRO. El curso de WebAssembly (WASM) for High Performance Apps incluye 4 lecciones en total.

¿Qué aprenderé en «Trabajar con el sistema de archivos de WASI»?

Aprenda cómo WASI expone el sistema de archivos del host a un módulo aislado mediante directorios preabiertos y acceso a archivos basado en capacidades. Practicas WebAssembly (WASM) for High Performance Apps con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar WebAssembly (WASM) for High Performance Apps?

No se requiere experiencia previa. WebAssembly (WASM) for High Performance Apps en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Trabajar con el sistema de archivos de WASI»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de WebAssembly (WASM) for High Performance Apps?

Sí. Cada lección de WebAssembly (WASM) for High Performance Apps incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Introducción a WASI y sus objetivos
  2. Crear y ejecutar módulos WASI
  3. Capacidades y futuro de WASI
  4. Trabajar con el sistema de archivos de WASI
← Volver a WebAssembly (WASM) for High Performance Apps