Skip to content

Commit 98ace21

Browse files
committed
Start using with_native_path in std::sys::fs
1 parent db68788 commit 98ace21

File tree

4 files changed

+181
-118
lines changed

4 files changed

+181
-118
lines changed

library/std/src/fs.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ impl AsInner<fs_imp::DirEntry> for DirEntry {
23622362
#[doc(alias = "rm", alias = "unlink", alias = "DeleteFile")]
23632363
#[stable(feature = "rust1", since = "1.0.0")]
23642364
pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
2365-
fs_imp::unlink(path.as_ref())
2365+
fs_imp::remove_file(path.as_ref())
23662366
}
23672367

23682368
/// Given a path, queries the file system to get information about a file,
@@ -2401,7 +2401,7 @@ pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
24012401
#[doc(alias = "stat")]
24022402
#[stable(feature = "rust1", since = "1.0.0")]
24032403
pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2404-
fs_imp::stat(path.as_ref()).map(Metadata)
2404+
fs_imp::metadata(path.as_ref()).map(Metadata)
24052405
}
24062406

24072407
/// Queries the metadata about a file without following symlinks.
@@ -2436,7 +2436,7 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
24362436
#[doc(alias = "lstat")]
24372437
#[stable(feature = "symlink_metadata", since = "1.1.0")]
24382438
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
2439-
fs_imp::lstat(path.as_ref()).map(Metadata)
2439+
fs_imp::symlink_metadata(path.as_ref()).map(Metadata)
24402440
}
24412441

24422442
/// Renames a file or directory to a new name, replacing the original file if
@@ -2590,7 +2590,7 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
25902590
#[doc(alias = "CreateHardLink", alias = "linkat")]
25912591
#[stable(feature = "rust1", since = "1.0.0")]
25922592
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
2593-
fs_imp::link(original.as_ref(), link.as_ref())
2593+
fs_imp::hard_link(original.as_ref(), link.as_ref())
25942594
}
25952595

25962596
/// Creates a new symbolic link on the filesystem.
@@ -2622,7 +2622,7 @@ pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
26222622
std::os::windows::fs::{symlink_file, symlink_dir}"
26232623
)]
26242624
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
2625-
fs_imp::symlink(original.as_ref(), link.as_ref())
2625+
fs_imp::soft_link(original.as_ref(), link.as_ref())
26262626
}
26272627

26282628
/// Reads a symbolic link, returning the file that the link points to.
@@ -2656,7 +2656,7 @@ pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Re
26562656
/// ```
26572657
#[stable(feature = "rust1", since = "1.0.0")]
26582658
pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
2659-
fs_imp::readlink(path.as_ref())
2659+
fs_imp::read_link(path.as_ref())
26602660
}
26612661

26622662
/// Returns the canonical, absolute form of a path with all intermediate
@@ -2832,7 +2832,7 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
28322832
#[doc(alias = "rmdir", alias = "RemoveDirectory")]
28332833
#[stable(feature = "rust1", since = "1.0.0")]
28342834
pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
2835-
fs_imp::rmdir(path.as_ref())
2835+
fs_imp::remove_dir(path.as_ref())
28362836
}
28372837

28382838
/// Removes a directory at this path, after removing all its contents. Use
@@ -2959,7 +2959,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
29592959
#[doc(alias = "ls", alias = "opendir", alias = "FindFirstFile", alias = "FindNextFile")]
29602960
#[stable(feature = "rust1", since = "1.0.0")]
29612961
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
2962-
fs_imp::readdir(path.as_ref()).map(ReadDir)
2962+
fs_imp::read_dir(path.as_ref()).map(ReadDir)
29632963
}
29642964

29652965
/// Changes the permissions found on a file or a directory.
@@ -2995,7 +2995,7 @@ pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
29952995
#[doc(alias = "chmod", alias = "SetFileAttributes")]
29962996
#[stable(feature = "set_permissions", since = "1.1.0")]
29972997
pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
2998-
fs_imp::set_perm(path.as_ref(), perm.0)
2998+
fs_imp::set_permissions(path.as_ref(), perm.0)
29992999
}
30003000

30013001
impl DirBuilder {

library/std/src/os/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl DirEntryExt2 for fs::DirEntry {
975975
/// ```
976976
#[stable(feature = "symlink", since = "1.1.0")]
977977
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
978-
sys::fs::symlink(original.as_ref(), link.as_ref())
978+
sys::fs::soft_link(original.as_ref(), link.as_ref())
979979
}
980980

981981
/// Unix-specific extensions to [`fs::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+
#[allow(unused)]
16+
pub(crate) use CachedFileMetadata;
17+
use crate::sys::common::small_c_string::run_path_with_cstr as with_native_path;
918
} else if #[cfg(target_os = "windows")] {
1019
mod windows;
11-
pub use windows::*;
20+
use windows as imp;
21+
pub use windows::{symlink_inner, junction_point};
1222
} else if #[cfg(target_os = "hermit")] {
1323
mod hermit;
14-
pub use hermit::*;
24+
use hermit as imp;
1525
} else if #[cfg(target_os = "solid_asp3")] {
1626
mod solid;
17-
pub use solid::*;
27+
use solid as imp;
1828
} else if #[cfg(target_os = "uefi")] {
1929
mod uefi;
20-
pub use uefi::*;
30+
use uefi as imp;
2131
} else if #[cfg(target_os = "wasi")] {
2232
mod wasi;
23-
pub use wasi::*;
33+
use wasi as imp;
2434
} else {
2535
mod unsupported;
26-
pub use unsupported::*;
36+
use unsupported as imp;
2737
}
2838
}
39+
40+
// FIXME: Replace this with platform-specific path conversion functions.
41+
#[cfg(not(target_family = "unix"))]
42+
#[inline]
43+
pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
44+
f(path)
45+
}
46+
47+
pub use imp::{
48+
DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
49+
ReadDir,
50+
};
51+
52+
pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
53+
// FIXME: use with_native_path
54+
imp::readdir(path)
55+
}
56+
57+
pub fn remove_file(path: &Path) -> io::Result<()> {
58+
with_native_path(path, &imp::unlink)
59+
}
60+
61+
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
62+
with_native_path(old, &|old| with_native_path(new, &|new| imp::rename(old, new)))
63+
}
64+
65+
pub fn remove_dir(path: &Path) -> io::Result<()> {
66+
with_native_path(path, &imp::rmdir)
67+
}
68+
69+
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
70+
with_native_path(path, &imp::remove_dir_all)
71+
}
72+
73+
pub fn read_link(path: &Path) -> io::Result<PathBuf> {
74+
with_native_path(path, &imp::readlink)
75+
}
76+
77+
pub fn soft_link(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)