use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use anyhow::{Context as _, Result, bail};
static PINNED: OnceLock<PathBuf> = OnceLock::new();
pub(super) fn pinned() -> Option<&'static Path> {
PINNED.get().map(PathBuf::as_path)
}
pub fn arg_form(path: &Path) -> String {
crate::context::path_string(path)
}
pub fn checked(word: &str, env: Option<&OsStr>) -> Result<PathBuf> {
let Some(dir) = openmoat_hosts::decode_home(word) else {
bail!("the hook's `--home {word}` is not a state directory OpenMoat writes");
};
let dir = PathBuf::from(dir);
if !dir.is_absolute() {
bail!(
"the hook's `--home {}` is not an absolute path",
dir.display()
);
}
if let Some(env) = env.filter(|v| !v.is_empty()) {
let named = Path::new(env);
if !same_dir(&dir, named) {
bail!(
"the hook names the state directory {} but its environment sets {}={}; the \
hook's command line decides, so the call is refused: remove {} from the \
agent's settings, or run `openmoat init` with the {} you mean",
dir.display(),
super::ENV,
named.display(),
super::ENV,
super::ENV,
);
}
}
Ok(dir)
}
pub fn pin(word: &str, env: Option<&OsStr>) -> Result<()> {
let dir = checked(word, env)?;
match PINNED.get() {
Some(already) if *already == dir => Ok(()),
Some(already) => bail!(
"the state directory is already {}, not {}",
already.display(),
dir.display()
),
None => PINNED.set(dir).ok().context("pinning the state directory"),
}
}
fn same_dir(a: &Path, b: &Path) -> bool {
if let (Ok(a), Ok(b)) = (a.canonicalize(), b.canonicalize()) {
return a == b;
}
let plain = |p: &Path| arg_form(p).trim_end_matches('/').to_owned();
plain(a) == plain(b)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_argument_decides_and_a_matching_or_absent_variable_is_fine() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("state dir");
std::fs::create_dir(&root).unwrap();
let word = openmoat_hosts::encode_home(&arg_form(&root));
assert_eq!(
checked(&word, None).unwrap(),
PathBuf::from(arg_form(&root))
);
assert!(checked(&word, Some(OsStr::new(""))).is_ok());
assert!(checked(&word, Some(root.as_os_str())).is_ok());
let slashed = format!("{}/", root.display());
assert!(checked(&word, Some(OsStr::new(&slashed))).is_ok());
}
#[test]
fn another_directory_in_the_variable_is_refused() {
let dir = tempfile::tempdir().unwrap();
let (real, other) = (dir.path().join("real"), dir.path().join("other"));
std::fs::create_dir(&real).unwrap();
std::fs::create_dir(&other).unwrap();
let word = openmoat_hosts::encode_home(&arg_form(&real));
let error = checked(&word, Some(other.as_os_str())).unwrap_err();
assert!(format!("{error:#}").contains("MOAT_HOME="), "{error:#}");
let missing = dir.path().join("missing");
assert!(checked(&word, Some(missing.as_os_str())).is_err());
}
#[test]
fn a_relative_or_malformed_argument_is_refused() {
for word in ["relative/.moat", "/a b", "/a+zz", ""] {
assert!(checked(word, None).is_err(), "{word}");
}
}
#[cfg(unix)]
#[test]
fn a_link_to_the_same_directory_is_the_same_directory() {
let dir = tempfile::tempdir().unwrap();
let real = dir.path().join("real");
std::fs::create_dir(&real).unwrap();
let link = dir.path().join("link");
std::os::unix::fs::symlink(&real, &link).unwrap();
let word = openmoat_hosts::encode_home(&arg_form(&real));
assert!(checked(&word, Some(link.as_os_str())).is_ok());
}
}