1use crate::{unbounded, Config, Error, EventHandler, Receiver, RecursiveMode, Sender, Watcher};
7use std::{
8 collections::HashMap,
9 path::{Path, PathBuf},
10 sync::{
11 atomic::{AtomicBool, Ordering},
12 Arc, Mutex,
13 },
14 thread,
15 time::Duration,
16};
17
18pub type ScanEvent = crate::Result<PathBuf>;
20
21pub trait ScanEventHandler: Send + 'static {
26 fn handle_event(&mut self, event: ScanEvent);
28}
29
30impl<F> ScanEventHandler for F
31where
32 F: FnMut(ScanEvent) + Send + 'static,
33{
34 fn handle_event(&mut self, event: ScanEvent) {
35 (self)(event);
36 }
37}
38
39#[cfg(feature = "crossbeam-channel")]
40impl ScanEventHandler for crossbeam_channel::Sender<ScanEvent> {
41 fn handle_event(&mut self, event: ScanEvent) {
42 let _ = self.send(event);
43 }
44}
45
46#[cfg(feature = "flume")]
47impl ScanEventHandler for flume::Sender<ScanEvent> {
48 fn handle_event(&mut self, event: ScanEvent) {
49 let _ = self.send(event);
50 }
51}
52
53impl ScanEventHandler for std::sync::mpsc::Sender<ScanEvent> {
54 fn handle_event(&mut self, event: ScanEvent) {
55 let _ = self.send(event);
56 }
57}
58
59impl ScanEventHandler for () {
60 fn handle_event(&mut self, _event: ScanEvent) {}
61}
62
63use data::{DataBuilder, WatchData};
64mod data {
65 use crate::{
66 event::{CreateKind, DataChange, Event, EventKind, MetadataKind, ModifyKind, RemoveKind},
67 EventHandler,
68 };
69 use std::{
70 cell::RefCell,
71 collections::{hash_map::RandomState, HashMap},
72 fmt::{self, Debug},
73 fs::{self, File, Metadata},
74 hash::{BuildHasher, Hasher},
75 io::{self, Read},
76 path::{Path, PathBuf},
77 time::Instant,
78 };
79 use walkdir::WalkDir;
80
81 use super::ScanEventHandler;
82
83 fn system_time_to_seconds(time: std::time::SystemTime) -> i64 {
84 match time.duration_since(std::time::SystemTime::UNIX_EPOCH) {
85 Ok(d) => d.as_secs() as i64,
86 Err(e) => -(e.duration().as_secs() as i64),
87 }
88 }
89
90 pub(super) struct DataBuilder {
92 emitter: EventEmitter,
93 scan_emitter: Option<Box<RefCell<dyn ScanEventHandler>>>,
94
95 build_hasher: Option<RandomState>,
98
99 now: Instant,
101 }
102
103 impl DataBuilder {
104 pub(super) fn new<F, G>(
105 event_handler: F,
106 compare_content: bool,
107 scan_emitter: Option<G>,
108 ) -> Self
109 where
110 F: EventHandler,
111 G: ScanEventHandler,
112 {
113 let scan_emitter = match scan_emitter {
114 None => None,
115 Some(v) => {
116 let intermediate: Box<RefCell<dyn ScanEventHandler>> =
118 Box::new(RefCell::new(v));
119 Some(intermediate)
120 }
121 };
122 Self {
123 emitter: EventEmitter::new(event_handler),
124 scan_emitter,
125 build_hasher: compare_content.then(RandomState::default),
126 now: Instant::now(),
127 }
128 }
129
130 pub(super) fn update_timestamp(&mut self) {
132 self.now = Instant::now();
133 }
134
135 pub(super) fn build_watch_data(
140 &self,
141 root: PathBuf,
142 is_recursive: bool,
143 follow_symlinks: bool,
144 ) -> Option<WatchData> {
145 WatchData::new(self, root, is_recursive, follow_symlinks)
146 }
147
148 fn build_path_data(&self, meta_path: &MetaPath) -> PathData {
150 PathData::new(self, meta_path)
151 }
152 }
153
154 impl Debug for DataBuilder {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 f.debug_struct("DataBuilder")
157 .field("build_hasher", &self.build_hasher)
158 .field("now", &self.now)
159 .finish()
160 }
161 }
162
163 #[derive(Debug)]
164 pub(super) struct WatchData {
165 root: PathBuf,
167 is_recursive: bool,
168 follow_symlinks: bool,
169
170 all_path_data: HashMap<PathBuf, PathData>,
172 }
173
174 impl WatchData {
175 fn new(
181 data_builder: &DataBuilder,
182 root: PathBuf,
183 is_recursive: bool,
184 follow_symlinks: bool,
185 ) -> Option<Self> {
186 if let Err(e) = fs::metadata(&root) {
205 data_builder.emitter.emit_io_err(e, Some(&root));
206 return None;
207 }
208
209 let all_path_data = Self::scan_all_path_data(
210 data_builder,
211 root.clone(),
212 is_recursive,
213 follow_symlinks,
214 true,
215 )
216 .collect();
217
218 Some(Self {
219 root,
220 is_recursive,
221 follow_symlinks,
222 all_path_data,
223 })
224 }
225
226 pub(super) fn rescan(&mut self, data_builder: &mut DataBuilder) {
232 for (path, new_path_data) in Self::scan_all_path_data(
234 data_builder,
235 self.root.clone(),
236 self.is_recursive,
237 self.follow_symlinks,
238 false,
239 ) {
240 let old_path_data = self
241 .all_path_data
242 .insert(path.clone(), new_path_data.clone());
243
244 let event =
246 PathData::compare_to_event(path, old_path_data.as_ref(), Some(&new_path_data));
247 if let Some(event) = event {
248 data_builder.emitter.emit_ok(event);
249 }
250 }
251
252 let mut disappeared_paths = Vec::new();
254 for (path, path_data) in self.all_path_data.iter() {
255 if path_data.last_check < data_builder.now {
256 disappeared_paths.push(path.clone());
257 }
258 }
259
260 for path in disappeared_paths {
262 let old_path_data = self.all_path_data.remove(&path);
263
264 let event = PathData::compare_to_event(path, old_path_data.as_ref(), None);
266 if let Some(event) = event {
267 data_builder.emitter.emit_ok(event);
268 }
269 }
270 }
271
272 fn scan_all_path_data(
278 data_builder: &'_ DataBuilder,
279 root: PathBuf,
280 is_recursive: bool,
281 follow_symlinks: bool,
282 is_initial: bool,
284 ) -> impl Iterator<Item = (PathBuf, PathData)> + '_ {
285 log::trace!("rescanning {root:?}");
286 WalkDir::new(root)
291 .follow_links(follow_symlinks)
292 .max_depth(Self::dir_scan_depth(is_recursive))
293 .into_iter()
294 .filter_map(|entry_res| match entry_res {
295 Ok(entry) => Some(entry),
296 Err(err) => {
297 log::warn!("walkdir error scanning {err:?}");
298
299 if let Some(io_error) = err.io_error() {
300 let new_io_error = io::Error::new(io_error.kind(), err.to_string());
302 data_builder.emitter.emit_io_err(new_io_error, err.path());
303 } else {
304 let crate_err =
305 crate::Error::new(crate::ErrorKind::Generic(err.to_string()));
306 data_builder.emitter.emit(Err(crate_err));
307 }
308 None
309 }
310 })
311 .filter_map(move |entry| match entry.metadata() {
312 Ok(metadata) => {
313 let path = entry.into_path();
314 if is_initial {
315 if let Some(ref emitter) = data_builder.scan_emitter {
317 emitter.borrow_mut().handle_event(Ok(path.clone()));
318 }
319 }
320 let meta_path = MetaPath::from_parts_unchecked(path, metadata);
321 let data_path = data_builder.build_path_data(&meta_path);
322
323 Some((meta_path.into_path(), data_path))
324 }
325 Err(e) => {
326 let path = entry.into_path();
328 data_builder.emitter.emit_io_err(e, Some(path));
329
330 None
331 }
332 })
333 }
334
335 fn dir_scan_depth(is_recursive: bool) -> usize {
336 if is_recursive {
337 usize::MAX
338 } else {
339 1
340 }
341 }
342 }
343
344 #[derive(Debug, Clone)]
348 struct PathData {
349 mtime: i64,
351
352 hash: Option<u64>,
355
356 last_check: Instant,
358 }
359
360 impl PathData {
361 fn new(data_builder: &DataBuilder, meta_path: &MetaPath) -> PathData {
363 let metadata = meta_path.metadata();
364
365 PathData {
366 mtime: metadata.modified().map_or(0, system_time_to_seconds),
367 hash: data_builder
368 .build_hasher
369 .as_ref()
370 .filter(|_| metadata.is_file())
371 .and_then(|build_hasher| {
372 Self::get_content_hash(build_hasher, meta_path.path()).ok()
373 }),
374
375 last_check: data_builder.now,
376 }
377 }
378
379 fn get_content_hash(build_hasher: &RandomState, path: &Path) -> io::Result<u64> {
381 let mut hasher = build_hasher.build_hasher();
382 let mut file = File::open(path)?;
383 let mut buf = [0; 512];
384
385 loop {
386 let n = match file.read(&mut buf) {
387 Ok(0) => break,
388 Ok(len) => len,
389 Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
390 Err(e) => return Err(e),
391 };
392
393 hasher.write(&buf[..n]);
394 }
395
396 Ok(hasher.finish())
397 }
398
399 fn compare_to_event<P>(
401 path: P,
402 old: Option<&PathData>,
403 new: Option<&PathData>,
404 ) -> Option<Event>
405 where
406 P: Into<PathBuf>,
407 {
408 match (old, new) {
409 (Some(old), Some(new)) => {
410 if new.mtime > old.mtime {
411 Some(EventKind::Modify(ModifyKind::Metadata(
412 MetadataKind::WriteTime,
413 )))
414 } else if new.hash != old.hash {
415 Some(EventKind::Modify(ModifyKind::Data(DataChange::Any)))
416 } else {
417 None
418 }
419 }
420 (None, Some(_new)) => Some(EventKind::Create(CreateKind::Any)),
421 (Some(_old), None) => Some(EventKind::Remove(RemoveKind::Any)),
422 (None, None) => None,
423 }
424 .map(|event_kind| Event::new(event_kind).add_path(path.into()))
425 }
426 }
427
428 #[derive(Debug)]
434 pub(super) struct MetaPath {
435 path: PathBuf,
436 metadata: Metadata,
437 }
438
439 impl MetaPath {
440 fn from_parts_unchecked(path: PathBuf, metadata: Metadata) -> Self {
446 Self { path, metadata }
447 }
448
449 fn path(&self) -> &Path {
450 &self.path
451 }
452
453 fn metadata(&self) -> &Metadata {
454 &self.metadata
455 }
456
457 fn into_path(self) -> PathBuf {
458 self.path
459 }
460 }
461
462 struct EventEmitter(
464 Box<RefCell<dyn EventHandler>>,
467 );
468
469 impl EventEmitter {
470 fn new<F: EventHandler>(event_handler: F) -> Self {
471 Self(Box::new(RefCell::new(event_handler)))
472 }
473
474 fn emit(&self, event: crate::Result<Event>) {
476 self.0.borrow_mut().handle_event(event);
477 }
478
479 fn emit_ok(&self, event: Event) {
481 self.emit(Ok(event))
482 }
483
484 fn emit_io_err<E, P>(&self, err: E, path: Option<P>)
486 where
487 E: Into<io::Error>,
488 P: Into<PathBuf>,
489 {
490 let e = crate::Error::io(err.into());
491 if let Some(path) = path {
492 self.emit(Err(e.add_path(path.into())));
493 } else {
494 self.emit(Err(e));
495 }
496 }
497 }
498}
499
500#[derive(Debug)]
507pub struct PollWatcher {
508 watches: Arc<Mutex<HashMap<PathBuf, WatchData>>>,
509 data_builder: Arc<Mutex<DataBuilder>>,
510 want_to_stop: Arc<AtomicBool>,
511 message_channel: Sender<()>,
514 delay: Option<Duration>,
515 follow_sylinks: bool,
516}
517
518impl PollWatcher {
519 pub fn new<F: EventHandler>(event_handler: F, config: Config) -> crate::Result<PollWatcher> {
521 Self::with_opt::<_, ()>(event_handler, config, None)
522 }
523
524 pub fn poll(&self) -> crate::Result<()> {
526 self.message_channel
527 .send(())
528 .map_err(|_| Error::generic("failed to send poll message"))?;
529 Ok(())
530 }
531
532 #[cfg(test)]
534 pub(crate) fn poll_sender(&self) -> Sender<()> {
535 self.message_channel.clone()
536 }
537
538 pub fn with_initial_scan<F: EventHandler, G: ScanEventHandler>(
542 event_handler: F,
543 config: Config,
544 scan_callback: G,
545 ) -> crate::Result<PollWatcher> {
546 Self::with_opt(event_handler, config, Some(scan_callback))
547 }
548
549 fn with_opt<F: EventHandler, G: ScanEventHandler>(
551 event_handler: F,
552 config: Config,
553 scan_callback: Option<G>,
554 ) -> crate::Result<PollWatcher> {
555 let data_builder =
556 DataBuilder::new(event_handler, config.compare_contents(), scan_callback);
557
558 let (tx, rx) = unbounded();
559
560 let poll_watcher = PollWatcher {
561 watches: Default::default(),
562 data_builder: Arc::new(Mutex::new(data_builder)),
563 want_to_stop: Arc::new(AtomicBool::new(false)),
564 delay: config.poll_interval(),
565 follow_sylinks: config.follow_symlinks(),
566 message_channel: tx,
567 };
568
569 poll_watcher.run(rx);
570
571 Ok(poll_watcher)
572 }
573
574 fn run(&self, rx: Receiver<()>) {
575 let watches = Arc::clone(&self.watches);
576 let data_builder = Arc::clone(&self.data_builder);
577 let want_to_stop = Arc::clone(&self.want_to_stop);
578 let delay = self.delay;
579
580 let _ = thread::Builder::new()
581 .name("notify-rs poll loop".to_string())
582 .spawn(move || {
583 loop {
584 if want_to_stop.load(Ordering::SeqCst) {
585 break;
586 }
587
588 if let (Ok(mut watches), Ok(mut data_builder)) =
593 (watches.lock(), data_builder.lock())
594 {
595 data_builder.update_timestamp();
596
597 let vals = watches.values_mut();
598 for watch_data in vals {
599 watch_data.rescan(&mut data_builder);
600 }
601 }
602 if let Some(delay) = delay {
604 let _ = rx.recv_timeout(delay);
605 } else {
606 let _ = rx.recv();
607 }
608 }
609 });
610 }
611
612 fn watch_inner(&mut self, path: &Path, recursive_mode: RecursiveMode) {
617 if let (Ok(mut watches), Ok(mut data_builder)) =
621 (self.watches.lock(), self.data_builder.lock())
622 {
623 data_builder.update_timestamp();
624
625 let watch_data = data_builder.build_watch_data(
626 path.to_path_buf(),
627 recursive_mode.is_recursive(),
628 self.follow_sylinks,
629 );
630
631 if let Some(watch_data) = watch_data {
633 watches.insert(path.to_path_buf(), watch_data);
634 }
635 }
636 }
637
638 fn unwatch_inner(&mut self, path: &Path) -> crate::Result<()> {
642 self.watches
644 .lock()
645 .unwrap()
646 .remove(path)
647 .map(|_| ())
648 .ok_or_else(crate::Error::watch_not_found)
649 }
650}
651
652impl Watcher for PollWatcher {
653 fn new<F: EventHandler>(event_handler: F, config: Config) -> crate::Result<Self> {
655 Self::new(event_handler, config)
656 }
657
658 fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> crate::Result<()> {
659 self.watch_inner(path, recursive_mode);
660
661 Ok(())
662 }
663
664 fn unwatch(&mut self, path: &Path) -> crate::Result<()> {
665 self.unwatch_inner(path)
666 }
667
668 fn kind() -> crate::WatcherKind {
669 crate::WatcherKind::PollWatcher
670 }
671}
672
673impl Drop for PollWatcher {
674 fn drop(&mut self) {
675 self.want_to_stop.store(true, Ordering::Relaxed);
676 }
677}
678
679#[cfg(test)]
680mod tests {
681 use super::PollWatcher;
682 use crate::test::*;
683
684 fn watcher() -> (TestWatcher<PollWatcher>, Receiver) {
685 poll_watcher_channel()
686 }
687
688 #[test]
689 fn poll_watcher_is_send_and_sync() {
690 fn check<T: Send + Sync>() {}
691 check::<PollWatcher>();
692 }
693
694 #[test]
695 fn create_file() {
696 let tmpdir = testdir();
697 let (mut watcher, mut rx) = watcher();
698 watcher.watch_recursively(&tmpdir);
699
700 let path = tmpdir.path().join("entry");
701 std::fs::File::create_new(&path).expect("Unable to create");
702
703 rx.sleep_until_parent_contains(&path);
704 rx.sleep_until_exists(&path);
705
706 rx.wait_ordered_exact([expected(&path).create_any()]);
707 }
708
709 #[test]
710 fn create_dir() {
711 let tmpdir = testdir();
712 let (mut watcher, mut rx) = watcher();
713 watcher.watch_recursively(&tmpdir);
714
715 let path = tmpdir.path().join("entry");
716 std::fs::create_dir(&path).expect("Unable to create");
717
718 rx.sleep_until_parent_contains(&path);
719 rx.sleep_until_exists(&path);
720
721 rx.wait_ordered_exact([expected(&path).create_any()]);
722 }
723
724 #[test]
725 fn modify_file() {
726 let tmpdir = testdir();
727 let (mut watcher, mut rx) = watcher();
728 let path = tmpdir.path().join("entry");
729 std::fs::File::create_new(&path).expect("Unable to create");
730
731 rx.sleep_until_parent_contains(&path);
732
733 watcher.watch_recursively(&tmpdir);
734 std::fs::write(&path, b"123").expect("Unable to write");
735
736 assert!(
737 rx.sleep_until(|| std::fs::read_to_string(&path).is_ok_and(|content| content == "123")),
738 "the file wasn't modified"
739 );
740 rx.wait_ordered_exact([expected(&path).modify_data_any()]);
741 }
742
743 #[test]
744 fn remove_file() {
745 let tmpdir = testdir();
746 let (mut watcher, mut rx) = watcher();
747 let path = tmpdir.path().join("entry");
748 std::fs::File::create_new(&path).expect("Unable to create");
749
750 rx.sleep_until_parent_contains(&path);
751
752 watcher.watch_recursively(&tmpdir);
753
754 std::fs::remove_file(&path).expect("Unable to remove");
755
756 rx.sleep_while_exists(&path);
757 rx.sleep_while_parent_contains(&path);
758
759 rx.wait_ordered_exact([expected(&path).remove_any()]);
760 }
761
762 #[test]
763 fn rename_file() {
764 let tmpdir = testdir();
765 let (mut watcher, mut rx) = watcher();
766 let path = tmpdir.path().join("entry");
767 let new_path = tmpdir.path().join("new_entry");
768 std::fs::File::create_new(&path).expect("Unable to create");
769
770 rx.sleep_until_parent_contains(&path);
771
772 watcher.watch_recursively(&tmpdir);
773
774 std::fs::rename(&path, &new_path).expect("Unable to remove");
775
776 rx.sleep_while_exists(&path);
777 rx.sleep_until_exists(&new_path);
778
779 rx.sleep_while_parent_contains(&path);
780 rx.sleep_until_parent_contains(&new_path);
781
782 rx.wait_unordered_exact([
783 expected(&path).remove_any(),
784 expected(&new_path).create_any(),
785 ]);
786 }
787
788 #[test]
789 fn create_write_overwrite() {
790 let tmpdir = testdir();
791 let (mut watcher, mut rx) = watcher();
792 let overwritten_file = tmpdir.path().join("overwritten_file");
793 let overwriting_file = tmpdir.path().join("overwriting_file");
794 std::fs::write(&overwritten_file, "123").expect("write1");
795
796 rx.sleep_until_parent_contains(&overwritten_file);
797
798 watcher.watch_nonrecursively(&tmpdir);
799
800 std::fs::File::create(&overwriting_file).expect("create");
801 std::fs::write(&overwriting_file, "321").expect("write2");
802 std::fs::rename(&overwriting_file, &overwritten_file).expect("rename");
803
804 rx.sleep_while_exists(&overwriting_file);
805 rx.sleep_while_parent_contains(&overwriting_file);
806
807 assert!(
808 rx.sleep_until(
809 || std::fs::read_to_string(&overwritten_file).is_ok_and(|cnt| cnt == "321")
810 ),
811 "file {overwritten_file:?} was not replaced"
812 );
813
814 rx.wait_unordered([expected(&overwritten_file).modify_data_any()]);
815 }
816}