|
5 | 5 | //! pieces of kernel code termed filters. |
6 | 6 |
|
7 | 7 | use super::event::*; |
8 | | -use super::{Config, Error, EventHandler, RecursiveMode, Result, Watcher}; |
| 8 | +use super::{Config, Error, EventHandler, RecursiveMode, Result, Watcher, WatchFilter}; |
9 | 9 | use crate::{unbounded, Receiver, Sender}; |
10 | 10 | use kqueue::{EventData, EventFilter, FilterFlag, Ident}; |
11 | 11 | use std::collections::HashMap; |
@@ -45,7 +45,7 @@ pub struct KqueueWatcher { |
45 | 45 | } |
46 | 46 |
|
47 | 47 | enum EventLoopMsg { |
48 | | - AddWatch(PathBuf, RecursiveMode, Sender<Result<()>>), |
| 48 | + AddWatch(PathBuf, RecursiveMode, WatchFilter, Sender<Result<()>>), |
49 | 49 | RemoveWatch(PathBuf, Sender<Result<()>>), |
50 | 50 | Shutdown, |
51 | 51 | } |
@@ -130,8 +130,9 @@ impl EventLoop { |
130 | 130 | fn handle_messages(&mut self) { |
131 | 131 | while let Ok(msg) = self.event_loop_rx.try_recv() { |
132 | 132 | match msg { |
133 | | - EventLoopMsg::AddWatch(path, recursive_mode, tx) => { |
134 | | - let _ = tx.send(self.add_watch(path, recursive_mode.is_recursive())); |
| 133 | + EventLoopMsg::AddWatch(path, recursive_mode, watch_filter, tx) => { |
| 134 | + let _ = |
| 135 | + tx.send(self.add_watch(path, recursive_mode.is_recursive(), watch_filter)); |
135 | 136 | } |
136 | 137 | EventLoopMsg::RemoveWatch(path, tx) => { |
137 | 138 | let _ = tx.send(self.remove_watch(path, false)); |
@@ -288,11 +289,20 @@ impl EventLoop { |
288 | 289 | } |
289 | 290 |
|
290 | 291 | for path in add_watches { |
291 | | - self.add_watch(path, true).ok(); |
| 292 | + self.add_watch(path, true, WatchFilter::accept_all()).ok(); |
292 | 293 | } |
293 | 294 | } |
294 | 295 |
|
295 | | - fn add_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result<()> { |
| 296 | + fn add_watch( |
| 297 | + &mut self, |
| 298 | + path: PathBuf, |
| 299 | + is_recursive: bool, |
| 300 | + watch_filter: WatchFilter, |
| 301 | + ) -> Result<()> { |
| 302 | + if !watch_filter.should_watch(&path) { |
| 303 | + return Ok(()); |
| 304 | + } |
| 305 | + |
296 | 306 | // If the watch is not recursive, or if we determine (by stat'ing the path to get its |
297 | 307 | // metadata) that the watched path is not a directory, add a single path watch. |
298 | 308 | if !is_recursive || !metadata(&path).map_err(Error::io)?.is_dir() { |
@@ -387,15 +397,20 @@ impl KqueueWatcher { |
387 | 397 | Ok(KqueueWatcher { channel, waker }) |
388 | 398 | } |
389 | 399 |
|
390 | | - fn watch_inner(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> { |
| 400 | + fn watch_inner( |
| 401 | + &mut self, |
| 402 | + path: &Path, |
| 403 | + recursive_mode: RecursiveMode, |
| 404 | + watch_filter: WatchFilter, |
| 405 | + ) -> Result<()> { |
391 | 406 | let pb = if path.is_absolute() { |
392 | 407 | path.to_owned() |
393 | 408 | } else { |
394 | 409 | let p = env::current_dir().map_err(Error::io)?; |
395 | 410 | p.join(path) |
396 | 411 | }; |
397 | 412 | let (tx, rx) = unbounded(); |
398 | | - let msg = EventLoopMsg::AddWatch(pb, recursive_mode, tx); |
| 413 | + let msg = EventLoopMsg::AddWatch(pb, recursive_mode, watch_filter, tx); |
399 | 414 |
|
400 | 415 | self.channel |
401 | 416 | .send(msg) |
@@ -436,8 +451,17 @@ impl Watcher for KqueueWatcher { |
436 | 451 | Self::from_event_handler(Box::new(event_handler), config.follow_symlinks()) |
437 | 452 | } |
438 | 453 |
|
| 454 | + fn watch_filtered( |
| 455 | + &mut self, |
| 456 | + path: &Path, |
| 457 | + recursive_mode: RecursiveMode, |
| 458 | + watch_filter: WatchFilter, |
| 459 | + ) -> Result<()> { |
| 460 | + self.watch_inner(path, recursive_mode, watch_filter) |
| 461 | + } |
| 462 | + |
439 | 463 | fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> { |
440 | | - self.watch_inner(path, recursive_mode) |
| 464 | + self.watch_inner(path, recursive_mode, WatchFilter::accept_all()) |
441 | 465 | } |
442 | 466 |
|
443 | 467 | fn unwatch(&mut self, path: &Path) -> Result<()> { |
|
0 commit comments