From 865111738aa22e94ccb2571c4f2c2c416426b2be Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 15 Jul 2021 09:56:49 +0200 Subject: [PATCH] Treat watch dir errors and fix various things Verity if dmon_watch returns an error. Add a check if an added file for which we received a create event is ignored based on the user's config. Add some explanatory comments in the code. --- data/core/init.lua | 2 +- src/api/system.c | 21 +++++++++------------ src/dirmonitor.c | 12 ++++++++---- src/dirmonitor.h | 4 ++-- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index f30c0853..beccc8df 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -336,7 +336,7 @@ local function project_scan_add_file(watch_id, filepath) project_dir_entry = core.project_directories[i] end end - if not project_dir_entry then return end + if not project_dir_entry or common.match_pattern(filepath, config.ignore_files) then return end local size_limit = config.file_size_limit * 10e5 local fileinfo = get_project_file_info(project_dir_entry.name, PATHSEP .. filepath, size_limit) local index, match = file_search(project_dir_entry.files, fileinfo) diff --git a/src/api/system.c b/src/api/system.c index 9ce20319..bf868087 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -660,20 +660,11 @@ static int f_set_window_opacity(lua_State *L) { return 1; } -static void watch_callback(dmon_watch_id watch_id, dmon_action action, const char* rootdir, - const char* filepath, const char* oldfilepath, void* user) -{ - (void)(user); - (void)(rootdir); - dirmonitor_push_event(watch_id, action, filepath, oldfilepath); -} - static int f_watch_dir(lua_State *L) { const char *path = luaL_checkstring(L, 1); - fprintf(stderr, "DEBUG: watching dir: %s\n", path); fflush(stderr); - dmon_watch_id watch_id = dmon_watch(path, watch_callback, DMON_WATCHFLAGS_RECURSIVE, NULL); + dmon_watch_id watch_id = dmon_watch(path, dirmonitor_watch_callback, DMON_WATCHFLAGS_RECURSIVE, NULL); + if (watch_id.id == 0) { luaL_error(L, "directory monitoring watch failed"); } lua_pushnumber(L, watch_id.id); - // FIXME: we ignore if there is an error. return 1; } @@ -683,7 +674,8 @@ static int f_watch_dir(lua_State *L) { #define PATHSEP '/' #endif - +/* Special purpose filepath compare function. Corresponds to the + order used in the TreeView view of the project's files. */ static int f_path_compare(lua_State *L) { const char *path1 = luaL_checkstring(L, 1); const char *type1_s = luaL_checkstring(L, 2); @@ -692,20 +684,25 @@ static int f_path_compare(lua_State *L) { const int len1 = strlen(path1), len2 = strlen(path2); int type1 = strcmp(type1_s, "dir") != 0; int type2 = strcmp(type2_s, "dir") != 0; + /* Find the index of the common part of the path. */ int i; for (i = 0; i < len1 && i < len2; i++) { if (path1[i] != path2[i]) break; } + /* If a path separator is present in the name after the common part we consider + the entry like a directory. */ if (strchr(path1 + i, PATHSEP)) { type1 = 0; } if (strchr(path2 + i, PATHSEP)) { type2 = 0; } + /* If types are different "dir" types comes before "file" types. */ if (type1 != type2) { lua_pushboolean(L, type1 < type2); return 1; } + /* If types are the same compare the files' path alphabetically. */ lua_pushboolean(L, strcmp(path1 + i, path2 + i) < 0); return 1; } diff --git a/src/dirmonitor.c b/src/dirmonitor.c index e94c7687..eb3b185f 100644 --- a/src/dirmonitor.c +++ b/src/dirmonitor.c @@ -11,6 +11,8 @@ static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const char *filepath) { SDL_Event ev; const int size = strlen(filepath) + 1; + /* The string allocated below should be deallocated as soon as the event is + treated in the SDL main loop. */ char *new_filepath = malloc(size); if (!new_filepath) return; memcpy(new_filepath, filepath, size); @@ -23,7 +25,6 @@ static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const cha #endif SDL_zero(ev); ev.type = SDL_USEREVENT; - fprintf(stderr, "DEBUG: send watch_id: %d action; %d\n", watch_id.id, action); fflush(stderr); ev.user.code = ((watch_id.id & 0xffff) << 16) | (action & 0xffff); ev.user.data1 = new_filepath; SDL_PushEvent(&ev); @@ -31,7 +32,8 @@ static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const cha void dirmonitor_init() { dmon_init(); - /* FIXME: not needed ? */ + /* In theory we should register our user event but since we + have just one type of user event this is not really needed. */ /* sdl_dmon_event_type = SDL_RegisterEvents(1); */ } @@ -39,9 +41,11 @@ void dirmonitor_deinit() { dmon_deinit(); } -void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath, - const char *oldfilepath) +void dirmonitor_watch_callback(dmon_watch_id watch_id, dmon_action action, const char *rootdir, + const char *filepath, const char *oldfilepath, void *user) { + (void) rootdir; + (void) user; switch (action) { case DMON_ACTION_MOVE: send_sdl_event(watch_id, DMON_ACTION_DELETE, oldfilepath); diff --git a/src/dirmonitor.h b/src/dirmonitor.h index 1fae4635..ab9376c0 100644 --- a/src/dirmonitor.h +++ b/src/dirmonitor.h @@ -7,8 +7,8 @@ void dirmonitor_init(); void dirmonitor_deinit(); -void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath, - const char *oldfilepath); +void dirmonitor_watch_callback(dmon_watch_id watch_id, dmon_action action, const char *rootdir, + const char *filepath, const char *oldfilepath, void *user); #endif