Fix Linux inotify event handling and simplify module events
- Simplify to STK_MOD_LOAD/STK_MOD_UNLOAD event types - Replace IN_CREATE/IN_MODIFY with IN_CLOSE_WRITE to wait for complete writes - Update watch flags to IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM
This commit is contained in:
+1
-6
@@ -11,12 +11,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum {
|
typedef enum { STK_MOD_LOAD, STK_MOD_UNLOAD } stk_module_event_t;
|
||||||
STK_FILE_CREATED,
|
|
||||||
STK_FILE_MODIFIED,
|
|
||||||
STK_FILE_DELETED,
|
|
||||||
STK_FILE_RENAMED
|
|
||||||
} stk_file_event_t;
|
|
||||||
|
|
||||||
int stk_init(const char *mod_dir);
|
int stk_init(const char *mod_dir);
|
||||||
int stk_shutdown(void);
|
int stk_shutdown(void);
|
||||||
|
|||||||
+12
-21
@@ -52,9 +52,9 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
wd = inotify_add_watch(fd, path,
|
wd = inotify_add_watch(
|
||||||
IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO |
|
fd, path, IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
|
||||||
IN_MOVED_FROM);
|
|
||||||
if (wd < 0) {
|
if (wd < 0) {
|
||||||
close(fd);
|
close(fd);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -95,11 +95,11 @@ void platform_directory_watch_stop(void *handle)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
stk_file_event_t *platform_directory_watch_check(void *handle,
|
stk_module_event_t *platform_directory_watch_check(void *handle,
|
||||||
char ***file_list,
|
char ***file_list,
|
||||||
size_t *out_count)
|
size_t *out_count)
|
||||||
{
|
{
|
||||||
stk_file_event_t *events = NULL;
|
stk_module_event_t *events = NULL;
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
int fd;
|
int fd;
|
||||||
char buffer[EVENT_BUFFER_SIZE];
|
char buffer[EVENT_BUFFER_SIZE];
|
||||||
@@ -130,7 +130,7 @@ stk_file_event_t *platform_directory_watch_check(void *handle,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
events = malloc(file_count * sizeof(stk_file_event_t));
|
events = malloc(file_count * sizeof(stk_module_event_t));
|
||||||
*file_list = malloc(file_count * sizeof(char *));
|
*file_list = malloc(file_count * sizeof(char *));
|
||||||
if (!events || !*file_list) {
|
if (!events || !*file_list) {
|
||||||
free(events);
|
free(events);
|
||||||
@@ -144,19 +144,10 @@ stk_file_event_t *platform_directory_watch_check(void *handle,
|
|||||||
while (event_ptr < buffer + bytes_read) {
|
while (event_ptr < buffer + bytes_read) {
|
||||||
event = (struct inotify_event *)event_ptr;
|
event = (struct inotify_event *)event_ptr;
|
||||||
if (event->len > 0) {
|
if (event->len > 0) {
|
||||||
switch (event->mask) {
|
events[index] =
|
||||||
case IN_CREATE:
|
(event->mask & (IN_CLOSE_WRITE | IN_MOVED_TO))
|
||||||
case IN_MOVED_TO:
|
? STK_MOD_LOAD
|
||||||
events[index] = STK_FILE_CREATED;
|
: STK_MOD_UNLOAD;
|
||||||
break;
|
|
||||||
case IN_MODIFY:
|
|
||||||
events[index] = STK_FILE_MODIFIED;
|
|
||||||
break;
|
|
||||||
case IN_DELETE:
|
|
||||||
case IN_MOVED_FROM:
|
|
||||||
events[index] = STK_FILE_DELETED;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
(*file_list)[index] = malloc(strlen(event->name) + 1);
|
(*file_list)[index] = malloc(strlen(event->name) + 1);
|
||||||
if ((*file_list)[index]) {
|
if ((*file_list)[index]) {
|
||||||
strcpy((*file_list)[index], event->name);
|
strcpy((*file_list)[index], event->name);
|
||||||
|
|||||||
Reference in New Issue
Block a user