Change to seek function

This commit is contained in:
pjht 2024-09-30 13:31:51 -05:00
parent 8656e63674
commit b910ed388d
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -1,17 +1,21 @@
use std::{ use std::{
borrow::Cow, borrow::Cow, boxed::Box, os::mikros::ipc::{
boxed::Box,
os::mikros::ipc::{
self, self,
rpc::{self, CallId, IncomingCall}, rpc::{self, CallId, IncomingCall},
}, }, vec::Vec
vec::Vec,
}; };
use bitflags::bitflags; use bitflags::bitflags;
use parking_lot::RwLock; use parking_lot::RwLock;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub enum SeekFrom {
Start(u64),
End(i64),
Current(i64),
}
bitflags! { bitflags! {
#[derive(Serialize, Deserialize, Clone, Copy, Debug)] #[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub struct PollEvents: u16 { pub struct PollEvents: u16 {
@ -33,8 +37,8 @@ static SERVER: RwLock<Option<Box<dyn Server>>> = RwLock::new(None);
const PROTO: u16 = 1; const PROTO: u16 = 1;
pub trait Server: Send + Sync { pub trait Server: Send + Sync {
fn read<'a>(&'a self, fd: u64, pos: u64, len: usize) -> Result<Cow<'a, [u8]>, ()>; fn read<'a>(&'a self, fd: u64, len: usize) -> Result<Cow<'a, [u8]>, ()>;
fn write(&self, fd: u64, pos: u64, data: &[u8]) -> Result<(), ()>; fn write(&self, fd: u64, data: &[u8]) -> Result<(), ()>;
fn close(&self, fd: u64); fn close(&self, fd: u64);
fn size(&self, fd: u64) -> Option<u64>; fn size(&self, fd: u64) -> Option<u64>;
fn dup(&self, fd: u64) -> Option<u64>; fn dup(&self, fd: u64) -> Option<u64>;
@ -45,6 +49,7 @@ pub trait Server: Send + Sync {
None None
} }
fn cancel_poll(&self, _poll_id: u64) {} fn cancel_poll(&self, _poll_id: u64) {}
fn seek(&self, fd: u64, pos: SeekFrom) -> u64;
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -114,26 +119,26 @@ impl Client {
Self(pid) Self(pid)
} }
pub fn read(self, fd: u64, pos: u64, len: usize) -> Result<Vec<u8>, ()> { pub fn read(self, fd: u64, len: usize) -> Result<Vec<u8>, ()> {
postcard::from_bytes( postcard::from_bytes(
&rpc::send_call( &rpc::send_call(
self.0, self.0,
PROTO, PROTO,
0, 0,
&postcard::to_stdvec(&(fd, pos, len)).unwrap(), &postcard::to_stdvec(&(fd, len)).unwrap(),
) )
.get_return(), .get_return(),
) )
.unwrap() .unwrap()
} }
pub fn write(self, fd: u64, pos: u64, data: &[u8]) -> Result<(), ()> { pub fn write(self, fd: u64, data: &[u8]) -> Result<(), ()> {
postcard::from_bytes( postcard::from_bytes(
&rpc::send_call( &rpc::send_call(
self.0, self.0,
PROTO, PROTO,
1, 1,
&postcard::to_stdvec(&(fd, pos, data)).unwrap(), &postcard::to_stdvec(&(fd, data)).unwrap(),
) )
.get_return(), .get_return(),
) )
@ -179,6 +184,13 @@ impl Client {
call_id, 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<dyn Server>) { pub fn register_server(server: Box<dyn Server>) {
@ -196,16 +208,16 @@ fn callback(call: IncomingCall) {
let server = server_lock.as_ref().unwrap(); let server = server_lock.as_ref().unwrap();
match call.func { match call.func {
0 => { 0 => {
let (fd, pos, len) = postcard::from_bytes(call.args()).unwrap(); let (fd, len) = postcard::from_bytes(call.args()).unwrap();
let ret = server.read(fd, pos, len); let ret = server.read(fd, len);
let ser_size = postcard::experimental::serialized_size(&ret).unwrap(); let ser_size = postcard::experimental::serialized_size(&ret).unwrap();
let mut buf = call.make_return_buf(ser_size); let mut buf = call.make_return_buf(ser_size);
postcard::to_slice(&ret, &mut buf[..]).unwrap(); postcard::to_slice(&ret, &mut buf[..]).unwrap();
buf.send(); buf.send();
} }
1 => { 1 => {
let (fd, pos, data): (u64, u64, Vec<u8>) = postcard::from_bytes(call.args()).unwrap(); let (fd, data): (u64, Vec<u8>) = postcard::from_bytes(call.args()).unwrap();
let ret = postcard::to_stdvec(&server.write(fd, pos, &data)).unwrap(); let ret = postcard::to_stdvec(&server.write(fd, &data)).unwrap();
call.send_return(&ret); call.send_return(&ret);
} }
2 => { 2 => {
@ -240,6 +252,11 @@ fn callback(call: IncomingCall) {
let ret = postcard::to_stdvec(&server.cancel_poll(poll_id)).unwrap(); let ret = postcard::to_stdvec(&server.cancel_poll(poll_id)).unwrap();
call.send_return(&ret); 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);
}
_ => (), _ => (),
} }
} }