summaryrefslogtreecommitdiffstats
path: root/autoreload_inotify.c
blob: 383e42b63fd051edd9ec6cda81e0fc1513bb42bc (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* 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 <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 && arl->wd != -1)
	{
		if(inotify_rm_watch(arl->fd, arl->wd))
			error(0, 0, "Failed to remove inotify watch.");
	}
}

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

	if (arl->fd == -1)
	{
		error(0, 0, "Uninitialized, could not add inotify watch on directory.");
		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, "Failed to add inotify watch on directory '%s'.", dn);
	else
		arl->watching_dir = true;

	free(dntmp);
}

bool arl_handle(arl_t *arl, const char *filepath)
{
	bool reload = false;
	ssize_t len;
	char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
	const struct inotify_event *event;
	char *ptr;
	char *fntmp, *fn;

	len = read(arl->fd, buf, sizeof buf);
	if (len == -1)
	{
		error(0, 0, "Failed to read inotify events.");
		return false;
	}

	for (ptr = buf; ptr < buf + len;
			ptr += sizeof(struct inotify_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)
		{
			fntmp = strdup(filepath);
			fn = basename(fntmp);

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

				/* cleanup, this has not been one-shot */
				if (arl->watching_dir)
				{
					if(inotify_rm_watch(arl->fd, arl->wd))
						error(0, 0, "Failed to remove inotify watch.");
					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_init();
	arl->watching_dir = false;
	if (arl->fd == -1)
		error(0, 0, "Could not initialize inotify.");
}

void arl_setup(arl_t *arl, const char *filepath)
{
	if (arl->fd == -1)
	{
		error(0, 0, "Uninitialized, could not add inotify watch.");
		return;
	}

	/* may have switched from a deleted to another image */
	if (arl->watching_dir)
	{
		if (inotify_rm_watch(arl->fd, arl->wd))
			error(0, 0, "Failed to remove inotify watch.");
		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, "Failed to add inotify watch on file '%s'.", filepath);
}