1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
3 /* inotify-helper.c - Gnome VFS Monitor based on inotify.
5 Copyright (C) 2005 John McCutchan
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 see <http://www.gnu.org/licenses/>.
22 John McCutchan <john@johnmccutchan.com>
27 #include "inotify-missing.h"
28 #include "inotify-path.h"
29 #include "glib-private.h"
31 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
33 static gboolean im_debug_enabled = FALSE;
34 #define IM_W if (im_debug_enabled) g_warning
36 /* We put inotify_sub's that are missing on this list */
37 static GList *missing_sub_list = NULL;
38 static gboolean im_scan_missing (gpointer user_data);
39 static gboolean scan_missing_running = FALSE;
40 static void (*missing_cb)(inotify_sub *sub) = NULL;
42 G_LOCK_EXTERN (inotify_lock);
44 /* inotify_lock must be held before calling */
46 _im_startup (void (*callback)(inotify_sub *sub))
48 static gboolean initialized = FALSE;
52 missing_cb = callback;
57 /* inotify_lock must be held before calling */
59 _im_add (inotify_sub *sub)
61 if (g_list_find (missing_sub_list, sub))
63 IM_W ("asked to add %s to missing list but it's already on the list!\n", sub->dirname);
67 IM_W ("adding %s to missing list\n", sub->dirname);
68 missing_sub_list = g_list_prepend (missing_sub_list, sub);
70 /* If the timeout is turned off, we turn it back on */
71 if (!scan_missing_running)
75 scan_missing_running = TRUE;
76 source = g_timeout_source_new_seconds (SCAN_MISSING_TIME);
77 g_source_set_callback (source, im_scan_missing, NULL, NULL);
78 g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
79 g_source_unref (source);
83 /* inotify_lock must be held before calling */
85 _im_rm (inotify_sub *sub)
89 link = g_list_find (missing_sub_list, sub);
93 IM_W ("asked to remove %s from missing list but it isn't on the list!\n", sub->dirname);
97 IM_W ("removing %s from missing list\n", sub->dirname);
99 missing_sub_list = g_list_remove_link (missing_sub_list, link);
100 g_list_free_1 (link);
103 /* Scans the list of missing subscriptions checking if they
107 im_scan_missing (gpointer user_data)
109 GList *nolonger_missing = NULL;
112 G_LOCK (inotify_lock);
114 IM_W ("scanning missing list with %d items\n", g_list_length (missing_sub_list));
115 for (l = missing_sub_list; l; l = l->next)
117 inotify_sub *sub = l->data;
118 gboolean not_m = FALSE;
120 IM_W ("checking %p\n", sub);
122 g_assert (sub->dirname);
123 not_m = _ip_start_watching (sub);
128 IM_W ("removed %s from missing list\n", sub->dirname);
129 /* We have to build a list of list nodes to remove from the
130 * missing_sub_list. We do the removal outside of this loop.
132 nolonger_missing = g_list_prepend (nolonger_missing, l);
136 for (l = nolonger_missing; l ; l = l->next)
138 GList *llink = l->data;
139 missing_sub_list = g_list_remove_link (missing_sub_list, llink);
140 g_list_free_1 (llink);
143 g_list_free (nolonger_missing);
145 /* If the missing list is now empty, we disable the timeout */
146 if (missing_sub_list == NULL)
148 scan_missing_running = FALSE;
149 G_UNLOCK (inotify_lock);
154 G_UNLOCK (inotify_lock);