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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#![no_std]
#![feature(allocator_internals)]
#![needs_allocator]
#![allow(incomplete_features)]
#![feature(alloc_prelude)]
#![feature(trait_upcasting)]
#![feature(get_mut_unchecked)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
#[macro_use]
extern crate slos_log;
#[allow(unused_imports)]
use self::alloc_prelude::*;
use alloc::prelude::v1 as alloc_prelude;
use core::fmt::{self, Debug};
use lasso::{Rodeo, Spur};
use slos_helpers::{StaticCollection, UnsafeContainer};
lazy_static::lazy_static! {
static ref INTERNED: UnsafeContainer<Rodeo> = UnsafeContainer::new(Rodeo::new());
}
mod errors;
pub use self::errors::*;
pub mod path;
pub mod impls;
pub trait FsReadDir {
fn readdir(&mut self) -> Result<Vec<&mut dyn FsNode>, FsError> {
Err(FsError::InvalidArgument)
}
}
pub trait FsWriteDir {
fn touch(&mut self, _name: &str) -> Result<&mut dyn FsNode, FsError> {
Err(FsError::InvalidArgument)
}
fn mkdir(&mut self, _name: &str) -> Result<&mut dyn FsNode, FsError> {
Err(FsError::InvalidArgument)
}
}
pub trait FsNode: Debug {
fn inode(&self) -> usize;
fn name(&self) -> &str;
fn permissions(&self) -> u16;
fn try_root(&mut self) -> Option<&mut dyn FsRoot> {
None
}
fn try_directory(&mut self) -> Option<&mut dyn FsDirectory> {
None
}
fn try_file(&mut self) -> Option<&mut dyn FsFile> {
None
}
}
pub trait FsDirectory: FsNode + FsReadDir + FsWriteDir {}
pub trait FsFile: FsNode {
fn open(&mut self) -> Result<&mut dyn FsFileHandle, FsError>;
}
pub trait FsRoot: Send + FsDirectory + Debug {}
pub trait FsFileHandle: Debug {
fn raw_read(&mut self, offset: usize, length: Option<usize>) -> Result<Vec<u8>, FsError>;
fn raw_write(&mut self, offset: usize, data: &[u8]) -> Result<(), FsError>;
}
#[derive(Default)]
pub struct FilesystemMountpoint {
pub path: StaticCollection<Option<Spur>>,
pub root: Option<UnsafeContainer<Box<dyn FsRoot>>>,
}
impl FilesystemMountpoint {
pub fn path_vec(&self) -> Vec<&'static str> {
self.path
.as_slice()
.iter()
.map(|x| x.unwrap_or(INTERNED.get().get_or_intern("[unknown]")))
.map(|x| INTERNED.resolve(&x))
.collect::<Vec<&str>>()
}
pub fn path_string(&self) -> String {
let segs = self
.path_vec()
.iter()
.map(|x| String::from(*x))
.collect::<Vec<String>>();
path::join(&segs)
}
}
impl Debug for FilesystemMountpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FilesystemMountpoint")
.field("path", &self.path_vec())
.finish()
}
}
#[derive(Debug)]
pub struct FilesystemBase {
pub mountpoints: StaticCollection<UnsafeContainer<FilesystemMountpoint>>,
}
impl FilesystemBase {
pub fn new() -> Self {
Self {
mountpoints: StaticCollection::new(),
}
}
pub fn mount(&mut self, path: &[&str], root: Box<dyn FsRoot>) -> Result<(), MountError> {
let mut path_segments: StaticCollection<Option<Spur>> = StaticCollection::new();
for seg in path.into_iter() {
path_segments.push(Some(INTERNED.get().get_or_intern(seg)));
}
let mountpoint = FilesystemMountpoint {
path: path_segments,
root: Some(UnsafeContainer::new(root)),
};
trace!("mountpoint={:?}", &mountpoint);
self.mountpoints.push(UnsafeContainer::new(mountpoint));
Ok(())
}
pub fn node_at_path<'a>(&mut self, path: &[&str]) -> Result<&'a mut (dyn FsNode), FsError> {
let path = crate::path::split(&crate::path::join(
&path
.iter()
.map(|x| String::from(*x))
.collect::<Vec<String>>(),
));
let mut closest: Option<&UnsafeContainer<FilesystemMountpoint>> = None;
'ep: for fs in self.mountpoints.as_slice().iter() {
if fs.get().path_vec().is_empty() {
trace!("fs={:?}", fs);
closest = Some(fs);
break 'ep;
}
}
let mut xsc = 0usize;
for fs in self.mountpoints.as_slice().iter() {
let pathvec = fs.get().path_vec();
let mut startcount = 0usize;
'uidx: for (unit, idx) in pathvec.iter().zip(0..) {
if path.len() >= idx && &path[idx] == unit {
startcount += 1;
} else {
break 'uidx;
}
}
if startcount > xsc {
trace!("startcount={:?} xsc={:?} fs={:?}", startcount, xsc, fs);
closest = Some(fs);
xsc = startcount;
}
}
if closest.is_none() {
trace!("couldn't find a mountpoint close to {:?}", &path);
return Err(FsError::FileNotFound);
}
let mountpoint = closest.unwrap();
let path_remaining = {
if mountpoint.get().path.as_slice().is_empty() {
path
} else {
let (_, r) = path.split_at(mountpoint.get().path_vec().len());
r.to_vec()
}
};
trace!(
"mountpoint={:?} path_remaining={:?}",
mountpoint,
path_remaining
);
let mount_root = match &mountpoint.get().root {
Some(root) => root.get().as_mut() as &mut dyn FsNode,
None => {
return Err(FsError::FilesystemRootError);
}
};
match traverse_node(mount_root, path_remaining.clone(), false) {
Some(node) => Ok(node),
None => Err(FsError::FileNotFound),
}
}
}
pub fn traverse_node<'x>(
root: &'x mut dyn FsNode,
mut subpath: Vec<String>,
ignore_root: bool,
) -> Option<&'x mut dyn FsNode> {
subpath.reverse();
let root_inode = root.inode();
let current_node: UnsafeContainer<&'x mut dyn FsNode> = UnsafeContainer::new(root);
'fsearch: while let Some(path_seg) = subpath.pop() {
if let Some(dir) = current_node.get().try_directory() {
if let Ok(rd) = dir.readdir() {
for new in rd {
if new.name() == path_seg {
trace!("found next node, name={:?}", path_seg);
current_node.replace(new);
continue 'fsearch;
}
}
}
}
if subpath.is_empty() {
break 'fsearch;
}
}
let node = current_node.into_inner();
if ignore_root && node.try_root().is_some() && node.inode() == root_inode {
trace!("ignore_root set, returning None");
return None;
} else {
trace!("we've got our node, returning {:?}", node);
return Some(node);
}
}