use std::ffi::OsStr;
use std::path::Path;
pub const DAEMON_STEM: &str = "zccache-daemon";
pub fn invoked_as_daemon() -> bool {
std::env::args_os()
.next()
.is_some_and(|arg0| stem_matches(&arg0, DAEMON_STEM))
}
fn stem_matches(arg0: &OsStr, target: &str) -> bool {
let Some(stem) = Path::new(arg0).file_stem() else {
return false;
};
let Some(stem) = stem.to_str() else {
return false;
};
if cfg!(windows) {
stem.eq_ignore_ascii_case(target)
} else {
stem == target
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsString;
fn matches(s: &str) -> bool {
stem_matches(&OsString::from(s), DAEMON_STEM)
}
#[test]
fn bare_daemon_name_matches() {
assert!(matches("zccache-daemon"));
}
#[test]
fn full_path_matches() {
assert!(matches("/home/u/.zccache/v1.12.15/zccache-daemon"));
#[cfg(windows)]
assert!(matches(r"C:\Users\u\.zccache\v1.12.15\zccache-daemon.exe"));
}
#[test]
fn exe_suffix_stripped() {
assert!(matches("zccache-daemon.exe"));
}
#[test]
fn cli_name_does_not_match() {
assert!(!matches("zccache"));
assert!(!matches("zccache.exe"));
assert!(!matches("/usr/bin/zccache"));
}
#[test]
fn teardown_orphan_does_not_match() {
assert!(!matches("zccache-daemon.old.9271.exe"));
assert!(!matches("zccache-daemon.old.9271"));
}
#[test]
fn empty_or_unrelated_does_not_match() {
assert!(!matches(""));
assert!(!matches("some-other-tool"));
assert!(!matches("zccache-download-daemon"));
}
#[cfg(windows)]
#[test]
fn windows_is_case_insensitive() {
assert!(matches("ZCCACHE-DAEMON.EXE"));
assert!(matches("Zccache-Daemon"));
}
#[cfg(not(windows))]
#[test]
fn unix_is_case_sensitive() {
assert!(!matches("Zccache-Daemon"));
}
}