update: consolidate upgrade/rename loop

Since p2pool/xmrig could be running as well, this also applies the
windows old binary rename code to p2pool/xmrig.
This commit is contained in:
hinto-janaiyo 2022-11-21 20:54:19 -05:00
parent 88e172a2e3
commit e8a01e71b9
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45

View file

@ -262,20 +262,23 @@ impl Update {
std::thread::spawn(move|| { std::thread::spawn(move|| {
info!("Spawning update thread..."); info!("Spawning update thread...");
match Update::start(update.clone(), og.clone(), state_ver.clone()) { match Update::start(update.clone(), og.clone(), state_ver.clone()) {
Err(e) => { Ok(_) => {
info!("Update ... FAIL ... {}", e);
*update.lock().unwrap().msg.lock().unwrap() = format!("{} | {}", MSG_FAILED, e);
},
_ => {
info!("Update | Saving state..."); info!("Update | Saving state...");
let original_version = og.lock().unwrap().version.clone();
og.lock().unwrap().version = state_ver;
match State::save(&mut og.lock().unwrap(), &state_path) { match State::save(&mut og.lock().unwrap(), &state_path) {
Ok(_) => info!("Update ... OK"), Ok(_) => info!("Update ... OK"),
Err(e) => { Err(e) => {
warn!("Update | Saving state ... FAIL ... {}", e); warn!("Update | Saving state ... FAIL ... {}", e);
og.lock().unwrap().version = original_version;
*update.lock().unwrap().msg.lock().unwrap() = format!("Saving new versions into state failed"); *update.lock().unwrap().msg.lock().unwrap() = format!("Saving new versions into state failed");
}, },
}; };
} }
Err(e) => {
info!("Update ... FAIL ... {}", e);
*update.lock().unwrap().msg.lock().unwrap() = format!("{} | {}", MSG_FAILED, e);
},
}; };
*update.lock().unwrap().updating.lock().unwrap() = false; *update.lock().unwrap().updating.lock().unwrap() = false;
}); });
@ -423,17 +426,17 @@ impl Update {
// that gets updated during an update. This prevents the updater always thinking // that gets updated during an update. This prevents the updater always thinking
// there's a new Gupax update since the user didnt restart and is still technically // there's a new Gupax update since the user didnt restart and is still technically
// using the old version (even though the underlying binary was updated). // using the old version (even though the underlying binary was updated).
old_ver = og.lock().unwrap().version.lock().unwrap().gupax.clone(); old_ver = state_ver.lock().unwrap().gupax.clone();
diff = old_ver != new_ver && GUPAX_VERSION != new_ver; diff = old_ver != new_ver && GUPAX_VERSION != new_ver;
name = "Gupax"; name = "Gupax";
} }
P2pool => { P2pool => {
old_ver = og.lock().unwrap().version.lock().unwrap().p2pool.clone(); old_ver = state_ver.lock().unwrap().p2pool.clone();
diff = old_ver != new_ver; diff = old_ver != new_ver;
name = "P2Pool"; name = "P2Pool";
} }
Xmrig => { Xmrig => {
old_ver = og.lock().unwrap().version.lock().unwrap().xmrig.clone(); old_ver = state_ver.lock().unwrap().xmrig.clone();
diff = old_ver != new_ver; diff = old_ver != new_ver;
name = "XMRig"; name = "XMRig";
} }
@ -558,40 +561,42 @@ impl Update {
if ! entry.file_type().is_file() { continue } if ! entry.file_type().is_file() { continue }
let basename = entry.file_name().to_str().ok_or(anyhow::Error::msg("WalkDir basename failed"))?; let basename = entry.file_name().to_str().ok_or(anyhow::Error::msg("WalkDir basename failed"))?;
match basename { match basename {
GUPAX_BINARY => { GUPAX_BINARY|P2POOL_BINARY|XMRIG_BINARY => {
// Unix can replace running binaries no problem (they're loading into memory) let name = match basename {
GUPAX_BINARY => Gupax,
P2POOL_BINARY => P2pool,
_ => Xmrig,
};
let path = match name {
Gupax => update.lock().unwrap().path_gupax.clone(),
P2pool => update.lock().unwrap().path_p2pool.clone(),
Xmrig => update.lock().unwrap().path_xmrig.clone(),
};
// Unix can replace running binaries no problem (they're loaded into memory)
// Windows locks binaries in place, so we must move (rename) current binary // Windows locks binaries in place, so we must move (rename) current binary
// into the temp folder, then move the new binary into the old ones spot. // into the temp folder, then move the new binary into the old ones spot.
// Clearing the temp folder is now moved at startup instead at the end // Clearing the temp folder is now moved at startup instead at the end
// of this function due to this behavior, thanks Windows. // of this function due to this behavior, thanks Windows.
let path = update.lock().unwrap().path_gupax.clone();
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let tmp_windows = tmp_dir.clone() + "gupax_old.exe"; let tmp_windows = match name {
Gupax => tmp_dir.clone() + "gupax_old.exe",
P2pool => tmp_dir.clone() + "p2pool_old.exe",
Xmrig => tmp_dir.clone() + "xmrig_old.exe",
};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
info!("Update | WINDOWS ONLY ... Moving [{}] -> [{}]", &path, tmp_windows); info!("Update | WINDOWS ONLY ... Moving [{}] -> [{}]", &path, tmp_windows);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
std::fs::rename(&path, tmp_windows)?; std::fs::rename(&path, tmp_windows)?;
info!("Update | Moving [{}] -> [{}]", entry.path().display(), path); info!("Update | Moving [{}] -> [{}]", entry.path().display(), path);
if name == P2pool || name == Xmrig {
std::fs::create_dir_all(std::path::Path::new(&path).parent().ok_or(anyhow::Error::msg(format!("{} path failed", name)))?)?;
}
std::fs::rename(entry.path(), path)?; std::fs::rename(entry.path(), path)?;
og.lock().unwrap().version.lock().unwrap().gupax = Pkg::get_new_pkg_version(Gupax, &vec4)?; match name {
*update.lock().unwrap().prog.lock().unwrap() += (5.0 / pkg_amount).round(); Gupax => state_ver.lock().unwrap().gupax = Pkg::get_new_pkg_version(Gupax, &vec4)?,
}, P2pool => state_ver.lock().unwrap().p2pool = Pkg::get_new_pkg_version(P2pool, &vec4)?,
P2POOL_BINARY => { Xmrig => state_ver.lock().unwrap().xmrig = Pkg::get_new_pkg_version(Xmrig, &vec4)?,
let path = update.lock().unwrap().path_p2pool.clone(); };
let path = std::path::Path::new(&path);
info!("Update | Moving [{}] -> [{}]", entry.path().display(), path.display());
std::fs::create_dir_all(path.parent().ok_or(anyhow::Error::msg("P2Pool path failed"))?)?;
std::fs::rename(entry.path(), path)?;
og.lock().unwrap().version.lock().unwrap().p2pool = Pkg::get_new_pkg_version(P2pool, &vec4)?;
*update.lock().unwrap().prog.lock().unwrap() += (5.0 / pkg_amount).round();
},
XMRIG_BINARY => {
let path = update.lock().unwrap().path_xmrig.clone();
let path = std::path::Path::new(&path);
info!("Update | Moving [{}] -> [{}]", entry.path().display(), path.display());
std::fs::create_dir_all(path.parent().ok_or(anyhow::Error::msg("XMRig path failed"))?)?;
std::fs::rename(entry.path(), path)?;
og.lock().unwrap().version.lock().unwrap().xmrig = Pkg::get_new_pkg_version(Xmrig, &vec4)?;
*update.lock().unwrap().prog.lock().unwrap() += (5.0 / pkg_amount).round(); *update.lock().unwrap().prog.lock().unwrap() += (5.0 / pkg_amount).round();
}, },
_ => (), _ => (),