kimun_notes/update/channel.rs
1//! Install-channel detection. Decides whether this binary may self-update or
2//! must defer to a package manager. Detection that is ambiguous or fails is
3//! treated as notify-only: the conservative default never risks corrupting a
4//! managed install.
5//!
6//! Order of precedence:
7//! 1. The install marker (`install.toml`) written by `install.sh` — deterministic.
8//! 2. A heuristic on the canonicalised executable path.
9//!
10//! Anything that cannot be classified fails safe to notify-only.
11
12use std::env;
13use std::path::Path;
14use std::sync::OnceLock;
15
16const MARKER_FILE: &str = "install.toml";
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum InstallChannel {
20 /// Installed via the official `install.sh`.
21 Script,
22 /// A manually downloaded release archive.
23 Direct,
24 /// Homebrew tap — package-manager owned, notify-only.
25 Brew,
26 /// `cargo install` — package-manager owned, notify-only.
27 Cargo,
28 /// Could not determine; treated as notify-only.
29 Unknown,
30}
31
32impl InstallChannel {
33 /// Whether kimün may replace its own binary on this channel.
34 pub fn self_update_eligible(self) -> bool {
35 matches!(self, Self::Script | Self::Direct)
36 }
37
38 /// The command a user should run to upgrade on a package-manager channel,
39 /// or `None` where self-update applies (or the channel is unknown).
40 pub fn upgrade_hint(self) -> Option<&'static str> {
41 match self {
42 Self::Brew => Some("brew upgrade kimun"),
43 Self::Cargo => Some("cargo install kimun-notes"),
44 _ => None,
45 }
46 }
47}
48
49#[derive(serde::Deserialize)]
50struct InstallMarker {
51 channel: String,
52}
53
54/// Detect how the running binary was installed. `config_dir` is kimün's config
55/// directory (where `install.sh` writes the marker).
56///
57/// The marker (cheap file read, depends on `config_dir`) is consulted first.
58/// The fallback path heuristic — `current_exe` canonicalisation plus a
59/// filesystem writability probe — is the expensive part and is invariant for
60/// the process, so only *it* is cached. Caching keyed on the result of an
61/// argument would let the first caller's `config_dir` win forever.
62pub fn detect(config_dir: &Path) -> InstallChannel {
63 if let Some(channel) = channel_from_marker(config_dir) {
64 return channel;
65 }
66 static EXE_CHANNEL: OnceLock<InstallChannel> = OnceLock::new();
67 *EXE_CHANNEL.get_or_init(channel_from_exe_path)
68}
69
70fn channel_from_marker(config_dir: &Path) -> Option<InstallChannel> {
71 let raw = std::fs::read_to_string(config_dir.join(MARKER_FILE)).ok()?;
72 let marker: InstallMarker = toml::from_str(&raw).ok()?;
73 match marker.channel.as_str() {
74 "script" => Some(InstallChannel::Script),
75 "direct" => Some(InstallChannel::Direct),
76 "brew" => Some(InstallChannel::Brew),
77 "cargo" => Some(InstallChannel::Cargo),
78 _ => None,
79 }
80}
81
82fn channel_from_exe_path() -> InstallChannel {
83 let exe = match env::current_exe().and_then(|p| p.canonicalize()) {
84 Ok(p) => p,
85 // No idea where we live — do not risk touching a managed binary.
86 Err(_) => return InstallChannel::Unknown,
87 };
88 let path = exe.to_string_lossy();
89
90 // Homebrew: an explicit prefix env var, or the Cellar layout the formula
91 // installs into (current_exe is canonicalised, so brew's bin symlink is
92 // already resolved into the Cellar path).
93 if let Ok(prefix) = env::var("HOMEBREW_PREFIX")
94 && !prefix.is_empty()
95 && path.starts_with(prefix.as_str())
96 {
97 return InstallChannel::Brew;
98 }
99 if path.contains("/Cellar/") || path.contains("/homebrew/") {
100 return InstallChannel::Brew;
101 }
102
103 // cargo install: under CARGO_HOME/bin or ~/.cargo/bin.
104 if let Ok(cargo_home) = env::var("CARGO_HOME")
105 && !cargo_home.is_empty()
106 && exe.starts_with(&cargo_home)
107 {
108 return InstallChannel::Cargo;
109 }
110 if let Ok(home) = crate::settings::get_home_dir()
111 && exe.starts_with(home.join(".cargo").join("bin"))
112 {
113 return InstallChannel::Cargo;
114 }
115
116 // Otherwise the user placed this binary themselves. Only call it
117 // self-update eligible if its directory is actually writable: a binary in a
118 // root-owned/system location (e.g. /usr/bin, a distro package, the Nix
119 // store) must stay notify-only and never be overwritten in place, even
120 // though it is neither brew nor cargo.
121 match exe.parent() {
122 Some(dir) if dir_is_writable(dir) => InstallChannel::Direct,
123 _ => InstallChannel::Unknown,
124 }
125}
126
127/// Whether a probe file can be created in `dir` (i.e. the current user may
128/// write there). Cleans up the probe. A best-effort check used only to gate
129/// self-update eligibility; on any error it returns false (fail safe).
130fn dir_is_writable(dir: &Path) -> bool {
131 let probe = dir.join(format!(".kimun-write-probe-{}", std::process::id()));
132 match std::fs::File::create(&probe) {
133 Ok(_) => {
134 let _ = std::fs::remove_file(&probe);
135 true
136 }
137 Err(_) => false,
138 }
139}