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

Linker copies files as a fallback when ref-linking fails #1773

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Changes from 3 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
23 changes: 19 additions & 4 deletions crates/install-wheel-rs/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,37 @@ fn clone_recursive(site_packages: &Path, wheel: &Path, entry: &DirEntry) -> Resu
debug!("Cloning {} to {}", from.display(), to.display());

// Attempt to copy the file or directory
let reflink = reflink_copy::reflink(&from, &to);
let mut reflink = reflink_copy::reflink_or_copy(&from, &to);
snowsignal marked this conversation as resolved.
Show resolved Hide resolved

let entry_file_type = entry.file_type()?;

// `reflink_or_copy` returns `InvalidInput` on macOS when reflinking a directory/symlink that already exists
// at the destination - macOS has the capability to reflink those, but `reflink_or_copy` doesn't make an exception
// and obscures the underlying `AlreadyExists` error. We need to properly fallback in this case,
// so we run `reflink` and check if that gives us an `AlreadyExists` error.
if reflink
.as_ref()
.is_err_and(|err| err.kind() == std::io::ErrorKind::InvalidInput)
&& cfg!(target_os = "macos")
&& !entry_file_type.is_file()
{
reflink = reflink_copy::reflink(&from, &to).map(|()| None);
}

if reflink
.as_ref()
.is_err_and(|err| matches!(err.kind(), std::io::ErrorKind::AlreadyExists))
.is_err_and(|err| err.kind() == std::io::ErrorKind::AlreadyExists)
{
// If copying fails and the directory exists already, it must be merged recursively.
if entry.file_type()?.is_dir() {
if entry_file_type.is_dir() {
for entry in fs::read_dir(from)? {
clone_recursive(site_packages, wheel, &entry?)?;
}
} else {
// If file already exists, overwrite it.
let tempdir = tempdir_in(site_packages)?;
let tempfile = tempdir.path().join(from.file_name().unwrap());
reflink_copy::reflink(from, &tempfile)?;
reflink_copy::reflink_or_copy(from, &tempfile)?;
fs::rename(&tempfile, to)?;
}
} else {
Expand Down