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:
commit
1f9dc9a182
1 changed files with 6 additions and 4 deletions
|
@ -5,19 +5,21 @@ use crate::io::{self, Error, ErrorKind};
|
||||||
use crate::path::Path;
|
use crate::path::Path;
|
||||||
|
|
||||||
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
|
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(
|
return Err(Error::new(
|
||||||
ErrorKind::InvalidInput,
|
ErrorKind::InvalidInput,
|
||||||
"the source path is not an existing regular file",
|
"the source path is not an existing regular file",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut reader = fs::File::open(from)?;
|
|
||||||
let mut writer = fs::File::create(to)?;
|
let mut writer = fs::File::create(to)?;
|
||||||
let perm = reader.metadata()?.permissions();
|
let perm = metadata.permissions();
|
||||||
|
|
||||||
let ret = io::copy(&mut reader, &mut writer)?;
|
let ret = io::copy(&mut reader, &mut writer)?;
|
||||||
fs::set_permissions(to, perm)?;
|
writer.set_permissions(perm)?;
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue