11//! inotify support for working with inotifies
2+ //!
3+ //! # Examples
4+ //!
5+ //! ```
6+ //! use rustix::fs::inotify;
7+ //! use rustix::io;
8+ //! use std::mem::MaybeUninit;
9+ //!
10+ //! # fn test() -> io::Result<()> {
11+ //! // Creeate an inotify object. In this example, we use `NONBLOCK` so that
12+ //! // the reader fails with `WOULDBLOCk` when no events are ready. Otherwise
13+ //! // it will block until at least one event is ready.
14+ //! let inotify = inotify::init(inotify::CreateFlags::NONBLOCK)?;
15+ //!
16+ //! // Add a directory to watch.
17+ //! inotify::add_watch(
18+ //! &inotify,
19+ //! "/path/to/some/directory/to/watch",
20+ //! inotify::WatchFlags::ALL_EVENTS,
21+ //! )?;
22+ //!
23+ //! // Generate some events in the watched directory...
24+ //!
25+ //! // Loop over pending events.
26+ //! let mut buf = [MaybeUninit::uninit(); 512];
27+ //! let mut iter = inotify::Reader::new(inotify, &mut buf);
28+ //! loop {
29+ //! let entry = match iter.next() {
30+ //! // Stop iterating if there are no more events for now.
31+ //! Err(io::Errno::WOULDBLOCK) => break,
32+ //! Err(e) => return Err(e),
33+ //! Ok(entry) => entry,
34+ //! };
35+ //!
36+ //! // Use `entry`...
37+ //! }
38+ //!
39+ //! # Ok(())
40+ //! # }
241
342#![ allow( unused_qualifications) ]
443
@@ -130,7 +169,19 @@ impl<'a> InotifyEvent<'a> {
130169
131170impl < ' buf , Fd : AsFd > Reader < ' buf , Fd > {
132171 /// Read the next inotify event.
172+ ///
173+ /// This is similar to `[Iterator::next`] except that it doesn't return an
174+ /// `Option`, because the stream doesn't have an ending. It always returns
175+ /// events or errors.
176+ ///
177+ /// If there are no events in the buffer and none ready to be read:
178+ /// - If the file descriptor was opened with
179+ /// [`inotify::CreateFlags::NONBLOCK`], this will fail with
180+ /// [`Errno::AGAIN`].
181+ /// - Otherwise this will block until at least one event is ready or an
182+ /// error occurs.
133183 #[ allow( unsafe_code) ]
184+ #[ allow( clippy:: should_implement_trait) ]
134185 pub fn next ( & mut self ) -> io:: Result < InotifyEvent > {
135186 if self . is_buffer_empty ( ) {
136187 match read_uninit ( self . fd . as_fd ( ) , self . buf ) . map ( |( init, _) | init. len ( ) ) {
@@ -144,6 +195,7 @@ impl<'buf, Fd: AsFd> Reader<'buf, Fd> {
144195 }
145196
146197 let ptr = self . buf [ self . offset ..] . as_ptr ( ) ;
198+
147199 // SAFETY:
148200 // - This data is initialized by the check above.
149201 // - Assumption: the kernel will not give us partial structs.
0 commit comments