Virtual File System Toolkit
A lightweight, extensible virtual filesystem (VFS) toolkit for Rust. Provides in‑process abstractions over real or simulated filesystems, ideal for testing, sandboxing, custom storage backends, and so on.
Overview
vfs-kit lets you work with filesystem-like structures in Rust without touching the real disk (unless you want to).
It defines a common FsBackend trait and provides concrete implementations like DirFS that map to real directories.
Key ideas:
- Abstraction: Treat different storage backends (real dirs, memory maps, etc.) via a unified API.
- Safety: Operations are confined to a root path; no accidental host filesystem access.
- Testability: Use in unit tests to simulate filesystems without side effects.
- Extensibility: Plug in new backends by implementing
FsBackend. - Clarity: Comprehensive error messages and documentation.
Features
- Path normalization (
.,.., trailing slashes) - Current working directory (
cwd) support - Create/read/write/remove files and directories
- Existence checks and state tracking
- Auto‑cleanup on drop (optional)
- Cross‑platform path handling
- Rich error messages via
anyhow - Clean, documented API
- Easy to extend with custom backends
Installation
Add to your Cargo.toml:
[]
= "0.1"
Or via cargo add:
Getting Started
- Add
vfs-kitto yourCargo.toml. - Choose a backend (
DirFSfor real dirs, planMapFSfor memory). - Create an instance with a root path.
- Use
mkdir,mkfile,rm,cdand other commands as needed. - Let the VFS clean up on drop (or disable auto‑cleanup).
Usage Example
use ;
use Path;
API Summary
Core Trait
FsBackend: Defines the VFS interface:root()— get the root pathcwd()— get current working directorycd(path)— change directoryexists(path)— check if path existsls(path)— returns an iterator over directory entriestree(path)— returns a recursive iterator over the directory tree starting from a given pathmkdir(path)— creates directorymkfile(path, content)— creates file with optional contentread(path)— read all contents of a filewrite(path, content)— writes contents to a fileappend(path, content)— appends content to the end of the filerm(path)— removes file or directory (recursively)cleanup()— removes all created artifacts (dirs and files)
Implementations
DirFS: Maps to a real directory on disk.- All operations are relative to root.
- Tracks state.
- Supports auto‑cleanup of created parent directories.
- Normalizes paths automatically.
- Enforces absolute root path at construction.
Design Principles
- Minimalism: Only essential VFS operations.
- Transparency: Errors include context (e.g., which path failed).
- Zero‑cost abstraction: No runtime overhead beyond what the backend needs.
- User‑first: Clear docs, examples, and error messages.
- Test‑friendly: Designed for use in unit and integration tests.
Planned Features
We’re working on these backends:
-
MapFS- In‑memory filesystem using Map.
- Ideal for testing and transient data.
- No disk I/O; fully deterministic.
- Great for mocking file content in tests.
-
LogFS- Append‑only log‑structured filesystem.
- Persists operations as a sequence of atomic log entries.
- Useful for audit trails, replay, and crash recovery.
- Configurable log rotation and compaction.
-
Additional Backends (roadmap)
- ZipFS: Read/write ZIP archives as VFS.
- CloudFS: Mount remote HTTP/S3 resources.
- EncryptedFS: Layered encryption over any backend.
Contributing
We welcome:
- Bug reports
- Feature requests
- Documentation improvements
Contact & Links
- Repository: https://github.com/dvshapkin/vfs-kit
- Issues: https://github.com/dvshapkin/vfs-kit/issues
- Documentation: https://docs.rs/vfs-kit