Krolyxon
Commit 0e1a85d

Branches

Refs →

nsxiv0e1a85d

Read all available inotify events

Loop reading from inotify fd in arl_handle(); requires non-blocking inotify fd.
Author
Bert Münnich
Date
2017-05-17 20:14 +0200
Parent
6695cd4
Commit
0e1a85d22485e2aac1b9930d1e3e0897a2076db3

+32 −27

1 file changed

  1. M autoreload_inotify.c +32 −27
M autoreload_inotify.c View
@@ -16,6 +16,7 @@
1616 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
1717 */
1818
19+#include <errno.h>
1920 #include <stdlib.h>
2021 #include <string.h>
2122 #include <unistd.h>
@@ -62,37 +63,41 @@ bool arl_handle(arl_t *arl, const char *filepath)
6263 char *ptr;
6364 const struct inotify_event *event;
6465
65− ssize_t len = read(arl->fd, buf, sizeof(buf));
66+ for (;;) {
67+ ssize_t len = read(arl->fd, buf, sizeof(buf));
6668
67− if (len == -1)
68− return false;
69−
70− for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
71− event = (const struct inotify_event*) ptr;
72−
73− /* events from watching the file itself */
74− if (event->mask & IN_CLOSE_WRITE) {
75− reload = true;
69+ if (len == -1) {
70+ if (errno == EINTR)
71+ continue;
72+ break;
7673 }
77− if (event->mask & IN_DELETE_SELF)
78− arl_setup_dir(arl, filepath);
79−
80− /* events from watching the file's directory */
81− if (event->mask & IN_CREATE) {
82− char *fntmp = strdup(filepath);
83− char *fn = basename(fntmp);
84−
85− if (STREQ(event->name, fn)) {
86− /* this is the file we're looking for */
74+ for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
75+ event = (const struct inotify_event*) ptr;
8776
88− /* cleanup, this has not been one-shot */
89− if (arl->watching_dir) {
90− inotify_rm_watch(arl->fd, arl->wd);
91− arl->watching_dir = false;
92− }
77+ /* events from watching the file itself */
78+ if (event->mask & IN_CLOSE_WRITE) {
9379 reload = true;
9480 }
95− free(fntmp);
81+ if (event->mask & IN_DELETE_SELF)
82+ arl_setup_dir(arl, filepath);
83+
84+ /* events from watching the file's directory */
85+ if (event->mask & IN_CREATE) {
86+ char *fntmp = strdup(filepath);
87+ char *fn = basename(fntmp);
88+
89+ if (STREQ(event->name, fn)) {
90+ /* this is the file we're looking for */
91+
92+ /* cleanup, this has not been one-shot */
93+ if (arl->watching_dir) {
94+ inotify_rm_watch(arl->fd, arl->wd);
95+ arl->watching_dir = false;
96+ }
97+ reload = true;
98+ }
99+ free(fntmp);
100+ }
96101 }
97102 }
98103 return reload;
@@ -101,7 +106,7 @@ bool arl_handle(arl_t *arl, const char *filepath)
101106 void arl_init(arl_t *arl)
102107 {
103108 /* this needs to be done only once */
104− arl->fd = inotify_init();
109+ arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
105110 arl->watching_dir = false;
106111 if (arl->fd == -1)
107112 error(0, 0, "Could not initialize inotify, no automatic image reloading");