make filewatcher more robust

This commit is contained in:
2025-04-12 01:15:30 +03:00
parent c9205336c1
commit b63261a739
14 changed files with 543 additions and 287 deletions

193
src/filewatcher/filewatch.c Normal file
View File

@@ -0,0 +1,193 @@
#include "filewatch.h"
#include "common.h"
#include "filewatch_context.h"
#include "filewatch_type.h"
#include <errno.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
/*
* File Watch
*/
struct WatchMaskParams {
u32 file_mask;
u32 parent_mask;
};
static inline struct WatchMaskParams inode_watch_masks_create(u32 flags) {
u32 file_mask = 0u;
u32 parent_mask = 0u;
if (has_mask(flags, HI_FILE_MODIFY)) {
file_mask |= IN_MODIFY;
}
if (has_mask(flags, HI_FILE_CREATE)) {
parent_mask |= IN_CREATE;
}
if (has_mask(flags, HI_FILE_DELETE)) {
file_mask |= IN_DELETE_SELF;
parent_mask |= IN_DELETE;
}
if (has_mask(flags, HI_FILE_MOVE)) {
file_mask |= IN_MOVE_SELF;
parent_mask |= IN_MOVED_TO;
}
// Append if watch exists
file_mask |= IN_MASK_ADD;
parent_mask |= IN_MASK_ADD;
return (struct WatchMaskParams){.file_mask = file_mask,
.parent_mask = parent_mask};
}
static void file_watch_destroy(int fd, hiFileWatch *fw) {
inotify_rm_watch(fd, fw->wd);
free((void *)fw->path);
fw->wd = -1;
fw->path = NULL;
}
void file_watch_destroy_watches(struct hiFileWatcherContext *ctx) {
for (size_t i = 0; i < sc_array_size(&ctx->watches); i++) {
hiFileWatch *fw = &sc_array_at(&ctx->watches, i);
file_watch_destroy(ctx->fd, fw);
}
sc_array_term(&ctx->watches);
}
hiFileWatch *file_watch_find_by_wd(struct hiFileWatcherContext *ctx, int wd,
size_t *index) {
struct sc_array_fwatch *watches = &ctx->watches;
for (size_t i = 0; i < sc_array_size(watches); ++i) {
hiFileWatch *fw = &sc_array_at(watches, i);
if (fw->wd == wd) {
if (index) {
*index = i;
}
return fw;
}
}
return NULL;
}
hiFileWatch *file_watch_find_by_path(struct hiFileWatcherContext *ctx,
const char *path, size_t *index) {
for (size_t i = 0; i < sc_array_size(&ctx->watches); i++) {
hiFileWatch *watch = &sc_array_at(&ctx->watches, i);
if (strcmp(watch->path, path) == 0) {
if (index) {
*index = i;
}
return watch;
}
}
return NULL;
}
/**
* Return null terminated char array with the parent, or NULL if no path
* separator was found.
* */
static char *get_parent_folder(const char path[static 1]) {
const char *last_separator = strrchr(path, '/');
if (!last_separator)
return NULL;
size_t length = last_separator - path;
char *parent = calloc(length, sizeof(char));
strncpy(parent, path, length);
parent[length] = '\0';
return parent;
}
HiloadResult file_watch_add(hiFileWatcherContext *ctx, u32 mask,
const char *path) {
if (!ctx || ctx->fd == -1) {
sc_log_error("Invalid inotify context\n");
return HILOAD_FAIL;
}
struct WatchMaskParams params = inode_watch_masks_create(mask);
hiFileWatch *watch = file_watch_find_by_path(ctx, path, NULL);
if (!watch) {
int wd = inotify_add_watch(ctx->fd, path, params.file_mask);
if (wd == -1) {
sc_log_error("Couldn't watch: %s: %s\n", path, strerror(errno));
return HILOAD_FAIL;
}
hiFileWatch wp = {.wd = wd, .mask = mask, .path = strdup(path)};
sc_array_add(&ctx->watches, wp);
watch = &sc_array_last(&ctx->watches);
} else {
inotify_add_watch(ctx->fd, path, params.file_mask);
}
if (!has_mask(HI_FILE_PARENT, mask)) {
char *parent = get_parent_folder(path);
hiFileWatch *parent_watch = file_watch_find_by_path(ctx, parent, NULL);
if (!parent_watch) {
// parent not yet watched
int wd = inotify_add_watch(ctx->fd, parent, params.parent_mask);
hiFileWatch wp = {.wd = wd, .mask = HI_FILE_PARENT, .path = parent};
sc_array_init(&wp.files);
sc_array_add(&wp.files, strdup(watch->path));
sc_array_add(&ctx->watches, wp);
} else {
free(parent);
inotify_add_watch(ctx->fd, parent_watch->path, params.parent_mask);
{
// Make sure we don't add a duplicate if the file is already marked
bool exists = false;
for (size_t i = 0; i < sc_array_size(&parent_watch->files); i++) {
const char *file = sc_array_at(&parent_watch->files, i);
// we store the pointer, so this should be fine
if (file == watch->path) {
exists = true;
break;
}
}
if (!exists) {
sc_array_add(&parent_watch->files, watch->path);
}
}
}
}
return HILOAD_OK;
}
HiloadResult file_watch_remove(hiFileWatcherContext *ctx, const char *path) {
size_t i = 0;
hiFileWatch *watch = file_watch_find_by_path(ctx, path, &i);
if (watch) {
// destroy parent reference. We assume it will only have one.
char *parent = get_parent_folder(path);
if (parent) {
hiFileWatch *pw = file_watch_find_by_path(ctx, parent, NULL);
if (pw) {
for (size_t i = 0; i < sc_array_size(&pw->files); i++) {
const char *fn = sc_array_at(&pw->files, i);
if (fn == watch->path) {
sc_array_del(&pw->files, i);
break;
}
}
}
free(parent);
}
file_watch_destroy(ctx->fd, watch);
sc_array_del(&ctx->watches, i);
return HILOAD_OK;
}
return HILOAD_FAIL;
}