Skip to content

Commit

Permalink
Rename Fanotify creation functions, and bubble up registration erro…
Browse files Browse the repository at this point in the history
…rs. Introduce a builder pattern.
  • Loading branch information
pantsman0 committed Feb 5, 2024
1 parent a0e8006 commit 8445564
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fanotify-rs"
version = "0.3.1-rc1"
version = "0.3.1-rc2"
authors = ["zhanglei <[email protected]>", "n01e0 <[email protected]>", "Philip Woolford <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
51 changes: 42 additions & 9 deletions src/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
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<Self, Error> {
Ok(Fanotify {
fd: fanotify_init(
FAN_CLOEXEC | FAN_NONBLOCK | mode.to_fan_class(),
(O_CLOEXEC | O_RDONLY) as u32,
)
.unwrap(),
}
)?
})
}

pub fn add_path<P: ?Sized + FanotifyPath>(&self, mode: u64, path: &P) -> Result<(), Error> {
Expand Down Expand Up @@ -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<Fanotify, Error> {
Ok(Fanotify {
fd: fanotify_init(self.flags | self.class.to_fan_class(), self.event_flags)?,
})
}
}
2 changes: 1 addition & 1 deletion tests/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8445564

Please sign in to comment.