1#![deny(unsafe_op_in_unsafe_fn)]
2
3use crate::io;
4use crate::path::{Path, PathBuf};
5
6pub mod common;
7
8cfg_select! {
9 target_family = "unix" => {
10 mod unix;
11 use unix as imp;
12 pub use unix::{chown, fchown, lchown, mkfifo};
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(super) use unix::CachedFileMetadata;
18 use crate::sys::common::small_c_string::run_path_with_cstr as with_native_path;
19 }
20 target_os = "windows" => {
21 mod windows;
22 use windows as imp;
23 pub use windows::{symlink_inner, junction_point};
24 use crate::sys::path::with_native_path;
25 }
26 target_os = "hermit" => {
27 mod hermit;
28 use hermit as imp;
29 }
30 target_os = "motor" => {
31 mod motor;
32 use motor as imp;
33 }
34 target_os = "solid_asp3" => {
35 mod solid;
36 use solid as imp;
37 }
38 target_os = "uefi" => {
39 mod uefi;
40 use uefi as imp;
41 }
42 target_os = "vexos" => {
43 mod vexos;
44 use vexos as imp;
45 }
46 target_os = "wasi" => {
47 mod wasi;
48 use wasi as imp;
49 }
50 _ => {
51 mod unsupported;
52 use unsupported as imp;
53 }
54}
55
56#[cfg(not(any(target_family = "unix", target_os = "windows")))]
58#[inline]
59pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
60 f(path)
61}
62
63pub use imp::{
64 DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
65 ReadDir,
66};
67
68pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
69 imp::readdir(path)
71}
72
73pub fn remove_file(path: &Path) -> io::Result<()> {
74 with_native_path(path, &imp::unlink)
75}
76
77pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
78 with_native_path(old, &|old| with_native_path(new, &|new| imp::rename(old, new)))
79}
80
81pub fn remove_dir(path: &Path) -> io::Result<()> {
82 with_native_path(path, &imp::rmdir)
83}
84
85pub fn remove_dir_all(path: &Path) -> io::Result<()> {
86 #[cfg(not(windows))]
88 return imp::remove_dir_all(path);
89 #[cfg(windows)]
90 with_native_path(path, &imp::remove_dir_all)
91}
92
93pub fn read_link(path: &Path) -> io::Result<PathBuf> {
94 with_native_path(path, &imp::readlink)
95}
96
97pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
98 #[cfg(windows)]
100 return imp::symlink(original, link);
101 #[cfg(not(windows))]
102 with_native_path(original, &|original| {
103 with_native_path(link, &|link| imp::symlink(original, link))
104 })
105}
106
107pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
108 with_native_path(original, &|original| {
109 with_native_path(link, &|link| imp::link(original, link))
110 })
111}
112
113pub fn metadata(path: &Path) -> io::Result<FileAttr> {
114 with_native_path(path, &imp::stat)
115}
116
117pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
118 with_native_path(path, &imp::lstat)
119}
120
121pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
122 with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
123}
124
125#[cfg(all(unix, not(target_os = "vxworks")))]
126pub fn set_permissions_nofollow(path: &Path, perm: crate::fs::Permissions) -> io::Result<()> {
127 use crate::fs::OpenOptions;
128
129 let mut options = OpenOptions::new();
130
131 #[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
134 {
135 use crate::os::unix::fs::OpenOptionsExt;
136 options.custom_flags(libc::O_NOFOLLOW);
137 }
138
139 options.open(path)?.set_permissions(perm)
140}
141
142#[cfg(any(not(unix), target_os = "vxworks"))]
143pub fn set_permissions_nofollow(_path: &Path, _perm: crate::fs::Permissions) -> io::Result<()> {
144 crate::unimplemented!(
145 "`set_permissions_nofollow` is currently only implemented on Unix platforms"
146 )
147}
148
149pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
150 with_native_path(path, &imp::canonicalize)
151}
152
153pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
154 #[cfg(not(windows))]
156 return imp::copy(from, to);
157 #[cfg(windows)]
158 with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
159}
160
161pub fn exists(path: &Path) -> io::Result<bool> {
162 #[cfg(not(windows))]
164 return imp::exists(path);
165 #[cfg(windows)]
166 with_native_path(path, &imp::exists)
167}
168
169pub fn set_times(path: &Path, times: FileTimes) -> io::Result<()> {
170 with_native_path(path, &|path| imp::set_times(path, times.clone()))
171}
172
173pub fn set_times_nofollow(path: &Path, times: FileTimes) -> io::Result<()> {
174 with_native_path(path, &|path| imp::set_times_nofollow(path, times.clone()))
175}