Skip to content
/ rust Public
forked from rust-lang/rust

Commit 429b676

Browse files
authored
Rollup merge of rust-lang#138832 - ChrisDenton:with_native_path, r=joboet
Start using `with_native_path` in `std::sys::fs` Ideally, each platform should use their own native path type internally. This will, for example, allow passing a `CStr` directly to `std::fs::File::open` and therefore avoid the need for allocating a new null-terminated C string. However, doing that for every function and platform all at once makes for a large PR that is way too prone to breaking. So this PR does some minimal refactoring which should help progress towards that goal. The changes are Unix-only and even then I avoided functions that require more changes so that this PR is just moving things around. r? joboet
2 parents 2777f4a + 265ee9c commit 429b676

File tree

3 files changed

+178
-114
lines changed

3 files changed

+178
-114
lines changed

library/std/src/fs.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
23702370
#[doc(alias = "rm", alias = "unlink", alias = "DeleteFile")]
23712371
#[stable(feature = "rust1", since = "1.0.0")]
23722372
pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
2373-
fs_imp::unlink(path.as_ref())
2373+
fs_imp::remove_file(path.as_ref())
23742374
}
23752375

23762376
/// Given a path, queries the file system to get information about a file,
@@ -2409,7 +2409,7 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
24092409
#[doc(alias = "stat")]
24102410
#[stable(feature = "rust1", since = "1.0.0")]
24112411
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2412-
fs_imp::stat(path.as_ref()).map(Metadata)
2412+
fs_imp::metadata(path.as_ref()).map(Metadata)
24132413
}
24142414

24152415
/// Queries the metadata about a file without following symlinks.
@@ -2444,7 +2444,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
24442444
#[doc(alias = "lstat")]
24452445
#[stable(feature = "symlink_metadata", since = "1.1.0")]
24462446
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2447-
fs_imp::lstat(path.as_ref()).map(Metadata)
2447+
fs_imp::symlink_metadata(path.as_ref()).map(Metadata)
24482448
}
24492449

24502450
/// Renames a file or directory to a new name, replacing the original file if
@@ -2598,7 +2598,7 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
25982598
#[doc(alias = "CreateHardLink", alias = "linkat")]
25992599
#[stable(feature = "rust1", since = "1.0.0")]
26002600
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
2601-
fs_imp::link(original.as_ref(), link.as_ref())
2601+
fs_imp::hard_link(original.as_ref(), link.as_ref())
26022602
}
26032603

26042604
/// Creates a new symbolic link on the filesystem.
@@ -2664,7 +2664,7 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
26642664
/// ```
26652665
#[stable(feature = "rust1", since = "1.0.0")]
26662666
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2667-
fs_imp::readlink(path.as_ref())
2667+
fs_imp::read_link(path.as_ref())
26682668
}
26692669

26702670
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2840,7 +2840,7 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
28402840
#[doc(alias = "rmdir", alias = "RemoveDirectory")]
28412841
#[stable(feature = "rust1", since = "1.0.0")]
28422842
pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2843-
fs_imp::rmdir(path.as_ref())
2843+
fs_imp::remove_dir(path.as_ref())
28442844
}
28452845

28462846
/// Removes a directory at this path, after removing all its contents. Use
@@ -2967,7 +2967,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
29672967
#[doc(alias = "ls", alias = "opendir", alias = "FindFirstFile", alias = "FindNextFile")]
29682968
#[stable(feature = "rust1", since = "1.0.0")]
29692969
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2970-
fs_imp::readdir(path.as_ref()).map(ReadDir)
2970+
fs_imp::read_dir(path.as_ref()).map(ReadDir)
29712971
}
29722972

29732973
/// Changes the permissions found on a file or a directory.
@@ -3003,7 +3003,7 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
30033003
#[doc(alias = "chmod", alias = "SetFileAttributes")]
30043004
#[stable(feature = "set_permissions", since = "1.1.0")]
30053005
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
3006-
fs_imp::set_perm(path.as_ref(), perm.0)
3006+
fs_imp::set_permissions(path.as_ref(), perm.0)
30073007
}
30083008

