From b78fffd9b0358cf2e64755e3a2e08ff286773a67 Mon Sep 17 00:00:00 2001 From: pjht Date: Fri, 4 Oct 2024 12:27:43 -0500 Subject: [PATCH] mikros: implement read_dir --- library/std/src/sys/pal/mikros/fs.rs | 58 +++++++++++++++++++++------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/library/std/src/sys/pal/mikros/fs.rs b/library/std/src/sys/pal/mikros/fs.rs index e78321aac10..346133e96ca 100644 --- a/library/std/src/sys/pal/mikros/fs.rs +++ b/library/std/src/sys/pal/mikros/fs.rs @@ -43,9 +43,16 @@ pub struct FileAttr { size: u64, } -pub struct ReadDir(!); +#[derive(Debug)] +pub struct ReadDir { + path: PathBuf, + fs_pid: u64, + fd: u64, +} -pub struct DirEntry(!); +pub struct DirEntry { + path: PathBuf, +} #[derive(Clone, Debug)] pub struct OpenOptions {} @@ -169,35 +176,46 @@ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -impl fmt::Debug for ReadDir { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0 - } -} - impl Iterator for ReadDir { type Item = io::Result; fn next(&mut self) -> Option> { - self.0 + let msg_data = + &rpc::send_call(self.fs_pid, 7, 0, &postcard::to_allocvec(&self.fd).unwrap()) + .get_return(); + let entry_res: Result, Errno> = postcard::from_bytes(msg_data).unwrap(); + let entry_name = match entry_res.transpose()? { + Ok(val) => val, + Err(e) => return Some(Err(e.into())), + }; + let mut entry_path = self.path.clone(); + entry_path.push(entry_name); + Some(Ok(DirEntry { path: entry_path })) + } +} + +impl Drop for ReadDir { + fn drop(&mut self) { + let _ = &rpc::send_call(self.fs_pid, 7, 1, &postcard::to_allocvec(&self.fd).unwrap()) + .get_return(); } } impl DirEntry { pub fn path(&self) -> PathBuf { - self.0 + self.path.clone() } pub fn file_name(&self) -> OsString { - self.0 + self.path.file_name().unwrap().to_owned() } pub fn metadata(&self) -> io::Result { - self.0 + unsupported() } pub fn file_type(&self) -> io::Result { - self.0 + unsupported() } } @@ -363,8 +381,18 @@ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -pub fn readdir(_p: &Path) -> io::Result { - unsupported() +pub fn readdir(path: &Path) -> io::Result { + let vfs_pid = loop { + if let Some(pid) = syscalls::try_get_registered(0) { + break pid; + } + }; + let open_res: Result<(u64, u64), Errno> = postcard::from_bytes( + &rpc::send_call(vfs_pid, 2, 6, &postcard::to_allocvec(path).unwrap()).get_return(), + ) + .unwrap(); + let (fs_pid, fd) = open_res?; + Ok(ReadDir { path: path.to_owned(), fs_pid, fd }) } pub fn unlink(_p: &Path) -> io::Result<()> {