Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */ 2 : : 3 : : /* inotify-missing.c - GVFS Monitor based on inotify. 4 : : 5 : : Copyright (C) 2005 John McCutchan 6 : : 7 : : This library is free software; you can redistribute it and/or 8 : : modify it under the terms of the GNU Lesser General Public 9 : : License as published by the Free Software Foundation; either 10 : : version 2.1 of the License, or (at your option) any later version. 11 : : 12 : : This 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 : : Lesser General Public License for more details. 16 : : 17 : : You should have received a copy of the GNU Lesser General Public License 18 : : along with this library; if not, see <http://www.gnu.org/licenses/>. 19 : : 20 : : Authors: 21 : : John McCutchan <john@johnmccutchan.com> 22 : : */ 23 : : 24 : : #include "config.h" 25 : : #include <glib.h> 26 : : #include "inotify-missing.h" 27 : : #include "inotify-path.h" 28 : : #include "glib-private.h" 29 : : 30 : : #define SCAN_MISSING_TIME 4 /* 1/4 Hz */ 31 : : 32 : : static gboolean im_debug_enabled = FALSE; 33 : : #define IM_W if (im_debug_enabled) g_warning 34 : : 35 : : /* We put inotify_sub's that are missing on this list */ 36 : : static GList *missing_sub_list = NULL; 37 : : static gboolean im_scan_missing (gpointer user_data); 38 : : static gboolean scan_missing_running = FALSE; 39 : : static void (*missing_cb)(inotify_sub *sub) = NULL; 40 : : 41 : : G_LOCK_EXTERN (inotify_lock); 42 : : 43 : : /* inotify_lock must be held before calling */ 44 : : void 45 : 57 : _im_startup (void (*callback)(inotify_sub *sub)) 46 : : { 47 : : static gboolean initialized = FALSE; 48 : : 49 [ + - ]: 57 : if (!initialized) 50 : : { 51 : 57 : missing_cb = callback; 52 : 57 : initialized = TRUE; 53 : : } 54 : 57 : } 55 : : 56 : : /* inotify_lock must be held before calling */ 57 : : void 58 : 209 : _im_add (inotify_sub *sub) 59 : : { 60 [ - + ]: 209 : if (g_list_find (missing_sub_list, sub)) 61 : : { 62 [ # # ]: 0 : IM_W ("asked to add %s to missing list but it's already on the list!\n", sub->dirname); 63 : 0 : return; 64 : : } 65 : : 66 [ - + ]: 209 : IM_W ("adding %s to missing list\n", sub->dirname); 67 : 209 : missing_sub_list = g_list_prepend (missing_sub_list, sub); 68 : : 69 : : /* If the timeout is turned off, we turn it back on */ 70 [ + + ]: 209 : if (!scan_missing_running) 71 : : { 72 : : GSource *source; 73 : : 74 : 7 : scan_missing_running = TRUE; 75 : 7 : source = g_timeout_source_new_seconds (SCAN_MISSING_TIME); 76 : 7 : g_source_set_callback (source, im_scan_missing, NULL, NULL); 77 : 7 : g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ()); 78 : 7 : g_source_unref (source); 79 : : } 80 : : } 81 : : 82 : : /* inotify_lock must be held before calling */ 83 : : void 84 : 436 : _im_rm (inotify_sub *sub) 85 : : { 86 : : GList *link; 87 : : 88 : 436 : link = g_list_find (missing_sub_list, sub); 89 : : 90 [ + + ]: 436 : if (!link) 91 : : { 92 [ - + ]: 228 : IM_W ("asked to remove %s from missing list but it isn't on the list!\n", sub->dirname); 93 : 228 : return; 94 : : } 95 : : 96 [ - + ]: 208 : IM_W ("removing %s from missing list\n", sub->dirname); 97 : : 98 : 208 : missing_sub_list = g_list_remove_link (missing_sub_list, link); 99 : 208 : g_list_free_1 (link); 100 : : } 101 : : 102 : : /* Scans the list of missing subscriptions checking if they 103 : : * are available yet. 104 : : */ 105 : : static gboolean 106 : 5 : im_scan_missing (gpointer user_data) 107 : : { 108 : 5 : GList *nolonger_missing = NULL; 109 : : GList *l; 110 : : 111 : 5 : G_LOCK (inotify_lock); 112 : : 113 [ - + ]: 5 : IM_W ("scanning missing list with %d items\n", g_list_length (missing_sub_list)); 114 [ + + ]: 8 : for (l = missing_sub_list; l; l = l->next) 115 : : { 116 : 3 : inotify_sub *sub = l->data; 117 : 3 : gboolean not_m = FALSE; 118 : : 119 [ - + ]: 3 : IM_W ("checking %p\n", sub); 120 : 3 : g_assert (sub); 121 : 3 : g_assert (sub->dirname); 122 : 3 : not_m = _ip_start_watching (sub); 123 : : 124 [ + + ]: 3 : if (not_m) 125 : : { 126 : 1 : missing_cb (sub); 127 [ - + ]: 1 : IM_W ("removed %s from missing list\n", sub->dirname); 128 : : /* We have to build a list of list nodes to remove from the 129 : : * missing_sub_list. We do the removal outside of this loop. 130 : : */ 131 : 1 : nolonger_missing = g_list_prepend (nolonger_missing, l); 132 : : } 133 : : } 134 : : 135 [ + + ]: 6 : for (l = nolonger_missing; l ; l = l->next) 136 : : { 137 : 1 : GList *llink = l->data; 138 : 1 : missing_sub_list = g_list_remove_link (missing_sub_list, llink); 139 : 1 : g_list_free_1 (llink); 140 : : } 141 : : 142 : 5 : g_list_free (nolonger_missing); 143 : : 144 : : /* If the missing list is now empty, we disable the timeout */ 145 [ + + ]: 5 : if (missing_sub_list == NULL) 146 : : { 147 : 3 : scan_missing_running = FALSE; 148 : 3 : G_UNLOCK (inotify_lock); 149 : 3 : return FALSE; 150 : : } 151 : : else 152 : : { 153 : 2 : G_UNLOCK (inotify_lock); 154 : 2 : return TRUE; 155 : : } 156 : : }