Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(node/fs/copy): reduce metadata lookups copying directory #27495

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's faster because we avoid the second symlink_metadata lookup when the first metadata call is a directory.

(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
Loading