]>
Commit | Line | Data |
---|---|---|
353b0b11 FG |
1 | //! inotify support for working with inotifies |
2 | ||
fe692bf9 | 3 | use crate::backend::c; |
353b0b11 FG |
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 | |
fe692bf9 FG |
13 | #[repr(transparent)] |
14 | #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] | |
353b0b11 FG |
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; | |
781aab86 FG |
20 | |
21 | /// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags> | |
22 | const _ = !0; | |
353b0b11 FG |
23 | } |
24 | } | |
25 | ||
26 | bitflags! { | |
27 | /// `IN*` for use with [`inotify_add_watch`]. | |
28 | /// | |
29 | /// [`inotify_add_watch`]: crate::fs::inotify::inotify_add_watch | |
fe692bf9 FG |
30 | #[repr(transparent)] |
31 | #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)] | |
353b0b11 FG |
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; | |
781aab86 FG |
77 | |
78 | /// <https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags> | |
79 | const _ = !0; | |
353b0b11 FG |
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")] | |
fe692bf9 | 88 | #[inline] |
353b0b11 FG |
89 | pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> { |
90 | syscalls::inotify_init1(flags) | |
91 | } | |
92 | ||
49aad941 | 93 | /// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify. |
353b0b11 FG |
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. | |
fe692bf9 | 102 | #[inline] |
353b0b11 FG |
103 | pub fn inotify_add_watch<P: crate::path::Arg>( |
104 | inot: BorrowedFd<'_>, | |
105 | path: P, | |
106 | flags: WatchFlags, | |
107 | ) -> io::Result<i32> { | |
781aab86 | 108 | path.into_with_c_str(|path| syscalls::inotify_add_watch(inot, path, flags)) |
353b0b11 FG |
109 | } |
110 | ||
111 | /// `inotify_rm_watch(self, wd)`—Removes a watch from this inotify | |
112 | /// | |
781aab86 FG |
113 | /// The watch descriptor provided should have previously been returned by |
114 | /// [`inotify_add_watch`] and not previously have been removed. | |
353b0b11 | 115 | #[doc(alias = "inotify_rm_watch")] |
fe692bf9 | 116 | #[inline] |
353b0b11 FG |
117 | pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> { |
118 | syscalls::inotify_rm_watch(inot, wd) | |
119 | } |