Krolyxon
Commit 9ac8fc6

Branches

Refs →

nsxiv9ac8fc6

Fix code-style in autoreload_inotify.c

Author
Bert Münnich
Date
2017-05-17 20:12 +0200
Parent
8aaa5c9
Commit
9ac8fc62dfc8061c6bc1f973454ba10228acfbf2

+23 −38

1 file changed

  1. M autoreload_inotify.c +23 −38
M autoreload_inotify.c View
@@ -27,9 +27,8 @@
2727
2828 CLEANUP void arl_cleanup(arl_t *arl)
2929 {
30− if (arl->fd != -1 && arl->wd != -1)
31− {
32− if(inotify_rm_watch(arl->fd, arl->wd))
30+ if (arl->fd != -1 && arl->wd != -1) {
31+ if (inotify_rm_watch(arl->fd, arl->wd))
3332 error(0, 0, "Failed to remove inotify watch.");
3433 }
3534 }
@@ -38,8 +37,7 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
3837 {
3938 char *dntmp, *dn;
4039
41− if (arl->fd == -1)
42− {
40+ if (arl->fd == -1) {
4341 error(0, 0, "Uninitialized, could not add inotify watch on directory.");
4442 return;
4543 }
@@ -49,9 +47,10 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
4947 dn = (char*) dirname(dntmp);
5048
5149 /* this is not one-shot as other stuff may be created too
52− note: we won't handle deletion of the directory itself,
53− this is a design decision */
54− arl->wd = inotify_add_watch(arl->fd, dn,IN_CREATE);
50+ * note: we won't handle deletion of the directory itself,
51+ * this is a design decision
52+ */
53+ arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
5554 if (arl->wd == -1)
5655 error(0, 0, "Failed to add inotify watch on directory '%s'.", dn);
5756 else
@@ -63,52 +62,40 @@ static void arl_setup_dir(arl_t *arl, const char *filepath)
6362 bool arl_handle(arl_t *arl, const char *filepath)
6463 {
6564 bool reload = false;
66− ssize_t len;
6765 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
68− const struct inotify_event *event;
6966 char *ptr;
70− char *fntmp, *fn;
67+ const struct inotify_event *event;
68+
69+ ssize_t len = read(arl->fd, buf, sizeof(buf));
7170
72− len = read(arl->fd, buf, sizeof buf);
73− if (len == -1)
74− {
71+ if (len == -1) {
7572 error(0, 0, "Failed to read inotify events.");
7673 return false;
7774 }
78−
79− for (ptr = buf; ptr < buf + len;
80− ptr += sizeof(struct inotify_event) + event->len)
81− {
82−
83− event = (const struct inotify_event *) ptr;
75+ for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
76+ event = (const struct inotify_event*) ptr;
8477
8578 /* events from watching the file itself */
86− if (event->mask & IN_CLOSE_WRITE)
87− {
79+ if (event->mask & IN_CLOSE_WRITE) {
8880 reload = true;
8981 }
90−
9182 if (event->mask & IN_DELETE_SELF)
9283 arl_setup_dir(arl, filepath);
9384
9485 /* events from watching the file's directory */
95− if (event->mask & IN_CREATE)
96− {
97− fntmp = strdup(filepath);
98− fn = basename(fntmp);
86+ if (event->mask & IN_CREATE) {
87+ char *fntmp = strdup(filepath);
88+ char *fn = basename(fntmp);
9989
100− if (0 == strcmp(event->name, fn))
101− {
90+ if (STREQ(event->name, fn)) {
10291 /* this is the file we're looking for */
10392
10493 /* cleanup, this has not been one-shot */
105− if (arl->watching_dir)
106− {
107− if(inotify_rm_watch(arl->fd, arl->wd))
94+ if (arl->watching_dir) {
95+ if (inotify_rm_watch(arl->fd, arl->wd))
10896 error(0, 0, "Failed to remove inotify watch.");
10997 arl->watching_dir = false;
11098 }
111−
11299 reload = true;
113100 }
114101 free(fntmp);
@@ -128,22 +115,20 @@ void arl_init(arl_t *arl)
128115
129116 void arl_setup(arl_t *arl, const char *filepath)
130117 {
131− if (arl->fd == -1)
132− {
118+ if (arl->fd == -1) {
133119 error(0, 0, "Uninitialized, could not add inotify watch.");
134120 return;
135121 }
136122
137123 /* may have switched from a deleted to another image */
138− if (arl->watching_dir)
139− {
124+ if (arl->watching_dir) {
140125 if (inotify_rm_watch(arl->fd, arl->wd))
141126 error(0, 0, "Failed to remove inotify watch.");
142127 arl->watching_dir = false;
143128 }
144129
145130 arl->wd = inotify_add_watch(arl->fd, filepath,
146− IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
131+ IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
147132 if (arl->wd == -1)
148133 error(0, 0, "Failed to add inotify watch on file '%s'.", filepath);
149134 }