common.rs - source
Description: Source of the Rust file `library/std/src/sys/fs/common.rs`.

#
std/sys/fs/
common.rs

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