Determine the install source for a binary at `current_exe`. Manifest is consulted first under the trust gate (canonicalized path must match current_exe). Preserve its command path for upgrade guidance, rather than installing into a versioned symlink target.
(current_exe: &Path, env: &dyn EnvLookup)
| 184 | /// must match current_exe). Preserve its command path for upgrade guidance, |
| 185 | /// rather than installing into a versioned symlink target. |
| 186 | pub fn detect_source(current_exe: &Path, env: &dyn EnvLookup) -> InstallSource { |
| 187 | let canonical_exe = |
| 188 | std::fs::canonicalize(current_exe).unwrap_or_else(|_| current_exe.to_path_buf()); |
| 189 | |
| 190 | // 1. Manifest trust gate. Any parse / schema / mismatch failure falls |
| 191 | // through silently to path heuristics — manifest is best-effort |
| 192 | // metadata, not a source of errors. |
| 193 | let manifest_p = manifest_path(env); |
| 194 | if let Ok(raw) = std::fs::read_to_string(&manifest_p) { |
| 195 | if let Ok(m) = serde_json::from_str::<Manifest>(&raw) { |
| 196 | if m.schema_version == 1 && m.source == "official-installer" { |
| 197 | let recorded = std::fs::canonicalize(&m.install_path); |
| 198 | if recorded.as_deref().unwrap_or(&m.install_path) == canonical_exe { |
| 199 | return InstallSource::OfficialInstaller { |
| 200 | manifest_path: manifest_p, |
| 201 | recorded_install_path: m.install_path, |
| 202 | recorded_version: m.version, |
| 203 | }; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // 2. Path heuristics. |
| 210 | let path_str = canonical_exe.to_string_lossy(); |
| 211 | |
| 212 | // Homebrew: Apple Silicon (/opt/homebrew), Intel (/usr/local/Cellar), |
| 213 | // and linuxbrew. canonicalize() above already resolved the |
| 214 | // /opt/homebrew/bin/atomic symlink into the Cellar path on macOS. |
| 215 | let homebrew_markers = [ |
| 216 | "/Cellar/", |
| 217 | "/opt/homebrew/", |
| 218 | "/home/linuxbrew/.linuxbrew/", |
| 219 | "/usr/local/Homebrew/", |
| 220 | ]; |
| 221 | for marker in homebrew_markers { |
| 222 | if let Some(idx) = path_str.find(marker) { |
| 223 | let prefix = if marker == "/Cellar/" { |
| 224 | PathBuf::from(&path_str[..idx]) |
| 225 | } else { |
| 226 | PathBuf::from(marker.trim_end_matches('/')) |
| 227 | }; |
| 228 | return InstallSource::Homebrew { prefix }; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Cargo: $CARGO_HOME/bin or ~/.cargo/bin. |
| 233 | if let Some(cargo_home) = env.var("CARGO_HOME") { |
| 234 | if path_str.starts_with(&format!("{}/bin/", cargo_home)) { |
| 235 | return InstallSource::Cargo; |
| 236 | } |
| 237 | } |
| 238 | if let Some(home) = env.var("HOME") { |
| 239 | if path_str.starts_with(&format!("{}/.cargo/bin/", home)) { |
| 240 | return InstallSource::Cargo; |
| 241 | } |
| 242 | } |
| 243 |