Type and naming cleanup
Do not use Hi prefic for things that shouldn't be visible to outside code
This commit is contained in:
@@ -19,35 +19,36 @@
|
||||
#include "logger/logger.h"
|
||||
#include "types.h"
|
||||
|
||||
sc_array_def(hiFileEvent, fwevent);
|
||||
sc_array_def(FileEvent, fwevent);
|
||||
typedef struct sc_array_fwevent VectorFileEvent;
|
||||
|
||||
typedef struct hiFileWatcher {
|
||||
typedef struct FileWatcher {
|
||||
thrd_t thread;
|
||||
mtx_t mutex;
|
||||
cnd_t cond;
|
||||
int running;
|
||||
struct hiFileWatcherContext context;
|
||||
struct sc_array_fwevent events;
|
||||
} hiFileWatcher;
|
||||
FileWatcherContext context;
|
||||
VectorFileEvent events;
|
||||
} FileWatcher;
|
||||
|
||||
/*
|
||||
* Watch Params
|
||||
*/
|
||||
|
||||
static inline hiWatchParams watch_params_create(const char *path, u32 mask) {
|
||||
return (hiWatchParams){.path = strdup(path), .mask = mask};
|
||||
static inline WatchParams watch_params_create(const char *path, u32 mask) {
|
||||
return (WatchParams){.path = strdup(path), .mask = mask};
|
||||
}
|
||||
|
||||
static inline void watch_params_destroy(hiWatchParams *params) {
|
||||
static inline void watch_params_destroy(WatchParams *params) {
|
||||
free((char *)params->path);
|
||||
}
|
||||
|
||||
static hiWatchParams *watch_params_find_by_path(const char *path,
|
||||
hiFileWatcherContext *ctx,
|
||||
size_t *index) {
|
||||
static WatchParams *watch_params_find_by_path(const char *path,
|
||||
FileWatcherContext *ctx,
|
||||
size_t *index) {
|
||||
const struct sc_array_fwparam *params = &ctx->params;
|
||||
for (size_t i = 0; i < sc_array_size(params); ++i) {
|
||||
hiWatchParams *param = &sc_array_at(params, i);
|
||||
WatchParams *param = &sc_array_at(params, i);
|
||||
if (strcmp(path, param->path) == 0) {
|
||||
if (index)
|
||||
*index = i;
|
||||
@@ -61,17 +62,17 @@ static hiWatchParams *watch_params_find_by_path(const char *path,
|
||||
* File Events
|
||||
*/
|
||||
|
||||
#define NULL_EVENT (hiFileEvent){0};
|
||||
#define NULL_EVENT (FileEvent){0};
|
||||
|
||||
static hiFileEvent file_event_create(const struct inotify_event *inevent,
|
||||
hiFileWatcherContext *ctx) {
|
||||
hiFileEvent event = {0};
|
||||
static FileEvent file_event_create(const struct inotify_event *inevent,
|
||||
FileWatcherContext *ctx) {
|
||||
FileEvent event = {0};
|
||||
|
||||
if (!inevent) {
|
||||
return event;
|
||||
}
|
||||
|
||||
const hiFileWatch *fw = file_watch_find_by_wd(ctx, inevent->wd, NULL);
|
||||
const FileWatch *fw = file_watch_find_by_wd(ctx, inevent->wd, NULL);
|
||||
|
||||
if (has_mask(inevent->mask, IN_IGNORED)) {
|
||||
sc_log_debug("IN_IGNORED received on %s\n", fw->path);
|
||||
@@ -102,7 +103,7 @@ static hiFileEvent file_event_create(const struct inotify_event *inevent,
|
||||
if (strcmp(watched_file, filename) == 0) {
|
||||
|
||||
// check if event mask matches the watcher mask
|
||||
hiWatchParams *params =
|
||||
WatchParams *params =
|
||||
watch_params_find_by_path(watched_file, ctx, NULL);
|
||||
if (!params) {
|
||||
return NULL_EVENT;
|
||||
@@ -139,10 +140,10 @@ static hiFileEvent file_event_create(const struct inotify_event *inevent,
|
||||
return NULL_EVENT;
|
||||
}
|
||||
|
||||
hiFileEvent hi_file_event_pop(hiFileWatcher *fw) {
|
||||
FileEvent hi_file_event_pop(FileWatcher *fw) {
|
||||
mtx_lock(&fw->mutex);
|
||||
|
||||
hiFileEvent e = NULL_EVENT;
|
||||
FileEvent e = NULL_EVENT;
|
||||
if (sc_array_size(&fw->events) > 0) {
|
||||
e = sc_array_last(&fw->events);
|
||||
sc_array_del_last(&fw->events);
|
||||
@@ -156,19 +157,19 @@ hiFileEvent hi_file_event_pop(hiFileWatcher *fw) {
|
||||
* File Watcher
|
||||
*/
|
||||
|
||||
static HiloadResult file_watcher_ctx_init(hiFileWatcherContext *ctx) {
|
||||
static HiResult file_watcher_ctx_init(FileWatcherContext *ctx) {
|
||||
|
||||
ctx->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
|
||||
if (ctx->fd == -1) {
|
||||
return HILOAD_FAIL;
|
||||
return HI_FAIL;
|
||||
}
|
||||
|
||||
sc_array_init(&ctx->watches);
|
||||
sc_array_init(&ctx->params);
|
||||
return HILOAD_OK;
|
||||
return HI_OK;
|
||||
}
|
||||
|
||||
static void file_watcher_ctx_destroy(hiFileWatcherContext *ctx) {
|
||||
static void file_watcher_ctx_destroy(FileWatcherContext *ctx) {
|
||||
if (ctx) {
|
||||
file_watch_destroy_watches(ctx);
|
||||
}
|
||||
@@ -177,11 +178,11 @@ static void file_watcher_ctx_destroy(hiFileWatcherContext *ctx) {
|
||||
// Declare the thread func
|
||||
int file_watcher_watch(void *arg);
|
||||
|
||||
hiFileWatcher *hi_file_watcher_create() {
|
||||
FileWatcher *hi_file_watcher_create() {
|
||||
|
||||
// Allocate context
|
||||
hiFileWatcher *fw = malloc(sizeof(hiFileWatcher));
|
||||
memset(fw, 0, sizeof(hiFileWatcher));
|
||||
FileWatcher *fw = malloc(sizeof(FileWatcher));
|
||||
memset(fw, 0, sizeof(FileWatcher));
|
||||
|
||||
if (fw) {
|
||||
if (!HIOK(file_watcher_ctx_init(&fw->context))) {
|
||||
@@ -203,26 +204,25 @@ hiFileWatcher *hi_file_watcher_create() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HiloadResult hi_file_watcher_add(struct hiFileWatcher *fw, const char *filename,
|
||||
u32 flags) {
|
||||
HiResult hi_file_watcher_add(FileWatcher *fw, const char *filename, u32 flags) {
|
||||
if (!fw) {
|
||||
sc_log_error("Attempted to add file watcher for '%s' with null context\n",
|
||||
filename);
|
||||
return HILOAD_FAIL;
|
||||
return HI_FAIL;
|
||||
}
|
||||
|
||||
mtx_lock(&fw->mutex);
|
||||
|
||||
if (!HIOK(file_watch_add(&fw->context, flags, filename))) {
|
||||
mtx_unlock(&fw->mutex);
|
||||
return HILOAD_FAIL;
|
||||
return HI_FAIL;
|
||||
}
|
||||
{
|
||||
// Add watch param as well
|
||||
hiWatchParams *params =
|
||||
WatchParams *params =
|
||||
watch_params_find_by_path(filename, &fw->context, NULL);
|
||||
if (!params) {
|
||||
hiWatchParams params = watch_params_create(filename, flags);
|
||||
WatchParams params = watch_params_create(filename, flags);
|
||||
sc_array_add(&fw->context.params, params);
|
||||
} else {
|
||||
params->mask |= flags;
|
||||
@@ -230,42 +230,40 @@ HiloadResult hi_file_watcher_add(struct hiFileWatcher *fw, const char *filename,
|
||||
}
|
||||
|
||||
mtx_unlock(&fw->mutex);
|
||||
return HILOAD_OK;
|
||||
return HI_OK;
|
||||
}
|
||||
|
||||
HiloadResult hi_file_watcher_remove(struct hiFileWatcher *fw,
|
||||
const char *filename) {
|
||||
HiResult hi_file_watcher_remove(FileWatcher *fw, const char *filename) {
|
||||
if (!fw) {
|
||||
return HILOAD_FAIL;
|
||||
return HI_FAIL;
|
||||
}
|
||||
|
||||
mtx_lock(&fw->mutex);
|
||||
if (!HIOK(file_watch_remove(&fw->context, filename))) {
|
||||
mtx_unlock(&fw->mutex);
|
||||
return HILOAD_FAIL;
|
||||
return HI_FAIL;
|
||||
}
|
||||
|
||||
{
|
||||
// remove watchparam as well
|
||||
size_t i = 0;
|
||||
hiWatchParams *params =
|
||||
watch_params_find_by_path(filename, &fw->context, &i);
|
||||
WatchParams *params = watch_params_find_by_path(filename, &fw->context, &i);
|
||||
watch_params_destroy(params);
|
||||
sc_array_del(&fw->context.params, i);
|
||||
}
|
||||
|
||||
mtx_unlock(&fw->mutex);
|
||||
sc_log_info("Removed file: '%s' from watch, filename");
|
||||
return HILOAD_OK;
|
||||
return HI_OK;
|
||||
}
|
||||
|
||||
void hi_file_watcher_notify(hiFileWatcher *fw) {
|
||||
void hi_file_watcher_notify(FileWatcher *fw) {
|
||||
if (fw && fw->running) {
|
||||
cnd_signal(&fw->cond);
|
||||
}
|
||||
}
|
||||
|
||||
void hi_file_watcher_destroy(hiFileWatcher *fw) {
|
||||
void hi_file_watcher_destroy(FileWatcher *fw) {
|
||||
if (!fw)
|
||||
return;
|
||||
|
||||
@@ -280,7 +278,7 @@ void hi_file_watcher_destroy(hiFileWatcher *fw) {
|
||||
}
|
||||
|
||||
int file_watcher_watch(void *arg) {
|
||||
hiFileWatcher *ctx = (hiFileWatcher *)arg;
|
||||
FileWatcher *ctx = (FileWatcher *)arg;
|
||||
sc_log_set_thread_name("File Watcher");
|
||||
|
||||
char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
|
||||
@@ -303,7 +301,7 @@ int file_watcher_watch(void *arg) {
|
||||
ptr += sizeof(struct inotify_event) + event->len) {
|
||||
|
||||
event = (const struct inotify_event *)ptr;
|
||||
hiFileEvent e = file_event_create(event, &ctx->context);
|
||||
FileEvent e = file_event_create(event, &ctx->context);
|
||||
if (e.type != HI_FILE_NONE) {
|
||||
sc_array_add(&ctx->events, e);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user