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
//! File watcher — monitors a file for changes via notify (inotify on Linux).
//!
//! notify::RecommendedWatcher runs callbacks on an internal thread.
//! FileWatcher bridges change notifications to the main thread via mpsc::channel.
use std::path::Path;
use std::sync::mpsc;
use anyhow::Result;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
pub struct FileWatcher {
rx: mpsc::Receiver<()>,
_watcher: RecommendedWatcher, // Drop stops watching
}
impl FileWatcher {
/// Create a FileWatcher that monitors the given file for changes.
///
/// Linux inotify loses the watch on rename (atomic save), so we watch
/// the parent directory (NonRecursive) and filter events by path.
pub fn new(path: &Path) -> Result<Self> {
let canonical = path.canonicalize()?;
let target = canonical.clone();
let (tx, rx) = mpsc::channel();
let mut watcher = RecommendedWatcher::new(
move |res: Result<notify::Event, notify::Error>| {
if let Ok(event) = res {
let dominated = event.paths.iter().any(|p| p == &target);
if dominated && event.kind.is_modify() {
let _ = tx.send(());
}
}
},
notify::Config::default(),
)?;
let parent = canonical
.parent()
.ok_or_else(|| anyhow::anyhow!("cannot watch root path"))?;
watcher.watch(parent, RecursiveMode::NonRecursive)?;
Ok(Self {
rx,
_watcher: watcher,
})
}
/// Return true if the file has changed since last check (non-blocking).
/// Multiple queued notifications are collapsed into a single true.
pub fn has_changed(&self) -> bool {
let mut changed = false;
while self.rx.try_recv().is_ok() {
changed = true;
}
changed
}
}