From e887cc3fbbf6e961a429aff5f3f52c8f07416fe1 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 26 Nov 2024 13:07:59 -0800 Subject: [PATCH] FetchAndUnpackNix: remove destination if it already exists (#1319) * fixup: unnnecessary clone * FetchAndUnpackNix: remove destination if it already exists --- src/action/base/fetch_and_unpack_nix.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/action/base/fetch_and_unpack_nix.rs b/src/action/base/fetch_and_unpack_nix.rs index be326d521..c3b9c6b21 100644 --- a/src/action/base/fetch_and_unpack_nix.rs +++ b/src/action/base/fetch_and_unpack_nix.rs @@ -8,6 +8,7 @@ use crate::{ action::{Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction}, parse_ssl_cert, settings::UrlOrPath, + util::OnMissing, }; /** @@ -164,7 +165,15 @@ impl Action for FetchAndUnpackNix { // TODO(@Hoverbear): Pick directory tracing::trace!("Unpacking tar.xz"); - let dest_clone = self.dest.clone(); + + // NOTE(cole-h): If the destination exists (because maybe a previous install failed), we + // want to remove it so that tar doesn't complain with: + // trying to unpack outside of destination path: /nix/temp-install-dir + if self.dest.exists() { + crate::util::remove_dir_all(&self.dest, OnMissing::Ignore) + .await + .map_err(|e| Self::error(ActionErrorKind::Remove(self.dest.clone(), e)))?; + } let decoder = xz2::read::XzDecoder::new(bytes.reader()); let mut archive = tar::Archive::new(decoder); @@ -172,7 +181,7 @@ impl Action for FetchAndUnpackNix { archive.set_preserve_mtime(true); archive.set_unpack_xattrs(true); archive - .unpack(&dest_clone) + .unpack(&self.dest) .map_err(FetchUrlError::Unarchive) .map_err(Self::error)?;