diff --git a/CHANGELOG.md b/CHANGELOG.md index 802bae7..352ea8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + +## [0.3.1-rc2] - 2024-02-05 + +Rename `Fanotify` functions and stop eating registration errors. + +### Added +Added `FanotifyBuilder` to provide finer-grained control on what options are supplied to the libc call without directly using the low_level methods. + +### Changed +Rename `Fanotify::new_with_blocking` and `Fanotify::new_with_nonblocking` +The above functions now bubble up registration errors ## [0.3.1-rc1] - 2024-02-04 diff --git a/Cargo.toml b/Cargo.toml index c100b42..d78e167 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fanotify-rs" -version = "0.3.1-rc1" +version = "0.3.1-rc2" authors = ["zhanglei ", "n01e0 ", "Philip Woolford "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/src/high_level.rs b/src/high_level.rs index 6b81e3e..936fbf0 100644 --- a/src/high_level.rs +++ b/src/high_level.rs @@ -147,20 +147,19 @@ impl FanotifyMode { } impl Fanotify { - pub fn new_with_blocking(mode: FanotifyMode) -> Self { - Fanotify { - fd: fanotify_init(FAN_CLOEXEC | mode.to_fan_class(), (O_CLOEXEC | O_RDONLY) as u32).unwrap(), - } + pub fn new_blocking(mode: FanotifyMode) -> Result { + Ok(Fanotify { + fd: fanotify_init(FAN_CLOEXEC | mode.to_fan_class(), (O_CLOEXEC | O_RDONLY) as u32)?, + }) } - pub fn new_with_nonblocking(mode: FanotifyMode) -> Self { - Fanotify { + pub fn new_nonblocking(mode: FanotifyMode) -> Result { + Ok(Fanotify { fd: fanotify_init( FAN_CLOEXEC | FAN_NONBLOCK | mode.to_fan_class(), (O_CLOEXEC | O_RDONLY) as u32, - ) - .unwrap(), - } + )? + }) } pub fn add_path(&self, mode: u64, path: &P) -> Result<(), Error> { @@ -224,3 +223,37 @@ impl Fanotify { self.fd } } + +pub struct FanotifyBuilder { + class: FanotifyMode, + flags: u32, + event_flags: u32 +} + +impl FanotifyBuilder { + pub fn new() -> Self { + Self { + class: FanotifyMode::NOTIF, + flags: FAN_CLOEXEC, + event_flags: O_CLOEXEC as u32 + } + } + + pub fn with_class(self, class: FanotifyMode) -> Self { + Self { class, ..self } + } + + pub fn with_flags(self, flags: u32) -> Self { + Self { flags: FAN_CLOEXEC | flags, ..self } + } + + pub fn with_event_flags(self, event_flags: u32) -> Self { + Self { event_flags, ..self} + } + + pub fn register(&self) -> Result { + Ok(Fanotify { + fd: fanotify_init(self.flags | self.class.to_fan_class(), self.event_flags)?, + }) + } +} \ No newline at end of file diff --git a/tests/high_level.rs b/tests/high_level.rs index 2c38043..e1febe5 100644 --- a/tests/high_level.rs +++ b/tests/high_level.rs @@ -5,7 +5,7 @@ fn high_level_test() { FAN_OPEN, }; use std::io::{Read, Write}; - let ft = Fanotify::new_with_blocking(FanotifyMode::NOTIF); + let ft = Fanotify::new_blocking(FanotifyMode::NOTIF).expect("Error regitering fanotify listener"); ft.add_path( FAN_ACCESS | FAN_CLOSE | FAN_EVENT_ON_CHILD | FAN_MODIFY | FAN_ONDIR | FAN_OPEN, "/tmp",