]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys_common/fs.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys_common / fs.rs
1 #![allow(dead_code)] // not used on all platforms
2
3 use crate::fs;
4 use crate::io::{self, Error, ErrorKind};
5 use crate::path::Path;
6
7 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
8 if !from.is_file() {
9 return Err(Error::new(
10 ErrorKind::InvalidInput,
11 "the source path is not an existing regular file",
12 ));
13 }
14
15 let mut reader = fs::File::open(from)?;
16 let mut writer = fs::File::create(to)?;
17 let perm = reader.metadata()?.permissions();
18
19 let ret = io::copy(&mut reader, &mut writer)?;
20 fs::set_permissions(to, perm)?;
21 Ok(ret)
22 }
23
24 pub fn remove_dir_all(path: &Path) -> io::Result<()> {
25 let filetype = fs::symlink_metadata(path)?.file_type();
26 if filetype.is_symlink() { fs::remove_file(path) } else { remove_dir_all_recursive(path) }
27 }
28
29 fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
30 for child in fs::read_dir(path)? {
31 let child = child?;
32 if child.file_type()?.is_dir() {
33 remove_dir_all_recursive(&child.path())?;
34 } else {
35 fs::remove_file(&child.path())?;
36 }
37 }
38 fs::remove_dir(path)
39 }