forked from notify-rs/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollwatcher_scan.rs
55 lines (45 loc) · 1.63 KB
/
pollwatcher_scan.rs
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
use notify::{poll::ScanEvent, Config, PollWatcher, RecursiveMode, Watcher};
use std::path::Path;
// Example for the pollwatcher scan callback feature.
// Returns the scanned paths
fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let path = std::env::args()
.nth(1)
.expect("Argument 1 needs to be a path");
log::info!("Watching {path}");
if let Err(error) = watch(path) {
log::error!("Error: {error:?}");
}
}
fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();
// if you want to use the same channel for both events
// and you need to differentiate between scan and file change events,
// then you will have to use something like this
enum Message {
Event(notify::Result<notify::Event>),
Scan(ScanEvent),
}
let tx_c = tx.clone();
// use the pollwatcher and set a callback for the scanning events
let mut watcher = PollWatcher::with_initial_scan(
move |watch_event| {
tx_c.send(Message::Event(watch_event)).unwrap();
},
Config::default(),
move |scan_event| {
tx.send(Message::Scan(scan_event)).unwrap();
},
)?;
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
for res in rx {
match res {
Message::Event(e) => println!("Watch event {e:?}"),
Message::Scan(e) => println!("Scan event {e:?}"),
}
}
Ok(())
}