From 6f3c76b0058438c1a545198c178808a883bed298 Mon Sep 17 00:00:00 2001 From: Kasper Date: Sat, 12 Apr 2025 13:58:59 +0300 Subject: [PATCH] fix some path handling issues --- src/common.h | 1 + src/filewatcher/filewatch.c | 6 ++++-- src/filewatcher/filewatcher.c | 25 +++++++++++++++++++++---- src/hiload.c | 12 ++++++------ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/common.h b/src/common.h index ac14777..f6282ea 100644 --- a/src/common.h +++ b/src/common.h @@ -6,6 +6,7 @@ static inline u32 has_mask(u32 flags, u32 mask) { return flags & mask; } +#define MIN(A, B) ((A) < (B) ? (A) : (B)) #define ARRLEN(A) (sizeof((A)) / (sizeof((A)[0]))) #endif // COMMON_H_ diff --git a/src/filewatcher/filewatch.c b/src/filewatcher/filewatch.c index 710037a..1f4632e 100644 --- a/src/filewatcher/filewatch.c +++ b/src/filewatcher/filewatch.c @@ -101,9 +101,11 @@ static char *get_parent_folder(const char path[static 1]) { if (!last_separator) return NULL; - size_t length = last_separator - path; + // add one for the trailing slash + size_t length = last_separator - path + 1; char *parent = calloc(length, sizeof(char)); strncpy(parent, path, length); + parent[length - 1] = '/'; parent[length] = '\0'; return parent; } @@ -148,7 +150,7 @@ HiloadResult file_watch_add(hiFileWatcherContext *ctx, u32 mask, 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 + // we store the pointer directly, so this should be fine :D if (file == watch->path) { exists = true; break; diff --git a/src/filewatcher/filewatcher.c b/src/filewatcher/filewatcher.c index 06efa0d..9999009 100644 --- a/src/filewatcher/filewatcher.c +++ b/src/filewatcher/filewatcher.c @@ -78,10 +78,27 @@ static hiFileEvent file_event_create(const struct inotify_event *inevent, } if (inevent->len) { +#define BUFSIZE 1024 + char filename[BUFSIZE]; + size_t parent_name_size = strlen(fw->path); + size_t event_path_size = strlen(inevent->name); + size_t fullpath_size = parent_name_size + event_path_size; + if (fullpath_size > BUFSIZE - 1) { + sc_log_error("Pathname length over %u, please compile with smaller " + "paths. Event discarded for %s\n.", BUFSIZE - 1, + inevent->name); + return NULL_EVENT; + } +#undef BUFSIZE + + strncpy(filename, fw->path, parent_name_size); + strncpy(filename + parent_name_size, inevent->name, event_path_size); + filename[fullpath_size] = '\0'; + // find if file is associated with any path for (size_t i = 0; i < sc_array_size(&fw->files); ++i) { const char *watched_file = sc_array_at(&fw->files, i); - if (strcmp(watched_file, inevent->name)) { + if (strcmp(watched_file, filename) == 0) { // check if event mask matches the watcher mask hiWatchParams *params = @@ -117,7 +134,6 @@ static hiFileEvent file_event_create(const struct inotify_event *inevent, return NULL_EVENT; } // Event on the folder itself - return NULL_EVENT; } @@ -287,8 +303,9 @@ int file_watcher_watch(void *arg) { sc_array_add(&ctx->events, e); if (event->len) { - sc_log_debug("Event created: queue-size: %u, %s: %s\n", sc_array_size(&ctx->events), - e.pathname, hi_file_watch_type_to_str(e.type)); + sc_log_debug("Event created: queue-size: %u, %s %s\n", + sc_array_size(&ctx->events), e.pathname, + hi_file_watch_type_to_str(e.type)); } } continue; // read again without waiting diff --git a/src/hiload.c b/src/hiload.c index bc54194..2ac1331 100644 --- a/src/hiload.c +++ b/src/hiload.c @@ -99,12 +99,8 @@ static void module_infos_free(ModuleInfos *modules) { for (size_t i = 0; i < modules->count; i++) { // Free char* before clearing the array const char *n = 0; - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-qual" - sc_array_foreach(&modules->names, n) { free((void *)n); } + sc_array_foreach(&modules->names, n) { free((char *)n); } sc_array_term(&modules->names); -#pragma GCC diagnostic pop // Use a destructor for the symbolinfos hi_free_symbol_info(&(sc_array_at(&modules->symbols, i))); @@ -175,12 +171,16 @@ static ReloadResult reload_solib(ModuleInfos *modules, const char *filename, } // Open the module with RTLD_NOW - void *new_handle = dlopen(fullpath, RTLD_NOW); + void *new_handle = dlopen(fullpath, RTLD_LAZY); if (!new_handle) { sc_log_error("Error reloading module: %s\n", dlerror()); return HI_RELOAD_OPEN_ERROR; } + if (new_handle != handle) { + sc_log_info("%s: changed\n", fullpath); + } + // Update the handle in our structure modules->handles.elems[index] = new_handle;