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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 John McCutchan <john@johnmccutchan.com>
28 #include "inotify-missing.h"
29 #include "inotify-path.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)
73 scan_missing_running = TRUE;
74 g_timeout_add_seconds (SCAN_MISSING_TIME, im_scan_missing, NULL);
78 /* inotify_lock must be held before calling */
80 _im_rm (inotify_sub *sub)
84 link = g_list_find (missing_sub_list, sub);
88 IM_W ("asked to remove %s from missing list but it isn't on the list!\n", sub->dirname);
92 IM_W ("removing %s from missing list\n", sub->dirname);
94 missing_sub_list = g_list_remove_link (missing_sub_list, link);
98 /* Scans the list of missing subscriptions checking if they
102 im_scan_missing (gpointer user_data)
104 GList *nolonger_missing = NULL;
107 G_LOCK (inotify_lock);
109 IM_W ("scanning missing list with %d items\n", g_list_length (missing_sub_list));
110 for (l = missing_sub_list; l; l = l->next)
112 inotify_sub *sub = l->data;
113 gboolean not_m = FALSE;
115 IM_W ("checking %p\n", sub);
117 g_assert (sub->dirname);
118 not_m = _ip_start_watching (sub);
123 IM_W ("removed %s from missing list\n", sub->dirname);
124 /* We have to build a list of list nodes to remove from the
125 * missing_sub_list. We do the removal outside of this loop.
127 nolonger_missing = g_list_prepend (nolonger_missing, l);
131 for (l = nolonger_missing; l ; l = l->next)
133 GList *llink = l->data;
134 missing_sub_list = g_list_remove_link (missing_sub_list, llink);
135 g_list_free_1 (llink);
138 g_list_free (nolonger_missing);
140 /* If the missing list is now empty, we disable the timeout */
141 if (missing_sub_list == NULL)
143 scan_missing_running = FALSE;
144 G_UNLOCK (inotify_lock);
149 G_UNLOCK (inotify_lock);
155 /* inotify_lock must be held */
157 _im_diag_dump (GIOChannel *ioc)
160 g_io_channel_write_chars (ioc, "missing list:\n", -1, NULL, NULL);
161 for (l = missing_sub_list; l; l = l->next)
163 inotify_sub *sub = l->data;
164 g_io_channel_write_chars (ioc, sub->dirname, -1, NULL, NULL);
165 g_io_channel_write_chars (ioc, "\n", -1, NULL, NULL);