1#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum VortexFsError {
6 NotFound(String),
8 PermissionDenied(String),
10 DiskFull(String),
12 IoError(String),
14 AlreadyExists(String),
16 NotADirectory(String),
18 IsADirectory(String),
20 NotEmpty(String),
22 TornWrite {
24 path: String,
25 bytes_written: u64,
26 intended: u64,
27 },
28 Corrupted(String),
30}
31
32impl std::fmt::Display for VortexFsError {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 match self {
35 VortexFsError::NotFound(p) => write!(f, "not found: {p}"),
36 VortexFsError::PermissionDenied(p) => write!(f, "permission denied: {p}"),
37 VortexFsError::DiskFull(p) => write!(f, "disk full: {p}"),
38 VortexFsError::IoError(p) => write!(f, "I/O error: {p}"),
39 VortexFsError::AlreadyExists(p) => write!(f, "already exists: {p}"),
40 VortexFsError::NotADirectory(p) => write!(f, "not a directory: {p}"),
41 VortexFsError::IsADirectory(p) => write!(f, "is a directory: {p}"),
42 VortexFsError::NotEmpty(p) => write!(f, "not empty: {p}"),
43 VortexFsError::TornWrite {
44 path,
45 bytes_written,
46 intended,
47 } => {
48 write!(
49 f,
50 "torn write on {path}: wrote {bytes_written}/{intended} bytes"
51 )
52 }
53 VortexFsError::Corrupted(p) => write!(f, "data corrupted: {p}"),
54 }
55 }
56}
57
58impl std::error::Error for VortexFsError {}
59
60pub type VortexFsResult<T> = std::result::Result<T, VortexFsError>;
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum FileType {
66 File,
67 Directory,
68}
69
70#[derive(Debug, Clone)]
72pub struct FileMetadata {
73 pub file_type: FileType,
75 pub size: u64,
77}
78
79pub trait VortexFs {
84 fn read_file(&self, path: &str) -> VortexFsResult<Vec<u8>>;
86
87 fn write_file(&mut self, path: &str, data: &[u8]) -> VortexFsResult<()>;
89
90 fn append_file(&mut self, path: &str, data: &[u8]) -> VortexFsResult<()>;
92
93 fn remove_file(&mut self, path: &str) -> VortexFsResult<()>;
95
96 fn rename(&mut self, from: &str, to: &str) -> VortexFsResult<()>;
98
99 fn create_dir_all(&mut self, path: &str) -> VortexFsResult<()>;
101
102 fn remove_dir(&mut self, path: &str) -> VortexFsResult<()>;
104
105 fn read_dir(&self, path: &str) -> VortexFsResult<Vec<String>>;
107
108 fn metadata(&self, path: &str) -> VortexFsResult<FileMetadata>;
110
111 fn exists(&self, path: &str) -> bool;
113
114 fn fsync(&mut self, path: &str) -> VortexFsResult<()>;
116}