Skip to content

Commit b1c8eff

Browse files
passcodFélix Saparelli
authored andcommitted
Replace Walker by WalkDir
Fixes #31 Might help with #17 and #21
1 parent b0a68e2 commit b1c8eff

3 files changed

Lines changed: 40 additions & 72 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ libc = "*"
3535
log = "*"
3636
time = "*"
3737
filetime = "*"
38-
walker = "^1.0.0"
38+
walkdir = "^0.1"
3939

4040
[target.x86_64-unknown-linux-gnu.dependencies]
4141
inotify = "^0.1"

src/inotify/mod.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
extern crate inotify as inotify_sys;
22
extern crate libc;
3-
extern crate walker;
3+
extern crate walkdir;
44

55
use mio::{self, EventLoop};
66
use self::inotify_sys::wrapper::{self, INotify, Watch};
7-
use self::walker::Walker;
7+
use self::walkdir::WalkDir;
88
use std::collections::HashMap;
99
use std::fs::metadata;
1010
use std::path::{Path, PathBuf};
@@ -83,29 +83,20 @@ impl mio::Handler for INotifyHandler {
8383

8484
impl INotifyHandler {
8585
fn add_watch_recursively(&mut self, path: PathBuf) -> Result<(), Error> {
86-
let is_dir = match metadata(path.as_ref() as &Path) {
87-
Ok(m) => m.is_dir(),
86+
match metadata(&path) {
8887
Err(e) => return Err(Error::Io(e)),
89-
};
90-
if is_dir {
91-
match Walker::new(path.as_ref()) {
92-
Ok(dir) => {
93-
for entry in dir {
94-
match entry {
95-
Ok(entry) => {
96-
let path = entry.path();
97-
try!(self.add_watch(path));
98-
},
99-
Err(e) => return Err(Error::Io(e)),
100-
}
101-
}
102-
self.add_watch(path)
103-
},
104-
Err(e) => Err(Error::Io(e))
88+
Ok(m) => match m.is_dir() {
89+
false => return self.add_watch(path),
90+
true => {}
10591
}
106-
} else {
107-
self.add_watch(path)
10892
}
93+
94+
for entry in WalkDir::new(path).follow_links(true)
95+
.into_iter().filter_map(|e| e.ok()) {
96+
try!(self.add_watch(entry.path().to_path_buf()));
97+
}
98+
99+
Ok(())
109100
}
110101

111102
fn add_watch(&mut self, path: PathBuf) -> Result<(), Error> {

src/poll.rs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::fs;
55
use std::thread;
66
use super::{Error, Event, op, Watcher};
77
use std::path::{Path, PathBuf};
8-
use self::walker::Walker;
8+
use self::walkdir::WalkDir;
99

1010
use filetime::FileTime;
1111

12-
extern crate walker;
12+
extern crate walkdir;
1313

1414
pub struct PollWatcher {
1515
tx: Sender<Event>,
@@ -90,54 +90,31 @@ impl PollWatcher {
9090
}
9191

9292
// TODO: more efficient implementation where the dir tree is cached?
93-
match Walker::new(watch) {
94-
Err(e) => {
95-
let _ = tx.send(Event {
96-
path: Some(watch.clone()),
97-
op: Err(Error::Io(e))
98-
});
99-
continue
100-
},
101-
Ok(iter) => {
102-
for entry in iter {
103-
match entry {
104-
Ok(entry) => {
105-
let path = entry.path();
106-
107-
match fs::metadata(&path) {
108-
Err(e) => {
109-
let _ = tx.send(Event {
110-
path: Some(path.clone()),
111-
op: Err(Error::Io(e))
112-
});
113-
continue
114-
},
115-
Ok(stat) => {
116-
let modified =
117-
FileTime::from_last_modification_time(&stat)
118-
.seconds();
119-
120-
match mtimes.insert(path.clone(), modified) {
121-
None => continue, // First run
122-
Some(old) => {
123-
if modified > old {
124-
let _ = tx.send(Event {
125-
path: Some(path.clone()),
126-
op: Ok(op::WRITE)
127-
});
128-
continue
129-
}
130-
}
131-
}
132-
}
93+
for entry in WalkDir::new(watch).follow_links(true)
94+
.into_iter().filter_map(|e| e.ok()) {
95+
let path = entry.path();
96+
97+
match fs::metadata(&path) {
98+
Err(e) => {
99+
let _ = tx.send(Event {
100+
path: Some(path.to_path_buf()),
101+
op: Err(Error::Io(e))
102+
});
103+
continue
104+
},
105+
Ok(stat) => {
106+
let modified = FileTime::from_last_modification_time(&stat).seconds();
107+
match mtimes.insert(path.clone().to_path_buf(), modified) {
108+
None => continue, // First run
109+
Some(old) => {
110+
if modified > old {
111+
let _ = tx.send(Event {
112+
path: Some(path.to_path_buf()),
113+
op: Ok(op::WRITE)
114+
});
115+
continue
133116
}
134-
},
135-
Err(e) => {
136-
let _ = tx.send(Event {
137-
path: Some(watch.clone()),
138-
op: Err(Error::Io(e))
139-
});
140-
},
117+
}
141118
}
142119
}
143120
}

0 commit comments

Comments
 (0)