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, ExtraHomeDirs, ExtraMediaDirs, File, FileAttr, FilePermissions,
65 FileTimes, FileType, OpenOptions, 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 rename_noreplace(old: &Path, new: &Path) -> io::Result<()> {
82 with_native_path(old, &|old| with_native_path(new, &|new| imp::rename_noreplace(old, new)))
83}
84
85pub fn remove_dir(path: &Path) -> io::Result<()> {
86 with_native_path(path, &imp::rmdir)
87}
88
89pub fn remove_dir_all(path: &Path) -> io::Result<()> {
90 #[cfg(not(windows))]
92 return imp::remove_dir_all(path);
93 #[cfg(windows)]
94 with_native_path(path, &imp::remove_dir_all)
95}
96
97pub fn read_link(path: &Path) -> io::Result<PathBuf> {
98 with_native_path(path, &imp::readlink)
99}
100
101pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
102 #[cfg(windows)]
104 return imp::symlink(original, link);
105 #[cfg(not(windows))]
106 with_native_path(original, &|original| {
107 with_native_path(link, &|link| imp::symlink(original, link))
108 })
109}
110
111pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
112 with_native_path(original, &|original| {
113 with_native_path(link, &|link| imp::link(original, link))
114 })
115}
116
117pub fn metadata(path: &Path) -> io::Result<FileAttr> {
118 with_native_path(path, &imp::stat)
119}
120
121pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
122 with_native_path(path, &imp::lstat)
123}
124
125pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
126 with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
127}
128
129pub fn set_permissions_nofollow(path: &Path, perm: FilePermissions) -> io::Result<()> {
130 with_native_path(path, &|path| imp::set_perm_nofollow(path, perm.clone()))
131}
132
133pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
134 with_native_path(path, &imp::canonicalize)
135}
136
137pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
138 #[cfg(not(windows))]
140 return imp::copy(from, to);
141 #[cfg(windows)]
142 with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
143}
144
145pub fn exists(path: &Path) -> io::Result<bool> {
146 #[cfg(not(windows))]
148 return imp::exists(path);
149 #[cfg(windows)]
150 with_native_path(path, &imp::exists)
151}
152
153pub fn set_times(path: &Path, times: FileTimes) -> io::Result<()> {
154 with_native_path(path, &|path| imp::set_times(path, times.clone()))
155}
156
157pub fn set_times_nofollow(path: &Path, times: FileTimes) -> io::Result<()> {
158 with_native_path(path, &|path| imp::set_times_nofollow(path, times.clone()))
159}