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

fix: PollWatcher panic after delete-and-recreate #406

Merged
merged 1 commit into from May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/poll.rs
Expand Up @@ -194,17 +194,28 @@ impl PollWatcher {
continue;
}
Ok(metadata) => {
// this is a file type watching point
if !metadata.is_dir() {
let path_data = PathData::collect(
watch,
&metadata,
build_hasher.as_ref(),
current_time,
);

// Update `path_data` for this watching point. In file
// type watching point, only has single one path need
// to update.
match paths.insert(watch.clone(), path_data.clone()) {
// `old_path_data` not exists, this is a new path
None => {
unreachable!();
let kind = EventKind::Create(CreateKind::Any);
let ev = Event::new(kind).add_path(watch.clone());
event_handler(Ok(ev));
}

// path already exists, need further check to see
// what's the difference.
Some(old_path_data) => {
if let Some(kind) =
path_data.detect_change(&old_path_data)
Expand All @@ -215,6 +226,8 @@ impl PollWatcher {
}
}
}

// this is a dir type watching point
} else {
let depth =
if is_recursive { usize::max_value() } else { 1 };
Expand All @@ -225,6 +238,8 @@ impl PollWatcher {
.filter_map(|e| e.ok())
{
let path = entry.path();

// TODO: duplicate logic, considering refactor following lines to a function.
match entry.metadata() {
Err(e) => {
let err = Error::io(e.into())
Expand Down Expand Up @@ -268,7 +283,9 @@ impl PollWatcher {
}
}

// clear out all paths which not updated by this round.
for (_, &mut WatchData { ref mut paths, .. }) in watches.iter_mut() {
// find which paths should be removed in this watching point.
let mut removed = Vec::new();
for (path, &PathData { last_check, .. }) in paths.iter() {
if last_check < current_time {
Expand All @@ -278,6 +295,8 @@ impl PollWatcher {
removed.push(path.clone());
}
}

// remove actually.
for path in removed {
(*paths).remove(&path);
}
Expand Down