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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::collections::BTreeMap;
use std::collections::LinkedList;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use err::{Error, Result};
pub use file::File;
#[cfg(feature = "tar")]
pub use tar::Tar;
#[cfg(feature = "zip")]
pub use zip::Zip;
pub mod err;
mod file;
#[cfg(feature = "tar")]
pub mod tar;
#[cfg(feature = "zip")]
pub mod zip;
pub trait Store {
fn open(&self, path: &Path) -> Result<File>;
#[allow(unused_variables)]
fn open_write(&self, path: &Path) -> Result<File> {
Err(Error::Io(file::write_support_err()))
}
}
pub struct Local {
root: PathBuf,
}
impl Store for Local {
fn open(&self, path: &Path) -> Result<File> {
let file = fs::File::open(self.root.join(path))?;
Ok(File::from_fs(file))
}
fn open_write(&self, path: &Path) -> Result<File> {
let file = fs::OpenOptions::new()
.write(true)
.read(false)
.create(false)
.open(path)?;
Ok(File::from_fs(file))
}
}
impl Local {
pub fn new<P: Into<PathBuf>>(root: P) -> Self {
Self { root: root.into() }
}
pub fn pwd() -> Result<Self> {
Ok(Self::new(env::current_dir()?))
}
}
#[derive(Clone)]
pub struct Ram {
inner: BTreeMap<PathBuf, Vec<u8>>,
}
impl Store for Ram {
fn open(&self, path: &Path) -> Result<File> {
self.inner
.get(path)
.map(|b| File::from_ram(b))
.ok_or_else(|| Error::FileNotFound)
}
}
impl Ram {
pub fn new() -> Self {
Self {
inner: BTreeMap::new(),
}
}
pub fn clear(&mut self) {
self.inner.clear();
}
pub fn touch<P, F>(&mut self, path: P, file: F)
where
P: Into<PathBuf>,
F: Into<Vec<u8>>,
{
self.inner.insert(path.into(), file.into());
}
}
struct Mount {
path: PathBuf,
store: Box<dyn Store>,
}
pub struct MiniFs {
inner: LinkedList<Mount>,
}
impl MiniFs {
pub fn new() -> Self {
Self {
inner: LinkedList::new(),
}
}
pub fn mount<P, F>(mut self, path: P, store: F) -> Self
where
P: Into<PathBuf>,
F: Store + Send + 'static,
{
let path = path.into();
let store = Box::new(store);
self.inner.push_back(Mount { path, store });
self
}
pub fn umount<P: AsRef<Path>>(&mut self, path: P) -> Option<Box<dyn Store>> {
let path = path.as_ref();
if let Some(p) = self.inner.iter().rposition(|p| p.path == path) {
let mut tail = self.inner.split_off(p);
let fs = tail.pop_front().map(|m| m.store);
self.inner.append(&mut tail);
fs
} else {
None
}
}
}
impl Store for MiniFs {
fn open(&self, path: &Path) -> Result<File> {
let next = self.inner.iter().rev().find_map(|mnt| {
if let Ok(np) = path.strip_prefix(&mnt.path) {
Some((np, &mnt.store))
} else {
None
}
});
if let Some((np, store)) = next {
store.open(np)
} else {
Err(Error::FileNotFound)
}
}
}
macro_rules! tuples {
($head:ident,) => {};
($head:ident, $($tail:ident,)+) => {
impl<$head, $($tail,)+> Store for ($head, $($tail,)+)
where
$head: Store,
$($tail: Store,)+
{
#[allow(non_snake_case)]
fn open(&self, path: &Path) -> Result<File> {
let ($head, $($tail,)+) = self;
match $head.open(path) {
Ok(file) => return Ok(file),
Err(Error::FileNotFound) => {}
Err(err) => return Err(err),
}
$(
match $tail.open(path) {
Ok(file) => return Ok(file),
Err(Error::FileNotFound) => {}
Err(err) => return Err(err),
}
)+
Err(Error::FileNotFound)
}
}
tuples!($($tail,)+);
};
}
tuples! { A, B, C, D, E, F, G, H, }
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}