forked from notify-rs/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
263 lines (230 loc) · 8.44 KB
/
config.rs
File metadata and controls
263 lines (230 loc) · 8.44 KB
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
//! Configuration types
use notify_types::event::EventKindMask;
use std::{path::PathBuf, time::Duration};
/// Indicates whether only the provided directory or its sub-directories as well should be watched
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub enum RecursiveMode {
/// Watch all sub-directories as well, including directories created after installing the watch
Recursive,
/// Watch only the provided directory
NonRecursive,
}
impl RecursiveMode {
pub(crate) fn is_recursive(&self) -> bool {
match *self {
RecursiveMode::Recursive => true,
RecursiveMode::NonRecursive => false,
}
}
}
/// Watcher Backend configuration
///
/// This contains multiple settings that may relate to only one specific backend,
/// such as to correctly configure each backend regardless of what is selected during runtime.
///
/// ```rust
/// # use std::time::Duration;
/// # use notify::Config;
/// let config = Config::default()
/// .with_poll_interval(Duration::from_secs(2))
/// .with_compare_contents(true);
/// ```
///
/// Some options can be changed during runtime, others have to be set when creating the watcher backend.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Config {
/// See [Config::with_poll_interval]
poll_interval: Option<Duration>,
/// See [Config::with_compare_contents]
compare_contents: bool,
follow_symlinks: bool,
/// See [Config::with_event_kinds]
event_kinds: EventKindMask,
}
impl Config {
/// For the [`PollWatcher`](crate::PollWatcher) backend.
///
/// Interval between each re-scan attempt. This can be extremely expensive for large
/// file trees so it is recommended to measure and tune accordingly.
///
/// The default poll frequency is 30 seconds.
///
/// This will enable automatic polling, overwriting [`with_manual_polling()`](Config::with_manual_polling).
pub fn with_poll_interval(mut self, dur: Duration) -> Self {
// TODO: v7.0 break signature to option
self.poll_interval = Some(dur);
self
}
/// Returns current setting
pub fn poll_interval(&self) -> Option<Duration> {
// Changed Signature to Option
self.poll_interval
}
/// For the [`PollWatcher`](crate::PollWatcher) backend.
///
/// Disable automatic polling. Requires calling [`crate::PollWatcher::poll()`] manually.
///
/// This will disable automatic polling, overwriting [`with_poll_interval()`](Config::with_poll_interval).
pub fn with_manual_polling(mut self) -> Self {
self.poll_interval = None;
self
}
/// For the [`PollWatcher`](crate::PollWatcher) backend.
///
/// Optional feature that will evaluate the contents of changed files to determine if
/// they have indeed changed using a fast hashing algorithm. This is especially important
/// for pseudo filesystems like those on Linux under /sys and /proc which are not obligated
/// to respect any other filesystem norms such as modification timestamps, file sizes, etc.
/// By enabling this feature, performance will be significantly impacted as all files will
/// need to be read and hashed at each `poll_interval`.
///
/// This can't be changed during runtime. Off by default.
pub fn with_compare_contents(mut self, compare_contents: bool) -> Self {
self.compare_contents = compare_contents;
self
}
/// Returns current setting
pub fn compare_contents(&self) -> bool {
self.compare_contents
}
/// For the [INotifyWatcher](crate::INotifyWatcher), [KqueueWatcher](crate::KqueueWatcher),
/// and [PollWatcher](crate::PollWatcher).
///
/// Determine if symbolic links should be followed when recursively watching a directory.
///
/// This can't be changed during runtime. On by default.
pub fn with_follow_symlinks(mut self, follow_symlinks: bool) -> Self {
self.follow_symlinks = follow_symlinks;
self
}
/// Returns current setting
pub fn follow_symlinks(&self) -> bool {
self.follow_symlinks
}
/// Filter which event kinds are monitored.
///
/// This allows you to control which types of filesystem events are delivered
/// to your event handler. On backends that support kernel-level filtering
/// (inotify), the mask is translated to native flags for optimal
/// performance. On other backends (kqueue, Windows, FSEvents, PollWatcher),
/// filtering is applied in userspace.
///
/// The default is [`EventKindMask::ALL`], which includes all events.
/// Use [`EventKindMask::CORE`] to exclude access events.
///
/// This can't be changed during runtime.
///
/// # Example
///
/// ```rust
/// use notify::{Config, EventKindMask};
///
/// // Only monitor file creation and deletion
/// let config = Config::default()
/// .with_event_kinds(EventKindMask::CREATE | EventKindMask::REMOVE);
///
/// // Monitor everything including access events
/// let config_all = Config::default()
/// .with_event_kinds(EventKindMask::ALL);
/// ```
pub fn with_event_kinds(mut self, event_kinds: EventKindMask) -> Self {
self.event_kinds = event_kinds;
self
}
/// Returns current setting
pub fn event_kinds(&self) -> EventKindMask {
self.event_kinds
}
}
impl Default for Config {
fn default() -> Self {
Self {
poll_interval: Some(Duration::from_secs(30)),
compare_contents: false,
follow_symlinks: true,
event_kinds: EventKindMask::ALL,
}
}
}
/// Single watch backend configuration
///
/// This contains some settings that may relate to only one specific backend,
/// such as to correctly configure each backend regardless of what is selected during runtime.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WatchPathConfig {
recursive_mode: RecursiveMode,
}
impl WatchPathConfig {
/// Creates new instance with provided [`RecursiveMode`]
pub fn new(recursive_mode: RecursiveMode) -> Self {
Self { recursive_mode }
}
/// Set [`RecursiveMode`] for the watch
pub fn with_recursive_mode(mut self, recursive_mode: RecursiveMode) -> Self {
self.recursive_mode = recursive_mode;
self
}
/// Returns current setting
pub fn recursive_mode(&self) -> RecursiveMode {
self.recursive_mode
}
}
/// An operation to apply to a watcher
///
/// See [`Watcher::update_paths`] for more information
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathOp {
/// Path should be watched
Watch(PathBuf, WatchPathConfig),
/// Path should be unwatched
Unwatch(PathBuf),
}
impl PathOp {
/// Watch the path with [`RecursiveMode::Recursive`]
pub fn watch_recursive<P: Into<PathBuf>>(path: P) -> Self {
Self::Watch(path.into(), WatchPathConfig::new(RecursiveMode::Recursive))
}
/// Watch the path with [`RecursiveMode::NonRecursive`]
pub fn watch_non_recursive<P: Into<PathBuf>>(path: P) -> Self {
Self::Watch(
path.into(),
WatchPathConfig::new(RecursiveMode::NonRecursive),
)
}
/// Unwatch the path
pub fn unwatch<P: Into<PathBuf>>(path: P) -> Self {
Self::Unwatch(path.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use notify_types::event::EventKindMask;
#[test]
fn config_default_event_kinds_is_all() {
let config = Config::default();
assert_eq!(config.event_kinds(), EventKindMask::ALL);
}
#[test]
fn config_with_event_kinds() {
let mask = EventKindMask::CREATE | EventKindMask::REMOVE;
let config = Config::default().with_event_kinds(mask);
assert_eq!(config.event_kinds(), mask);
}
#[test]
fn config_with_all_events_includes_access() {
let config = Config::default().with_event_kinds(EventKindMask::ALL);
assert!(config.event_kinds().intersects(EventKindMask::ALL_ACCESS));
}
#[test]
fn config_with_empty_mask() {
let config = Config::default().with_event_kinds(EventKindMask::empty());
assert!(config.event_kinds().is_empty());
}
#[test]
fn event_kind_mask_default_matches_config_default() {
// Verify cross-crate consistency: both defaults should be ALL
assert_eq!(EventKindMask::default(), Config::default().event_kinds());
assert_eq!(EventKindMask::default(), EventKindMask::ALL);
}
}