liftinstall/src/installer.rs

463 lines
14 KiB
Rust
Raw Normal View History

2018-08-03 11:52:31 +00:00
//! installer.rs
//!
//! Contains the main installer structure, as well as high-level means of controlling it.
2018-01-31 03:36:29 +00:00
use serde_json;
2018-01-30 07:29:34 +00:00
use std::fs::File;
2018-08-09 05:21:50 +00:00
use std::fs::OpenOptions;
2018-08-09 05:21:50 +00:00
use std::env;
2018-01-27 04:14:56 +00:00
use std::env::var;
2018-05-03 03:30:58 +00:00
use std::path::Path;
2018-01-27 04:14:56 +00:00
use std::path::PathBuf;
use std::sync::mpsc::Sender;
2018-08-09 05:21:50 +00:00
use std::io::copy;
use std::io::Cursor;
use std::process::Command;
use std::process::{exit, Stdio};
2018-08-09 05:21:50 +00:00
2020-03-09 06:39:24 +00:00
use crate::config::BaseAttributes;
use crate::config::Config;
2018-01-27 03:27:41 +00:00
2020-03-09 06:39:24 +00:00
use crate::sources::types::Version;
2020-03-09 06:39:24 +00:00
use crate::tasks::install::InstallTask;
use crate::tasks::uninstall::UninstallTask;
use crate::tasks::uninstall_global_shortcut::UninstallGlobalShortcutsTask;
use crate::tasks::DependencyTree;
use crate::tasks::TaskMessage;
2018-01-31 03:36:29 +00:00
2020-03-09 06:39:24 +00:00
use crate::logging::LoggingErrors;
2018-08-04 08:35:00 +00:00
use dirs::home_dir;
2018-08-06 10:51:59 +00:00
use std::fs::remove_file;
2018-08-09 05:21:50 +00:00
2020-03-09 06:39:24 +00:00
use crate::http;
2018-08-09 05:21:50 +00:00
2020-05-27 22:14:09 +00:00
use number_prefix::NumberPrefix::{self, Prefixed, Standalone};
2018-08-06 10:51:59 +00:00
2020-03-09 06:39:24 +00:00
use crate::native;
/// A message thrown during the installation of packages.
#[derive(Serialize)]
pub enum InstallMessage {
Status(String, f64),
PackageInstalled,
Error(String),
EOF,
}
/// Metadata about the current installation itself.
#[derive(Serialize, Deserialize, Clone)]
pub struct InstallationDatabase {
pub packages: Vec<LocalInstallation>,
pub shortcuts: Vec<String>,
}
impl InstallationDatabase {
/// Creates a new, empty installation database.
pub fn new() -> InstallationDatabase {
InstallationDatabase {
packages: Vec::new(),
shortcuts: Vec::new(),
}
}
}
2018-01-27 03:27:41 +00:00
/// The installer framework contains metadata about packages, what is installable, what isn't,
/// etc.
pub struct InstallerFramework {
2018-08-07 05:34:57 +00:00
pub base_attributes: BaseAttributes,
pub config: Option<Config>,
pub database: InstallationDatabase,
pub install_path: Option<PathBuf>,
pub preexisting_install: bool,
2018-08-04 13:35:56 +00:00
pub is_launcher: bool,
// If we just completed an uninstall, and we should clean up after ourselves.
pub burn_after_exit: bool,
2018-08-04 13:35:56 +00:00
pub launcher_path: Option<String>,
2018-01-27 03:27:41 +00:00
}
/// Contains basic properties on the status of the session. Subset of InstallationFramework.
#[derive(Serialize)]
pub struct InstallationStatus {
pub database: InstallationDatabase,
pub install_path: Option<String>,
pub preexisting_install: bool,
2018-08-04 13:35:56 +00:00
pub is_launcher: bool,
pub launcher_path: Option<String>,
2018-01-30 04:53:28 +00:00
}
2018-01-31 03:36:29 +00:00
/// Tracks the state of a local installation
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LocalInstallation {
pub name: String,
pub version: Version,
2018-08-06 10:51:59 +00:00
/// Relative paths to generated files
pub files: Vec<String>,
2018-08-06 10:51:59 +00:00
/// Absolute paths to generated shortcut files
pub shortcuts: Vec<String>,
2018-01-31 03:36:29 +00:00
}
macro_rules! declare_messenger_callback {
($target:expr) => {
2019-06-23 10:27:35 +00:00
&|msg: &TaskMessage| match *msg {
TaskMessage::DisplayMessage(msg, progress) => {
if let Err(v) = $target.send(InstallMessage::Status(msg.to_string(), progress as _))
{
error!("Failed to submit queue message: {:?}", v);
}
}
2019-06-23 10:27:35 +00:00
TaskMessage::PackageInstalled => {
if let Err(v) = $target.send(InstallMessage::PackageInstalled) {
error!("Failed to submit queue message: {:?}", v);
}
}
}
};
}
2018-01-27 03:27:41 +00:00
impl InstallerFramework {
/// Returns a copy of the configuration.
2018-08-07 05:34:57 +00:00
pub fn get_config(&self) -> Option<Config> {
2018-01-27 03:27:41 +00:00
self.config.clone()
}
2018-01-27 04:14:56 +00:00
/// Returns the default install path.
pub fn get_default_path(&self) -> Option<String> {
2018-08-07 05:34:57 +00:00
let app_name = &self.base_attributes.name;
2018-01-27 04:14:56 +00:00
let base_dir = match var("LOCALAPPDATA") {
Ok(path) => PathBuf::from(path),
2018-01-27 11:58:56 +00:00
Err(_) => home_dir()?,
2018-01-27 04:14:56 +00:00
};
let file_name = if cfg!(unix) {
format!(".{}", app_name.to_ascii_lowercase())
} else {
app_name.to_string()
};
let file = base_dir.join(file_name);
2018-01-27 04:14:56 +00:00
Some(file.to_str()?.to_owned())
}
/// Sends a request for something to be installed.
2018-08-03 13:44:35 +00:00
/// items: Array of named packages to be installed/kept
2018-05-03 03:30:58 +00:00
/// messages: Channel used to send progress messages
/// fresh_install: If the install directory must be empty
pub fn install(
2018-05-03 03:30:58 +00:00
&mut self,
items: Vec<String>,
messages: &Sender<InstallMessage>,
fresh_install: bool,
) -> Result<(), String> {
2018-08-04 06:28:13 +00:00
info!(
"Framework: Installing {:?} to {:?}",
items,
self.install_path
.clone()
.log_expect("Install directory not initialised")
);
2018-01-30 04:53:28 +00:00
2018-08-03 14:38:34 +00:00
// Calculate packages to *uninstall*
let mut uninstall_items = Vec::new();
if !fresh_install {
for package in &self.database.packages {
2018-08-03 14:38:34 +00:00
if !items.contains(&package.name) {
uninstall_items.push(package.name.clone());
}
}
2018-08-04 06:28:13 +00:00
info!(
2018-08-03 14:38:34 +00:00
"Framework: Uninstalling {:?} additionally.",
uninstall_items
);
}
let task = Box::new(InstallTask {
items,
2018-08-03 14:38:34 +00:00
uninstall_items,
fresh_install,
});
2018-01-30 04:53:28 +00:00
let mut tree = DependencyTree::build(task);
2018-01-30 04:53:28 +00:00
2018-08-04 06:28:13 +00:00
info!("Dependency tree:\n{}", tree);
2018-01-30 04:53:28 +00:00
tree.execute(self, declare_messenger_callback!(messages))
.map(|_x| ())
}
2018-08-03 13:44:35 +00:00
/// Sends a request for everything to be uninstalled.
pub fn uninstall(&mut self, messages: &Sender<InstallMessage>) -> Result<(), String> {
let items: Vec<String> = self
.database
.packages
.iter()
.map(|x| x.name.clone())
.collect();
2018-08-03 13:44:35 +00:00
let task = Box::new(UninstallTask { items });
let mut tree = DependencyTree::build(task);
2018-08-04 06:28:13 +00:00
info!("Dependency tree:\n{}", tree);
2018-08-03 13:44:35 +00:00
tree.execute(self, declare_messenger_callback!(messages))
.map(|_x| ())?;
2018-08-06 10:51:59 +00:00
// Uninstall shortcuts
let task = Box::new(UninstallGlobalShortcutsTask {});
let mut tree = DependencyTree::build(task);
tree.execute(self, declare_messenger_callback!(messages))
.map(|_x| ())?;
2018-08-06 10:51:59 +00:00
// Delete the metadata file
let path = self
.install_path
.as_ref()
.log_expect("No install path specified");
remove_file(path.join("metadata.json"))
.map_err(|x| format!("Failed to delete metadata: {:?}", x))?;
// Logging will have to be done later
self.burn_after_exit = true;
2018-08-06 10:51:59 +00:00
Ok(())
2018-08-03 13:44:35 +00:00
}
2018-08-09 05:21:50 +00:00
/// Verifies that the config has all requirements met (no need to update the
/// updater, for example). This will terminate if this is the case after applying
/// the correct actions.
pub fn update_updater(&mut self, messages: &Sender<InstallMessage>) -> Result<(), String> {
let tool = self
.config
.as_ref()
.log_expect("Config should exist by now")
.new_tool
.as_ref()
.log_expect("Frontend asked for updater update when one doesn't exist");
let mut downloaded = 0;
let mut data_storage: Vec<u8> = Vec::new();
http::stream_file(tool, |data, size| {
{
data_storage.extend_from_slice(&data);
}
downloaded += data.len();
let percentage = if size == 0 {
0.0
} else {
(downloaded as f64) / (size as f64)
};
// Pretty print data volumes
let pretty_current = match NumberPrefix::decimal(downloaded as f64) {
2018-08-09 05:21:50 +00:00
Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};
let pretty_total = match NumberPrefix::decimal(size as f64) {
2018-08-09 05:21:50 +00:00
Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
};
if let Err(v) = messages.send(InstallMessage::Status(
format!(
"Downloading self-update ({} of {})...",
pretty_current, pretty_total
),
percentage as _,
)) {
error!("Failed to submit queue message: {:?}", v);
}
})?;
info!("Launching new updater...");
// Save to file in current dir
let current_exe = env::current_exe().log_expect("Current executable could not be found");
let path = current_exe
.parent()
.log_expect("Parent directory of executable could not be found");
let platform_extension = if cfg!(windows) {
"maintenancetool_new.exe"
} else {
"maintenancetool_new"
};
let new_app = path.join(platform_extension);
let mut file_metadata = OpenOptions::new();
file_metadata.write(true).create(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
file_metadata.mode(0o770);
}
{
let mut new_app_file = match file_metadata.open(&new_app) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open installer binary: {:?}", v)),
};
if let Err(v) = copy(&mut Cursor::new(data_storage), &mut new_app_file) {
return Err(format!("Unable to copy installer binary: {:?}", v));
}
}
// Save current command line arguments
let args_file = path.join("args.json");
let args: Vec<String> = env::args_os()
.map(|x| {
x.to_str()
.log_expect("Unable to convert argument to String")
.to_string()
})
.collect();
2018-08-09 05:21:50 +00:00
{
let new_app_file = match File::create(&args_file) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open args file: {:?}", v)),
};
serde_json::to_writer(new_app_file, &args).log_expect("Unable to write args");
}
let current_exe = env::current_exe().log_expect("Current executable could not be found");
// Launch this new process
Command::new(new_app)
.arg("--swap")
.arg(current_exe)
.spawn()
.log_expect("Unable to start child process");
exit(0);
}
2018-05-03 03:30:58 +00:00
/// Saves the applications database.
pub fn save_database(&self) -> Result<(), String> {
// We have to have a install path for us to be able to do anything
let path = match self.install_path.clone() {
Some(v) => v,
2018-08-04 08:35:00 +00:00
None => return Err("No install directory for installer".to_string()),
2018-05-03 03:30:58 +00:00
};
let metadata_path = path.join("metadata.json");
2018-05-03 03:30:58 +00:00
let metadata_file = match File::create(metadata_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
};
match serde_json::to_writer(metadata_file, &self.database) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to write to file: {:?}", v)),
};
Ok(())
}
/// Configures this installer to install to the specified location.
/// If there was a currently configured install path, this will be left as-is.
pub fn set_install_dir(&mut self, dir: &str) {
self.install_path = Some(Path::new(dir).to_owned());
2018-05-03 03:30:58 +00:00
}
/// Returns metadata on the current status of the installation.
pub fn get_installation_status(&self) -> InstallationStatus {
InstallationStatus {
database: self.database.clone(),
install_path: match self.install_path.clone() {
Some(v) => Some(v.display().to_string()),
None => None,
},
preexisting_install: self.preexisting_install,
2018-08-04 13:35:56 +00:00
is_launcher: self.is_launcher,
launcher_path: self.launcher_path.clone(),
}
}
/// Shuts down the installer instance.
pub fn shutdown(&mut self) -> Result<(), String> {
info!("Shutting down installer framework...");
if let Some(ref v) = self.launcher_path.take() {
info!("Launching {:?}", v);
Command::new(v)
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(|x| format!("Unable to start application: {:?}", x))?;
}
if self.burn_after_exit {
info!("Requesting that self be deleted after exit.");
native::burn_on_exit(&self.base_attributes.name);
self.burn_after_exit = false;
}
Ok(())
}
2018-01-27 03:27:41 +00:00
/// Creates a new instance of the Installer Framework with a specified Config.
2018-08-07 05:34:57 +00:00
pub fn new(attrs: BaseAttributes) -> Self {
2018-05-03 03:30:58 +00:00
InstallerFramework {
2018-08-07 05:34:57 +00:00
base_attributes: attrs,
config: None,
database: InstallationDatabase::new(),
install_path: None,
preexisting_install: false,
2018-08-04 13:35:56 +00:00
is_launcher: false,
burn_after_exit: false,
2018-08-04 13:35:56 +00:00
launcher_path: None,
2018-05-03 03:30:58 +00:00
}
}
/// Creates a new instance of the Installer Framework with a specified Config, managing
/// a pre-existing installation.
2018-08-07 05:34:57 +00:00
pub fn new_with_db(attrs: BaseAttributes, install_path: &Path) -> Result<Self, String> {
let path = install_path.to_owned();
let metadata_path = path.join("metadata.json");
2018-05-03 03:30:58 +00:00
let metadata_file = match File::open(metadata_path) {
Ok(v) => v,
Err(v) => return Err(format!("Unable to open file handle: {:?}", v)),
};
let database: InstallationDatabase = match serde_json::from_reader(metadata_file) {
2018-05-03 03:30:58 +00:00
Ok(v) => v,
Err(v) => return Err(format!("Unable to read metadata file: {:?}", v)),
};
Ok(InstallerFramework {
2018-08-07 05:34:57 +00:00
base_attributes: attrs,
config: None,
2018-05-03 03:30:58 +00:00
database,
install_path: Some(path),
preexisting_install: true,
2018-08-04 13:35:56 +00:00
is_launcher: false,
burn_after_exit: false,
2018-08-04 13:35:56 +00:00
launcher_path: None,
2018-05-03 03:30:58 +00:00
})
2018-01-27 03:27:41 +00:00
}
}