add kqueue directory start/stop, check is still wip

This commit is contained in:
2025-11-01 18:20:16 +01:00
parent f1c6372b3e
commit 390f1a40bc
+59 -6
View File
@@ -1,15 +1,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef __linux__
#include <stdint.h> #if defined(__unix__) || defined(__APPLE__)
#include <sys/inotify.h> #include <dlfcn.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef _WIN32 #ifdef __linux__
#include <sys/inotify.h>
#elif defined(_WIN32)
#include <windows.h> #include <windows.h>
#else #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
#include <dlfcn.h> defined(__NetBSD__)
#include <fcntl.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/types.h>
typedef struct {
int kq;
int dir_fd;
} kqueue_handle;
#endif #endif
#define EVENT_BUFFER_SIZE 4096 #define EVENT_BUFFER_SIZE 4096
@@ -70,6 +82,39 @@ void *platform_directory_watch_start(const char *path)
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return NULL; 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; return (void *)handle;
#else #else
return NULL return NULL
@@ -89,6 +134,14 @@ void platform_directory_watch_stop(void *handle)
HANDLE h; HANDLE h;
h = (HANDLE)handle; h = (HANDLE)handle;
CloseHandle(h); 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 #else
(void)handle; (void)handle;
#endif #endif