InotifySource.cxx 3.35 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "InotifySource.hxx"
22
#include "util/fifo_buffer.h"
23
#include "fd_util.h"
24
#include "mpd_error.h"
25

26 27
#include <glib.h>

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <sys/inotify.h>
#include <unistd.h>
#include <errno.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "inotify"

/**
 * A GQuark for GError instances.
 */
static inline GQuark
mpd_inotify_quark(void)
{
	return g_quark_from_static_string("inotify");
}

44
bool
45
InotifySource::OnSocketReady(gcc_unused unsigned flags)
46 47 48 49 50
{
	void *dest;
	size_t length;
	ssize_t nbytes;

51
	dest = fifo_buffer_write(buffer, &length);
52
	if (dest == NULL)
53
		MPD_ERROR("buffer full");
54

55
	nbytes = read(Get(), dest, length);
56
	if (nbytes < 0)
57 58
		MPD_ERROR("failed to read from inotify: %s",
			  g_strerror(errno));
59
	if (nbytes == 0)
60
		MPD_ERROR("end of file from inotify");
61

62
	fifo_buffer_append(buffer, nbytes);
63 64 65 66

	while (true) {
		const char *name;

Max Kellermann's avatar
Max Kellermann committed
67 68
		const struct inotify_event *event =
			(const struct inotify_event *)
69
			fifo_buffer_read(buffer, &length);
70 71 72 73 74 75 76 77 78
		if (event == NULL || length < sizeof(*event) ||
		    length < sizeof(*event) + event->len)
			break;

		if (event->len > 0 && event->name[event->len - 1] == 0)
			name = event->name;
		else
			name = NULL;

79 80
		callback(event->wd, event->mask, name, callback_ctx);
		fifo_buffer_consume(buffer, sizeof(*event) + event->len);
81
	}
82 83

	return true;
84
}
85

86
inline
87 88
InotifySource::InotifySource(EventLoop &_loop,
			     mpd_inotify_callback_t _callback, void *_ctx,
89
			     int _fd)
90 91
	:SocketMonitor(_fd, _loop),
	 callback(_callback), callback_ctx(_ctx),
92
	 buffer(fifo_buffer_new(4096))
93
{
94 95
	ScheduleRead();

96
}
97

98
InotifySource *
99 100
InotifySource::Create(EventLoop &loop,
		      mpd_inotify_callback_t callback, void *callback_ctx,
101 102 103 104
		      GError **error_r)
{
	int fd = inotify_init_cloexec();
	if (fd < 0) {
105 106 107 108 109 110
		g_set_error(error_r, mpd_inotify_quark(), errno,
			    "inotify_init() has failed: %s",
			    g_strerror(errno));
		return NULL;
	}

111
	return new InotifySource(loop, callback, callback_ctx, fd);
112 113
}

114
InotifySource::~InotifySource()
115
{
116
	fifo_buffer_free(buffer);
117 118 119
}

int
120
InotifySource::Add(const char *path_fs, unsigned mask, GError **error_r)
121
{
122
	int wd = inotify_add_watch(Get(), path_fs, mask);
123 124 125 126 127 128 129 130 131
	if (wd < 0)
		g_set_error(error_r, mpd_inotify_quark(), errno,
			    "inotify_add_watch() has failed: %s",
			    g_strerror(errno));

	return wd;
}

void
132
InotifySource::Remove(unsigned wd)
133
{
134
	int ret = inotify_rm_watch(Get(), wd);
135 136 137 138 139 140 141
	if (ret < 0 && errno != EINVAL)
		g_warning("inotify_rm_watch() has failed: %s",
			  g_strerror(errno));

	/* EINVAL may happen here when the file has been deleted; the
	   kernel seems to auto-unregister deleted files */
}