mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-26 14:43:24 +00:00
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:
parent
da305a2b00
commit
97baac4184
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user