From c6aa1cd7a31d5aa782cb1b64d5aebd48df03bf36 Mon Sep 17 00:00:00 2001 From: konsti Date: Wed, 1 Nov 2023 18:35:52 +0100 Subject: [PATCH] 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 --- crates/install-wheel-rs/src/linker.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/install-wheel-rs/src/linker.rs b/crates/install-wheel-rs/src/linker.rs index e702d98b5..306906ab5 100644 --- a/crates/install-wheel-rs/src/linker.rs +++ b/crates/install-wheel-rs/src/linker.rs @@ -361,7 +361,11 @@ fn hardlink_wheel_files( ) -> Result { 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;