Skip to content

Commit 582d6ee

Browse files
committed
Start using with_native_path in std::sys::fs
1 parent db68788 commit 582d6ee

File tree

3 files changed

+178
-116
lines changed

3 files changed

+178
-116
lines changed

library/std/src/fs.rs

+8-8
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.
@@ -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/sys/fs/mod.rs

+91-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,112 @@
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+
with_native_path(path, &imp::remove_dir_all)
70+
}
71+
72+
pub fn read_link(path: &Path) -> io::Result<PathBuf> {
73+
with_native_path(path, &imp::readlink)
74+
}
75+
76+
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
77+
with_native_path(original, &|original| {
78+
with_native_path(link, &|link| imp::symlink(original, link))
79+
})
80+
}
81+
82+
pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
83+
with_native_path(original, &|original| {
84+
with_native_path(link, &|link| imp::link(original, link))
85+
})
86+
}
87+
88+
pub fn metadata(path: &Path) -> io::Result<FileAttr> {
89+
with_native_path(path, &imp::stat)
90+
}
91+
92+
pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
93+
with_native_path(path, &imp::lstat)
94+
}
95+
96+
pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
97+
with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
98+
}
99+
100+
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
101+
with_native_path(path, &imp::canonicalize)
102+
}
103+
104+
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
105+
// FIXME: use with_native_path
106+
imp::copy(from, to)
107+
}
108+
109+
pub fn exists(path: &Path) -> io::Result<bool> {
110+
// FIXME: use with_native_path
111+
imp::exists(path)
112+
}

0 commit comments

Comments
 (0)