liftinstall/src/self_update.rs
James 68109894f1 Update config files for v7 (#12)
* platform: fix build on Linux and update web-view

* deps: replace xz-decom with xz2 and update deps

* platform: fix regression...

... that prevents the build on Windows

* linux: implement platform-dependent functions

* travis: add macos and windows CI

* travis: use official Rust Docker image

* Update Cargo.lock for new version

* Break apart REST into separate services

This cleans up locking, ensures consistent futures for all endpoints
and enhances code re-use.

* Clean up codebase, fixing minor errors

* Update packages, use async client for downloading config

While this has a hell of a lot more boilerplate, this is quite
a bit cleaner.

* Add explicit 'dyn's as per Rust nightly requirements

* Migrate self updating functions to own module

* Migrate assets to server module

* Use patched web-view to fix dialogs, remove nfd

* Implement basic dark mode

* Revert window.close usage

* ui: split files and use Webpack

* frontend: ui: include prebuilt assets...

... and update rust side stuff

* build: integrate webpack building into build.rs

* Polish Vue UI split

* Add instructions for node + yarn

* native: fix uninstall self-destruction behavior...... by not showing the command prompt window and fork-spawning the cmd

* native: deal with Unicode issues in native APIs

* native: further improve Unicode support on Windows

* travis: add cache and fix issues

* ui: use Buefy components to...

... beautify the UI

* ui: makes error message selectable

* Make launcher mode behaviour more robust

* Fix error display on launcher pages

* Correctly handle exit on error

* Bump installer version
2019-07-04 21:23:16 -04:00

112 lines
3.6 KiB
Rust

//! self_update.rs
//!
//! Handles different components of self-updating.
use std::fs::{remove_file, File};
use std::path::{Path, PathBuf};
use std::process::{exit, Command};
use std::{thread, time};
use clap::{App, ArgMatches};
use logging::LoggingErrors;
/// Swaps around the main executable if needed.
pub fn perform_swap(current_exe: &PathBuf, to_path: Option<&str>) {
// Check to see if we are currently in a self-update
if let Some(to_path) = to_path {
let to_path = PathBuf::from(to_path);
// Sleep a little bit to allow Windows to close the previous file handle
thread::sleep(time::Duration::from_millis(3000));
info!(
"Swapping installer from {} to {}",
current_exe.display(),
to_path.display()
);
// Attempt it a few times because Windows can hold a lock
for i in 1..=5 {
let swap_result = if cfg!(windows) {
use std::fs::copy;
copy(&current_exe, &to_path).map(|_x| ())
} else {
use std::fs::rename;
rename(&current_exe, &to_path)
};
match swap_result {
Ok(_) => break,
Err(e) => {
if i < 5 {
info!("Copy attempt failed: {:?}, retrying in 3 seconds.", e);
thread::sleep(time::Duration::from_millis(3000));
} else {
Err::<(), _>(e).log_expect("Copying new binary failed");
}
}
}
}
Command::new(to_path)
.spawn()
.log_expect("Unable to start child process");
exit(0);
}
}
pub fn check_args<'a>(app: App<'a, '_>, current_path: &Path) -> Option<ArgMatches<'a>> {
// If we just finished a update, we need to inject our previous command line arguments
let args_file = current_path.join("args.json");
if args_file.exists() {
let database: Vec<String> = {
let metadata_file =
File::open(&args_file).log_expect("Unable to open args file handle");
serde_json::from_reader(metadata_file).log_expect("Unable to read metadata file")
};
let matches = app.get_matches_from(database);
info!("Parsed command line arguments from original instance");
remove_file(args_file).log_expect("Unable to clean up args file");
Some(matches)
} else {
None
}
}
pub fn cleanup(current_path: &Path) {
// Cleanup any remaining new maintenance tool instances if they exist
if cfg!(windows) {
let updater_executable = current_path.join("maintenancetool_new.exe");
if updater_executable.exists() {
// Sleep a little bit to allow Windows to close the previous file handle
thread::sleep(time::Duration::from_millis(3000));
// Attempt it a few times because Windows can hold a lock
for i in 1..=5 {
let swap_result = remove_file(&updater_executable);
match swap_result {
Ok(_) => break,
Err(e) => {
if i < 5 {
info!("Cleanup attempt failed: {:?}, retrying in 3 seconds.", e);
thread::sleep(time::Duration::from_millis(3000));
} else {
warn!("Deleting temp binary failed after 5 attempts: {:?}", e);
}
}
}
}
}
}
}