Cross-platform filesystem notification library for Rust.
(Looking for desktop notifications instead? Have a look at notify-rust or alert-after!)
- API Documentation
- Crate page
- Changelog
- Earliest supported Rust version: 1.26.1
As used by: alacritty, cargo watch, cobalt, docket, handlebars-iron, mdBook, pax, rdiff, rust-analyzer, timetrack, watchexec, xi-editor, and others. (Want to be added to this list? Open a pull request!)
[dependencies]
notify = "4.0.0"
extern crate notify;
use notify::{RecommendedWatcher, Watcher, RecursiveMode};
use std::sync::mpsc::channel;
use std::time::Duration;
fn watch() -> notify::Result<()> {
// Create a channel to receive the events.
let (tx, rx) = channel();
// Automatically select the best implementation for your platform.
// You can also access each implementation directly e.g. INotifyWatcher.
let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2)));
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
try!(watcher.watch("/home/test/notify", RecursiveMode::Recursive));
// This is a simple loop, but you may want to use more complex logic here,
// for example to handle I/O.
loop {
match rx.recv() {
Ok(event) => println!("{:?}", event),
Err(e) => println!("watch error: {:?}", e),
}
}
}
fn main() {
if let Err(e) = watch() {
println!("error: {:?}", e)
}
}
Looking overly verbose? Too much boilerplate? Have a look at hotwatch, a friendly wrapper:
// Taken from the hotwatch readme
use hotwatch::{Hotwatch, Event};
let mut hotwatch = Hotwatch::new().expect("Hotwatch failed to initialize.");
hotwatch.watch("war.png", |event: Event| {
if let Event::Write(path) = event {
println!("War has changed.");
}
}).expect("Failed to watch file!");
- Linux / Android: inotify
- macOS: FSEvents
- Windows: ReadDirectoryChangesW
- All platforms: polling
Due to the inner security model of FSEvents (see FileSystemEventSecurity), some event cannot be observed easily when trying to follow files that do not belong to you. In this case, reverting to the pollwatcher can fix the issue, with a slight performance cost.
While this current version continues to be developed and maintained, a next
generation design of the library lives in the
next
branch. There is no solid
ETA, beyond that most of it will not be released before async
/await
is
stabilised in Rust. For an overview and background, see this draft
announce.
Instead of one large release, though, it is much more likely that smaller
components of the design, once they have gone through revising and maturing in
the next
branch, will be incorporated in the main
branch. The first large
piece, a new event classification system, landed in 5.0.
As usual, pull requests for fixes and features are welcome!
Do be aware of the licensing difference. Notify is so far under CC0. The
next
branch is instead under the Artistic License 2.0. Pieces of
the next
branch brought to main
are dual-licensed under CC0, but the eventual
plan is to be entirely Artistic License 2.0 code. The formal license change
will incur a major version bump.
Inspired by Go's fsnotify and Node.js's Chokidar, born out of need for cargo watch, and general frustration at the non-existence of C/Rust cross-platform notify libraries.
Written by Félix Saparelli and awesome contributors, and released in the Public Domain using the Creative Commons Zero Declaration.