fix some path handling issues

This commit is contained in:
2025-04-12 13:58:59 +03:00
parent b63261a739
commit 6f3c76b005
4 changed files with 32 additions and 12 deletions

View File

@@ -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_

View File

@@ -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;

View File

@@ -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

View File

@@ -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;