pub struct VirtualFs { /* private fields */ }Expand description
Sandboxed filesystem for a single knowledge base.
All file operations are constrained to the root directory. Path traversal attempts are rejected.
Implementations§
Source§impl VirtualFs
impl VirtualFs
Sourcepub fn new(root: PathBuf) -> Result<Self>
pub fn new(root: PathBuf) -> Result<Self>
Create a new VirtualFs rooted at the given directory.
Creates the directory if it doesn’t exist.
Sourcepub fn with_quota(self, quota_kb: i64) -> Self
pub fn with_quota(self, quota_kb: i64) -> Self
Set a storage quota in kilobytes (0 = unlimited).
Sourcepub fn safe_path(&self, dir: &str, filename: &str) -> Result<PathBuf, FsError>
pub fn safe_path(&self, dir: &str, filename: &str) -> Result<PathBuf, FsError>
Build a safe absolute path from a directory and filename.
Rejects path traversal attempts (e.g., ../../etc/passwd).
Sourcepub fn read_path(&self, path: &str) -> Result<String, FsError>
pub fn read_path(&self, path: &str) -> Result<String, FsError>
Read file content by POSIX-style relative path.
path examples: “Rust.md”, “brain/Rust.md”, “journal/2024.08 August.md”
Sourcepub fn write_path(&self, path: &str, content: &str) -> Result<(), FsError>
pub fn write_path(&self, path: &str, content: &str) -> Result<(), FsError>
Write file content by POSIX-style relative path.
Sourcepub fn delete_path(&self, path: &str) -> Result<(), FsError>
pub fn delete_path(&self, path: &str) -> Result<(), FsError>
Delete file by POSIX-style relative path.
Sourcepub fn rename_path(&self, old_path: &str, new_path: &str) -> Result<(), FsError>
pub fn rename_path(&self, old_path: &str, new_path: &str) -> Result<(), FsError>
Rename/move file by POSIX-style relative paths.
Sourcepub fn exists_path(&self, path: &str) -> Result<bool, FsError>
pub fn exists_path(&self, path: &str) -> Result<bool, FsError>
Check if file exists by POSIX-style relative path.
Sourcepub fn mtime_path(&self, path: &str) -> Result<i64, FsError>
pub fn mtime_path(&self, path: &str) -> Result<i64, FsError>
Get mtime by POSIX-style relative path.
Sourcepub fn exists(&self, dir: &str, filename: &str) -> Result<bool, FsError>
pub fn exists(&self, dir: &str, filename: &str) -> Result<bool, FsError>
Check if a file or directory exists.
Sourcepub fn read(&self, dir: &str, filename: &str) -> Result<String, FsError>
pub fn read(&self, dir: &str, filename: &str) -> Result<String, FsError>
Read file contents as a string.
Sourcepub fn write(
&self,
dir: &str,
filename: &str,
content: &str,
) -> Result<(), FsError>
pub fn write( &self, dir: &str, filename: &str, content: &str, ) -> Result<(), FsError>
Write content to a file, creating parent directories as needed.
Sourcepub fn read_bytes(&self, dir: &str, filename: &str) -> Result<Vec<u8>, FsError>
pub fn read_bytes(&self, dir: &str, filename: &str) -> Result<Vec<u8>, FsError>
Read a file as raw bytes.
Sourcepub fn write_bytes(
&self,
dir: &str,
filename: &str,
data: &[u8],
) -> Result<(), FsError>
pub fn write_bytes( &self, dir: &str, filename: &str, data: &[u8], ) -> Result<(), FsError>
Write raw bytes to a file, creating parent directories as needed.
Respects the configured quota (same logic as write()).
Sourcepub fn read_path_bytes(&self, path: &str) -> Result<Vec<u8>, FsError>
pub fn read_path_bytes(&self, path: &str) -> Result<Vec<u8>, FsError>
Read a file by POSIX path as raw bytes.
Sourcepub fn write_path_bytes(&self, path: &str, data: &[u8]) -> Result<(), FsError>
pub fn write_path_bytes(&self, path: &str, data: &[u8]) -> Result<(), FsError>
Write raw bytes to a file by POSIX path.
Sourcepub fn rename(
&self,
old_dir: &str,
old_filename: &str,
new_dir: &str,
new_filename: &str,
) -> Result<(), FsError>
pub fn rename( &self, old_dir: &str, old_filename: &str, new_dir: &str, new_filename: &str, ) -> Result<(), FsError>
Rename/move a file.
Sourcepub fn touch(&self, dir: &str, filename: &str) -> Result<(), FsError>
pub fn touch(&self, dir: &str, filename: &str) -> Result<(), FsError>
Touch a file: create if missing, update mtime if present.
Sourcepub fn ctime(&self, dir: &str, filename: &str) -> Result<i64, FsError>
pub fn ctime(&self, dir: &str, filename: &str) -> Result<i64, FsError>
Get the ctime/mtime of a file in milliseconds since epoch.
Sourcepub fn mtime(&self, dir: &str, filename: &str) -> Result<i64, FsError>
pub fn mtime(&self, dir: &str, filename: &str) -> Result<i64, FsError>
Get the modification time of a file in milliseconds since epoch.
Sourcepub fn mtimes(
&self,
root: &str,
extensions: &[&str],
) -> Result<HashMap<String, i64>, FsError>
pub fn mtimes( &self, root: &str, extensions: &[&str], ) -> Result<HashMap<String, i64>, FsError>
Recursively collect mtimes for all files with given extensions.
Sourcepub fn files_and_dirs(&self, dir: &str) -> Result<Vec<FileEntry>, FsError>
pub fn files_and_dirs(&self, dir: &str) -> Result<Vec<FileEntry>, FsError>
List files and directories in a directory.
Sourcepub fn is_multiline(&self, dir: &str, filename: &str) -> Result<bool, FsError>
pub fn is_multiline(&self, dir: &str, filename: &str) -> Result<bool, FsError>
Check if a file has non-whitespace content.
Sourcepub fn create_system_dirs(&self) -> Result<(), FsError>
pub fn create_system_dirs(&self) -> Result<(), FsError>
Create the standard system directories (archive, media, journal).