summaryrefslogtreecommitdiff
path: root/nslcd/inotify_iterator.h
blob: 0fa58297eb632073d3c125dcd9a4d1aa3e8bbde6 (plain)
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
/* inotify_helper.h - Simple iteration for inotify events
 *
 * Copyright (C) 2014 Luke Shumaker
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* The usage is pretty simple:
 *
 *     int my_filedesc = inotify_init();
 *     ...
 *     struct inotify_event *my_event;
 *     for (INOTIFY_ITERATOR(my_filedesc, my_event)) {
 *         ...
 *     }
 *
 * Easy, right?
 */

#ifndef _INOTIFY_ITERATOR_H
#define _INOTIFY_ITERATOR_H

#include <sys/inotify.h>
#include <limits.h> /* for NAME_MAX */
#include <string.h> /* for memset(3) */
#include <unistd.h> /* for read(3) */

struct _inotify_buffer {
	ssize_t len;
	ssize_t pos;
	char dat[sizeof(struct inotify_event)+NAME_MAX+1];
};

#define	_INOTIFY_ITERATOR_EVENT(BUF) \
	((struct inotify_event *)&((BUF).dat[(BUF).pos]))
#define	_INOTIFY_ITERATOR_EVENT_SIZE(BUF) \
	(sizeof(struct inotify_event) + _INOTIFY_ITERATOR_EVENT(BUF)->len)
#define	_INOTIFY_ITERATOR_CHECK(FD, BUF, EVENT) \
	BUF.len > -1
#define	_INOTIFY_ITERATOR_UPDATE(FD, BUF, EVENT)                               \
	EVENT = ({                                                             \
		if (BUF.len-(BUF.pos+_INOTIFY_ITERATOR_EVENT_SIZE(BUF)) < 1) { \
			do {                                                   \
				BUF.len = read(FD, BUF.dat, sizeof(BUF.dat));  \
			} while (BUF.len == 0);                                \
			BUF.pos = 0;                                           \
		} else {                                                       \
			BUF.pos += _INOTIFY_ITERATOR_EVENT_SIZE(BUF);          \
		}                                                              \
		_INOTIFY_ITERATOR_EVENT(BUF);                                  \
	})
#define _INOTIFY_ITERATOR_INIT(FD, BUF, EVENT)                                 \
	struct _inotify_buffer BUF = ({                                        \
		struct _inotify_buffer tmp;                                    \
		memset(&tmp, 0, sizeof(tmp));                                  \
		do {                                                           \
			tmp.len = read(FD, tmp.dat, sizeof(tmp.dat));          \
		} while (tmp.len == 0);                                        \
		EVENT = (struct inotify_event *)&BUF;                          \
		tmp;                                                           \
	})
#define INOTIFY_ITERATOR(FD, EVENT) \
	_INOTIFY_ITERATOR_INIT(FD, _buf, EVENT); \
	_INOTIFY_ITERATOR_CHECK(FD, _buf, EVENT); \
	_INOTIFY_ITERATOR_UPDATE(FD, _buf, EVENT)

#endif