Krolyxon
Commit 3724d3f

Branches

Refs →

nsxivautoreload_inotify.c

150 lines · 3.5 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 <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <sys/inotify.h>
23#include <libgen.h>
24
25#include "util.h"
26#include "autoreload.h"
27
28CLEANUP void arl_cleanup(arl_t *arl)
29{
30 if (arl->fd != -1 && arl->wd != -1)
31 {
32 if(inotify_rm_watch(arl->fd, arl->wd))
33 error(0, 0, "Failed to remove inotify watch.");
34 }
35}
36
37static void arl_setup_dir(arl_t *arl, const char *filepath)
38{
39 char *dntmp, *dn;
40
41 if (arl->fd == -1)
42 {
43 error(0, 0, "Uninitialized, could not add inotify watch on directory.");
44 return;
45 }
46
47 /* get dirname */
48 dntmp = (char*) strdup(filepath);
49 dn = (char*) dirname(dntmp);
50
51 /* this is not one-shot as other stuff may be created too
52 note: we won't handle deletion of the directory itself,
53 this is a design decision */
54 arl->wd = inotify_add_watch(arl->fd, dn,IN_CREATE);
55 if (arl->wd == -1)
56 error(0, 0, "Failed to add inotify watch on directory '%s'.", dn);
57 else
58 arl->watching_dir = true;
59
60 free(dntmp);
61}
62
63bool arl_handle(arl_t *arl, const char *filepath)
64{
65 bool reload = false;
66 ssize_t len;
67 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
68 const struct inotify_event *event;
69 char *ptr;
70 char *fntmp, *fn;
71
72 len = read(arl->fd, buf, sizeof buf);
73 if (len == -1)
74 {
75 error(0, 0, "Failed to read inotify events.");
76 return false;
77 }
78
79 for (ptr = buf; ptr < buf + len;
80 ptr += sizeof(struct inotify_event) + event->len)
81 {
82
83 event = (const struct inotify_event *) ptr;
84
85 /* events from watching the file itself */
86 if (event->mask & IN_CLOSE_WRITE)
87 {
88 reload = true;
89 }
90
91 if (event->mask & IN_DELETE_SELF)
92 arl_setup_dir(arl, filepath);
93
94 /* events from watching the file's directory */
95 if (event->mask & IN_CREATE)
96 {
97 fntmp = strdup(filepath);
98 fn = basename(fntmp);
99
100 if (0 == strcmp(event->name, fn))
101 {
102 /* this is the file we're looking for */
103
104 /* cleanup, this has not been one-shot */
105 if (arl->watching_dir)
106 {
107 if(inotify_rm_watch(arl->fd, arl->wd))
108 error(0, 0, "Failed to remove inotify watch.");
109 arl->watching_dir = false;
110 }
111
112 reload = true;
113 }
114 free(fntmp);
115 }
116 }
117 return reload;
118}
119
120void arl_init(arl_t *arl)
121{
122 /* this needs to be done only once */
123 arl->fd = inotify_init();
124 arl->watching_dir = false;
125 if (arl->fd == -1)
126 error(0, 0, "Could not initialize inotify.");
127}
128
129void arl_setup(arl_t *arl, const char *filepath)
130{
131 if (arl->fd == -1)
132 {
133 error(0, 0, "Uninitialized, could not add inotify watch.");
134 return;
135 }
136
137 /* may have switched from a deleted to another image */
138 if (arl->watching_dir)
139 {
140 if (inotify_rm_watch(arl->fd, arl->wd))
141 error(0, 0, "Failed to remove inotify watch.");
142 arl->watching_dir = false;
143 }
144
145 arl->wd = inotify_add_watch(arl->fd, filepath,
146 IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
147 if (arl->wd == -1)
148 error(0, 0, "Failed to add inotify watch on file '%s'.", filepath);
149}
150