1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-vfs-monitor.c - File Monitoring for the GNOME Virtual File System.
Copyright (C) 2001 Ian McKellar
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Ian McKellar <yakk@yakk.net>
*/
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <libgnomevfs/gnome-vfs-monitor.h>
#include <libgnomevfs/gnome-vfs-monitor-private.h>
#include <libgnomevfs/gnome-vfs-method.h>
#include <libgnomevfs/gnome-vfs-module-shared.h>
#include <glib.h>
typedef enum {
CALLBACK_STATE_NOT_SENT,
CALLBACK_STATE_SENDING,
CALLBACK_STATE_SENT
} CallbackState;
struct GnomeVFSMonitorHandle {
GnomeVFSURI *uri; /* the URI being monitored */
GnomeVFSMethodHandle *method_handle;
GnomeVFSMonitorType type;
GnomeVFSMonitorCallback callback;
gpointer user_data; /* FIXME - how does this get freed */
gboolean cancelled;
gboolean in_dispatch;
GQueue *pending_callbacks; /* protected by handle_hash */
guint pending_timeout; /* protected by handle_hash */
time_t min_send_at;
};
struct GnomeVFSMonitorCallbackData {
char *info_uri;
GnomeVFSMonitorEventType event_type;
CallbackState send_state;
time_t send_at;
};
/* Number of seconds between consecutive events of the same type to the same file */
#define CONSECUTIVE_CALLBACK_DELAY 2
typedef struct GnomeVFSMonitorCallbackData GnomeVFSMonitorCallbackData;
/* This hash maps the module-supplied handle pointer to our own MonitrHandle */
static GHashTable *handle_hash = NULL;
G_LOCK_DEFINE_STATIC (handle_hash);
static gint actually_dispatch_callback (gpointer data);
static guint32 get_min_send_at (GQueue *queue);
static void
init_hash_table (void)
{
G_LOCK (handle_hash);
if (handle_hash == NULL) {
handle_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
}
G_UNLOCK (handle_hash);
}
static void
free_callback_data (GnomeVFSMonitorCallbackData *callback_data)
{
g_free (callback_data->info_uri);
g_free (callback_data);
}
GnomeVFSResult
_gnome_vfs_monitor_do_add (GnomeVFSMethod *method,
GnomeVFSMonitorHandle **handle,
GnomeVFSURI *uri,
GnomeVFSMonitorType monitor_type,
GnomeVFSMonitorCallback callback,
gpointer user_data)
{
GnomeVFSResult result;
GnomeVFSMonitorHandle *monitor_handle =
g_new0(GnomeVFSMonitorHandle, 1);
init_hash_table ();
gnome_vfs_uri_ref (uri);
monitor_handle->uri = uri;
monitor_handle->type = monitor_type;
monitor_handle->callback = callback;
monitor_handle->user_data = user_data;
monitor_handle->pending_callbacks = g_queue_new ();
monitor_handle->min_send_at = 0;
result = uri->method->monitor_add (uri->method,
&monitor_handle->method_handle, uri, monitor_type);
if (result != GNOME_VFS_OK) {
gnome_vfs_uri_unref (uri);
g_free (monitor_handle);
monitor_handle = NULL;
} else {
G_LOCK (handle_hash);
g_hash_table_insert (handle_hash,
monitor_handle->method_handle,
monitor_handle);
G_UNLOCK (handle_hash);
}
*handle = monitor_handle;
return result;
}
/* Called with handle_hash lock held */
static gboolean
no_live_callbacks (GnomeVFSMonitorHandle *monitor_handle)
{
GList *l;
GnomeVFSMonitorCallbackData *callback_data;
l = monitor_handle->pending_callbacks->head;
while (l != NULL) {
callback_data = l->data;
if (callback_data->send_state == CALLBACK_STATE_NOT_SENT ||
callback_data->send_state == CALLBACK_STATE_SENDING) {
return FALSE;
}
l = l->next;
}
return TRUE;
}
/* Called with handle_hash lock held */
static void
destroy_monitor_handle (GnomeVFSMonitorHandle *handle)
{
gboolean res;
if (handle->pending_timeout)
g_source_remove (handle->pending_timeout);
g_queue_foreach (handle->pending_callbacks, (GFunc) free_callback_data, NULL);
g_queue_free (handle->pending_callbacks);
handle->pending_callbacks = NULL;
res = g_hash_table_remove (handle_hash, handle->method_handle);
if (!res) {
g_warning ("gnome-vfs-monitor.c: A monitor handle was destroyed "
"before it was added to the method hash table. This "
"is a bug in the application and can cause crashes. "
"It is probably a race-condition.");
}
gnome_vfs_uri_unref (handle->uri);
g_free (handle);
}
GnomeVFSResult
_gnome_vfs_monitor_do_cancel (GnomeVFSMonitorHandle *handle)
{
GnomeVFSResult result;
init_hash_table ();
if (!VFS_METHOD_HAS_FUNC(handle->uri->method, monitor_cancel)) {
return GNOME_VFS_ERROR_NOT_SUPPORTED;
}
result = handle->uri->method->monitor_cancel (handle->uri->method,
handle->method_handle);
if (result == GNOME_VFS_OK) {
G_LOCK (handle_hash);
/* mark this monitor as cancelled */
handle->cancelled = TRUE;
/* we might be in the unlocked part of actually_dispatch_callback,
* in that case, don't free the handle now.
* Instead we do it in actually_dispatch_callback.
*/
if (!handle->in_dispatch) {
destroy_monitor_handle (handle);
}
G_UNLOCK (handle_hash);
}
return result;
}
/* Called with handle_hash lock held */
static void
install_timeout (GnomeVFSMonitorHandle *monitor_handle, time_t now)
{
gint delay;
if (monitor_handle->pending_timeout)
g_source_remove (monitor_handle->pending_timeout);
delay = monitor_handle->min_send_at - now;
if (delay <= 0)
monitor_handle->pending_timeout = g_idle_add (actually_dispatch_callback, monitor_handle);
else
monitor_handle->pending_timeout = g_timeout_add (delay * 1000, actually_dispatch_callback, monitor_handle);
}
static gint
actually_dispatch_callback (gpointer data)
{
GnomeVFSMonitorHandle *monitor_handle = data;
GnomeVFSMonitorCallbackData *callback_data;
gchar *uri;
GList *l, *next;
GList *dispatch;
time_t now;
/* This function runs on the main loop, so it won't reenter,
* although other threads may add stuff to the pending queue
* while we don't have the lock
*/
time (&now);
G_LOCK (handle_hash);
/* Mark this so that do_cancel doesn't free the handle while
* we run without the lock during dispatch */
monitor_handle->in_dispatch = TRUE;
if (!monitor_handle->cancelled) {
/* Find all callbacks that needs to be dispatched */
dispatch = NULL;
l = monitor_handle->pending_callbacks->head;
while (l != NULL) {
callback_data = l->data;
g_assert (callback_data->send_state != CALLBACK_STATE_SENDING);
if (callback_data->send_state == CALLBACK_STATE_NOT_SENT &&
callback_data->send_at <= now) {
callback_data->send_state = CALLBACK_STATE_SENDING;
dispatch = g_list_prepend (dispatch, callback_data);
}
l = l->next;
}
dispatch = g_list_reverse (dispatch);
G_UNLOCK (handle_hash);
l = dispatch;
while (l != NULL) {
callback_data = l->data;
uri = gnome_vfs_uri_to_string
(monitor_handle->uri,
GNOME_VFS_URI_HIDE_NONE);
/* actually run app code */
monitor_handle->callback (monitor_handle, uri,
callback_data->info_uri,
callback_data->event_type,
monitor_handle->user_data);
g_free (uri);
callback_data->send_state = CALLBACK_STATE_SENT;
l = l->next;
}
g_list_free (dispatch);
G_LOCK (handle_hash);
l = monitor_handle->pending_callbacks->head;
while (l != NULL) {
callback_data = l->data;
next = l->next;
g_assert (callback_data->send_state != CALLBACK_STATE_SENDING);
/* If we've sent the event, and its not affecting coming events, free it */
if (callback_data->send_state == CALLBACK_STATE_SENT &&
callback_data->send_at + CONSECUTIVE_CALLBACK_DELAY <= now) {
/* free the callback_data */
free_callback_data (callback_data);
g_queue_delete_link (monitor_handle->pending_callbacks, l);
}
l = next;
}
}
monitor_handle->in_dispatch = FALSE;
/* if we were waiting for this callback to be dispatched
* to free this monitor, then do it now. */
if (monitor_handle->cancelled) {
destroy_monitor_handle (monitor_handle);
} else if (no_live_callbacks (monitor_handle)) {
monitor_handle->pending_timeout = 0;
monitor_handle->min_send_at = 0;
} else {
/* pending callbacks left, install another timeout */
monitor_handle->min_send_at = get_min_send_at (monitor_handle->pending_callbacks);
install_timeout (monitor_handle, now);
}
G_UNLOCK (handle_hash);
return FALSE;
}
/* Called with handle_hash lock held */
static void
send_uri_changes_now (GnomeVFSMonitorHandle *monitor_handle,
const char *uri,
gint32 now)
{
GList *l;
GnomeVFSMonitorCallbackData *callback_data;
l = monitor_handle->pending_callbacks->head;
while (l != NULL) {
callback_data = l->data;
if (callback_data->send_state != CALLBACK_STATE_SENT &&
strcmp (callback_data->info_uri, uri) == 0) {
callback_data->send_at = now;
}
l = l->next;
}
}
/* Called with handle_hash lock held */
static guint32
get_min_send_at (GQueue *queue)
{
time_t min_send_at;
GnomeVFSMonitorCallbackData *callback_data;
GList *list;
min_send_at = G_MAXINT;
list = queue->head;
while (list != NULL) {
callback_data = list->data;
if (callback_data->send_state == CALLBACK_STATE_NOT_SENT) {
min_send_at = MIN (min_send_at, callback_data->send_at);
}
list = list->next;
}
return min_send_at;
}
/**
* gnome_vfs_monitor_callback:
* @method_handle: Method-specific monitor handle obtained through gnome_vfs_monitor_add().
* @info_uri: URI that triggered the callback.
* @event_type: The event obtained for @info_uri.
*
* gnome_vfs_monitor_callback() is used by #GnomeVFSMethods to indicate that a particular
* resource changed, and will issue the emission of the #GnomeVFSMonitorCallback registered
* using gnome_vfs_monitor_add().
**/
void
gnome_vfs_monitor_callback (GnomeVFSMethodHandle *method_handle,
GnomeVFSURI *info_uri,
GnomeVFSMonitorEventType event_type)
{
GnomeVFSMonitorCallbackData *callback_data, *other_data, *last_data;
GnomeVFSMonitorHandle *monitor_handle;
char *uri;
time_t now;
GList *l;
g_return_if_fail (info_uri != NULL);
init_hash_table ();
/* We need to loop here, because there is a race after we add the
* handle and when we add it to the hash table.
*/
do {
G_LOCK (handle_hash);
monitor_handle = g_hash_table_lookup (handle_hash, method_handle);
if (monitor_handle == NULL) {
G_UNLOCK (handle_hash);
}
} while (monitor_handle == NULL);
if (monitor_handle->cancelled) {
G_UNLOCK (handle_hash);
return;
}
time (&now);
uri = gnome_vfs_uri_to_string (info_uri, GNOME_VFS_URI_HIDE_NONE);
last_data = NULL;
l = monitor_handle->pending_callbacks->tail;
while (l != NULL) {
other_data = l->data;
if (strcmp (other_data->info_uri, uri) == 0) {
last_data = l->data;
break;
}
l = l->prev;
}
if (last_data == NULL ||
(last_data->event_type != event_type ||
last_data->send_state == CALLBACK_STATE_SENT)) {
callback_data = g_new0 (GnomeVFSMonitorCallbackData, 1);
callback_data->info_uri = g_strdup (uri);
callback_data->event_type = event_type;
callback_data->send_state = CALLBACK_STATE_NOT_SENT;
if (last_data == NULL) {
callback_data->send_at = now;
} else {
if (last_data->event_type != event_type) {
/* New type, flush old events */
send_uri_changes_now (monitor_handle, uri, now);
callback_data->send_at = now;
} else {
callback_data->send_at = last_data->send_at + CONSECUTIVE_CALLBACK_DELAY;
}
}
g_queue_push_tail (monitor_handle->pending_callbacks,
callback_data);
if (monitor_handle->min_send_at == 0 ||
callback_data->send_at < monitor_handle->min_send_at) {
monitor_handle->min_send_at = callback_data->send_at;
install_timeout (monitor_handle, now);
}
}
g_free (uri);
G_UNLOCK (handle_hash);
}
|