Fix a failing testcase
[platform/upstream/glib.git] / gio / kqueue / kqueue-missing.c
1 /*******************************************************************************
2   Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
3
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:
10
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13
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 *******************************************************************************/
22
23 #include <glib.h>
24
25 #include "kqueue-helper.h"
26 #include "kqueue-sub.h"
27 #include "kqueue-missing.h"
28
29
30 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
31
32 static gboolean km_scan_missing (gpointer user_data);
33
34 static gboolean km_debug_enabled = FALSE;
35 #define KM_W if (km_debug_enabled) g_warning
36
37 static GSList *missing_subs_list = NULL;
38 G_LOCK_DEFINE_STATIC (missing_lock);
39
40 static volatile gboolean scan_missing_running = FALSE;
41 static on_create_cb file_appeared_callback;
42
43
44 /**
45  * _km_init:
46  * @cb: a callback function. It will be called when a watched file
47  *     will appear.
48  *
49  * Initialize the kqueue-missing module (optional).
50  **/
51 void
52 _km_init (on_create_cb cb)
53 {
54   file_appeared_callback = cb;
55 }
56
57
58 /**
59  * _km_add_missing:
60  * @sub: a #kqueue_sub
61  *
62  * Adds a subscription to the missing files list.
63  **/
64 void
65 _km_add_missing (kqueue_sub *sub)
66 {
67   G_LOCK (missing_lock);
68   if (g_slist_find (missing_subs_list, sub))
69     {
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;
73     }
74
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);
78
79   if (!scan_missing_running)
80     {
81       scan_missing_running = TRUE;
82       g_timeout_add_seconds (SCAN_MISSING_TIME, km_scan_missing, NULL);
83     }
84 }
85
86
87 /**
88  * km_scan_missing:
89  * @user_data: unused
90  *
91  * The core missing files watching routine.
92  *
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.
96  *
97  * Returns: %FALSE if no missing files left, %TRUE otherwise.
98  **/
99 static gboolean
100 km_scan_missing (gpointer user_data)
101 {
102   GSList *head;
103   GSList *not_missing = NULL;
104   gboolean retval = FALSE;
105   
106   G_LOCK (missing_lock);
107
108   if (missing_subs_list)
109     KM_W ("we have a job");
110
111   for (head = missing_subs_list; head; head = head->next)
112     {
113       kqueue_sub *sub = (kqueue_sub *) head->data;
114       g_assert (sub != NULL);
115       g_assert (sub->filename != NULL);
116
117       if (_kh_start_watching (sub))
118         {
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);
123         }
124     }
125
126   for (head = not_missing; head; head = head->next)
127     {
128       GSList *link = (GSList *) head->data;
129       missing_subs_list = g_slist_remove_link (missing_subs_list, link);
130     }
131   g_slist_free (not_missing);
132
133   if (missing_subs_list == NULL)
134     {
135       scan_missing_running = FALSE;
136       retval = FALSE;
137     }
138   else
139     retval = TRUE;
140
141   G_UNLOCK (missing_lock);
142   return retval;
143 }
144
145
146 /**
147  * _km_remove:
148  * @sub: a #kqueue_sub
149  *
150  * Removes a subscription from a list of missing files.
151  **/
152 void
153 _km_remove (kqueue_sub *sub)
154 {
155   G_LOCK (missing_lock);
156   missing_subs_list = g_slist_remove (missing_subs_list, sub);
157   G_UNLOCK (missing_lock);
158 }