Krolyxon
Branch master

Branches

Refs →

nsxivautoreload.c

152 lines · 3.3 KB · C

1/* Copyright 2017 Max Voit, Bert Muennich
2 * Copyright 2022-2023 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21
22#if HAVE_INOTIFY
23
24#include <assert.h>
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/inotify.h>
29#include <unistd.h>
30
31static struct {
32 char *buf;
33 size_t len;
34} scratch;
35
36void arl_init(arl_t *arl)
37{
38 arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
39 arl->wd_dir = arl->wd_file = -1;
40 if (arl->fd == -1)
41 error(0, 0, "Could not initialize inotify, no automatic image reloading");
42}
43
44CLEANUP void arl_cleanup(arl_t *arl)
45{
46 if (arl->fd != -1)
47 close(arl->fd);
48 free(scratch.buf);
49}
50
51static void rm_watch(int fd, int *wd)
52{
53 if (*wd != -1) {
54 inotify_rm_watch(fd, *wd);
55 *wd = -1;
56 }
57}
58
59static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
60{
61 *wd = inotify_add_watch(fd, path, mask);
62 if (*wd == -1)
63 error(0, errno, "inotify: %s", path);
64}
65
66static char *arl_scratch_push(const char *filepath, size_t len)
67{
68 if (scratch.len < len + 1) {
69 scratch.len = len + 1;
70 scratch.buf = erealloc(scratch.buf, scratch.len);
71 }
72 scratch.buf[len] = '\0';
73 return memcpy(scratch.buf, filepath, len);
74}
75
76void arl_add(arl_t *arl, const char *filepath)
77{
78 char *base, *dir;
79
80 if (arl->fd == -1)
81 return;
82
83 rm_watch(arl->fd, &arl->wd_dir);
84 rm_watch(arl->fd, &arl->wd_file);
85 add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
86
87 base = strrchr(filepath, '/');
88 assert(base != NULL); /* filepath must be result of `realpath(3)` */
89 dir = arl_scratch_push(filepath, base - filepath);
90 add_watch(arl->fd, &arl->wd_dir, dir, IN_CREATE | IN_MOVED_TO);
91 arl->filename = arl_scratch_push(base + 1, strlen(base + 1));
92}
93
94bool arl_handle(arl_t *arl)
95{
96 bool reload = false;
97 char *ptr;
98 const struct inotify_event *e;
99 /* inotify_event aligned buffer */
100 static union {
101 char d[4096];
102 struct inotify_event e;
103 } buf;
104
105 while (true) {
106 ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
107
108 if (len == -1) {
109 if (errno == EINTR)
110 continue;
111 break;
112 }
113 for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
114 e = (const struct inotify_event *)ptr;
115 if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
116 reload = true;
117 } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
118 rm_watch(arl->fd, &arl->wd_file);
119 } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
120 if (STREQ(e->name, arl->filename))
121 reload = true;
122 }
123 }
124 }
125 return reload;
126}
127
128#else
129
130void arl_init(arl_t *arl)
131{
132 arl->fd = -1;
133}
134
135void arl_cleanup(arl_t *arl)
136{
137 (void)arl;
138}
139
140void arl_add(arl_t *arl, const char *filepath)
141{
142 (void)arl;
143 (void)filepath;
144}
145
146bool arl_handle(arl_t *arl)
147{
148 (void)arl;
149 return false;
150}
151
152#endif /* HAVE_INOTIFY */