30093009
impl DirBuilder {

library/std/src/sys/fs/mod.rs

+92-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,113 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
22

3+
use crate::io;
4+
use crate::path::{Path, PathBuf};
5+
36
pub mod common;
47

58
cfg_if::cfg_if! {
69
if #[cfg(target_family = "unix")] {
710
mod unix;
8-
pub use unix::*;
11+
use unix as imp;
12+
pub use unix::{chown, fchown, lchown, chroot};
13+
pub(crate) use unix::debug_assert_fd_is_open;
14+
#[cfg(any(target_os = "linux", target_os = "android"))]
15+
pub(crate) use unix::CachedFileMetadata;
16+
use crate::sys::common::small_c_string::run_path_with_cstr as with_native_path;
917
} else if #[cfg(target_os = "windows")] {
1018
mod windows;
11-
pub use windows::*;
19+
use windows as imp;
20+
pub use windows::{symlink_inner, junction_point};
1221
} else if #[cfg(target_os = "hermit")] {
1322
mod hermit;
14-
pub use hermit::*;
23+
use hermit as imp;
1524
} else if #[cfg(target_os = "solid_asp3")] {
1625
mod solid;
17-
pub use solid::*;
26+
use solid as imp;
1827
} else if #[cfg(target_os = "uefi")] {
1928
mod uefi;
20-
pub use uefi::*;
29+
use uefi as imp;
2130
} else if #[cfg(target_os = "wasi")] {
2231
mod wasi;
23-
pub use wasi::*;
32+
use wasi as imp;
2433
} else {
2534
mod unsupported;
26-
pub use unsupported::*;
35+
use unsupported as imp;
2736
}
2837
}
38+
39+
// FIXME: Replace this with platform-specific path conversion functions.
40+
#[cfg(not(target_family = "unix"))]
41+
#[inline]
42+
pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
43+
f(path)
44+
}
45+
46+
pub use imp::{
47+
DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
48+
ReadDir,
49+
};
50+
51+
pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
52+
// FIXME: use with_native_path
53+
imp::readdir(path)
54+
}
55+
56+
pub fn remove_file(path: &Path) -> io::Result<()> {
57+
with_native_path(path, &imp::unlink)
58+
}
59+
60+
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
61+
with_native_path(old, &|old| with_native_path(new, &|new| imp::rename(old, new)))
62+
}
63+
64+
pub fn remove_dir(path: &Path) -> io::Result<()> {
65+
with_native_path(path, &imp::rmdir)
66+
}
67+
68+
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
69+
// FIXME: use with_native_path
70+
imp::remove_dir_all(path)
71+
}
72+
73+
pub fn read_link(path: &Path) -> io::Result<PathBuf> {
74+
with_native_path(path, &imp::readlink)
75+
}
76+
77+
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
78+
with_native_path(original, &|original| {
79+
with_native_path(link, &|link| imp::symlink(original, link))
80+
})
81+
}
82+
83+
pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
84+
with_native_path(original, &|original| {
85+
with_native_path(link, &|link| imp::link(original, link))
86+
})
87+
}
88+
89+
pub fn metadata(path: &Path) -> io::Result<FileAttr> {
90+
with_native_path(path, &imp::stat)
91+
}
92+
93+
pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
94+
with_native_path(path, &imp::lstat)
95+
}
96+
97+
pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
98+
with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
99+
}
100+
101+
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
102+
with_native_path(path, &imp::canonicalize)
103+
}
104+
105+
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
106+
// FIXME: use with_native_path
107+
imp::copy(from, to)
108+
}
109+
110+
pub fn exists(path: &Path) -> io::Result<bool> {
111+
// FIXME: use with_native_path
112+
imp::exists(path)
113+
}

0 commit comments

Comments
 (0)