• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "glib-private.h"
25 
26 #include "kqueue-helper.h"
27 
28 
29 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
30 
31 static gboolean km_debug_enabled = FALSE;
32 #define KM_W if (km_debug_enabled) g_warning
33 
34 static GSList *missing_subs_list = NULL;
35 G_LOCK_DEFINE_STATIC (missing_lock);
36 
37 static volatile gboolean scan_missing_running = FALSE;
38 
39 
40 static gboolean
_km_scan_missing_cb(gpointer user_data)41 _km_scan_missing_cb (gpointer user_data)
42 {
43   return _km_scan_missing (NULL);
44 }
45 
46 /**
47  * _km_add_missing:
48  * @sub: a #kqueue_sub
49  *
50  * Adds a subscription to the missing files list.
51  **/
52 void
_km_add_missing(kqueue_sub * sub)53 _km_add_missing (kqueue_sub *sub)
54 {
55   G_LOCK (missing_lock);
56   if (g_slist_find (missing_subs_list, sub))
57     {
58       KM_W ("asked to add %s to missing list but it's already on the list!\n", sub->filename);
59       G_UNLOCK (missing_lock);
60       return;
61     }
62 
63   KM_W ("adding %s to missing list\n", sub->filename);
64   missing_subs_list = g_slist_prepend (missing_subs_list, sub);
65   G_UNLOCK (missing_lock);
66 
67   if (!scan_missing_running)
68     {
69       GSource *source;
70       scan_missing_running = TRUE;
71       source = g_timeout_source_new_seconds (SCAN_MISSING_TIME);
72       g_source_set_callback (source, _km_scan_missing_cb, NULL, NULL);
73       g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
74       g_source_unref (source);
75     }
76 }
77 
78 /**
79  * _kh_file_appeared_cb:
80  * @sub: a #kqueue_sub
81  *
82  * A callback function for kqueue-missing subsystem.
83  *
84  * Signals that a missing file has finally appeared in the filesystem.
85  * Emits %G_FILE_MONITOR_EVENT_CREATED.
86  **/
87 static void
_kh_file_appeared_cb(kqueue_sub * sub)88 _kh_file_appeared_cb (kqueue_sub *sub)
89 {
90   gint64 now = g_get_monotonic_time ();
91 
92   g_assert (sub != NULL);
93   g_assert (sub->filename);
94 
95   if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
96     return;
97 
98   g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CREATED,
99                                       sub->basename, NULL, NULL, now);
100   g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT,
101                                       sub->basename, NULL, NULL, now);
102 }
103 
104 /**
105  * _km_scan_missing:
106  * @user_data: unused
107  *
108  * The core missing files watching routine.
109  *
110  * Traverses through a list of missing files, tries to start watching each with
111  * kqueue, removes the appropriate entry and invokes a user callback if the file
112  * has appeared.
113  *
114  * Returns: %FALSE if no missing files left, %TRUE otherwise.
115  **/
116 gboolean
_km_scan_missing(kqueue_sub * check_this_sub_only)117 _km_scan_missing (kqueue_sub *check_this_sub_only)
118 {
119   GSList *head;
120   GSList *not_missing = NULL;
121   gboolean retval = FALSE;
122 
123   G_LOCK (missing_lock);
124 
125   if (missing_subs_list)
126     KM_W ("we have a job");
127 
128   for (head = missing_subs_list; head; head = head->next)
129     {
130       kqueue_sub *sub = (kqueue_sub *) head->data;
131       g_assert (sub != NULL);
132       g_assert (sub->filename != NULL);
133 
134       if (check_this_sub_only != NULL && sub != check_this_sub_only)
135         continue;
136 
137       if (_kqsub_start_watching (sub))
138         {
139           KM_W ("file %s now exists, starting watching", sub->filename);
140           if (check_this_sub_only == NULL)
141             _kh_file_appeared_cb (sub);
142           not_missing = g_slist_prepend (not_missing, head);
143         }
144     }
145 
146   for (head = not_missing; head; head = head->next)
147     {
148       GSList *link = (GSList *) head->data;
149       missing_subs_list = g_slist_remove_link (missing_subs_list, link);
150     }
151   g_slist_free (not_missing);
152 
153   if (missing_subs_list == NULL)
154     {
155       scan_missing_running = FALSE;
156       retval = FALSE;
157     }
158   else
159     retval = TRUE;
160 
161   G_UNLOCK (missing_lock);
162   return retval;
163 }
164 
165 
166 /**
167  * _km_remove:
168  * @sub: a #kqueue_sub
169  *
170  * Removes a subscription from a list of missing files.
171  **/
172 void
_km_remove(kqueue_sub * sub)173 _km_remove (kqueue_sub *sub)
174 {
175   G_LOCK (missing_lock);
176   missing_subs_list = g_slist_remove (missing_subs_list, sub);
177   G_UNLOCK (missing_lock);
178 }
179