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_queue.h"
29 #define G_LOG_DOMAIN "inotify"
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
;
44 mpd_inotify_queue_init(void)
49 free_callback(gpointer data
, G_GNUC_UNUSED gpointer user_data
)
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
);
65 mpd_inotify_run_update(G_GNUC_UNUSED gpointer data
)
69 while (inotify_queue
!= NULL
) {
70 char *uri_utf8
= inotify_queue
->data
;
72 id
= update_enqueue(uri_utf8
, false);
77 g_debug("updating '%s' job=%u", uri_utf8
, id
);
80 inotify_queue
= g_slist_delete_link(inotify_queue
,
84 /* done, remove the timer event by returning false */
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
] == '/'));
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 */
116 inotify_queue
= g_slist_concat(inotify_queue
,
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 */
129 /* move the existing path to the new queue */
130 inotify_queue
= g_slist_prepend(inotify_queue
,
134 inotify_queue
= g_slist_prepend(inotify_queue
, uri_utf8
);