(&mut self)
| 174 | #[allow(unsafe_code)] |
| 175 | #[allow(clippy::should_implement_trait)] |
| 176 | pub fn next(&mut self) -> io::Result<Event<'_>> { |
| 177 | if self.is_buffer_empty() { |
| 178 | match read(self.fd.as_fd(), &mut *self.buf).map(|(init, _)| init.len()) { |
| 179 | Ok(0) => return Err(Errno::INVAL), |
| 180 | Ok(bytes_read) => { |
| 181 | self.initialized = bytes_read; |
| 182 | self.offset = 0; |
| 183 | } |
| 184 | Err(e) => return Err(e), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | let ptr = self.buf[self.offset..].as_ptr(); |
| 189 | |
| 190 | // SAFETY: |
| 191 | // - This data is initialized by the check above. |
| 192 | // - Assumption: the kernel will not give us partial structs. |
| 193 | // - Assumption: the kernel uses proper alignment between structs. |
| 194 | // - The starting pointer is aligned (performed in `Reader::new`). |
| 195 | let event = unsafe { &*ptr.cast::<inotify_event>() }; |
| 196 | |
| 197 | self.offset += size_of::<inotify_event>() + usize::try_from(event.len).unwrap(); |
| 198 | |
| 199 | Ok(Event { |
| 200 | wd: event.wd, |
| 201 | events: ReadFlags::from_bits_retain(event.mask), |
| 202 | cookie: event.cookie, |
| 203 | file_name: if event.len > 0 { |
| 204 | // SAFETY: The kernel guarantees a NUL-terminated string. |
| 205 | Some(unsafe { CStr::from_ptr(event.name.as_ptr().cast()) }) |
| 206 | } else { |
| 207 | None |
| 208 | }, |
| 209 | }) |
| 210 | } |
| 211 | |
| 212 | /// Returns true if the internal buffer is empty and will be refilled when |
| 213 | /// calling [`next`]. This is useful to avoid further blocking reads. |
nothing calls this directly
no test coverage detected