Revised error reporting in autoreload_inotify

No repeated error messages after failed initialization. No error messages on
failed inotify_rm_watch().
This commit is contained in:
Bert Münnich 2017-05-17 20:13:32 +02:00
parent 9ac8fc62df
commit 8bce80fdae
1 changed files with 11 additions and 20 deletions

View File

@ -27,20 +27,16 @@
CLEANUP void arl_cleanup(arl_t *arl) CLEANUP void arl_cleanup(arl_t *arl)
{ {
if (arl->fd != -1 && arl->wd != -1) { if (arl->fd != -1 && arl->wd != -1)
if (inotify_rm_watch(arl->fd, arl->wd)) inotify_rm_watch(arl->fd, arl->wd);
error(0, 0, "Failed to remove inotify watch.");
}
} }
static void arl_setup_dir(arl_t *arl, const char *filepath) static void arl_setup_dir(arl_t *arl, const char *filepath)
{ {
char *dntmp, *dn; char *dntmp, *dn;
if (arl->fd == -1) { if (arl->fd == -1)
error(0, 0, "Uninitialized, could not add inotify watch on directory.");
return; return;
}
/* get dirname */ /* get dirname */
dntmp = (char*) strdup(filepath); dntmp = (char*) strdup(filepath);
@ -52,7 +48,7 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
*/ */
arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE); arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
if (arl->wd == -1) if (arl->wd == -1)
error(0, 0, "Failed to add inotify watch on directory '%s'.", dn); error(0, 0, "%s: Error watching directory", dn);
else else
arl->watching_dir = true; arl->watching_dir = true;
@ -68,10 +64,9 @@ bool arl_handle(arl_t *arl, const char *filepath)
ssize_t len = read(arl->fd, buf, sizeof(buf)); ssize_t len = read(arl->fd, buf, sizeof(buf));
if (len == -1) { if (len == -1)
error(0, 0, "Failed to read inotify events.");
return false; return false;
}
for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) { for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
event = (const struct inotify_event*) ptr; event = (const struct inotify_event*) ptr;
@ -92,8 +87,7 @@ bool arl_handle(arl_t *arl, const char *filepath)
/* cleanup, this has not been one-shot */ /* cleanup, this has not been one-shot */
if (arl->watching_dir) { if (arl->watching_dir) {
if (inotify_rm_watch(arl->fd, arl->wd)) inotify_rm_watch(arl->fd, arl->wd);
error(0, 0, "Failed to remove inotify watch.");
arl->watching_dir = false; arl->watching_dir = false;
} }
reload = true; reload = true;
@ -110,26 +104,23 @@ void arl_init(arl_t *arl)
arl->fd = inotify_init(); arl->fd = inotify_init();
arl->watching_dir = false; arl->watching_dir = false;
if (arl->fd == -1) if (arl->fd == -1)
error(0, 0, "Could not initialize inotify."); error(0, 0, "Could not initialize inotify, no automatic image reloading");
} }
void arl_setup(arl_t *arl, const char *filepath) void arl_setup(arl_t *arl, const char *filepath)
{ {
if (arl->fd == -1) { if (arl->fd == -1)
error(0, 0, "Uninitialized, could not add inotify watch.");
return; return;
}
/* may have switched from a deleted to another image */ /* may have switched from a deleted to another image */
if (arl->watching_dir) { if (arl->watching_dir) {
if (inotify_rm_watch(arl->fd, arl->wd)) inotify_rm_watch(arl->fd, arl->wd);
error(0, 0, "Failed to remove inotify watch.");
arl->watching_dir = false; arl->watching_dir = false;
} }
arl->wd = inotify_add_watch(arl->fd, filepath, arl->wd = inotify_add_watch(arl->fd, filepath,
IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
if (arl->wd == -1) if (arl->wd == -1)
error(0, 0, "Failed to add inotify watch on file '%s'.", filepath); error(0, 0, "%s: Error watching file", filepath);
} }