anyfs-backend
Core traits and types for the AnyFS pluggable virtual filesystem standard.
This crate defines the trait hierarchy that filesystem backends implement. It contains only trait definitions and types โ no concrete implementations. For backends, see the anyfs crate.
๐ User Guide ยท ๐ Design Manual
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Using a Backend
Most users only need the Fs trait โ it covers 90% of use cases:
use ;
use Path;
// This function works with ANY backend that implements Fs
Implementing a Backend
To create your own backend, implement the component traits:
use ;
use Path;
use ;
// Now MyBackend automatically implements Fs!
// fn use_backend(backend: &impl Fs) { ... }
Trait Hierarchy
AnyFS uses a layered trait architecture. Each layer builds on the previous:
Layer 1 (Core): FsRead + FsWrite + FsDir = Fs
โ
Layer 2 (Extended): Fs + FsLink + FsPermissions + FsSync + FsStats = FsFull
โ
Layer 3 (FUSE): FsFull + FsInode = FsFuse
โ
Layer 4 (POSIX): FsFuse + FsHandles + FsLock + FsXattr = FsPosix
Which Trait Should I Use?
| Trait | Use Case | Coverage |
|---|---|---|
Fs |
Basic file I/O, config files, data processing | 90% of apps |
FsFull |
Backup tools, file managers, symlinks | Extended features |
FsFuse |
FUSE mounts, virtual drives | Userspace filesystems |
FsPosix |
Databases, file locking, xattrs | Full POSIX semantics |
Complete Trait Reference
Component Traits (What You Implement)
| Trait | Provides | Key Methods | When to Use |
|---|---|---|---|
FsRead |
Read operations | read, read_to_string, exists, metadata |
Always (core requirement) |
FsWrite |
Write operations | write, append, remove_file, rename |
Always (core requirement) |
FsDir |
Directory operations | read_dir, create_dir, remove_dir_all |
Always (core requirement) |
FsLink |
Symbolic/hard links | symlink, hard_link, read_link |
Backup tools, Unix-like behavior |
FsPermissions |
Permission management | set_permissions |
File managers, security-aware apps |
FsSync |
Force disk sync | sync, sync_path |
Databases, crash-safe writes |
FsStats |
Filesystem statistics | statfs |
Disk space monitoring, quotas |
FsInode |
Inode โ path mapping | path_to_inode, inode_to_path, lookup |
FUSE filesystems |
FsHandles |
Open file handles | open, close, read_at, write_at |
Random access, concurrent file access |
FsLock |
File locking | lock, unlock, try_lock |
Multi-process coordination, databases |
FsXattr |
Extended attributes | get_xattr, set_xattr, list_xattr |
Metadata storage, macOS/Linux features |
FsPath |
Path canonicalization | canonicalize, soft_canonicalize |
Symlink resolution, path normalization |
Composite Traits (What You Use in Bounds)
| Trait | Combines | Typical Consumer |
|---|---|---|
Fs |
FsRead + FsWrite + FsDir |
Generic file processing code |
FsFull |
Fs + FsLink + FsPermissions + FsSync + FsStats |
File managers, backup/restore tools |
FsFuse |
FsFull + FsInode |
FUSE filesystem implementations |
FsPosix |
FsFuse + FsHandles + FsLock + FsXattr |
Databases, POSIX-compliant applications |
Blanket Implementations
All composite traits have blanket implementations. Just implement the component traits and you get the composite for free:
// Implement FsRead, FsWrite, FsDir โ get Fs automatically
// Implement Fs, FsLink, FsPermissions, FsSync, FsStats โ get FsFull automatically
Core Types
| Type | Description |
|---|---|
Metadata |
File metadata: size, type, times, permissions, inode |
FileType |
File, Directory, or Symlink |
DirEntry |
Single entry from directory listing |
Permissions |
Unix-style permission bits (0o755, etc.) |
StatFs |
Filesystem statistics (total/used/available space) |
Handle |
Opaque file handle for POSIX operations |
OpenFlags |
Flags for opening files (READ, WRITE, CREATE, etc.) |
LockType |
Shared or Exclusive file lock |
FsError |
Comprehensive error type with path and operation context |
Error Handling
All operations return Result<T, FsError>. Errors include context:
use FsError;
use PathBuf;
// Errors tell you what went wrong and where
let err = NotFound ;
println!; // "not found: /missing.txt"
let err = PermissionDenied ;
println!; // "read: permission denied: /secret"
Middleware (Layer Trait)
Use the Layer trait for Tower-style middleware composition:
use Layer;
// Configuration
// Middleware wrapper
// Layer creates the middleware
Chain middleware with LayerExt:
use LayerExt;
// backend.layer(CacheLayer::new()).layer(LoggingLayer::new())
Extension Traits
FsExt
Convenience methods for any Fs backend:
use ;
use Path;
FsExtJson (requires serde feature)
[]
= { = "0.1", = ["serde"] }
use ;
use ;
Thread Safety
All traits require Send + Sync. Methods take &self (not &mut self), enabling safe concurrent access:
use Arc;
use thread;
Feature Flags
| Feature | Description |
|---|---|
serde |
Enable serialization for types + FsExtJson trait |
Related Crates
| Crate | Description |
|---|---|
anyfs |
Concrete backends: Memory, Native, Overlay, SQLite |
anyfs-fuse |
FUSE integration for mounting backends |
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
See AGENTS.md for contribution guidelines and architecture decisions.
For AI Agents
See LLM_CONTEXT.md for a Context7-style reference optimized for LLM consumption.