From bd740bb7e1073de0dac2f81c9797378f31569aad Mon Sep 17 00:00:00 2001 From: Julien Wajsberg Date: Fri, 3 Jun 2016 11:42:34 +0200 Subject: [PATCH] Fixes #49: update inotify to v0.2 --- Cargo.toml | 8 +++--- src/inotify/mod.rs | 68 +++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f22aeb2..56c9997 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,19 +38,19 @@ filetime = "^0.1.9" walkdir = "^0.1.5" [target.x86_64-unknown-linux-gnu.dependencies] -inotify = "^0.1.12" +inotify = "^0.2.3" mio = "^0.5.0" [target.i686-unknown-linux-gnu.dependencies] -inotify = "^0.1.12" +inotify = "^0.2.3" mio = "^0.5.0" [target.x86_64-unknown-linux-musl.dependencies] -inotify = "^0.1.12" +inotify = "^0.2.3" mio = "^0.5.0" [target.i686-unknown-linux-musl.dependencies] -inotify = "^0.1.12" +inotify = "^0.2.3" mio = "^0.5.0" [target.i686-apple-darwin.dependencies] diff --git a/src/inotify/mod.rs b/src/inotify/mod.rs index 6641bc5..5f71f83 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -20,7 +20,7 @@ const INOTIFY: mio::Token = mio::Token(0); pub struct INotifyWatcher(mio::Sender); struct INotifyHandler { - inotify: INotify, + inotify: Option, tx: Sender, watches: HashMap, paths: Arc>>, @@ -44,19 +44,21 @@ impl mio::Handler for INotifyHandler { INOTIFY => { assert!(events.is_readable()); - match self.inotify.available_events() { - Ok(events) => { - assert!(!events.is_empty()); + if let Some(ref mut inotify) = self.inotify { + match inotify.available_events() { + Ok(events) => { + assert!(!events.is_empty()); - for e in events { - handle_event(e.clone(), &self.tx, &self.paths) + for e in events { + handle_event(e.clone(), &self.tx, &self.paths) + } + } + Err(e) => { + let _ = self.tx.send(Event { + path: None, + op: Err(Error::Io(e)), + }); } - } - Err(e) => { - let _ = self.tx.send(Event { - path: None, - op: Err(Error::Io(e)), - }); } } } @@ -76,7 +78,9 @@ impl mio::Handler for INotifyHandler { for path in self.watches.clone().keys() { let _ = self.remove_watch(path.to_owned()); } - let _ = self.inotify.close(); + if let Some(inotify) = self.inotify.take() { + let _ = inotify.close(); + } event_loop.shutdown(); } @@ -115,14 +119,18 @@ impl INotifyHandler { watching.insert(flags::IN_MASK_ADD); } - match self.inotify.add_watch(&path, watching.bits()) { - Err(e) => Err(Error::Io(e)), - Ok(w) => { - watching.remove(flags::IN_MASK_ADD); - self.watches.insert(path.clone(), (w, watching)); - (*self.paths).write().unwrap().insert(w, path); - Ok(()) + if let Some(ref inotify) = self.inotify { + match inotify.add_watch(&path, watching.bits()) { + Err(e) => Err(Error::Io(e)), + Ok(w) => { + watching.remove(flags::IN_MASK_ADD); + self.watches.insert(path.clone(), (w, watching)); + (*self.paths).write().unwrap().insert(w, path); + Ok(()) + } } + } else { + Ok(()) } } @@ -131,14 +139,18 @@ impl INotifyHandler { None => Err(Error::WatchNotFound), Some(p) => { let w = p.0; - match self.inotify.rm_watch(w) { - Err(e) => Err(Error::Io(e)), - Ok(_) => { - // Nothing depends on the value being gone - // from here now that inotify isn't watching. - (*self.paths).write().unwrap().remove(&w); - Ok(()) + if let Some(ref inotify) = self.inotify { + match inotify.rm_watch(w) { + Err(e) => Err(Error::Io(e)), + Ok(_) => { + // Nothing depends on the value being gone + // from here now that inotify isn't watching. + (*self.paths).write().unwrap().remove(&w); + Ok(()) + } } + } else { + Ok(()) } } } @@ -190,7 +202,7 @@ impl Watcher for INotifyWatcher { let evented_inotify = mio::unix::EventedFd(&inotify_fd); let handler = INotifyHandler { - inotify: inotify, + inotify: Some(inotify), tx: tx, watches: HashMap::new(), paths: Arc::new(RwLock::new(HashMap::new())),