1#![allow(dead_code)] use crate::io::{self, Error, ErrorKind};
4use crate::path::{Path, PathBuf};
5use crate::sys::IntoInner;
6use crate::sys::fs::{File, FileAttr, OpenOptions};
7use crate::sys::helpers::ignore_notfound;
8use crate::{fmt, fs};
9
10pub(crate) const NOT_FILE_ERROR: Error = io::const_error!(
11 ErrorKind::InvalidInput,
12 "the source path is neither a regular file nor a symlink to a regular file",
13);
14
15pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
16 let mut reader = fs::File::open(from)?;
17 let metadata = reader.metadata()?;
18
19 if !metadata.is_file() {
20 return Err(NOT_FILE_ERROR);
21 }
22
23 let mut writer = fs::File::create(to)?;
24 let perm = metadata.permissions();
25
26 let ret = io::copy(&mut reader, &mut writer)?;
27 writer.set_permissions(perm)?;
28 Ok(ret)
29}
30
31pub fn remove_dir_all(path: &Path) -> io::Result<()> {
32 let filetype = fs::symlink_metadata(path)?.file_type();
33 if filetype.is_symlink() { fs::remove_file(path) } else { remove_dir_all_recursive(path) }
34}
35
36fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
37 for child in fs::read_dir(path)? {
38 let result: io::Result<()> = try {
39 let child = child?;
40 if child.file_type()?.is_dir() {
41 remove_dir_all_recursive(&child.path())?;
42 } else {
43 fs::remove_file(&child.path())?;
44 }
45 };
46 if let Err(err) = &result
48 && err.kind() != io::ErrorKind::NotFound
49 {
50 return result;
51 }
52 }
53 ignore_notfound(fs::remove_dir(path))
54}
55
56pub fn exists(path: &Path) -> io::Result<bool> {
57 match fs::metadata(path) {
58 Ok(_) => Ok(true),
59 Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
60 Err(error) => Err(error),
61 }
62}
63
64pub struct Dir {
65 path: PathBuf,
66}
67
68impl Dir {
69 pub fn open(path: &Path, _opts: &OpenOptions) -> io::Result<Self> {
70 path.canonicalize().map(|path| Self { path })
71 }
72
73 pub fn open_file(&self, path: &Path, opts: &OpenOptions) -> io::Result<File> {
74 File::open(&self.path.join(path), &opts)
75 }
76
77 pub fn metadata(&self) -> io::Result<FileAttr> {
78 self.path.metadata().map(|m| m.into_inner())
79 }
80}
81
82impl fmt::Debug for Dir {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 f.debug_struct("Dir").field("path", &self.path).finish()
85 }
86}