Skip to content

Commit

Permalink
Merge pull request #22 from Yuhta/inotify-single-file
Browse files Browse the repository at this point in the history
Single file support for inotify and poll
  • Loading branch information
passcod committed Jul 12, 2015
2 parents ac2cc46 + 11c8612 commit 2c80db4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = [
"Michael Maurizi <[email protected]>",
"Pierre Baillet <[email protected]>",
"ShuYu Wang <[email protected]>",
"Jimmy Lu <[email protected]>",
]

description = "Cross-platform filesystem notification library"
Expand Down Expand Up @@ -40,3 +41,7 @@ version = "^0.2.11"

[target.x86_64-apple-darwin.dependencies.fsevent-sys]
version = "^0.1"

[dev-dependencies]
tempfile = "^1.1.0"
tempdir = "^0.3.4"
40 changes: 20 additions & 20 deletions src/inotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,28 @@ impl Watcher for INotifyWatcher {
}

fn watch(&mut self, path: &Path) -> Result<(), Error> {
match Walker::new(path) {
Ok(d) => {
for dir in d {
match dir {
Ok(entry) => {
let path = entry.path();
let meta = match metadata(&path) {
Ok(m) => m,
Err(e) => return Err(Error::Io(e)),
};

if meta.is_dir() {
let is_dir = match metadata(&path) {
Ok(m) => m.is_dir(),
Err(e) => return Err(Error::Io(e)),
};
if is_dir {
match Walker::new(path) {
Ok(dir) => {
for entry in dir {
match entry {
Ok(entry) => {
let path = entry.path();
try!(self.add_watch(&path));
}
},
Err(e) => return Err(Error::Io(e)),
},
Err(e) => return Err(Error::Io(e)),
}
}
}

self.add_watch(path)
},
Err(e) => Err(Error::Io(e))
self.add_watch(path)
},
Err(e) => Err(Error::Io(e))
}
} else {
self.add_watch(&path)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl PollWatcher {
}
}
}

if !stat.is_dir() { continue }
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/notify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
extern crate notify;
extern crate tempdir;
extern crate tempfile;

use notify::*;
use std::io::Write;
use std::path::Path;
use std::thread;
use std::sync::mpsc::{channel, Sender, Receiver};
use tempdir::TempDir;
use tempfile::NamedTempFile;

fn validate_recv(rx: Receiver<Event>, evs: Vec<(&Path, Op)>) {
for expected in evs {
let actual = rx.recv().unwrap();
assert_eq!(actual.path.unwrap().as_path(), expected.0);
assert_eq!(actual.op.unwrap(), expected.1);
}
assert!(rx.try_recv().is_err());
}

fn validate_watch_single_file<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher
{
let mut file = NamedTempFile::new().unwrap();
let (tx, rx) = channel();
let mut w = ctor(tx).unwrap();
w.watch(file.path()).unwrap();
thread::sleep_ms(1000);
file.write_all(b"foo").unwrap();
file.flush().unwrap();
validate_recv(rx, vec![(file.path(), op::WRITE)]);
}

fn validate_watch_dir<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher
{
let dir = TempDir::new("dir").unwrap();
let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap();
let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap();
let dir11 = TempDir::new_in(dir1.path(), "dir11").unwrap();
let (tx, rx) = channel();
let mut w = ctor(tx).unwrap();
w.watch(dir.path()).unwrap();
let f111 = NamedTempFile::new_in(dir11.path()).unwrap();
let f111_path = f111.path().to_owned();
let f111_path = f111_path.as_path();
let f21 = NamedTempFile::new_in(dir2.path()).unwrap();
f111.close().unwrap();
validate_recv(rx, vec![(f111_path, op::CREATE),
(f21.path(), op::CREATE),
(f111_path, op::REMOVE)]);
}

#[test]
fn watch_single_file_recommended() {
validate_watch_single_file(RecommendedWatcher::new);
}

#[test]
fn watch_dir_recommended() {
validate_watch_dir(RecommendedWatcher::new);
}

#[test]
fn watch_single_file_poll() {
validate_watch_single_file(PollWatcher::new);
}

// FIXME
// #[test]
// fn watch_dir_poll() {
// validate_watch_dir(PollWatcher::new);
// }

0 comments on commit 2c80db4

Please sign in to comment.