mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* fix(updater): keep one .Hex-update folder, not one per staged dev build Every staged release unpacked its own `.Hex-update-<version>` folder beside `Hex.app`, and the only removal was `finish_on_launch`. The Dev channel checks every ten minutes, so a Hex that runs for days staged a full ~80 MB copy of the app per published build and left every one of them in `/Applications` (the folder it replaces was never removed); the launch sweep only ran when Hex next started, which for a menu-bar app can be days. Staging a newer release now removes the folder of the one it replaces, once the replacement is the staged update, so a running install holds exactly one folder and the app can never be caught without its staged update. The launch sweep stays as the safety net for a swap or an interrupted stage, and now skips symlinks (never followed) and the running bundle itself. The sweep moved to `updater/staging.rs` so it can be tested off macOS: `launch_cleanup_removes_every_unpacked_update_but_the_running_bundle`, `a_newer_update_removes_the_folder_of_the_one_it_replaces`, `a_bundle_running_from_a_staging_folder_keeps_that_folder`, and `a_symlink_named_like_a_staged_update_is_never_followed`, plus the macOS-only `a_newer_staged_release_removes_the_folder_of_the_one_it_replaces`. Observed on macOS 26 with the published 0.0.0-dev-1046 build: the launch sweep removed both a folder holding an older signed build and one holding a swapped-in peer beside /Applications/Hex.app. * fix(updater): never stage a release after a quit installed one a quit installs the staged update itself (`install_on_quit`) while the updater thread can still be in `check()`. two things went wrong in that window: - the late `check` overwrote `shared.staged`, and its `remove_superseded` then deleted the folder holding the bundle the quit had just replaced (the one the draining process runs from), orphaning the release it had unpacked. - `finish_restart` swapped again after `install_on_quit` had applied the update, exchanging the same two bundles back and losing the update. `check` now takes the state and staged locks together, in the order `install_on_quit` takes them, and refuses a release that lands while quitting or after `State::Applied`: it removes the folder it unpacked and keeps the installed update. `finish_restart` reports done without swapping once the state is Applied. The macOS `discard` helper removes that unused folder. `staging::remove_beside` now canonicalizes the bundle's folder and compares the kept folder by name (only when its own folder resolves there), so a `..`, `./`, or symlinked spelling still names the folder to keep, and a path outside it protects nothing. tests: `a_restart_after_a_quit_installed_the_update_does_not_swap_again` (fails without the Applied guard), `a_release_that_finishes_staging_after_a_quit_installed_one_is_discarded` (fails when the decision adopts everything), and `the_folder_to_keep_is_recognized_through_a_non_canonical_spelling` (fails with the old raw path comparison).
225 lines
8.3 KiB
Rust
225 lines
8.3 KiB
Rust
//! The folders macOS updates are unpacked into: `.<name>-update-<version>`
|
|
//! beside the installed bundle (see `macos.rs`). A newer staged release
|
|
//! replaces the earlier one, and a swap parks the bundle it replaced in the
|
|
//! folder that bundle came from until the new version launches, so each is
|
|
//! disposable — except the bundle this process runs from.
|
|
|
|
use std::ffi::OsString;
|
|
use std::fs;
|
|
use std::io;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// What `macos::stage` names an unpacked update's folder with, beside the
|
|
/// installed bundle. Only this exact prefix is ever removed.
|
|
pub const PREFIX: &str = ".Hex-update-";
|
|
|
|
/// An unpacked update this process could not remove.
|
|
///
|
|
/// Only macOS logs these; the tests that exercise the sweep elsewhere read
|
|
/// nothing from it.
|
|
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
|
|
pub struct Failure {
|
|
pub path: PathBuf,
|
|
pub error: io::Error,
|
|
}
|
|
|
|
/// Removes every unpacked update beside `bundle` — a directory in the
|
|
/// bundle's own folder whose name starts with [`PREFIX`] — except `keep`, and
|
|
/// never the bundle itself or anything that is not a directory (a symlink
|
|
/// with the prefix is left where it is, never followed).
|
|
///
|
|
/// Best effort: whatever cannot be removed comes back for the caller to log
|
|
/// and the rest still go.
|
|
pub fn remove_beside(bundle: &Path, keep: Option<&Path>) -> Vec<Failure> {
|
|
let Some(folder) = bundle.parent() else {
|
|
return Vec::new();
|
|
};
|
|
// The caller may spell the bundle's folder through `..`, `./`, or a
|
|
// symlink; compare everything in one canonical spelling of it.
|
|
let Ok(folder) = folder.canonicalize() else {
|
|
return Vec::new();
|
|
};
|
|
let Ok(entries) = fs::read_dir(&folder) else {
|
|
return Vec::new();
|
|
};
|
|
let keep = keep.and_then(|keep| kept_name(&folder, keep));
|
|
let bundle = bundle.file_name();
|
|
entries
|
|
.flatten()
|
|
.filter(|entry| {
|
|
let name = entry.file_name();
|
|
entry.file_type().is_ok_and(|kind| kind.is_dir())
|
|
&& name.to_str().is_some_and(|name| name.starts_with(PREFIX))
|
|
&& Some(name.as_os_str()) != keep.as_deref()
|
|
&& Some(name.as_os_str()) != bundle
|
|
})
|
|
.map(|entry| entry.path())
|
|
.filter_map(|path| {
|
|
fs::remove_dir_all(&path)
|
|
.err()
|
|
.map(|error| Failure { path, error })
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The name `keep` gives a folder in `folder`, whatever spelling it came with.
|
|
/// A path outside `folder` keeps nothing, so a caller can never protect an
|
|
/// unrelated folder; a path that does not exist yet (the update about to be
|
|
/// staged) still names one by its name.
|
|
fn kept_name(folder: &Path, keep: &Path) -> Option<OsString> {
|
|
let parent = keep.parent()?;
|
|
match parent.canonicalize() {
|
|
Ok(parent) if parent == folder => keep.file_name().map(ToOwned::to_owned),
|
|
Ok(_) => None,
|
|
Err(_) => keep.file_name().map(ToOwned::to_owned),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn temporary(name: &str) -> PathBuf {
|
|
let path = std::env::temp_dir().join(format!("hex-staging-{name}-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&path);
|
|
fs::create_dir_all(&path).unwrap();
|
|
path
|
|
}
|
|
|
|
/// A stand-in for the installed `Hex.app` in `root`.
|
|
fn bundle(root: &Path) -> PathBuf {
|
|
let bundle = root.join("Hex.app");
|
|
fs::create_dir_all(bundle.join("Contents/MacOS")).unwrap();
|
|
fs::write(bundle.join("Contents/MacOS/hex"), "running").unwrap();
|
|
bundle
|
|
}
|
|
|
|
/// A stand-in for `stage`'s `.<name>-update-<version>` folder.
|
|
fn staged(root: &Path, version: &str) -> PathBuf {
|
|
let staged = root.join(format!("{PREFIX}{version}"));
|
|
fs::create_dir_all(staged.join("Hex.app/Contents")).unwrap();
|
|
fs::write(staged.join("Hex.app/Contents/version"), version).unwrap();
|
|
staged
|
|
}
|
|
|
|
#[test]
|
|
fn launch_cleanup_removes_every_unpacked_update_but_the_running_bundle() {
|
|
let root = temporary("launch");
|
|
let bundle = bundle(&root);
|
|
let older = staged(&root, "0.0.0-dev-1014");
|
|
let swapped = staged(&root, "0.0.0-dev-1046");
|
|
fs::create_dir_all(root.join("Other.app/Contents")).unwrap();
|
|
fs::write(root.join("notes.txt"), "keep me").unwrap();
|
|
|
|
let failures = remove_beside(&bundle, None);
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(!older.exists(), "an earlier update's folder is removed");
|
|
assert!(
|
|
!swapped.exists(),
|
|
"the swapped-out bundle's folder is removed"
|
|
);
|
|
assert!(bundle.exists(), "the running bundle survives");
|
|
assert_eq!(
|
|
fs::read_to_string(bundle.join("Contents/MacOS/hex")).unwrap(),
|
|
"running"
|
|
);
|
|
assert!(root.join("Other.app").exists(), "another app is not ours");
|
|
assert!(root.join("notes.txt").exists(), "only the prefix is ours");
|
|
let _ = fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn a_newer_update_removes_the_folder_of_the_one_it_replaces() {
|
|
let root = temporary("replaced");
|
|
let bundle = bundle(&root);
|
|
let replaced = staged(&root, "0.0.0-dev-1014");
|
|
let staging = staged(&root, "0.0.0-dev-1017");
|
|
fs::write(replaced.join("Hex.app/Contents/marker"), "replaced").unwrap();
|
|
|
|
let failures = remove_beside(&bundle, Some(&staging));
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(!replaced.exists(), "the update it replaces is removed");
|
|
assert!(staging.exists(), "the update being staged stays");
|
|
assert!(bundle.exists());
|
|
let _ = fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn the_folder_to_keep_is_recognized_through_a_non_canonical_spelling() {
|
|
let root = temporary("keep-spelling");
|
|
let bundle = bundle(&root);
|
|
let replaced = staged(&root, "0.0.0-dev-1014");
|
|
let staging = staged(&root, "0.0.0-dev-1017");
|
|
fs::create_dir_all(root.join("nested")).unwrap();
|
|
let spelling = root
|
|
.join("nested")
|
|
.join("..")
|
|
.join(format!("{PREFIX}0.0.0-dev-1017"));
|
|
assert_ne!(spelling, staging);
|
|
|
|
let failures = remove_beside(&bundle, Some(&spelling));
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(!replaced.exists(), "the update it replaces is removed");
|
|
assert!(staging.exists(), "the folder it names stays");
|
|
let _ = fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[test]
|
|
fn a_keep_path_outside_the_bundles_folder_protects_nothing() {
|
|
let root = temporary("keep-elsewhere");
|
|
let bundle = bundle(&root);
|
|
let elsewhere = temporary("keep-elsewhere-other");
|
|
let staging = staged(&root, "0.0.0-dev-1017");
|
|
let lookalike = elsewhere.join(format!("{PREFIX}0.0.0-dev-1017"));
|
|
|
|
let failures = remove_beside(&bundle, Some(&lookalike));
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(
|
|
!staging.exists(),
|
|
"a path in another folder keeps nothing here"
|
|
);
|
|
let _ = fs::remove_dir_all(root);
|
|
let _ = fs::remove_dir_all(elsewhere);
|
|
}
|
|
|
|
#[test]
|
|
fn a_bundle_running_from_a_staging_folder_keeps_that_folder() {
|
|
let root = temporary("inside");
|
|
let staging = staged(&root, "0.0.0-dev-1046");
|
|
let bundle = staging.join("Hex.app");
|
|
|
|
let failures = remove_beside(&bundle, None);
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(bundle.exists());
|
|
assert!(staging.exists(), "the folder this bundle runs from stays");
|
|
let _ = fs::remove_dir_all(root);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_symlink_named_like_a_staged_update_is_never_followed() {
|
|
let root = temporary("symlink");
|
|
let bundle = bundle(&root);
|
|
let precious = root.join("precious");
|
|
fs::create_dir_all(precious.join("Hex.app")).unwrap();
|
|
std::os::unix::fs::symlink(&precious, root.join(format!("{PREFIX}1.0.0"))).unwrap();
|
|
|
|
let failures = remove_beside(&bundle, None);
|
|
|
|
assert!(failures.is_empty());
|
|
assert!(
|
|
precious.join("Hex.app").exists(),
|
|
"nothing behind it is touched"
|
|
);
|
|
assert!(
|
|
root.join(format!("{PREFIX}1.0.0")).exists(),
|
|
"the link itself is left alone"
|
|
);
|
|
let _ = fs::remove_dir_all(root);
|
|
}
|
|
}
|