Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Fix auto-updater. (#4869)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork authored and arkpar committed Mar 11, 2017
1 parent f14e425 commit fcdc396
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
17 changes: 14 additions & 3 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod stratum;
use std::{process, env};
use std::collections::HashMap;
use std::io::{self as stdio, BufReader, Read, Write};
use std::fs::File;
use std::fs::{metadata, File};
use std::path::PathBuf;
use util::sha3::sha3;
use cli::Args;
Expand Down Expand Up @@ -290,8 +290,19 @@ fn main() {
let latest_exe = latest_exe_path();
let have_update = latest_exe.as_ref().map_or(false, |p| p.exists());
let is_non_updated_current = exe.as_ref().map_or(false, |exe| latest_exe.as_ref().map_or(false, |lexe| exe.canonicalize().ok() != lexe.canonicalize().ok()));
trace_main!("Starting... (have-update: {}, non-updated-current: {})", have_update, is_non_updated_current);
let exit_code = if have_update && is_non_updated_current {
let update_is_newer = match (
latest_exe.as_ref()
.and_then(|p| metadata(p.as_path()).ok())
.and_then(|m| m.modified().ok()),
exe.as_ref()
.and_then(|p| metadata(p.as_path()).ok())
.and_then(|m| m.modified().ok())
) {
(Some(latest_exe_time), Some(this_exe_time)) if latest_exe_time > this_exe_time => true,
_ => false,
};
trace_main!("Starting... (have-update: {}, non-updated-current: {})", have_update, is_non_updated_current); trace_main!("Starting... (have-update: {}, non-updated-current: {})", have_update, is_non_updated_current);
let exit_code = if have_update && is_non_updated_current && update_is_newer {
trace_main!("Attempting to run latest update ({})...", latest_exe.as_ref().expect("guarded by have_update; latest_exe must exist for have_update; qed").display());
run_parity().unwrap_or_else(|| { trace_main!("Falling back to local..."); main_direct(true) })
} else {
Expand Down
36 changes: 24 additions & 12 deletions updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ impl Updater {
let auto = {
let mut s = self.state.lock();
let fetched = s.fetching.take().unwrap();
let b = result.map_err(|e| (format!("Unable to fetch update ({}): {:?}", fetched.version, e), false))?;
info!(target: "updater", "Fetched latest version ({}) OK to {}", fetched.version, b.display());
let dest = self.updates_path(&Self::update_file_name(&fetched.version));
fs::create_dir_all(dest.parent().expect("at least one thing pushed; qed")).map_err(|e| (format!("Unable to create updates path: {:?}", e), true))?;
fs::copy(&b, &dest).map_err(|e| (format!("Unable to copy update: {:?}", e), true))?;
restrict_permissions_owner(&dest, false, true).map_err(|e| (format!("Unable to update permissions: {}", e), true))?;
info!(target: "updater", "Installed updated binary to {}", dest.display());
if !dest.exists() {
let b = result.map_err(|e| (format!("Unable to fetch update ({}): {:?}", fetched.version, e), false))?;
info!(target: "updater", "Fetched latest version ({}) OK to {}", fetched.version, b.display());
fs::create_dir_all(dest.parent().expect("at least one thing pushed; qed")).map_err(|e| (format!("Unable to create updates path: {:?}", e), true))?;
fs::copy(&b, &dest).map_err(|e| (format!("Unable to copy update: {:?}", e), true))?;
restrict_permissions_owner(&dest, false, true).map_err(|e| (format!("Unable to update permissions: {}", e), true))?;
info!(target: "updater", "Installed updated binary to {}", dest.display());
}
let auto = match self.update_policy.filter {
UpdateFilter::All => true,
UpdateFilter::Critical if fetched.is_critical /* TODO: or is on a bad fork */ => true,
Expand Down Expand Up @@ -271,15 +273,23 @@ impl Updater {
let running_later = latest.track.version.version < self.version_info().version;
let running_latest = latest.track.version.hash == self.version_info().hash;
let already_have_latest = s.installed.as_ref().or(s.ready.as_ref()).map_or(false, |t| *t == latest.track);

if self.update_policy.enable_downloading && !running_later && !running_latest && !already_have_latest {
if let Some(b) = latest.track.binary {
if s.fetching.is_none() {
info!(target: "updater", "Attempting to get parity binary {}", b);
s.fetching = Some(latest.track.clone());
drop(s);
let weak_self = self.weak_self.lock().clone();
let f = move |r: Result<PathBuf, fetch::Error>| if let Some(this) = weak_self.upgrade() { this.fetch_done(r) };
self.fetcher.lock().as_ref().expect("Created on `new`; qed").fetch(b, Box::new(f));
if self.updates_path(&Self::update_file_name(&latest.track.version)).exists() {
info!(target: "updater", "Already fetched binary.");
s.fetching = Some(latest.track.clone());
drop(s);
self.fetch_done(Ok(PathBuf::new()));
} else {
info!(target: "updater", "Attempting to get parity binary {}", b);
s.fetching = Some(latest.track.clone());
drop(s);
let weak_self = self.weak_self.lock().clone();
let f = move |r: Result<PathBuf, fetch::Error>| if let Some(this) = weak_self.upgrade() { this.fetch_done(r) };
self.fetcher.lock().as_ref().expect("Created on `new`; qed").fetch(b, Box::new(f));
}
}
}
}
Expand Down Expand Up @@ -356,6 +366,8 @@ impl Service for Updater {
s.installed = Some(r);
if let Some(ref h) = *self.exit_handler.lock() {
(*h)();
} else {
info!("Update installed; ready for restart.");
}
Ok(true)
}
Expand Down

0 comments on commit fcdc396

Please sign in to comment.