diff --git a/library/std/src/sys/pal/mikros/fs.rs b/library/std/src/sys/pal/mikros/fs.rs index d70a7c03a53..d5eb9e4e3ac 100644 --- a/library/std/src/sys/pal/mikros/fs.rs +++ b/library/std/src/sys/pal/mikros/fs.rs @@ -7,7 +7,7 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; use crate::os::mikros::ipc::rpc; use crate::os::mikros::{Errno, syscalls}; -use crate::path::{Path, PathBuf}; +use crate::path::{Component, Path, PathBuf}; use crate::sys::time::SystemTime; use crate::sys::unsupported; use crate::{fmt, str}; @@ -328,13 +328,14 @@ pub fn create_new(&mut self, _create_new: bool) {} impl File { pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result { + let path = path.canonicalize()?; 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, 2, &postcard::to_allocvec(path).unwrap()).get_return(), + &rpc::send_call(vfs_pid, 2, 2, &postcard::to_allocvec(&path).unwrap()).get_return(), ) .unwrap(); let (fs_pid, fd) = open_res?; @@ -476,13 +477,14 @@ fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { } pub fn readdir(path: &Path) -> io::Result { + let path = path.canonicalize()?; 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(), + &rpc::send_call(vfs_pid, 2, 6, &postcard::to_allocvec(&path).unwrap()).get_return(), ) .unwrap(); let (fs_pid, fd) = open_res?; @@ -533,8 +535,20 @@ pub fn lstat(_p: &Path) -> io::Result { unsupported() } -pub fn canonicalize(_p: &Path) -> io::Result { - unsupported() +pub fn canonicalize(path: &Path) -> io::Result { + let path = crate::path::absolute(path)?; + Ok(path.components().fold(PathBuf::from("/"), |mut buf, component| match component { + Component::Prefix(_) => unreachable!(), + Component::RootDir | Component::Normal(_) => { + buf.push(component); + buf + } + Component::CurDir => buf, + Component::ParentDir => { + buf.pop(); + buf + } + })) } pub fn copy(_from: &Path, _to: &Path) -> io::Result { diff --git a/library/std/src/sys/pal/mikros/os.rs b/library/std/src/sys/pal/mikros/os.rs index 84c8bd10e55..52bc295abc1 100644 --- a/library/std/src/sys/pal/mikros/os.rs +++ b/library/std/src/sys/pal/mikros/os.rs @@ -4,7 +4,7 @@ use crate::marker::PhantomData; use crate::os::mikros::Errno; use crate::os::mikros::ipc::rpc; -use crate::path::{self, Component, Path, PathBuf}; +use crate::path::{self, PathBuf}; use crate::sync::Mutex; use crate::{fmt, io}; @@ -160,30 +160,8 @@ pub fn getcwd() -> io::Result { Ok(CWD.lock().unwrap().clone().unwrap()) } -fn canonicalize_path(path: &Path) -> PathBuf { - path.components().fold(PathBuf::from("/"), |mut buf, component| match component { - Component::Prefix(_) => unreachable!(), - Component::RootDir | Component::Normal(_) => { - buf.push(component); - buf - } - Component::CurDir => buf, - Component::ParentDir => { - buf.pop(); - buf - } - }) -} - pub fn chdir(path: &path::Path) -> io::Result<()> { - let cwd = getcwd()?; - let abs_path = if path.is_relative() { - let mut abs_path = cwd.clone(); - abs_path.push(path); - canonicalize_path(&abs_path) - } else { - path.to_owned() - }; + let abs_path = path.canonicalize()?; // Try to open the given directory, if it fails it's not a valid directory. // Use Metadata instead once it's implemented. crate::fs::read_dir(&abs_path)?;