1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use filenamegen::Glob;
use once_cell::sync::Lazy;
use parking_lot::FairMutex as Mutex;
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::watch::{channel, Receiver, Sender};
use tokio::task::spawn_blocking;

static CONFIG: Lazy<Mutex<ConfigurationParams>> =
    Lazy::new(|| Mutex::new(ConfigurationParams::new()));

static EPOCH: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConfigEpoch(usize);

pub struct ConfigurationParams {
    pub globs: Arc<Vec<Glob>>,
    sender: Sender<ConfigEpoch>,
    receiver: Receiver<ConfigEpoch>,
}

impl ConfigurationParams {
    pub fn new() -> Self {
        let glob = Glob::new("/opt/kumomta/etc/**/*.{lua,json,toml,yaml}")
            .expect("failed to parse default glob");

        let (sender, receiver) = channel(ConfigEpoch(0));
        Self {
            globs: Arc::new(vec![glob]),
            sender,
            receiver,
        }
    }

    pub fn set_globs(&mut self, patterns: Vec<String>) -> anyhow::Result<()> {
        let mut globs = vec![];
        for p in patterns {
            globs.push(Glob::new(&p)?);
        }
        self.globs = Arc::new(globs);
        Ok(())
    }

    async fn eval_globs() -> anyhow::Result<BTreeSet<PathBuf>> {
        let globs = CONFIG.lock().globs.clone();
        spawn_blocking(move || {
            let mut paths: BTreeSet<PathBuf> = BTreeSet::new();
            for glob in globs.iter() {
                for path in glob.walk("/") {
                    let path = if !path.is_absolute() {
                        Path::new("/").join(path)
                    } else {
                        path
                    };
                    if path.is_file() {
                        paths.insert(path);
                    }
                }
            }
            Ok(paths)
        })
        .await?
    }

    pub fn subscribe(&self) -> Receiver<ConfigEpoch> {
        self.receiver.clone()
    }

    async fn config_epoch_task() -> anyhow::Result<()> {
        tracing::info!("config_epoch_task: starting");

        pub fn compute_hash(paths: BTreeSet<PathBuf>) -> String {
            if paths.is_empty() {
                tracing::warn!("config_epoch_task: glob evaluated to no paths");
                return "-no-files-".into();
            }

            let mut ctx = Sha256::new();
            for path in &paths {
                tracing::trace!("hashing {}", path.display());
                ctx.update(path.display().to_string());
                match std::fs::File::open(path) {
                    Ok(mut f) => {
                        let mut buf = [0u8; 8192];
                        while let Ok(n) = f.read(&mut buf) {
                            if n == 0 {
                                break;
                            }
                            ctx.update(&buf[0..n]);
                        }
                    }
                    Err(err) => {
                        tracing::error!("Error opening {}: {err:#}", path.display());
                    }
                }
            }
            let hash = ctx.finalize();
            let hex = data_encoding::HEXLOWER.encode(&hash);

            tracing::debug!("hashed {} files as {hex}", paths.len());

            hex
        }

        let mut current_hash = String::new();

        loop {
            match Self::eval_globs().await {
                Ok(paths) => match spawn_blocking(move || compute_hash(paths)).await {
                    Ok(hash) => {
                        if hash != current_hash {
                            tracing::info!("config_epoch_task: config change detected {hash:?}");
                            current_hash = hash.clone();

                            bump_current_epoch();
                        }
                    }
                    Err(err) => {
                        tracing::error!("config_epoch_task: error computing epoch: {err:#}");
                    }
                },
                Err(err) => {
                    tracing::error!("config_epoch_task: error computing hashes: {err:#}");
                }
            }

            tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
        }
    }
}

pub fn bump_current_epoch() {
    let epoch = 1 + EPOCH.fetch_add(1, Ordering::SeqCst);
    CONFIG.lock().sender.send(ConfigEpoch(epoch)).ok();
}

pub fn get_current_epoch() -> ConfigEpoch {
    ConfigEpoch(EPOCH.load(Ordering::SeqCst))
}

pub fn subscribe() -> Receiver<ConfigEpoch> {
    CONFIG.lock().subscribe()
}

pub fn set_globs(globs: Vec<String>) -> anyhow::Result<()> {
    CONFIG.lock().set_globs(globs)
}

pub async fn eval_globs() -> anyhow::Result<Vec<String>> {
    let mut result = vec![];
    let paths = ConfigurationParams::eval_globs().await?;
    for p in paths {
        result.push(
            p.to_str()
                .ok_or_else(|| anyhow::anyhow!("path {} cannot be converted to UTF8", p.display()))?
                .to_string(),
        );
    }
    Ok(result)
}

pub fn start_monitor() {
    tokio::spawn(async move {
        if let Err(err) = ConfigurationParams::config_epoch_task().await {
            tracing::error!("config_epoch_task: {err:#}");
        }
    });
}