#![allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]
use crate::error::Error;
use crate::syscall;
use crate::types::{IN_NONBLOCK, RawFd, WatchMask};
use core::ffi::CStr;
use core::mem;
pub struct Inotify {
fd: RawFd,
}
impl Inotify {
pub fn new() -> Result<Self, Error> {
let fd = syscall::inotify_init1(IN_NONBLOCK)? as RawFd;
Ok(Self { fd })
}
#[inline]
#[must_use]
pub const fn fd(&self) -> RawFd {
self.fd
}
#[inline]
#[must_use]
pub const fn into_fd(self) -> RawFd {
let fd = self.fd;
mem::forget(self);
fd
}
pub fn add_watch(&self, path: &CStr, mask: WatchMask) -> Result<i32, Error> {
Ok(syscall::inotify_add_watch(self.fd as usize, path.as_ptr().cast(), mask.bits())? as i32)
}
pub fn remove_watch(&self, wd: i32) -> Result<(), Error> {
syscall::inotify_rm_watch(self.fd as usize, wd)
}
}
impl Drop for Inotify {
fn drop(&mut self) {
let _ = syscall::close(self.fd as usize);
}
}