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 4 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
42 changes: 32 additions & 10 deletions notify/src/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +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.

struct EventLoop {
running: bool,
poll: mio::Poll,
Expand All @@ -35,7 +36,9 @@ struct EventLoop {
event_loop_rx: Receiver<EventLoopMsg>,
inotify: Option<Inotify>,
event_handler: Box<dyn EventHandler>,
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool)>,
// PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir)
/// PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir)
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 +61,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 +80,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 +284,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 +297,26 @@ 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this now: When does this unwrap fail ? Do we need to clone here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

considering it's in else branch which checks for is_none condition, it shouldn't fail on unwrap.
The clone is there coz it needs to be owned by Event but path is in notify.

let current_watch = self.watches.get(&watched_path);
remove_kind = match current_watch {
Some(&(_, _, _, true)) => RemoveKind::Folder,
Some(&(_, _, _, false)) => RemoveKind::File,
None => 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)
}
if event.mask.contains(EventMask::MODIFY) {
evs.push(
Event::new(EventKind::Modify(ModifyKind::Data(
Expand Down Expand Up @@ -401,7 +422,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 +442,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 +457,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