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 volatile gboolean scan_missing_running
= FALSE
;
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
);
65 G_UNLOCK (missing_lock
);
67 if (!scan_missing_running
)
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
);
79 * _kh_file_appeared_cb:
82 * A callback function for kqueue-missing subsystem.
84 * Signals that a missing file has finally appeared in the filesystem.
85 * Emits %G_FILE_MONITOR_EVENT_CREATED.
88 _kh_file_appeared_cb (kqueue_sub
*sub
)
90 gint64 now
= g_get_monotonic_time ();
92 g_assert (sub
!= NULL
);
93 g_assert (sub
->filename
);
95 if (!g_file_test (sub
->filename
, G_FILE_TEST_EXISTS
))
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
);
108 * The core missing files watching routine.
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
114 * Returns: %FALSE if no missing files left, %TRUE otherwise.
117 _km_scan_missing (kqueue_sub
*check_this_sub_only
)
120 GSList
*not_missing
= NULL
;
121 gboolean retval
= FALSE
;
123 G_LOCK (missing_lock
);
125 if (missing_subs_list
)
126 KM_W ("we have a job");
128 for (head
= missing_subs_list
; head
; head
= head
->next
)
130 kqueue_sub
*sub
= (kqueue_sub
*) head
->data
;
131 g_assert (sub
!= NULL
);
132 g_assert (sub
->filename
!= NULL
);
134 if (check_this_sub_only
!= NULL
&& sub
!= check_this_sub_only
)
137 if (_kqsub_start_watching (sub
))
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
);
146 for (head
= not_missing
; head
; head
= head
->next
)
148 GSList
*link
= (GSList
*) head
->data
;
149 missing_subs_list
= g_slist_remove_link (missing_subs_list
, link
);
151 g_slist_free (not_missing
);
153 if (missing_subs_list
== NULL
)
155 scan_missing_running
= FALSE
;
161 G_UNLOCK (missing_lock
);
168 * @sub: a #kqueue_sub
170 * Removes a subscription from a list of missing files.
173 _km_remove (kqueue_sub
*sub
)
175 G_LOCK (missing_lock
);
176 missing_subs_list
= g_slist_remove (missing_subs_list
, sub
);
177 G_UNLOCK (missing_lock
);