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 gboolean scan_missing_running = FALSE;  /* must be accessed under @missing_lock */
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 
66   if (!scan_missing_running)
67     {
68       GSource *source;
69       scan_missing_running = TRUE;
70       source = g_timeout_source_new_seconds (SCAN_MISSING_TIME);
71       g_source_set_callback (source, _km_scan_missing_cb, NULL, NULL);
72       g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
73       g_source_unref (source);
74     }
75 
76   G_UNLOCK (missing_lock);
77 }
78 
79 /**
80  * _kh_file_appeared_cb:
81  * @sub: a #kqueue_sub
82  *
83  * A callback function for kqueue-missing subsystem.
84  *
85  * Signals that a missing file has finally appeared in the filesystem.
86  * Emits %G_FILE_MONITOR_EVENT_CREATED.
87  **/
88 static void
_kh_file_appeared_cb(kqueue_sub * sub)89 _kh_file_appeared_cb (kqueue_sub *sub)
90 {
91   gint64 now = g_get_monotonic_time ();
92 
93   g_assert (sub != NULL);
94   g_assert (sub->filename);
95 
96   if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
97     return;
98 
99   g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CREATED,
100                                       sub->basename, NULL, NULL, now);
101   g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT,
102                                       sub->basename, NULL, NULL, now);
103 }
104 
105 /**
106  * _km_scan_missing:
107  * @user_data: unused
108  *
109  * The core missing files watching routine.
110  *
111  * Traverses through a list of missing files, tries to start watching each with
112  * kqueue, removes the appropriate entry and invokes a user callback if the file
113  * has appeared.
114  *
115  * Returns: %FALSE if no missing files left, %TRUE otherwise.
116  **/
117 gboolean
_km_scan_missing(kqueue_sub * check_this_sub_only)118 _km_scan_missing (kqueue_sub *check_this_sub_only)
119 {
120   GSList *head;
121   GSList *not_missing = NULL;
122   gboolean retval = FALSE;
123 
124   G_LOCK (missing_lock);
125 
126   if (missing_subs_list)
127     KM_W ("we have a job");
128 
129   for (head = missing_subs_list; head; head = head->next)
130     {
131       kqueue_sub *sub = (kqueue_sub *) head->data;
132       g_assert (sub != NULL);
133       g_assert (sub->filename != NULL);
134 
135       if (check_this_sub_only != NULL && sub != check_this_sub_only)
136         continue;
137 
138       if (_kqsub_start_watching (sub))
139         {
140           KM_W ("file %s now exists, starting watching", sub->filename);
141           if (check_this_sub_only == NULL)
142             _kh_file_appeared_cb (sub);
143           not_missing = g_slist_prepend (not_missing, head);
144         }
145     }
146 
147   for (head = not_missing; head; head = head->next)
148     {
149       GSList *link = (GSList *) head->data;
150       missing_subs_list = g_slist_remove_link (missing_subs_list, link);
151     }
152   g_slist_free (not_missing);
153 
154   if (missing_subs_list == NULL)
155     {
156       scan_missing_running = FALSE;
157       retval = FALSE;
158     }
159   else
160     retval = TRUE;
161 
162   G_UNLOCK (missing_lock);
163   return retval;
164 }
165 
166 
167 /**
168  * _km_remove:
169  * @sub: a #kqueue_sub
170  *
171  * Removes a subscription from a list of missing files.
172  **/
173 void
_km_remove(kqueue_sub * sub)174 _km_remove (kqueue_sub *sub)
175 {
176   G_LOCK (missing_lock);
177   missing_subs_list = g_slist_remove (missing_subs_list, sub);
178   G_UNLOCK (missing_lock);
179 }
180