configure.ac: Move OpenAL to Audio Output Plugins (nonstreaming), add header.
[mpd-mk.git] / src / inotify_update.c
blob218440110534da911903b7240d0041cc9bb3736d
1 /*
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.
20 #include "config.h" /* must be first for large file support */
21 #include "inotify_update.h"
22 #include "inotify_source.h"
23 #include "inotify_queue.h"
24 #include "database.h"
25 #include "mapper.h"
26 #include "path.h"
28 #include <assert.h>
29 #include <sys/inotify.h>
30 #include <sys/stat.h>
31 #include <stdbool.h>
32 #include <string.h>
33 #include <dirent.h>
34 #include <errno.h>
36 #undef G_LOG_DOMAIN
37 #define G_LOG_DOMAIN "inotify"
39 enum {
40 IN_MASK = IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
41 |IN_MOVE|IN_MOVE_SELF
42 #ifdef IN_ONLYDIR
43 |IN_ONLYDIR
44 #endif
47 struct watch_directory {
48 struct watch_directory *parent;
50 char *name;
52 int descriptor;
54 GList *children;
57 static struct mpd_inotify_source *inotify_source;
59 static unsigned inotify_max_depth;
60 static struct watch_directory inotify_root;
61 static GTree *inotify_directories;
63 static gint
64 compare(gconstpointer a, gconstpointer b)
66 if (a < b)
67 return -1;
68 else if (a > b)
69 return 1;
70 else
71 return 0;
74 static void
75 tree_add_watch_directory(struct watch_directory *directory)
77 g_tree_insert(inotify_directories,
78 GINT_TO_POINTER(directory->descriptor), directory);
81 static void
82 tree_remove_watch_directory(struct watch_directory *directory)
84 G_GNUC_UNUSED
85 bool found = g_tree_remove(inotify_directories,
86 GINT_TO_POINTER(directory->descriptor));
87 assert(found);
90 static struct watch_directory *
91 tree_find_watch_directory(int wd)
93 return g_tree_lookup(inotify_directories, GINT_TO_POINTER(wd));
96 static void
97 remove_watch_directory(struct watch_directory *directory)
99 assert(directory != NULL);
100 assert(directory->parent != NULL);
101 assert(directory->parent->children != NULL);
103 tree_remove_watch_directory(directory);
105 while (directory->children != NULL)
106 remove_watch_directory(directory->children->data);
108 directory->parent->children =
109 g_list_remove(directory->parent->children, directory);
111 mpd_inotify_source_rm(inotify_source, directory->descriptor);
112 g_free(directory->name);
113 g_slice_free(struct watch_directory, directory);
116 static char *
117 watch_directory_get_uri_fs(const struct watch_directory *directory)
119 char *parent_uri, *uri;
121 if (directory->parent == NULL)
122 return NULL;
124 parent_uri = watch_directory_get_uri_fs(directory->parent);
125 if (parent_uri == NULL)
126 return g_strdup(directory->name);
128 uri = g_strconcat(parent_uri, "/", directory->name, NULL);
129 g_free(parent_uri);
131 return uri;
134 /* we don't look at "." / ".." nor files with newlines in their name */
135 static bool skip_path(const char *path)
137 return (path[0] == '.' && path[1] == 0) ||
138 (path[0] == '.' && path[1] == '.' && path[2] == 0) ||
139 strchr(path, '\n') != NULL;
142 static void
143 recursive_watch_subdirectories(struct watch_directory *directory,
144 const char *path_fs, unsigned depth)
146 GError *error = NULL;
147 DIR *dir;
148 struct dirent *ent;
150 assert(directory != NULL);
151 assert(depth <= inotify_max_depth);
152 assert(path_fs != NULL);
154 ++depth;
156 if (depth > inotify_max_depth)
157 return;
159 dir = opendir(path_fs);
160 if (dir == NULL) {
161 g_warning("Failed to open directory %s: %s",
162 path_fs, g_strerror(errno));
163 return;
166 while ((ent = readdir(dir))) {
167 char *child_path_fs;
168 struct stat st;
169 int ret;
170 struct watch_directory *child;
172 if (skip_path(ent->d_name))
173 continue;
175 child_path_fs = g_strconcat(path_fs, "/", ent->d_name, NULL);
176 /* XXX what about symlinks? */
177 ret = lstat(child_path_fs, &st);
178 if (ret < 0) {
179 g_warning("Failed to stat %s: %s",
180 child_path_fs, g_strerror(errno));
181 g_free(child_path_fs);
182 continue;
185 if (!S_ISDIR(st.st_mode)) {
186 g_free(child_path_fs);
187 continue;
190 ret = mpd_inotify_source_add(inotify_source, child_path_fs,
191 IN_MASK, &error);
192 if (ret < 0) {
193 g_warning("Failed to register %s: %s",
194 child_path_fs, error->message);
195 g_error_free(error);
196 error = NULL;
197 g_free(child_path_fs);
198 continue;
201 child = tree_find_watch_directory(ret);
202 if (child != NULL) {
203 /* already being watched */
204 g_free(child_path_fs);
205 continue;
208 child = g_slice_new(struct watch_directory);
209 child->parent = directory;
210 child->name = g_strdup(ent->d_name);
211 child->descriptor = ret;
212 child->children = NULL;
214 directory->children = g_list_prepend(directory->children,
215 child);
217 tree_add_watch_directory(child);
219 recursive_watch_subdirectories(child, child_path_fs, depth);
220 g_free(child_path_fs);
223 closedir(dir);
226 G_GNUC_PURE
227 static unsigned
228 watch_directory_depth(const struct watch_directory *d)
230 assert(d != NULL);
232 unsigned depth = 0;
233 while ((d = d->parent) != NULL)
234 ++depth;
236 return depth;
239 static void
240 mpd_inotify_callback(int wd, unsigned mask,
241 G_GNUC_UNUSED const char *name, G_GNUC_UNUSED void *ctx)
243 struct watch_directory *directory;
244 char *uri_fs;
246 /*g_debug("wd=%d mask=0x%x name='%s'", wd, mask, name);*/
248 directory = tree_find_watch_directory(wd);
249 if (directory == NULL)
250 return;
252 uri_fs = watch_directory_get_uri_fs(directory);
254 if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) {
255 g_free(uri_fs);
256 remove_watch_directory(directory);
257 return;
260 if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 &&
261 (mask & IN_ISDIR) != 0) {
262 /* a sub directory was changed: register those in
263 inotify */
264 char *root = map_directory_fs(db_get_root());
265 char *path_fs;
267 if (uri_fs != NULL) {
268 path_fs = g_strconcat(root, "/", uri_fs, NULL);
269 g_free(root);
270 } else
271 path_fs = root;
273 recursive_watch_subdirectories(directory, path_fs,
274 watch_directory_depth(directory));
275 g_free(path_fs);
278 if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 ||
279 /* at the maximum depth, we watch out for newly created
280 directories */
281 (watch_directory_depth(directory) == inotify_max_depth &&
282 (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) {
283 /* a file was changed, or a directory was
284 moved/deleted: queue a database update */
285 char *uri_utf8 = uri_fs != NULL
286 ? fs_charset_to_utf8(uri_fs)
287 : g_strdup("");
289 if (uri_utf8 != NULL)
290 /* this function will take care of freeing
291 uri_utf8 */
292 mpd_inotify_enqueue(uri_utf8);
295 g_free(uri_fs);
298 void
299 mpd_inotify_init(unsigned max_depth)
301 struct directory *root;
302 char *path;
303 GError *error = NULL;
305 g_debug("initializing inotify");
307 root = db_get_root();
308 if (root == NULL) {
309 g_debug("no music directory configured");
310 return;
313 path = map_directory_fs(root);
314 if (path == NULL) {
315 g_warning("mapper has failed");
316 return;
319 inotify_source = mpd_inotify_source_new(mpd_inotify_callback, NULL,
320 &error);
321 if (inotify_source == NULL) {
322 g_warning("%s", error->message);
323 g_error_free(error);
324 g_free(path);
325 return;
328 inotify_max_depth = max_depth;
330 inotify_root.name = path;
331 inotify_root.descriptor = mpd_inotify_source_add(inotify_source, path,
332 IN_MASK, &error);
333 if (inotify_root.descriptor < 0) {
334 g_warning("%s", error->message);
335 g_error_free(error);
336 mpd_inotify_source_free(inotify_source);
337 inotify_source = NULL;
338 g_free(path);
339 return;
342 inotify_directories = g_tree_new(compare);
343 tree_add_watch_directory(&inotify_root);
345 recursive_watch_subdirectories(&inotify_root, path, 0);
347 mpd_inotify_queue_init();
349 g_debug("watching music directory");
352 static gboolean
353 free_watch_directory(G_GNUC_UNUSED gpointer key, gpointer value,
354 G_GNUC_UNUSED gpointer data)
356 struct watch_directory *directory = value;
358 g_free(directory->name);
359 g_list_free(directory->children);
361 if (directory != &inotify_root)
362 g_slice_free(struct watch_directory, directory);
364 return false;
367 void
368 mpd_inotify_finish(void)
370 if (inotify_source == NULL)
371 return;
373 mpd_inotify_queue_finish();
374 mpd_inotify_source_free(inotify_source);
376 g_tree_foreach(inotify_directories, free_watch_directory, NULL);
377 g_tree_destroy(inotify_directories);