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 use crate::sys::path::with_native_path;
24 } else if #[cfg(target_os = "hermit")] {
25 mod hermit;
26 use hermit as imp;
27 } else if #[cfg(target_os = "solid_asp3")] {
28 mod solid;
29 use solid as imp;
30 } else if #[cfg(target_os = "uefi")] {
31 mod uefi;
32 use uefi as imp;
33 } else if #[cfg(target_os = "wasi")] {
34 mod wasi;
35 use wasi as imp;
36 } else {
37 mod unsupported;
38 use unsupported as imp;
39 }
40}
41
42#[cfg(not(any(target_family = "unix", target_os = "windows")))]
44#[inline]
45pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
46 f(path)
47}
48
49pub use imp::{
50 DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
51 ReadDir,
52};
53
54pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
55 imp::readdir(path)
57}
58
59pub fn remove_file(path: &Path) -> io::Result<()> {
60 with_native_path(path, &imp::unlink)
61}
62
63pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
64 with_native_path(old, &|old| with_native_path(new, &|new| imp::rename(old, new)))
65}
66
67pub fn remove_dir(path: &Path) -> io::Result<()> {
68 with_native_path(path, &imp::rmdir)
69}
70
71pub fn remove_dir_all(path: &Path) -> io::Result<()> {
72 #[cfg(not(windows))]
74 return imp::remove_dir_all(path);
75 #[cfg(windows)]
76 with_native_path(path, &imp::remove_dir_all)
77}
78
79pub fn read_link(path: &Path) -> io::Result<PathBuf> {
80 with_native_path(path, &imp::readlink)
81}
82
83pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
84 #[cfg(windows)]
86 return imp::symlink(original, link);
87 #[cfg(not(windows))]
88 with_native_path(original, &|original| {
89 with_native_path(link, &|link| imp::symlink(original, link))
90 })
91}
92
93pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> {
94 with_native_path(original, &|original| {
95 with_native_path(link, &|link| imp::link(original, link))
96 })
97}
98
99pub fn metadata(path: &Path) -> io::Result<FileAttr> {
100 with_native_path(path, &imp::stat)
101}
102
103pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> {
104 with_native_path(path, &imp::lstat)
105}
106
107pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
108 with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
109}
110
111pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
112 with_native_path(path, &imp::canonicalize)
113}
114
115pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
116 #[cfg(not(windows))]
118 return imp::copy(from, to);
119 #[cfg(windows)]
120 with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
121}
122
123pub fn exists(path: &Path) -> io::Result<bool> {
124 #[cfg(not(windows))]
126 return imp::exists(path);
127 #[cfg(windows)]
128 with_native_path(path, &imp::exists)
129}