1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
use std::{
hash::{
Hash,
Hasher,
},
os::raw::c_int,
sync::Weak,
};
use inotify_sys as ffi;
use fd_guard::FdGuard;
bitflags! {
/// Describes a file system watch
///
/// Passed to [`Inotify::add_watch`], to describe what file system events
/// to watch for, and how to do that.
///
/// # Examples
///
/// `WatchMask` constants can be passed to [`Inotify::add_watch`] as is. For
/// example, here's how to create a watch that triggers an event when a file
/// is accessed:
///
/// ``` rust
/// # use inotify::{
/// # Inotify,
/// # WatchMask,
/// # };
/// #
/// # let mut inotify = Inotify::init().unwrap();
/// #
/// # // Create a temporary file, so `add_watch` won't return an error.
/// # use std::fs::File;
/// # File::create("/tmp/inotify-rs-test-file")
/// # .expect("Failed to create test file");
/// #
/// inotify.add_watch("/tmp/inotify-rs-test-file", WatchMask::ACCESS)
/// .expect("Error adding watch");
/// ```
///
/// You can also combine multiple `WatchMask` constants. Here we add a watch
/// this is triggered both when files are created or deleted in a directory:
///
/// ``` rust
/// # use inotify::{
/// # Inotify,
/// # WatchMask,
/// # };
/// #
/// # let mut inotify = Inotify::init().unwrap();
/// inotify.add_watch("/tmp/", WatchMask::CREATE | WatchMask::DELETE)
/// .expect("Error adding watch");
/// ```
///
/// [`Inotify::add_watch`]: struct.Inotify.html#method.add_watch
pub struct WatchMask: u32 {
/// File was accessed
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_ACCESS`].
///
/// [`inotify_sys::IN_ACCESS`]: ../inotify_sys/constant.IN_ACCESS.html
const ACCESS = ffi::IN_ACCESS;
/// Metadata (permissions, timestamps, ...) changed
///
/// When watching a directory, this event can be triggered for the
/// directory itself, as well as objects inside the directory.
///
/// See [`inotify_sys::IN_ATTRIB`].
///
/// [`inotify_sys::IN_ATTRIB`]: ../inotify_sys/constant.IN_ATTRIB.html
const ATTRIB = ffi::IN_ATTRIB;
/// File opened for writing was closed
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_CLOSE_WRITE`].
///
/// [`inotify_sys::IN_CLOSE_WRITE`]: ../inotify_sys/constant.IN_CLOSE_WRITE.html
const CLOSE_WRITE = ffi::IN_CLOSE_WRITE;
/// File or directory not opened for writing was closed
///
/// When watching a directory, this event can be triggered for the
/// directory itself, as well as objects inside the directory.
///
/// See [`inotify_sys::IN_CLOSE_NOWRITE`].
///
/// [`inotify_sys::IN_CLOSE_NOWRITE`]: ../inotify_sys/constant.IN_CLOSE_NOWRITE.html
const CLOSE_NOWRITE = ffi::IN_CLOSE_NOWRITE;
/// File/directory created in watched directory
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_CREATE`].
///
/// [`inotify_sys::IN_CREATE`]: ../inotify_sys/constant.IN_CREATE.html
const CREATE = ffi::IN_CREATE;
/// File/directory deleted from watched directory
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_DELETE`].
///
/// [`inotify_sys::IN_DELETE`]: ../inotify_sys/constant.IN_DELETE.html
const DELETE = ffi::IN_DELETE;
/// Watched file/directory was deleted
///
/// See [`inotify_sys::IN_DELETE_SELF`].
///
/// [`inotify_sys::IN_DELETE_SELF`]: ../inotify_sys/constant.IN_DELETE_SELF.html
const DELETE_SELF = ffi::IN_DELETE_SELF;
/// File was modified
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_MODIFY`].
///
/// [`inotify_sys::IN_MODIFY`]: ../inotify_sys/constant.IN_MODIFY.html
const MODIFY = ffi::IN_MODIFY;
/// Watched file/directory was moved
///
/// See [`inotify_sys::IN_MOVE_SELF`].
///
/// [`inotify_sys::IN_MOVE_SELF`]: ../inotify_sys/constant.IN_MOVE_SELF.html
const MOVE_SELF = ffi::IN_MOVE_SELF;
/// File was renamed/moved; watched directory contained old name
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_MOVED_FROM`].
///
/// [`inotify_sys::IN_MOVED_FROM`]: ../inotify_sys/constant.IN_MOVED_FROM.html
const MOVED_FROM = ffi::IN_MOVED_FROM;
/// File was renamed/moved; watched directory contains new name
///
/// When watching a directory, this event is only triggered for objects
/// inside the directory, not the directory itself.
///
/// See [`inotify_sys::IN_MOVED_TO`].
///
/// [`inotify_sys::IN_MOVED_TO`]: ../inotify_sys/constant.IN_MOVED_TO.html
const MOVED_TO = ffi::IN_MOVED_TO;
/// File or directory was opened
///
/// When watching a directory, this event can be triggered for the
/// directory itself, as well as objects inside the directory.
///
/// See [`inotify_sys::IN_OPEN`].
///
/// [`inotify_sys::IN_OPEN`]: ../inotify_sys/constant.IN_OPEN.html
const OPEN = ffi::IN_OPEN;
/// Watch for all events
///
/// This constant is simply a convenient combination of the following
/// other constants:
///
/// - [`ACCESS`]
/// - [`ATTRIB`]
/// - [`CLOSE_WRITE`]
/// - [`CLOSE_NOWRITE`]
/// - [`CREATE`]
/// - [`DELETE`]
/// - [`DELETE_SELF`]
/// - [`MODIFY`]
/// - [`MOVE_SELF`]
/// - [`MOVED_FROM`]
/// - [`MOVED_TO`]
/// - [`OPEN`]
///
/// See [`inotify_sys::IN_ALL_EVENTS`].
///
/// [`ACCESS`]: #associatedconstant.ACCESS
/// [`ATTRIB`]: #associatedconstant.ATTRIB
/// [`CLOSE_WRITE`]: #associatedconstant.CLOSE_WRITE
/// [`CLOSE_NOWRITE`]: #associatedconstant.CLOSE_NOWRITE
/// [`CREATE`]: #associatedconstant.CREATE
/// [`DELETE`]: #associatedconstant.DELETE
/// [`DELETE_SELF`]: #associatedconstant.DELETE_SELF
/// [`MODIFY`]: #associatedconstant.MODIFY
/// [`MOVE_SELF`]: #associatedconstant.MOVE_SELF
/// [`MOVED_FROM`]: #associatedconstant.MOVED_FROM
/// [`MOVED_TO`]: #associatedconstant.MOVED_TO
/// [`OPEN`]: #associatedconstant.OPEN
/// [`inotify_sys::IN_ALL_EVENTS`]: ../inotify_sys/constant.IN_ALL_EVENTS.html
const ALL_EVENTS = ffi::IN_ALL_EVENTS;
/// Watch for all move events
///
/// This constant is simply a convenient combination of the following
/// other constants:
///
/// - [`MOVED_FROM`]
/// - [`MOVED_TO`]
///
/// See [`inotify_sys::IN_MOVE`].
///
/// [`MOVED_FROM`]: #associatedconstant.MOVED_FROM
/// [`MOVED_TO`]: #associatedconstant.MOVED_TO
/// [`inotify_sys::IN_MOVE`]: ../inotify_sys/constant.IN_MOVE.html
const MOVE = ffi::IN_MOVE;
/// Watch for all close events
///
/// This constant is simply a convenient combination of the following
/// other constants:
///
/// - [`CLOSE_WRITE`]
/// - [`CLOSE_NOWRITE`]
///
/// See [`inotify_sys::IN_CLOSE`].
///
/// [`CLOSE_WRITE`]: #associatedconstant.CLOSE_WRITE
/// [`CLOSE_NOWRITE`]: #associatedconstant.CLOSE_NOWRITE
/// [`inotify_sys::IN_CLOSE`]: ../inotify_sys/constant.IN_CLOSE.html
const CLOSE = ffi::IN_CLOSE;
/// Don't dereference the path if it is a symbolic link
///
/// See [`inotify_sys::IN_DONT_FOLLOW`].
///
/// [`inotify_sys::IN_DONT_FOLLOW`]: ../inotify_sys/constant.IN_DONT_FOLLOW.html
const DONT_FOLLOW = ffi::IN_DONT_FOLLOW;
/// Filter events for directory entries that have been unlinked
///
/// See [`inotify_sys::IN_EXCL_UNLINK`].
///
/// [`inotify_sys::IN_EXCL_UNLINK`]: ../inotify_sys/constant.IN_EXCL_UNLINK.html
const EXCL_UNLINK = ffi::IN_EXCL_UNLINK;
/// If a watch for the inode exists, amend it instead of replacing it
///
/// See [`inotify_sys::IN_MASK_ADD`].
///
/// [`inotify_sys::IN_MASK_ADD`]: ../inotify_sys/constant.IN_MASK_ADD.html
const MASK_ADD = ffi::IN_MASK_ADD;
/// Only receive one event, then remove the watch
///
/// See [`inotify_sys::IN_ONESHOT`].
///
/// [`inotify_sys::IN_ONESHOT`]: ../inotify_sys/constant.IN_ONESHOT.html
const ONESHOT = ffi::IN_ONESHOT;
/// Only watch path, if it is a directory
///
/// See [`inotify_sys::IN_ONLYDIR`].
///
/// [`inotify_sys::IN_ONLYDIR`]: ../inotify_sys/constant.IN_ONLYDIR.html
const ONLYDIR = ffi::IN_ONLYDIR;
}
}
/// Represents a watch on an inode
///
/// Can be obtained from [`Inotify::add_watch`] or from an [`Event`]. A watch
/// descriptor can be used to get inotify to stop watching an inode by passing
/// it to [`Inotify::rm_watch`].
///
/// [`Inotify::add_watch`]: struct.Inotify.html#method.add_watch
/// [`Inotify::rm_watch`]: struct.Inotify.html#method.rm_watch
/// [`Event`]: struct.Event.html
#[derive(Clone, Debug)]
pub struct WatchDescriptor{
pub(crate) id: c_int,
pub(crate) fd: Weak<FdGuard>,
}
impl Eq for WatchDescriptor {}
impl PartialEq for WatchDescriptor {
fn eq(&self, other: &Self) -> bool {
let self_fd = self.fd.upgrade();
let other_fd = other.fd.upgrade();
self.id == other.id && self_fd.is_some() && self_fd == other_fd
}
}
impl Hash for WatchDescriptor {
fn hash<H: Hasher>(&self, state: &mut H) {
// This function only takes `self.id` into account, as `self.fd` is a
// weak pointer that might no longer be available. Since neither
// panicking nor changing the hash depending on whether it's available
// is acceptable, we just don't look at it at all.
// I don't think that this influences storage in a `HashMap` or
// `HashSet` negatively, as storing `WatchDescriptor`s from different
// `Inotify` instances seems like something of an anti-pattern anyway.
self.id.hash(state);
}
}