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 _ => {
51 mod unsupported;
52 use unsupported as imp;
53 }
54}
55
56#[cfg(not(any(target_family = "unix", target_os = "windows", target_os = "wasi")))]
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 Dir, 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
125pub fn set_permissions_nofollow(path: &Path, perm: FilePermissions) -> io::Result<()> {
126 with_native_path(path, &|path| imp::set_perm_nofollow(path, perm.clone()))
127}
128
129pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
130 with_native_path(path, &imp::canonicalize)
131}
132
133pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
134 #[cfg(not(windows))]
136 return imp::copy(from, to);
137 #[cfg(windows)]
138 with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
139}
140
141pub fn exists(path: &Path) -> io::Result<bool> {
142 #[cfg(not(windows))]
144 return imp::exists(path);
145 #[cfg(windows)]
146 with_native_path(path, &imp::exists)
147}
148
149pub fn set_times(path: &Path, times: FileTimes) -> io::Result<()> {
150 with_native_path(path, &|path| imp::set_times(path, times.clone()))
151}
152
153pub fn set_times_nofollow(path: &Path, times: FileTimes) -> io::Result<()> {
154 with_native_path(path, &|path| imp::set_times_nofollow(path, times.clone()))
155}