1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::error::VfsResultExt;
use crate::{FileSystem, VfsError, VfsResult};
use std::io::{Read, Seek, Write};
use std::sync::Arc;
pub trait SeekAndRead: Seek + Read {}
impl<T> SeekAndRead for T where T: Seek + Read {}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum VfsFileType {
File,
Directory,
}
#[derive(Debug)]
pub struct VfsMetadata {
pub file_type: VfsFileType,
pub len: u64,
}
#[derive(Debug)]
pub struct VFS {
fs: Box<dyn FileSystem>,
}
#[derive(Clone, Debug)]
pub struct VfsPath {
path: String,
fs: Arc<VFS>,
}
impl PartialEq for VfsPath {
fn eq(&self, other: &Self) -> bool {
self.path == other.path && Arc::ptr_eq(&self.fs, &other.fs)
}
}
impl Eq for VfsPath {}
impl VfsPath {
pub fn new<T: FileSystem>(filesystem: T) -> Self {
VfsPath {
path: "".to_string(),
fs: Arc::new(VFS {
fs: Box::new(filesystem),
}),
}
}
pub fn as_str(&self) -> &str {
&self.path
}
pub fn join(&self, path: &str) -> VfsResult<Self> {
if path.is_empty() {
return Ok(VfsPath {
path: self.path.clone(),
fs: self.fs.clone(),
});
}
let mut new_path = self.path.clone();
for component in path.split('/') {
if component == "." {
continue;
}
if component == ".." {
return Err(VfsError::InvalidPath {
path: path.to_string(),
});
}
new_path += "/";
new_path += component;
}
Ok(VfsPath {
path: new_path,
fs: self.fs.clone(),
})
}
pub fn read_dir(&self) -> VfsResult<Box<dyn Iterator<Item = VfsPath>>> {
let parent = self.path.clone();
let fs = self.fs.clone();
Ok(Box::new(
self.fs
.fs
.read_dir(&self.path)
.with_context(|| format!("Could not read directory '{}'", &self.path))?
.map(move |path| VfsPath {
path: format!("{}/{}", parent, path),
fs: fs.clone(),
}),
))
}
pub fn create_dir(&self) -> VfsResult<()> {
self.fs
.fs
.create_dir(&self.path)
.with_context(|| format!("Could not create directory '{}'", &self.path))
}
pub fn create_dir_all(&self) -> VfsResult<()> {
let mut pos = 1;
let path = &self.path;
loop {
let end = path[pos..]
.find('/')
.map(|it| it + pos)
.unwrap_or_else(|| path.len());
let directory = &path[..end];
if !self.fs.fs.exists(directory) {
self.fs.fs.create_dir(directory)?;
}
if end == path.len() {
break;
}
pos = end + 1;
}
Ok(())
}
pub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead>> {
self.fs
.fs
.open_file(&self.path)
.with_context(|| format!("Could not open file '{}'", &self.path))
}
pub fn create_file(&self) -> VfsResult<Box<dyn Write>> {
self.fs
.fs
.create_file(&self.path)
.with_context(|| format!("Could not create file '{}'", &self.path))
}
pub fn append_file(&self) -> VfsResult<Box<dyn Write>> {
self.fs
.fs
.append_file(&self.path)
.with_context(|| format!("Could not open file '{}' for appending", &self.path))
}
pub fn remove_file(&self) -> VfsResult<()> {
self.fs
.fs
.remove_file(&self.path)
.with_context(|| format!("Could not remove file '{}'", &self.path))
}
pub fn remove_dir(&self) -> VfsResult<()> {
self.fs
.fs
.remove_dir(&self.path)
.with_context(|| format!("Could not remove directory '{}'", &self.path))
}
pub fn remove_dir_all(&self) -> VfsResult<()> {
if !self.exists() {
return Ok(());
}
for child in self.read_dir()? {
let metadata = child.metadata()?;
match metadata.file_type {
VfsFileType::File => child.remove_file()?,
VfsFileType::Directory => child.remove_dir_all()?,
}
}
self.remove_dir()?;
Ok(())
}
pub fn metadata(&self) -> VfsResult<VfsMetadata> {
self.fs
.fs
.metadata(&self.path)
.with_context(|| format!("Could get metadata for '{}'", &self.path))
}
pub fn exists(&self) -> bool {
self.fs.fs.exists(&self.path)
}
pub fn filename(&self) -> String {
let index = self.path.rfind('/').map(|x| x + 1).unwrap_or(0);
self.path[index..].to_string()
}
pub fn extension(&self) -> Option<String> {
let filename = self.filename();
let mut parts = filename.rsplitn(2, '.');
let after = parts.next();
let before = parts.next();
match before {
None | Some("") => None,
_ => after.map(|x| x.to_string()),
}
}
pub fn parent(&self) -> Option<Self> {
let index = self.path.rfind('/').map(|x| x);
index.map(|idx| VfsPath {
path: self.path[..idx].to_string(),
fs: self.fs.clone(),
})
}
}