From 3ff35b2e62de3afdf590ab7f053cb1668e5a5f59 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 8 Aug 2018 20:22:59 +1000 Subject: [PATCH] Delete the maintenance tool binary itself after uninstall (closes #1) --- src/installer.rs | 5 +++++ src/native/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/rest.rs | 14 +++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/installer.rs b/src/installer.rs index c94c857..c482e44 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -63,6 +63,8 @@ pub struct InstallerFramework { pub install_path: Option, pub preexisting_install: bool, pub is_launcher: bool, + // If we just completed an uninstall, and we should clean up after ourselves. + pub burn_after_exit: bool, pub launcher_path: Option, } @@ -199,6 +201,7 @@ impl InstallerFramework { .map_err(|x| format!("Failed to delete metadata: {:?}", x))?; // Logging will have to be done later + self.burn_after_exit = true; Ok(()) } @@ -254,6 +257,7 @@ impl InstallerFramework { install_path: None, preexisting_install: false, is_launcher: false, + burn_after_exit: false, launcher_path: None, } } @@ -280,6 +284,7 @@ impl InstallerFramework { install_path: Some(path), preexisting_install: true, is_launcher: false, + burn_after_exit: false, launcher_path: None, }) } diff --git a/src/native/mod.rs b/src/native/mod.rs index 64851e9..acbd117 100644 --- a/src/native/mod.rs +++ b/src/native/mod.rs @@ -11,6 +11,8 @@ mod natives { use logging::LoggingErrors; use std::env; + use std::path::PathBuf; + use std::process::Command; include!(concat!(env!("OUT_DIR"), "/interop.rs")); @@ -59,10 +61,39 @@ mod natives { )), } } + + /// Cleans up the installer + pub fn burn_on_exit(path: PathBuf) { + // Need a cmd workaround here. + let tool = path.join("maintenancetool.exe"); + let tool = tool + .to_str() + .log_expect("Unable to convert tool path to string") + .replace(" ", "\\ "); + + let log = path.join("installer.log"); + let log = log + .to_str() + .log_expect("Unable to convert log path to string") + .replace(" ", "\\ "); + + let target_arguments = format!("ping 127.0.0.1 -n 6 > nul && del {} {}", tool, log); + + info!("Launching cmd with {:?}", target_arguments); + + Command::new("C:\\Windows\\system32\\cmd.exe") + .arg("/C") + .arg(&target_arguments) + .spawn() + .log_expect("Unable to start child process"); + } } #[cfg(not(windows))] mod natives { + use std::fs::remove_file; + use std::path::PathBuf; + pub fn create_shortcut( name: &str, description: &str, @@ -75,6 +106,20 @@ mod natives { Ok("".to_string()) } + + /// Cleans up the installer + pub fn burn_on_exit(path: PathBuf) { + // Thank god for *nix platforms + if let Err(e) = remove_file(path.join("/maintenancetool")) { + // No regular logging now. + eprintln!("Failed to delete maintenancetool: {:?}", e); + }; + + if let Err(e) = remove_file(path.join("/installer.log")) { + // No regular logging now. + eprintln!("Failed to delete installer log: {:?}", e); + }; + } } pub use self::natives::*; diff --git a/src/rest.rs b/src/rest.rs index 917a066..4d8a1bb 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -19,6 +19,7 @@ use url::form_urlencoded; use std::collections::HashMap; use std::net::SocketAddr; use std::process::exit; +use std::process::Command; use std::sync::mpsc::channel; use std::sync::Arc; use std::sync::RwLock; @@ -30,12 +31,13 @@ use installer::InstallMessage; use installer::InstallerFramework; use logging::LoggingErrors; -use std::process::Command; use http; use config::Config; +use native; + #[derive(Serialize)] struct FileSelection { path: Option, @@ -204,6 +206,16 @@ impl Service for WebService { .log_expect("Unable to start child process"); } + if framework.burn_after_exit { + let path = framework + .install_path + .clone() + .log_expect("No install path when one should have existed?"); + + println!("Base path: {:?}", path); + native::burn_on_exit(path); + } + exit(0); } // Gets properties such as if the application is in maintenance mode