From f8b529c5e177dea5e498cbc1f17b4432b323876c Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Sat, 7 Oct 2023 23:26:45 +0545 Subject: [PATCH 1/8] bugfix:493_watched_dir_status_File --- notify/src/inotify.rs | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index ccf8187f..ddbdf6ee 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -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, @@ -35,7 +37,7 @@ struct EventLoop { event_loop_rx: Receiver, inotify: Option, event_handler: Box, - watches: HashMap, + watches: HashMap, paths: HashMap, rename_event: Option, } @@ -58,13 +60,13 @@ enum EventLoopMsg { fn add_watch_by_event( path: &Option, event: &inotify_sys::Event<&OsStr>, - watches: &HashMap, + watches: &HashMap, add_watches: &mut Vec, ) { 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()); } @@ -77,7 +79,7 @@ fn add_watch_by_event( #[inline] fn remove_watch_by_event( path: &Option, - watches: &HashMap, + watches: &HashMap, remove_watches: &mut Vec, ) { if let Some(ref path) = *path { @@ -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) { @@ -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() { + Some(path) => { + let current_watch = self.watches.get(&path); + match current_watch { + Some(&(_, _, _, is_dir)) => { + if is_dir { + 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( @@ -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); } @@ -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(()) } @@ -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()); From 7b365c32ad4cb8dabfd1ad80c57ecd119b8e9e45 Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Sun, 8 Oct 2023 00:04:41 +0545 Subject: [PATCH 2/8] refactoring --- notify/src/inotify.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index ddbdf6ee..66c0a57f 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -297,24 +297,22 @@ 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() { - Some(path) => { - let current_watch = self.watches.get(&path); - match current_watch { - Some(&(_, _, _, is_dir)) => { - if is_dir { - RemoveKind::Folder - } else { - RemoveKind::File - } - } - None => RemoveKind::Other, - } - } + let remove_kind: RemoveKind; + + if path.is_none() { + remove_kind = RemoveKind::Other + } else { + let watched_path = path.clone().unwrap(); + let current_watch = self.watches.get(&watched_path); + remove_kind = match current_watch { + Some(&(_, _, _, true)) => RemoveKind::Folder, + Some(&(_, _, _, false)) => RemoveKind::File, None => RemoveKind::Other, - })) - .add_some_path(path.clone()), + } + } + evs.push( + Event::new(EventKind::Remove(remove_kind)) + .add_some_path(path.clone()), ); remove_watch_by_event(&path, &self.watches, &mut remove_watches) } From 31e2a704dbbee2c0a04525cb3e5b404ba64fac54 Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Sun, 8 Oct 2023 00:07:08 +0545 Subject: [PATCH 3/8] refactors --- notify/src/inotify.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index 66c0a57f..9ee26964 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -27,8 +27,7 @@ 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, @@ -37,6 +36,7 @@ struct EventLoop { event_loop_rx: Receiver, inotify: Option, event_handler: Box, + // PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir) watches: HashMap, paths: HashMap, rename_event: Option, From 6e4b4aaeaf73d4f1a788666d603a8707f073d60f Mon Sep 17 00:00:00 2001 From: Bijay Shrestha <32957118+zeroishero@users.noreply.github.com> Date: Sun, 8 Oct 2023 00:13:16 +0545 Subject: [PATCH 4/8] Update notify/src/inotify.rs Co-authored-by: Aron --- notify/src/inotify.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index 9ee26964..9ec8000c 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -37,6 +37,7 @@ struct EventLoop { inotify: Option, event_handler: Box, // PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir) + /// PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir) watches: HashMap, paths: HashMap, rename_event: Option, From 59e7d894fbf3e89eb9369fc94b3c1326038b6942 Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Sat, 21 Oct 2023 05:33:35 +0545 Subject: [PATCH 5/8] refactor: Removed unwrap call --- notify/src/inotify.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index 9ec8000c..7d87c903 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -298,19 +298,17 @@ impl EventLoop { remove_watch_by_event(&path, &self.watches, &mut remove_watches); } if event.mask.contains(EventMask::DELETE_SELF) { - let remove_kind: RemoveKind; - - if path.is_none() { - remove_kind = RemoveKind::Other - } else { - let watched_path = path.clone().unwrap(); - let current_watch = self.watches.get(&watched_path); - remove_kind = match current_watch { - Some(&(_, _, _, true)) => RemoveKind::Folder, - Some(&(_, _, _, false)) => RemoveKind::File, - None => RemoveKind::Other, + let remove_kind = match path.clone() { + Some(watched_path) => { + let current_watch = self.watches.get(&watched_path); + match current_watch { + Some(&(_, _, _, true)) => RemoveKind::Folder, + Some(&(_, _, _, false)) => RemoveKind::File, + None => RemoveKind::Other, + } } - } + None => RemoveKind::Other, + }; evs.push( Event::new(EventKind::Remove(remove_kind)) .add_some_path(path.clone()), From 47e6aa052e1f92912d7cbd6afd9430b3e2a3a739 Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Sun, 22 Oct 2023 16:03:19 +0545 Subject: [PATCH 6/8] Removed the extra comment before doc comment. --- notify/src/inotify.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index 7d87c903..fbdf9cc8 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -36,7 +36,6 @@ struct EventLoop { event_loop_rx: Receiver, inotify: Option, event_handler: Box, - // PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir) /// PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir) watches: HashMap, paths: HashMap, From 0f1fc87101e7b7fdc4576a00da75e263decf4a66 Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Wed, 1 Nov 2023 01:20:07 +0100 Subject: [PATCH 7/8] remove unnecessary clone --- notify/src/inotify.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index fbdf9cc8..b1ef7945 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -297,9 +297,9 @@ impl EventLoop { remove_watch_by_event(&path, &self.watches, &mut remove_watches); } if event.mask.contains(EventMask::DELETE_SELF) { - let remove_kind = match path.clone() { + let remove_kind = match &path { Some(watched_path) => { - let current_watch = self.watches.get(&watched_path); + let current_watch = self.watches.get(watched_path); match current_watch { Some(&(_, _, _, true)) => RemoveKind::Folder, Some(&(_, _, _, false)) => RemoveKind::File, From 53b1a0079272923d8414cc9409297878642a7eab Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Wed, 1 Nov 2023 01:26:00 +0100 Subject: [PATCH 8/8] warn on missing path for DELETE_SELF --- notify/src/inotify.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/notify/src/inotify.rs b/notify/src/inotify.rs index b1ef7945..d8d81401 100644 --- a/notify/src/inotify.rs +++ b/notify/src/inotify.rs @@ -306,13 +306,16 @@ impl EventLoop { None => RemoveKind::Other, } } - None => RemoveKind::Other, + None => { + log::trace!("No patch for DELETE_SELF event, may be a bug?"); + RemoveKind::Other + }, }; evs.push( Event::new(EventKind::Remove(remove_kind)) .add_some_path(path.clone()), ); - remove_watch_by_event(&path, &self.watches, &mut remove_watches) + remove_watch_by_event(&path, &self.watches, &mut remove_watches); } if event.mask.contains(EventMask::MODIFY) { evs.push(