pub struct InMemoryFs { /* private fields */ }Expand description
In-memory filesystem implementation.
InMemoryFs is the default filesystem used by Bash::new().
It stores all files and directories in memory using a HashMap, making it
ideal for sandboxed execution where no real filesystem access is needed.
§Features
- Thread-safe: Uses
RwLockfor concurrent read/write access - Binary-safe: Fully supports binary data including null bytes
- Default directories: Creates
/,/tmp,/home,/home/user,/dev - Special devices:
/dev/nulldiscards writes and returns empty on read
§Example
use bashkit::{Bash, FileSystem, InMemoryFs};
use std::path::Path;
use std::sync::Arc;
// InMemoryFs is the default when using Bash::new()
let mut bash = Bash::new();
// Or create explicitly for direct filesystem access
let fs = Arc::new(InMemoryFs::new());
// Write files
fs.write_file(Path::new("/tmp/test.txt"), b"hello").await?;
// Read files
let content = fs.read_file(Path::new("/tmp/test.txt")).await?;
assert_eq!(content, b"hello");
// Create directories
fs.mkdir(Path::new("/data/nested/dir"), true).await?;
// Check existence
assert!(fs.exists(Path::new("/data/nested/dir")).await?);
// Use with Bash
let mut bash = Bash::builder().fs(fs.clone()).build();
bash.exec("echo 'from bash' >> /tmp/test.txt").await?;
let content = fs.read_file(Path::new("/tmp/test.txt")).await?;
assert_eq!(content, b"hellofrom bash\n");§Default Directory Structure
InMemoryFs::new() creates these directories:
/
├── tmp/
├── home/
│ └── user/
└── dev/
└── null (special device)§Binary Data
The filesystem fully supports binary data:
use bashkit::{FileSystem, InMemoryFs};
use std::path::Path;
let fs = InMemoryFs::new();
// Write binary with null bytes
let data = vec![0x89, 0x50, 0x4E, 0x47, 0x00, 0xFF];
fs.write_file(Path::new("/tmp/binary.bin"), &data).await?;
// Read it back unchanged
let read = fs.read_file(Path::new("/tmp/binary.bin")).await?;
assert_eq!(read, data);§Resource Limits
Configure limits to prevent memory exhaustion:
use bashkit::{FileSystem, InMemoryFs, FsLimits};
use std::path::Path;
let limits = FsLimits::new()
.max_total_bytes(1_000_000) // 1MB total
.max_file_size(100_000) // 100KB per file
.max_file_count(100); // 100 files max
let fs = InMemoryFs::with_limits(limits);
// This works
fs.write_file(Path::new("/tmp/small.txt"), b"hello").await?;
// This would fail with "file too large" error:
// let big_data = vec![0u8; 200_000];
// fs.write_file(Path::new("/tmp/big.bin"), &big_data).await?;Implementations§
Source§impl InMemoryFs
impl InMemoryFs
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new in-memory filesystem with default directories and default limits.
Creates the following directory structure:
/- Root directory/tmp- Temporary files/home- Home directories/home/user- Default user home/dev- Device files/dev/null- Null device (discards writes, returns empty)
§Default Limits
- Total filesystem: 100MB
- Single file: 10MB
- File count: 10,000
Use InMemoryFs::with_limits for custom limits.
§Example
use bashkit::{FileSystem, InMemoryFs};
use std::path::Path;
let fs = InMemoryFs::new();
// Default directories exist
assert!(fs.exists(Path::new("/tmp")).await?);
assert!(fs.exists(Path::new("/home/user")).await?);
assert!(fs.exists(Path::new("/dev/null")).await?);Sourcepub fn with_limits(limits: FsLimits) -> Self
pub fn with_limits(limits: FsLimits) -> Self
Create a new in-memory filesystem with custom limits.
§Example
use bashkit::{FileSystem, InMemoryFs, FsLimits};
use std::path::Path;
let limits = FsLimits::new()
.max_total_bytes(50_000_000) // 50MB
.max_file_size(5_000_000); // 5MB per file
let fs = InMemoryFs::with_limits(limits);Sourcepub fn add_file(
&self,
path: impl AsRef<Path>,
content: impl AsRef<[u8]>,
mode: u32,
)
pub fn add_file( &self, path: impl AsRef<Path>, content: impl AsRef<[u8]>, mode: u32, )
Add a file with specific mode (synchronous, for initial setup).
This method is primarily used by BashBuilder to
pre-populate the filesystem during construction. For runtime file operations,
use the async FileSystem::write_file method instead.
Parent directories are created automatically.
§Arguments
path- Absolute path where the file will be createdcontent- File content (will be converted to bytes)mode- Unix permission mode (e.g.,0o644for writable,0o444for readonly)
§Example
use bashkit::InMemoryFs;
let fs = InMemoryFs::new();
// Add a writable config file
fs.add_file("/config/app.conf", "debug=true\n", 0o644);
// Add a readonly file
fs.add_file("/etc/version", "1.0.0", 0o444);