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), })