From 783aa92bde59e48bcfa9f59eecfd71024e7c6e49 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 30 Sep 2024 13:31:07 -0500 Subject: [PATCH] mikros: move file seeking into the RPC servers --- library/std/src/sys/pal/mikros/fs.rs | 66 +++++-------------------- library/std/src/sys/pal/mikros/stdio.rs | 7 ++- 2 files changed, 16 insertions(+), 57 deletions(-) diff --git a/library/std/src/sys/pal/mikros/fs.rs b/library/std/src/sys/pal/mikros/fs.rs index c315b863dae..e1ee03ab65b 100644 --- a/library/std/src/sys/pal/mikros/fs.rs +++ b/library/std/src/sys/pal/mikros/fs.rs @@ -8,7 +8,6 @@ use crate::os::mikros::ipc::rpc; use crate::os::mikros::syscalls; use crate::path::{Path, PathBuf}; -use crate::sync::atomic::{self, AtomicUsize}; use crate::sys::time::SystemTime; use crate::sys::unsupported; @@ -38,7 +37,6 @@ fn serialize(&self, serializer: S) -> Result pub struct File { pub(crate) fs_pid: u64, pub(crate) fd: u64, - pub(crate) pos: AtomicUsize, } pub struct FileAttr { @@ -229,7 +227,7 @@ pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result { .unwrap(); let (fs_pid, fd) = open_res.map_err(|_| io::Error::new(io::ErrorKind::NotFound, "No such file"))?; - Ok(Self { fs_pid, fd, pos: AtomicUsize::new(0) }) + Ok(Self { fs_pid, fd }) } pub fn file_attr(&self) -> io::Result { @@ -259,19 +257,13 @@ pub fn read(&self, buf: &mut [u8]) -> io::Result { self.fs_pid, 1, 0, - &postcard::to_allocvec(&( - self.fd, - self.pos.load(atomic::Ordering::Relaxed) as u64, - buf.len(), - )) - .unwrap(), + &postcard::to_allocvec(&(self.fd, buf.len())).unwrap(), ) .get_return(); let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap(); let read_data = read_res.unwrap(); let copy_len = usize::min(read_data.len(), buf.len()); buf[0..copy_len].copy_from_slice(&read_data[0..copy_len]); - self.pos.fetch_add(copy_len, atomic::Ordering::Relaxed); Ok(copy_len) } @@ -288,40 +280,23 @@ pub fn read_buf(&self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { self.fs_pid, 1, 0, - &postcard::to_allocvec(&( - self.fd, - self.pos.load(atomic::Ordering::Relaxed) as u64, - cursor.capacity(), - )) - .unwrap(), + &postcard::to_allocvec(&(self.fd, cursor.capacity())).unwrap(), ) .get_return(); let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap(); let read_data = read_res.unwrap(); let copy_len = usize::min(read_data.len(), cursor.capacity()); cursor.append(&read_data[0..copy_len]); - self.pos.fetch_add(copy_len, atomic::Ordering::Relaxed); Ok(()) } pub fn write(&self, buf: &[u8]) -> io::Result { let write_res: Result<(), ()> = postcard::from_bytes( - &rpc::send_call( - self.fs_pid, - 1, - 1, - &postcard::to_allocvec(&( - self.fd, - self.pos.load(atomic::Ordering::Relaxed) as u64, - buf, - )) - .unwrap(), - ) - .get_return(), + &rpc::send_call(self.fs_pid, 1, 1, &postcard::to_allocvec(&(self.fd, buf)).unwrap()) + .get_return(), ) .unwrap(); write_res.unwrap(); - self.pos.fetch_add(buf.len(), atomic::Ordering::Relaxed); Ok(buf.len()) } @@ -338,25 +313,14 @@ pub fn flush(&self) -> io::Result<()> { } pub fn seek(&self, pos: SeekFrom) -> io::Result { - match pos { - SeekFrom::Start(offset) => self.pos.store(offset as usize, atomic::Ordering::Relaxed), - SeekFrom::Current(offset) => { - if offset > 0 { - self.pos.fetch_add(offset as usize, atomic::Ordering::Relaxed); - } else { - let offset = (-offset) as u64; - if offset > (self.pos.load(atomic::Ordering::Relaxed) as u64) { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "tried to seek before byte 0", - )); - } - self.pos.fetch_sub(offset as usize, atomic::Ordering::Relaxed); - } - } - SeekFrom::End(_offset) => todo!(), + let args = match pos { + SeekFrom::Start(offset) => postcard::to_allocvec(&(self.fd, 0u8, offset)).unwrap(), + SeekFrom::End(offset) => postcard::to_allocvec(&(self.fd, 1u8, offset)).unwrap(), + SeekFrom::Current(offset) => postcard::to_allocvec(&(self.fd, 2u8, offset)).unwrap(), }; - Ok(self.pos.load(atomic::Ordering::Relaxed) as u64) + let seek_res: u64 = + postcard::from_bytes(&rpc::send_call(self.fs_pid, 1, 8, &args).get_return()).unwrap(); + Ok(seek_res) } pub fn duplicate(&self) -> io::Result { @@ -365,11 +329,7 @@ pub fn duplicate(&self) -> io::Result { .get_return(); let dup_res: Option = postcard::from_bytes(msg_data).unwrap(); let dup_data = dup_res.unwrap(); - Ok(Self { - fs_pid: self.fs_pid, - fd: dup_data, - pos: AtomicUsize::new(self.pos.load(atomic::Ordering::Relaxed)), - }) + Ok(Self { fs_pid: self.fs_pid, fd: dup_data }) } pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> { diff --git a/library/std/src/sys/pal/mikros/stdio.rs b/library/std/src/sys/pal/mikros/stdio.rs index 6641706e6f6..94f32d106b1 100644 --- a/library/std/src/sys/pal/mikros/stdio.rs +++ b/library/std/src/sys/pal/mikros/stdio.rs @@ -4,7 +4,6 @@ use crate::io; use crate::os::mikros::ipc::rpc; use crate::sync::OnceLock; -use crate::sync::atomic::AtomicUsize; pub(crate) static STDIN_FD: OnceLock> = OnceLock::new(); pub(crate) static STDOUT_FD: OnceLock> = OnceLock::new(); @@ -25,7 +24,7 @@ fn read(&mut self, buf: &mut [u8]) -> io::Result { let Some(fd) = *STDIN_FD.get().unwrap() else { return Ok(0); }; - let file = File { fs_pid: fd.0, fd: fd.1, pos: AtomicUsize::new(0) }; + let file = File { fs_pid: fd.0, fd: fd.1 }; let res = file.read(buf); crate::mem::drop(file); // do not close the temporary file struct res @@ -49,7 +48,7 @@ fn write(&mut self, buf: &[u8]) -> io::Result { let Some(fd) = *STDOUT_FD.get().unwrap() else { return Ok(0); }; - let file = File { fs_pid: fd.0, fd: fd.1, pos: AtomicUsize::new(0) }; + let file = File { fs_pid: fd.0, fd: fd.1 }; let res = file.write(buf); crate::mem::drop(file); // do not close the temporary file struct res @@ -77,7 +76,7 @@ fn write(&mut self, buf: &[u8]) -> io::Result { let Some(fd) = *STDERR_FD.get().unwrap() else { return Ok(0); }; - let file = File { fs_pid: fd.0, fd: fd.1, pos: AtomicUsize::new(0) }; + let file = File { fs_pid: fd.0, fd: fd.1 }; let res = file.write(buf); crate::mem::drop(file); // do not close the temporary file struct res