[2.1.1] Now with Updates!
Added: - A working updater
This commit is contained in:
15
xmpd-update/Cargo.toml
Normal file
15
xmpd-update/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "xmpd-update"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
reqwest.workspace = true
|
||||
anyhow.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
semver.workspace = true
|
||||
log.workspace = true
|
||||
123
xmpd-update/src/lib.rs
Normal file
123
xmpd-update/src/lib.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
#[cfg(target_family = "unix")]
|
||||
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
|
||||
use std::{env::args, path::PathBuf, process::{Stdio, exit}, str::FromStr};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use reqwest::blocking::Client;
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
const CURRENT_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||
const GIT_HOST: &'static str = "git.mcorangehq.xyz";
|
||||
const GIT_USER: &'static str = "XOR64";
|
||||
const GIT_REPO: &'static str = "xmpd";
|
||||
|
||||
#[cfg(target_family = "windows")]
|
||||
const EXT: &'static str = ".exe";
|
||||
#[cfg(target_family = "unix")]
|
||||
const EXT: &'static str = "";
|
||||
|
||||
|
||||
|
||||
pub type Result<T> = anyhow::Result<T>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReleaseInfo {
|
||||
tag_name: String,
|
||||
assets: Vec<ReleaseAsset>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReleaseAsset {
|
||||
browser_download_url: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
|
||||
pub struct Update {
|
||||
|
||||
}
|
||||
|
||||
impl Update {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
pub fn update_xmpd_if_needed(&self) -> Result<()> {
|
||||
let current = semver::Version::parse(CURRENT_VERSION)?;
|
||||
log::info!("Current xmpd version: {current}");
|
||||
let client = Client::new();
|
||||
let req = client
|
||||
.get(format!("https://{GIT_HOST}/api/v1/repos/{GIT_USER}/{GIT_REPO}/releases/latest"))
|
||||
.send();
|
||||
|
||||
let Ok(req) = req else {
|
||||
log::warn!("Unable to check latest release! (No internet?)");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let exec_p = std::env::current_exe()?;
|
||||
let exec_d = exec_p.parent().unwrap_or(&exec_p);
|
||||
|
||||
let release: ReleaseInfo = req.json().map_err(|e| anyhow!("Unable to parse release info: {e}"))?;
|
||||
let latest = semver::Version::parse(&release.tag_name)?;
|
||||
|
||||
|
||||
log::info!("Latest xmpd version: {latest}");
|
||||
if latest > current {
|
||||
log::warn!("Update available, proceeding to update!");
|
||||
for asset in release.assets {
|
||||
if asset.name != format!("xmpd{EXT}") {
|
||||
continue;
|
||||
}
|
||||
let tmp_f = exec_d.join(format!("xmpd.update{EXT}"));
|
||||
let old_f = exec_d.join(format!("xmpd.old{EXT}"));
|
||||
log::info!("Downloading {}...", asset.browser_download_url);
|
||||
let mut res;
|
||||
match reqwest::blocking::get(asset.browser_download_url) {
|
||||
Ok(r) => res = r,
|
||||
Err(e) => {
|
||||
log::error!("Failed to download update: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let mut tmp_h = std::fs::File::create(&tmp_f)?;
|
||||
if let Err(e) = std::io::copy(&mut res, &mut tmp_h) {
|
||||
log::error!("Failed to write update to file: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
drop(tmp_h); // close handle (just in case you didnt know)
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
{
|
||||
// set *only* the exec bit, doesnt change other permissions
|
||||
let meta = std::fs::metadata(&tmp_f)?;
|
||||
let mut perms = meta.permissions();
|
||||
perms.set_mode(perms.mode() | 0o111);
|
||||
|
||||
std::fs::set_permissions(&tmp_f, perms)?;
|
||||
}
|
||||
std::fs::rename(&exec_p, &old_f)?;
|
||||
std::fs::rename(&tmp_f, &exec_p)?;
|
||||
|
||||
log::debug!("{exec_p:?} -> {old_f:?}");
|
||||
log::debug!("{tmp_f:?} -> {exec_p:?}");
|
||||
log::info!("Update done, restarting!");
|
||||
|
||||
let status = std::process::Command::new(exec_p)
|
||||
.args(&args().collect::<Vec<String>>().as_slice()[1..])
|
||||
.stdin(Stdio::inherit())
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
.status()?;
|
||||
exit(status.code().unwrap_or(0));
|
||||
}
|
||||
|
||||
} else {
|
||||
log::info!("Already at latest version, skipping update!");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user