From 3fc8583646ace6202072ef75c7b0987ffb69949e Mon Sep 17 00:00:00 2001 From: liushuyu Date: Wed, 30 Mar 2022 00:30:25 -0600 Subject: [PATCH] tasks: uninstall the program as clean as possible --- src/native/mod.rs | 21 ++++++++++++--------- src/tasks/uninstall_pkg.rs | 22 +++++++++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/native/mod.rs b/src/native/mod.rs index 442c900..db3566e 100644 --- a/src/native/mod.rs +++ b/src/native/mod.rs @@ -200,8 +200,10 @@ mod natives { .to_str() .log_expect("Unable to convert log path to string") .replace(" ", "\\ "); + + let install_path = path.to_str().log_expect("Unable to convert path to string").replace(" ", "\\ "); - let target_arguments = format!("/C choice /C Y /N /D Y /T 2 & del {} {}", tool, log); + let target_arguments = format!("/C choice /C Y /N /D Y /T 2 & del {} {} & rmdir {}", tool, log, install_path); info!("Launching cmd with {:?}", target_arguments); @@ -331,7 +333,7 @@ mod natives { #[cfg(not(windows))] mod natives { - use std::fs::remove_file; + use std::fs::{remove_file, remove_dir}; use std::env; @@ -421,19 +423,20 @@ mod natives { /// Cleans up the installer pub fn burn_on_exit(app_name: &str) { let current_exe = env::current_exe().log_expect("Current executable could not be found"); + let exe_dir = current_exe.parent().log_expect("Current executable directory cannot be found"); + + if let Err(e) = remove_file(exe_dir.join(format!("{}_installer.log", app_name))) { + // No regular logging now. + eprintln!("Failed to delete maintenance log: {:?}", e); + }; // Thank god for *nix platforms if let Err(e) = remove_file(¤t_exe) { // No regular logging now. eprintln!("Failed to delete maintenancetool: {:?}", e); }; - - let current_dir = env::current_dir().log_expect("Current directory cannot be found"); - - if let Err(e) = remove_file(current_dir.join(format!("{}_installer.log", app_name))) { - // No regular logging now. - eprintln!("Failed to delete installer log: {:?}", e); - }; + // delete the directory if not empty and ignore errors (since we can't handle errors anymore) + remove_dir(exe_dir).ok(); } /// Returns a list of running processes diff --git a/src/tasks/uninstall_pkg.rs b/src/tasks/uninstall_pkg.rs index 2e1dc58..ebdc85f 100644 --- a/src/tasks/uninstall_pkg.rs +++ b/src/tasks/uninstall_pkg.rs @@ -44,7 +44,7 @@ impl Task for UninstallPackageTask { } } - let mut package = match metadata { + let package = match metadata { Some(v) => v, None => { if self.optional { @@ -63,9 +63,8 @@ impl Task for UninstallPackageTask { 0.0, )); - // Reverse, as to delete directories last - package.files.reverse(); - + let mut directories = Vec::new(); + let max = package.files.len(); for (i, file) in package.files.iter().enumerate() { let name = file.clone(); @@ -78,7 +77,9 @@ impl Task for UninstallPackageTask { )); let result = if file.is_dir() { - remove_dir(file) + // we don't delete directory just yet + directories.push(file); + Ok(()) } else { remove_file(file) }; @@ -88,6 +89,17 @@ impl Task for UninstallPackageTask { } } + // sort directories by reverse depth order + directories.sort_by(|a, b| { + let depth_a = a.components().fold(0usize, |acc, _| acc + 1); + let depth_b = b.components().fold(0usize, |acc, _| acc + 1); + depth_b.cmp(&depth_a) + }); + for i in directories.iter() { + info!("Deleting directory: {:?}", i); + remove_dir(i).ok(); + } + Ok(TaskParamType::None) }