Skip to content

Commit 0449e91

Browse files
authored
refactor(inotify): wrap watch tuple into Watch struct (#787)
Replace the internal (WatchDescriptor, WatchMask, is_recursive, is_dir) tuple with a named Watch struct to improve readability
1 parent 25a5095 commit 0449e91

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

notify/src/inotify.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ struct EventLoop {
8787
inotify: Option<Inotify>,
8888
event_handler: Box<dyn EventHandler>,
8989
/// PathBuf -> (WatchDescriptor, WatchMask, is_recursive, is_dir)
90-
watches: HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
90+
watches: HashMap<PathBuf, Watch>,
9191
paths: HashMap<WatchDescriptor, PathBuf>,
9292
rename_event: Option<Event>,
9393
follow_links: bool,
9494
event_kind_mask: EventKindMask,
9595
}
9696

97+
struct Watch {
98+
watch_descriptor: WatchDescriptor,
99+
watch_mask: WatchMask,
100+
is_recursive: bool,
101+
is_dir: bool,
102+
}
103+
97104
/// Watcher implementation based on inotify
98105
#[derive(Debug)]
99106
pub struct INotifyWatcher {
@@ -112,13 +119,13 @@ enum EventLoopMsg {
112119
fn add_watch_by_event(
113120
path: &PathBuf,
114121
event: &inotify_sys::Event<&OsStr>,
115-
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
122+
watches: &HashMap<PathBuf, Watch>,
116123
add_watches: &mut Vec<PathBuf>,
117124
) {
118125
if event.mask.contains(EventMask::ISDIR) {
119126
if let Some(parent_path) = path.parent() {
120-
if let Some(&(_, _, is_recursive, _)) = watches.get(parent_path) {
121-
if is_recursive {
127+
if let Some(watch) = watches.get(parent_path) {
128+
if watch.is_recursive {
122129
add_watches.push(path.to_owned());
123130
}
124131
}
@@ -129,7 +136,7 @@ fn add_watch_by_event(
129136
#[inline]
130137
fn remove_watch_by_event(
131138
path: &PathBuf,
132-
watches: &HashMap<PathBuf, (WatchDescriptor, WatchMask, bool, bool)>,
139+
watches: &HashMap<PathBuf, Watch>,
133140
remove_watches: &mut Vec<PathBuf>,
134141
) {
135142
if watches.contains_key(path) {
@@ -356,8 +363,8 @@ impl EventLoop {
356363
}
357364
if event.mask.contains(EventMask::DELETE_SELF) {
358365
let remove_kind = match self.watches.get(&path) {
359-
Some(&(_, _, _, true)) => RemoveKind::Folder,
360-
Some(&(_, _, _, false)) => RemoveKind::File,
366+
Some(watch) if watch.is_dir => RemoveKind::Folder,
367+
Some(_) => RemoveKind::File,
361368
None => RemoveKind::Other,
362369
};
363370
evs.push(
@@ -487,8 +494,8 @@ impl EventLoop {
487494
watchmask.insert(WatchMask::MOVE_SELF);
488495
}
489496

490-
if let Some(&(_, old_watchmask, _, _)) = self.watches.get(&path) {
491-
watchmask.insert(old_watchmask);
497+
if let Some(watch) = self.watches.get(&path) {
498+
watchmask.insert(watch.watch_mask);
492499
watchmask.insert(WatchMask::MASK_ADD);
493500
}
494501

@@ -510,8 +517,15 @@ impl EventLoop {
510517
Ok(w) => {
511518
watchmask.remove(WatchMask::MASK_ADD);
512519
let is_dir = metadata(&path).map_err(Error::io)?.is_dir();
513-
self.watches
514-
.insert(path.clone(), (w.clone(), watchmask, is_recursive, is_dir));
520+
self.watches.insert(
521+
path.clone(),
522+
Watch {
523+
watch_descriptor: w.clone(),
524+
watch_mask: watchmask,
525+
is_recursive,
526+
is_dir,
527+
},
528+
);
515529
self.paths.insert(w, path);
516530
Ok(())
517531
}
@@ -524,15 +538,18 @@ impl EventLoop {
524538
fn remove_watch(&mut self, path: PathBuf, remove_recursive: bool) -> Result<()> {
525539
match self.watches.remove(&path) {
526540
None => return Err(Error::watch_not_found().add_path(path)),
527-
Some((w, _, is_recursive, _)) => {
541+
Some(watch) => {
528542
if let Some(ref mut inotify) = self.inotify {
529543
let mut inotify_watches = inotify.watches();
530544
log::trace!("removing inotify watch for {path:?}, remove_recursive: {remove_recursive:?}");
531545

532-
Self::remove_single_descriptor(&mut inotify_watches, w.clone());
533-
self.paths.remove(&w);
546+
Self::remove_single_descriptor(
547+
&mut inotify_watches,
548+
watch.watch_descriptor.clone(),
549+
);
550+
self.paths.remove(&watch.watch_descriptor);
534551

535-
if is_recursive || remove_recursive {
552+
if watch.is_recursive || remove_recursive {
536553
let mut remove_list = Vec::new();
537554
for (w, p) in &self.paths {
538555
if p.starts_with(&path) {

0 commit comments

Comments
 (0)