-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoreload_inotify.c
94 lines (78 loc) · 1.96 KB
/
autoreload_inotify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "topaz.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/inotify.h>
void arl_init(arl_t *arl)
{
arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
arl->wd_dir = arl->wd_file = -1;
if (arl->fd == -1)
error(0, 0, "Could not initialize inotify, no automatic image reloading");
}
CLEANUP void arl_cleanup(arl_t *arl)
{
if (arl->fd != -1)
close(arl->fd);
free(arl->filename);
}
static void rm_watch(int fd, int *wd)
{
if (*wd != -1) {
inotify_rm_watch(fd, *wd);
*wd = -1;
}
}
static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
{
*wd = inotify_add_watch(fd, path, mask);
if (*wd == -1)
error(0, errno, "inotify: %s", path);
}
void arl_setup(arl_t *arl, const char *filepath)
{
char *base = strrchr(filepath, '/');
if (arl->fd == -1)
return;
rm_watch(arl->fd, &arl->wd_dir);
rm_watch(arl->fd, &arl->wd_file);
add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
free(arl->filename);
arl->filename = estrdup(filepath);
if (base != NULL) {
arl->filename[++base - filepath] = '\0';
add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
strcpy(arl->filename, base);
}
}
union {
char d[4096]; /* aligned buffer */
struct inotify_event e;
} buf;
bool arl_handle(arl_t *arl)
{
bool reload = false;
char *ptr;
const struct inotify_event *e;
for (;;) {
ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
if (len == -1) {
if (errno == EINTR)
continue;
break;
}
for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
e = (const struct inotify_event*) ptr;
if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
reload = true;
} else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
rm_watch(arl->fd, &arl->wd_file);
} else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
if (STREQ(e->name, arl->filename))
reload = true;
}
}
}
return reload;
}