1//! inotify support for working with inotifies
2
3use crate::backend::c;
4use crate::backend::fs::syscalls;
5use crate::fd::{BorrowedFd, OwnedFd};
6use crate::io;
7use bitflags::bitflags;
8
9bitflags! {
10 /// `IN_*` for use with [`inotify_init`].
11 ///
12 /// [`inotify_init`]: crate::fs::inotify::inotify_init
13 #[repr(transparent)]
14 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
15 pub struct CreateFlags: c::c_uint {
16 /// `IN_CLOEXEC`
17 const CLOEXEC = linux_raw_sys::general::IN_CLOEXEC;
18 /// `IN_NONBLOCK`
19 const NONBLOCK = linux_raw_sys::general::IN_NONBLOCK;
20
21 /// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags>
22 const _ = !0;
23 }
24}
25
26bitflags! {
27 /// `IN*` for use with [`inotify_add_watch`].
28 ///
29 /// [`inotify_add_watch`]: crate::fs::inotify::inotify_add_watch
30 #[repr(transparent)]
31 #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
32 pub struct WatchFlags: c::c_uint {
33 /// `IN_ACCESS`
34 const ACCESS = linux_raw_sys::general::IN_ACCESS;
35 /// `IN_ATTRIB`
36 const ATTRIB = linux_raw_sys::general::IN_ATTRIB;
37 /// `IN_CLOSE_NOWRITE`
38 const CLOSE_NOWRITE = linux_raw_sys::general::IN_CLOSE_NOWRITE;
39 /// `IN_CLOSE_WRITE`
40 const CLOSE_WRITE = linux_raw_sys::general::IN_CLOSE_WRITE;
41 /// `IN_CREATE`
42 const CREATE = linux_raw_sys::general::IN_CREATE;
43 /// `IN_DELETE`
44 const DELETE = linux_raw_sys::general::IN_DELETE;
45 /// `IN_DELETE_SELF`
46 const DELETE_SELF = linux_raw_sys::general::IN_DELETE_SELF;
47 /// `IN_MODIFY`
48 const MODIFY = linux_raw_sys::general::IN_MODIFY;
49 /// `IN_MOVE_SELF`
50 const MOVE_SELF = linux_raw_sys::general::IN_MOVE_SELF;
51 /// `IN_MOVED_FROM`
52 const MOVED_FROM = linux_raw_sys::general::IN_MOVED_FROM;
53 /// `IN_MOVED_TO`
54 const MOVED_TO = linux_raw_sys::general::IN_MOVED_TO;
55 /// `IN_OPEN`
56 const OPEN = linux_raw_sys::general::IN_OPEN;
57
58 /// `IN_CLOSE`
59 const CLOSE = linux_raw_sys::general::IN_CLOSE;
60 /// `IN_MOVE`
61 const MOVE = linux_raw_sys::general::IN_MOVE;
62 /// `IN_ALL_EVENTS`
63 const ALL_EVENTS = linux_raw_sys::general::IN_ALL_EVENTS;
64
65 /// `IN_DONT_FOLLOW`
66 const DONT_FOLLOW = linux_raw_sys::general::IN_DONT_FOLLOW;
67 /// `IN_EXCL_UNLINK`
68 const EXCL_UNLINK = linux_raw_sys::general::IN_EXCL_UNLINK;
69 /// `IN_MASK_ADD`
70 const MASK_ADD = linux_raw_sys::general::IN_MASK_ADD;
71 /// `IN_MASK_CREATE`
72 const MASK_CREATE = linux_raw_sys::general::IN_MASK_CREATE;
73 /// `IN_ONESHOT`
74 const ONESHOT = linux_raw_sys::general::IN_ONESHOT;
75 /// `IN_ONLYDIR`
76 const ONLYDIR = linux_raw_sys::general::IN_ONLYDIR;
77
78 /// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags>
79 const _ = !0;
80 }
81}
82
83/// `inotify_init1(flags)`—Creates a new inotify object.
84///
85/// Use the [`CreateFlags::CLOEXEC`] flag to prevent the resulting file
86/// descriptor from being implicitly passed across `exec` boundaries.
87#[doc(alias = "inotify_init1")]
88#[inline]
89pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
90 syscalls::inotify_init1(flags)
91}
92
93/// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify.
94///
95/// This registers or updates a watch for the filesystem path `path`
96/// and returns a watch descriptor corresponding to this watch.
97///
98/// Note: Due to the existence of hardlinks, providing two
99/// different paths to this method may result in it returning
100/// the same watch descriptor. An application should keep track of this
101/// externally to avoid logic errors.
102#[inline]
103pub fn inotify_add_watch<P: crate::path::Arg>(
104 inot: BorrowedFd<'_>,
105 path: P,
106 flags: WatchFlags,
107) -> io::Result<i32> {
108 path.into_with_c_str(|path: &CStr| syscalls::inotify_add_watch(infd:inot, path, flags))
109}
110
111/// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify
112///
113/// The watch descriptor provided should have previously been returned by
114/// [`inotify_add_watch`] and not previously have been removed.
115#[doc(alias = "inotify_rm_watch")]
116#[inline]
117pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
118 syscalls::inotify_rm_watch(infd:inot, wfd:wd)
119}
120