Skip to content

Commit 4514dc5

Browse files
committed
Updated fanotify.rs to store file_handle separately
1 parent 665c6ae commit 4514dc5

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/sys/fanotify.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,45 @@ pub const FANOTIFY_METADATA_VERSION: u8 = libc::FANOTIFY_METADATA_VERSION;
240240

241241
/// Abstract over [`libc::fanotify_event_info_fid`], which represents an
242242
/// information record received via [`Fanotify::read_events_with_info_records`].
243-
// Is not Clone due to fd field, to avoid use-after-close scenarios.
244243
#[derive(Debug, Eq, Hash, PartialEq)]
245244
#[repr(transparent)]
246245
#[allow(missing_copy_implementations)]
247-
pub struct FanotifyFidRecord(libc::fanotify_event_info_fid);
246+
pub struct LibcFanotifyFidRecord(libc::fanotify_event_info_fid);
247+
248+
/// Extends LibcFanotifyFidRecord to include file_handle bytes.
249+
/// This allows Rust to move the record around in memory and not lose the file_handle
250+
/// as the libc::fanotify_event_info_fid does not include any of the file_handle bytes.
251+
// Is not Clone due to fd field, to avoid use-after-close scenarios.
252+
#[derive(Debug, Eq, Hash, PartialEq)]
253+
#[repr(C)]
254+
#[allow(missing_copy_implementations)]
255+
pub struct FanotifyFidRecord {
256+
record: LibcFanotifyFidRecord,
257+
handle_bytes: *const u8,
258+
}
248259

249260
impl FanotifyFidRecord {
250261
/// The filesystem id where this event occurred. The value this method returns
251262
/// differs depending on the host system. Please read the statfs(2) documentation
252263
/// for more information:
253264
/// <https://man7.org/linux/man-pages/man2/statfs.2.html#VERSIONS>
254265
pub fn filesystem_id(&self) -> libc::__kernel_fsid_t {
255-
self.0.fsid
266+
self.record.0.fsid
256267
}
257268

258269
/// The file handle for the filesystem object where the event occurred. The handle is
259270
/// represented as a 0-length u8 array, but it actually points to variable-length
260271
/// file_handle struct.For more information:
261272
/// <https://man7.org/linux/man-pages/man2/open_by_handle_at.2.html>
262-
pub fn handle(&self) -> [u8; 0] {
263-
self.0.handle
273+
pub fn handle(&self) -> *const u8 {
274+
self.handle_bytes
275+
}
276+
277+
/// The specific info_type for this Fid Record. Fanotify can return an Fid Record
278+
/// with many different possible info_types. The info_type is not always necessary
279+
/// but can be useful for connecting similar events together (like a FAN_RENAME)
280+
pub fn info_type(&self) -> u8 {
281+
self.record.0.hdr.info_type
264282
}
265283
}
266284

@@ -613,7 +631,18 @@ impl Fanotify {
613631
&buffer,
614632
current_event_offset,
615633
);
616-
Some(FanotifyInfoRecord::Fid(FanotifyFidRecord(record)))
634+
635+
let record_ptr: *const libc::fanotify_event_info_fid = unsafe {
636+
buffer.as_ptr().add(current_event_offset)
637+
as *const libc::fanotify_event_info_fid
638+
};
639+
640+
let file_handle_ptr = unsafe { record_ptr.add(1) as *const u8 };
641+
642+
Some(FanotifyInfoRecord::Fid(FanotifyFidRecord {
643+
record: LibcFanotifyFidRecord(record),
644+
handle_bytes: file_handle_ptr,
645+
}))
617646
}
618647
#[cfg(target_env = "gnu")]
619648
libc::FAN_EVENT_INFO_TYPE_ERROR => {

0 commit comments

Comments
 (0)