Add seek function

This commit is contained in:
pjht 2024-09-30 13:32:54 -05:00
parent 2ad62a5ea6
commit 6f4bf91a25
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
3 changed files with 59 additions and 11 deletions

9
Cargo.lock generated
View File

@ -19,9 +19,12 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.5.0" version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -95,6 +98,7 @@ checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced"
name = "file_rpc" name = "file_rpc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bitflags",
"parking_lot", "parking_lot",
"postcard", "postcard",
"serde", "serde",
@ -130,6 +134,7 @@ dependencies = [
"dev_driver_rpc", "dev_driver_rpc",
"devfs_rpc", "devfs_rpc",
"file_rpc", "file_rpc",
"parking_lot",
] ]
[[package]] [[package]]

View File

@ -7,6 +7,7 @@ edition = "2021"
dev_driver_rpc = { version = "0.1.0", path = "../dev_driver_rpc" } dev_driver_rpc = { version = "0.1.0", path = "../dev_driver_rpc" }
devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" } devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" }
file_rpc = { version = "0.1.0", path = "../file_rpc" } file_rpc = { version = "0.1.0", path = "../file_rpc" }
parking_lot = "0.12.3"
[profile.release] [profile.release]
strip = true strip = true

View File

@ -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 { struct Serv {
initrd: &'static [u8] initrd: &'static [u8],
files: Arc<RwLock<Vec<u64>>>,
} }
impl dev_driver_rpc::Server for Serv { impl dev_driver_rpc::Server for Serv {
fn open(&self, _path: &std::path::Path) -> Result<u64, ()> { fn open(&self, _path: &std::path::Path) -> Result<u64, ()> {
Ok(0) let mut files = self.files.write();
files.push(0);
Ok((files.len() - 1) as u64)
} }
} }
impl file_rpc::Server for Serv { impl file_rpc::Server for Serv {
fn read(&self, _fd: u64, pos: u64, len: usize) -> Result<Cow<'_, [u8]>, ()> { fn read(&self, fd: u64, len: usize) -> Result<Cow<'_, [u8]>, ()> {
let pos = self.files.read()[fd as usize];
if pos as usize >= self.initrd.len() { if pos as usize >= self.initrd.len() {
return Err(()) return Err(())
} }
let avail_bytes = self.initrd.len() - pos as usize; let avail_bytes = self.initrd.len() - pos as usize;
let read_len = usize::min(len, avail_bytes); 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()) Ok(self.initrd[(pos as usize)..((pos as usize)+read_len)].into())
} }
fn write(&self, _fd: u64, _pos: u64, data: &[u8]) -> Result<(), ()> { fn write(&self, _fd: u64, _data: &[u8]) -> Result<(), ()> {
print!("{}", std::str::from_utf8(data).map_err(|_| ())?);
Ok(()) Ok(())
} }
@ -33,14 +40,49 @@ impl file_rpc::Server for Serv {
} }
fn dup(&self, fd: u64) -> Option<u64> { fn dup(&self, fd: u64) -> Option<u64> {
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() { fn main() {
let initrd = get_initrd(); let initrd = get_initrd();
dev_driver_rpc::register_server(Box::new(Serv {initrd})); let serv = Serv {
file_rpc::register_server(Box::new(Serv {initrd})); 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; let devfs_pid;
loop { loop {
if let Some(pid) = syscalls::try_get_registered(1) { if let Some(pid) = syscalls::try_get_registered(1) {