System: add a Windows DirectoryWatcher backend

Implement DirectoryWatcher on Windows with ReadDirectoryChangesW: one
recursive handle, a dedicated watcher thread feeding a bounded queue, a
condition variable for poll timeouts, rename pairing, ignore filtering
and RescanRequired on buffer overflow. rescan cancels the pending read
and clears the queue; wake/simulate hooks work as on Linux.

Tests run on both platforms with platform-appropriate watch-count and
overflow expectations.

Verified under Wine 11 (x86_64-w64-mingw32 GCC 16.2): full VoidTests.exe
suite passes (1337 tests, 1325 passed, 12 skipped, 0 failed). Linux
System+Testing suite unchanged (477 tests, 474 passed).

Closes #65
This commit is contained in:
faumaray committed 2026-12-06 01:01:28 +03:00
1 parent 796609787e
commit 482a3ea46b
3 files changed
+377 -8

No files matched your search

+33 -8
View File
@@ -36,9 +36,6 @@ namespace
Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames()
{
#ifdef _WIN32
Void::Testing::skip("DirectoryWatcher has no Windows backend (inotify-only)");
#endif
Void::FileSystem::TempDir tmp{"void_dir_watch_"};
const auto ignored_dir = tmp / ".git";
Void::Testing::require(
@@ -83,7 +80,12 @@ Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames
ev.kind == Void::FileSystem::DirectoryEventKind::Modified);
});
Void::Testing::expect(saw_file_create_or_mod);
#ifdef _WIN32
// One recursive ReadDirectoryChangesW handle covers the whole subtree.
Void::Testing::expect(watcher.watch_count() >= 1U);
#else
Void::Testing::expect(watcher.watch_count() >= 3U);
#endif
// Modify inside auto-watched nested directory
Void::Testing::require(
@@ -124,8 +126,17 @@ Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames
Void::Testing::expect(std::any_of(
post_move_events.begin(), post_move_events.end(),
[&](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Modified &&
ev.path == (watcher.root() / "src/engine/entry.cpp");
if (ev.kind != Void::FileSystem::DirectoryEventKind::Modified ||
ev.path.filename() != "entry.cpp") {
return false;
}
#ifdef _WIN32
// ReadDirectoryChangesW may report the pre-rename path for a
// modification shortly after a directory rename.
return true;
#else
return ev.path == (watcher.root() / "src/engine/entry.cpp");
#endif
}));
// Directory replacement / deletion storm
@@ -141,9 +152,6 @@ Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames
Void::Testing::Test directory_watcher_signals_rescan_on_overflow_and_limit()
{
#ifdef _WIN32
Void::Testing::skip("DirectoryWatcher has no Windows backend (inotify-only)");
#endif
Void::FileSystem::TempDir tmp{"void_dir_ovf_"};
// 1. Simulated queue overflow emits RescanRequired and wake recovers
@@ -171,12 +179,29 @@ Void::Testing::Test directory_watcher_signals_rescan_on_overflow_and_limit()
Void::Testing::require(w_res.has_value());
Void::Testing::require(
Void::FileSystem::ensure_directory(tmp / "extra_dir").has_value());
#ifdef _WIN32
// ReadDirectoryChangesW delivery can lag under load.
const auto evs = collect_events(*w_res, std::chrono::milliseconds{2000});
#else
const auto evs = collect_events(*w_res, std::chrono::milliseconds{400});
#endif
#ifdef _WIN32
// Windows watches recursively with a single handle, so max_watches
// cannot be exhausted; the new directory is simply reported.
Void::Testing::expect(std::any_of(
evs.begin(), evs.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.path.filename() == "extra_dir" &&
ev.kind !=
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
#else
Void::Testing::expect(std::any_of(
evs.begin(), evs.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind ==
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
#endif
}
}