Skip to main content

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_select! {
9    any(target_family = "unix", target_os = "wasi") => {
10        mod unix;
11        use unix as imp;
12        #[cfg(any(target_os = "linux", target_os = "android"))]
13        pub(super) use unix::CachedFileMetadata;
14        #[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
15        pub use unix::chroot;
16        #[cfg(not(target_os = "wasi"))]
17        pub(crate) use unix::debug_assert_fd_is_open;
18        #[cfg(not(target_os = "wasi"))]
19        pub use unix::{chown, fchown, lchown, mkfifo};
20
21        use crate::sys::helpers::run_path_with_cstr as with_native_path;
22    }
23    target_os = "windows" => {
24        mod windows;
25        use windows as imp;
26        pub use windows::{junction_point, symlink_inner};
27
28        use crate::sys::path::with_native_path;
29    }
30    target_os = "hermit" => {
31        mod hermit;
32        use hermit as imp;
33    }
34    target_os = "motor" => {
35        mod motor;
36        use motor as imp;
37    }
38    target_os = "solid_asp3" => {
39        mod solid;
40        use solid as imp;
41    }
42    target_os = "uefi" => {
43        mod uefi;
44        use uefi as imp;
45    }
46    target_os = "vexos" => {
47        mod vexos;
48        use vexos as imp;
49    }
50    target_vendor = "apple" => {
51        mod darwin;
52    }
53    _ => {
54        mod unsupported;
55        use unsupported as imp;
56    }
57}
58
59// FIXME: Replace this with platform-specific path conversion functions.
60#[cfg(not(any(target_family = "unix", target_os = "windows", target_os = "wasi")))]
61#[inline]
62pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
63    f(path)
64}
65
66pub use imp::{
67    Dir, DirBuilder, DirEntry, ExtraHomeDirs, ExtraMediaDirs, File, FileAttr, FilePermissions,
68    FileTimes, FileType, OpenOptions, ReadDir,
69};
70
71pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
72    // FIXME: use with_native_path on all platforms
73    imp::readdir(path)
74}
75
76pub fn remove_file(path: &Path) -> io::Result<()> {
77    with_native_path(path, &imp::unlink)
78}
79
80pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
81    with_native_path(old, &|old| with_native_path(new, &|new| imp::rename(old, new)))
82}
83
84pub fn remove_dir(path: &Path) -> io::Result<()> {
85    with_native_path(path, &imp::rmdir)
86}
87
88pub fn remove_dir_all(path: &Path) -> io::Result<()> {
89    // FIXME: use with_native_path on all platforms
90    #[cfg(not(windows))]
91    return imp::remove_dir_all(path);
92    #[cfg(windows)]
93    with_native_path(path, &imp::remove_dir_all)
94}
95
96pub fn read_link(path: &Path) -> io::Result<PathBuf> {
97    with_native_path(path, &imp::readlink)
98}
99
100pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
101    // FIXME: use with_native_path on all platforms
102    #[cfg(windows)]
103    return imp::symlink(original, link);
104    #[cfg(not(windows))]
105    with_native_path(original, &|original| {
106        with_native_path(link, &|link| imp::symlink(original, link))
107    })
108}
109
110pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
111    with_native_path(original, &|original| {
112        with_native_path(link, &|link| imp::link(original, link))
113    })
114}
115
116pub fn metadata(path: &Path) -> io::Result<FileAttr> {
117    with_native_path(path, &imp::stat)
118}
119
120pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
121    with_native_path(path, &imp::lstat)
122}
123
124pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
125    with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
126}
127
128pub fn set_permissions_nofollow(path: &Path, perm: FilePermissions) -> io::Result<()> {
129    with_native_path(path, &|path| imp::set_perm_nofollow(path, perm.clone()))
130}
131
132pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
133    with_native_path(path, &imp::canonicalize)
134}
135
136pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
137    // FIXME: use with_native_path on all platforms
138    #[cfg(not(windows))]
139    return imp::copy(from, to);
140    #[cfg(windows)]
141    with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
142}
143
144pub fn exists(path: &Path) -> io::Result<bool> {
145    // FIXME: use with_native_path on all platforms
146    #[cfg(not(windows))]
147    return imp::exists(path);
148    #[cfg(windows)]
149    with_native_path(path, &imp::exists)
150}
151
152pub fn set_times(path: &Path, times: FileTimes) -> io::Result<()> {
153    with_native_path(path, &|path| imp::set_times(path, times.clone()))
154}
155
156pub fn set_times_nofollow(path: &Path, times: FileTimes) -> io::Result<()> {
157    with_native_path(path, &|path| imp::set_times_nofollow(path, times.clone()))
158}