From 10968423e81e1987f7e7ed4baede5b14452307b9 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 30 Sep 2024 13:31:51 -0500 Subject: [PATCH] Add seek function --- src/lib.rs | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 72aa322..d4e3078 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,21 @@ use std::{ - borrow::Cow, - boxed::Box, - os::mikros::ipc::{ + borrow::Cow, boxed::Box, os::mikros::ipc::{ self, rpc::{self, CallId, IncomingCall}, - }, - vec::Vec, + }, vec::Vec }; use bitflags::bitflags; use parking_lot::RwLock; use serde::{Deserialize, Serialize}; +#[derive(Deserialize, Serialize)] +pub enum SeekFrom { + Start(u64), + End(i64), + Current(i64), +} + bitflags! { #[derive(Serialize, Deserialize, Clone, Copy, Debug)] pub struct PollEvents: u16 { @@ -33,8 +37,8 @@ static SERVER: RwLock>> = RwLock::new(None); const PROTO: u16 = 1; pub trait Server: Send + Sync { - fn read<'a>(&'a self, fd: u64, pos: u64, len: usize) -> Result, ()>; - fn write(&self, fd: u64, pos: u64, data: &[u8]) -> Result<(), ()>; + fn read<'a>(&'a self, fd: u64, len: usize) -> Result, ()>; + fn write(&self, fd: u64, data: &[u8]) -> Result<(), ()>; fn close(&self, fd: u64); fn size(&self, fd: u64) -> Option; fn dup(&self, fd: u64) -> Option; @@ -45,6 +49,7 @@ pub trait Server: Send + Sync { None } fn cancel_poll(&self, _poll_id: u64) {} + fn seek(&self, fd: u64, pos: SeekFrom) -> u64; } #[derive(Copy, Clone)] @@ -114,26 +119,26 @@ impl Client { Self(pid) } - pub fn read(self, fd: u64, pos: u64, len: usize) -> Result, ()> { + pub fn read(self, fd: u64, len: usize) -> Result, ()> { postcard::from_bytes( &rpc::send_call( self.0, PROTO, 0, - &postcard::to_stdvec(&(fd, pos, len)).unwrap(), + &postcard::to_stdvec(&(fd, len)).unwrap(), ) .get_return(), ) .unwrap() } - pub fn write(self, fd: u64, pos: u64, data: &[u8]) -> Result<(), ()> { + pub fn write(self, fd: u64, data: &[u8]) -> Result<(), ()> { postcard::from_bytes( &rpc::send_call( self.0, PROTO, 1, - &postcard::to_stdvec(&(fd, pos, data)).unwrap(), + &postcard::to_stdvec(&(fd, data)).unwrap(), ) .get_return(), ) @@ -179,6 +184,13 @@ impl Client { call_id, } } + + pub fn seek(&self, fd: u64, pos: SeekFrom) -> u64 { + postcard::from_bytes( + &rpc::send_call(self.0, PROTO, 8, &postcard::to_stdvec(&(fd, pos)).unwrap()).get_return(), + ) + .unwrap() + } } pub fn register_server(server: Box) { @@ -196,16 +208,16 @@ fn callback(call: IncomingCall) { let server = server_lock.as_ref().unwrap(); match call.func { 0 => { - let (fd, pos, len) = postcard::from_bytes(call.args()).unwrap(); - let ret = server.read(fd, pos, len); + let (fd, len) = postcard::from_bytes(call.args()).unwrap(); + let ret = server.read(fd, len); let ser_size = postcard::experimental::serialized_size(&ret).unwrap(); let mut buf = call.make_return_buf(ser_size); postcard::to_slice(&ret, &mut buf[..]).unwrap(); buf.send(); } 1 => { - let (fd, pos, data): (u64, u64, Vec) = postcard::from_bytes(call.args()).unwrap(); - let ret = postcard::to_stdvec(&server.write(fd, pos, &data)).unwrap(); + let (fd, data): (u64, Vec) = postcard::from_bytes(call.args()).unwrap(); + let ret = postcard::to_stdvec(&server.write(fd, &data)).unwrap(); call.send_return(&ret); } 2 => { @@ -240,6 +252,11 @@ fn callback(call: IncomingCall) { let ret = postcard::to_stdvec(&server.cancel_poll(poll_id)).unwrap(); call.send_return(&ret); } + 8 => { + let (fd, pos) = postcard::from_bytes(call.args()).unwrap(); + let ret = postcard::to_stdvec(&server.seek(fd, pos)).unwrap(); + call.send_return(&ret); + } _ => (), } }