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

Fixed the Event of watch dir Deletion #540

Merged
merged 8 commits into from
Nov 1, 2023
Merged
Changes from 1 commit
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
43 changes: 33 additions & 10 deletions notify/src/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const MESSAGE: mio::Token = mio::Token(1);
// - messages telling it what to do
//
// - events telling it that something has happened on one of the watched files.
//
// - watches HashMap Value tuple (WatchDescriptor, WatchMask, is_recursive, is_dir)
struct EventLoop {
running: bool,
poll: mio::Poll,
Expand All @@ -35,7 +37,7 @@ struct EventLoop {
event_loop_rx: Receiver<EventLoopMsg>,
inotify: Option<Inotify>,
event_handler: Box<dyn EventHandler>,
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
zeroishero marked this conversation as resolved.
Show resolved Hide resolved
paths: HashMap<WatchDescriptor, PathBuf>,
rename_event: Option<Event>,
}
Expand All @@ -58,13 +60,13 @@ enum EventLoopMsg {
fn add_watch_by_event(
path: &Option<PathBuf>,
event: &inotify_sys::Event<&OsStr>,
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
add_watches: &mut Vec<PathBuf>,
) {
if let Some(ref path) = *path {
if event.mask.contains(EventMask::ISDIR) {
if let Some(parent_path) = path.parent() {
if let Some(&(_, _, is_recursive)) = watches.get(parent_path) {
if let Some(&(_, _, is_recursive, _)) = watches.get(parent_path) {
if is_recursive {
add_watches.push(path.to_owned());
}
Expand All @@ -77,7 +79,7 @@ fn add_watch_by_event(
#[inline]
fn remove_watch_by_event(
path: &Option<PathBuf>,
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
remove_watches: &mut Vec<PathBuf>,
) {
if let Some(ref path) = *path {
Expand Down Expand Up @@ -281,9 +283,7 @@ impl EventLoop {
);
add_watch_by_event(&path, &event, &self.watches, &mut add_watches);
}
if event.mask.contains(EventMask::DELETE_SELF)
|| event.mask.contains(EventMask::DELETE)
{
if event.mask.contains(EventMask::DELETE) {
evs.push(
Event::new(EventKind::Remove(
if event.mask.contains(EventMask::ISDIR) {
Expand All @@ -296,6 +296,28 @@ impl EventLoop {
);
remove_watch_by_event(&path, &self.watches, &mut remove_watches);
}
if event.mask.contains(EventMask::DELETE_SELF) {
evs.push(
Event::new(EventKind::Remove(match path.clone() {
zeroishero marked this conversation as resolved.
Show resolved Hide resolved
Some(path) => {
let current_watch = self.watches.get(&path);
match current_watch {
Some(&(_, _, _, is_dir)) => {
if is_dir {
zeroishero marked this conversation as resolved.
Show resolved Hide resolved
RemoveKind::Folder
} else {
RemoveKind::File
}
}
None => RemoveKind::Other,
}
}
None => RemoveKind::Other,
}))
.add_some_path(path.clone()),
);
remove_watch_by_event(&path, &self.watches, &mut remove_watches)
}
if event.mask.contains(EventMask::MODIFY) {
evs.push(
Event::new(EventKind::Modify(ModifyKind::Data(
Expand Down Expand Up @@ -401,7 +423,7 @@ impl EventLoop {
watchmask.insert(WatchMask::MOVE_SELF);
}

if let Some(&(_, old_watchmask, _)) = self.watches.get(&path) {
if let Some(&(_, old_watchmask, _, _)) = self.watches.get(&path) {
watchmask.insert(old_watchmask);
watchmask.insert(WatchMask::MASK_ADD);
}
Expand All @@ -421,8 +443,9 @@ impl EventLoop {
}
Ok(w) => {
watchmask.remove(WatchMask::MASK_ADD);
let is_dir = metadata(&path).map_err(Error::io)?.is_dir();
self.watches
.insert(path.clone(), (w.clone(), watchmask, is_recursive));
.insert(path.clone(), (w.clone(), watchmask, is_recursive, is_dir));
self.paths.insert(w, path);
Ok(())
}
Expand All @@ -435,7 +458,7 @@ impl EventLoop {
fn remove_watch(&mut self, path: PathBuf, remove_recursive: bool) -> Result<()> {
match self.watches.remove(&path) {
None => return Err(Error::watch_not_found().add_path(path)),
Some((w, _, is_recursive)) => {
Some((w, _, is_recursive, _)) => {
if let Some(ref mut inotify) = self.inotify {
log::trace!("removing inotify watch: {}", path.display());

Expand Down