Browse Source

Fix code-style in autoreload_inotify.c

master
Bert Münnich 8 years ago
parent
commit
9ac8fc62df
1 changed files with 23 additions and 38 deletions
  1. +23
    -38
      autoreload_inotify.c

+ 23
- 38
autoreload_inotify.c View File

@@ -27,9 +27,8 @@


CLEANUP void arl_cleanup(arl_t *arl) CLEANUP void arl_cleanup(arl_t *arl)
{ {
if (arl->fd != -1 && arl->wd != -1)
{
if(inotify_rm_watch(arl->fd, arl->wd))
if (arl->fd != -1 && arl->wd != -1) {
if (inotify_rm_watch(arl->fd, arl->wd))
error(0, 0, "Failed to remove inotify watch."); error(0, 0, "Failed to remove inotify watch.");
} }
} }
@@ -38,8 +37,7 @@ 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."); error(0, 0, "Uninitialized, could not add inotify watch on directory.");
return; return;
} }
@@ -49,9 +47,10 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
dn = (char*) dirname(dntmp); dn = (char*) dirname(dntmp);


/* this is not one-shot as other stuff may be created too /* this is not one-shot as other stuff may be created too
note: we won't handle deletion of the directory itself,
this is a design decision */
arl->wd = inotify_add_watch(arl->fd, dn,IN_CREATE);
* note: we won't handle deletion of the directory itself,
* this is a design decision
*/
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, "Failed to add inotify watch on directory '%s'.", dn);
else else
@@ -63,52 +62,40 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
bool arl_handle(arl_t *arl, const char *filepath) bool arl_handle(arl_t *arl, const char *filepath)
{ {
bool reload = false; bool reload = false;
ssize_t len;
char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
char *ptr; char *ptr;
char *fntmp, *fn;
const struct inotify_event *event;

ssize_t len = read(arl->fd, buf, sizeof(buf));


len = read(arl->fd, buf, sizeof buf);
if (len == -1)
{
if (len == -1) {
error(0, 0, "Failed to read inotify events."); error(0, 0, "Failed to read inotify events.");
return false; return false;
} }

for (ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event->len)
{

event = (const struct inotify_event *) ptr;
for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
event = (const struct inotify_event*) ptr;


/* events from watching the file itself */ /* events from watching the file itself */
if (event->mask & IN_CLOSE_WRITE)
{
if (event->mask & IN_CLOSE_WRITE) {
reload = true; reload = true;
} }

if (event->mask & IN_DELETE_SELF) if (event->mask & IN_DELETE_SELF)
arl_setup_dir(arl, filepath); arl_setup_dir(arl, filepath);


/* events from watching the file's directory */ /* events from watching the file's directory */
if (event->mask & IN_CREATE)
{
fntmp = strdup(filepath);
fn = basename(fntmp);
if (event->mask & IN_CREATE) {
char *fntmp = strdup(filepath);
char *fn = basename(fntmp);


if (0 == strcmp(event->name, fn))
{
if (STREQ(event->name, fn)) {
/* this is the file we're looking for */ /* this is the file we're looking for */


/* cleanup, this has not been one-shot */ /* cleanup, this has not been one-shot */
if (arl->watching_dir)
{
if(inotify_rm_watch(arl->fd, arl->wd))
if (arl->watching_dir) {
if (inotify_rm_watch(arl->fd, arl->wd))
error(0, 0, "Failed to remove inotify watch."); error(0, 0, "Failed to remove inotify watch.");
arl->watching_dir = false; arl->watching_dir = false;
} }

reload = true; reload = true;
} }
free(fntmp); free(fntmp);
@@ -128,22 +115,20 @@ void arl_init(arl_t *arl)


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."); 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)) if (inotify_rm_watch(arl->fd, arl->wd))
error(0, 0, "Failed to remove inotify watch."); 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, "Failed to add inotify watch on file '%s'.", filepath);
} }


Loading…
Cancel
Save