1use std::borrow::Cow;
4use std::collections::HashMap;
5use std::collections::HashSet;
6use std::mem::ManuallyDrop;
7use std::path::Path;
8use std::path::PathBuf;
9use std::sync::mpsc;
10use std::sync::mpsc::Receiver;
11use std::sync::mpsc::RecvTimeoutError;
12use std::time::Duration;
13
14use globset::GlobBuilder;
15use globset::GlobSet;
16use globset::GlobSetBuilder;
17use notify::Config;
18use notify::Event;
19use notify::EventKind;
20use notify::RecommendedWatcher;
21use notify::RecursiveMode;
22use notify::Watcher as NotifyWatcher;
23use notify::event::ModifyKind;
24
25use crate::Database;
26use crate::DatabaseReader;
27use crate::ReadDatabase;
28use crate::error::DatabaseError;
29use crate::exclusion::Exclusion;
30use crate::file::File;
31use crate::file::FileId;
32use crate::file::FileType;
33use crate::loader::calculate_pattern_specificity;
34use crate::loader::resolve_file_type;
35use crate::utils::bytes_to_path;
36
37const DEFAULT_POLL_INTERVAL_MS: u64 = 1000;
38const WAIT_INTERNAL_MS: u64 = 100;
39const WAIT_DEBOUNCE_MS: u64 = 300;
40const STABILITY_CHECK_MS: u64 = 10;
41
42#[derive(Debug, Clone, PartialEq, Eq, Hash)]
43struct ChangedFile {
44 id: FileId,
45 path: PathBuf,
46}
47
48#[derive(Debug, Clone)]
50pub struct WatchOptions {
51 pub poll_interval: Option<Duration>,
52 pub additional_excludes: Vec<Exclusion<'static>>,
53}
54
55impl Default for WatchOptions {
56 #[inline]
57 fn default() -> Self {
58 Self { poll_interval: Some(Duration::from_millis(DEFAULT_POLL_INTERVAL_MS)), additional_excludes: vec![] }
59 }
60}
61
62pub struct DatabaseWatcher<'config> {
64 database: Database<'config>,
65 watcher: Option<RecommendedWatcher>,
66 watched_paths: Vec<PathBuf>,
67 receiver: Option<Receiver<Vec<ChangedFile>>>,
68 host_base_paths: Vec<(PathBuf, usize)>,
74 patch_base_paths: Vec<(PathBuf, usize)>,
75 include_base_paths: Vec<(PathBuf, usize)>,
76}
77
78impl<'config> DatabaseWatcher<'config> {
79 #[inline]
80 #[must_use]
81 pub fn new(database: Database<'config>) -> Self {
82 Self {
83 database,
84 watcher: None,
85 watched_paths: Vec::new(),
86 receiver: None,
87 host_base_paths: Vec::new(),
88 patch_base_paths: Vec::new(),
89 include_base_paths: Vec::new(),
90 }
91 }
92
93 #[inline]
102 pub fn watch(&mut self, options: WatchOptions) -> Result<(), DatabaseError> {
103 self.stop();
104
105 let config = &self.database.configuration;
106
107 let (tx, rx) = mpsc::channel();
108
109 let mut all_exclusions = vec![
110 Exclusion::Pattern(Cow::Borrowed("**/node_modules/**")),
111 Exclusion::Pattern(Cow::Borrowed("**/.git/**")),
112 Exclusion::Pattern(Cow::Borrowed("**/.idea/**")),
113 Exclusion::Pattern(Cow::Borrowed("**/vendor/**")),
114 ];
115 all_exclusions.extend(config.excludes.iter().cloned());
116 all_exclusions.extend(options.additional_excludes);
117
118 let glob_settings = &config.glob;
119 let mut glob_builder = GlobSetBuilder::new();
120 for ex in &all_exclusions {
121 if let Exclusion::Pattern(pat) = ex {
122 let glob = GlobBuilder::new(pat)
123 .case_insensitive(glob_settings.case_insensitive)
124 .literal_separator(glob_settings.literal_separator)
125 .backslash_escape(glob_settings.backslash_escape)
126 .empty_alternates(glob_settings.empty_alternates)
127 .build()?;
128 glob_builder.add(glob);
129 }
130 }
131
132 let glob_excludes = glob_builder.build()?;
133
134 let path_excludes: HashSet<PathBuf> = all_exclusions
135 .iter()
136 .filter_map(|ex| match ex {
137 Exclusion::Path(p) => Some(p.as_ref().to_path_buf()),
138 Exclusion::Pattern(_) => None,
139 })
140 .collect();
141
142 let extensions: HashSet<Vec<u8>> = config.extensions.iter().map(|c| c.to_vec()).collect();
143 let workspace = config.workspace.as_ref().to_path_buf();
144
145 let mut unique_watch_paths = HashSet::new();
150
151 let mut host_base_paths = Vec::new();
152 for path in &config.paths {
153 let specificity = calculate_pattern_specificity(path.as_ref());
156 let watch_path = Self::extract_watch_path(path.as_ref());
157 let absolute_path = if watch_path.is_absolute() { watch_path } else { config.workspace.join(watch_path) };
158
159 let canonical = absolute_path.canonicalize().unwrap_or_else(|_| absolute_path.clone());
160 host_base_paths.push((canonical, specificity));
161 unique_watch_paths.insert(absolute_path);
162 }
163
164 let mut include_base_paths = Vec::new();
165 for path in &config.includes {
166 let specificity = calculate_pattern_specificity(path.as_ref());
167 let watch_path = Self::extract_watch_path(path.as_ref());
168 let absolute_path = if watch_path.is_absolute() { watch_path } else { config.workspace.join(watch_path) };
169 let canonical = absolute_path.canonicalize().unwrap_or_else(|_| absolute_path.clone());
170 include_base_paths.push((canonical, specificity));
171 unique_watch_paths.insert(absolute_path);
172 }
173
174 let mut patch_base_paths = Vec::new();
175 for path in &config.patches {
176 let specificity = calculate_pattern_specificity(path.as_ref());
177 let watch_path = Self::extract_watch_path(path.as_ref());
178 let absolute_path = if watch_path.is_absolute() { watch_path } else { config.workspace.join(watch_path) };
179 let canonical = absolute_path.canonicalize().unwrap_or_else(|_| absolute_path.clone());
180 patch_base_paths.push((canonical, specificity));
181 unique_watch_paths.insert(absolute_path);
182 }
183
184 let explicit_watch_paths: Vec<PathBuf> = unique_watch_paths
185 .iter()
186 .filter(|wp| glob_excludes.is_match(wp.as_path()) || path_excludes.contains(wp.as_path()))
187 .cloned()
188 .collect();
189
190 let mut watcher = RecommendedWatcher::new(
191 move |res: Result<Event, notify::Error>| {
192 if let Ok(event) = res
193 && let Some(changed) = Self::handle_event(
194 event,
195 &workspace,
196 &glob_excludes,
197 &path_excludes,
198 &extensions,
199 &explicit_watch_paths,
200 )
201 {
202 let _ = tx.send(changed);
203 }
204 },
205 Config::default()
206 .with_poll_interval(options.poll_interval.unwrap_or(Duration::from_millis(DEFAULT_POLL_INTERVAL_MS))),
207 )
208 .map_err(DatabaseError::WatcherInit)?;
209
210 let mut watched_paths = Vec::new();
211 for path in unique_watch_paths {
212 watcher.watch(&path, RecursiveMode::Recursive).map_err(DatabaseError::WatcherWatch)?;
213 watched_paths.push(path.clone());
214 tracing::debug!("Watching path: {}", path.display());
215 }
216
217 tracing::info!("Database watcher started for workspace: {}", config.workspace.display());
218
219 self.watcher = Some(watcher);
220 self.watched_paths = watched_paths;
221 self.receiver = Some(rx);
222 self.host_base_paths = host_base_paths;
223 self.patch_base_paths = patch_base_paths;
224 self.include_base_paths = include_base_paths;
225
226 Ok(())
227 }
228
229 #[inline]
231 pub fn stop(&mut self) {
232 if let Some(mut watcher) = self.watcher.take() {
233 for path in &self.watched_paths {
234 let _ = watcher.unwatch(path);
235 tracing::debug!("Stopped watching: {}", path.display());
236 }
237 }
238 self.watched_paths.clear();
239 self.receiver = None;
240 self.host_base_paths.clear();
241 self.patch_base_paths.clear();
242 self.include_base_paths.clear();
243 }
244
245 #[inline]
247 #[must_use]
248 pub fn is_watching(&self) -> bool {
249 self.watcher.is_some()
250 }
251
252 fn extract_watch_path(pattern: &[u8]) -> PathBuf {
263 let is_glob =
264 pattern.contains(&b'*') || pattern.contains(&b'?') || pattern.contains(&b'[') || pattern.contains(&b'{');
265
266 if !is_glob {
267 return bytes_to_path(pattern).into_owned();
268 }
269
270 let first_glob_pos =
271 pattern.iter().position(|&b| matches!(b, b'*' | b'?' | b'[' | b'{')).unwrap_or(pattern.len());
272
273 let base = &pattern[..first_glob_pos];
274
275 let mut end = base.len();
276 while end > 0 && matches!(base[end - 1], b'/' | b'\\') {
277 end -= 1;
278 }
279 let base = &base[..end];
280
281 if base.is_empty() { PathBuf::from(".") } else { bytes_to_path(base).into_owned() }
282 }
283
284 fn handle_event(
285 event: Event,
286 workspace: &Path,
287 glob_excludes: &GlobSet,
288 path_excludes: &HashSet<PathBuf>,
289 extensions: &HashSet<Vec<u8>>,
290 explicit_watch_paths: &[PathBuf],
291 ) -> Option<Vec<ChangedFile>> {
292 tracing::debug!("Watcher received event: kind={:?}, paths={:?}", event.kind, event.paths);
293
294 if let EventKind::Other | EventKind::Any | EventKind::Access(_) | EventKind::Modify(ModifyKind::Metadata(_)) =
295 event.kind
296 {
297 tracing::debug!("Ignoring non-modification event: {:?}", event.kind);
298
299 return None;
300 }
301
302 let mut changed_files = Vec::new();
303
304 for path in event.paths {
305 if let Some(ext) = path.extension() {
307 if !extensions.contains(ext.as_encoded_bytes()) {
308 continue;
309 }
310 } else {
311 continue;
312 }
313
314 let is_explicitly_watched = explicit_watch_paths.iter().any(|wp| path.starts_with(wp));
315 if !is_explicitly_watched {
316 if glob_excludes.is_match(&path) {
318 tracing::debug!("Skipping path excluded by pattern: {}", path.display());
319 continue;
320 }
321
322 if path_excludes.contains(&path) {
324 tracing::debug!("Skipping excluded path: {}", path.display());
325 continue;
326 }
327
328 let mut should_skip = false;
330 for ancestor in path.ancestors().skip(1) {
331 if path_excludes.contains(ancestor) {
332 tracing::debug!("Skipping path under excluded directory: {}", path.display());
333 should_skip = true;
334 break;
335 }
336 }
337
338 if should_skip {
339 continue;
340 }
341 }
342
343 #[cfg(windows)]
345 let logical_name = path
346 .trim_prefix(workspace)
347 .as_os_str()
348 .as_encoded_bytes()
349 .iter()
350 .map(|i| if *i == b'\\' { b'/' } else { *i })
351 .collect::<Vec<_>>();
352 #[cfg(not(windows))]
353 let logical_name = path.trim_prefix(workspace).as_os_str().as_encoded_bytes().to_owned();
354
355 let file_id = FileId::new(&logical_name);
356
357 changed_files.push(ChangedFile { id: file_id, path });
358 }
359
360 if changed_files.is_empty() { None } else { Some(changed_files) }
361 }
362
363 #[inline]
374 pub fn wait(&mut self) -> Result<Vec<FileId>, DatabaseError> {
375 let Some(receiver) = &self.receiver else {
376 return Err(DatabaseError::WatcherNotActive);
377 };
378
379 let config = &self.database.configuration;
380 let workspace = config.workspace.as_ref().to_path_buf();
381
382 match receiver.recv_timeout(Duration::from_millis(WAIT_INTERNAL_MS)) {
383 Ok(changed_files) => {
384 let mut all_changed = changed_files;
385 loop {
386 match receiver.recv_timeout(Duration::from_millis(WAIT_DEBOUNCE_MS)) {
387 Ok(more) => all_changed.extend(more),
388 Err(RecvTimeoutError::Timeout) => break,
389 Err(RecvTimeoutError::Disconnected) => {
390 self.stop();
391 return Err(DatabaseError::WatcherNotActive);
392 }
393 }
394 }
395
396 let mut latest_changes: HashMap<FileId, ChangedFile> = HashMap::new();
397 for changed in all_changed {
398 latest_changes.insert(changed.id, changed);
399 }
400 let all_changed: Vec<ChangedFile> = latest_changes.into_values().collect();
401 let mut changed_ids = Vec::new();
402
403 for changed_file in &all_changed {
404 changed_ids.push(changed_file.id);
405
406 let Ok(file) = self.database.get(&changed_file.id) else {
407 if changed_file.path.exists() {
408 let new_file_type = classify_added_file(
409 &changed_file.path,
410 &self.host_base_paths,
411 &self.patch_base_paths,
412 &self.include_base_paths,
413 );
414 match File::read(&workspace, &changed_file.path, new_file_type) {
415 Ok(file) => {
416 self.database.add(file);
417 tracing::debug!("Added new file to database: {}", changed_file.path.display());
418 }
419 Err(e) => {
420 tracing::error!("Failed to load new file {}: {}", changed_file.path.display(), e);
421 }
422 }
423 }
424
425 continue;
426 };
427
428 if !changed_file.path.exists() {
429 self.database.delete(changed_file.id);
430 tracing::trace!("Deleted file from database: {}", String::from_utf8_lossy(&file.name));
431 continue;
432 }
433
434 match Self::read_stable_contents(&changed_file.path) {
435 Ok(contents) => {
436 if self.database.update(changed_file.id, Cow::Owned(contents)) {
437 tracing::trace!("Updated file in database: {}", String::from_utf8_lossy(&file.name));
438 } else {
439 tracing::warn!(
440 "Failed to update file in database (ID not found): {}",
441 String::from_utf8_lossy(&file.name)
442 );
443 }
444 }
445 Err(e) => {
446 tracing::error!("Failed to read file {}: {}", changed_file.path.display(), e);
447 }
448 }
449 }
450
451 Ok(changed_ids)
452 }
453 Err(RecvTimeoutError::Timeout) => Ok(Vec::new()),
454 Err(RecvTimeoutError::Disconnected) => {
455 self.stop();
456 Err(DatabaseError::WatcherNotActive)
457 }
458 }
459 }
460
461 fn read_stable_contents(path: &Path) -> std::io::Result<Vec<u8>> {
467 let contents = std::fs::read(path)?;
468
469 std::thread::sleep(Duration::from_millis(STABILITY_CHECK_MS));
470
471 if path.exists()
472 && let Ok(reread) = std::fs::read(path)
473 && reread != contents
474 {
475 tracing::debug!("File content changed during stability check: {}", path.display());
476
477 return Ok(reread);
478 }
479
480 Ok(contents)
481 }
482
483 #[inline]
485 #[must_use]
486 pub fn database(&self) -> &Database<'config> {
487 &self.database
488 }
489
490 #[inline]
492 #[must_use]
493 pub fn read_only_database(&self) -> ReadDatabase {
494 self.database.read_only()
495 }
496
497 #[inline]
499 pub fn database_mut(&mut self) -> &mut Database<'config> {
500 &mut self.database
501 }
502
503 #[inline]
512 pub fn with_database_mut<F, R>(&mut self, f: F) -> R
513 where
514 F: for<'borrow> FnOnce(&'borrow mut Database<'config>) -> R,
515 {
516 f(&mut self.database)
517 }
518
519 #[inline]
521 #[must_use]
522 pub fn into_database(self) -> Database<'config> {
523 let mut md = ManuallyDrop::new(self);
524 md.stop();
525 unsafe { std::ptr::read(&raw const md.database) }
528 }
529}
530
531impl Drop for DatabaseWatcher<'_> {
532 #[inline]
533 fn drop(&mut self) {
534 self.stop();
535 }
536}
537
538fn classify_added_file(
547 path: &Path,
548 host_bases: &[(PathBuf, usize)],
549 patch_bases: &[(PathBuf, usize)],
550 include_bases: &[(PathBuf, usize)],
551) -> FileType {
552 let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
553 let max_spec = |bases: &[(PathBuf, usize)]| {
554 bases.iter().filter(|(b, _)| canonical.starts_with(b.as_path())).map(|(_, s)| *s).max()
555 };
556
557 resolve_file_type(max_spec(host_bases), max_spec(include_bases), max_spec(patch_bases))
558}
559
560#[cfg(test)]
561mod classify_added_file_tests {
562 use super::*;
563
564 fn bases(items: &[&str]) -> Vec<(PathBuf, usize)> {
565 items.iter().map(|p| (PathBuf::from(p), calculate_pattern_specificity(p.as_bytes()))).collect()
566 }
567
568 #[test]
569 fn defaults_to_host_when_no_base_matches() {
570 let ft =
571 classify_added_file(Path::new("/ws/orphan/foo.php"), &bases(&["/ws/src"]), &[], &bases(&["/ws/stubs"]));
572 assert_eq!(ft, FileType::Host);
573 }
574
575 #[test]
576 fn vendored_when_only_include_matches() {
577 let ft = classify_added_file(Path::new("/ws/stubs/foo.php"), &bases(&["/ws/src"]), &[], &bases(&["/ws/stubs"]));
581 assert_eq!(ft, FileType::Vendored);
582 }
583
584 #[test]
585 fn host_when_only_host_matches() {
586 let ft = classify_added_file(Path::new("/ws/src/foo.php"), &bases(&["/ws/src"]), &[], &bases(&["/ws/stubs"]));
587 assert_eq!(ft, FileType::Host);
588 }
589
590 #[test]
591 fn matches_loader_at_equal_specificity_for_same_dir_under_both() {
592 let ft =
596 classify_added_file(Path::new("/ws/shared/foo.php"), &bases(&["/ws/shared"]), &[], &bases(&["/ws/shared"]));
597 assert_eq!(ft, FileType::Vendored);
598 }
599
600 #[test]
601 fn include_wins_when_strictly_more_specific() {
602 let ft = classify_added_file(
605 Path::new("/ws/src/vendor/stub.php"),
606 &bases(&["/ws/src"]),
607 &[],
608 &bases(&["/ws/src/vendor"]),
609 );
610 assert_eq!(ft, FileType::Vendored);
611 }
612
613 #[test]
614 fn exact_host_file_beats_directory_include_via_loader_score() {
615 let ft =
620 classify_added_file(Path::new("/ws/src/foo.php"), &bases(&["/ws/src/foo.php"]), &[], &bases(&["/ws/src"]));
621 assert_eq!(ft, FileType::Host);
622 }
623
624 #[test]
625 fn patch_when_only_patch_matches() {
626 let ft = classify_added_file(
627 Path::new("/ws/patches/foo.php"),
628 &bases(&["/ws/src"]),
629 &bases(&["/ws/patches"]),
630 &bases(&["/ws/stubs"]),
631 );
632 assert_eq!(ft, FileType::Patch);
633 }
634
635 #[test]
636 fn patch_wins_over_host_when_strictly_more_specific() {
637 let ft = classify_added_file(
638 Path::new("/ws/src/patches/foo.php"),
639 &bases(&["/ws/src"]),
640 &bases(&["/ws/src/patches"]),
641 &[],
642 );
643 assert_eq!(ft, FileType::Patch);
644 }
645
646 #[test]
647 fn host_wins_over_patch_at_equal_specificity() {
648 let ft =
650 classify_added_file(Path::new("/ws/shared/foo.php"), &bases(&["/ws/shared"]), &bases(&["/ws/shared"]), &[]);
651 assert_eq!(ft, FileType::Host);
652 }
653
654 #[test]
655 fn patch_wins_over_include_when_both_match_without_host() {
656 let ft = classify_added_file(
659 Path::new("/ws/overlap/foo.php"),
660 &[],
661 &bases(&["/ws/overlap"]),
662 &bases(&["/ws/overlap"]),
663 );
664 assert_eq!(ft, FileType::Patch);
665 }
666}