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 *******************************************************************************/
25 #include "kqueue-helper.h"
26 #include "kqueue-sub.h"
27 #include "kqueue-missing.h"
30 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
32 static gboolean
km_scan_missing (gpointer user_data
);
34 static gboolean km_debug_enabled
= FALSE
;
35 #define KM_W if (km_debug_enabled) g_warning
37 static GSList
*missing_subs_list
= NULL
;
38 G_LOCK_DEFINE_STATIC (missing_lock
);
40 static volatile gboolean scan_missing_running
= FALSE
;
41 static on_create_cb file_appeared_callback
;
46 * @cb: a callback function. It will be called when a watched file
49 * Initialize the kqueue-missing module (optional).
52 _km_init (on_create_cb cb
)
54 file_appeared_callback
= cb
;
62 * Adds a subscription to the missing files list.
65 _km_add_missing (kqueue_sub
*sub
)
67 G_LOCK (missing_lock
);
68 if (g_slist_find (missing_subs_list
, sub
))
70 KM_W ("asked to add %s to missing list but it's already on the list!\n", sub
->filename
);
74 KM_W ("adding %s to missing list\n", sub
->filename
);
75 missing_subs_list
= g_slist_prepend (missing_subs_list
, sub
);
76 G_UNLOCK (missing_lock
);
78 if (!scan_missing_running
)
80 scan_missing_running
= TRUE
;
81 g_timeout_add_seconds (SCAN_MISSING_TIME
, km_scan_missing
, NULL
);
90 * The core missing files watching routine.
92 * Traverses through a list of missing files, tries to start watching each with
93 * kqueue, removes the appropriate entry and invokes a user callback if the file
96 * Returns: %FALSE if no missing files left, %TRUE otherwise.
99 km_scan_missing (gpointer user_data
)
102 GSList
*not_missing
= NULL
;
103 gboolean retval
= FALSE
;
105 G_LOCK (missing_lock
);
107 if (missing_subs_list
)
108 KM_W ("we have a job");
110 for (head
= missing_subs_list
; head
; head
= head
->next
)
112 kqueue_sub
*sub
= (kqueue_sub
*) head
->data
;
113 g_assert (sub
!= NULL
);
114 g_assert (sub
->filename
!= NULL
);
116 if (_kh_start_watching (sub
))
118 KM_W ("file %s now exists, starting watching", sub
->filename
);
119 if (file_appeared_callback
)
120 file_appeared_callback (sub
);
121 not_missing
= g_slist_prepend (not_missing
, head
);
125 for (head
= not_missing
; head
; head
= head
->next
)
127 GSList
*link
= (GSList
*) head
->data
;
128 missing_subs_list
= g_slist_remove_link (missing_subs_list
, link
);
130 g_slist_free (not_missing
);
132 if (missing_subs_list
== NULL
)
134 scan_missing_running
= FALSE
;
140 G_UNLOCK (missing_lock
);
147 * @sub: a #kqueue_sub
149 * Removes a subscription from a list of missing files.
152 _km_remove (kqueue_sub
*sub
)
154 G_LOCK (missing_lock
);
155 missing_subs_list
= g_slist_remove (missing_subs_list
, sub
);
156 G_UNLOCK (missing_lock
);