nullnet_libconfmon/detector.rs
1use crate::Platform;
2use std::ffi::OsStr;
3use std::path::Path;
4use tokio::fs;
5use tokio::fs::ReadDir;
6
7/// Represents the possible states of the system configuration.
8#[derive(Debug, PartialEq)]
9pub enum State {
10 /// The configuration is in draft mode and has not been applied.
11 Draft,
12 /// The configuration has been applied to the system.
13 Applied,
14 /// The state of the configuration is undefined due to an error or missing information.
15 Undefined,
16}
17
18/// A detector responsible for determining whether the system configuration is in draft mode or applied.
19pub struct Detector {}
20
21impl Detector {
22 /// Checks the current configuration state based on the platform.
23 ///
24 /// # Parameters
25 /// - `platform`: The target platform for which the configuration state is being checked.
26 ///
27 /// # Returns
28 /// - `State::Draft` if the configuration is still in draft mode.
29 /// - `State::Applied` if the configuration has been applied.
30 /// - `State::Undefined` if the check fails or the state cannot be determined.
31 pub async fn check(platform: Platform) -> State {
32 match platform {
33 Platform::PfSense => Detector::check_pfsense().await,
34 Platform::OPNsense => todo!("Not implemented"),
35 }
36 }
37
38 /// Checks the configuration state for the **PfSense** platform.
39 ///
40 /// # Returns
41 /// - `State::Draft` if a file with a `.dirty` extension exists in `/var/run/`, indicating pending changes.
42 /// - `State::Applied` if no such files are found.
43 /// - `State::Undefined` if an error occurs while reading the directory.
44 async fn check_pfsense() -> State {
45 let mut entries: ReadDir = match fs::read_dir("/var/run/").await {
46 Ok(entries) => entries,
47 Err(_) => return State::Undefined,
48 };
49
50 while let Ok(Some(entry)) = entries.next_entry().await {
51 if let Some(ext) = Path::new(&entry.file_name())
52 .extension()
53 .and_then(OsStr::to_str)
54 {
55 if ext == "dirty" {
56 return State::Draft;
57 }
58 }
59 }
60
61 State::Applied
62 }
63}