Auto merge of #80755 - sunfishcode:path-cleanup/copy, r=nagisa

Optimize away some path lookups in the generic `fs::copy` implementation

This also eliminates a use of a `Path` convenience function, in support
of #80741, refactoring `std::path` to focus on pure data structures and
algorithms.
This commit is contained in:
bors 2021-01-09 07:48:53 +00:00
commit 1f9dc9a182

View file

@ -5,19 +5,21 @@ use crate::io::{self, Error, ErrorKind};
use crate::path::Path;
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
if !from.is_file() {
let mut reader = fs::File::open(from)?;
let metadata = reader.metadata()?;
if !metadata.is_file() {
return Err(Error::new(
ErrorKind::InvalidInput,
"the source path is not an existing regular file",
));
}
let mut reader = fs::File::open(from)?;
let mut writer = fs::File::create(to)?;
let perm = reader.metadata()?.permissions();
let perm = metadata.permissions();
let ret = io::copy(&mut reader, &mut writer)?;
fs::set_permissions(to, perm)?;
writer.set_permissions(perm)?;
Ok(ret)
}