Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kqueue: Only make a single syscall to add all the watches #454

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notify/src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ fn test_fsevent_watcher_drop() {
let (tx, rx) = std::sync::mpsc::channel();

{
let mut watcher = FsEventWatcher::new(tx).unwrap();
let mut watcher = FsEventWatcher::new(tx, Default::default()).unwrap();
watcher.watch(dir.path(), RecursiveMode::Recursive).unwrap();
thread::sleep(Duration::from_millis(2000));
println!("is running -> {}", watcher.is_running());
Expand Down
19 changes: 12 additions & 7 deletions notify/src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! pieces of kernel code termed filters.

use super::event::*;
use super::{Error, EventHandler, RecursiveMode, Result, Watcher, Config};
use super::{Config, Error, EventHandler, RecursiveMode, Result, Watcher};
use crate::{unbounded, Receiver, Sender};
use kqueue::{EventData, EventFilter, FilterFlag, Ident};
use std::collections::HashMap;
Expand Down Expand Up @@ -288,18 +288,23 @@ impl EventLoop {
// If the watch is not recursive, or if we determine (by stat'ing the path to get its
// metadata) that the watched path is not a directory, add a single path watch.
if !is_recursive || !metadata(&path).map_err(Error::io)?.is_dir() {
return self.add_single_watch(path, false);
self.add_single_watch(path, false)?;
} else {
for entry in WalkDir::new(path).follow_links(true).into_iter() {
let entry = entry.map_err(map_walkdir_error)?;
self.add_single_watch(entry.path().to_path_buf(), is_recursive)?;
}
}

for entry in WalkDir::new(path).follow_links(true).into_iter() {
let entry = entry.map_err(map_walkdir_error)?;
self.add_single_watch(entry.path().to_path_buf(), is_recursive)?;
}
// Only make a single `kevent` syscall to add all the watches.
self.kqueue.watch()?;

Ok(())
}

/// Adds a single watch to the kqueue.
///
/// The caller of this function must call `self.kqueue.watch()` afterwards to register the new watch.
fn add_single_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result<()> {
let event_filter = EventFilter::EVFILT_VNODE;
let filter_flags = FilterFlag::NOTE_DELETE
Expand All @@ -314,7 +319,7 @@ impl EventLoop {
.add_filename(&path, event_filter, filter_flags)
.map_err(|e| Error::io(e).add_path(path.clone()))?;
self.watches.insert(path, is_recursive);
self.kqueue.watch()?;
alexkirsz marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

Expand Down