1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
extern crate inotify as inotify_sys;
extern crate libc;
extern crate walker;
use self::inotify_sys::wrapper::{self, INotify, Watch};
use self::walker::Walker;
use std::collections::HashMap;
use std::fs::metadata;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};
use std::thread;
use super::{Error, Event, op, Op, Watcher};
mod flags;
pub struct INotifyWatcher {
inotify: INotify,
tx: Sender<Event>,
watches: HashMap<PathBuf, (Watch, flags::Mask)>,
paths: Arc<RwLock<HashMap<Watch, PathBuf>>>
}
impl INotifyWatcher {
fn run(&mut self) {
let mut ino = self.inotify.clone();
let tx = self.tx.clone();
let paths = self.paths.clone();
thread::spawn(move || {
loop {
match ino.wait_for_events() {
Ok(es) => {
for e in es.iter() {
handle_event(e.clone(), &tx, &paths)
}
},
Err(e) => {
match e.kind() {
_ => {
let _ = tx.send(Event {
path: None,
op: Err(Error::Io(e))
});
}
}
}
}
}
});
}
fn add_watch(&mut self, path: &Path) -> Result<(), Error> {
let mut watching = flags::IN_ATTRIB
| flags::IN_CREATE
| flags::IN_DELETE
| flags::IN_DELETE_SELF
| flags::IN_MODIFY
| flags::IN_MOVED_FROM
| flags::IN_MOVED_TO
| flags::IN_MOVE_SELF;
let path = path.to_path_buf();
match self.watches.get(&path) {
None => {},
Some(p) => {
watching.insert((&p.1).clone());
watching.insert(flags::IN_MASK_ADD);
}
}
match self.inotify.add_watch(&path, watching.bits()) {
Err(e) => return Err(Error::Io(e)),
Ok(w) => {
watching.remove(flags::IN_MASK_ADD);
self.watches.insert(path.clone(), (w.clone(), watching));
(*self.paths).write().unwrap().insert(w.clone(), path);
Ok(())
}
}
}
}
#[inline]
fn handle_event(event: wrapper::Event, tx: &Sender<Event>, paths: &Arc<RwLock<HashMap<Watch, PathBuf>>>) {
let mut o = Op::empty();
if event.is_create() || event.is_moved_to() {
o.insert(op::CREATE);
}
if event.is_delete_self() || event.is_delete() {
o.insert(op::WRITE);
}
if event.is_modify() {
o.insert(op::REMOVE);
}
if event.is_move_self() || event.is_moved_from() {
o.insert(op::RENAME);
}
if event.is_attrib() {
o.insert(op::CHMOD);
}
let path = match event.name.is_empty() {
true => {
match (*paths).read().unwrap().get(&event.wd) {
Some(p) => Some(p.clone()),
None => None
}
},
false => paths.read().unwrap().get(&event.wd).map(|root| root.join(&event.name)),
};
let _ = tx.send(Event {
path: path,
op: Ok(o)
});
}
impl Watcher for INotifyWatcher {
fn new(tx: Sender<Event>) -> Result<INotifyWatcher, Error> {
let mut it = match INotify::init() {
Ok(i) => INotifyWatcher {
inotify: i,
tx: tx,
watches: HashMap::new(),
paths: Arc::new(RwLock::new(HashMap::new()))
},
Err(e) => return Err(Error::Io(e))
};
it.run();
return Ok(it);
}
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() {
try!(self.add_watch(&path));
}
},
Err(e) => return Err(Error::Io(e)),
}
}
self.add_watch(path)
},
Err(e) => Err(Error::Io(e))
}
}
fn unwatch(&mut self, path: &Path) -> Result<(), Error> {
match self.watches.remove(&path.to_path_buf()) {
None => Err(Error::WatchNotFound),
Some(p) => {
let w = &p.0;
match self.inotify.rm_watch(w.clone()) {
Err(e) => Err(Error::Io(e)),
Ok(_) => {
(*self.paths).write().unwrap().remove(w);
Ok(())
}
}
}
}
}
}
impl Drop for INotifyWatcher {
fn drop(&mut self) {
for path in self.watches.clone().keys() {
let _ = self.unwatch(path);
}
let _ = self.inotify.close();
}
}