Use xopen and xdup in Scheme::handle.
This commit is contained in:
@@ -16,6 +16,7 @@ sed 's/trait Scheme/trait SchemeBlock/' scheme.rs \
|
||||
| sed 's/Ok(0)/Ok(Some(0))/g' \
|
||||
| sed 's/Result<\([^>]\+\)>/Result<Option<\1>>/g' \
|
||||
| sed 's/convert_to_this_scheme/convert_to_this_scheme_block/g' \
|
||||
| sed 's/convert_in_scheme_handle/convert_in_scheme_handle_block/g' \
|
||||
> scheme_block.rs
|
||||
|
||||
echo "Generating SchemeBlockMut from SchemeBlock"
|
||||
|
||||
+30
-1
@@ -1,6 +1,6 @@
|
||||
use core::{slice, str};
|
||||
|
||||
use crate::Result;
|
||||
use crate::{Error, Result, EOPNOTSUPP, ESKMSG, Packet};
|
||||
|
||||
pub use self::scheme::Scheme;
|
||||
pub use self::scheme_mut::SchemeMut;
|
||||
@@ -30,9 +30,38 @@ pub enum OpenResult {
|
||||
OtherScheme { fd: usize },
|
||||
}
|
||||
|
||||
// TODO: Find a better solution than generate.sh
|
||||
pub(crate) fn convert_to_this_scheme(r: Result<usize>) -> Result<OpenResult> {
|
||||
r.map(|number| OpenResult::ThisScheme { number })
|
||||
}
|
||||
pub(crate) fn convert_to_this_scheme_block(r: Result<Option<usize>>) -> Result<Option<OpenResult>> {
|
||||
r.map(|o| o.map(|number| OpenResult::ThisScheme { number }))
|
||||
}
|
||||
pub(crate) fn convert_in_scheme_handle_block(_: &Packet, result: Result<Option<OpenResult>>) -> Result<Option<usize>> {
|
||||
match result {
|
||||
Ok(Some(OpenResult::ThisScheme { number })) => Ok(Some(number)),
|
||||
Ok(Some(OpenResult::OtherScheme { .. })) => Err(Error::new(EOPNOTSUPP)),
|
||||
Ok(None) => Ok(None),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
pub(crate) fn convert_in_scheme_handle(packet: &mut Packet, result: Result<OpenResult>) -> Result<usize> {
|
||||
match result {
|
||||
Ok(OpenResult::ThisScheme { number }) => Ok(number),
|
||||
Ok(OpenResult::OtherScheme { fd }) => {
|
||||
packet.b = fd;
|
||||
Err(Error::new(ESKMSG))
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
impl CallerCtx {
|
||||
pub fn from_packet(packet: &Packet) -> Self {
|
||||
Self {
|
||||
pid: packet.pid,
|
||||
uid: packet.uid,
|
||||
gid: packet.gid,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,9 @@ pub trait Scheme {
|
||||
fn handle(&self, packet: &mut Packet) {
|
||||
let res = match packet.a {
|
||||
SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
self.open(path, packet.d, packet.uid, packet.gid)
|
||||
} else {
|
||||
convert_in_scheme_handle(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet)))
|
||||
}
|
||||
else {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
@@ -27,7 +28,7 @@ pub trait Scheme {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
|
||||
SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_DUP => convert_in_scheme_handle(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))),
|
||||
SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }),
|
||||
SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o as usize),
|
||||
|
||||
@@ -12,8 +12,9 @@ pub trait SchemeBlock {
|
||||
fn handle(&self, packet: &Packet) -> Option<usize> {
|
||||
let res = match packet.a {
|
||||
SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
self.open(path, packet.d, packet.uid, packet.gid)
|
||||
} else {
|
||||
convert_in_scheme_handle_block(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet)))
|
||||
}
|
||||
else {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
@@ -27,7 +28,7 @@ pub trait SchemeBlock {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
|
||||
SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_DUP => convert_in_scheme_handle_block(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))),
|
||||
SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }),
|
||||
SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o.map(|o| o as usize)),
|
||||
|
||||
@@ -12,8 +12,9 @@ pub trait SchemeBlockMut {
|
||||
fn handle(&mut self, packet: &Packet) -> Option<usize> {
|
||||
let res = match packet.a {
|
||||
SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
self.open(path, packet.d, packet.uid, packet.gid)
|
||||
} else {
|
||||
convert_in_scheme_handle_block(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet)))
|
||||
}
|
||||
else {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
@@ -27,7 +28,7 @@ pub trait SchemeBlockMut {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
|
||||
SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_DUP => convert_in_scheme_handle_block(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))),
|
||||
SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }),
|
||||
SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o.map(|o| o as usize)),
|
||||
|
||||
@@ -12,8 +12,9 @@ pub trait SchemeMut {
|
||||
fn handle(&mut self, packet: &mut Packet) {
|
||||
let res = match packet.a {
|
||||
SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
self.open(path, packet.d, packet.uid, packet.gid)
|
||||
} else {
|
||||
convert_in_scheme_handle(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet)))
|
||||
}
|
||||
else {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } {
|
||||
@@ -27,7 +28,7 @@ pub trait SchemeMut {
|
||||
Err(Error::new(EINVAL))
|
||||
},
|
||||
|
||||
SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_DUP => convert_in_scheme_handle(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))),
|
||||
SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }),
|
||||
SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }),
|
||||
SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o as usize),
|
||||
|
||||
Reference in New Issue
Block a user