From b4a60e2ae227627de5ce7cafc84cdbf53b071507 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Tue, 13 Jan 2026 14:54:56 +0100 Subject: [PATCH] deb: cleanup ephemeral context only on success --- src/deb/ephemeral.rs | 62 ++++++++++++++++++++++++++++++-------------- src/deb/mod.rs | 7 ++++- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/deb/ephemeral.rs b/src/deb/ephemeral.rs index f4f3c5d..5f52a63 100644 --- a/src/deb/ephemeral.rs +++ b/src/deb/ephemeral.rs @@ -12,6 +12,7 @@ use xz2::read::XzDecoder; pub struct EphemeralContextGuard { previous_context: String, chroot_path: PathBuf, + build_succeeded: bool, } impl EphemeralContextGuard { @@ -41,6 +42,7 @@ impl EphemeralContextGuard { Ok(Self { previous_context: current_context_name, chroot_path, + build_succeeded: false, }) } @@ -222,6 +224,11 @@ impl EphemeralContextGuard { Ok(()) } + + /// Mark the build as successful, which will trigger chroot cleanup on drop + pub fn mark_build_successful(&mut self) { + self.build_succeeded = true; + } } impl Drop for EphemeralContextGuard { @@ -232,31 +239,46 @@ impl Drop for EphemeralContextGuard { log::error!("Failed to restore context {}: {}", self.previous_context, e); } - // Remove chroot directory - // We use the restored context to execute the cleanup command - let result = context::current() - .command("sudo") - .arg("rm") - .arg("-rf") - .arg(&self.chroot_path) - .status(); + // Remove chroot directory only if build succeeded + if self.build_succeeded { + log::debug!( + "Build succeeded, removing chroot directory: {}", + self.chroot_path.display() + ); + let result = context::current() + .command("sudo") + .arg("rm") + .arg("-rf") + .arg(&self.chroot_path) + .status(); - match result { - Ok(status) => { - if !status.success() { + match result { + Ok(status) => { + if !status.success() { + log::error!( + "Failed to remove chroot directory {}", + self.chroot_path.display() + ); + } else { + log::debug!( + "Successfully removed chroot directory: {}", + self.chroot_path.display() + ); + } + } + Err(e) => { log::error!( - "Failed to remove chroot directory {}", - self.chroot_path.display() + "Failed to execute cleanup command for {}: {}", + self.chroot_path.display(), + e ); } } - Err(e) => { - log::error!( - "Failed to execute cleanup command for {}: {}", - self.chroot_path.display(), - e - ); - } + } else { + log::debug!( + "Build did not succeed or was not marked as successful, keeping chroot directory: {}", + self.chroot_path.display() + ); } } } diff --git a/src/deb/mod.rs b/src/deb/mod.rs index 758aea4..5858c25 100644 --- a/src/deb/mod.rs +++ b/src/deb/mod.rs @@ -48,7 +48,7 @@ pub fn build_binary_package( }; // Create an ephemeral unshare context for all Local builds - let _guard = if mode == BuildMode::Local { + let mut guard = if mode == BuildMode::Local { Some(ephemeral::EphemeralContextGuard::new(series)?) } else { None @@ -82,6 +82,11 @@ pub fn build_binary_package( } } + // Mark build as successful to trigger chroot cleanup + if let Some(ref mut g) = guard { + g.mark_build_successful(); + } + Ok(()) }