#memory #fs #buf-fs

buf-fs

A buffer based, in-memory filesystem

1 unstable release

Uses new Rust 2024

new 0.1.0 May 3, 2025

#1127 in Filesystem

MIT/Apache

18KB
407 lines

buf-fs

crates.io Documentation License

A buffer based, in-memory filesystem.

This crate mimics a file system, operating entirely within memory. By establishing a Fat (File Allocation Table) partition within a byte buffer, it exposes an API resembling that of a traditional filesystem to its users.

Example

use std::path::PathBuf;

use buf_fs::FileSystem;

fn main() -> anyhow::Result<()> {

let mut buffer = vec![0u8; 1024 * 1024];
let mut fs = FileSystem::mount_or_format(&mut buffer)?;

let data = b"foo";

fs.mkdir("/var/share/baz")?;

fs.open("/var/share/data.bin")?
    .update(|b| b.extend(data))
    .save(&mut fs)?;

fs.open("/var/share/bar.bin")?
    .update(|b| b.extend(data))
    .save(&mut fs)?;

assert_eq!(fs.open("/var/share/data.bin")?.contents, data);

let contents = fs.ls("/var/share")?;

assert_eq!(contents.len(), 3);
assert_eq!(contents[0].path(), PathBuf::from("/var/share/baz"));
assert_eq!(contents[1].path(), PathBuf::from("/var/share/bar.bin"));
assert_eq!(contents[2].path(), PathBuf::from("/var/share/data.bin"));

Ok(())
}

Dependencies

~2MB
~31K SLoC