From b8bf308ad01ae795c6363ced32c9a20b70bf74f4 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Mon, 11 Dec 2023 00:36:25 +0000 Subject: [PATCH] Various OS implementations of `NativePath` --- library/std/src/sys/pal/hermit/fs.rs | 110 ++++++++++----------- library/std/src/sys/pal/solid/fs.rs | 57 ++++++++++- library/std/src/sys/pal/unsupported/fs.rs | 114 +++++++++++----------- 3 files changed, 166 insertions(+), 115 deletions(-) diff --git a/library/std/src/sys/pal/hermit/fs.rs b/library/std/src/sys/pal/hermit/fs.rs index d4da53fd3df9e..ead14ed4cbb38 100644 --- a/library/std/src/sys/pal/hermit/fs.rs +++ b/library/std/src/sys/pal/hermit/fs.rs @@ -7,15 +7,66 @@ use crate::io::{self, Error, ErrorKind}; use crate::io::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd}; use crate::path::{Path, PathBuf}; -use crate::sys::common::small_c_string::run_path_with_cstr; use crate::sys::cvt; use crate::sys::time::SystemTime; use crate::sys::unsupported; use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; -pub use crate::sys_common::fs::{copy, try_exists}; //pub use crate::sys_common::fs::remove_dir_all; +pub(crate) mod fs_imp { + pub(crate) use super::{ + DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions, + ReadDir, + }; + use crate::io; + use crate::path::{AsPath, PathBuf}; + use crate::sys::unsupported; + + pub(crate) fn remove_file(_path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn symlink_metadata(_path: P) -> io::Result { + unsupported() + } + pub(crate) fn metadata(_path: P) -> io::Result { + unsupported() + } + pub(crate) fn rename(_from: P, _to: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn hard_link(_original: P, _link: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn soft_link(_original: P, _link: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn remove_dir(_path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn read_dir(_path: P) -> io::Result { + unsupported() + } + pub(crate) fn set_permissions(_path: P, _perms: FilePermissions) -> io::Result<()> { + unsupported() + } + pub(crate) fn copy(_from: P, _to: Q) -> io::Result { + unsupported() + } + pub(crate) fn canonicalize(_path: P) -> io::Result { + unsupported() + } + pub(crate) fn remove_dir_all(_path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn read_link(_path: P) -> io::Result { + unsupported() + } + pub(crate) fn try_exists(_path: P) -> io::Result { + unsupported() + } +} + #[derive(Debug)] pub struct File(FileDesc); @@ -268,11 +319,7 @@ impl OpenOptions { } impl File { - pub fn open(path: &Path, opts: &OpenOptions) -> io::Result { - run_path_with_cstr(path, &|path| File::open_c(&path, opts)) - } - - pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result { + pub fn open_native(path: &CStr, opts: &OpenOptions) -> io::Result { let mut flags = opts.get_access_mode()?; flags = flags | opts.get_creation_mode()?; @@ -415,52 +462,3 @@ impl FromRawFd for File { Self(FromRawFd::from_raw_fd(raw_fd)) } } - -pub fn readdir(_p: &Path) -> io::Result { - unsupported() -} - -pub fn unlink(path: &Path) -> io::Result<()> { - run_path_with_cstr(path, &|path| cvt(unsafe { abi::unlink(path.as_ptr()) }).map(|_| ())) -} - -pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> { - unsupported() -} - -pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> { - match perm.0 {} -} - -pub fn rmdir(_p: &Path) -> io::Result<()> { - unsupported() -} - -pub fn remove_dir_all(_path: &Path) -> io::Result<()> { - //unsupported() - Ok(()) -} - -pub fn readlink(_p: &Path) -> io::Result { - unsupported() -} - -pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> { - unsupported() -} - -pub fn link(_original: &Path, _link: &Path) -> io::Result<()> { - unsupported() -} - -pub fn stat(_p: &Path) -> io::Result { - unsupported() -} - -pub fn lstat(_p: &Path) -> io::Result { - unsupported() -} - -pub fn canonicalize(_p: &Path) -> io::Result { - unsupported() -} diff --git a/library/std/src/sys/pal/solid/fs.rs b/library/std/src/sys/pal/solid/fs.rs index a6c1336109ad7..23c11465b349c 100644 --- a/library/std/src/sys/pal/solid/fs.rs +++ b/library/std/src/sys/pal/solid/fs.rs @@ -14,6 +14,58 @@ use crate::{ pub use crate::sys_common::fs::try_exists; +pub(crate) mod fs_imp { + pub(crate) use super::{ + DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions, + ReadDir, + }; + use crate::io; + use crate::path::{AsPath, PathBuf}; + + pub(crate) fn remove_file(path: P) -> io::Result<()> { + path.with_path(super::unlink) + } + pub(crate) fn symlink_metadata(path: P) -> io::Result { + path.with_path(|path| super::lstat(path)) + } + pub(crate) fn metadata(path: P) -> io::Result { + path.with_path(|path| super::stat(path)) + } + pub(crate) fn rename(from: P, to: Q) -> io::Result<()> { + from.with_path(|from| to.with_path(|to| super::rename(from, to))) + } + pub(crate) fn hard_link(original: P, link: Q) -> io::Result<()> { + original.with_path(|original| link.with_path(|link| super::link(original, link))) + } + pub(crate) fn soft_link(original: P, link: Q) -> io::Result<()> { + original.with_path(|original| link.with_path(|link| super::symlink(original, link))) + } + pub(crate) fn remove_dir(path: P) -> io::Result<()> { + path.with_path(super::rmdir) + } + pub(crate) fn read_dir(path: P) -> io::Result { + path.with_path(super::readdir) + } + pub(crate) fn set_permissions(path: P, perms: FilePermissions) -> io::Result<()> { + path.with_path(|path| super::set_perm(path, perms)) + } + pub(crate) fn copy(from: P, to: Q) -> io::Result { + from.with_path(|from| to.with_path(|to| super::copy(from, to))) + } + pub(crate) fn canonicalize(path: P) -> io::Result { + path.with_path(super::canonicalize) + } + pub(crate) fn remove_dir_all(path: P) -> io::Result<()> { + path.with_path(super::remove_dir_all) + } + pub(crate) fn read_link(path: P) -> io::Result { + path.with_path(super::readlink) + } + pub(crate) fn try_exists(path: P) -> io::Result { + path.with_path(super::try_exists) + } +} + /// A file descriptor. #[derive(Clone, Copy)] #[rustc_layout_scalar_valid_range_start(0)] @@ -325,6 +377,9 @@ fn cstr(path: &Path) -> io::Result { impl File { pub fn open(path: &Path, opts: &OpenOptions) -> io::Result { + Self::open_native(&cstr(path)?, opts) + } + pub fn open_native(path: &CStr, opts: &OpenOptions) -> io::Result { let flags = opts.get_access_mode()? | opts.get_creation_mode()? | (opts.custom_flags as c_int & !abi::O_ACCMODE); @@ -332,7 +387,7 @@ impl File { let mut fd = MaybeUninit::uninit(); error::SolidError::err_if_negative(abi::SOLID_FS_Open( fd.as_mut_ptr(), - cstr(path)?.as_ptr(), + path.as_ptr(), flags, )) .map_err(|e| e.as_io_error())?; diff --git a/library/std/src/sys/pal/unsupported/fs.rs b/library/std/src/sys/pal/unsupported/fs.rs index 6ac1b5d2bcfca..f0ec12c394516 100644 --- a/library/std/src/sys/pal/unsupported/fs.rs +++ b/library/std/src/sys/pal/unsupported/fs.rs @@ -1,4 +1,4 @@ -use crate::ffi::OsString; +use crate::ffi::{CStr, OsString}; use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; @@ -14,6 +14,60 @@ pub struct ReadDir(!); pub struct DirEntry(!); +#[allow(unused_variables)] +pub(crate) mod fs_imp { + use super::unsupported; + pub(crate) use super::{ + DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions, + ReadDir, + }; + use crate::io; + use crate::path::{AsPath, PathBuf}; + + pub(crate) fn remove_file(path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn symlink_metadata(path: P) -> io::Result { + unsupported() + } + pub(crate) fn metadata(path: P) -> io::Result { + unsupported() + } + pub(crate) fn rename(from: P, to: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn hard_link(original: P, link: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn soft_link(original: P, link: Q) -> io::Result<()> { + unsupported() + } + pub(crate) fn remove_dir(path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn read_dir(path: P) -> io::Result { + unsupported() + } + pub(crate) fn set_permissions(path: P, perms: FilePermissions) -> io::Result<()> { + unsupported() + } + pub(crate) fn copy(from: P, to: Q) -> io::Result { + unsupported() + } + pub(crate) fn canonicalize(path: P) -> io::Result { + unsupported() + } + pub(crate) fn remove_dir_all(path: P) -> io::Result<()> { + unsupported() + } + pub(crate) fn read_link(path: P) -> io::Result { + unsupported() + } + pub(crate) fn try_exists(path: P) -> io::Result { + unsupported() + } +} + #[derive(Clone, Debug)] pub struct OpenOptions {} @@ -182,7 +236,7 @@ impl OpenOptions { } impl File { - pub fn open(_path: &Path, _opts: &OpenOptions) -> io::Result { + pub fn open_native(_path: &CStr, _opts: &OpenOptions) -> io::Result { unsupported() } @@ -266,59 +320,3 @@ impl fmt::Debug for File { self.0 } } - -pub fn readdir(_p: &Path) -> io::Result { - unsupported() -} - -pub fn unlink(_p: &Path) -> io::Result<()> { - unsupported() -} - -pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> { - unsupported() -} - -pub fn set_perm(_p: &Path, perm: FilePermissions) -> io::Result<()> { - match perm.0 {} -} - -pub fn rmdir(_p: &Path) -> io::Result<()> { - unsupported() -} - -pub fn remove_dir_all(_path: &Path) -> io::Result<()> { - unsupported() -} - -pub fn try_exists(_path: &Path) -> io::Result { - unsupported() -} - -pub fn readlink(_p: &Path) -> io::Result { - unsupported() -} - -pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> { - unsupported() -} - -pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> { - unsupported() -} - -pub fn stat(_p: &Path) -> io::Result { - unsupported() -} - -pub fn lstat(_p: &Path) -> io::Result { - unsupported() -} - -pub fn canonicalize(_p: &Path) -> io::Result { - unsupported() -} - -pub fn copy(_from: &Path, _to: &Path) -> io::Result { - unsupported() -}