-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of new PollWatcher compare_contents feature
The example shows how to use this to effectively watch pseudo filesystems like those through sysfs (i.e. /sys/). The example by default will only work on Linux but does demonstrate well that it works where the previous metadata only approach would not.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::path::Path; | ||
use std::time::Duration; | ||
use notify::poll::PollWatcherConfig; | ||
use notify::{PollWatcher, RecursiveMode, Watcher}; | ||
use glob::glob; | ||
|
||
fn main() -> notify::Result<()> { | ||
let mut paths: Vec<_> = std::env::args().skip(1) | ||
.map(|arg| Path::new(&arg).to_path_buf()) | ||
.collect(); | ||
if paths.is_empty() { | ||
let interfaces = glob("/sys/class/net/*/statistics") | ||
.map_err(|e| notify::Error::generic(&e.to_string()))? | ||
.filter_map(|result| result.ok()); | ||
paths.extend(interfaces); | ||
} | ||
|
||
if paths.is_empty() { | ||
eprintln!("Must provide path to watch, default system paths in /sys/class/net were not found (maybe you're not running on Linux?)"); | ||
std::process::exit(1); | ||
} | ||
|
||
println!("watching {:?}...", paths); | ||
|
||
let config = PollWatcherConfig { | ||
compare_contents: true, | ||
poll_interval: Duration::from_secs(2), | ||
}; | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut watcher = PollWatcher::with_config(tx, config)?; | ||
for path in paths { | ||
watcher.watch(&path, RecursiveMode::Recursive)?; | ||
} | ||
|
||
for res in rx { | ||
match res { | ||
Ok(event) => println!("changed: {:?}", event), | ||
Err(e) => println!("watch error: {:?}", e), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |