std/sys/fs/
common.rs

1#![allow(dead_code)] // not used on all platforms
2
3use crate::fs;
4use crate::io::{self, Error, ErrorKind};
5use crate::path::Path;
6use crate::sys_common::ignore_notfound;
7
8pub(crate) const NOT_FILE_ERROR: Error = io::const_error!(
9    ErrorKind::InvalidInput,
10    "the source path is neither a regular file nor a symlink to a regular file",
11);
12
13pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
14    let mut reader = fs::File::open(from)?;
15    let metadata = reader.metadata()?;
16
17    if !metadata.is_file() {
18        return Err(NOT_FILE_ERROR);
19    }
20
21    let mut writer = fs::File::create(to)?;
22    let perm = metadata.permissions();
23
24    let ret = io::copy(&mut reader, &mut writer)?;
25    writer.set_permissions(perm)?;
26    Ok(ret)
27}
28
29pub fn remove_dir_all(path: &Path) -> io::Result<()> {
30    let filetype = fs::symlink_metadata(path)?.file_type();
31    if filetype.is_symlink() { fs::remove_file(path) } else { remove_dir_all_recursive(path) }
32}
33
34fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
35    for child in fs::read_dir(path)? {
36        let result: io::Result<()> = try {
37            let child = child?;
38            if child.file_type()?.is_dir() {
39                remove_dir_all_recursive(&child.path())?;
40            } else {
41                fs::remove_file(&child.path())?;
42            }
43        };
44        // ignore internal NotFound errors to prevent race conditions
45        if let Err(err) = &result
46            && err.kind() != io::ErrorKind::NotFound
47        {
48            return result;
49        }
50    }
51    ignore_notfound(fs::remove_dir(path))
52}
53
54pub fn exists(path: &Path) -> io::Result<bool> {
55    match fs::metadata(path) {
56        Ok(_) => Ok(true),
57        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
58        Err(error) => Err(error),
59    }
60}