tasks: uninstall the program as clean as possible

This commit is contained in:
liushuyu 2022-03-30 00:30:25 -06:00
parent a3f0d0f999
commit 3fc8583646
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437
2 changed files with 29 additions and 14 deletions

View File

@ -200,8 +200,10 @@ mod natives {
.to_str() .to_str()
.log_expect("Unable to convert log path to string") .log_expect("Unable to convert log path to string")
.replace(" ", "\\ "); .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); info!("Launching cmd with {:?}", target_arguments);
@ -331,7 +333,7 @@ mod natives {
#[cfg(not(windows))] #[cfg(not(windows))]
mod natives { mod natives {
use std::fs::remove_file; use std::fs::{remove_file, remove_dir};
use std::env; use std::env;
@ -421,19 +423,20 @@ mod natives {
/// Cleans up the installer /// Cleans up the installer
pub fn burn_on_exit(app_name: &str) { pub fn burn_on_exit(app_name: &str) {
let current_exe = env::current_exe().log_expect("Current executable could not be found"); 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 // Thank god for *nix platforms
if let Err(e) = remove_file(&current_exe) { if let Err(e) = remove_file(&current_exe) {
// No regular logging now. // No regular logging now.
eprintln!("Failed to delete maintenancetool: {:?}", e); eprintln!("Failed to delete maintenancetool: {:?}", e);
}; };
// delete the directory if not empty and ignore errors (since we can't handle errors anymore)
let current_dir = env::current_dir().log_expect("Current directory cannot be found"); remove_dir(exe_dir).ok();
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);
};
} }
/// Returns a list of running processes /// Returns a list of running processes

View File

@ -44,7 +44,7 @@ impl Task for UninstallPackageTask {
} }
} }
let mut package = match metadata { let package = match metadata {
Some(v) => v, Some(v) => v,
None => { None => {
if self.optional { if self.optional {
@ -63,9 +63,8 @@ impl Task for UninstallPackageTask {
0.0, 0.0,
)); ));
// Reverse, as to delete directories last let mut directories = Vec::new();
package.files.reverse();
let max = package.files.len(); let max = package.files.len();
for (i, file) in package.files.iter().enumerate() { for (i, file) in package.files.iter().enumerate() {
let name = file.clone(); let name = file.clone();
@ -78,7 +77,9 @@ impl Task for UninstallPackageTask {
)); ));
let result = if file.is_dir() { let result = if file.is_dir() {
remove_dir(file) // we don't delete directory just yet
directories.push(file);
Ok(())
} else { } else {
remove_file(file) 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) Ok(TaskParamType::None)
} }