1 //! inotify support for working with inotifies
2
3 use crate::backend::c;
4 use crate::backend::fs::syscalls;
5 use crate::fd::{BorrowedFd, OwnedFd};
6 use crate::io;
7 use bitflags::bitflags;
8
9 bitflags! {
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/*/bitflags/#externally-defined-flags>
22 const _ = !0;
23 }
24 }
25
26 bitflags! {
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/*/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]
inotify_init(flags: CreateFlags) -> io::Result<OwnedFd>89 pub 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` and
96 /// returns a watch descriptor corresponding to this watch.
97 ///
98 /// Note: Due to the existence of hardlinks, providing two different paths to
99 /// this method may result in it returning the same watch descriptor. An
100 /// application should keep track of this externally to avoid logic errors.
101 #[inline]
inotify_add_watch<P: crate::path::Arg>( inot: BorrowedFd<'_>, path: P, flags: WatchFlags, ) -> io::Result<i32>102 pub fn inotify_add_watch<P: crate::path::Arg>(
103 inot: BorrowedFd<'_>,
104 path: P,
105 flags: WatchFlags,
106 ) -> io::Result<i32> {
107 path.into_with_c_str(|path| syscalls::inotify_add_watch(inot, path, flags))
108 }
109
110 /// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify.
111 ///
112 /// The watch descriptor provided should have previously been returned by
113 /// [`inotify_add_watch`] and not previously have been removed.
114 #[doc(alias = "inotify_rm_watch")]
115 #[inline]
inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()>116 pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
117 syscalls::inotify_rm_watch(inot, wd)
118 }
119