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
use crate::environment::is_uhyve;
use crate::synch::spinlock::Spinlock;
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use core::ops::Deref;
pub static FILESYSTEM: Spinlock<Filesystem> = Spinlock::new(Filesystem::new());
pub struct Filesystem {
mounts: BTreeMap<String, Box<dyn PosixFileSystem + Send>>,
files: BTreeMap<u64, Box<dyn PosixFile + Send>>,
}
impl Filesystem {
pub const fn new() -> Self {
Self {
mounts: BTreeMap::new(),
files: BTreeMap::new(),
}
}
fn assign_new_fd(&self) -> u64 {
if let Some((fd, _)) = self.files.iter().next_back() {
fd + 1
} else {
3
}
}
fn add_file(&mut self, file: Box<dyn PosixFile + Send>) -> u64 {
let fd = self.assign_new_fd();
self.files.insert(fd, file);
fd
}
fn parse_path<'a, 'b>(
&'a self,
path: &'b str,
) -> Result<(&'a (dyn PosixFileSystem + Send), &'b str), FileError> {
if path.starts_with('/') {
let mut pathsplit = path.splitn(3, '/');
pathsplit.next();
let mount = pathsplit.next().unwrap();
let internal_path = pathsplit.next().unwrap();
if let Some(fs) = self.mounts.get(mount) {
return Ok((fs.deref(), internal_path));
}
warn!(
"Trying to open file on non-existing mount point '{}'!",
mount
);
} else {
let mut pathsplit = path.splitn(3, '/');
let mount = if !is_uhyve() {
option_env!("HERMIT_WD").unwrap_or("root")
} else {
"."
};
let internal_path = pathsplit.next().unwrap();
debug!(
"Assume that the directory '{}' is used as mount point!",
mount
);
if let Some(fs) = self.mounts.get(mount) {
return Ok((fs.deref(), internal_path));
}
info!(
"Trying to open file on non-existing mount point '{}'!",
mount
);
}
Err(FileError::ENOENT())
}
pub fn open(&mut self, path: &str, perms: FilePerms) -> Result<u64, FileError> {
debug!("Opening file {} {:?}", path, perms);
let (fs, internal_path) = self.parse_path(path)?;
let file = fs.open(internal_path, perms)?;
Ok(self.add_file(file))
}
pub fn close(&mut self, fd: u64) {
debug!("Closing fd {}", fd);
if let Some(file) = self.files.get_mut(&fd) {
file.close().unwrap();
}
self.files.remove(&fd);
}
pub fn unlink(&mut self, path: &str) -> Result<(), FileError> {
info!("Unlinking file {}", path);
let (fs, internal_path) = self.parse_path(path)?;
fs.unlink(internal_path)?;
Ok(())
}
pub fn mount(
&mut self,
mntpath: &str,
mntobj: Box<dyn PosixFileSystem + Send>,
) -> Result<(), ()> {
info!("Mounting {}", mntpath);
if mntpath.contains('/') {
warn!(
"Trying to mount at '{}', but slashes in name are not supported!",
mntpath
);
return Err(());
}
if self.mounts.contains_key(mntpath) {
warn!("Mountpoint already exists!");
return Err(());
}
self.mounts.insert(mntpath.to_owned(), mntobj);
Ok(())
}
pub fn fd_op(&mut self, fd: u64, f: impl FnOnce(&mut Box<dyn PosixFile + Send>)) {
f(self.files.get_mut(&fd).unwrap());
}
}
#[derive(Debug)]
pub enum FileError {
ENOENT(),
ENOSYS(),
}
pub trait PosixFileSystem {
fn open(&self, _path: &str, _perms: FilePerms) -> Result<Box<dyn PosixFile + Send>, FileError>;
fn unlink(&self, _path: &str) -> Result<(), FileError>;
}
pub trait PosixFile {
fn close(&mut self) -> Result<(), FileError>;
fn read(&mut self, len: u32) -> Result<Vec<u8>, FileError>;
fn write(&mut self, buf: &[u8]) -> Result<u64, FileError>;
fn lseek(&mut self, offset: isize, whence: SeekWhence) -> Result<usize, FileError>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct FilePerms {
pub write: bool,
pub creat: bool,
pub excl: bool,
pub trunc: bool,
pub append: bool,
pub directio: bool,
pub raw: u32,
pub mode: u32,
}
pub enum SeekWhence {
Set,
Cur,
End,
}