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

@ -201,7 +201,9 @@ mod natives {
.log_expect("Unable to convert log path to string")
.replace(" ", "\\ ");
let target_arguments = format!("/C choice /C Y /N /D Y /T 2 & del {} {}", tool, log);
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 {} {} & 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(&current_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

View file

@ -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,8 +63,7 @@ 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() {
@ -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)
}