Skip to content

Commit

Permalink
Auto merge of #80755 - sunfishcode:path-cleanup/copy, r=nagisa
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jan 9, 2021
2 parents c87ef0a + 97baac4 commit 1f9dc9a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions library/std/src/sys_common/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 1f9dc9a

Please sign in to comment.