mikros: implement read_dir

This commit is contained in:
pjht 2024-10-04 12:27:43 -05:00
parent b9a358e1c5
commit b78fffd9b0
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -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<DirEntry>;
fn next(&mut self) -> Option<io::Result<DirEntry>> {
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<Option<String>, 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<FileAttr> {
self.0
unsupported()
}
pub fn file_type(&self) -> io::Result<FileType> {
self.0
unsupported()
}
}
@ -363,8 +381,18 @@ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
unsupported()
pub fn readdir(path: &Path) -> io::Result<ReadDir> {
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<()> {