Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fs_native_path #108981

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
118 changes: 60 additions & 58 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mod tests;
use crate::ffi::OsString;
use crate::fmt;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
use crate::path::{Path, PathBuf};
use crate::path::{AsPath, Path, PathBuf};
use crate::sealed::Sealed;
use crate::sync::Arc;
use crate::sys::fs as fs_imp;
use crate::sys;
use crate::sys::fs::fs_imp;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::time::SystemTime;

Expand Down Expand Up @@ -256,16 +257,16 @@ pub struct DirBuilder {
/// }
/// ```
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
fn inner(path: &Path) -> io::Result<Vec<u8>> {
let mut file = File::open(path)?;
pub fn read<P: AsPath>(path: P) -> io::Result<Vec<u8>> {
fn inner(mut file: File) -> io::Result<Vec<u8>> {
let size = file.metadata().map(|m| m.len() as usize).ok();
let mut bytes = Vec::new();
bytes.try_reserve_exact(size.unwrap_or(0)).map_err(|_| io::ErrorKind::OutOfMemory)?;
io::default_read_to_end(&mut file, &mut bytes, size)?;
Ok(bytes)
}
inner(path.as_ref())
let file = File::open(path)?;
inner(file)
}

/// Read the entire contents of a file into a string.
Expand Down Expand Up @@ -299,16 +300,16 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
/// }
/// ```
#[stable(feature = "fs_read_write", since = "1.26.0")]
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
fn inner(path: &Path) -> io::Result<String> {
let mut file = File::open(path)?;
pub fn read_to_string<P: AsPath>(path: P) -> io::Result<String> {
fn inner(mut file: File) -> io::Result<String> {
let size = file.metadata().map(|m| m.len() as usize).ok();
let mut string = String::new();
string.try_reserve_exact(size.unwrap_or(0)).map_err(|_| io::ErrorKind::OutOfMemory)?;
io::default_read_to_string(&mut file, &mut string, size)?;
Ok(string)
}
inner(path.as_ref())
let file = File::open(path)?;
inner(file)
}

/// Write a slice as the entire contents of a file.
Expand Down Expand Up @@ -336,11 +337,12 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
/// }
/// ```
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
File::create(path)?.write_all(contents)
pub fn write<P: AsPath, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
fn inner(mut file: File, contents: &[u8]) -> io::Result<()> {
file.write_all(contents)
}
inner(path.as_ref(), contents.as_ref())
let file = File::create(path)?;
inner(file, contents.as_ref())
}

impl File {
Expand Down Expand Up @@ -371,8 +373,8 @@ impl File {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new().read(true).open(path.as_ref())
pub fn open<P: AsPath>(path: P) -> io::Result<File> {
OpenOptions::new().read(true).open(path)
}

/// Opens a file in write-only mode.
Expand Down Expand Up @@ -400,8 +402,8 @@ impl File {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
pub fn create<P: AsPath>(path: P) -> io::Result<File> {
OpenOptions::new().write(true).create(true).truncate(true).open(path)
}

/// Creates a new file in read-write mode; error if the file exists.
Expand Down Expand Up @@ -429,8 +431,8 @@ impl File {
/// }
/// ```
#[stable(feature = "file_create_new", since = "1.77.0")]
pub fn create_new<P: AsRef<Path>>(path: P) -> io::Result<File> {
OpenOptions::new().read(true).write(true).create_new(true).open(path.as_ref())
pub fn create_new<P: AsPath>(path: P) -> io::Result<File> {
OpenOptions::new().read(true).write(true).create_new(true).open(path)
}

/// Returns a new OpenOptions object.
Expand Down Expand Up @@ -1127,12 +1129,12 @@ impl OpenOptions {
/// [`NotFound`]: io::ErrorKind::NotFound
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
#[stable(feature = "rust1", since = "1.0.0")]
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
self._open(path.as_ref())
pub fn open<P: AsPath>(&self, path: P) -> io::Result<File> {
path.with_native_path(|path| self._open(path))
}

fn _open(&self, path: &Path) -> io::Result<File> {
fs_imp::File::open(path, &self.0).map(|inner| File { inner })
fn _open(&self, path: &sys::path::NativePath) -> io::Result<File> {
fs_imp::File::open_native(path, &self.0).map(|inner| File { inner })
}
}

Expand Down Expand Up @@ -1884,8 +1886,8 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
/// ```
#[doc(alias = "rm", alias = "unlink", alias = "DeleteFile")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
fs_imp::unlink(path.as_ref())
pub fn remove_file<P: AsPath>(path: P) -> io::Result<()> {
fs_imp::remove_file(path)
}

/// Given a path, query the file system to get information about a file,
Expand Down Expand Up @@ -1923,8 +1925,8 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// ```
#[doc(alias = "stat")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
fs_imp::stat(path.as_ref()).map(Metadata)
pub fn metadata<P: AsPath>(path: P) -> io::Result<Metadata> {
fs_imp::metadata(path).map(Metadata)
}

/// Query the metadata about a file without following symlinks.
Expand Down Expand Up @@ -1958,8 +1960,8 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
/// ```
#[doc(alias = "lstat")]
#[stable(feature = "symlink_metadata", since = "1.1.0")]
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
fs_imp::lstat(path.as_ref()).map(Metadata)
pub fn symlink_metadata<P: AsPath>(path: P) -> io::Result<Metadata> {
fs_imp::symlink_metadata(path).map(Metadata)
}

/// Rename a file or directory to a new name, replacing the original file if
Expand Down Expand Up @@ -2002,8 +2004,8 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
/// ```
#[doc(alias = "mv", alias = "MoveFile", alias = "MoveFileEx")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
fs_imp::rename(from.as_ref(), to.as_ref())
pub fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<()> {
fs_imp::rename(from, to)
}

/// Copies the contents of one file to another. This function will also
Expand Down Expand Up @@ -2063,8 +2065,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
#[doc(alias = "CopyFile", alias = "CopyFileEx")]
#[doc(alias = "fclonefileat", alias = "fcopyfile")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
fs_imp::copy(from.as_ref(), to.as_ref())
pub fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> {
fs_imp::copy(from, to)
}

/// Creates a new hard link on the filesystem.
Expand Down Expand Up @@ -2108,8 +2110,8 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
/// ```
#[doc(alias = "CreateHardLink", alias = "linkat")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
fs_imp::link(original.as_ref(), link.as_ref())
pub fn hard_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
fs_imp::hard_link(original, link)
}

/// Creates a new symbolic link on the filesystem.
Expand Down Expand Up @@ -2140,8 +2142,8 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
note = "replaced with std::os::unix::fs::symlink and \
std::os::windows::fs::{symlink_file, symlink_dir}"
)]
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
fs_imp::symlink(original.as_ref(), link.as_ref())
pub fn soft_link<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
fs_imp::soft_link(original, link)
}

/// Reads a symbolic link, returning the file that the link points to.
Expand Down Expand Up @@ -2174,8 +2176,8 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
fs_imp::readlink(path.as_ref())
pub fn read_link<P: AsPath>(path: P) -> io::Result<PathBuf> {
fs_imp::read_link(path)
}

/// Returns the canonical, absolute form of a path with all intermediate
Expand Down Expand Up @@ -2217,8 +2219,8 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
#[doc(alias = "realpath")]
#[doc(alias = "GetFinalPathNameByHandle")]
#[stable(feature = "fs_canonicalize", since = "1.5.0")]
pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
fs_imp::canonicalize(path.as_ref())
pub fn canonicalize<P: AsPath>(path: P) -> io::Result<PathBuf> {
fs_imp::canonicalize(path)
}

/// Creates a new, empty directory at the provided path
Expand Down Expand Up @@ -2259,8 +2261,8 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
#[doc(alias = "mkdir", alias = "CreateDirectory")]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "fs_create_dir")]
pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
DirBuilder::new().create(path.as_ref())
pub fn create_dir<P: AsPath>(path: P) -> io::Result<()> {
DirBuilder::new().create(path)
}

/// Recursively create a directory and all of its parent components if they
Expand Down Expand Up @@ -2303,8 +2305,8 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
DirBuilder::new().recursive(true).create(path.as_ref())
pub fn create_dir_all<P: AsPath>(path: P) -> io::Result<()> {
DirBuilder::new().recursive(true).create(path)
}

/// Removes an empty directory.
Expand Down Expand Up @@ -2339,8 +2341,8 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// ```
#[doc(alias = "rmdir", alias = "RemoveDirectory")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
fs_imp::rmdir(path.as_ref())
pub fn remove_dir<P: AsPath>(path: P) -> io::Result<()> {
fs_imp::remove_dir(path)
}

/// Removes a directory at this path, after removing all its contents. Use
Expand Down Expand Up @@ -2386,8 +2388,8 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
fs_imp::remove_dir_all(path.as_ref())
pub fn remove_dir_all<P: AsPath>(path: P) -> io::Result<()> {
fs_imp::remove_dir_all(path)
}

/// Returns an iterator over the entries within a directory.
Expand Down Expand Up @@ -2462,8 +2464,8 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// ```
#[doc(alias = "ls", alias = "opendir", alias = "FindFirstFile", alias = "FindNextFile")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
fs_imp::readdir(path.as_ref()).map(ReadDir)
pub fn read_dir<P: AsPath>(path: P) -> io::Result<ReadDir> {
fs_imp::read_dir(path).map(ReadDir)
}

/// Changes the permissions found on a file or a directory.
Expand Down Expand Up @@ -2498,8 +2500,8 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
/// ```
#[doc(alias = "chmod", alias = "SetFileAttributes")]
#[stable(feature = "set_permissions", since = "1.1.0")]
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
fs_imp::set_perm(path.as_ref(), perm.0)
pub fn set_permissions<P: AsPath>(path: P, perm: Permissions) -> io::Result<()> {
fs_imp::set_permissions(path, perm.0)
}

impl DirBuilder {
Expand Down Expand Up @@ -2558,8 +2560,8 @@ impl DirBuilder {
/// assert!(fs::metadata(path).unwrap().is_dir());
/// ```
#[stable(feature = "dir_builder", since = "1.6.0")]
pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self._create(path.as_ref())
pub fn create<P: AsPath>(&self, path: P) -> io::Result<()> {
path.with_path(|path| self._create(path))
}

fn _create(&self, path: &Path) -> io::Result<()> {
Expand Down Expand Up @@ -2630,6 +2632,6 @@ impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
// instead.
#[unstable(feature = "fs_try_exists", issue = "83186")]
#[inline]
pub fn try_exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
fs_imp::try_exists(path.as_ref())
pub fn try_exists<P: AsPath>(path: P) -> io::Result<bool> {
fs_imp::try_exists(path)
}
19 changes: 19 additions & 0 deletions library/std/src/os/unix/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,24 @@

mod os_str;

use crate::ffi::CStr;
use crate::path::NativePath;

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::os_str::{OsStrExt, OsStringExt};

#[unstable(feature = "fs_native_path", issue = "108979")]
pub trait NativePathExt: crate::sealed::Sealed {
fn from_cstr(cstr: &CStr) -> &NativePath;
fn into_cstr(&self) -> &CStr;
}

#[unstable(feature = "fs_native_path", issue = "108979")]
impl NativePathExt for NativePath {
fn from_cstr(cstr: &CStr) -> &NativePath {
unsafe { &*(cstr as *const CStr as *const NativePath) }
}
fn into_cstr(&self) -> &CStr {
unsafe { &*(self as *const Self as *const CStr) }
}
}
6 changes: 3 additions & 3 deletions library/std/src/os/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::platform::fs::MetadataExt as _;
use crate::fs::{self, OpenOptions, Permissions};
use crate::io;
use crate::os::unix::io::{AsFd, AsRawFd};
use crate::path::Path;
use crate::path::{AsPath, Path};
use crate::sys;
use crate::sys_common::{AsInner, AsInnerMut, FromInner};
// Used for `File::read` on intra-doc links
Expand Down Expand Up @@ -954,8 +954,8 @@ impl DirEntryExt2 for fs::DirEntry {
/// }
/// ```
#[stable(feature = "symlink", since = "1.1.0")]
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
sys::fs::symlink(original.as_ref(), link.as_ref())
pub fn symlink<P: AsPath, Q: AsPath>(original: P, link: Q) -> io::Result<()> {
sys::fs::fs_imp::soft_link(original, link)
}

/// Unix-specific extensions to [`fs::DirBuilder`].
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/os/wasi/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,9 @@ pub fn symlink<P: AsRef<Path>, U: AsRef<Path>>(
/// This is a convenience API similar to `std::os::unix::fs::symlink` and
/// `std::os::windows::fs::symlink_file` and `std::os::windows::fs::symlink_dir`.
pub fn symlink_path<P: AsRef<Path>, U: AsRef<Path>>(old_path: P, new_path: U) -> io::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these methods be updated to use AsPath instead?

crate::sys::fs::symlink(old_path.as_ref(), new_path.as_ref())
crate::sys::common::small_c_string::run_path_with_cstr(new_path.as_ref(), &|new_path| {
crate::sys::fs::symlink(old_path.as_ref(), new_path)
})
}

fn osstr2str(f: &OsStr) -> io::Result<&str> {
Expand Down
Loading
Loading