Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(cli): self-update via `jan update`

Akarshan Biswasbbec0748c141
diff --git a/src-tauri/src/bin/jan.rs b/src-tauri/src/bin/jan.rs
index b3ae6e331..0acccb4b1 100644
--- a/src-tauri/src/bin/jan.rs
+++ b/src-tauri/src/bin/jan.rs
@@ -36,7 +36,8 @@ Models are served by remote providers configured in ~/.jan/config.toml\n\
jan --resume 3f7a91c2 # resume a session by id (or id prefix)\n \
jan cli agent run \"fix the failing test\" # run the agent non-interactively\n \
jan cli models list # show every configured provider model\n \
- jan cli threads list # list saved conversation threads",
+ jan cli threads list # list saved conversation threads\n \
+ jan update # install the latest build of this channel",
version
)]
struct Cli {
@@ -125,6 +126,16 @@ enum Commands {
#[command(subcommand)]
cmd: AgentConfigCommands,
},
+ /// Update this binary to the latest build of the channel it was built for
+ #[command(display_order = 3)]
+ Update {
+ /// Report whether an update exists without installing it
+ #[arg(long)]
+ check: bool,
+ /// Reinstall even when already on the latest version
+ #[arg(long, conflicts_with = "check")]
+ force: bool,
+ },
}
/// The non-interactive command surface, reached via `jan cli <command>`.
@@ -348,7 +359,10 @@ async fn main() {
}))
.init();
- app_lib::core::cli::updater::print_update_notice_if_available().await;
+ // `jan update` reports the same thing itself, in more detail.
+ if !std::env::args().any(|a| a == "update") {
+ app_lib::core::cli::updater::print_update_notice_if_available().await;
+ }
// Inject the logo at runtime so we can use ANSI styling.
let logo = make_logo();
@@ -387,6 +401,43 @@ async fn main() {
std::process::exit(1);
}
}
+ Commands::Update { check, force } => handle_update(check, force).await,
+ }
+}
+
+// ── Update handler ─────────────────────────────────────────────────────────
+
+async fn handle_update(check: bool, force: bool) {
+ use app_lib::core::cli::updater::{self, UpdateOutcome};
+
+ let result = if check {
+ updater::check_for_update(std::time::Duration::from_secs(10))
+ .await
+ .map(|u| {
+ if u.is_newer() {
+ println!(
+ "A new {} build is available: {} -> {}",
+ u.channel, u.current, u.latest
+ );
+ } else {
+ println!("Already on the latest {} build ({})", u.channel, u.current);
+ }
+ })
+ } else {
+ updater::self_update(force)
+ .await
+ .map(|outcome| match outcome {
+ UpdateOutcome::UpToDate { version } => {
+ println!("Already up to date ({version})");
+ }
+ UpdateOutcome::Installed { from, to, path } => {
+ println!("Updated {} from {from} to {to}", path.display());
+ }
+ })
+ };
+ if let Err(e) = result {
+ eprintln!("Error: {e}");
+ std::process::exit(1);
}
}
@@ -578,4 +629,22 @@ mod tests {
let cli = Cli::parse_from(["jan"]);
assert!(!cli.plan);
}
+
+ #[test]
+ fn update_command_parses() {
+ let cli = Cli::parse_from(["jan", "update"]);
+ assert!(matches!(
+ cli.command,
+ Some(Commands::Update {
+ check: false,
+ force: false
+ })
+ ));
+ let cli = Cli::parse_from(["jan", "update", "--check"]);
+ assert!(matches!(
+ cli.command,
+ Some(Commands::Update { check: true, .. })
+ ));
+ assert!(Cli::try_parse_from(["jan", "update", "--check", "--force"]).is_err());
+ }
}
diff --git a/src-tauri/src/core/cli/updater.rs b/src-tauri/src/core/cli/updater.rs
index bd6f6f596..00db5a0b5 100644
--- a/src-tauri/src/core/cli/updater.rs
+++ b/src-tauri/src/core/cli/updater.rs
@@ -1,16 +1,23 @@
-//! Startup update check for the headless `jan` CLI.
+//! Startup update check and self-update for the headless `jan` CLI.
//!
//! Only active when the binary was built by the nightly CI templates, which
//! embed `JAN_CLI_UPDATE_CHANNEL` (e.g. `agent-nightly`) and
//! `JAN_CLI_BUILD_VERSION` (the actual nightly version, since `Cargo.toml`'s
//! `version` stays pinned). A local `cargo build --features cli` has neither,
-//! so the check is a no-op there.
+//! so both the check and `jan update` are no-ops there.
+use std::cmp::Ordering;
+use std::fs::{self, File};
+use std::io::{self, Read, Write};
+use std::path::{Path, PathBuf};
use std::time::Duration;
use serde::Deserialize;
+use sha2::{Digest, Sha256};
const CHECK_TIMEOUT: Duration = Duration::from_millis(1500);
+const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(300);
+const BINARY_NAME: &str = if cfg!(windows) { "jan.exe" } else { "jan" };
#[derive(Debug, Deserialize)]
struct UpdateManifest {
@@ -19,6 +26,29 @@ struct UpdateManifest {
platforms: serde_json::Value,
}
+/// A newer build published on the channel this binary was built for.
+#[derive(Debug, Clone)]
+pub struct AvailableUpdate {
+ pub channel: &'static str,
+ pub current: String,
+ pub latest: String,
+ pub url: Option<String>,
+ pub sha256: Option<String>,
+}
+
+/// What `self_update` actually did.
+#[derive(Debug)]
+pub enum UpdateOutcome {
+ UpToDate {
+ version: String,
+ },
+ Installed {
+ from: String,
+ to: String,
+ path: PathBuf,
+ },
+}
+
fn update_channel() -> Option<&'static str> {
option_env!("JAN_CLI_UPDATE_CHANNEL")
}
@@ -38,10 +68,40 @@ fn platform_key() -> Option<&'static str> {
}
}
-async fn fetch_manifest(channel: &str) -> Result<UpdateManifest, String> {
+/// Compare nightly versions like `0.8.4-1723`: split into `.`/`-` separated
+/// segments, comparing numerically where both sides are numeric so that
+/// build 9 sorts before build 10. A shorter version is the older one
+/// (`0.8.4` predates `0.8.4-1`).
+fn compare_versions(a: &str, b: &str) -> Ordering {
+ let seg = |v: &str| -> Vec<String> {
+ v.trim_start_matches('v')
+ .split(['.', '-', '+'])
+ .filter(|s| !s.is_empty())
+ .map(str::to_string)
+ .collect()
+ };
+ let (a, b) = (seg(a), seg(b));
+ for i in 0..a.len().max(b.len()) {
+ let ord = match (a.get(i), b.get(i)) {
+ (Some(x), Some(y)) => match (x.parse::<u64>(), y.parse::<u64>()) {
+ (Ok(x), Ok(y)) => x.cmp(&y),
+ _ => x.cmp(y),
+ },
+ (Some(_), None) => Ordering::Greater,
+ (None, Some(_)) => Ordering::Less,
+ (None, None) => Ordering::Equal,
+ };
+ if ord != Ordering::Equal {
+ return ord;
+ }
+ }
+ Ordering::Equal
+}
+
+async fn fetch_manifest(channel: &str, timeout: Duration) -> Result<UpdateManifest, String> {
let url = format!("https://delta.jan.ai/{channel}/manifest.json");
let client = reqwest::Client::builder()
- .timeout(CHECK_TIMEOUT)
+ .timeout(timeout)
.build()
.map_err(|e| e.to_string())?;
client
@@ -49,11 +109,49 @@ async fn fetch_manifest(channel: &str) -> Result<UpdateManifest, String> {
.send()
.await
.map_err(|e| e.to_string())?
+ .error_for_status()
+ .map_err(|e| e.to_string())?
.json::<UpdateManifest>()
.await
.map_err(|e| e.to_string())
}
+fn read_update(manifest: &UpdateManifest, channel: &'static str) -> AvailableUpdate {
+ let entry = platform_key().and_then(|key| manifest.platforms.get(key));
+ let field = |name: &str| {
+ entry
+ .and_then(|p| p.get(name))
+ .and_then(|v| v.as_str())
+ .map(str::to_string)
+ };
+ AvailableUpdate {
+ channel,
+ current: build_version().to_string(),
+ latest: manifest.version.clone(),
+ url: field("url"),
+ sha256: field("sha256"),
+ }
+}
+
+/// Resolve the newest published build, or `None` when this binary has no
+/// channel embedded (a local build) or the manifest can't be reached.
+pub async fn check_for_update(timeout: Duration) -> Result<AvailableUpdate, String> {
+ let channel = update_channel().ok_or_else(|| {
+ "this build has no update channel embedded (built from source, not by the nightly CI)"
+ .to_string()
+ })?;
+ let manifest = tokio::time::timeout(timeout, fetch_manifest(channel, timeout))
+ .await
+ .map_err(|_| format!("timed out contacting https://delta.jan.ai/{channel}"))??;
+ Ok(read_update(&manifest, channel))
+}
+
+impl AvailableUpdate {
+ pub fn is_newer(&self) -> bool {
+ compare_versions(&self.latest, &self.current) == Ordering::Greater
+ }
+}
+
/// Check for a newer nightly build and print a one-line notice to stderr.
/// Best-effort: any network error, timeout, or missing embed is silently
/// ignored so it never blocks or breaks CLI startup.
@@ -61,30 +159,417 @@ pub async fn print_update_notice_if_available() {
if std::env::var_os("JAN_CLI_NO_UPDATE_CHECK").is_some() {
return;
}
- let Some(channel) = update_channel() else {
+ let Ok(update) = check_for_update(CHECK_TIMEOUT).await else {
return;
};
- let current = build_version();
- let Ok(Ok(manifest)) =
- tokio::time::timeout(CHECK_TIMEOUT, fetch_manifest(channel)).await
- else {
+ if !update.is_newer() {
return;
+ }
+ eprintln!(
+ "A new {} build is available: {} -> {}. Run `jan update` to install it.",
+ update.channel, update.current, update.latest
+ );
+}
+
+// ── Self-update ────────────────────────────────────────────────────────────
+
+/// Path of the binary to replace, with symlinks resolved so that updating a
+/// `~/.local/bin/jan` symlink rewrites the real file rather than clobbering
+/// the link.
+fn target_binary_path() -> Result<PathBuf, String> {
+ let exe = std::env::current_exe().map_err(|e| format!("cannot locate this binary: {e}"))?;
+ Ok(fs::canonicalize(&exe).unwrap_or(exe))
+}
+
+/// Fail early (before downloading tens of megabytes) when the install location
+/// is not writable, e.g. a `/usr/local/bin` install owned by root.
+fn ensure_writable(exe: &Path) -> Result<(), String> {
+ let dir = exe
+ .parent()
+ .ok_or_else(|| format!("{} has no parent directory", exe.display()))?;
+ let probe = dir.join(format!(".jan-update-probe-{}", std::process::id()));
+ let writable = File::create(&probe).is_ok();
+ let _ = fs::remove_file(&probe);
+ if !writable {
+ return Err(format!(
+ "{} is not writable; re-run with the permissions that own the install (or reinstall manually)",
+ dir.display()
+ ));
+ }
+ if fs::metadata(exe)
+ .map(|m| m.permissions().readonly())
+ .unwrap_or(false)
+ {
+ return Err(format!("{} is read-only", exe.display()));
+ }
+ Ok(())
+}
+
+async fn download_to(url: &str, dest: &Path) -> Result<String, String> {
+ if !url.starts_with("https://") {
+ return Err(format!("refusing to download over a non-HTTPS URL: {url}"));
+ }
+ let client = reqwest::Client::builder()
+ .timeout(DOWNLOAD_TIMEOUT)
+ .build()
+ .map_err(|e| e.to_string())?;
+ let mut response = client
+ .get(url)
+ .send()
+ .await
+ .map_err(|e| format!("download failed: {e}"))?
+ .error_for_status()
+ .map_err(|e| format!("download failed: {e}"))?;
+
+ let mut file = File::create(dest).map_err(|e| format!("{}: {e}", dest.display()))?;
+ let mut hasher = Sha256::new();
+ while let Some(chunk) = response
+ .chunk()
+ .await
+ .map_err(|e| format!("download failed: {e}"))?
+ {
+ hasher.update(&chunk);
+ file.write_all(&chunk)
+ .map_err(|e| format!("{}: {e}", dest.display()))?;
+ }
+ file.flush().map_err(|e| e.to_string())?;
+ Ok(hex::encode(hasher.finalize()))
+}
+
+/// Extract the `jan` binary out of a release archive. Entry paths differ per
+/// platform (the Windows zip keeps the build-tree prefix), so match on the
+/// file name rather than the full path.
+fn extract_binary(archive: &Path, dest: &Path) -> Result<(), String> {
+ let name = archive.file_name().unwrap_or_default().to_string_lossy();
+ let mut out = File::create(dest).map_err(|e| format!("{}: {e}", dest.display()))?;
+ let found = if name.ends_with(".zip") {
+ extract_from_zip(archive, &mut out)?
+ } else {
+ extract_from_tar_gz(archive, &mut out)?
};
- if manifest.version == current {
- return;
+ if !found {
+ return Err(format!(
+ "no `{BINARY_NAME}` entry inside the release archive"
+ ));
+ }
+ out.flush().map_err(|e| e.to_string())?;
+ Ok(())
+}
+
+fn is_binary_entry(path: &Path) -> bool {
+ path.file_name().map(|n| n == BINARY_NAME).unwrap_or(false)
+}
+
+fn extract_from_tar_gz(archive: &Path, out: &mut impl Write) -> Result<bool, String> {
+ let file = File::open(archive).map_err(|e| format!("{}: {e}", archive.display()))?;
+ let mut tar = tar::Archive::new(flate2::read::GzDecoder::new(file));
+ for entry in tar.entries().map_err(|e| e.to_string())? {
+ let mut entry = entry.map_err(|e| e.to_string())?;
+ let path = entry.path().map_err(|e| e.to_string())?.into_owned();
+ if is_binary_entry(&path) {
+ io::copy(&mut entry, out).map_err(|e| e.to_string())?;
+ return Ok(true);
+ }
+ }
+ Ok(false)
+}
+
+fn extract_from_zip(archive: &Path, out: &mut impl Write) -> Result<bool, String> {
+ let file = File::open(archive).map_err(|e| format!("{}: {e}", archive.display()))?;
+ let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?;
+ for i in 0..zip.len() {
+ let mut entry = zip.by_index(i).map_err(|e| e.to_string())?;
+ let matches = entry.enclosed_name().map(is_binary_entry).unwrap_or(false);
+ if matches {
+ io::copy(&mut entry, out).map_err(|e| e.to_string())?;
+ return Ok(true);
+ }
}
- let download_url = platform_key()
- .and_then(|key| manifest.platforms.get(key))
- .and_then(|p| p.get("url"))
- .and_then(|u| u.as_str());
- match download_url {
- Some(url) => eprintln!(
- "A new {channel} build is available: {current} -> {} ({url})",
- manifest.version
- ),
- None => eprintln!(
- "A new {channel} build is available: {current} -> {}",
- manifest.version
- ),
+ Ok(false)
+}
+
+/// Sanity-check the extracted file before it replaces a working binary: a
+/// truncated download or an HTML error page must not be installed.
+fn verify_executable(path: &Path) -> Result<(), String> {
+ let mut magic = [0u8; 4];
+ let read = File::open(path)
+ .and_then(|mut f| f.read(&mut magic))
+ .map_err(|e| format!("{}: {e}", path.display()))?;
+ let looks_native = read == 4
+ && (magic == [0x7f, b'E', b'L', b'F'] // ELF
+ || magic[..2] == *b"MZ" // PE
+ || magic == [0xcf, 0xfa, 0xed, 0xfe] // Mach-O 64
+ || magic == [0xca, 0xfe, 0xba, 0xbe]); // Mach-O universal
+ if !looks_native {
+ return Err("the downloaded file is not an executable".to_string());
+ }
+ Ok(())
+}
+
+fn make_executable(path: &Path) -> Result<(), String> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ fs::set_permissions(path, fs::Permissions::from_mode(0o755))
+ .map_err(|e| format!("{}: {e}", path.display()))?;
+ }
+ #[cfg(not(unix))]
+ let _ = path;
+ Ok(())
+}
+
+/// Swap the staged binary in. Both files live in the install directory so the
+/// rename is atomic; on Windows the running image cannot be overwritten, so
+/// move it aside first and let a later run delete it.
+fn replace_binary(staged: &Path, exe: &Path) -> Result<(), String> {
+ #[cfg(windows)]
+ {
+ let backup = exe.with_extension("old");
+ let _ = fs::remove_file(&backup);
+ fs::rename(exe, &backup).map_err(|e| format!("{}: {e}", exe.display()))?;
+ if let Err(e) = fs::rename(staged, exe) {
+ let _ = fs::rename(&backup, exe);
+ return Err(format!("{}: {e}", exe.display()));
+ }
+ return Ok(());
+ }
+ #[cfg(not(windows))]
+ fs::rename(staged, exe).map_err(|e| format!("{}: {e}", exe.display()))
+}
+
+/// Leftover from a previous Windows update; the file is unlocked once the old
+/// process has exited, so clean it up opportunistically.
+fn clean_stale_backup(exe: &Path) {
+ if cfg!(windows) {
+ let _ = fs::remove_file(exe.with_extension("old"));
+ }
+}
+
+/// Download the newest build for this channel and replace the running binary
+/// in place. `force` reinstalls even when the versions already match.
+pub async fn self_update(force: bool) -> Result<UpdateOutcome, String> {
+ let update = check_for_update(CHECK_TIMEOUT).await?;
+ if !update.is_newer() && !force {
+ return Ok(UpdateOutcome::UpToDate {
+ version: update.current,
+ });
+ }
+ let url = update.url.clone().ok_or_else(|| {
+ format!(
+ "no {} build published for {}-{}",
+ update.channel,
+ std::env::consts::OS,
+ std::env::consts::ARCH
+ )
+ })?;
+
+ let exe = target_binary_path()?;
+ clean_stale_backup(&exe);
+ ensure_writable(&exe)?;
+ let dir = exe.parent().unwrap_or(Path::new("."));
+
+ let pid = std::process::id();
+ let archive = dir.join(format!(".jan-update-{pid}{}", archive_suffix(&url)));
+ let staged = dir.join(format!(".jan-update-{pid}.bin"));
+ let result = install(&url, update.sha256.as_deref(), &archive, &staged, &exe).await;
+ let _ = fs::remove_file(&archive);
+ let _ = fs::remove_file(&staged);
+ result?;
+ Ok(UpdateOutcome::Installed {
+ from: update.current,
+ to: update.latest,
+ path: exe,
+ })
+}
+
+/// Download, verify and swap in the new binary. Staging files are cleaned up
+/// by the caller on both the success and failure paths.
+async fn install(
+ url: &str,
+ sha256: Option<&str>,
+ archive: &Path,
+ staged: &Path,
+ exe: &Path,
+) -> Result<(), String> {
+ let digest = download_to(url, archive).await?;
+ if let Some(expected) = sha256 {
+ if !expected.eq_ignore_ascii_case(&digest) {
+ return Err(format!(
+ "checksum mismatch for {url}: expected {expected}, got {digest}"
+ ));
+ }
+ }
+ extract_binary(archive, staged)?;
+ verify_executable(staged)?;
+ make_executable(staged)?;
+ replace_binary(staged, exe)
+}
+
+fn archive_suffix(url: &str) -> &'static str {
+ if url.ends_with(".zip") {
+ ".zip"
+ } else {
+ ".tar.gz"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::io::Cursor;
+
+ #[test]
+ fn nightly_build_numbers_compare_numerically() {
+ assert_eq!(compare_versions("0.8.4-10", "0.8.4-9"), Ordering::Greater);
+ assert_eq!(compare_versions("0.8.4-9", "0.8.4-10"), Ordering::Less);
+ assert_eq!(compare_versions("0.8.4-9", "0.8.4-9"), Ordering::Equal);
+ assert_eq!(compare_versions("0.9.0-1", "0.8.4-999"), Ordering::Greater);
+ assert_eq!(compare_versions("v0.8.4", "0.8.4"), Ordering::Equal);
+ // A plain release is older than any nightly cut from it.
+ assert_eq!(compare_versions("0.8.4-1", "0.8.4"), Ordering::Greater);
+ }
+
+ fn update_with(latest: &str, current: &str) -> AvailableUpdate {
+ AvailableUpdate {
+ channel: "agent-nightly",
+ current: current.to_string(),
+ latest: latest.to_string(),
+ url: None,
+ sha256: None,
+ }
+ }
+
+ #[test]
+ fn only_strictly_newer_versions_trigger_an_update() {
+ assert!(update_with("0.8.4-11", "0.8.4-10").is_newer());
+ assert!(!update_with("0.8.4-10", "0.8.4-10").is_newer());
+ // A rolled-back manifest must not offer a "new" older build.
+ assert!(!update_with("0.8.4-9", "0.8.4-10").is_newer());
+ }
+
+ #[test]
+ fn manifest_fields_are_read_per_platform() {
+ let manifest: UpdateManifest = serde_json::from_value(serde_json::json!({
+ "version": "0.8.4-12",
+ "platforms": {
+ platform_key().unwrap_or("linux-x86_64"): {
+ "url": "https://delta.jan.ai/agent-nightly/jan.tar.gz",
+ "sha256": "abc123"
+ }
+ }
+ }))
+ .unwrap();
+ let update = read_update(&manifest, "agent-nightly");
+ assert_eq!(update.latest, "0.8.4-12");
+ if platform_key().is_some() {
+ assert_eq!(
+ update.url.as_deref(),
+ Some("https://delta.jan.ai/agent-nightly/jan.tar.gz")
+ );
+ assert_eq!(update.sha256.as_deref(), Some("abc123"));
+ }
+ }
+
+ #[test]
+ fn missing_platform_entry_yields_no_url() {
+ let manifest: UpdateManifest = serde_json::from_value(serde_json::json!({
+ "version": "0.8.4-12",
+ "platforms": { "solaris-sparc": { "url": "https://example.com/x" } }
+ }))
+ .unwrap();
+ assert!(read_update(&manifest, "agent-nightly").url.is_none());
+ }
+
+ fn tar_gz_with(entry: &str, body: &[u8]) -> Vec<u8> {
+ let mut header = tar::Header::new_gnu();
+ header.set_size(body.len() as u64);
+ header.set_mode(0o755);
+ header.set_cksum();
+ let mut builder = tar::Builder::new(flate2::write::GzEncoder::new(
+ Vec::new(),
+ flate2::Compression::fast(),
+ ));
+ builder.append_data(&mut header, entry, body).unwrap();
+ builder.into_inner().unwrap().finish().unwrap()
+ }
+
+ #[test]
+ fn extracts_the_binary_from_a_tarball() {
+ let dir = tempfile::tempdir().unwrap();
+ let archive = dir.path().join("jan-agent-linux-x86_64-0.8.4-1.tar.gz");
+ fs::write(&archive, tar_gz_with(BINARY_NAME, b"\x7fELF-payload")).unwrap();
+ let dest = dir.path().join("out");
+ extract_binary(&archive, &dest).unwrap();
+ assert_eq!(fs::read(&dest).unwrap(), b"\x7fELF-payload");
+ }
+
+ #[test]
+ fn rejects_an_archive_without_the_binary() {
+ let dir = tempfile::tempdir().unwrap();
+ let archive = dir.path().join("bad.tar.gz");
+ fs::write(&archive, tar_gz_with("README.md", b"nope")).unwrap();
+ let err = extract_binary(&archive, &dir.path().join("out")).unwrap_err();
+ assert!(err.contains(BINARY_NAME), "{err}");
+ }
+
+ #[test]
+ fn extracts_the_binary_from_a_zip_with_a_path_prefix() {
+ let dir = tempfile::tempdir().unwrap();
+ let archive = dir.path().join("jan-agent-windows-x86_64-0.8.4-1.zip");
+ let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ zip.start_file(
+ format!("src-tauri/target/release/{BINARY_NAME}"),
+ zip::write::FileOptions::default(),
+ )
+ .unwrap();
+ zip.write_all(b"MZ-payload").unwrap();
+ fs::write(&archive, zip.finish().unwrap().into_inner()).unwrap();
+ let dest = dir.path().join("out");
+ extract_binary(&archive, &dest).unwrap();
+ assert_eq!(fs::read(&dest).unwrap(), b"MZ-payload");
+ }
+
+ #[test]
+ fn html_error_pages_are_not_installed() {
+ let dir = tempfile::tempdir().unwrap();
+ let bogus = dir.path().join("out");
+ fs::write(&bogus, b"<!DOCTYPE html><html>404</html>").unwrap();
+ assert!(verify_executable(&bogus).is_err());
+
+ let good = dir.path().join("good");
+ fs::write(&good, b"\x7fELF\x02\x01").unwrap();
+ verify_executable(&good).unwrap();
+ }
+
+ #[test]
+ fn replace_binary_swaps_the_file_in_place() {
+ let dir = tempfile::tempdir().unwrap();
+ let exe = dir.path().join(BINARY_NAME);
+ let staged = dir.path().join(".staged");
+ fs::write(&exe, b"old").unwrap();
+ fs::write(&staged, b"new").unwrap();
+ replace_binary(&staged, &exe).unwrap();
+ assert_eq!(fs::read(&exe).unwrap(), b"new");
+ assert!(!staged.exists());
+ }
+
+ #[test]
+ fn unwritable_install_dir_is_rejected_before_downloading() {
+ let dir = tempfile::tempdir().unwrap();
+ let exe = dir.path().join(BINARY_NAME);
+ fs::write(&exe, b"old").unwrap();
+ ensure_writable(&exe).unwrap();
+
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ // Running as root ignores the mode bits, so the check can't be tested there.
+ if unsafe { libc::geteuid() } != 0 {
+ fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o500)).unwrap();
+ let err = ensure_writable(&exe).unwrap_err();
+ fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o700)).unwrap();
+ assert!(err.contains("not writable"), "{err}");
+ }
+ }
}
}