configure.ac: Move OpenAL to Audio Output Plugins (nonstreaming), add header.
[mpd-mk.git] / src / inotify_queue.c
blob5391a17152f91d7bb28e6b6fc791bc28f753f185
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"
21 #include "inotify_queue.h"
22 #include "update.h"
24 #include <glib.h>
26 #include <string.h>
28 #undef G_LOG_DOMAIN
29 #define G_LOG_DOMAIN "inotify"
31 enum {
32 /**
33 * Wait this long after the last change before calling
34 * update_enqueue(). This increases the probability that
35 * updates can be bundled.
37 INOTIFY_UPDATE_DELAY_S = 5,
40 static GSList *inotify_queue;
41 static guint queue_source_id;
43 void
44 mpd_inotify_queue_init(void)
48 static void
49 free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
51 g_free(data);
54 void
55 mpd_inotify_queue_finish(void)
57 if (queue_source_id != 0)
58 g_source_remove(queue_source_id);
60 g_slist_foreach(inotify_queue, free_callback, NULL);
61 g_slist_free(inotify_queue);
64 static gboolean
65 mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
67 unsigned id;
69 while (inotify_queue != NULL) {
70 char *uri_utf8 = inotify_queue->data;
72 id = update_enqueue(uri_utf8, false);
73 if (id == 0)
74 /* retry later */
75 return true;
77 g_debug("updating '%s' job=%u", uri_utf8, id);
79 g_free(uri_utf8);
80 inotify_queue = g_slist_delete_link(inotify_queue,
81 inotify_queue);
84 /* done, remove the timer event by returning false */
85 queue_source_id = 0;
86 return false;
89 static bool
90 path_in(const char *path, const char *possible_parent)
92 size_t length = strlen(possible_parent);
94 return path[0] == 0 ||
95 (memcmp(possible_parent, path, length) == 0 &&
96 (path[length] == 0 || path[length] == '/'));
99 void
100 mpd_inotify_enqueue(char *uri_utf8)
102 GSList *old_queue = inotify_queue;
104 if (queue_source_id != 0)
105 g_source_remove(queue_source_id);
106 queue_source_id = g_timeout_add_seconds(INOTIFY_UPDATE_DELAY_S,
107 mpd_inotify_run_update, NULL);
109 inotify_queue = NULL;
110 while (old_queue != NULL) {
111 char *current_uri = old_queue->data;
113 if (path_in(uri_utf8, current_uri)) {
114 /* already enqueued */
115 g_free(uri_utf8);
116 inotify_queue = g_slist_concat(inotify_queue,
117 old_queue);
118 return;
121 old_queue = g_slist_delete_link(old_queue, old_queue);
123 if (path_in(current_uri, uri_utf8))
124 /* existing path is a sub-path of the new
125 path; we can dequeue the existing path and
126 update the new path instead */
127 g_free(current_uri);
128 else
129 /* move the existing path to the new queue */
130 inotify_queue = g_slist_prepend(inotify_queue,
131 current_uri);
134 inotify_queue = g_slist_prepend(inotify_queue, uri_utf8);