diff --git a/lib/dmon/dmon.h b/lib/dmon/dmon.h index 496463f1..1a7eed3b 100644 --- a/lib/dmon/dmon.h +++ b/lib/dmon/dmon.h @@ -107,7 +107,8 @@ typedef enum dmon_error_enum { DMON_ERROR_OPEN_DIR, DMON_ERROR_MONITOR_FAIL, DMON_ERROR_UNSUPPORTED_SYMLINK, - DMON_ERROR_END, + DMON_ERROR_SUBDIR_LOCATION, + DMON_ERROR_END } dmon_error; #ifdef __cplusplus @@ -364,6 +365,7 @@ static const char *dmon__errors[] = { "Error opening directory", "Error enabling monitoring", "Error support for symlink disabled", + "Error not a subdirectory", }; DMON_API_IMPL const char *dmon_error_str(dmon_error err) { diff --git a/lib/dmon/dmon_extra.h b/lib/dmon/dmon_extra.h index 9b201e17..2252c88e 100644 --- a/lib/dmon/dmon_extra.h +++ b/lib/dmon/dmon_extra.h @@ -27,7 +27,7 @@ extern "C" { #endif -DMON_API_DECL bool dmon_watch_add(dmon_watch_id id, const char* subdir); +DMON_API_DECL bool dmon_watch_add(dmon_watch_id id, const char* subdir, dmon_error *error_code); DMON_API_DECL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir); #ifdef __cplusplus @@ -36,7 +36,7 @@ DMON_API_DECL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir); #ifdef DMON_IMPL #if DMON_OS_LINUX -DMON_API_IMPL bool dmon_watch_add(dmon_watch_id id, const char* watchdir) +DMON_API_IMPL bool dmon_watch_add(dmon_watch_id id, const char* watchdir, dmon_error *error_code) { DMON_ASSERT(id.id > 0 && id.id <= DMON_MAX_WATCHES); @@ -64,6 +64,7 @@ DMON_API_IMPL bool dmon_watch_add(dmon_watch_id id, const char* watchdir) dmon__strcpy(fullpath, sizeof(fullpath), watch->rootdir); dmon__strcat(fullpath, sizeof(fullpath), watchdir); if (stat(fullpath, &st) != 0 || (st.st_mode & S_IFDIR) == 0) { + *error_code = DMON_ERROR_UNSUPPORTED_SYMLINK; if (!skip_lock) pthread_mutex_unlock(&_dmon.mutex); return false; @@ -80,6 +81,7 @@ DMON_API_IMPL bool dmon_watch_add(dmon_watch_id id, const char* watchdir) // check that the directory is not already added for (int i = 0, c = stb_sb_count(watch->subdirs); i < c; i++) { if (strcmp(subdir.rootdir, watch->subdirs[i].rootdir) == 0) { + *error_code = DMON_ERROR_SUBDIR_LOCATION; if (!skip_lock) pthread_mutex_unlock(&_dmon.mutex); return false; @@ -92,6 +94,7 @@ DMON_API_IMPL bool dmon_watch_add(dmon_watch_id id, const char* watchdir) dmon__strcat(fullpath, sizeof(fullpath), subdir.rootdir); int wd = inotify_add_watch(watch->fd, fullpath, inotify_mask); if (wd == -1) { + *error_code = DMON_ERROR_WATCH_DIR; if (!skip_lock) pthread_mutex_unlock(&_dmon.mutex); return false; @@ -136,6 +139,7 @@ DMON_API_IMPL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir) } } if (i >= c) { + *error_code = DMON_ERROR_SUBDIR_LOCATION; if (!skip_lock) pthread_mutex_unlock(&_dmon.mutex); return false; diff --git a/src/api/system.c b/src/api/system.c index 1a862f78..45776b51 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -753,7 +753,14 @@ static int f_watch_dir_add(lua_State *L) { dmon_watch_id watch_id; watch_id.id = luaL_checkinteger(L, 1); const char *subdir = luaL_checkstring(L, 2); - lua_pushboolean(L, dmon_watch_add(watch_id, subdir)); + dmon_error error_code; + int success = dmon_watch_add(watch_id, subdir, &error_code) + if (!success) { + lua_pushboolean(L, 0); + lua_pushstring(L, dmon_error_str(error_code)); + return 2; + } + lua_pushboolean(L, 1); return 1; }