Skip to content

Commit

Permalink
Various OS implementations of NativePath
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Feb 28, 2024
1 parent 35fb3dc commit b8bf308
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 115 deletions.
110 changes: 54 additions & 56 deletions library/std/src/sys/pal/hermit/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsPath>(_path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn symlink_metadata<P: AsPath>(_path: P) -> io::Result<FileAttr> {
unsupported()
}
pub(crate) fn metadata<P: AsPath>(_path: P) -> io::Result<FileAttr> {
unsupported()
}
pub(crate) fn rename<P: AsPath, Q: AsPath>(_from: P, _to: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn hard_link<P: AsPath, Q: AsPath>(_original: P, _link: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn soft_link<P: AsPath, Q: AsPath>(_original: P, _link: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn remove_dir<P: AsPath>(_path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn read_dir<P: AsPath>(_path: P) -> io::Result<ReadDir> {
unsupported()
}
pub(crate) fn set_permissions<P: AsPath>(_path: P, _perms: FilePermissions) -> io::Result<()> {
unsupported()
}
pub(crate) fn copy<P: AsPath, Q: AsPath>(_from: P, _to: Q) -> io::Result<u64> {
unsupported()
}
pub(crate) fn canonicalize<P: AsPath>(_path: P) -> io::Result<PathBuf> {
unsupported()
}
pub(crate) fn remove_dir_all<P: AsPath>(_path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn read_link<P: AsPath>(_path: P) -> io::Result<PathBuf> {
unsupported()
}
pub(crate) fn try_exists<P: AsPath>(_path: P) -> io::Result<bool> {
unsupported()
}
}

#[derive(Debug)]
pub struct File(FileDesc);

Expand Down Expand Up @@ -268,11 +319,7 @@ impl OpenOptions {
}

impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
run_path_with_cstr(path, &|path| File::open_c(&path, opts))
}

pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
pub fn open_native(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
let mut flags = opts.get_access_mode()?;
flags = flags | opts.get_creation_mode()?;

Expand Down Expand Up @@ -415,52 +462,3 @@ impl FromRawFd for File {
Self(FromRawFd::from_raw_fd(raw_fd))
}
}

pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
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<PathBuf> {
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<FileAttr> {
unsupported()
}

pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
unsupported()
}

pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
unsupported()
}
57 changes: 56 additions & 1 deletion library/std/src/sys/pal/solid/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsPath>(path: P) -> io::Result<()> {
path.with_path(super::unlink)
}
pub(crate) fn symlink_metadata<P: AsPath>(path: P) -> io::Result<FileAttr> {
path.with_path(|path| super::lstat(path))
}
pub(crate) fn metadata<P: AsPath>(path: P) -> io::Result<FileAttr> {
path.with_path(|path| super::stat(path))
}
pub(crate) fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<()> {
from.with_path(|from| to.with_path(|to| super::rename(from, to)))
}
pub(crate) fn hard_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
original.with_path(|original| link.with_path(|link| super::link(original, link)))
}
pub(crate) fn soft_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
original.with_path(|original| link.with_path(|link| super::symlink(original, link)))
}
pub(crate) fn remove_dir<P: AsPath>(path: P) -> io::Result<()> {
path.with_path(super::rmdir)
}
pub(crate) fn read_dir<P: AsPath>(path: P) -> io::Result<ReadDir> {
path.with_path(super::readdir)
}
pub(crate) fn set_permissions<P: AsPath>(path: P, perms: FilePermissions) -> io::Result<()> {
path.with_path(|path| super::set_perm(path, perms))
}
pub(crate) fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> {
from.with_path(|from| to.with_path(|to| super::copy(from, to)))
}
pub(crate) fn canonicalize<P: AsPath>(path: P) -> io::Result<PathBuf> {
path.with_path(super::canonicalize)
}
pub(crate) fn remove_dir_all<P: AsPath>(path: P) -> io::Result<()> {
path.with_path(super::remove_dir_all)
}
pub(crate) fn read_link<P: AsPath>(path: P) -> io::Result<PathBuf> {
path.with_path(super::readlink)
}
pub(crate) fn try_exists<P: AsPath>(path: P) -> io::Result<bool> {
path.with_path(super::try_exists)
}
}

/// A file descriptor.
#[derive(Clone, Copy)]
#[rustc_layout_scalar_valid_range_start(0)]
Expand Down Expand Up @@ -325,14 +377,17 @@ fn cstr(path: &Path) -> io::Result<CString> {

impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
Self::open_native(&cstr(path)?, opts)
}
pub fn open_native(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
let flags = opts.get_access_mode()?
| opts.get_creation_mode()?
| (opts.custom_flags as c_int & !abi::O_ACCMODE);
unsafe {
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())?;
Expand Down
114 changes: 56 additions & 58 deletions library/std/src/sys/pal/unsupported/fs.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<P: AsPath>(path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn symlink_metadata<P: AsPath>(path: P) -> io::Result<FileAttr> {
unsupported()
}
pub(crate) fn metadata<P: AsPath>(path: P) -> io::Result<FileAttr> {
unsupported()
}
pub(crate) fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn hard_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn soft_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
unsupported()
}
pub(crate) fn remove_dir<P: AsPath>(path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn read_dir<P: AsPath>(path: P) -> io::Result<ReadDir> {
unsupported()
}
pub(crate) fn set_permissions<P: AsPath>(path: P, perms: FilePermissions) -> io::Result<()> {
unsupported()
}
pub(crate) fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> {
unsupported()
}
pub(crate) fn canonicalize<P: AsPath>(path: P) -> io::Result<PathBuf> {
unsupported()
}
pub(crate) fn remove_dir_all<P: AsPath>(path: P) -> io::Result<()> {
unsupported()
}
pub(crate) fn read_link<P: AsPath>(path: P) -> io::Result<PathBuf> {
unsupported()
}
pub(crate) fn try_exists<P: AsPath>(path: P) -> io::Result<bool> {
unsupported()
}
}

#[derive(Clone, Debug)]
pub struct OpenOptions {}

Expand Down Expand Up @@ -182,7 +236,7 @@ impl OpenOptions {
}

impl File {
pub fn open(_path: &Path, _opts: &OpenOptions) -> io::Result<File> {
pub fn open_native(_path: &CStr, _opts: &OpenOptions) -> io::Result<File> {
unsupported()
}

Expand Down Expand Up @@ -266,59 +320,3 @@ impl fmt::Debug for File {
self.0
}
}

pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
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<bool> {
unsupported()
}

pub fn readlink(_p: &Path) -> io::Result<PathBuf> {
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<FileAttr> {
unsupported()
}

pub fn lstat(_p: &Path) -> io::Result<FileAttr> {
unsupported()
}

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

pub fn copy(_from: &Path, _to: &Path) -> io::Result<u64> {
unsupported()
}

0 comments on commit b8bf308

Please sign in to comment.