use std::path::PathBuf;
pub mod cgfs;
pub mod effector;
pub mod history;
pub mod journal;
pub mod lock;
pub mod notify;
pub mod policy;
pub mod report;
pub mod resolve;
pub mod sampler;
pub mod service;
pub mod systemd;
pub mod types;
pub use effector::{Applied, Effector};
pub use journal::Journal;
pub use policy::PolicyEngine;
pub use sampler::Sampler;
pub use systemd::SystemdUser;
pub use types::{Action, Intervention, Level, ProcInfo, PsiSource, Sample, Target};
pub fn guard_file(name: &str) -> Option<PathBuf> {
guard_file_from(dirs::state_dir(), dirs::runtime_dir(), name)
}
fn guard_file_from(
state: Option<PathBuf>,
runtime: Option<PathBuf>,
name: &str,
) -> Option<PathBuf> {
state.or(runtime).map(|d| d.join("rlm").join(name))
}
pub fn try_journal_path() -> Option<PathBuf> {
guard_file("guard-journal.jsonl")
}
pub fn journal_path() -> PathBuf {
try_journal_path().unwrap_or_default()
}
pub fn lock_path() -> Option<PathBuf> {
guard_file("rlm-guard.lock")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn guard_files_prefer_the_state_dir() {
assert_eq!(
guard_file_from(Some("/s".into()), Some("/r".into()), "rlm-guard.lock"),
Some(PathBuf::from("/s/rlm/rlm-guard.lock"))
);
}
#[test]
fn guard_files_fall_back_to_the_runtime_dir_not_tmp() {
assert_eq!(
guard_file_from(None, Some("/run/user/1000".into()), "guard-journal.jsonl"),
Some(PathBuf::from("/run/user/1000/rlm/guard-journal.jsonl"))
);
assert_eq!(guard_file_from(None, None, "guard-journal.jsonl"), None);
}
#[test]
fn journal_reader_path_is_empty_without_a_dir() {
let p = try_journal_path().unwrap_or_default();
assert!(!p.starts_with("/tmp"), "{p:?}");
assert!(Journal::read_entries(&PathBuf::new(), "boot").is_empty());
}
}