Add seek function

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

View File

@ -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<Option<Box<dyn Server>>> = RwLock::new(None);
const PROTO: u16 = 1;
pub trait Server: Send + Sync {
fn read<'a>(&'a self, fd: u64, pos: u64, len: usize) -> Result<Cow<'a, [u8]>, ()>;
fn write(&self, fd: u64, pos: u64, data: &[u8]) -> Result<(), ()>;
fn read<'a>(&'a self, fd: u64, len: usize) -> Result<Cow<'a, [u8]>, ()>;
fn write(&self, fd: u64, data: &[u8]) -> Result<(), ()>;
fn close(&self, fd: u64);
fn size(&self, fd: u64) -> Option<u64>;
fn dup(&self, fd: u64) -> Option<u64>;
@ -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<Vec<u8>, ()> {
pub fn read(self, fd: u64, len: usize) -> Result<Vec<u8>, ()> {
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<dyn Server>) {
@ -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<u8>) = postcard::from_bytes(call.args()).unwrap();
let ret = postcard::to_stdvec(&server.write(fd, pos, &data)).unwrap();
let (fd, data): (u64, Vec<u8>) = 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);
}
_ => (),
}
}