use anyhow::{Context, Result, bail};
use std::{fs, io::ErrorKind, path::Path};
pub const MARKER: &str = "recovery-required.json";
const OFFLINE: &str = "offline recovery directory: activating saved node or encryption state is unsupported; use a fresh directory and authenticated enrollment";
pub fn ensure_active(home: &Path) -> Result<()> {
let absolute = std::path::absolute(home)?;
check_ancestors(&absolute)?;
for ancestor in absolute.ancestors() {
match fs::canonicalize(ancestor) {
Ok(resolved) => return check_ancestors(&resolved),
Err(error) if error.kind() == ErrorKind::NotFound => {}
Err(error) => return Err(error).context("cannot check recovery directory"),
}
}
bail!("cannot resolve recovery directory boundary")
}
fn check_ancestors(home: &Path) -> Result<()> {
for ancestor in home.ancestors() {
match fs::symlink_metadata(ancestor.join(MARKER)) {
Ok(_) => bail!(OFFLINE),
Err(error) if error.kind() == ErrorKind::NotFound => {}
Err(error) => return Err(error).context("cannot check recovery guard"),
}
}
Ok(())
}