Krolyxon
Commit 0e1a85d

Branches

Refs →

nsxivautoreload_inotify.c

131 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
59bool arl_handle(arl_t *arl, const char *filepath)
60{
61 bool reload = false;
62 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
63 char *ptr;
64 const struct inotify_event *event;
65
66 for (;;) {
67 ssize_t len = read(arl->fd, buf, sizeof(buf));
68
69 if (len == -1) {
70 if (errno == EINTR)
71 continue;
72 break;
73 }
74 for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
75 event = (const struct inotify_event*) ptr;
76
77 /* events from watching the file itself */
78 if (event->mask & IN_CLOSE_WRITE) {
79 reload = true;
80 }
81 if (event->mask & IN_DELETE_SELF)
82 arl_setup_dir(arl, filepath);
83
84 /* events from watching the file's directory */
85 if (event->mask & IN_CREATE) {
86 char *fntmp = strdup(filepath);
87 char *fn = basename(fntmp);
88
89 if (STREQ(event->name, fn)) {
90 /* this is the file we're looking for */
91
92 /* cleanup, this has not been one-shot */
93 if (arl->watching_dir) {
94 inotify_rm_watch(arl->fd, arl->wd);
95 arl->watching_dir = false;
96 }
97 reload = true;
98 }
99 free(fntmp);
100 }
101 }
102 }
103 return reload;
104}
105
106void arl_init(arl_t *arl)
107{
108 /* this needs to be done only once */
109 arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
110 arl->watching_dir = false;
111 if (arl->fd == -1)
112 error(0, 0, "Could not initialize inotify, no automatic image reloading");
113}
114
115void arl_setup(arl_t *arl, const char *filepath)
116{
117 if (arl->fd == -1)
118 return;
119
120 /* may have switched from a deleted to another image */
121 if (arl->watching_dir) {
122 inotify_rm_watch(arl->fd, arl->wd);
123 arl->watching_dir = false;
124 }
125
126 arl->wd = inotify_add_watch(arl->fd, filepath,
127 IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
128 if (arl->wd == -1)
129 error(0, 0, "%s: Error watching file", filepath);
130}
131