Only fall back to copy when the first hard linking failed (#268)

Hard linking might not be supported but we (afaik) can't detect this
ahead of time, so we'll try hard linking the first file, if this
succeeds we'll know later hard linking errors are not due to lack of
os/fs support, if it fails we'll switch to copying for the rest of the
install. Follow up to
https://github.com/astral-sh/puffin/pull/237#discussion_r1376705137
This commit is contained in:
konsti 2023-11-01 18:35:52 +01:00 committed by GitHub
parent b0678aa6fc
commit c6aa1cd7a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -361,7 +361,11 @@ fn hardlink_wheel_files(
) -> Result<usize, Error> {
let mut count = 0usize;
// Avoid causing the same error for every file
// Hard linking might not be supported but we (afaik) can't detect this ahead of time, so we'll
// try hard linking the first file, if this succeeds we'll know later hard linking errors are
// not due to lack of os/fs support, if it fails we'll switch to copying for the rest of the
// install
let mut first_try_hard_linking = true;
let mut use_copy_fallback = false;
// Walk over the directory.
@ -383,10 +387,15 @@ fn hardlink_wheel_files(
} else {
let hard_link_result = fs::hard_link(entry.path(), &out_path);
// Once https://github.com/rust-lang/rust/issues/86442 is stable, use that
if hard_link_result.is_err() {
fs::copy(entry.path(), &out_path)?;
use_copy_fallback = true;
if let Err(err) = hard_link_result {
if first_try_hard_linking {
fs::copy(entry.path(), &out_path)?;
use_copy_fallback = true;
} else {
return Err(err.into());
}
}
first_try_hard_linking = false;
}
count += 1;