use super::super::c;
use super::super::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_owned_fd};
use crate::fd::{BorrowedFd, OwnedFd};
use crate::io;
use bitflags::bitflags;
bitflags! {
pub struct CreateFlags: c::c_int {
const CLOEXEC = c::IN_CLOEXEC;
const NONBLOCK = c::IN_NONBLOCK;
}
}
bitflags! {
#[derive(Default)]
pub struct WatchFlags: u32 {
const ACCESS = c::IN_ACCESS;
const ATTRIB = c::IN_ATTRIB;
const CLOSE_NOWRITE = c::IN_CLOSE_NOWRITE;
const CLOSE_WRITE = c::IN_CLOSE_WRITE;
const CREATE = c::IN_CREATE;
const DELETE = c::IN_DELETE;
const DELETE_SELF = c::IN_DELETE_SELF;
const MODIFY = c::IN_MODIFY;
const MOVE_SELF = c::IN_MOVE_SELF;
const MOVED_FROM = c::IN_MOVED_FROM;
const MOVED_TO = c::IN_MOVED_TO;
const OPEN = c::IN_OPEN;
const CLOSE = c::IN_CLOSE;
const MOVE = c::IN_MOVE;
const ALL_EVENTS = c::IN_ALL_EVENTS;
const DONT_FOLLOW = c::IN_DONT_FOLLOW;
const EXCL_UNLINK = 1;
const MASK_ADD = 1;
const MASK_CREATE = 1;
const ONESHOT = c::IN_ONESHOT;
const ONLYDIR = c::IN_ONLYDIR;
}
}
#[doc(alias = "inotify_init1")]
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(c::inotify_init1(flags.bits())) }
}
pub fn inotify_add_watch<P: crate::path::Arg>(
inot: BorrowedFd<'_>,
path: P,
flags: WatchFlags,
) -> io::Result<i32> {
let path = path.as_cow_c_str().unwrap();
unsafe {
ret_c_int(c::inotify_add_watch(
borrowed_fd(inot),
c_str(&path),
flags.bits(),
))
}
}
#[doc(alias = "inotify_rm_watch")]
pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
#[cfg(target_os = "android")]
let wd = wd as u32;
unsafe { ret(c::inotify_rm_watch(borrowed_fd(inot), wd)) }
}