vfs-host
Host trait implementation for VFS that enables multiple WebAssembly applications to share a single in-memory filesystem at runtime.
Overview
vfs-host is a Rust library that implements wasmtime Host traits using fs-core directly. This library enables multiple applications to concurrently access and modify the same VFS instance.
Features
- Shared VFS State: Multiple applications access the same VFS instance concurrently
- Thread-Safe: Uses
Arc<Mutex<>>for safe concurrent access - State Persistence: VFS state persists as long as any application references it
- Direct fs-core Integration: No WASM adapter needed, uses fs-core natively
- Complete WASI Implementation: All 33 WASI filesystem Host trait methods implemented
- 26 real implementations (file I/O, directories, metadata, stream API, etc.)
- 7 stub implementations (advisory hints, sync operations)
- Full Stream API Support: Complete implementation of
read_via_stream,write_via_stream,append_via_stream - Zero-Copy Resource Mapping: Efficient descriptor and stream resource management
Usage
Basic Example
use VfsHostState;
use ;
// Create engine with component model support
let mut config = new;
config.wasm_component_model;
let engine = new?;
// Create VFS host (uses fs-core directly, no WASM adapter needed)
let vfs_host = new?;
// Create application store
let mut store = new;
Sharing VFS Across Multiple Applications
use VfsHostState;
use ;
// Create shared VFS host
let vfs_host = new?;
// Create multiple application contexts sharing the same VFS
let vfs_host2 = vfs_host.clone_shared;
let vfs_host3 = vfs_host.clone_shared;
// Create stores for each application
let mut store1 = new;
let mut store2 = new;
let mut store3 = new;
// All three stores share the same VFS instance
State Persistence Example
use VfsHostState;
let vfs_host = new?;
let vfs_host2 = vfs_host.clone_shared;
// App2 starts AFTER App1 terminated
let mut store2 = new;
// App2 can still access files created by App1
// VFS state persisted because vfs_host2 still held a reference
Architecture
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Application1 │ │ Application2 │ │ Application3 │
└──────┬───────┘ └──────┬───────┘ └──────┬───────┘
│ │ │
│ VfsHostState (clone_shared()) │
└───────────────────┼───────────────────┘
│
Arc<Mutex<SharedVfsCore>>
│
┌───────┴───────┐
│ fs-core │
│ (In-Memory) │
└───────────────┘
API Reference
Core Types
VfsHostState
Main host context that implements WASI Host traits.
Methods:
-
new() -> Result<Self>- Creates a new VFS host with a fresh fs-core filesystem instance
-
clone_shared(&self) -> Self- Creates a new VFS host that shares the same VFS core
- Used to create multiple application contexts sharing one VFS
-
clone_shared_with_env(&self, env_vars: &[(&str, &str)]) -> Self- Creates a new VFS host with shared VFS and custom environment variables
-
from_shared_vfs_with_env(shared_vfs: Arc<Mutex<SharedVfsCore>>, env_vars: &[(&str, &str)]) -> Self- Creates a new VFS host from an existing shared VFS with environment variables
-
get_shared_vfs(&self) -> Arc<Mutex<SharedVfsCore>>- Returns the shared VFS core for external use
SharedVfsCore
Shared VFS state accessed by all applications.
Implementation Details
Host Trait Coverage
The library implements all WASI Preview 2 filesystem Host traits:
wasi:filesystem/types@0.2.6:
Hosttrait (2 methods): Error conversion utilitiesHostDescriptortrait (28 methods): File/directory operationsHostDirectoryEntryStreamtrait (2 methods): Directory listing
wasi:filesystem/preopens@0.2.6:
Hosttrait (1 method): Preopened directory listing
Real Implementations (26 methods)
- File I/O:
read,write - Path operations:
open_at,stat,stat_at,read_directory - Directory ops:
create_directory_at,remove_directory_at,unlink_file_at - Metadata ops:
set_size,get_flags,get_type - Comparison ops:
is_same_object,metadata_hash,metadata_hash_at - Stream API:
read_via_stream,write_via_stream,append_via_stream - Directory streaming:
read_directory_entry,drop(DirectoryEntryStream)
Unsupported Operations
These methods return Unsupported error as fs-core doesn't support them:
link_at,symlink_at,readlink_at: Symbolic/hard linksrename_at: File renamingset_times,set_times_at: Timestamp modificationadvise,sync_data,sync: Advisory/sync operations (no-op for in-memory FS)
Complete Example
See the examples/host-trait/runtime-linker directory in the parent repository for a complete working example:
Dependencies
wasmtimev27.0: WebAssembly runtime with component model supportwasmtime-wasiv27.0: WASI host implementationsfs-core: In-memory filesystem implementationanyhowv1.0: Error handling
License
MIT OR Apache-2.0