Add some more cases to the app-id unit tests
[glib.git] / gio / kqueue / kqueue-missing.c
blob9decdc937d9ab6d9221eb19bb6f7a946b1b75d25
1 /*******************************************************************************
2 Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 *******************************************************************************/
23 #include <glib.h>
25 #include "kqueue-helper.h"
26 #include "kqueue-sub.h"
27 #include "kqueue-missing.h"
30 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
32 static gboolean km_scan_missing (gpointer user_data);
34 static gboolean km_debug_enabled = FALSE;
35 #define KM_W if (km_debug_enabled) g_warning
37 static GSList *missing_subs_list = NULL;
38 G_LOCK_DEFINE_STATIC (missing_lock);
40 static volatile gboolean scan_missing_running = FALSE;
41 static on_create_cb file_appeared_callback;
44 /**
45 * _km_init:
46 * @cb: a callback function. It will be called when a watched file
47 * will appear.
49 * Initialize the kqueue-missing module (optional).
50 **/
51 void
52 _km_init (on_create_cb cb)
54 file_appeared_callback = cb;
58 /**
59 * _km_add_missing:
60 * @sub: a #kqueue_sub
62 * Adds a subscription to the missing files list.
63 **/
64 void
65 _km_add_missing (kqueue_sub *sub)
67 G_LOCK (missing_lock);
68 if (g_slist_find (missing_subs_list, sub))
70 KM_W ("asked to add %s to missing list but it's already on the list!\n", sub->filename);
71 G_UNLOCK (missing_lock);
72 return;
75 KM_W ("adding %s to missing list\n", sub->filename);
76 missing_subs_list = g_slist_prepend (missing_subs_list, sub);
77 G_UNLOCK (missing_lock);
79 if (!scan_missing_running)
81 scan_missing_running = TRUE;
82 g_timeout_add_seconds (SCAN_MISSING_TIME, km_scan_missing, NULL);
87 /**
88 * km_scan_missing:
89 * @user_data: unused
91 * The core missing files watching routine.
93 * Traverses through a list of missing files, tries to start watching each with
94 * kqueue, removes the appropriate entry and invokes a user callback if the file
95 * has appeared.
97 * Returns: %FALSE if no missing files left, %TRUE otherwise.
98 **/
99 static gboolean
100 km_scan_missing (gpointer user_data)
102 GSList *head;
103 GSList *not_missing = NULL;
104 gboolean retval = FALSE;
106 G_LOCK (missing_lock);
108 if (missing_subs_list)
109 KM_W ("we have a job");
111 for (head = missing_subs_list; head; head = head->next)
113 kqueue_sub *sub = (kqueue_sub *) head->data;
114 g_assert (sub != NULL);
115 g_assert (sub->filename != NULL);
117 if (_kh_start_watching (sub))
119 KM_W ("file %s now exists, starting watching", sub->filename);
120 if (file_appeared_callback)
121 file_appeared_callback (sub);
122 not_missing = g_slist_prepend (not_missing, head);
126 for (head = not_missing; head; head = head->next)
128 GSList *link = (GSList *) head->data;
129 missing_subs_list = g_slist_remove_link (missing_subs_list, link);
131 g_slist_free (not_missing);
133 if (missing_subs_list == NULL)
135 scan_missing_running = FALSE;
136 retval = FALSE;
138 else
139 retval = TRUE;
141 G_UNLOCK (missing_lock);
142 return retval;
147 * _km_remove:
148 * @sub: a #kqueue_sub
150 * Removes a subscription from a list of missing files.
152 void
153 _km_remove (kqueue_sub *sub)
155 G_LOCK (missing_lock);
156 missing_subs_list = g_slist_remove (missing_subs_list, sub);
157 G_UNLOCK (missing_lock);