-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.rs
383 lines (328 loc) · 11.9 KB
/
mod.rs
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
372
373
374
375
376
377
378
379
380
381
382
383
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::hash::{BuildHasher, Hash, Hasher};
use mr_mime::Mime;
use syscall::{Error, ENOTDIR};
use unix_path::{Path, PathBuf};
use crate::common::hash_map::HashMap;
// going one-further than most other implementations to ensure this never overflows
#[allow(non_camel_case_types)]
pub type time_t = i128;
pub type FileData = Vec<u8>;
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntryKind<'a> {
Directory(Arc<HashMap<Properties<'a>, Arc<Entry<'a>>>>),
File(FileData),
Root(Arc<RootEntry<'a>>),
}
impl Hash for EntryKind<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::Directory(map) => {
for (properties, entry) in map.iter() {
properties.hash(state);
entry.hash(state);
}
}
Self::File(data) => {
data.hash(state);
}
Self::Root(root) => {
root.hash(state);
}
}
}
}
pub fn new_map_shorthand<'a>() -> HashMap<Properties<'a>, Arc<Entry<'a>>> {
HashMap::<Properties<'a>, Arc<Entry<'a>>>::default()
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Entry<'a> {
kind: EntryKind<'a>,
checksum: u64,
parent: Option<EntryKind<'a>>,
}
impl<'a> Entry<'a> {
pub fn new(kind: EntryKind<'a>, parent: Option<EntryKind<'a>>) -> Self {
let mut new = Self {
kind,
checksum: 0x0,
parent: parent.clone(),
};
if let Some(parent) = parent {
match parent {
EntryKind::Directory(dir) => {
new.checksum = dir.hasher().hash_one(&new);
}
EntryKind::File(_) => panic!("Parent must be a directory"),
EntryKind::Root(root) => {
let root = Arc::clone(&root);
let root = root.get_root_dir();
if let EntryKind::Directory(dir) = root.kind {
new.checksum = dir.hasher().hash_one(&new);
} else {
unreachable!("always a directory here")
}
}
}
};
new
}
pub fn parent(&self) -> Option<EntryKind> {
self.parent.clone()
}
pub fn mkdir(&self, name: String, timestamp: time_t) -> syscall::Result<Self> {
match self.kind.clone() {
EntryKind::Directory(mut dir) => {
let parent = Some(EntryKind::Directory(dir.clone()));
let new_map_inner = new_map_shorthand();
let new_map = Arc::new(new_map_inner);
let props = Properties::new(
name,
EntryKind::Directory(dir.clone()),
None,
0o777,
String::from("root"), // TODO: users
timestamp,
timestamp,
String::from("root"), // TODO: users
);
let kind = EntryKind::Directory(new_map);
let checksum = dir.hasher().hash_one(&kind);
let to_insert = Self {
kind,
checksum,
parent,
};
let mut_dir = Arc::make_mut(&mut dir);
mut_dir.insert(props.clone(), Arc::new(to_insert));
let ret = Arc::make_mut(Arc::make_mut(&mut dir).get_mut(&props).unwrap());
Ok(ret.clone())
}
EntryKind::Root(mut root) => {
if let EntryKind::Directory(ref mut dir) = Arc::make_mut(&mut root).dir.kind {
let parent = Some(EntryKind::Directory(dir.clone()));
let new_map_inner = new_map_shorthand();
let new_map = Arc::new(new_map_inner);
let props = Properties::new(
name,
EntryKind::Directory(dir.clone()),
None,
0o777,
String::from("root"), // TODO: users
timestamp,
timestamp,
String::from("root"), // TODO: users
);
let kind = EntryKind::Directory(new_map);
let checksum = dir.hasher().hash_one(&kind);
let to_insert = Self {
kind,
checksum,
parent,
};
let mut_dir = Arc::make_mut(dir);
mut_dir.insert(props.clone(), Arc::new(to_insert));
let ret = dir.get(&props).cloned().unwrap();
Ok(ret.clone().as_ref().clone())
} else {
unreachable!("root entry is always a directory")
}
}
EntryKind::File(_) => Err(Error::new(ENOTDIR)),
}
}
pub fn create_file(
&self,
mime: Mime<'a>,
name: String,
timestamp: time_t,
data: FileData,
) -> syscall::Result<Self> {
match self.kind.clone() {
EntryKind::Directory(ref mut dir) => {
let parent = EntryKind::Directory(dir.clone());
// borrow checker
let duplicate = parent.clone();
let props = Properties::new(
name,
parent,
Some(mime),
0o777, // TODO: users and permissions
String::from("root"), // TODO: users and permissions
timestamp,
timestamp,
String::from("root"), // TODO: users and permissions
);
let mut props_arc = Arc::new(props);
// borrow checker
let parent = duplicate;
let kind = EntryKind::File(data);
let checksum = dir.hasher().hash_one(&kind);
let to_insert = Self {
kind,
checksum,
parent: Some(parent),
};
Arc::make_mut(dir)
.insert(Arc::make_mut(&mut props_arc).clone(), Arc::new(to_insert));
let ret = dir.get(Arc::make_mut(&mut props_arc)).cloned().unwrap();
Ok(ret.as_ref().clone())
}
EntryKind::File(_) => Err(Error::new(ENOTDIR)), // Not a directory
EntryKind::Root(ref mut root) => {
// TODO: throw EACCES if a non-root user attempts this
if let EntryKind::Directory(ref mut dir) = Arc::make_mut(root).dir.kind {
let parent = EntryKind::Directory(dir.clone());
// borrow checker
let duplicate = parent.clone();
let props = Properties::new(
name,
parent,
Some(mime),
0o777, // TODO: users and permissions
String::from("root"), // TODO: users and permissions
timestamp,
timestamp,
String::from("root"), // TODO: users and permissions
);
let mut props_arc = Arc::new(props);
// borrow checker
let parent = duplicate;
let kind = EntryKind::File(data);
let checksum = dir.hasher().hash_one(&kind);
let to_insert = Self {
kind,
checksum,
parent: Some(parent),
};
Arc::make_mut(dir)
.insert(Arc::make_mut(&mut props_arc).clone(), Arc::new(to_insert));
let ret = dir.get(Arc::make_mut(&mut props_arc)).cloned().unwrap();
Ok(ret.as_ref().clone())
} else {
unreachable!("root entry is always a directory")
}
} // TODO: give root an exception to this
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub struct Properties<'a> {
name: String,
full_path: PathBuf,
entry_kind: EntryKind<'a>,
mime_type: Option<Mime<'a>>,
mode: u32,
created_by: String,
date_created: time_t,
date_modified: time_t,
owner: String,
}
#[allow(clippy::too_many_arguments)]
impl<'a> Properties<'a> {
pub fn new(
name: String,
entry_kind: EntryKind<'a>,
mime_type: Option<Mime<'a>>,
mode: u32,
created_by: String,
date_created: time_t,
date_modified: time_t,
owner: String,
) -> Self {
// borrow checker
let cloned = name.clone();
Self {
name,
full_path: Path::new(cloned.as_str()).to_owned(),
entry_kind,
mime_type,
mode,
created_by,
date_created,
date_modified,
owner,
}
}
}
// Pave the way for (partition) formatting
pub fn root_entry_bytes(entry: RootEntry) -> &'static mut [u8] {
let map_addr = &entry as *const _ as usize as u64;
unsafe {
core::slice::from_raw_parts_mut::<'static>(
map_addr as *mut u8,
core::mem::size_of::<RootEntry>(),
)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[allow(dead_code)]
pub struct RootEntry<'a> {
magic: u32,
system_clock: time_t,
entry_count: usize,
checksum: u64,
dir: Entry<'a>,
}
impl<'a> RootEntry<'a> {
pub fn new(timestamp: time_t) -> Self {
let mut root_map_inner = new_map_shorthand();
let root_map = Arc::new(root_map_inner.clone());
let root_props = Properties::new(
String::from("/"),
EntryKind::Directory(Arc::clone(&root_map)),
None,
0o777,
String::from("root"),
timestamp,
timestamp,
String::from("root"),
);
root_map_inner.insert(
root_props.clone(),
Arc::new(Entry::new(
EntryKind::Directory(Arc::clone(&root_map)),
None,
)),
);
drop(root_map);
let new_root_map = Arc::new(root_map_inner);
let old_entry = Entry::new(EntryKind::Directory(new_root_map.clone()), None);
let mut new_entry_parent = Self {
magic: 0x90a7cafe,
system_clock: timestamp,
entry_count: Arc::strong_count(&new_root_map),
checksum: new_root_map.hasher().hash_one(&old_entry),
dir: old_entry,
};
let new_entry = Entry::new(
EntryKind::Directory(new_root_map.clone()),
Some(EntryKind::Root(Arc::new(new_entry_parent.clone()))),
);
new_entry_parent.dir = new_entry.clone();
// keep these values up-to-date
new_entry_parent.dir.parent = Some(EntryKind::Root(Arc::new(new_entry_parent.clone())));
new_entry_parent.dir.kind = EntryKind::Directory(new_root_map.clone());
new_entry_parent.checksum = new_root_map.hasher().hash_one(&new_entry);
// shadow this
let new_entry = new_entry_parent.dir.clone();
// keep HashMap up-to-date
if let EntryKind::Directory(ref mut dir) = &mut new_entry_parent.dir.kind {
Arc::get_mut(dir).unwrap().remove_entry(&root_props);
Arc::get_mut(dir)
.unwrap()
.insert(root_props, Arc::new(new_entry));
} else {
unreachable!()
}
new_entry_parent
}
pub fn get_root_dir(&self) -> Entry {
assert_eq!(self.magic, 0x90a7cafe); // TODO: find a compiler-level way to do this
self.dir.clone()
}
}