mikros: move file seeking into the RPC servers
This commit is contained in:
parent
a94d4d57de
commit
783aa92bde
@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
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<File> {
|
||||
.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<FileAttr> {
|
||||
@ -259,19 +257,13 @@ pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
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<usize> {
|
||||
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(),
|
||||
)
|
||||
&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<u64> {
|
||||
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<File> {
|
||||
@ -365,11 +329,7 @@ pub fn duplicate(&self) -> io::Result<File> {
|
||||
.get_return();
|
||||
let dup_res: Option<u64> = 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<()> {
|
||||
|
@ -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<Option<(u64, u64)>> = OnceLock::new();
|
||||
pub(crate) static STDOUT_FD: OnceLock<Option<(u64, u64)>> = OnceLock::new();
|
||||
@ -25,7 +24,7 @@ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
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<usize> {
|
||||
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<usize> {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user