Report error codes from dmon_watch_add

This commit is contained in:
Francesco Abbate 2022-01-08 17:22:23 +01:00
parent 39366d3a09
commit f0aea5b1a4
3 changed files with 17 additions and 4 deletions

View File

@ -107,7 +107,8 @@ typedef enum dmon_error_enum {
DMON_ERROR_OPEN_DIR, DMON_ERROR_OPEN_DIR,
DMON_ERROR_MONITOR_FAIL, DMON_ERROR_MONITOR_FAIL,
DMON_ERROR_UNSUPPORTED_SYMLINK, DMON_ERROR_UNSUPPORTED_SYMLINK,
DMON_ERROR_END, DMON_ERROR_SUBDIR_LOCATION,
DMON_ERROR_END
} dmon_error; } dmon_error;
#ifdef __cplusplus #ifdef __cplusplus
@ -364,6 +365,7 @@ static const char *dmon__errors[] = {
"Error opening directory", "Error opening directory",
"Error enabling monitoring", "Error enabling monitoring",
"Error support for symlink disabled", "Error support for symlink disabled",
"Error not a subdirectory",
}; };
DMON_API_IMPL const char *dmon_error_str(dmon_error err) { DMON_API_IMPL const char *dmon_error_str(dmon_error err) {

View File

@ -27,7 +27,7 @@
extern "C" { extern "C" {
#endif #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); DMON_API_DECL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir);
#ifdef __cplusplus #ifdef __cplusplus
@ -36,7 +36,7 @@ DMON_API_DECL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir);
#ifdef DMON_IMPL #ifdef DMON_IMPL
#if DMON_OS_LINUX #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); 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__strcpy(fullpath, sizeof(fullpath), watch->rootdir);
dmon__strcat(fullpath, sizeof(fullpath), watchdir); dmon__strcat(fullpath, sizeof(fullpath), watchdir);
if (stat(fullpath, &st) != 0 || (st.st_mode & S_IFDIR) == 0) { if (stat(fullpath, &st) != 0 || (st.st_mode & S_IFDIR) == 0) {
*error_code = DMON_ERROR_UNSUPPORTED_SYMLINK;
if (!skip_lock) if (!skip_lock)
pthread_mutex_unlock(&_dmon.mutex); pthread_mutex_unlock(&_dmon.mutex);
return false; 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 // check that the directory is not already added
for (int i = 0, c = stb_sb_count(watch->subdirs); i < c; i++) { for (int i = 0, c = stb_sb_count(watch->subdirs); i < c; i++) {
if (strcmp(subdir.rootdir, watch->subdirs[i].rootdir) == 0) { if (strcmp(subdir.rootdir, watch->subdirs[i].rootdir) == 0) {
*error_code = DMON_ERROR_SUBDIR_LOCATION;
if (!skip_lock) if (!skip_lock)
pthread_mutex_unlock(&_dmon.mutex); pthread_mutex_unlock(&_dmon.mutex);
return false; 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); dmon__strcat(fullpath, sizeof(fullpath), subdir.rootdir);
int wd = inotify_add_watch(watch->fd, fullpath, inotify_mask); int wd = inotify_add_watch(watch->fd, fullpath, inotify_mask);
if (wd == -1) { if (wd == -1) {
*error_code = DMON_ERROR_WATCH_DIR;
if (!skip_lock) if (!skip_lock)
pthread_mutex_unlock(&_dmon.mutex); pthread_mutex_unlock(&_dmon.mutex);
return false; return false;
@ -136,6 +139,7 @@ DMON_API_IMPL bool dmon_watch_rm(dmon_watch_id id, const char* watchdir)
} }
} }
if (i >= c) { if (i >= c) {
*error_code = DMON_ERROR_SUBDIR_LOCATION;
if (!skip_lock) if (!skip_lock)
pthread_mutex_unlock(&_dmon.mutex); pthread_mutex_unlock(&_dmon.mutex);
return false; return false;

View File

@ -753,7 +753,14 @@ static int f_watch_dir_add(lua_State *L) {
dmon_watch_id watch_id; dmon_watch_id watch_id;
watch_id.id = luaL_checkinteger(L, 1); watch_id.id = luaL_checkinteger(L, 1);
const char *subdir = luaL_checkstring(L, 2); 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; return 1;
} }