MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / next

Method next

src/fs/inotify.rs:176–210  ·  view source on GitHub ↗
(&mut self)

Source from the content-addressed store, hash-verified

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.

Callers

nothing calls this directly

Calls 5

readFunction · 0.50
is_buffer_emptyMethod · 0.45
as_fdMethod · 0.45
lenMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected