Krolyxon
Commit de3d782

Branches

Refs →

nsxivautoreload_inotify.c

135 lines · 3.2 KB · C

1/* Copyright 2017 Max Voit
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <sys/inotify.h>
24#include <libgen.h>
25
26#include "util.h"
27#include "autoreload.h"
28
29CLEANUP void arl_cleanup(arl_t *arl)
30{
31 if (arl->fd != -1)
32 close(arl->fd);
33}
34
35static void arl_setup_dir(arl_t *arl, const char *filepath)
36{
37 char *dntmp, *dn;
38
39 if (arl->fd == -1)
40 return;
41
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);
57}
58
59union {
60 char d[4096]; /* aligned buffer */
61 struct inotify_event e;
62} buf;
63
64bool arl_handle(arl_t *arl, const char *filepath)
65{
66 bool reload = false;
67 char *ptr;
68 const struct inotify_event *event;
69
70 for (;;) {
71 ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
72
73 if (len == -1) {
74 if (errno == EINTR)
75 continue;
76 break;
77 }
78 for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
79 event = (const struct inotify_event*) ptr;
80
81 /* events from watching the file itself */
82 if (event->mask & IN_CLOSE_WRITE) {
83 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 }
101 reload = true;
102 }
103 free(fntmp);
104 }
105 }
106 }
107 return reload;
108}
109
110void 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
119void 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