| 1 | #![deny (unsafe_op_in_unsafe_fn)] |
| 2 | |
| 3 | use crate::io; |
| 4 | use crate::path::{Path, PathBuf}; |
| 5 | |
| 6 | pub mod common; |
| 7 | |
| 8 | cfg_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 | // FIXME: Replace this with platform-specific path conversion functions. |
| 57 | #[cfg (not(any(target_family = "unix" , target_os = "windows" )))] |
| 58 | #[inline ] |
| 59 | pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> { |
| 60 | f(path) |
| 61 | } |
| 62 | |
| 63 | pub use imp::{ |
| 64 | DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions, |
| 65 | ReadDir, |
| 66 | }; |
| 67 | |
| 68 | pub fn read_dir(path: &Path) -> io::Result<ReadDir> { |
| 69 | // FIXME: use with_native_path on all platforms |
| 70 | imp::readdir(path) |
| 71 | } |
| 72 | |
| 73 | pub fn remove_file(path: &Path) -> io::Result<()> { |
| 74 | with_native_path(path, &imp::unlink) |
| 75 | } |
| 76 | |
| 77 | pub fn rename(old: &Path, new: &Path) -> io::Result<()> { |
| 78 | with_native_path(path:old, &|old: &CStr| with_native_path(path:new, &|new: &CStr| imp::rename(old, new))) |
| 79 | } |
| 80 | |
| 81 | pub fn remove_dir(path: &Path) -> io::Result<()> { |
| 82 | with_native_path(path, &imp::rmdir) |
| 83 | } |
| 84 | |
| 85 | pub fn remove_dir_all(path: &Path) -> io::Result<()> { |
| 86 | // FIXME: use with_native_path on all platforms |
| 87 | #[cfg (not(windows))] |
| 88 | return imp::remove_dir_all(path); |
| 89 | #[cfg (windows)] |
| 90 | with_native_path(path, &imp::remove_dir_all) |
| 91 | } |
| 92 | |
| 93 | pub fn read_link(path: &Path) -> io::Result<PathBuf> { |
| 94 | with_native_path(path, &imp::readlink) |
| 95 | } |
| 96 | |
| 97 | pub fn symlink(original: &Path, link: &Path) -> io::Result<()> { |
| 98 | // FIXME: use with_native_path on all platforms |
| 99 | #[cfg (windows)] |
| 100 | return imp::symlink(original, link); |
| 101 | #[cfg (not(windows))] |
| 102 | with_native_path(path:original, &|original: &CStr| { |
| 103 | with_native_path(path:link, &|link: &CStr| imp::symlink(original, link)) |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> { |
| 108 | with_native_path(path:original, &|original: &CStr| { |
| 109 | with_native_path(path:link, &|link: &CStr| imp::link(original, link)) |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | pub fn metadata(path: &Path) -> io::Result<FileAttr> { |
| 114 | with_native_path(path, &imp::stat) |
| 115 | } |
| 116 | |
| 117 | pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> { |
| 118 | with_native_path(path, &imp::lstat) |
| 119 | } |
| 120 | |
| 121 | pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> { |
| 122 | with_native_path(path, &|path: &CStr| imp::set_perm(p:path, perm.clone())) |
| 123 | } |
| 124 | |
| 125 | #[cfg (all(unix, not(target_os = "vxworks" )))] |
| 126 | pub fn set_permissions_nofollow(path: &Path, perm: crate::fs::Permissions) -> io::Result<()> { |
| 127 | use crate::fs::OpenOptions; |
| 128 | |
| 129 | let mut options: OpenOptions = OpenOptions::new(); |
| 130 | |
| 131 | // ESP-IDF and Horizon do not support O_NOFOLLOW, so we skip setting it. |
| 132 | // Their filesystems do not have symbolic links, so no special handling is required. |
| 133 | #[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" ))] |
| 143 | pub 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 | |
| 149 | pub fn canonicalize(path: &Path) -> io::Result<PathBuf> { |
| 150 | with_native_path(path, &imp::canonicalize) |
| 151 | } |
| 152 | |
| 153 | pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { |
| 154 | // FIXME: use with_native_path on all platforms |
| 155 | #[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 | |
| 161 | pub fn exists(path: &Path) -> io::Result<bool> { |
| 162 | // FIXME: use with_native_path on all platforms |
| 163 | #[cfg (not(windows))] |
| 164 | return imp::exists(path); |
| 165 | #[cfg (windows)] |
| 166 | with_native_path(path, &imp::exists) |
| 167 | } |
| 168 | |
| 169 | pub fn set_times(path: &Path, times: FileTimes) -> io::Result<()> { |
| 170 | with_native_path(path, &|path: &CStr| imp::set_times(p:path, times.clone())) |
| 171 | } |
| 172 | |
| 173 | pub fn set_times_nofollow(path: &Path, times: FileTimes) -> io::Result<()> { |
| 174 | with_native_path(path, &|path: &CStr| imp::set_times_nofollow(p:path, times.clone())) |
| 175 | } |
| 176 | |