Skip to content

Commit

Permalink
Recursive poll impl
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Dec 20, 2014
1 parent a14f028 commit b24a533
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "notify"
version = "0.0.1"
version = "1.0.0"
authors = ["Félix Saparelli <[email protected]>"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/inotify/flags.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

bitflags! {
#[deriving(Copy)] flags Mask: u32 {
flags Mask: u32 {
#[doc = " Event: File was accessed."]
#[doc = " "]
#[doc = " When monitoring a directory, the event may occur both for the"]
Expand Down
6 changes: 3 additions & 3 deletions src/inotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl INotifyWatcher {
let mut ino = self.inotify.clone();
let tx = self.tx.clone();
let paths = self.paths.clone();
spawn(proc() {
spawn(move || {
loop {
match ino.event() {
Ok(e) => {
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Watcher for INotifyWatcher {
match self.watches.get(path) {
None => {},
Some(p) => {
watching.insert(p.ref1().clone());
watching.insert((&p.1).clone());
watching.insert(flags::IN_MASK_ADD);
}
}
Expand All @@ -125,7 +125,7 @@ impl Watcher for INotifyWatcher {
match self.watches.remove(path) {
None => Err(Error::WatchNotFound),
Some(p) => {
let w = p.ref0();
let w = &p.0;
match self.inotify.rm_watch(w.clone()) {
Err(e) => Err(Error::Io(e)),
Ok(_) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod poll;

pub mod op {
bitflags! {
#[deriving(Copy)] flags Op: u32 {
flags Op: u32 {
const CHMOD = 0b00001,
const CREATE = 0b00010,
const REMOVE = 0b00100,
Expand Down Expand Up @@ -71,4 +71,4 @@ fn new_poll() {
Ok(_) => assert!(true),
Err(_) => assert!(false)
}
}
}
49 changes: 46 additions & 3 deletions src/poll.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::io::fs::PathExtensions;
use std::io::fs::{mod, PathExtensions};
use std::sync::{Arc, RWLock};
use super::{Error, Event, op, Op, Watcher};
use super::{Error, Event, op, Watcher};

pub struct PollWatcher {
tx: Sender<Event>,
Expand All @@ -14,7 +14,13 @@ impl PollWatcher {
let tx = self.tx.clone();
let watches = self.watches.clone();
let open = self.open.clone();
spawn(proc() {
spawn(move || {
// In order of priority:
// TODO: populate mtimes before loop, and then handle creation events
// TODO: handle deletion events
// TODO: handle chmod events
// TODO: handle renames
// TODO: DRY it up
let mut mtimes: HashMap<Path, u64> = HashMap::new();
loop {
if !(*open.read()) {
Expand Down Expand Up @@ -55,6 +61,43 @@ impl PollWatcher {
}

// TODO: recurse into the dir
// TODO: more efficient implementation where the dir tree is cached?
match fs::walk_dir(watch) {
Err(e) => {
tx.send(Event {
path: Some(watch.clone()),
op: Err(Error::Io(e))
});
continue
},
Ok(mut iter) => {
for path in iter {
match path.lstat() {
Err(e) => {
tx.send(Event {
path: Some(path.clone()),
op: Err(Error::Io(e))
});
continue
},
Ok(stat) => {
match mtimes.insert(path.clone(), stat.modified) {
None => continue, // First run
Some(old) => {
if stat.modified > old {
tx.send(Event {
path: Some(path.clone()),
op: Ok(op::WRITE)
});
continue
}
}
}
}
}
}
}
}
}
}
})
Expand Down

0 comments on commit b24a533

Please sign in to comment.