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
use core::usize;
use alloc::boxed::Box;
use kcore::{
chan::{ChanId, ChanKind, Dirent},
dev::Device,
error::{Error, Result},
};
#[derive(Default)]
pub struct Root {}
const ROOT_DIRS: &[&str] = &["boot", "root", "proc", "env", "dev"];
#[async_trait::async_trait_try]
impl Device for Root {
async fn shutdown(self)
where
Self: Sized,
{
}
async fn attach(&self, aname: &[u8]) -> Result<ChanId>
where
Self: Sized,
{
Ok(ChanId {
path: 0,
version: b'/' as u32,
kind: ChanKind::Dir,
})
}
async fn open(
&self,
dir: &ChanId,
name: &[u8],
create_dir: Option<bool>,
) -> Result<Option<ChanId>> {
if name.is_empty() {
Ok(Some(dir.clone()))
} else if create_dir.is_some() {
Ok(None)
} else if dir.path != 0 {
Ok(None)
} else {
debug_assert_eq!(dir.kind, ChanKind::Dir);
debug_assert_eq!(dir.path, 0);
Ok(ROOT_DIRS
.iter()
.enumerate()
.find(|(i, s)| s.as_bytes() == name)
.map(|(i, s)| ChanId {
path: i as u64 + 1,
version: 0,
kind: ChanKind::Dir,
}))
}
}
async fn close(&self, c: ChanId) {}
async fn remove(&self, c: &ChanId) -> Result<bool> {
Err(Error::BadRequest("remove file of devroot"))
}
async fn truncate(&self, c: &ChanId, size: usize) -> Result<usize> {
unimplemented!()
}
async fn stat(&self, c: &ChanId) -> Result<Dirent> {
todo!()
}
async fn wstat(&self, c: &ChanId, dirent: &Dirent) -> Result<()> {
todo!()
}
async fn read(&self, c: &ChanId, buf: &mut [u8], off: usize) -> Result<usize> {
unimplemented!()
}
async fn write(&self, c: &ChanId, buf: &[u8], off: usize) -> Result<usize> {
unimplemented!()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fat::FAT;
use alloc::sync::Arc;
use kcore::chan::Chan;
use ksched::task;
use ktest::fs::{gen_fat32img, FileDisk};
#[test]
fn test_mount() {
let (dir, img_path) = gen_fat32img();
let disk = Arc::new(FileDisk::new(img_path));
task::spawn(async move {
let devroot = Arc::new(Root::default());
let root = Chan::attach(devroot, b"").await.unwrap();
assert_eq!(root.path().await.unwrap(), b"/");
assert!(root.open(b"dev", Some(true)).await.unwrap().is_none());
assert!(root.open(b"dev", Some(false)).await.unwrap().is_none());
assert!(root.open(b"x", Some(true)).await.unwrap().is_none());
assert!(root.open(b"x", Some(false)).await.unwrap().is_none());
let root2 = root.open(b"", None).await.unwrap().unwrap();
root2.close().await;
let root_dev = root.open(b"dev", None).await.unwrap().unwrap();
assert_eq!(root_dev.path().await.unwrap(), b"/dev");
let disk_root = Chan::attach(disk, b"").await.unwrap();
let fs = Arc::new(FAT::new(50, 100, &disk_root).await.unwrap());
disk_root.close().await;
let fs_root = Chan::attach(fs.clone(), b"").await.unwrap();
root.mount(&fs_root).await.unwrap();
assert_eq!(root.bind(&fs_root).await.is_err(), true);
fs_root.close().await;
let src_dir = root.open(b"src", None).await.unwrap().unwrap();
assert_eq!(src_dir.path().await.unwrap(), b"/src");
src_dir.close().await;
root.close().await;
root_dev.close().await;
let fs = Arc::try_unwrap(fs).unwrap();
fs.shutdown().unwrap().await;
})
.unwrap();
task::run();
drop(dir);
}
}