From 6f4bf91a25c340d40f647a6de371bfd076c1ac39 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 30 Sep 2024 13:32:54 -0500 Subject: [PATCH] Add seek function --- Cargo.lock | 9 ++++++-- Cargo.toml | 1 + src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b0d64e8..5fe8de3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,12 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +dependencies = [ + "serde", +] [[package]] name = "byteorder" @@ -95,6 +98,7 @@ checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" name = "file_rpc" version = "0.1.0" dependencies = [ + "bitflags", "parking_lot", "postcard", "serde", @@ -130,6 +134,7 @@ dependencies = [ "dev_driver_rpc", "devfs_rpc", "file_rpc", + "parking_lot", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ff7d892..eaf8216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" dev_driver_rpc = { version = "0.1.0", path = "../dev_driver_rpc" } devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" } file_rpc = { version = "0.1.0", path = "../file_rpc" } +parking_lot = "0.12.3" [profile.release] strip = true diff --git a/src/main.rs b/src/main.rs index 7087c29..94ae37a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,34 @@ -use std::{borrow::Cow, os::mikros::{ipc, syscalls::{self, get_initrd}}}; +use std::{borrow::Cow, os::mikros::{ipc, syscalls::{self, get_initrd}}, sync::Arc}; +use parking_lot::RwLock; + +#[derive(Clone)] struct Serv { - initrd: &'static [u8] + initrd: &'static [u8], + files: Arc>>, } impl dev_driver_rpc::Server for Serv { fn open(&self, _path: &std::path::Path) -> Result { - Ok(0) + let mut files = self.files.write(); + files.push(0); + Ok((files.len() - 1) as u64) } } impl file_rpc::Server for Serv { - fn read(&self, _fd: u64, pos: u64, len: usize) -> Result, ()> { + fn read(&self, fd: u64, len: usize) -> Result, ()> { + let pos = self.files.read()[fd as usize]; if pos as usize >= self.initrd.len() { return Err(()) } let avail_bytes = self.initrd.len() - pos as usize; let read_len = usize::min(len, avail_bytes); + self.files.write()[fd as usize] += read_len as u64; Ok(self.initrd[(pos as usize)..((pos as usize)+read_len)].into()) } - fn write(&self, _fd: u64, _pos: u64, data: &[u8]) -> Result<(), ()> { - print!("{}", std::str::from_utf8(data).map_err(|_| ())?); + fn write(&self, _fd: u64, _data: &[u8]) -> Result<(), ()> { Ok(()) } @@ -33,14 +40,49 @@ impl file_rpc::Server for Serv { } fn dup(&self, fd: u64) -> Option { - Some(fd) + let mut files = self.files.write(); + let file = files[fd as usize]; + files.push(file); + Some((files.len() - 1) as u64) + } + + fn seek(&self, fd: u64, pos: file_rpc::SeekFrom) -> u64 { + let mut files = self.files.write(); + let file_pos = &mut files[fd as usize]; + match pos { + file_rpc::SeekFrom::Start(offset) => { + *file_pos = offset; + }, + file_rpc::SeekFrom::End(offset) => { + if offset <= 0 { + *file_pos = self.initrd.len() as u64 - (-offset) as u64; + } + }, + file_rpc::SeekFrom::Current(offset) => { + if offset > 0 { + *file_pos = u64::min(*file_pos + offset as u64, self.initrd.len() as u64); + } else { + let offset = (-offset) as u64; + if offset > *file_pos { + *file_pos = 0; + } else { + *file_pos -= offset; + } + } + }, + } + *file_pos } } fn main() { let initrd = get_initrd(); - dev_driver_rpc::register_server(Box::new(Serv {initrd})); - file_rpc::register_server(Box::new(Serv {initrd})); + let serv = Serv { + initrd, + files: Arc::new(RwLock::new(Vec::new())), + }; + dev_driver_rpc::register_server(Box::new(serv.clone())); + file_rpc::register_server(Box::new(serv)); let devfs_pid; loop { if let Some(pid) = syscalls::try_get_registered(1) {