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:
parent
3e6b9fedfe
commit
3994c3ac42
|
@ -340,7 +340,7 @@ local function project_scan_add_file(watch_id, filepath)
|
||||||
project_dir_entry = core.project_directories[i]
|
project_dir_entry = core.project_directories[i]
|
||||||
end
|
end
|
||||||
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 size_limit = config.file_size_limit * 10e5
|
||||||
local fileinfo = get_project_file_info(project_dir_entry.name, PATHSEP .. filepath, size_limit)
|
local fileinfo = get_project_file_info(project_dir_entry.name, PATHSEP .. filepath, size_limit)
|
||||||
local index, match = file_search(project_dir_entry.files, fileinfo)
|
local index, match = file_search(project_dir_entry.files, fileinfo)
|
||||||
|
|
|
@ -646,20 +646,11 @@ static int f_set_window_opacity(lua_State *L) {
|
||||||
return 1;
|
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) {
|
static int f_watch_dir(lua_State *L) {
|
||||||
const char *path = luaL_checkstring(L, 1);
|
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, dirmonitor_watch_callback, DMON_WATCHFLAGS_RECURSIVE, NULL);
|
||||||
dmon_watch_id watch_id = dmon_watch(path, watch_callback, DMON_WATCHFLAGS_RECURSIVE, NULL);
|
if (watch_id.id == 0) { luaL_error(L, "directory monitoring watch failed"); }
|
||||||
lua_pushnumber(L, watch_id.id);
|
lua_pushnumber(L, watch_id.id);
|
||||||
// FIXME: we ignore if there is an error.
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -669,7 +660,8 @@ static int f_watch_dir(lua_State *L) {
|
||||||
#define PATHSEP '/'
|
#define PATHSEP '/'
|
||||||
#endif
|
#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) {
|
static int f_path_compare(lua_State *L) {
|
||||||
const char *path1 = luaL_checkstring(L, 1);
|
const char *path1 = luaL_checkstring(L, 1);
|
||||||
const char *type1_s = luaL_checkstring(L, 2);
|
const char *type1_s = luaL_checkstring(L, 2);
|
||||||
|
@ -678,20 +670,25 @@ static int f_path_compare(lua_State *L) {
|
||||||
const int len1 = strlen(path1), len2 = strlen(path2);
|
const int len1 = strlen(path1), len2 = strlen(path2);
|
||||||
int type1 = strcmp(type1_s, "dir") != 0;
|
int type1 = strcmp(type1_s, "dir") != 0;
|
||||||
int type2 = strcmp(type2_s, "dir") != 0;
|
int type2 = strcmp(type2_s, "dir") != 0;
|
||||||
|
/* Find the index of the common part of the path. */
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < len1 && i < len2; i++) {
|
for (i = 0; i < len1 && i < len2; i++) {
|
||||||
if (path1[i] != path2[i]) break;
|
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)) {
|
if (strchr(path1 + i, PATHSEP)) {
|
||||||
type1 = 0;
|
type1 = 0;
|
||||||
}
|
}
|
||||||
if (strchr(path2 + i, PATHSEP)) {
|
if (strchr(path2 + i, PATHSEP)) {
|
||||||
type2 = 0;
|
type2 = 0;
|
||||||
}
|
}
|
||||||
|
/* If types are different "dir" types comes before "file" types. */
|
||||||
if (type1 != type2) {
|
if (type1 != type2) {
|
||||||
lua_pushboolean(L, type1 < type2);
|
lua_pushboolean(L, type1 < type2);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
/* If types are the same compare the files' path alphabetically. */
|
||||||
lua_pushboolean(L, strcmp(path1 + i, path2 + i) < 0);
|
lua_pushboolean(L, strcmp(path1 + i, path2 + i) < 0);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const char *filepath) {
|
static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const char *filepath) {
|
||||||
SDL_Event ev;
|
SDL_Event ev;
|
||||||
const int size = strlen(filepath) + 1;
|
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);
|
char *new_filepath = malloc(size);
|
||||||
if (!new_filepath) return;
|
if (!new_filepath) return;
|
||||||
memcpy(new_filepath, filepath, size);
|
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
|
#endif
|
||||||
SDL_zero(ev);
|
SDL_zero(ev);
|
||||||
ev.type = SDL_USEREVENT;
|
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.code = ((watch_id.id & 0xffff) << 16) | (action & 0xffff);
|
||||||
ev.user.data1 = new_filepath;
|
ev.user.data1 = new_filepath;
|
||||||
SDL_PushEvent(&ev);
|
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() {
|
void dirmonitor_init() {
|
||||||
dmon_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); */
|
/* sdl_dmon_event_type = SDL_RegisterEvents(1); */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,9 +41,11 @@ void dirmonitor_deinit() {
|
||||||
dmon_deinit();
|
dmon_deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath,
|
void dirmonitor_watch_callback(dmon_watch_id watch_id, dmon_action action, const char *rootdir,
|
||||||
const char *oldfilepath)
|
const char *filepath, const char *oldfilepath, void *user)
|
||||||
{
|
{
|
||||||
|
(void) rootdir;
|
||||||
|
(void) user;
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case DMON_ACTION_MOVE:
|
case DMON_ACTION_MOVE:
|
||||||
send_sdl_event(watch_id, DMON_ACTION_DELETE, oldfilepath);
|
send_sdl_event(watch_id, DMON_ACTION_DELETE, oldfilepath);
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
|
|
||||||
void dirmonitor_init();
|
void dirmonitor_init();
|
||||||
void dirmonitor_deinit();
|
void dirmonitor_deinit();
|
||||||
void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath,
|
void dirmonitor_watch_callback(dmon_watch_id watch_id, dmon_action action, const char *rootdir,
|
||||||
const char *oldfilepath);
|
const char *filepath, const char *oldfilepath, void *user);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue