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.
This commit is contained in:
Francesco Abbate 2021-07-15 09:56:49 +02:00
parent 7b7dfe8c75
commit 865111738a
4 changed files with 20 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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