From bd49332d4ff316ad1a20718f706be45aca886143 Mon Sep 17 00:00:00 2001 From: vyrti Date: Tue, 17 Nov 2026 23:04:25 +0200 Subject: [PATCH] Treat missing paths reported as modified as deletions --- crates/vuio-core/src/watcher/mod.rs | 35 +++++++++++++------ crates/vuio-core/src/watcher/tests.rs | 49 ++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/crates/vuio-core/src/watcher/mod.rs b/crates/vuio-core/src/watcher/mod.rs index d8d14a1..32285b0 100644 --- a/crates/vuio-core/src/watcher/mod.rs +++ b/crates/vuio-core/src/watcher/mod.rs @@ -337,17 +337,30 @@ fn convert_watcher_events( | notify::event::ModifyKind::Metadata(_) | notify::event::ModifyKind::Any | notify::event::ModifyKind::Other => { - // Only process modify events for media files - let media_paths: Vec<_> = event - .event - .paths - .iter() - .filter(|path| path_is_relevant(path, policies)) - .collect(); - - for path in media_paths { - debug!("Media file modified: {}", path.display()); - fs_events.push(FileSystemEvent::Modified(path.clone())); + for path in &event.event.paths { + // macOS reports a deletion as a metadata change on the + // removed path, and the debouncer can drop the matching + // Remove event when it coalesces a create+remove pair. + // A path that no longer exists is a deletion however the + // backend labelled it — otherwise a deleted folder is + // never retired from the index and the TV keeps listing + // it until a rescan. An I/O error does not establish a + // deletion, so only `Ok(false)` counts. + if matches!(path.try_exists(), Ok(false)) + && ScanPolicy::for_path(policies, path).is_some() + { + debug!( + "Path reported modified but missing; treating as deleted: {}", + path.display() + ); + fs_events.push(FileSystemEvent::Deleted { + path: path.clone(), + is_directory: None, + }); + } else if path_is_relevant(path, policies) { + debug!("Media file modified: {}", path.display()); + fs_events.push(FileSystemEvent::Modified(path.clone())); + } } } } diff --git a/crates/vuio-core/src/watcher/tests.rs b/crates/vuio-core/src/watcher/tests.rs index bf52c53..968073e 100644 --- a/crates/vuio-core/src/watcher/tests.rs +++ b/crates/vuio-core/src/watcher/tests.rs @@ -224,13 +224,15 @@ fn test_convert_watcher_events_ignores_access() { fn test_convert_watcher_events_handles_modify() { use notify::event::{ModifyKind, DataChange}; - let policy = ScanPolicy::platform_default(Path::new("/media"), true); - let policies = vec![policy]; + let root = TempDir::new().unwrap(); + let song = root.path().join("song.flac"); + fs::write(&song, b"audio").unwrap(); + let policies = vec![ScanPolicy::platform_default(root.path(), true)]; let modify_event = DebouncedEvent { event: notify::Event { kind: notify::EventKind::Modify(ModifyKind::Data(DataChange::Content)), - paths: vec![PathBuf::from("/media/song.flac")], + paths: vec![song.clone()], attrs: notify::event::EventAttributes::default(), }, time: std::time::Instant::now(), @@ -239,7 +241,7 @@ fn test_convert_watcher_events_handles_modify() { let converted = convert_watcher_events(vec![modify_event], &policies); assert_eq!(converted.len(), 1); match &converted[0] { - FileSystemEvent::Modified(p) => assert_eq!(p, Path::new("/media/song.flac")), + FileSystemEvent::Modified(p) => assert_eq!(p, &song), _ => panic!("Expected FileSystemEvent::Modified"), } } @@ -278,3 +280,42 @@ fn generic_events_report_missing_files_and_directories_as_deletions() { assert!(matches!(&converted[2], FileSystemEvent::Modified(path) if path == &present)); } } + +/// macOS reports a removal as `Modify(Metadata)` on the removed path when the +/// debouncer drops the matching `Remove` event. Missing paths must still reach +/// index cleanup, and directories have no media extension to let the regular +/// relevance check catch them. +#[test] +fn metadata_events_for_missing_paths_are_deletions() { + use notify::event::MetadataKind; + + let root = TempDir::new().unwrap(); + let policies = vec![ScanPolicy::platform_default(root.path(), true)]; + let missing_folder = root.path().join("deleted-folder"); + let missing_file = root.path().join("gone.mp4"); + let present = root.path().join("here.mp4"); + fs::write(&present, b"video").unwrap(); + + let event = DebouncedEvent { + event: notify::Event::new(notify::EventKind::Modify( + notify::event::ModifyKind::Metadata(MetadataKind::Extended), + )) + .add_path(missing_folder.clone()) + .add_path(missing_file.clone()) + .add_path(present.clone()), + time: std::time::Instant::now(), + }; + + let converted = convert_watcher_events(vec![event], &policies); + assert_eq!(converted.len(), 3); + for (event, expected) in converted[..2] + .iter() + .zip([&missing_folder, &missing_file]) + { + assert!( + matches!(event, FileSystemEvent::Deleted { path, is_directory: None } if path == expected), + "a metadata event for a missing path must delete indexed content: {event:?}" + ); + } + assert!(matches!(&converted[2], FileSystemEvent::Modified(path) if path == &present)); +}