Skip to content

Commit a519305

Browse files
committed
rustbuild: Fix copy helper with existing files
This erroneously truncated files when the destination already existed and was an existing hard link to the source. This in turn caused weird bugs! Closes #37745
1 parent 8f02c42 commit a519305

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/bootstrap/util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ pub fn mtime(path: &Path) -> FileTime {
4141
/// Copies a file from `src` to `dst`, attempting to use hard links and then
4242
/// falling back to an actually filesystem copy if necessary.
4343
pub fn copy(src: &Path, dst: &Path) {
44+
// A call to `hard_link` will fail if `dst` exists, so remove it if it
45+
// already exists so we can try to help `hard_link` succeed.
46+
let _ = fs::remove_file(&dst);
47+
48+
// Attempt to "easy copy" by creating a hard link (symlinks don't work on
49+
// windows), but if that fails just fall back to a slow `copy` operation.
4450
let res = fs::hard_link(src, dst);
4551
let res = res.or_else(|_| fs::copy(src, dst).map(|_| ()));
4652
if let Err(e) = res {

0 commit comments

Comments
 (0)