From 44feea8de90ffb440b88607e622e6793fe8e96e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Deharbe?= Date: Mon, 20 Nov 2023 11:01:46 +0100 Subject: [PATCH] Rename fanotify OFlags to EventFFlags --- src/fcntl.rs | 6 +++++- src/sys/fanotify.rs | 19 +++++++++++++++++-- test/sys/test_fanotify.rs | 8 +++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/fcntl.rs b/src/fcntl.rs index 7910cc9b7d..8f968fc2a9 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -64,7 +64,11 @@ libc_bitflags! { } } -#[cfg(any(feature = "fs", feature = "term"))] +#[cfg(any( + feature = "fs", + feature = "term", + all(feature = "fanotify", target_os = "linux") +))] libc_bitflags!( /// Configuration options for opened files. #[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "term"))))] diff --git a/src/sys/fanotify.rs b/src/sys/fanotify.rs index 7c95b6c872..4ff1cd7c7b 100644 --- a/src/sys/fanotify.rs +++ b/src/sys/fanotify.rs @@ -12,6 +12,7 @@ use crate::{NixPath, Result}; use crate::errno::Errno; +use crate::fcntl::OFlag; use crate::unistd::{read, write}; use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; use std::mem::{MaybeUninit, size_of}; @@ -102,7 +103,7 @@ libc_bitflags! { libc_bitflags! { /// File status flags for fanotify events file descriptors. - pub struct OFlags: libc::c_uint { + pub struct EventFFlags: libc::c_uint { /// Read only access. O_RDONLY as libc::c_uint; /// Write only access. @@ -126,6 +127,20 @@ libc_bitflags! { } } +impl TryFrom for EventFFlags { + type Error = Errno; + + fn try_from(o_flag: OFlag) -> Result { + EventFFlags::from_bits(o_flag.bits() as u32).ok_or(Errno::EINVAL) + } +} + +impl From for OFlag { + fn from(event_f_flags: EventFFlags) -> Self { + OFlag::from_bits_retain(event_f_flags.bits() as i32) + } +} + libc_bitflags! { /// Configuration options for [`fanotify_mark`](fn.fanotify_mark.html). pub struct MarkFlags: libc::c_uint { @@ -223,7 +238,7 @@ impl Fanotify { /// Returns a Result containing a Fanotify instance. /// /// For more information, see [fanotify_init(2)](https://man7.org/linux/man-pages/man7/fanotify_init.2.html). - pub fn init(flags: InitFlags, event_f_flags: OFlags) -> Result { + pub fn init(flags: InitFlags, event_f_flags: EventFFlags) -> Result { let res = Errno::result(unsafe { libc::fanotify_init(flags.bits(), event_f_flags.bits()) }); diff --git a/test/sys/test_fanotify.rs b/test/sys/test_fanotify.rs index b3d4ce9329..c9fe1519a6 100644 --- a/test/sys/test_fanotify.rs +++ b/test/sys/test_fanotify.rs @@ -1,6 +1,6 @@ use crate::*; use nix::sys::fanotify::{ - Fanotify, FanotifyResponse, InitFlags, MarkFlags, MaskFlags, OFlags, + EventFFlags, Fanotify, FanotifyResponse, InitFlags, MarkFlags, MaskFlags, Response, }; use std::fs::{read_link, File, OpenOptions}; @@ -20,7 +20,8 @@ pub fn test_fanotify() { fn test_fanotify_notifications() { let group = - Fanotify::init(InitFlags::FAN_CLASS_NOTIF, OFlags::O_RDONLY).unwrap(); + Fanotify::init(InitFlags::FAN_CLASS_NOTIF, EventFFlags::O_RDONLY) + .unwrap(); let tempdir = tempfile::tempdir().unwrap(); group @@ -88,7 +89,8 @@ fn test_fanotify_notifications() { fn test_fanotify_responses() { let group = - Fanotify::init(InitFlags::FAN_CLASS_CONTENT, OFlags::O_RDONLY).unwrap(); + Fanotify::init(InitFlags::FAN_CLASS_CONTENT, EventFFlags::O_RDONLY) + .unwrap(); let tempdir = tempfile::tempdir().unwrap(); group