1#![allow(dead_code)] use crate::fs::{create_dir, remove_dir, remove_file, rename};
4use crate::io::{self, Error, ErrorKind};
5use crate::path::{Path, PathBuf};
6use crate::sys::IntoInner;
7use crate::sys::fs::{File, FileAttr, OpenOptions};
8use crate::sys::helpers::ignore_notfound;
9use crate::{fmt, fs};
10
11pub(crate) const NOT_FILE_ERROR: Error = ::core::hint::must_use(::core::io::Error::from_static_message(const {
&::core::io::SimpleMessage {
kind: ErrorKind::InvalidInput,
message: "the source path is neither a regular file nor a symlink to a regular file",
}
}))io::const_error!(
12 ErrorKind::InvalidInput,
13 "the source path is neither a regular file nor a symlink to a regular file",
14);
15
16pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
17 let mut reader = fs::File::open(from)?;
18 let metadata = reader.metadata()?;
19
20 if !metadata.is_file() {
21 return Err(NOT_FILE_ERROR);
22 }
23
24 let mut writer = fs::File::create(to)?;
25 let perm = metadata.permissions();
26
27 let ret = io::copy(&mut reader, &mut writer)?;
28 writer.set_permissions(perm)?;
29 Ok(ret)
30}
31
32pub fn remove_dir_all(path: &Path) -> io::Result<()> {
33 let filetype = fs::symlink_metadata(path)?.file_type();
34 if filetype.is_symlink() { fs::remove_file(path) } else { remove_dir_all_recursive(path) }
35}
36
37fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
38 for child in fs::read_dir(path)? {
39 let result: io::Result<()> = try {
40 let child = child?;
41 if child.file_type()?.is_dir() {
42 remove_dir_all_recursive(&child.path())?;
43 } else {
44 fs::remove_file(&child.path())?;
45 }
46 };
47 if let Err(err) = &result
49 && err.kind() != io::ErrorKind::NotFound
50 {
51 return result;
52 }
53 }
54 ignore_notfound(fs::remove_dir(path))
55}
56
57pub fn exists(path: &Path) -> io::Result<bool> {
58 match fs::metadata(path) {
59 Ok(_) => Ok(true),
60 Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
61 Err(error) => Err(error),
62 }
63}
64
65pub struct Dir {
66 path: PathBuf,
67}
68
69impl Dir {
70 pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result<Self> {
71 path.canonicalize().map(|path| Self { path })
72 }
73
74 pub fn open_for_traversal(path: &Path) -> io::Result<Self> {
75 let mut opts = OpenOptions::new();
76 opts.read(true);
77 Self::open(path, &opts)
78 }
79
80 pub fn duplicate(&self) -> io::Result<Self> {
81 Ok(Self { path: self.path.clone() })
82 }
83
84 pub fn open_file(&self, path: &Path, opts: &OpenOptions) -> io::Result<File> {
85 File::open(&self.path.join(path), opts)
86 }
87
88 pub fn self_metadata(&self) -> io::Result<FileAttr> {
89 self.path.metadata().map(|m| m.into_inner())
90 }
91
92 pub fn remove_file(&self, path: &Path) -> io::Result<()> {
93 remove_file(self.path.join(path))
94 }
95
96 pub fn rename(&self, from: &Path, to_dir: &Self, to: &Path) -> io::Result<()> {
97 rename(self.path.join(from), to_dir.path.join(to))
98 }
99
100 pub fn create_dir(&self, path: &Path) -> io::Result<()> {
101 create_dir(self.path.join(path))
102 }
103
104 pub fn open_dir(&self, path: &Path, opts: &OpenOptions) -> io::Result<Self> {
105 Self::open(&self.path.join(path), opts)
106 }
107
108 pub fn remove_dir(&self, path: &Path) -> io::Result<()> {
109 remove_dir(self.path.join(path))
110 }
111
112 pub fn metadata(&self, path: &Path) -> io::Result<FileAttr> {
113 self.path.join(path).metadata().map(|m| m.into_inner())
114 }
115
116 pub fn symlink_metadata(&self, path: &Path) -> io::Result<FileAttr> {
117 self.path.join(path).symlink_metadata().map(|m| m.into_inner())
118 }
119}
120
121impl fmt::Debug for Dir {
122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123 f.debug_struct("Dir").field("path", &self.path).finish()
124 }
125}
126
127#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ExtraHomeDirs {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "ExtraHomeDirs")
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ExtraHomeDirs {
#[inline]
fn clone(&self) -> Self { ExtraHomeDirs {} }
}Clone, #[automatically_derived]
impl ::core::default::Default for ExtraHomeDirs {
#[inline]
fn default() -> Self { ExtraHomeDirs {} }
}Default)]
128#[non_exhaustive]
129pub struct ExtraHomeDirs {}
130
131#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ExtraMediaDirs {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "ExtraMediaDirs")
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ExtraMediaDirs {
#[inline]
fn clone(&self) -> Self { ExtraMediaDirs {} }
}Clone, #[automatically_derived]
impl ::core::default::Default for ExtraMediaDirs {
#[inline]
fn default() -> Self { ExtraMediaDirs {} }
}Default)]
132#[non_exhaustive]
133pub struct ExtraMediaDirs {}