-
Notifications
You must be signed in to change notification settings - Fork 203
/
kqueue.rs
398 lines (355 loc) 路 15.1 KB
/
kqueue.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! Watcher implementation for the kqueue API
//!
//! The kqueue() system call provides a generic method of notifying the user
//! when an event happens or a condition holds, based on the results of small
//! pieces of kernel code termed filters.
use super::event::*;
use super::{Error, EventHandler, RecursiveMode, Result, Watcher};
use crossbeam_channel::{unbounded, Sender};
use kqueue::{EventData, EventFilter, FilterFlag, Ident};
use std::collections::HashMap;
use std::env;
use std::fs::metadata;
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread;
use walkdir::WalkDir;
const KQUEUE: mio::Token = mio::Token(0);
const MESSAGE: mio::Token = mio::Token(1);
// The EventLoop will set up a mio::Poll and use it to wait for the following:
//
// - messages telling it what to do
//
// - events telling it that something has happened on one of the watched files.
struct EventLoop {
running: bool,
poll: mio::Poll,
event_loop_waker: Arc<mio::Waker>,
event_loop_tx: crossbeam_channel::Sender<EventLoopMsg>,
event_loop_rx: crossbeam_channel::Receiver<EventLoopMsg>,
kqueue: kqueue::Watcher,
event_handler: Box<dyn EventHandler>,
watches: HashMap<PathBuf, bool>,
}
/// Watcher implementation based on inotify
pub struct KqueueWatcher {
channel: crossbeam_channel::Sender<EventLoopMsg>,
waker: Arc<mio::Waker>,
}
enum EventLoopMsg {
AddWatch(PathBuf, RecursiveMode, Sender<Result<()>>),
RemoveWatch(PathBuf, Sender<Result<()>>),
Shutdown,
}
impl EventLoop {
pub fn new(kqueue: kqueue::Watcher, event_handler: Box<dyn EventHandler>) -> Result<Self> {
let (event_loop_tx, event_loop_rx) = crossbeam_channel::unbounded::<EventLoopMsg>();
let poll = mio::Poll::new()?;
let event_loop_waker = Arc::new(mio::Waker::new(poll.registry(), MESSAGE)?);
let kqueue_fd = kqueue.as_raw_fd();
let mut evented_kqueue = mio::unix::SourceFd(&kqueue_fd);
poll.registry()
.register(&mut evented_kqueue, KQUEUE, mio::Interest::READABLE)?;
let event_loop = EventLoop {
running: true,
poll,
event_loop_waker,
event_loop_tx,
event_loop_rx,
kqueue,
event_handler,
watches: HashMap::new(),
};
Ok(event_loop)
}
// Run the event loop.
pub fn run(self) {
thread::spawn(|| self.event_loop_thread());
}
fn event_loop_thread(mut self) {
let mut events = mio::Events::with_capacity(16);
loop {
// Wait for something to happen.
match self.poll.poll(&mut events, None) {
Err(ref e) if matches!(e.kind(), std::io::ErrorKind::Interrupted) => {
// System call was interrupted, we will retry
// TODO: Not covered by tests (to reproduce likely need to setup signal handlers)
}
Err(e) => panic!("poll failed: {}", e),
Ok(()) => {}
}
// Process whatever happened.
for event in &events {
self.handle_event(event);
}
// Stop, if we're done.
if !self.running {
break;
}
}
}
// Handle a single event.
fn handle_event(&mut self, event: &mio::event::Event) {
match event.token() {
MESSAGE => {
// The channel is readable - handle messages.
self.handle_messages()
}
KQUEUE => {
// inotify has something to tell us.
self.handle_kqueue()
}
_ => unreachable!(),
}
}
fn handle_messages(&mut self) {
while let Ok(msg) = self.event_loop_rx.try_recv() {
match msg {
EventLoopMsg::AddWatch(path, recursive_mode, tx) => {
let _ = tx.send(self.add_watch(path, recursive_mode.is_recursive()));
}
EventLoopMsg::RemoveWatch(path, tx) => {
let _ = tx.send(self.remove_watch(path, false));
}
EventLoopMsg::Shutdown => {
self.running = false;
break;
}
}
}
}
fn handle_kqueue(&mut self) {
let mut add_watches = Vec::new();
let mut remove_watches = Vec::new();
loop {
match self.kqueue.poll(None) {
Some(event) => {
dbg!(&event);
match event {
kqueue::Event {
data: EventData::Vnode(data),
ident: Ident::Filename(_, path),
} => {
let path = PathBuf::from(path);
let event = match data {
/*
TODO: Differenciate folders and files
kqueue dosen't tell us if this was a file or a dir, so we
could only emulate this inotify behavior if we keep track of
all files and directories internally and then perform a
lookup.
*/
kqueue::Vnode::Delete => {
remove_watches.push(path.clone());
Event::new(EventKind::Remove(RemoveKind::Any))
}
//data was written to this file
kqueue::Vnode::Write => Event::new(EventKind::Access(
AccessKind::Close(AccessMode::Write),
)),
/*
Extend and Truncate are just different names for the same
operation, extend is only used on FreeBSD, truncate everwhere
else
*/
kqueue::Vnode::Extend | kqueue::Vnode::Truncate => Event::new(
EventKind::Modify(ModifyKind::Data(DataChange::Size)),
),
/*
this kevent has the same problem as the delete kevent. The
only way i can think of providing "better" event with more
information is to do the diff our self, while this maybe do
able of delete. In this case it would somewhat expensive to
keep track and compare ever peace of metadata for every file
*/
kqueue::Vnode::Attrib => Event::new(EventKind::Modify(
ModifyKind::Metadata(MetadataKind::Any),
)),
/*
The link count on a file changed => subdirectory created or
delete.
*/
kqueue::Vnode::Link => {
// As we currently don't have a solution that whould allow us
// to only add/remove the new/delete directory and that dosn't include a
// possible race condition. On possible solution would be to
// create a `HashMap<PathBuf, Vec<PathBuf>>` which would
// include every directory and this content add the time of
// adding it to kqueue. While this sould allow us to do the
// diff and only add/remove the files nessesary. This whould
// also introduce a race condition, where multiple files could
// all ready be remove from the directory, and we could get out
// of sync.
// So for now, until we find a better solution, let remove and
// readd the whole directory.
// This is a expensive operation, as we recursive through all
// subdirectories.
remove_watches.push(path.clone());
add_watches.push(path.clone());
Event::new(EventKind::Modify(ModifyKind::Any))
}
// Kqueue not provide us with the infomation nessesary to provide
// the new file name to the event.
kqueue::Vnode::Rename => {
remove_watches.push(path.clone());
Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Any)))
}
// Access to the file was revoked via revoke(2) or the underlying file system was unmounted.
kqueue::Vnode::Revoke => {
remove_watches.push(path.clone());
Event::new(EventKind::Remove(RemoveKind::Any))
}
}
.add_path(path);
self.event_handler.handle_event(Ok(event));
}
// as we don't add any other EVFILTER to kqueue we should never get here
kqueue::Event { ident: _, data: _ } => unreachable!(),
}
}
None => break,
}
}
for path in remove_watches {
self.remove_watch(path, true).ok();
}
for path in add_watches {
self.add_watch(path, true).ok();
}
}
fn add_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result<()> {
// If the watch is not recursive, or if we determine (by stat'ing the path to get its
// metadata) that the watched path is not a directory, add a single path watch.
if !is_recursive || !metadata(&path).map_err(Error::io)?.is_dir() {
return self.add_single_watch(path, false);
}
for entry in WalkDir::new(path)
.follow_links(true)
.into_iter()
.filter_map(filter_dir)
{
self.add_single_watch(entry.path().to_path_buf(), is_recursive)?;
}
self.kqueue.watch()?;
Ok(())
}
fn add_single_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result<()> {
let event_filter = EventFilter::EVFILT_VNODE;
let filter_flags = FilterFlag::NOTE_DELETE
| FilterFlag::NOTE_WRITE
| FilterFlag::NOTE_EXTEND
| FilterFlag::NOTE_ATTRIB
| FilterFlag::NOTE_LINK
| FilterFlag::NOTE_RENAME
| FilterFlag::NOTE_REVOKE;
self.kqueue
.add_filename(&path, event_filter, filter_flags)
.map_err(|e| Error::io(e).add_path(path.clone()))?;
self.watches.insert(path, is_recursive);
self.kqueue.watch()?;
Ok(())
}
fn remove_watch(&mut self, path: PathBuf, remove_recursive: bool) -> Result<()> {
match self.watches.remove(&path) {
None => return Err(Error::watch_not_found()),
Some(is_recursive) => {
self.kqueue
.remove_filename(&path, EventFilter::EVFILT_VNODE)
.map_err(|e| Error::io(e).add_path(path.clone()))?;
if is_recursive || remove_recursive {
for entry in WalkDir::new(path)
.follow_links(true)
.into_iter()
.filter_map(filter_dir)
{
let p = entry.path().to_path_buf();
self.kqueue
.remove_filename(&p, EventFilter::EVFILT_VNODE)
.map_err(|e| Error::io(e).add_path(p))?;
}
}
self.kqueue.watch()?;
}
}
Ok(())
}
}
/// return `DirEntry` when it is a directory
fn filter_dir(e: walkdir::Result<walkdir::DirEntry>) -> Option<walkdir::DirEntry> {
if let Ok(e) = e {
if let Ok(metadata) = e.metadata() {
if metadata.is_dir() {
return Some(e);
}
}
}
None
}
impl KqueueWatcher {
fn from_event_handler(event_handler: Box<dyn EventHandler>) -> Result<Self> {
let kqueue = kqueue::Watcher::new()?;
let event_loop = EventLoop::new(kqueue, event_handler)?;
let channel = event_loop.event_loop_tx.clone();
let waker = event_loop.event_loop_waker.clone();
event_loop.run();
Ok(KqueueWatcher { channel, waker })
}
fn watch_inner(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> {
let pb = if path.is_absolute() {
path.to_owned()
} else {
let p = env::current_dir().map_err(Error::io)?;
p.join(path)
};
let (tx, rx) = unbounded();
let msg = EventLoopMsg::AddWatch(pb, recursive_mode, tx);
self.channel
.send(msg)
.map_err(|e| Error::generic(&e.to_string()))?;
self.waker
.wake()
.map_err(|e| Error::generic(&e.to_string()))?;
rx.recv()
.unwrap()
.map_err(|e| Error::generic(&e.to_string()))
}
fn unwatch_inner(&mut self, path: &Path) -> Result<()> {
let pb = if path.is_absolute() {
path.to_owned()
} else {
let p = env::current_dir().map_err(Error::io)?;
p.join(path)
};
let (tx, rx) = unbounded();
let msg = EventLoopMsg::RemoveWatch(pb, tx);
self.channel
.send(msg)
.map_err(|e| Error::generic(&e.to_string()))?;
self.waker
.wake()
.map_err(|e| Error::generic(&e.to_string()))?;
rx.recv()
.unwrap()
.map_err(|e| Error::generic(&e.to_string()))
}
}
impl Watcher for KqueueWatcher {
/// Create a new watcher.
fn new<F: EventHandler>(event_handler: F) -> Result<Self> {
Self::from_event_handler(Box::new(event_handler))
}
fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> {
self.watch_inner(path, recursive_mode)
}
fn unwatch(&mut self, path: &Path) -> Result<()> {
self.unwatch_inner(path)
}
}
impl Drop for KqueueWatcher {
fn drop(&mut self) {
// we expect the event loop to live => unwrap must not panic
self.channel.send(EventLoopMsg::Shutdown).unwrap();
self.waker.wake().unwrap();
}
}