std/sys/fs/
mod.rs

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