1use core::{mem, slice};
2
3use crate::{data::*, error::*, flag::*, number::*, scheme::*, CallerCtx, OpenResult};
4
5pub trait Scheme {
6 fn handle(&self, packet: &mut Packet) {
7 let res = match packet.a {
8 SYS_OPEN => {
9 if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
10 convert_in_scheme_handle(
11 packet,
12 self.xopen(path, packet.d, &CallerCtx::from_packet(&packet)),
13 )
14 } else {
15 Err(Error::new(EINVAL))
16 }
17 }
18 SYS_RMDIR => {
19 if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
20 self.rmdir(path, packet.uid, packet.gid)
21 } else {
22 Err(Error::new(EINVAL))
23 }
24 }
25 SYS_UNLINK => {
26 if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
27 self.unlink(path, packet.uid, packet.gid)
28 } else {
29 Err(Error::new(EINVAL))
30 }
31 }
32
33 SYS_DUP => convert_in_scheme_handle(
34 packet,
35 self.xdup(
36 packet.b,
37 unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) },
38 &CallerCtx::from_packet(&packet),
39 ),
40 ),
41 SYS_READ => self.read(packet.b, unsafe {
42 slice::from_raw_parts_mut(packet.c as *mut u8, packet.d)
43 }),
44 SYS_WRITE => self.write(packet.b, unsafe {
45 slice::from_raw_parts(packet.c as *const u8, packet.d)
46 }),
47 SYS_LSEEK => self
48 .seek(packet.b, packet.c as isize, packet.d)
49 .map(|o| o as usize),
50 SYS_FCHMOD => self.fchmod(packet.b, packet.c as u16),
51 SYS_FCHOWN => self.fchown(packet.b, packet.c as u32, packet.d as u32),
52 SYS_FCNTL => self.fcntl(packet.b, packet.c, packet.d),
53 SYS_FEVENT => self
54 .fevent(packet.b, EventFlags::from_bits_truncate(packet.c))
55 .map(|f| f.bits()),
56 SYS_FLINK => {
57 if let Some(path) = unsafe { str_from_raw_parts(packet.c as *const u8, packet.d) } {
58 self.flink(packet.b, path, packet.uid, packet.gid)
59 } else {
60 Err(Error::new(EINVAL))
61 }
62 }
63 SYS_FPATH => self.fpath(packet.b, unsafe {
64 slice::from_raw_parts_mut(packet.c as *mut u8, packet.d)
65 }),
66 SYS_FRENAME => {
67 if let Some(path) = unsafe { str_from_raw_parts(packet.c as *const u8, packet.d) } {
68 self.frename(packet.b, path, packet.uid, packet.gid)
69 } else {
70 Err(Error::new(EINVAL))
71 }
72 }
73 SYS_FSTAT => {
74 if packet.d >= mem::size_of::<Stat>() {
75 self.fstat(packet.b, unsafe { &mut *(packet.c as *mut Stat) })
76 } else {
77 Err(Error::new(EFAULT))
78 }
79 }
80 SYS_FSTATVFS => {
81 if packet.d >= mem::size_of::<StatVfs>() {
82 self.fstatvfs(packet.b, unsafe { &mut *(packet.c as *mut StatVfs) })
83 } else {
84 Err(Error::new(EFAULT))
85 }
86 }
87 SYS_FSYNC => self.fsync(packet.b),
88 SYS_FTRUNCATE => self.ftruncate(packet.b, packet.c),
89 SYS_FUTIMENS => {
90 if packet.d >= mem::size_of::<TimeSpec>() {
91 self.futimens(packet.b, unsafe {
92 slice::from_raw_parts(
93 packet.c as *const TimeSpec,
94 packet.d / mem::size_of::<TimeSpec>(),
95 )
96 })
97 } else {
98 Err(Error::new(EFAULT))
99 }
100 }
101 SYS_CLOSE => self.close(packet.b),
102
103 KSMSG_MMAP_PREP => self.mmap_prep(
104 packet.b,
105 u64::from(packet.uid) | (u64::from(packet.gid) << 32),
106 packet.c,
107 MapFlags::from_bits_truncate(packet.d),
108 ),
109 KSMSG_MUNMAP => self.munmap(
110 packet.b,
111 u64::from(packet.uid) | (u64::from(packet.gid) << 32),
112 packet.c,
113 MunmapFlags::from_bits_truncate(packet.d),
114 ),
115
116 _ => Err(Error::new(ENOSYS)),
117 };
118
119 packet.a = Error::mux(res);
120 }
121
122 #[allow(unused_variables)]
125 fn open(&self, path: &str, flags: usize, uid: u32, gid: u32) -> Result<usize> {
126 Err(Error::new(ENOENT))
127 }
128 #[allow(unused_variables)]
129 fn xopen(&self, path: &str, flags: usize, ctx: &CallerCtx) -> Result<OpenResult> {
130 convert_to_this_scheme(self.open(path, flags, ctx.uid, ctx.gid))
131 }
132
133 #[allow(unused_variables)]
134 fn chmod(&self, path: &str, mode: u16, uid: u32, gid: u32) -> Result<usize> {
135 Err(Error::new(ENOENT))
136 }
137
138 #[allow(unused_variables)]
139 fn rmdir(&self, path: &str, uid: u32, gid: u32) -> Result<usize> {
140 Err(Error::new(ENOENT))
141 }
142
143 #[allow(unused_variables)]
144 fn unlink(&self, path: &str, uid: u32, gid: u32) -> Result<usize> {
145 Err(Error::new(ENOENT))
146 }
147
148 #[allow(unused_variables)]
150 fn dup(&self, old_id: usize, buf: &[u8]) -> Result<usize> {
151 Err(Error::new(EBADF))
152 }
153
154 #[allow(unused_variables)]
155 fn xdup(&self, old_id: usize, buf: &[u8], ctx: &CallerCtx) -> Result<OpenResult> {
156 convert_to_this_scheme(self.dup(old_id, buf))
157 }
158
159 #[allow(unused_variables)]
160 fn read(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
161 Err(Error::new(EBADF))
162 }
163
164 #[allow(unused_variables)]
165 fn write(&self, id: usize, buf: &[u8]) -> Result<usize> {
166 Err(Error::new(EBADF))
167 }
168
169 #[allow(unused_variables)]
170 fn seek(&self, id: usize, pos: isize, whence: usize) -> Result<isize> {
171 Err(Error::new(EBADF))
172 }
173
174 #[allow(unused_variables)]
175 fn fchmod(&self, id: usize, mode: u16) -> Result<usize> {
176 Err(Error::new(EBADF))
177 }
178
179 #[allow(unused_variables)]
180 fn fchown(&self, id: usize, uid: u32, gid: u32) -> Result<usize> {
181 Err(Error::new(EBADF))
182 }
183
184 #[allow(unused_variables)]
185 fn fcntl(&self, id: usize, cmd: usize, arg: usize) -> Result<usize> {
186 Err(Error::new(EBADF))
187 }
188
189 #[allow(unused_variables)]
190 fn fevent(&self, id: usize, flags: EventFlags) -> Result<EventFlags> {
191 Err(Error::new(EBADF))
192 }
193
194 #[allow(unused_variables)]
195 fn flink(&self, id: usize, path: &str, uid: u32, gid: u32) -> Result<usize> {
196 Err(Error::new(EBADF))
197 }
198
199 #[allow(unused_variables)]
200 fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
201 Err(Error::new(EBADF))
202 }
203
204 #[allow(unused_variables)]
205 fn frename(&self, id: usize, path: &str, uid: u32, gid: u32) -> Result<usize> {
206 Err(Error::new(EBADF))
207 }
208
209 #[allow(unused_variables)]
210 fn fstat(&self, id: usize, stat: &mut Stat) -> Result<usize> {
211 Err(Error::new(EBADF))
212 }
213
214 #[allow(unused_variables)]
215 fn fstatvfs(&self, id: usize, stat: &mut StatVfs) -> Result<usize> {
216 Err(Error::new(EBADF))
217 }
218
219 #[allow(unused_variables)]
220 fn fsync(&self, id: usize) -> Result<usize> {
221 Err(Error::new(EBADF))
222 }
223
224 #[allow(unused_variables)]
225 fn ftruncate(&self, id: usize, len: usize) -> Result<usize> {
226 Err(Error::new(EBADF))
227 }
228
229 #[allow(unused_variables)]
230 fn futimens(&self, id: usize, times: &[TimeSpec]) -> Result<usize> {
231 Err(Error::new(EBADF))
232 }
233
234 #[allow(unused_variables)]
235 fn close(&self, id: usize) -> Result<usize> {
236 Err(Error::new(EBADF))
237 }
238
239 #[allow(unused_variables)]
240 fn mmap_prep(&self, id: usize, offset: u64, size: usize, flags: MapFlags) -> Result<usize> {
241 Err(Error::new(EOPNOTSUPP))
242 }
243
244 #[allow(unused_variables)]
245 fn munmap(&self, id: usize, offset: u64, size: usize, flags: MunmapFlags) -> Result<usize> {
246 Err(Error::new(EOPNOTSUPP))
247 }
248}