2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "inotify_source.h"
22 #include "fifo_buffer.h"
25 #include <sys/inotify.h>
31 #define G_LOG_DOMAIN "inotify"
33 struct mpd_inotify_source
{
39 * The channel's source id in the GLib main loop.
43 struct fifo_buffer
*buffer
;
45 mpd_inotify_callback_t callback
;
50 * A GQuark for GError instances.
53 mpd_inotify_quark(void)
55 return g_quark_from_static_string("inotify");
59 mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel
*_source
,
60 G_GNUC_UNUSED GIOCondition condition
,
63 struct mpd_inotify_source
*source
= data
;
67 const struct inotify_event
*event
;
69 dest
= fifo_buffer_write(source
->buffer
, &length
);
71 g_error("buffer full");
73 nbytes
= read(source
->fd
, dest
, length
);
75 g_error("failed to read from inotify: %s", g_strerror(errno
));
77 g_error("end of file from inotify");
79 fifo_buffer_append(source
->buffer
, nbytes
);
84 event
= fifo_buffer_read(source
->buffer
, &length
);
85 if (event
== NULL
|| length
< sizeof(*event
) ||
86 length
< sizeof(*event
) + event
->len
)
89 if (event
->len
> 0 && event
->name
[event
->len
- 1] == 0)
94 source
->callback(event
->wd
, event
->mask
, name
,
95 source
->callback_ctx
);
96 fifo_buffer_consume(source
->buffer
,
97 sizeof(*event
) + event
->len
);
103 struct mpd_inotify_source
*
104 mpd_inotify_source_new(mpd_inotify_callback_t callback
, void *callback_ctx
,
107 struct mpd_inotify_source
*source
=
108 g_new(struct mpd_inotify_source
, 1);
110 source
->fd
= inotify_init_cloexec();
111 if (source
->fd
< 0) {
112 g_set_error(error_r
, mpd_inotify_quark(), errno
,
113 "inotify_init() has failed: %s",
119 source
->buffer
= fifo_buffer_new(4096);
121 source
->channel
= g_io_channel_unix_new(source
->fd
);
122 source
->id
= g_io_add_watch(source
->channel
, G_IO_IN
,
123 mpd_inotify_in_event
, source
);
125 source
->callback
= callback
;
126 source
->callback_ctx
= callback_ctx
;
132 mpd_inotify_source_free(struct mpd_inotify_source
*source
)
134 g_source_remove(source
->id
);
135 g_io_channel_unref(source
->channel
);
136 fifo_buffer_free(source
->buffer
);
142 mpd_inotify_source_add(struct mpd_inotify_source
*source
,
143 const char *path_fs
, unsigned mask
,
146 int wd
= inotify_add_watch(source
->fd
, path_fs
, mask
);
148 g_set_error(error_r
, mpd_inotify_quark(), errno
,
149 "inotify_add_watch() has failed: %s",
156 mpd_inotify_source_rm(struct mpd_inotify_source
*source
, unsigned wd
)
158 int ret
= inotify_rm_watch(source
->fd
, wd
);
159 if (ret
< 0 && errno
!= EINVAL
)
160 g_warning("inotify_rm_watch() has failed: %s",
163 /* EINVAL may happen here when the file has been deleted; the
164 kernel seems to auto-unregister deleted files */