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::ipc::rpc;
|
||||||
use crate::os::mikros::syscalls;
|
use crate::os::mikros::syscalls;
|
||||||
use crate::path::{Path, PathBuf};
|
use crate::path::{Path, PathBuf};
|
||||||
use crate::sync::atomic::{self, AtomicUsize};
|
|
||||||
use crate::sys::time::SystemTime;
|
use crate::sys::time::SystemTime;
|
||||||
use crate::sys::unsupported;
|
use crate::sys::unsupported;
|
||||||
|
|
||||||
@ -38,7 +37,6 @@ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|||||||
pub struct File {
|
pub struct File {
|
||||||
pub(crate) fs_pid: u64,
|
pub(crate) fs_pid: u64,
|
||||||
pub(crate) fd: u64,
|
pub(crate) fd: u64,
|
||||||
pub(crate) pos: AtomicUsize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileAttr {
|
pub struct FileAttr {
|
||||||
@ -229,7 +227,7 @@ pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result<File> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let (fs_pid, fd) =
|
let (fs_pid, fd) =
|
||||||
open_res.map_err(|_| io::Error::new(io::ErrorKind::NotFound, "No such file"))?;
|
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> {
|
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,
|
self.fs_pid,
|
||||||
1,
|
1,
|
||||||
0,
|
0,
|
||||||
&postcard::to_allocvec(&(
|
&postcard::to_allocvec(&(self.fd, buf.len())).unwrap(),
|
||||||
self.fd,
|
|
||||||
self.pos.load(atomic::Ordering::Relaxed) as u64,
|
|
||||||
buf.len(),
|
|
||||||
))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
)
|
||||||
.get_return();
|
.get_return();
|
||||||
let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap();
|
let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap();
|
||||||
let read_data = read_res.unwrap();
|
let read_data = read_res.unwrap();
|
||||||
let copy_len = usize::min(read_data.len(), buf.len());
|
let copy_len = usize::min(read_data.len(), buf.len());
|
||||||
buf[0..copy_len].copy_from_slice(&read_data[0..copy_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)
|
Ok(copy_len)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,40 +280,23 @@ pub fn read_buf(&self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
|
|||||||
self.fs_pid,
|
self.fs_pid,
|
||||||
1,
|
1,
|
||||||
0,
|
0,
|
||||||
&postcard::to_allocvec(&(
|
&postcard::to_allocvec(&(self.fd, cursor.capacity())).unwrap(),
|
||||||
self.fd,
|
|
||||||
self.pos.load(atomic::Ordering::Relaxed) as u64,
|
|
||||||
cursor.capacity(),
|
|
||||||
))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
)
|
||||||
.get_return();
|
.get_return();
|
||||||
let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap();
|
let read_res: Result<&[u8], ()> = postcard::from_bytes(msg_data).unwrap();
|
||||||
let read_data = read_res.unwrap();
|
let read_data = read_res.unwrap();
|
||||||
let copy_len = usize::min(read_data.len(), cursor.capacity());
|
let copy_len = usize::min(read_data.len(), cursor.capacity());
|
||||||
cursor.append(&read_data[0..copy_len]);
|
cursor.append(&read_data[0..copy_len]);
|
||||||
self.pos.fetch_add(copy_len, atomic::Ordering::Relaxed);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
let write_res: Result<(), ()> = postcard::from_bytes(
|
let write_res: Result<(), ()> = postcard::from_bytes(
|
||||||
&rpc::send_call(
|
&rpc::send_call(self.fs_pid, 1, 1, &postcard::to_allocvec(&(self.fd, buf)).unwrap())
|
||||||
self.fs_pid,
|
.get_return(),
|
||||||
1,
|
|
||||||
1,
|
|
||||||
&postcard::to_allocvec(&(
|
|
||||||
self.fd,
|
|
||||||
self.pos.load(atomic::Ordering::Relaxed) as u64,
|
|
||||||
buf,
|
|
||||||
))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.get_return(),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
write_res.unwrap();
|
write_res.unwrap();
|
||||||
self.pos.fetch_add(buf.len(), atomic::Ordering::Relaxed);
|
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -338,25 +313,14 @@ pub fn flush(&self) -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
|
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
|
||||||
match pos {
|
let args = match pos {
|
||||||
SeekFrom::Start(offset) => self.pos.store(offset as usize, atomic::Ordering::Relaxed),
|
SeekFrom::Start(offset) => postcard::to_allocvec(&(self.fd, 0u8, offset)).unwrap(),
|
||||||
SeekFrom::Current(offset) => {
|
SeekFrom::End(offset) => postcard::to_allocvec(&(self.fd, 1u8, offset)).unwrap(),
|
||||||
if offset > 0 {
|
SeekFrom::Current(offset) => postcard::to_allocvec(&(self.fd, 2u8, offset)).unwrap(),
|
||||||
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!(),
|
|
||||||
};
|
};
|
||||||
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> {
|
pub fn duplicate(&self) -> io::Result<File> {
|
||||||
@ -365,11 +329,7 @@ pub fn duplicate(&self) -> io::Result<File> {
|
|||||||
.get_return();
|
.get_return();
|
||||||
let dup_res: Option<u64> = postcard::from_bytes(msg_data).unwrap();
|
let dup_res: Option<u64> = postcard::from_bytes(msg_data).unwrap();
|
||||||
let dup_data = dup_res.unwrap();
|
let dup_data = dup_res.unwrap();
|
||||||
Ok(Self {
|
Ok(Self { fs_pid: self.fs_pid, fd: dup_data })
|
||||||
fs_pid: self.fs_pid,
|
|
||||||
fd: dup_data,
|
|
||||||
pos: AtomicUsize::new(self.pos.load(atomic::Ordering::Relaxed)),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
|
pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::os::mikros::ipc::rpc;
|
use crate::os::mikros::ipc::rpc;
|
||||||
use crate::sync::OnceLock;
|
use crate::sync::OnceLock;
|
||||||
use crate::sync::atomic::AtomicUsize;
|
|
||||||
|
|
||||||
pub(crate) static STDIN_FD: OnceLock<Option<(u64, u64)>> = OnceLock::new();
|
pub(crate) static STDIN_FD: OnceLock<Option<(u64, u64)>> = OnceLock::new();
|
||||||
pub(crate) static STDOUT_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 {
|
let Some(fd) = *STDIN_FD.get().unwrap() else {
|
||||||
return Ok(0);
|
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);
|
let res = file.read(buf);
|
||||||
crate::mem::drop(file); // do not close the temporary file struct
|
crate::mem::drop(file); // do not close the temporary file struct
|
||||||
res
|
res
|
||||||
@ -49,7 +48,7 @@ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|||||||
let Some(fd) = *STDOUT_FD.get().unwrap() else {
|
let Some(fd) = *STDOUT_FD.get().unwrap() else {
|
||||||
return Ok(0);
|
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);
|
let res = file.write(buf);
|
||||||
crate::mem::drop(file); // do not close the temporary file struct
|
crate::mem::drop(file); // do not close the temporary file struct
|
||||||
res
|
res
|
||||||
@ -77,7 +76,7 @@ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|||||||
let Some(fd) = *STDERR_FD.get().unwrap() else {
|
let Some(fd) = *STDERR_FD.get().unwrap() else {
|
||||||
return Ok(0);
|
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);
|
let res = file.write(buf);
|
||||||
crate::mem::drop(file); // do not close the temporary file struct
|
crate::mem::drop(file); // do not close the temporary file struct
|
||||||
res
|
res
|
||||||
|
Loading…
Reference in New Issue
Block a user