fix(platform): harden linux watch check and clean up whitespace
This commit is contained in:
+22
-21
@@ -117,8 +117,10 @@ int platform_copy_file(const char *from, const char *to)
|
|||||||
cleanup:
|
cleanup:
|
||||||
if (src)
|
if (src)
|
||||||
fclose(src);
|
fclose(src);
|
||||||
|
|
||||||
if (dst)
|
if (dst)
|
||||||
fclose(dst);
|
fclose(dst);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -269,6 +271,7 @@ char (*platform_directory_init_scan(const char *dir_path, size_t *out_count))
|
|||||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||||
if (!is_valid_module_file(e->d_name))
|
if (!is_valid_module_file(e->d_name))
|
||||||
goto count_loop;
|
goto count_loop;
|
||||||
|
|
||||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||||
goto count_loop;
|
goto count_loop;
|
||||||
|
|
||||||
@@ -292,6 +295,7 @@ char (*platform_directory_init_scan(const char *dir_path, size_t *out_count))
|
|||||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||||
if (!is_valid_module_file(e->d_name))
|
if (!is_valid_module_file(e->d_name))
|
||||||
goto fill_loop;
|
goto fill_loop;
|
||||||
|
|
||||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||||
goto fill_loop;
|
goto fill_loop;
|
||||||
|
|
||||||
@@ -323,6 +327,7 @@ static void update_watches(platform_watch_context_t *ctx)
|
|||||||
|
|
||||||
for (i = 0; i < ctx->watch.k.file_fd_count; i++)
|
for (i = 0; i < ctx->watch.k.file_fd_count; i++)
|
||||||
close(ctx->watch.k.file_fds[i]);
|
close(ctx->watch.k.file_fds[i]);
|
||||||
|
|
||||||
free(ctx->watch.k.file_fds);
|
free(ctx->watch.k.file_fds);
|
||||||
ctx->watch.k.file_fds = NULL;
|
ctx->watch.k.file_fds = NULL;
|
||||||
ctx->watch.k.file_fd_count = 0;
|
ctx->watch.k.file_fd_count = 0;
|
||||||
@@ -331,7 +336,6 @@ static void update_watches(platform_watch_context_t *ctx)
|
|||||||
EV_ADD | EV_ENABLE | EV_CLEAR,
|
EV_ADD | EV_ENABLE | EV_CLEAR,
|
||||||
NOTE_WRITE | NOTE_DELETE | NOTE_RENAME, 0, NULL);
|
NOTE_WRITE | NOTE_DELETE | NOTE_RENAME, 0, NULL);
|
||||||
kevent(ctx->watch.k.kq, &ev, 1, NULL, 0, NULL);
|
kevent(ctx->watch.k.kq, &ev, 1, NULL, 0, NULL);
|
||||||
|
|
||||||
d = opendir(ctx->path);
|
d = opendir(ctx->path);
|
||||||
if (!d)
|
if (!d)
|
||||||
return;
|
return;
|
||||||
@@ -340,10 +344,11 @@ count_loop:
|
|||||||
e = readdir(d);
|
e = readdir(d);
|
||||||
if (!e)
|
if (!e)
|
||||||
goto count_done;
|
goto count_done;
|
||||||
|
|
||||||
if (is_valid_module_file(e->d_name))
|
if (is_valid_module_file(e->d_name))
|
||||||
count++;
|
count++;
|
||||||
goto count_loop;
|
|
||||||
|
|
||||||
|
goto count_loop;
|
||||||
count_done:
|
count_done:
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@@ -354,7 +359,6 @@ count_done:
|
|||||||
|
|
||||||
rewinddir(d);
|
rewinddir(d);
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
scan_loop:
|
scan_loop:
|
||||||
e = readdir(d);
|
e = readdir(d);
|
||||||
if (!e || i >= count)
|
if (!e || i >= count)
|
||||||
@@ -369,17 +373,13 @@ scan_loop:
|
|||||||
goto scan_loop;
|
goto scan_loop;
|
||||||
|
|
||||||
new_fds[i++] = fd;
|
new_fds[i++] = fd;
|
||||||
|
|
||||||
EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
|
EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
|
||||||
NOTE_WRITE | NOTE_ATTRIB, 0, NULL);
|
NOTE_WRITE | NOTE_ATTRIB, 0, NULL);
|
||||||
kevent(ctx->watch.k.kq, &ev, 1, NULL, 0, NULL);
|
kevent(ctx->watch.k.kq, &ev, 1, NULL, 0, NULL);
|
||||||
|
|
||||||
goto scan_loop;
|
goto scan_loop;
|
||||||
|
|
||||||
scan_done:
|
scan_done:
|
||||||
ctx->watch.k.file_fds = new_fds;
|
ctx->watch.k.file_fds = new_fds;
|
||||||
ctx->watch.k.file_fd_count = i;
|
ctx->watch.k.file_fd_count = i;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
closedir(d);
|
closedir(d);
|
||||||
}
|
}
|
||||||
@@ -391,6 +391,7 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
int fd = inotify_init1(IN_NONBLOCK);
|
int fd = inotify_init1(IN_NONBLOCK);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
inotify_add_watch(
|
inotify_add_watch(
|
||||||
fd, path, IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
|
fd, path, IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
|
||||||
return (void *)(long)fd;
|
return (void *)(long)fd;
|
||||||
@@ -411,8 +412,8 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
calloc(1, sizeof(platform_watch_context_t));
|
calloc(1, sizeof(platform_watch_context_t));
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
strncpy(ctx->path, path, STK_PATH_MAX - 1);
|
|
||||||
|
|
||||||
|
strncpy(ctx->path, path, STK_PATH_MAX - 1);
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ctx->watch.change_handle =
|
ctx->watch.change_handle =
|
||||||
CreateFileA(path, FILE_LIST_DIRECTORY,
|
CreateFileA(path, FILE_LIST_DIRECTORY,
|
||||||
@@ -434,7 +435,6 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
} while (FindNextFileA(h, &fd));
|
} while (FindNextFileA(h, &fd));
|
||||||
|
|
||||||
FindClose(h);
|
FindClose(h);
|
||||||
|
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
@@ -463,7 +463,6 @@ void *platform_directory_watch_start(const char *path)
|
|||||||
|
|
||||||
ctx->watch.k.kq = kqueue();
|
ctx->watch.k.kq = kqueue();
|
||||||
ctx->watch.k.dir_fd = open(path, O_RDONLY);
|
ctx->watch.k.dir_fd = open(path, O_RDONLY);
|
||||||
|
|
||||||
d = opendir(path);
|
d = opendir(path);
|
||||||
if (!d)
|
if (!d)
|
||||||
goto bsd_setup;
|
goto bsd_setup;
|
||||||
@@ -476,6 +475,7 @@ bsd_count_loop:
|
|||||||
sprintf(f, "%s/%s", path, e->d_name);
|
sprintf(f, "%s/%s", path, e->d_name);
|
||||||
if (!is_valid_module_file(e->d_name))
|
if (!is_valid_module_file(e->d_name))
|
||||||
goto bsd_count_loop;
|
goto bsd_count_loop;
|
||||||
|
|
||||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||||
goto bsd_count_loop;
|
goto bsd_count_loop;
|
||||||
|
|
||||||
@@ -491,7 +491,6 @@ bsd_count_done:
|
|||||||
goto bsd_setup;
|
goto bsd_setup;
|
||||||
|
|
||||||
rewinddir(d);
|
rewinddir(d);
|
||||||
|
|
||||||
bsd_scan_loop:
|
bsd_scan_loop:
|
||||||
e = readdir(d);
|
e = readdir(d);
|
||||||
if (!e || i >= count)
|
if (!e || i >= count)
|
||||||
@@ -500,6 +499,7 @@ bsd_scan_loop:
|
|||||||
sprintf(f, "%s/%s", path, e->d_name);
|
sprintf(f, "%s/%s", path, e->d_name);
|
||||||
if (!is_valid_module_file(e->d_name))
|
if (!is_valid_module_file(e->d_name))
|
||||||
goto bsd_scan_loop;
|
goto bsd_scan_loop;
|
||||||
|
|
||||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||||
goto bsd_scan_loop;
|
goto bsd_scan_loop;
|
||||||
|
|
||||||
@@ -511,7 +511,6 @@ bsd_scan_loop:
|
|||||||
bsd_scan_done:
|
bsd_scan_done:
|
||||||
closedir(d);
|
closedir(d);
|
||||||
ctx->count = i;
|
ctx->count = i;
|
||||||
|
|
||||||
bsd_setup:
|
bsd_setup:
|
||||||
update_watches(ctx);
|
update_watches(ctx);
|
||||||
#endif
|
#endif
|
||||||
@@ -535,7 +534,6 @@ void platform_directory_watch_stop(void *handle)
|
|||||||
platform_watch_context_t *ctx = (platform_watch_context_t *)handle;
|
platform_watch_context_t *ctx = (platform_watch_context_t *)handle;
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CloseHandle(ctx->watch.change_handle);
|
CloseHandle(ctx->watch.change_handle);
|
||||||
#else
|
#else
|
||||||
@@ -585,10 +583,14 @@ stk_module_event_t *platform_directory_watch_check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
evs = malloc(count * sizeof(stk_module_event_t));
|
evs = malloc(count * sizeof(stk_module_event_t));
|
||||||
|
if (!evs) {
|
||||||
|
*out_count = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
*file_list = malloc(count * sizeof(**file_list));
|
*file_list = malloc(count * sizeof(**file_list));
|
||||||
if (!evs || !*file_list) {
|
if (!*file_list) {
|
||||||
free(evs);
|
free(evs);
|
||||||
free(*file_list);
|
|
||||||
*out_count = 0;
|
*out_count = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -633,8 +635,10 @@ stk_module_event_t *platform_directory_watch_check(
|
|||||||
else
|
else
|
||||||
event_type = STK_MOD_LOAD;
|
event_type = STK_MOD_LOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
evs[idx++] = event_type;
|
evs[idx++] = event_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr += sizeof(struct inotify_event) + e->len;
|
ptr += sizeof(struct inotify_event) + e->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -647,7 +651,6 @@ stk_module_event_t *platform_directory_watch_check(
|
|||||||
size_t new_count = 0, i, j, ev_idx = 0;
|
size_t new_count = 0, i, j, ev_idx = 0;
|
||||||
stk_module_event_t *evs = NULL;
|
stk_module_event_t *evs = NULL;
|
||||||
int found;
|
int found;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WIN32_FIND_DATAA fd;
|
WIN32_FIND_DATAA fd;
|
||||||
HANDLE h;
|
HANDLE h;
|
||||||
@@ -666,7 +669,6 @@ stk_module_event_t *platform_directory_watch_check(
|
|||||||
} while (FindNextFileA(h, &fd));
|
} while (FindNextFileA(h, &fd));
|
||||||
|
|
||||||
FindClose(h);
|
FindClose(h);
|
||||||
|
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
goto build_diff;
|
goto build_diff;
|
||||||
|
|
||||||
@@ -692,7 +694,6 @@ stk_module_event_t *platform_directory_watch_check(
|
|||||||
} while (FindNextFileA(h, &fd));
|
} while (FindNextFileA(h, &fd));
|
||||||
|
|
||||||
FindClose(h);
|
FindClose(h);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
struct kevent kev;
|
struct kevent kev;
|
||||||
struct timespec ts = {0, 0};
|
struct timespec ts = {0, 0};
|
||||||
@@ -737,7 +738,6 @@ bsd_count_done:
|
|||||||
}
|
}
|
||||||
|
|
||||||
rewinddir(d);
|
rewinddir(d);
|
||||||
|
|
||||||
bsd_snap_loop:
|
bsd_snap_loop:
|
||||||
e = readdir(d);
|
e = readdir(d);
|
||||||
if (!e || new_count >= count)
|
if (!e || new_count >= count)
|
||||||
@@ -754,10 +754,8 @@ bsd_snap_loop:
|
|||||||
new_snaps[new_count].mtime = st.st_mtime;
|
new_snaps[new_count].mtime = st.st_mtime;
|
||||||
new_count++;
|
new_count++;
|
||||||
goto bsd_snap_loop;
|
goto bsd_snap_loop;
|
||||||
|
|
||||||
bsd_snap_done:
|
bsd_snap_done:
|
||||||
closedir(d);
|
closedir(d);
|
||||||
|
|
||||||
bsd_update:
|
bsd_update:
|
||||||
update_watches(ctx);
|
update_watches(ctx);
|
||||||
#endif
|
#endif
|
||||||
@@ -802,6 +800,7 @@ build_diff:
|
|||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
strncpy((*file_list)[ev_idx], ctx->snaps[i].filename,
|
strncpy((*file_list)[ev_idx], ctx->snaps[i].filename,
|
||||||
STK_PATH_MAX - 1);
|
STK_PATH_MAX - 1);
|
||||||
@@ -818,6 +817,7 @@ build_diff:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
strncpy((*file_list)[ev_idx], new_snaps[j].filename,
|
strncpy((*file_list)[ev_idx], new_snaps[j].filename,
|
||||||
STK_PATH_MAX - 1);
|
STK_PATH_MAX - 1);
|
||||||
@@ -837,6 +837,7 @@ build_diff:
|
|||||||
cleanup_error:
|
cleanup_error:
|
||||||
if (evs)
|
if (evs)
|
||||||
free(evs);
|
free(evs);
|
||||||
|
|
||||||
if (*file_list)
|
if (*file_list)
|
||||||
free(*file_list);
|
free(*file_list);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user