Krolyxon
Commit a20173a

Branches

Refs →

nsxiva20173a

Detect all file overwrites in autoreload_inotify

mv(1) inside the same filesystem was not detected.

Supporting this case made it necessary to always watch the directory. Turns out
the logic and state keeping between arl_setup() and arl_handle() is easier,
when using different watch descriptors for the file and the directory and not
using a oneshot descriptor for the file.

Requiring an absolute canonical path for arl_setup() simplifies dir and base
name splitting. No need for dirname(3) and basename(3) anymore.
Author
Bert Münnich
Date
2017-05-17 20:16 +0200
Committer
Bert Münnich · 2017-05-17 20:20 +0200
Parent
de3d782
Commit
a20173a42df64515c0a5d1c5fba0c056a633a441

+51 −72

3 files changed

  1. M autoreload.h +6 −5
  2. M autoreload_inotify.c +44 −66
  3. M main.c +1 −1
M autoreload.h View
@@ -23,13 +23,14 @@
2323
2424 typedef struct {
2525 int fd;
26− int wd;
27− bool watching_dir;
26+ int wd_dir;
27+ int wd_file;
28+ char *filename;
2829 } arl_t;
2930
30−void arl_cleanup(arl_t*);
31−bool arl_handle(arl_t*, const char*);
3231 void arl_init(arl_t*);
33−void arl_setup(arl_t*, const char*);
32+void arl_cleanup(arl_t*);
33+void arl_setup(arl_t*, const char* /* result of realpath(3) */);
34+bool arl_handle(arl_t*);
3435
3536 #endif /* AUTORELOAD_H */
M autoreload_inotify.c View
@@ -21,39 +21,60 @@
2121 #include <string.h>
2222 #include <unistd.h>
2323 #include <sys/inotify.h>
24−#include <libgen.h>
2524
2625 #include "util.h"
2726 #include "autoreload.h"
2827
28+void arl_init(arl_t *arl)
29+{
30+ arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
31+ arl->wd_dir = arl->wd_file = -1;
32+ if (arl->fd == -1)
33+ error(0, 0, "Could not initialize inotify, no automatic image reloading");
34+}
35+
2936 CLEANUP void arl_cleanup(arl_t *arl)
3037 {
3138 if (arl->fd != -1)
3239 close(arl->fd);
40+ free(arl->filename);
3341 }
3442
35−static void arl_setup_dir(arl_t *arl, const char *filepath)
43+static void rm_watch(int fd, int *wd)
3644 {
37− char *dntmp, *dn;
45+ if (*wd != -1) {
46+ inotify_rm_watch(fd, *wd);
47+ *wd = -1;
48+ }
49+}
50+
51+static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
52+{
53+ *wd = inotify_add_watch(fd, path, mask);
54+ if (*wd == -1)
55+ error(0, errno, "inotify: %s", path);
56+}
57+
58+void arl_setup(arl_t *arl, const char *filepath)
59+{
60+ char *base = strrchr(filepath, '/');
3861
3962 if (arl->fd == -1)
4063 return;
4164
42− /* get dirname */
43− dntmp = (char*) strdup(filepath);
44− dn = (char*) dirname(dntmp);
45−
46− /* this is not one-shot as other stuff may be created too
47− * note: we won't handle deletion of the directory itself,
48− * this is a design decision
49− */
50− arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
51− if (arl->wd == -1)
52− error(0, 0, "%s: Error watching directory", dn);
53− else
54− arl->watching_dir = true;
55−
56− free(dntmp);
65+ rm_watch(arl->fd, &arl->wd_dir);
66+ rm_watch(arl->fd, &arl->wd_file);
67+
68+ add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
69+
70+ free(arl->filename);
71+ arl->filename = estrdup(filepath);
72+
73+ if (base != NULL) {
74+ arl->filename[++base - filepath] = '\0';
75+ add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
76+ strcpy(arl->filename, base);
77+ }
5778 }
5879
5980 union {
@@ -61,7 +82,7 @@ union {
6182 struct inotify_event e;
6283 } buf;
6384
64−bool arl_handle(arl_t *arl, const char *filepath)
85+bool arl_handle(arl_t *arl)
6586 {
6687 bool reload = false;
6788 char *ptr;
@@ -77,59 +98,16 @@ bool arl_handle(arl_t *arl, const char *filepath)
7798 }
7899 for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
79100 event = (const struct inotify_event*) ptr;
80−
81− /* events from watching the file itself */
82101 if (event->mask & IN_CLOSE_WRITE) {
83102 reload = true;
84− }
85− if (event->mask & IN_DELETE_SELF)
86− arl_setup_dir(arl, filepath);
87−
88− /* events from watching the file's directory */
89− if (event->mask & IN_CREATE) {
90− char *fntmp = strdup(filepath);
91− char *fn = basename(fntmp);
92−
93− if (STREQ(event->name, fn)) {
94− /* this is the file we're looking for */
95−
96− /* cleanup, this has not been one-shot */
97− if (arl->watching_dir) {
98− inotify_rm_watch(arl->fd, arl->wd);
99− arl->watching_dir = false;
100− }
103+ } else if (event->mask & IN_DELETE_SELF) {
104+ rm_watch(arl->fd, &arl->wd_file);
105+ } else if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
106+ if (STREQ(event->name, arl->filename))
101107 reload = true;
102− }
103− free(fntmp);
104108 }
105109 }
106110 }
107111 return reload;
108112 }
109113
110−void arl_init(arl_t *arl)
111−{
112− /* this needs to be done only once */
113− arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
114− arl->watching_dir = false;
115− if (arl->fd == -1)
116− error(0, 0, "Could not initialize inotify, no automatic image reloading");
117−}
118−
119−void arl_setup(arl_t *arl, const char *filepath)
120−{
121− if (arl->fd == -1)
122− return;
123−
124− /* may have switched from a deleted to another image */
125− if (arl->watching_dir) {
126− inotify_rm_watch(arl->fd, arl->wd);
127− arl->watching_dir = false;
128− }
129−
130− arl->wd = inotify_add_watch(arl->fd, filepath,
131− IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
132− if (arl->wd == -1)
133− error(0, 0, "%s: Error watching file", filepath);
134−}
135−
M main.c View
@@ -723,7 +723,7 @@ void run(void)
723723 if (info.fd != -1 && FD_ISSET(info.fd, &fds))
724724 read_info();
725725 if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
726− if (arl_handle(&arl, files[fileidx].path)) {
726+ if (arl_handle(&arl)) {
727727 /* when too fast, imlib2 can't load the image */
728728 nanosleep(&ten_ms, NULL);
729729 load_image(fileidx);