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
21 *******************************************************************************/
24 #include "glib-private.h"
26 #include "kqueue-helper.h"
29 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
31 static gboolean km_debug_enabled = FALSE;
32 #define KM_W if (km_debug_enabled) g_warning
34 static GSList *missing_subs_list = NULL;
35 G_LOCK_DEFINE_STATIC (missing_lock);
37 static gboolean scan_missing_running = FALSE; /* must be accessed under @missing_lock */
41 _km_scan_missing_cb (gpointer user_data)
43 return _km_scan_missing (NULL);
50 * Adds a subscription to the missing files list.
53 _km_add_missing (kqueue_sub *sub)
55 G_LOCK (missing_lock);
56 if (g_slist_find (missing_subs_list, sub))
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);
63 KM_W ("adding %s to missing list\n", sub->filename);
64 missing_subs_list = g_slist_prepend (missing_subs_list, sub);
66 if (!scan_missing_running)
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);
76 G_UNLOCK (missing_lock);
80 * _kh_file_appeared_cb:
83 * A callback function for kqueue-missing subsystem.
85 * Signals that a missing file has finally appeared in the filesystem.
86 * Emits %G_FILE_MONITOR_EVENT_CREATED.
89 _kh_file_appeared_cb (kqueue_sub *sub)
91 gint64 now = g_get_monotonic_time ();
93 g_assert (sub != NULL);
94 g_assert (sub->filename);
96 if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
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);
109 * The core missing files watching routine.
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
115 * Returns: %FALSE if no missing files left, %TRUE otherwise.
118 _km_scan_missing (kqueue_sub *check_this_sub_only)
121 GSList *not_missing = NULL;
122 gboolean retval = FALSE;
124 G_LOCK (missing_lock);
126 if (missing_subs_list)
127 KM_W ("we have a job");
129 for (head = missing_subs_list; head; head = head->next)
131 kqueue_sub *sub = (kqueue_sub *) head->data;
132 g_assert (sub != NULL);
133 g_assert (sub->filename != NULL);
135 if (check_this_sub_only != NULL && sub != check_this_sub_only)
138 if (_kqsub_start_watching (sub))
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);
147 for (head = not_missing; head; head = head->next)
149 GSList *link = (GSList *) head->data;
150 missing_subs_list = g_slist_remove_link (missing_subs_list, link);
152 g_slist_free (not_missing);
154 if (missing_subs_list == NULL)
156 scan_missing_running = FALSE;
162 G_UNLOCK (missing_lock);
169 * @sub: a #kqueue_sub
171 * Removes a subscription from a list of missing files.
174 _km_remove (kqueue_sub *sub)
176 G_LOCK (missing_lock);
177 missing_subs_list = g_slist_remove (missing_subs_list, sub);
178 G_UNLOCK (missing_lock);