Krolyxon
Commit 8f0322c

Branches

Refs →

nsxivautoreload.c

146 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 { char *buf; size_t len; } scratch;
32
33void arl_init(arl_t *arl)
34{
35 arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
36 arl->wd_dir = arl->wd_file = -1;
37 if (arl->fd == -1)
38 error(0, 0, "Could not initialize inotify, no automatic image reloading");
39}
40
41CLEANUP void arl_cleanup(arl_t *arl)
42{
43 if (arl->fd != -1)
44 close(arl->fd);
45 free(scratch.buf);
46}
47
48static void rm_watch(int fd, int *wd)
49{
50 if (*wd != -1) {
51 inotify_rm_watch(fd, *wd);
52 *wd = -1;
53 }
54}
55
56static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
57{
58 *wd = inotify_add_watch(fd, path, mask);
59 if (*wd == -1)
60 error(0, errno, "inotify: %s", path);
61}
62
63static char *arl_scratch_push(const char *filepath, size_t len)
64{
65 if (scratch.len < len + 1) {
66 scratch.len = len + 1;
67 scratch.buf = erealloc(scratch.buf, scratch.len);
68 }
69 scratch.buf[len] = '\0';
70 return memcpy(scratch.buf, filepath, len);
71}
72
73void arl_add(arl_t *arl, const char *filepath)
74{
75 char *base, *dir;
76
77 if (arl->fd == -1)
78 return;
79
80 rm_watch(arl->fd, &arl->wd_dir);
81 rm_watch(arl->fd, &arl->wd_file);
82 add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
83
84 base = strrchr(filepath, '/');
85 assert(base != NULL); /* filepath must be result of `realpath(3)` */
86 dir = arl_scratch_push(filepath, base - filepath);
87 add_watch(arl->fd, &arl->wd_dir, dir, IN_CREATE | IN_MOVED_TO);
88 arl->filename = arl_scratch_push(base + 1, strlen(base + 1));
89}
90
91bool arl_handle(arl_t *arl)
92{
93 bool reload = false;
94 char *ptr;
95 const struct inotify_event *e;
96 /* inotify_event aligned buffer */
97 static union { char d[4096]; struct inotify_event e; } buf;
98
99 while (true) {
100 ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
101
102 if (len == -1) {
103 if (errno == EINTR)
104 continue;
105 break;
106 }
107 for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
108 e = (const struct inotify_event*) ptr;
109 if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
110 reload = true;
111 } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
112 rm_watch(arl->fd, &arl->wd_file);
113 } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
114 if (STREQ(e->name, arl->filename))
115 reload = true;
116 }
117 }
118 }
119 return reload;
120}
121
122#else
123
124void arl_init(arl_t *arl)
125{
126 arl->fd = -1;
127}
128
129void arl_cleanup(arl_t *arl)
130{
131 (void) arl;
132}
133
134void arl_add(arl_t *arl, const char *filepath)
135{
136 (void) arl;
137 (void) filepath;
138}
139
140bool arl_handle(arl_t *arl)
141{
142 (void) arl;
143 return false;
144}
145
146#endif /* HAVE_INOTIFY */