From 390f1a40bc7414284086b3361c8fa6b80536bd00 Mon Sep 17 00:00:00 2001 From: anth64 Date: Sat, 1 Nov 2025 18:20:16 +0100 Subject: [PATCH] add kqueue directory start/stop, check is still wip --- src/platform.c | 65 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/src/platform.c b/src/platform.c index 668b74b..b1cdeb6 100644 --- a/src/platform.c +++ b/src/platform.c @@ -1,15 +1,27 @@ #include #include -#ifdef __linux__ -#include -#include + +#if defined(__unix__) || defined(__APPLE__) +#include #include #endif -#ifdef _WIN32 +#ifdef __linux__ +#include +#elif defined(_WIN32) #include -#else -#include +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ + defined(__NetBSD__) +#include +#include +#include +#include + +typedef struct { + int kq; + int dir_fd; +} kqueue_handle; + #endif #define EVENT_BUFFER_SIZE 4096 @@ -70,6 +82,39 @@ void *platform_directory_watch_start(const char *path) if (handle == INVALID_HANDLE_VALUE) return NULL; + return (void *)handle; +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(OpenBSD) || \ + defined(__NetBSD__) + kqueue_handle *handle; + struct kevent change; + + handle = malloc(sizeof(kqueue_handle)); + if (!handle) + return NULL; + + handle->kq = kqueue(); + if (handle->kq < 0) { + free(handle); + return NULL; + } + + handle->dir_fd = open(path, O_RDONLY); + if (handle->dir_fd < 0) { + close(handle->kq); + free(handle); + return NULL; + } + + EV_SET(&change, handle->dir_fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, + NOTE_WRITE | NOTE_DELETE | NOTE_EXTEND | NOTE_RENAME, 0, 0); + + if (kevent(handle->kq, &change, 1, NULL, 0, NULL) < 0) { + close(handle->dir_fd); + close(handle->kq); + free(handle); + return NULL; + } + return (void *)handle; #else return NULL @@ -89,6 +134,14 @@ void platform_directory_watch_stop(void *handle) HANDLE h; h = (HANDLE)handle; CloseHandle(h); +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ + defined(__NetBSD__) + kqueue_handle *handle; + + handle = (kqueue_handle *)handle; + close(handle->dir_fd); + close(handle->kq); + free(handle); #else (void)handle; #endif