The Inotify API provides a Linux-only mechanism to monitor filesystem events. You can initialize an instance, add watches on specific paths, and read triggered events.
To use it:
- Initialize an
Inotify instance using Inotify::init(flags: InitFlags). - Add a watch to a path using
instance.add_watch(path, mask: AddWatchFlags), which returns a WatchDescriptor. - Retrieve events using
instance.read_events(), which returns a Vec<InotifyEvent>. - Remove a watch using
instance.rm_watch(wd: WatchDescriptor).
# use nix::sys::inotify::{AddWatchFlags,InitFlags,Inotify};
#
// We create a new inotify instance.
let instance = Inotify::init(InitFlags::empty()).unwrap();
// We add a new watch on directory "test" for all events.
let wd = instance.add_watch("test", AddWatchFlags::IN_ALL_EVENTS).unwrap();
loop {
// We read from our inotify instance for events.
let events = instance.read_events().unwrap();
println!("Events: {:?}", events);
}