| // SPF Smart Gateway - Path Resolution | |
| // Copyright 2026 Joseph Stone - All Rights Reserved | |
| // | |
| // Single source of truth for all SPF path resolution. | |
| // Uses SPF_ROOT env var (preferred) or walk-up discovery from binary location. | |
| // Cached via OnceLock for zero-overhead repeated access. | |
| // | |
| // SECURITY NOTE: Write allowlist paths are computed here but ENFORCED | |
| // in validate.rs. The allowlist remains compiled Rust, not configurable. | |
| // | |
| // CLONE NOTE: SPF_ROOT env var is checked FIRST so cloned instances | |
| // resolve to their own root, not the primary's. Walk-up from binary | |
| // location is the fallback for single-instance setups. | |
| use std::path::{Path, PathBuf}; | |
| use std::sync::OnceLock; | |
| static SPF_ROOT_CACHE: OnceLock<PathBuf> = OnceLock::new(); | |
| static ACTUAL_HOME_CACHE: OnceLock<PathBuf> = OnceLock::new(); | |
| /// Find SPFsmartGATE root — supports cloned instances. | |
| /// | |
| /// Resolution order: | |
| /// 1. SPF_ROOT environment variable (clone-safe, explicit) | |
| /// 2. Walk up from binary location looking for Cargo.toml | |
| /// 3. HOME env + /SPFsmartGATE | |
| /// 4. Panic (unrecoverable — cannot operate without known root) | |
| pub fn spf_root() -> &'static Path { | |
| SPF_ROOT_CACHE.get_or_init(|| { | |
| // PRIMARY: SPF_ROOT env var — explicit, clone-safe | |
| if let Ok(root) = std::env::var("SPF_ROOT") { | |
| let p = PathBuf::from(&root); | |
| if p.exists() { | |
| return p; | |
| } | |
| } | |
| // FALLBACK: walk up from binary location | |
| if let Ok(exe) = std::env::current_exe() { | |
| if let Ok(canonical) = exe.canonicalize() { | |
| let mut dir = canonical.parent(); | |
| while let Some(d) = dir { | |
| if d.join("Cargo.toml").exists() { | |
| return d.to_path_buf(); | |
| } | |
| dir = d.parent(); | |
| } | |
| } | |
| } | |
| // Last resort: HOME/SPFsmartGATE | |
| if let Ok(home) = std::env::var("HOME") { | |
| return PathBuf::from(home).join("SPFsmartGATE"); | |
| } | |
| panic!("Cannot determine SPFsmartGATE root: SPF_ROOT not set, binary walk-up failed, HOME not set"); | |
| }) | |
| } | |
| /// Actual user home directory — parent of SPFsmartGATE root. | |
| /// | |
| /// Resolution order: | |
| /// 1. Parent directory of spf_root() | |
| /// 2. HOME environment variable | |
| /// 3. Panic | |
| pub fn actual_home() -> &'static Path { | |
| ACTUAL_HOME_CACHE.get_or_init(|| { | |
| if let Some(parent) = spf_root().parent() { | |
| return parent.to_path_buf(); | |
| } | |
| if let Ok(home) = std::env::var("HOME") { | |
| return PathBuf::from(home); | |
| } | |
| panic!("Cannot determine home directory: spf_root has no parent and HOME not set"); | |
| }) | |
| } | |
| /// System package manager path — platform-detected at compile time. | |
| /// Android/Termux: PREFIX env or /data/data/com.termux/files/usr | |
| /// Linux/macOS: /usr | |
| pub fn system_pkg_path() -> String { | |
| if cfg!(target_os = "android") { | |
| if let Ok(prefix) = std::env::var("PREFIX") { | |
| return prefix; | |
| } | |
| "/data/data/com.termux/files/usr".to_string() | |
| } else { | |
| "/usr".to_string() | |
| } | |
| } | |