-
Notifications
You must be signed in to change notification settings - Fork 67
/
linux-inotify.rs
64 lines (55 loc) · 1.77 KB
/
linux-inotify.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
56
57
58
59
60
61
62
63
64
//! Uses the `inotify` crate to watch for changes in the current directory.
//!
//! Run with:
//!
//! ```
//! cargo run --example linux-inotify
//! ```
#[cfg(target_os = "linux")]
fn main() -> std::io::Result<()> {
use std::ffi::OsString;
use std::io;
use async_io::Async;
use futures_lite::future;
use inotify::{EventMask, Inotify, WatchMask};
type Event = (OsString, EventMask);
/// Reads some events without blocking.
///
/// If there are no events, an [`io::ErrorKind::WouldBlock`] error is returned.
fn read_op(inotify: &mut Inotify) -> io::Result<Vec<Event>> {
let mut buffer = [0; 1024];
let events = inotify
.read_events(&mut buffer)?
.filter_map(|ev| ev.name.map(|name| (name.to_owned(), ev.mask)))
.collect::<Vec<_>>();
if events.is_empty() {
Err(io::ErrorKind::WouldBlock.into())
} else {
Ok(events)
}
}
future::block_on(async {
// Watch events in the current directory.
let mut inotify = Async::new(Inotify::init()?)?;
// SAFETY: We do not move the inner file descriptor out.
unsafe {
inotify
.get_mut()
.watches()
.add(".", WatchMask::ALL_EVENTS)?;
}
println!("Watching for filesystem events in the current directory...");
println!("Try opening a file to trigger some events.");
println!();
// Wait for events in a loop and print them on the screen.
loop {
for event in unsafe { inotify.read_with_mut(read_op).await? } {
println!("{:?}", event);
}
}
})
}
#[cfg(not(target_os = "linux"))]
fn main() {
println!("This example works only on Linux!");
}