From d89f7853ecc7ab5df92d0d4fed32e41dc2014e6f Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 17 Sep 2024 10:11:31 +0000 Subject: [PATCH] Make symlink_or_hardlink_file remove dest --- src/utils/raw.rs | 5 ----- src/utils/utils.rs | 6 +++++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utils/raw.rs b/src/utils/raw.rs index 1a3ba56238..fe263e1721 100644 --- a/src/utils/raw.rs +++ b/src/utils/raw.rs @@ -226,11 +226,6 @@ fn symlink_junction_inner(target: &Path, junction: &Path) -> io::Result<()> { } } -pub(crate) fn hardlink(src: &Path, dest: &Path) -> io::Result<()> { - let _ = fs::remove_file(dest); - fs::hard_link(src, dest) -} - pub fn remove_dir(path: &Path) -> io::Result<()> { if fs::symlink_metadata(path)?.file_type().is_symlink() { #[cfg(windows)] diff --git a/src/utils/utils.rs b/src/utils/utils.rs index 8c85269e88..7344ea745f 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -312,7 +312,11 @@ where }) } +/// Attempts to symlink a file, falling back to hard linking if that fails. +/// +/// If `dest` already exists then it will be replaced. pub(crate) fn symlink_or_hardlink_file(src: &Path, dest: &Path) -> Result<()> { + let _ = fs::remove_file(dest); // The error is only used by macos let Err(_err) = symlink_file(src, dest) else { return Ok(()); @@ -332,7 +336,7 @@ pub(crate) fn symlink_or_hardlink_file(src: &Path, dest: &Path) -> Result<()> { } pub fn hardlink_file(src: &Path, dest: &Path) -> Result<()> { - raw::hardlink(src, dest).with_context(|| RustupError::LinkingFile { + fs::hard_link(src, dest).with_context(|| RustupError::LinkingFile { src: PathBuf::from(src), dest: PathBuf::from(dest), })