rename event buffer, add directory watching for windows (WIP/needs testing)
This commit is contained in:
+66
-4
@@ -4,7 +4,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define PLATFORM_INOTIFY_EVENT_BUFFER_SIZE 4096
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -13,6 +12,8 @@
|
|||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define EVENT_BUFFER_SIZE 4096
|
||||||
|
|
||||||
void *platform_load_library(const char *path)
|
void *platform_load_library(const char *path)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -60,7 +61,16 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
|
|
||||||
return (void *)(intptr_t)fd;
|
return (void *)(intptr_t)fd;
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
return NULL
|
HANDLE handle;
|
||||||
|
handle =
|
||||||
|
CreateFileA(path, FILE_LIST_DIRECTORY,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||||
|
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
||||||
|
|
||||||
|
if (handle == INVALID_HANDLE_VALUE)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return (void *)handle;
|
||||||
#else
|
#else
|
||||||
return NULL
|
return NULL
|
||||||
#endif
|
#endif
|
||||||
@@ -76,7 +86,9 @@ void platform_directory_watch_stop(void *handle)
|
|||||||
fd = (int)(intptr_t)handle;
|
fd = (int)(intptr_t)handle;
|
||||||
close(fd);
|
close(fd);
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
(void)handle;
|
HANDLE h;
|
||||||
|
h = (HANDLE)handle;
|
||||||
|
CloseHandle(h);
|
||||||
#else
|
#else
|
||||||
(void)handle;
|
(void)handle;
|
||||||
#endif
|
#endif
|
||||||
@@ -86,7 +98,7 @@ char **platform_directory_watch_check(void *handle, size_t *out_count)
|
|||||||
{
|
{
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
int fd;
|
int fd;
|
||||||
char buffer[PLATFORM_INOTIFY_EVENT_BUFFER_SIZE];
|
char buffer[EVENT_BUFFER_SIZE];
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
struct inotify_event *event;
|
struct inotify_event *event;
|
||||||
char *event_ptr;
|
char *event_ptr;
|
||||||
@@ -139,6 +151,56 @@ char **platform_directory_watch_check(void *handle, size_t *out_count)
|
|||||||
*out_count = index;
|
*out_count = index;
|
||||||
return file_list;
|
return file_list;
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
|
HANDLE h;
|
||||||
|
BYTE buffer[EVENT_BUFFER_SIZE];
|
||||||
|
DWORD bytes_returned;
|
||||||
|
FILE_NOTIFY_INFORMATION *info;
|
||||||
|
BYTE *event_ptr;
|
||||||
|
char **file_list;
|
||||||
|
size_t file_count, index;
|
||||||
|
int char_count;
|
||||||
|
BOOL result;
|
||||||
|
|
||||||
|
h = (HANDLE)handle;
|
||||||
|
|
||||||
|
result = ReadDirectoryChangesW(h, buffer, sizeof(buffer), FALSE,
|
||||||
|
FILE_NOTIFY_CHANGE_FILE_NAME |
|
||||||
|
FILE_NOTIFY_CHANGE_LAST_WRITE,
|
||||||
|
&bytes_returned, NULL, NULL);
|
||||||
|
if (!result || bytes_returned == 0) {
|
||||||
|
*out_count = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_count = 0;
|
||||||
|
event_ptr = buffer;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
info = (FILE_NOTIFY_INFORMATION *)event_ptr;
|
||||||
|
char_count = WideCharToMultiByte(
|
||||||
|
CP_UTF8, info->FileName,
|
||||||
|
info->FileNameLength / sizeof(WCHAR), NULL, 0, NULL, NULL);
|
||||||
|
|
||||||
|
if (char_count > 0) {
|
||||||
|
file_list[index] = malloc(char_count + 1);
|
||||||
|
if (file_list[index]) {
|
||||||
|
WideCharToMultiByte(
|
||||||
|
CP_UTF8, 0, info->FileName,
|
||||||
|
info->FileNameLength / sizeof(WCHAR),
|
||||||
|
file_list[index], char_count, NULL, NULL);
|
||||||
|
file_list[index][char_count] = '\0';
|
||||||
|
++index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (info->NextEntryOffset == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
event_ptr += info->NextEntryOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_count = index;
|
||||||
|
return file_list;
|
||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user