Skip to content

Commit

Permalink
perf(node/fs/copy): reduce metadata lookups copying directory (#27495)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 9, 2025
1 parent f009654 commit 40ae711
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions ext/fs/std_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,30 +723,34 @@ fn cp(from: &Path, to: &Path) -> FsResult<()> {
}
}

match (fs::metadata(to), fs::symlink_metadata(to)) {
(Ok(m), _) if m.is_dir() => cp_(
source_meta,
from,
&to.join(from.file_name().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"the source path is not a valid file",
)
})?),
)?,
(_, Ok(m)) if is_identical(&source_meta, &m) => {
if let Ok(m) = fs::metadata(to) {
if m.is_dir() {
return cp_(
source_meta,
from,
&to.join(from.file_name().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"the source path is not a valid file",
)
})?),
);
}
}

if let Ok(m) = fs::symlink_metadata(to) {
if is_identical(&source_meta, &m) {
return Err(
io::Error::new(
io::ErrorKind::InvalidInput,
"the source and destination are the same file",
)
.into(),
)
);
}
_ => cp_(source_meta, from, to)?,
}

Ok(())
cp_(source_meta, from, to)
}

#[cfg(not(windows))]
Expand Down

0 comments on commit 40ae711

Please sign in to comment.