summaryrefslogtreecommitdiffstats
path: root/autoreload_inotify.c
blob: 473b8cb301d48ca6c062a6cb13c17dffcc43a25f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright 2017 Max Voit
 *
 * This file is part of sxiv.
 *
 * sxiv is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * sxiv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with sxiv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <libgen.h>

#include "util.h"
#include "autoreload.h"

CLEANUP void arl_cleanup(arl_t *arl)
{
	if (arl->fd != -1)
		close(arl->fd);
}

static void arl_setup_dir(arl_t *arl, const char *filepath)
{
	char *dntmp, *dn;

	if (arl->fd == -1)
		return;

	/* get dirname */
	dntmp = (char*) strdup(filepath);
	dn = (char*) dirname(dntmp);

	/* this is not one-shot as other stuff may be created too
	 * note: we won't handle deletion of the directory itself,
	 * this is a design decision
	 */
	arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
	if (arl->wd == -1)
		error(0, 0, "%s: Error watching directory", dn);
	else
		arl->watching_dir = true;

	free(dntmp);
}

union {
	char d[4096]; /* aligned buffer */
	struct inotify_event e;
} buf;

bool arl_handle(arl_t *arl, const char *filepath)
{
	bool reload = false;
	char *ptr;
	const struct inotify_event *event;

	for (;;) {
		ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));

		if (len == -1) {
			if (errno == EINTR)
				continue;
			break;
		}
		for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
			event = (const struct inotify_event*) ptr;

			/* events from watching the file itself */
			if (event->mask & IN_CLOSE_WRITE) {
				reload = true;
			}
			if (event->mask & IN_DELETE_SELF)
				arl_setup_dir(arl, filepath);

			/* events from watching the file's directory */
			if (event->mask & IN_CREATE) {
				char *fntmp = strdup(filepath);
				char *fn = basename(fntmp);

				if (STREQ(event->name, fn)) {
					/* this is the file we're looking for */

					/* cleanup, this has not been one-shot */
					if (arl->watching_dir) {
						inotify_rm_watch(arl->fd, arl->wd);
						arl->watching_dir = false;
					}
					reload = true;
				}
				free(fntmp);
			}
		}
	}
	return reload;
}

void arl_init(arl_t *arl)
{
	/* this needs to be done only once */
	arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
	arl->watching_dir = false;
	if (arl->fd == -1)
		error(0, 0, "Could not initialize inotify, no automatic image reloading");
}

void arl_setup(arl_t *arl, const char *filepath)
{
	if (arl->fd == -1)
		return;

	/* may have switched from a deleted to another image */
	if (arl->watching_dir) {
		inotify_rm_watch(arl->fd, arl->wd);
		arl->watching_dir = false;
	}

	arl->wd = inotify_add_watch(arl->fd, filepath,
		                          IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
	if (arl->wd == -1)
		error(0, 0, "%s: Error watching file", filepath);
}