| 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_if::cfg_if! { |
| 9 | if #[cfg(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(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 | // FIXME: Replace this with platform-specific path conversion functions. |
| 43 | #[cfg (not(any(target_family = "unix" , target_os = "windows" )))] |
| 44 | #[inline ] |
| 45 | pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> { |
| 46 | f(path) |
| 47 | } |
| 48 | |
| 49 | pub use imp::{ |
| 50 | DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions, |
| 51 | ReadDir, |
| 52 | }; |
| 53 | |
| 54 | pub fn read_dir(path: &Path) -> io::Result<ReadDir> { |
| 55 | // FIXME: use with_native_path on all platforms |
| 56 | imp::readdir(path) |
| 57 | } |
| 58 | |
| 59 | pub fn remove_file(path: &Path) -> io::Result<()> { |
| 60 | with_native_path(path, &imp::unlink) |
| 61 | } |
| 62 | |
| 63 | pub fn rename(old: &Path, new: &Path) -> io::Result<()> { |
| 64 | with_native_path(path:old, &|old: &CStr| with_native_path(path:new, &|new: &CStr| imp::rename(old, new))) |
| 65 | } |
| 66 | |
| 67 | pub fn remove_dir(path: &Path) -> io::Result<()> { |
| 68 | with_native_path(path, &imp::rmdir) |
| 69 | } |
| 70 | |
| 71 | pub fn remove_dir_all(path: &Path) -> io::Result<()> { |
| 72 | // FIXME: use with_native_path on all platforms |
| 73 | #[cfg (not(windows))] |
| 74 | return imp::remove_dir_all(path); |
| 75 | #[cfg (windows)] |
| 76 | with_native_path(path, &imp::remove_dir_all) |
| 77 | } |
| 78 | |
| 79 | pub fn read_link(path: &Path) -> io::Result<PathBuf> { |
| 80 | with_native_path(path, &imp::readlink) |
| 81 | } |
| 82 | |
| 83 | pub fn symlink(original: &Path, link: &Path) -> io::Result<()> { |
| 84 | // FIXME: use with_native_path on all platforms |
| 85 | #[cfg (windows)] |
| 86 | return imp::symlink(original, link); |
| 87 | #[cfg (not(windows))] |
| 88 | with_native_path(path:original, &|original: &CStr| { |
| 89 | with_native_path(path:link, &|link: &CStr| imp::symlink(original, link)) |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | pub fn hard_link(original: &Path, link: &Path) -> io::Result<()> { |
| 94 | with_native_path(path:original, &|original: &CStr| { |
| 95 | with_native_path(path:link, &|link: &CStr| imp::link(original, link)) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | pub fn metadata(path: &Path) -> io::Result<FileAttr> { |
| 100 | with_native_path(path, &imp::stat) |
| 101 | } |
| 102 | |
| 103 | pub fn symlink_metadata(path: &Path) -> io::Result<FileAttr> { |
| 104 | with_native_path(path, &imp::lstat) |
| 105 | } |
| 106 | |
| 107 | pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> { |
| 108 | with_native_path(path, &|path: &CStr| imp::set_perm(p:path, perm.clone())) |
| 109 | } |
| 110 | |
| 111 | pub fn canonicalize(path: &Path) -> io::Result<PathBuf> { |
| 112 | with_native_path(path, &imp::canonicalize) |
| 113 | } |
| 114 | |
| 115 | pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { |
| 116 | // FIXME: use with_native_path on all platforms |
| 117 | #[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 | |
| 123 | pub fn exists(path: &Path) -> io::Result<bool> { |
| 124 | // FIXME: use with_native_path on all platforms |
| 125 | #[cfg (not(windows))] |
| 126 | return imp::exists(path); |
| 127 | #[cfg (windows)] |
| 128 | with_native_path(path, &imp::exists) |
| 129 | } |
| 130 | |