Canonicalize input path for file and directory opens
This commit is contained in:
parent
9ec700f2e9
commit
a345b1efdc
@ -7,7 +7,7 @@
|
|||||||
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
|
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
|
||||||
use crate::os::mikros::ipc::rpc;
|
use crate::os::mikros::ipc::rpc;
|
||||||
use crate::os::mikros::{Errno, syscalls};
|
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::time::SystemTime;
|
||||||
use crate::sys::unsupported;
|
use crate::sys::unsupported;
|
||||||
use crate::{fmt, str};
|
use crate::{fmt, str};
|
||||||
@ -328,13 +328,14 @@ pub fn create_new(&mut self, _create_new: bool) {}
|
|||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result<File> {
|
pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result<File> {
|
||||||
|
let path = path.canonicalize()?;
|
||||||
let vfs_pid = loop {
|
let vfs_pid = loop {
|
||||||
if let Some(pid) = syscalls::try_get_registered(0) {
|
if let Some(pid) = syscalls::try_get_registered(0) {
|
||||||
break pid;
|
break pid;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let open_res: Result<(u64, u64), Errno> = postcard::from_bytes(
|
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();
|
.unwrap();
|
||||||
let (fs_pid, fd) = open_res?;
|
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<ReadDir> {
|
pub fn readdir(path: &Path) -> io::Result<ReadDir> {
|
||||||
|
let path = path.canonicalize()?;
|
||||||
let vfs_pid = loop {
|
let vfs_pid = loop {
|
||||||
if let Some(pid) = syscalls::try_get_registered(0) {
|
if let Some(pid) = syscalls::try_get_registered(0) {
|
||||||
break pid;
|
break pid;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let open_res: Result<(u64, u64), Errno> = postcard::from_bytes(
|
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();
|
.unwrap();
|
||||||
let (fs_pid, fd) = open_res?;
|
let (fs_pid, fd) = open_res?;
|
||||||
@ -533,8 +535,20 @@ pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
|
|||||||
unsupported()
|
unsupported()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
|
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
|
||||||
unsupported()
|
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<u64> {
|
pub fn copy(_from: &Path, _to: &Path) -> io::Result<u64> {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
use crate::marker::PhantomData;
|
use crate::marker::PhantomData;
|
||||||
use crate::os::mikros::Errno;
|
use crate::os::mikros::Errno;
|
||||||
use crate::os::mikros::ipc::rpc;
|
use crate::os::mikros::ipc::rpc;
|
||||||
use crate::path::{self, Component, Path, PathBuf};
|
use crate::path::{self, PathBuf};
|
||||||
use crate::sync::Mutex;
|
use crate::sync::Mutex;
|
||||||
use crate::{fmt, io};
|
use crate::{fmt, io};
|
||||||
|
|
||||||
@ -160,30 +160,8 @@ pub fn getcwd() -> io::Result<PathBuf> {
|
|||||||
Ok(CWD.lock().unwrap().clone().unwrap())
|
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<()> {
|
pub fn chdir(path: &path::Path) -> io::Result<()> {
|
||||||
let cwd = getcwd()?;
|
let abs_path = path.canonicalize()?;
|
||||||
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()
|
|
||||||
};
|
|
||||||
// Try to open the given directory, if it fails it's not a valid directory.
|
// Try to open the given directory, if it fails it's not a valid directory.
|
||||||
// Use Metadata instead once it's implemented.
|
// Use Metadata instead once it's implemented.
|
||||||
crate::fs::read_dir(&abs_path)?;
|
crate::fs::read_dir(&abs_path)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user