Implement Linux directory watching using inotify
This commit is contained in:
@@ -60,3 +60,12 @@ static void stk_module_unload(size_t index)
|
|||||||
|
|
||||||
--module_count;
|
--module_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void stk_free_file_list(char **list, size_t count)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < count; ++i)
|
||||||
|
free(list[i]);
|
||||||
|
|
||||||
|
free(list);
|
||||||
|
}
|
||||||
|
|||||||
+112
@@ -1,3 +1,12 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#define PLATFORM_INOTIFY_EVENT_BUFFER_SIZE 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
@@ -30,3 +39,106 @@ void *platform_get_symbol(void *handle, const char *symbol)
|
|||||||
return dlsym(handle, symbol);
|
return dlsym(handle, symbol);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *platform_directory_watch_start(const char *path)
|
||||||
|
{
|
||||||
|
#ifdef __linux__
|
||||||
|
int fd;
|
||||||
|
int wd;
|
||||||
|
|
||||||
|
fd = inotify_init1(IN_NONBLOCK);
|
||||||
|
if (fd < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
wd = inotify_add_watch(fd, path,
|
||||||
|
IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO |
|
||||||
|
IN_MOVED_FROM);
|
||||||
|
if (wd < 0) {
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (void *)(intptr_t)fd;
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
return NULL
|
||||||
|
#else
|
||||||
|
return NULL
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void platform_directory_watch_stop(void *handle)
|
||||||
|
{
|
||||||
|
#ifdef __linux__
|
||||||
|
int fd;
|
||||||
|
if (!handle)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fd = (int)(intptr_t)handle;
|
||||||
|
close(fd);
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
(void)handle;
|
||||||
|
#else
|
||||||
|
(void)handle;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
char **platform_directory_watch_check(void *handle, size_t *out_count)
|
||||||
|
{
|
||||||
|
#ifdef __linux__
|
||||||
|
int fd;
|
||||||
|
char buffer[PLATFORM_INOTIFY_EVENT_BUFFER_SIZE];
|
||||||
|
ssize_t bytes_read;
|
||||||
|
struct inotify_event *event;
|
||||||
|
char *event_ptr;
|
||||||
|
size_t file_count, index;
|
||||||
|
char **file_list;
|
||||||
|
|
||||||
|
fd = (int)(intptr_t)handle;
|
||||||
|
bytes_read = read(fd, buffer, sizeof(buffer));
|
||||||
|
if (bytes_read <= 0) {
|
||||||
|
*out_count = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_count = 0;
|
||||||
|
event_ptr = buffer;
|
||||||
|
while (event_ptr < buffer + bytes_read) {
|
||||||
|
event = (struct inotify_event *)event_ptr;
|
||||||
|
if (event->len > 0)
|
||||||
|
++file_count;
|
||||||
|
|
||||||
|
event_ptr += sizeof(struct inotify_event) + event->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_count == 0) {
|
||||||
|
*out_count = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file_list = malloc(file_count * sizeof(char *));
|
||||||
|
if (!file_list) {
|
||||||
|
*out_count = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = 0;
|
||||||
|
event_ptr = buffer;
|
||||||
|
while (event_ptr < buffer + bytes_read) {
|
||||||
|
event = (struct inotify_event *)event_ptr;
|
||||||
|
if (event->len > 0) {
|
||||||
|
file_list[index] = malloc(strlen(event->name) + 1);
|
||||||
|
if (file_list[index]) {
|
||||||
|
strcpy(file_list[index], event->name);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
event_ptr += sizeof(struct inotify_event) + event->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_count = index;
|
||||||
|
return file_list;
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user