#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VortexFsError {
NotFound(String),
PermissionDenied(String),
DiskFull(String),
IoError(String),
AlreadyExists(String),
NotADirectory(String),
IsADirectory(String),
NotEmpty(String),
TornWrite {
path: String,
bytes_written: u64,
intended: u64,
},
Corrupted(String),
}
impl std::fmt::Display for VortexFsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VortexFsError::NotFound(p) => write!(f, "not found: {p}"),
VortexFsError::PermissionDenied(p) => write!(f, "permission denied: {p}"),
VortexFsError::DiskFull(p) => write!(f, "disk full: {p}"),
VortexFsError::IoError(p) => write!(f, "I/O error: {p}"),
VortexFsError::AlreadyExists(p) => write!(f, "already exists: {p}"),
VortexFsError::NotADirectory(p) => write!(f, "not a directory: {p}"),
VortexFsError::IsADirectory(p) => write!(f, "is a directory: {p}"),
VortexFsError::NotEmpty(p) => write!(f, "not empty: {p}"),
VortexFsError::TornWrite {
path,
bytes_written,
intended,
} => {
write!(
f,
"torn write on {path}: wrote {bytes_written}/{intended} bytes"
)
}
VortexFsError::Corrupted(p) => write!(f, "data corrupted: {p}"),
}
}
}
impl std::error::Error for VortexFsError {}
pub type VortexFsResult<T> = std::result::Result<T, VortexFsError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileType {
File,
Directory,
}
#[derive(Debug, Clone)]
pub struct FileMetadata {
pub file_type: FileType,
pub size: u64,
}
pub trait VortexFs {
fn read_file(&self, path: &str) -> VortexFsResult<Vec<u8>>;
fn write_file(&mut self, path: &str, data: &[u8]) -> VortexFsResult<()>;
fn append_file(&mut self, path: &str, data: &[u8]) -> VortexFsResult<()>;
fn remove_file(&mut self, path: &str) -> VortexFsResult<()>;
fn rename(&mut self, from: &str, to: &str) -> VortexFsResult<()>;
fn create_dir_all(&mut self, path: &str) -> VortexFsResult<()>;
fn remove_dir(&mut self, path: &str) -> VortexFsResult<()>;
fn read_dir(&self, path: &str) -> VortexFsResult<Vec<String>>;
fn metadata(&self, path: &str) -> VortexFsResult<FileMetadata>;
fn exists(&self, path: &str) -> bool;
fn fsync(&mut self, path: &str) -> VortexFsResult<()>;
}