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:
1 parent
796609787e
commit
482a3ea46b
3 files changed
+377
-8
No files matched your search
@@ -19,6 +19,19 @@
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(_WIN32)
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
namespace Void::FileSystem
|
||||
@@ -776,6 +789,334 @@ int DirectoryWatcher::native_handle() const noexcept
|
||||
return impl_ ? impl_->inotify_fd.get() : -1;
|
||||
}
|
||||
|
||||
#elif defined(_WIN32)
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
constexpr DWORD kNotifyFilter =
|
||||
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME |
|
||||
FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE |
|
||||
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION;
|
||||
constexpr std::size_t kWatchBufferBytes = 64U * 1024U;
|
||||
|
||||
} // namespace
|
||||
|
||||
// ReadDirectoryChangesW is blocking on a dedicated thread; poll() drains the
|
||||
// queue and waits on a condition variable. This avoids the overlapped-event
|
||||
// races and is portable across Windows and Wine.
|
||||
struct DirectoryWatcher::Impl {
|
||||
HANDLE directory{INVALID_HANDLE_VALUE};
|
||||
std::filesystem::path root{};
|
||||
DirectoryWatcherOptions options{};
|
||||
|
||||
std::thread worker{};
|
||||
std::atomic<bool> stop{false};
|
||||
std::atomic<bool> rescan_requested{false};
|
||||
|
||||
mutable std::mutex mutex{};
|
||||
std::condition_variable changed{};
|
||||
std::deque<DirectoryEvent> queue{};
|
||||
bool rescan_emitted{false};
|
||||
bool wake_requested{false};
|
||||
|
||||
~Impl() noexcept
|
||||
{
|
||||
stop.store(true);
|
||||
if (worker.joinable()) {
|
||||
if (directory != INVALID_HANDLE_VALUE) {
|
||||
(void)::CancelIoEx(directory, nullptr);
|
||||
}
|
||||
worker.join();
|
||||
}
|
||||
if (directory != INVALID_HANDLE_VALUE) {
|
||||
(void)::CloseHandle(directory);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool should_ignore(const std::filesystem::path& path,
|
||||
const bool is_directory) const
|
||||
{
|
||||
if (options.ignore) {
|
||||
return options.ignore(path, is_directory);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void push_event_locked(DirectoryEvent event)
|
||||
{
|
||||
if (event.kind == DirectoryEventKind::RescanRequired) {
|
||||
if (rescan_emitted) {
|
||||
return;
|
||||
}
|
||||
rescan_emitted = true;
|
||||
queue.push_back(std::move(event));
|
||||
return;
|
||||
}
|
||||
if (queue.size() >= options.max_queued_events) {
|
||||
if (!rescan_emitted) {
|
||||
rescan_emitted = true;
|
||||
queue.push_back(DirectoryEvent{
|
||||
.kind = DirectoryEventKind::RescanRequired,
|
||||
.path = root,
|
||||
.old_path = {},
|
||||
.is_directory = true,
|
||||
.detail = "DirectoryWatcher event queue full",
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
queue.push_back(std::move(event));
|
||||
}
|
||||
|
||||
void push_filtered_locked(DirectoryEvent event)
|
||||
{
|
||||
if (should_ignore(event.path, event.is_directory)) {
|
||||
return;
|
||||
}
|
||||
push_event_locked(std::move(event));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<DirectoryEvent> drain_locked()
|
||||
{
|
||||
std::vector<DirectoryEvent> out;
|
||||
out.reserve(queue.size());
|
||||
while (!queue.empty()) {
|
||||
out.push_back(std::move(queue.front()));
|
||||
queue.pop_front();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void parse_buffer_locked(const std::byte* data, const std::size_t size)
|
||||
{
|
||||
std::optional<std::filesystem::path> renamed_from;
|
||||
bool renamed_from_is_dir = false;
|
||||
std::size_t offset = 0;
|
||||
while (offset + sizeof(FILE_NOTIFY_INFORMATION) <= size) {
|
||||
const auto* info = reinterpret_cast<const FILE_NOTIFY_INFORMATION*>(
|
||||
data + offset);
|
||||
const std::wstring name{info->FileName,
|
||||
info->FileNameLength / sizeof(WCHAR)};
|
||||
const std::filesystem::path path = root / std::filesystem::path{name};
|
||||
std::error_code ec;
|
||||
const bool is_directory = std::filesystem::is_directory(path, ec);
|
||||
|
||||
switch (info->Action) {
|
||||
case FILE_ACTION_ADDED:
|
||||
push_filtered_locked(
|
||||
DirectoryEvent{.kind = DirectoryEventKind::Created,
|
||||
.path = path,
|
||||
.is_directory = is_directory});
|
||||
break;
|
||||
case FILE_ACTION_REMOVED:
|
||||
push_filtered_locked(
|
||||
DirectoryEvent{.kind = DirectoryEventKind::Deleted,
|
||||
.path = path,
|
||||
.is_directory = is_directory});
|
||||
break;
|
||||
case FILE_ACTION_MODIFIED:
|
||||
push_filtered_locked(
|
||||
DirectoryEvent{.kind = DirectoryEventKind::Modified,
|
||||
.path = path,
|
||||
.is_directory = is_directory});
|
||||
break;
|
||||
case FILE_ACTION_RENAMED_OLD_NAME:
|
||||
renamed_from = path;
|
||||
renamed_from_is_dir = is_directory;
|
||||
break;
|
||||
case FILE_ACTION_RENAMED_NEW_NAME: {
|
||||
DirectoryEvent event{.kind = DirectoryEventKind::Renamed,
|
||||
.path = path,
|
||||
.old_path = renamed_from.value_or(path),
|
||||
.is_directory = is_directory ||
|
||||
renamed_from_is_dir};
|
||||
push_filtered_locked(std::move(event));
|
||||
renamed_from.reset();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (info->NextEntryOffset == 0) {
|
||||
break;
|
||||
}
|
||||
offset += info->NextEntryOffset;
|
||||
}
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
std::vector<std::byte> buffer(kWatchBufferBytes);
|
||||
while (!stop.load(std::memory_order_acquire)) {
|
||||
if (rescan_requested.exchange(false)) {
|
||||
std::lock_guard lock{mutex};
|
||||
queue.clear();
|
||||
rescan_emitted = false;
|
||||
}
|
||||
DWORD transferred = 0;
|
||||
const BOOL ok = ::ReadDirectoryChangesW(
|
||||
directory, buffer.data(), static_cast<DWORD>(buffer.size()),
|
||||
TRUE, kNotifyFilter, &transferred, nullptr, nullptr);
|
||||
if (stop.load(std::memory_order_acquire)) {
|
||||
break;
|
||||
}
|
||||
if (ok == 0) {
|
||||
const DWORD error = ::GetLastError();
|
||||
if (error == ERROR_OPERATION_ABORTED) {
|
||||
continue;
|
||||
}
|
||||
{
|
||||
std::lock_guard lock{mutex};
|
||||
push_event_locked(DirectoryEvent{
|
||||
.kind = DirectoryEventKind::RescanRequired,
|
||||
.path = root,
|
||||
.old_path = {},
|
||||
.is_directory = true,
|
||||
.detail = std::format(
|
||||
"DirectoryWatcher read failed ({})", error),
|
||||
});
|
||||
}
|
||||
changed.notify_all();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{50});
|
||||
continue;
|
||||
}
|
||||
if (transferred == 0) {
|
||||
continue;
|
||||
}
|
||||
{
|
||||
std::lock_guard lock{mutex};
|
||||
parse_buffer_locked(buffer.data(), transferred);
|
||||
}
|
||||
changed.notify_all();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
DirectoryWatcher::DirectoryWatcher(std::unique_ptr<Impl> impl) noexcept
|
||||
: impl_(std::move(impl))
|
||||
{
|
||||
}
|
||||
|
||||
DirectoryWatcher::~DirectoryWatcher() noexcept = default;
|
||||
DirectoryWatcher::DirectoryWatcher(DirectoryWatcher&&) noexcept = default;
|
||||
DirectoryWatcher&
|
||||
DirectoryWatcher::operator=(DirectoryWatcher&&) noexcept = default;
|
||||
|
||||
Void::Result::Result<DirectoryWatcher, Void::Result::Error>
|
||||
DirectoryWatcher::create(const std::filesystem::path& root,
|
||||
DirectoryWatcherOptions options)
|
||||
{
|
||||
std::error_code ec;
|
||||
if (!std::filesystem::is_directory(root, ec)) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("FileSystem::DirectoryWatcher::create: '{}' is not a "
|
||||
"directory",
|
||||
root.string()))};
|
||||
}
|
||||
|
||||
auto impl = std::make_unique<Impl>();
|
||||
impl->root = root;
|
||||
impl->options = std::move(options);
|
||||
impl->directory = ::CreateFileW(
|
||||
root.c_str(), FILE_LIST_DIRECTORY,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
|
||||
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
||||
if (impl->directory == INVALID_HANDLE_VALUE) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("FileSystem::DirectoryWatcher::create: cannot open "
|
||||
"'{}'",
|
||||
root.string()))};
|
||||
}
|
||||
impl->worker = std::thread{[raw = impl.get()] { raw->run(); }};
|
||||
return DirectoryWatcher{std::move(impl)};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<DirectoryEvent>, Void::Result::Error>
|
||||
DirectoryWatcher::poll(std::chrono::milliseconds timeout)
|
||||
{
|
||||
if (!impl_) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
"FileSystem::DirectoryWatcher::poll: moved-from watcher")};
|
||||
}
|
||||
std::unique_lock lock{impl_->mutex};
|
||||
impl_->changed.wait_for(
|
||||
lock, std::max(std::chrono::milliseconds{0}, timeout), [&] {
|
||||
return !impl_->queue.empty() || impl_->wake_requested;
|
||||
});
|
||||
impl_->wake_requested = false;
|
||||
return impl_->drain_locked();
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> DirectoryWatcher::rescan()
|
||||
{
|
||||
if (!impl_) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
"FileSystem::DirectoryWatcher::rescan: moved-from watcher")};
|
||||
}
|
||||
impl_->rescan_requested.store(true);
|
||||
// Unblock the blocking ReadDirectoryChangesW; the worker re-arms and
|
||||
// clears the queue on the next iteration.
|
||||
(void)::CancelIoEx(impl_->directory, nullptr);
|
||||
return {};
|
||||
}
|
||||
|
||||
void DirectoryWatcher::wake() noexcept
|
||||
{
|
||||
if (!impl_) {
|
||||
return;
|
||||
}
|
||||
{
|
||||
std::lock_guard lock{impl_->mutex};
|
||||
impl_->wake_requested = true;
|
||||
}
|
||||
impl_->changed.notify_all();
|
||||
}
|
||||
|
||||
void DirectoryWatcher::simulate_overflow_for_test()
|
||||
{
|
||||
if (!impl_) {
|
||||
return;
|
||||
}
|
||||
{
|
||||
std::lock_guard lock{impl_->mutex};
|
||||
impl_->push_event_locked(DirectoryEvent{
|
||||
.kind = DirectoryEventKind::RescanRequired,
|
||||
.path = impl_->root,
|
||||
.old_path = {},
|
||||
.is_directory = true,
|
||||
.detail = "simulated buffer overflow",
|
||||
});
|
||||
}
|
||||
impl_->changed.notify_all();
|
||||
}
|
||||
|
||||
const std::filesystem::path& DirectoryWatcher::root() const noexcept
|
||||
{
|
||||
static const std::filesystem::path kEmpty{};
|
||||
return impl_ ? impl_->root : kEmpty;
|
||||
}
|
||||
|
||||
std::size_t DirectoryWatcher::watch_count() const noexcept
|
||||
{
|
||||
if (!impl_) {
|
||||
return 0;
|
||||
}
|
||||
// One recursive handle covers the whole subtree.
|
||||
return impl_->directory == INVALID_HANDLE_VALUE ? 0 : 1;
|
||||
}
|
||||
|
||||
int DirectoryWatcher::native_handle() const noexcept
|
||||
{
|
||||
return impl_ ? static_cast<int>(
|
||||
reinterpret_cast<std::intptr_t>(impl_->directory))
|
||||
: -1;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct DirectoryWatcher::Impl {
|
||||
|
||||
Reference in new issue
Block a user