Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions crates/vuio-core/src/watcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
}
Expand Down
49 changes: 45 additions & 4 deletions crates/vuio-core/src/watcher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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"),
}
}
Expand Down Expand Up @@ -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));
}
Loading