2018-08-03 11:52:31 +00:00
|
|
|
//! main.rs
|
|
|
|
//!
|
|
|
|
//! The main entrypoint for the application. Orchestrates the building of the installation
|
|
|
|
//! framework, and opens necessary HTTP servers/frontends.
|
|
|
|
|
2018-08-03 15:12:03 +00:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
2018-08-04 07:15:27 +00:00
|
|
|
#![deny(unsafe_code)]
|
2018-08-04 08:35:00 +00:00
|
|
|
#![deny(missing_docs)]
|
2018-01-26 12:29:28 +00:00
|
|
|
|
2021-10-15 10:35:47 +00:00
|
|
|
extern crate wry;
|
2018-01-27 11:56:36 +00:00
|
|
|
|
2018-07-28 05:33:06 +00:00
|
|
|
extern crate futures;
|
2018-08-03 07:50:17 +00:00
|
|
|
extern crate hyper;
|
2018-08-03 11:52:31 +00:00
|
|
|
extern crate url;
|
2018-01-29 10:27:54 +00:00
|
|
|
|
2018-01-30 04:53:28 +00:00
|
|
|
extern crate number_prefix;
|
2018-01-30 04:54:44 +00:00
|
|
|
extern crate reqwest;
|
2018-01-30 04:53:28 +00:00
|
|
|
|
2018-01-27 03:27:41 +00:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate toml;
|
|
|
|
|
2018-01-29 12:28:14 +00:00
|
|
|
extern crate regex;
|
2018-01-29 12:37:17 +00:00
|
|
|
extern crate semver;
|
2018-01-29 07:21:37 +00:00
|
|
|
|
2018-08-04 08:35:00 +00:00
|
|
|
extern crate dirs;
|
2018-08-08 02:47:32 +00:00
|
|
|
extern crate tar;
|
2019-07-05 01:23:16 +00:00
|
|
|
extern crate xz2;
|
2018-01-30 07:29:34 +00:00
|
|
|
extern crate zip;
|
|
|
|
|
2018-08-04 06:28:13 +00:00
|
|
|
extern crate fern;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
extern crate chrono;
|
|
|
|
|
2018-08-04 13:35:56 +00:00
|
|
|
extern crate clap;
|
2019-07-05 01:23:16 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate widestring;
|
2019-11-16 05:43:11 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate winapi;
|
2019-07-05 01:23:16 +00:00
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
extern crate slug;
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
extern crate sysinfo;
|
2018-12-15 08:14:25 +00:00
|
|
|
|
2019-10-02 07:13:53 +00:00
|
|
|
extern crate jsonwebtoken as jwt;
|
|
|
|
|
2019-10-21 07:09:16 +00:00
|
|
|
extern crate base64;
|
|
|
|
|
2018-08-08 02:47:32 +00:00
|
|
|
mod archives;
|
2018-01-27 03:27:41 +00:00
|
|
|
mod config;
|
2019-07-05 01:23:16 +00:00
|
|
|
mod frontend;
|
2018-05-03 11:50:44 +00:00
|
|
|
mod http;
|
2018-01-27 03:27:41 +00:00
|
|
|
mod installer;
|
2018-08-04 07:03:32 +00:00
|
|
|
mod logging;
|
2018-08-06 10:51:59 +00:00
|
|
|
mod native;
|
2019-07-05 01:23:16 +00:00
|
|
|
mod self_update;
|
2018-01-29 07:21:37 +00:00
|
|
|
mod sources;
|
2018-08-03 07:50:17 +00:00
|
|
|
mod tasks;
|
2018-01-26 12:29:28 +00:00
|
|
|
|
2018-01-27 03:27:41 +00:00
|
|
|
use installer::InstallerFramework;
|
2018-01-26 12:29:28 +00:00
|
|
|
|
2018-08-04 07:03:32 +00:00
|
|
|
use logging::LoggingErrors;
|
2019-12-07 05:31:37 +00:00
|
|
|
use std::path::PathBuf;
|
2018-08-04 07:03:32 +00:00
|
|
|
|
2018-08-04 13:35:56 +00:00
|
|
|
use clap::App;
|
|
|
|
use clap::Arg;
|
|
|
|
|
2018-08-07 05:34:57 +00:00
|
|
|
use config::BaseAttributes;
|
2019-12-20 06:05:33 +00:00
|
|
|
use std::fs;
|
2021-10-16 00:47:45 +00:00
|
|
|
use std::process::{exit, Command, Stdio};
|
2018-08-07 05:34:57 +00:00
|
|
|
|
2020-03-09 06:24:29 +00:00
|
|
|
const RAW_CONFIG: &str = include_str!(concat!(env!("OUT_DIR"), "/bootstrap.toml"));
|
2018-01-26 12:29:28 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-10-01 03:17:59 +00:00
|
|
|
let config = BaseAttributes::from_toml_str(RAW_CONFIG).expect("Config file could not be read");
|
2018-10-01 01:27:31 +00:00
|
|
|
|
2018-10-01 03:17:59 +00:00
|
|
|
logging::setup_logger(format!("{}_installer.log", config.name))
|
|
|
|
.expect("Unable to setup logging!");
|
2018-01-27 03:27:41 +00:00
|
|
|
|
2019-07-05 01:23:16 +00:00
|
|
|
// Parse CLI arguments
|
2018-08-07 05:34:57 +00:00
|
|
|
let app_name = config.name.clone();
|
2018-01-27 04:33:15 +00:00
|
|
|
|
2018-08-09 05:21:50 +00:00
|
|
|
let app_about = format!("An interactive installer for {}", app_name);
|
|
|
|
let app = App::new(format!("{} installer", app_name))
|
2018-08-04 13:35:56 +00:00
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
2018-08-09 05:21:50 +00:00
|
|
|
.about(app_about.as_ref())
|
2018-08-04 13:35:56 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("launcher")
|
|
|
|
.long("launcher")
|
|
|
|
.value_name("TARGET")
|
|
|
|
.help("Launches the specified executable after checking for updates")
|
|
|
|
.takes_value(true),
|
2019-07-05 01:23:16 +00:00
|
|
|
)
|
|
|
|
.arg(
|
2018-08-09 05:21:50 +00:00
|
|
|
Arg::with_name("swap")
|
|
|
|
.long("swap")
|
|
|
|
.value_name("TARGET")
|
|
|
|
.help("Internal usage - swaps around a new installer executable")
|
|
|
|
.takes_value(true),
|
|
|
|
);
|
|
|
|
|
|
|
|
let reinterpret_app = app.clone(); // In case a reparse is needed
|
|
|
|
let mut matches = app.get_matches();
|
2018-08-04 13:35:56 +00:00
|
|
|
|
2018-08-04 06:28:13 +00:00
|
|
|
info!("{} installer", app_name);
|
2018-08-03 10:59:43 +00:00
|
|
|
|
2019-06-23 11:46:04 +00:00
|
|
|
// Handle self-updating if needed
|
2018-08-04 07:03:32 +00:00
|
|
|
let current_exe = std::env::current_exe().log_expect("Current executable could not be found");
|
|
|
|
let current_path = current_exe
|
|
|
|
.parent()
|
|
|
|
.log_expect("Parent directory of executable could not be found");
|
2018-08-09 05:21:50 +00:00
|
|
|
|
2019-12-07 05:31:37 +00:00
|
|
|
// Handle self-updating if needed
|
2019-07-05 01:23:16 +00:00
|
|
|
self_update::perform_swap(¤t_exe, matches.value_of("swap"));
|
|
|
|
if let Some(new_matches) = self_update::check_args(reinterpret_app, current_path) {
|
|
|
|
matches = new_matches;
|
2018-08-09 05:21:50 +00:00
|
|
|
}
|
2019-07-05 01:23:16 +00:00
|
|
|
self_update::cleanup(current_path);
|
2018-08-09 05:21:50 +00:00
|
|
|
|
2019-07-05 01:23:16 +00:00
|
|
|
// Load in metadata + setup the installer framework
|
2019-12-07 05:31:37 +00:00
|
|
|
let mut fresh_install = false;
|
2018-05-03 04:14:44 +00:00
|
|
|
let metadata_file = current_path.join("metadata.json");
|
2018-08-04 13:35:56 +00:00
|
|
|
let mut framework = if metadata_file.exists() {
|
2018-08-04 06:28:13 +00:00
|
|
|
info!("Using pre-existing metadata file: {:?}", metadata_file);
|
2021-10-15 07:29:20 +00:00
|
|
|
InstallerFramework::new_with_db(config.clone(), current_path).unwrap_or_else(|e| {
|
|
|
|
error!("Failed to load metadata: {:?}", e);
|
|
|
|
warn!("Entering recovery mode");
|
|
|
|
InstallerFramework::new_recovery_mode(config, current_path)
|
|
|
|
})
|
2018-05-03 03:30:58 +00:00
|
|
|
} else {
|
2018-08-04 06:28:13 +00:00
|
|
|
info!("Starting fresh install");
|
2019-12-07 05:31:37 +00:00
|
|
|
fresh_install = true;
|
2018-05-03 03:30:58 +00:00
|
|
|
InstallerFramework::new(config)
|
|
|
|
};
|
2018-01-27 03:27:41 +00:00
|
|
|
|
2019-12-07 05:31:37 +00:00
|
|
|
// check for existing installs if we are running as a fresh install
|
|
|
|
let installed_path = PathBuf::from(framework.get_default_path().unwrap());
|
|
|
|
if fresh_install && installed_path.join("metadata.json").exists() {
|
2019-12-20 06:05:33 +00:00
|
|
|
info!("Existing install detected! Copying Trying to launch this install instead");
|
|
|
|
// Ignore the return value from this since it should exit the application if its successful
|
|
|
|
let _ = replace_existing_install(¤t_exe, &installed_path);
|
2019-12-07 05:31:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 13:35:56 +00:00
|
|
|
let is_launcher = if let Some(string) = matches.value_of("launcher") {
|
|
|
|
framework.is_launcher = true;
|
|
|
|
framework.launcher_path = Some(string.to_string());
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2019-07-05 01:23:16 +00:00
|
|
|
// Start up the UI
|
|
|
|
frontend::launch(&app_name, is_launcher, framework);
|
2018-01-26 12:29:28 +00:00
|
|
|
}
|
2019-12-20 06:05:33 +00:00
|
|
|
|
|
|
|
fn replace_existing_install(current_exe: &PathBuf, installed_path: &PathBuf) -> Result<(), String> {
|
|
|
|
// Generate installer path
|
|
|
|
let platform_extension = if cfg!(windows) {
|
|
|
|
"maintenancetool.exe"
|
|
|
|
} else {
|
|
|
|
"maintenancetool"
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_tool = if cfg!(windows) {
|
|
|
|
"maintenancetool_new.exe"
|
|
|
|
} else {
|
|
|
|
"maintenancetool_new"
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(v) = fs::copy(current_exe, installed_path.join(new_tool)) {
|
|
|
|
return Err(format!("Unable to copy installer binary: {:?}", v));
|
|
|
|
}
|
|
|
|
|
2021-10-16 00:47:45 +00:00
|
|
|
let existing = installed_path
|
|
|
|
.join(platform_extension)
|
|
|
|
.into_os_string()
|
|
|
|
.into_string();
|
2019-12-20 06:05:33 +00:00
|
|
|
let new = installed_path.join(new_tool).into_os_string().into_string();
|
|
|
|
if existing.is_ok() && new.is_ok() {
|
|
|
|
// Remove NTFS alternate stream which tells the operating system that the updater was downloaded from the internet
|
|
|
|
if cfg!(windows) {
|
2021-10-16 00:47:45 +00:00
|
|
|
let _ = fs::remove_file(
|
|
|
|
installed_path.join("maintenancetool_new.exe:Zone.Identifier:$DATA"),
|
|
|
|
);
|
2019-12-20 06:05:33 +00:00
|
|
|
}
|
|
|
|
info!("Launching {:?}", existing);
|
|
|
|
let success = Command::new(new.unwrap())
|
|
|
|
.arg("--swap")
|
|
|
|
.arg(existing.unwrap())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.spawn();
|
|
|
|
if success.is_ok() {
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
error!("Unable to start existing yuzu maintenance tool. Launching old one instead");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|