Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions library/std/src/sys/fs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
ignore_notfound(fs::remove_dir(path))
}

pub fn exists(path: &Path) -> io::Result<bool> {
match fs::metadata(path) {
Ok(_) => Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}

pub struct Dir {
path: PathBuf,
}
Expand Down
58 changes: 30 additions & 28 deletions library/std/src/sys/fs/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, Raw
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::{Dir, copy, exists};
pub use crate::sys::fs::common::{Dir, copy};
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, cvt, unsupported, unsupported_err};
Expand Down Expand Up @@ -234,9 +234,7 @@ impl DirEntry {
}

pub fn metadata(&self) -> io::Result<FileAttr> {
let mut path = self.path();
path.set_file_name(self.file_name_os_str());
lstat(&path)
run_path_with_cstr(&self.path(), &lstat)
}

pub fn file_type(&self) -> io::Result<FileType> {
Expand Down Expand Up @@ -554,62 +552,66 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
Ok(ReadDir::new(InnerReadDir::new(root, vec)))
}

pub fn unlink(path: &Path) -> io::Result<()> {
run_path_with_cstr(path, &|path| cvt(unsafe { hermit_abi::unlink(path.as_ptr()) }).map(|_| ()))
pub fn unlink(path: &CStr) -> io::Result<()> {
cvt(unsafe { hermit_abi::unlink(path.as_ptr()) }).map(|_| ())
}

pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
pub fn rename(_old: &CStr, _new: &CStr) -> io::Result<()> {
unsupported()
}

pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> {
pub fn set_perm(_p: &CStr, _perm: FilePermissions) -> io::Result<()> {
Err(Error::from_raw_os_error(22))
}

pub fn set_times(_p: &Path, _times: FileTimes) -> io::Result<()> {
pub fn set_times(_p: &CStr, _times: FileTimes) -> io::Result<()> {
Err(Error::from_raw_os_error(22))
}

pub fn set_times_nofollow(_p: &Path, _times: FileTimes) -> io::Result<()> {
pub fn set_times_nofollow(_p: &CStr, _times: FileTimes) -> io::Result<()> {
Err(Error::from_raw_os_error(22))
}

pub fn rmdir(path: &Path) -> io::Result<()> {
run_path_with_cstr(path, &|path| cvt(unsafe { hermit_abi::rmdir(path.as_ptr()) }).map(|_| ()))
pub fn rmdir(path: &CStr) -> io::Result<()> {
cvt(unsafe { hermit_abi::rmdir(path.as_ptr()) }).map(|_| ())
}

pub fn exists(path: &CStr) -> io::Result<bool> {
match stat(path) {
Ok(_) => Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}

pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
unsupported()
}

pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
pub fn readlink(_p: &CStr) -> io::Result<PathBuf> {
unsupported()
}

pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
pub fn symlink(_original: &CStr, _link: &CStr) -> io::Result<()> {
unsupported()
}

pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
pub fn link(_original: &CStr, _link: &CStr) -> io::Result<()> {
unsupported()
}

pub fn stat(path: &Path) -> io::Result<FileAttr> {
run_path_with_cstr(path, &|path| {
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
cvt(unsafe { hermit_abi::stat(path.as_ptr(), &mut stat_val) })?;
Ok(FileAttr::from_stat(stat_val))
})
pub fn stat(path: &CStr) -> io::Result<FileAttr> {
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
cvt(unsafe { hermit_abi::stat(path.as_ptr(), &mut stat_val) })?;
Ok(FileAttr::from_stat(stat_val))
}

pub fn lstat(path: &Path) -> io::Result<FileAttr> {
run_path_with_cstr(path, &|path| {
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
cvt(unsafe { hermit_abi::lstat(path.as_ptr(), &mut stat_val) })?;
Ok(FileAttr::from_stat(stat_val))
})
pub fn lstat(path: &CStr) -> io::Result<FileAttr> {
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
cvt(unsafe { hermit_abi::lstat(path.as_ptr(), &mut stat_val) })?;
Ok(FileAttr::from_stat(stat_val))
}

pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
pub fn canonicalize(_p: &CStr) -> io::Result<PathBuf> {
unsupported()
}
17 changes: 6 additions & 11 deletions library/std/src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,35 @@ cfg_select! {
target_os = "hermit" => {
mod hermit;
use hermit as imp;
use crate::sys::helpers::run_path_with_cstr as with_native_path;
}
target_os = "motor" => {
mod motor;
use motor as imp;
use crate::sys::path::with_native_path;
}
target_os = "solid_asp3" => {
mod solid;
use solid as imp;
use crate::sys::path::with_native_path;
}
target_os = "uefi" => {
mod uefi;
use uefi as imp;
use crate::sys::path::with_native_path;
}
target_os = "vexos" => {
mod vexos;
use vexos as imp;
use crate::sys::helpers::run_path_with_cstr as with_native_path;
}
_ => {
mod unsupported;
use unsupported as imp;
use unsupported::with_native_path;
}
}

// FIXME: Replace this with platform-specific path conversion functions.
#[cfg(not(any(target_family = "unix", target_os = "windows", target_os = "wasi")))]
#[inline]
pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
f(path)
}

pub use imp::{
Dir, DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
ReadDir,
Expand Down Expand Up @@ -157,10 +156,6 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
}

pub fn exists(path: &Path) -> io::Result<bool> {
// FIXME: use with_native_path on all platforms
#[cfg(not(windows))]
return imp::exists(path);
#[cfg(windows)]
with_native_path(path, &imp::exists)
}

Expand Down
41 changes: 21 additions & 20 deletions library/std/src/sys/fs/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::path::{Path, PathBuf};
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::{Dir, exists};
pub use crate::sys::fs::common::Dir;
use crate::sys::time::SystemTime;
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, map_motor_error, unsupported};

Expand Down Expand Up @@ -297,64 +297,65 @@ impl DirBuilder {
}
}

pub fn unlink(path: &Path) -> io::Result<()> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn unlink(path: &str) -> io::Result<()> {
moto_rt::fs::unlink(path).map_err(map_motor_error)
}

pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
let old = old.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
let new = new.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn rename(old: &str, new: &str) -> io::Result<()> {
moto_rt::fs::rename(old, new).map_err(map_motor_error)
}

pub fn rmdir(path: &Path) -> io::Result<()> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn rmdir(path: &str) -> io::Result<()> {
moto_rt::fs::rmdir(path).map_err(map_motor_error)
}

pub fn exists(path: &str) -> io::Result<bool> {
match stat(path) {
Ok(_) => Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}

pub fn remove_dir_all(path: &Path) -> io::Result<()> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
moto_rt::fs::rmdir_all(path).map_err(map_motor_error)
}

pub fn set_perm(path: &Path, perm: FilePermissions) -> io::Result<()> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn set_perm(path: &str, perm: FilePermissions) -> io::Result<()> {
moto_rt::fs::set_perm(path, perm.rt_perm).map_err(map_motor_error)
}

pub fn set_times(_p: &Path, _times: FileTimes) -> io::Result<()> {
pub fn set_times(_p: &str, _times: FileTimes) -> io::Result<()> {
unsupported()
}

pub fn set_times_nofollow(_p: &Path, _times: FileTimes) -> io::Result<()> {
pub fn set_times_nofollow(_p: &str, _times: FileTimes) -> io::Result<()> {
unsupported()
}

pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
pub fn readlink(_p: &str) -> io::Result<PathBuf> {
unsupported()
}

pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
pub fn symlink(_original: &str, _link: &str) -> io::Result<()> {
unsupported()
}

pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
pub fn link(_src: &str, _dst: &str) -> io::Result<()> {
unsupported()
}

pub fn stat(path: &Path) -> io::Result<FileAttr> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn stat(path: &str) -> io::Result<FileAttr> {
let inner = moto_rt::fs::stat(path).map_err(map_motor_error)?;
Ok(FileAttr { inner })
}

pub fn lstat(path: &Path) -> io::Result<FileAttr> {
pub fn lstat(path: &str) -> io::Result<FileAttr> {
stat(path)
}

pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
let path = path.to_str().ok_or(io::Error::from(io::ErrorKind::InvalidFilename))?;
pub fn canonicalize(path: &str) -> io::Result<PathBuf> {
let path = moto_rt::fs::canonicalize(path).map_err(map_motor_error)?;
Ok(path.into())
}
Expand Down
Loading
Loading