1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gfilemonitor.h"
27 #include "gioenumtypes.h"
34 typedef struct _FileChange FileChange;
35 static void file_change_free (FileChange *change);
38 * SECTION:gfilemonitor
39 * @short_description: File Monitor
42 * Monitors a file or directory for changes.
44 * To obtain a #GFileMonitor for a file or directory, use
45 * g_file_monitor(), g_file_monitor_file(), or
46 * g_file_monitor_directory().
48 * To get informed about changes to the file or directory you are
49 * monitoring, connect to the #GFileMonitor::changed signal. The
50 * signal will be emitted in the <link
51 * linkend="g-main-context-push-thread-default">thread-default main
52 * context</link> of the thread that the monitor was created in
53 * (though if the global default main context is blocked, this may
54 * cause notifications to be blocked even if the thread-default
55 * context is still running).
58 G_LOCK_DEFINE_STATIC(cancelled);
65 /* work around a limitation of the aliasing foo */
68 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
72 guint32 last_sent_change_time; /* 0 == not sent */
73 guint32 send_delayed_change_at; /* 0 == never */
74 guint32 send_virtual_changes_done_at; /* 0 == never */
77 struct _GFileMonitorPrivate {
81 /* Rate limiting change events */
82 GHashTable *rate_limiter;
84 GSource *pending_file_change_source;
85 GSList *pending_file_changes; /* FileChange */
88 guint32 timeout_fires_at;
90 GMainContext *context;
100 g_file_monitor_set_property (GObject *object,
105 GFileMonitor *monitor;
107 monitor = G_FILE_MONITOR (object);
111 case PROP_RATE_LIMIT:
112 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
122 g_file_monitor_get_property (GObject *object,
127 GFileMonitor *monitor;
128 GFileMonitorPrivate *priv;
130 monitor = G_FILE_MONITOR (object);
131 priv = monitor->priv;
135 case PROP_RATE_LIMIT:
136 g_value_set_int (value, priv->rate_limit_msec);
141 g_value_set_boolean (value, priv->cancelled);
142 G_UNLOCK (cancelled);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 #define DEFAULT_RATE_LIMIT_MSECS 800
152 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
154 static guint signals[LAST_SIGNAL] = { 0 };
157 rate_limiter_free (RateLimiter *limiter)
159 g_object_unref (limiter->file);
160 g_slice_free (RateLimiter, limiter);
164 g_file_monitor_finalize (GObject *object)
166 GFileMonitor *monitor;
168 monitor = G_FILE_MONITOR (object);
170 if (monitor->priv->timeout)
172 g_source_destroy (monitor->priv->timeout);
173 g_source_unref (monitor->priv->timeout);
176 g_hash_table_destroy (monitor->priv->rate_limiter);
178 g_main_context_unref (monitor->priv->context);
180 G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
184 g_file_monitor_dispose (GObject *object)
186 GFileMonitor *monitor;
187 GFileMonitorPrivate *priv;
189 monitor = G_FILE_MONITOR (object);
190 priv = monitor->priv;
192 if (priv->pending_file_change_source)
194 g_source_destroy (priv->pending_file_change_source);
195 g_source_unref (priv->pending_file_change_source);
196 priv->pending_file_change_source = NULL;
198 g_slist_foreach (priv->pending_file_changes, (GFunc) file_change_free, NULL);
199 g_slist_free (priv->pending_file_changes);
200 priv->pending_file_changes = NULL;
202 /* Make sure we cancel on last unref */
203 g_file_monitor_cancel (monitor);
205 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
209 g_file_monitor_class_init (GFileMonitorClass *klass)
211 GObjectClass *object_class;
213 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
215 object_class = G_OBJECT_CLASS (klass);
216 object_class->finalize = g_file_monitor_finalize;
217 object_class->dispose = g_file_monitor_dispose;
218 object_class->get_property = g_file_monitor_get_property;
219 object_class->set_property = g_file_monitor_set_property;
222 * GFileMonitor::changed:
223 * @monitor: a #GFileMonitor.
225 * @other_file: a #GFile or #NULL.
226 * @event_type: a #GFileMonitorEvent.
228 * Emitted when @file has been changed.
230 * If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
231 * #G_FILE_MONITOR_SEND_MOVED, @file will be set to a #GFile containing the
232 * old path, and @other_file will be set to a #GFile containing the new path.
234 * In all the other cases, @other_file will be set to #NULL.
237 g_signal_new (I_("changed"),
240 G_STRUCT_OFFSET (GFileMonitorClass, changed),
244 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
246 g_object_class_install_property (object_class,
248 g_param_spec_int ("rate-limit",
250 P_("The limit of the monitor to watch for changes, in milliseconds"),
252 DEFAULT_RATE_LIMIT_MSECS,
254 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
256 g_object_class_install_property (object_class,
258 g_param_spec_boolean ("cancelled",
260 P_("Whether the monitor has been cancelled"),
263 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
267 g_file_monitor_init (GFileMonitor *monitor)
269 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
271 GFileMonitorPrivate);
272 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
273 monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
274 NULL, (GDestroyNotify) rate_limiter_free);
275 monitor->priv->context = g_main_context_ref_thread_default ();
279 * g_file_monitor_is_cancelled:
280 * @monitor: a #GFileMonitor
282 * Returns whether the monitor is canceled.
284 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
287 g_file_monitor_is_cancelled (GFileMonitor *monitor)
291 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
294 res = monitor->priv->cancelled;
295 G_UNLOCK (cancelled);
301 * g_file_monitor_cancel:
302 * @monitor: a #GFileMonitor.
304 * Cancels a file monitor.
306 * Returns: %TRUE if monitor was cancelled.
309 g_file_monitor_cancel (GFileMonitor* monitor)
311 GFileMonitorClass *klass;
313 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
316 if (monitor->priv->cancelled)
318 G_UNLOCK (cancelled);
322 monitor->priv->cancelled = TRUE;
323 G_UNLOCK (cancelled);
325 g_object_notify (G_OBJECT (monitor), "cancelled");
327 klass = G_FILE_MONITOR_GET_CLASS (monitor);
328 return (* klass->cancel) (monitor);
332 * g_file_monitor_set_rate_limit:
333 * @monitor: a #GFileMonitor.
334 * @limit_msecs: a non-negative integer with the limit in milliseconds
335 * to poll for changes
337 * Sets the rate limit to which the @monitor will report
338 * consecutive change events to the same file.
341 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
344 GFileMonitorPrivate *priv;
346 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
347 g_return_if_fail (limit_msecs >= 0);
349 priv = monitor->priv;
350 if (priv->rate_limit_msec != limit_msecs)
352 monitor->priv->rate_limit_msec = limit_msecs;
353 g_object_notify (G_OBJECT (monitor), "rate-limit");
360 GFileMonitorEvent event_type;
364 file_change_free (FileChange *change)
366 g_object_unref (change->child);
367 if (change->other_file)
368 g_object_unref (change->other_file);
370 g_slice_free (FileChange, change);
374 emit_cb (gpointer data)
376 GFileMonitor *monitor = G_FILE_MONITOR (data);
377 GSList *pending, *iter;
379 pending = g_slist_reverse (monitor->priv->pending_file_changes);
380 monitor->priv->pending_file_changes = NULL;
381 if (monitor->priv->pending_file_change_source)
383 g_source_unref (monitor->priv->pending_file_change_source);
384 monitor->priv->pending_file_change_source = NULL;
387 g_object_ref (monitor);
388 for (iter = pending; iter; iter = iter->next)
390 FileChange *change = iter->data;
391 g_signal_emit (monitor, signals[CHANGED], 0,
392 change->child, change->other_file, change->event_type);
393 file_change_free (change);
395 g_slist_free (pending);
396 g_object_unref (monitor);
402 emit_in_idle (GFileMonitor *monitor,
405 GFileMonitorEvent event_type)
409 GFileMonitorPrivate *priv;
411 priv = monitor->priv;
413 change = g_slice_new (FileChange);
415 change->child = g_object_ref (child);
417 change->other_file = g_object_ref (other_file);
419 change->other_file = NULL;
420 change->event_type = event_type;
422 if (!priv->pending_file_change_source)
424 source = g_idle_source_new ();
425 priv->pending_file_change_source = source;
426 g_source_set_priority (source, 0);
428 /* We don't ref monitor here - instead dispose will free any
431 g_source_set_callback (source, emit_cb, monitor, NULL);
432 g_source_attach (source, monitor->priv->context);
434 /* We reverse this in the processor */
435 priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
439 get_time_msecs (void)
441 return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND;
445 time_difference (guint32 from, guint32 to)
452 /* Change event rate limiting support: */
455 new_limiter (GFileMonitor *monitor,
458 RateLimiter *limiter;
460 limiter = g_slice_new0 (RateLimiter);
461 limiter->file = g_object_ref (file);
462 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
468 rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
469 RateLimiter *limiter)
471 if (limiter->send_virtual_changes_done_at != 0)
473 emit_in_idle (monitor, limiter->file, NULL,
474 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
475 limiter->send_virtual_changes_done_at = 0;
480 rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
481 RateLimiter *limiter,
484 if (limiter->send_delayed_change_at != 0)
486 emit_in_idle (monitor,
488 G_FILE_MONITOR_EVENT_CHANGED);
489 limiter->send_delayed_change_at = 0;
490 limiter->last_sent_change_time = time_now;
497 GFileMonitor *monitor;
501 calc_min_time (GFileMonitor *monitor,
502 RateLimiter *limiter,
511 if (limiter->last_sent_change_time != 0)
513 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
514 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
516 if (time_difference (time_now, expire_at) > 0)
519 *min_time = MIN (*min_time,
520 time_difference (time_now, expire_at));
524 if (limiter->send_delayed_change_at != 0)
527 *min_time = MIN (*min_time,
528 time_difference (time_now, limiter->send_delayed_change_at));
531 if (limiter->send_virtual_changes_done_at != 0)
534 *min_time = MIN (*min_time,
535 time_difference (time_now, limiter->send_virtual_changes_done_at));
542 foreach_rate_limiter_fire (gpointer key,
546 RateLimiter *limiter = value;
547 ForEachData *data = user_data;
549 if (limiter->send_delayed_change_at != 0 &&
550 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
551 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
553 if (limiter->send_virtual_changes_done_at != 0 &&
554 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
555 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
557 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
561 rate_limiter_timeout (gpointer timeout_data)
563 GFileMonitor *monitor = timeout_data;
567 data.min_time = G_MAXUINT32;
568 data.monitor = monitor;
569 data.time_now = get_time_msecs ();
570 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
571 foreach_rate_limiter_fire,
574 /* Remove old timeout */
575 if (monitor->priv->timeout)
577 g_source_destroy (monitor->priv->timeout);
578 g_source_unref (monitor->priv->timeout);
579 monitor->priv->timeout = NULL;
580 monitor->priv->timeout_fires_at = 0;
583 /* Set up new timeout */
584 if (data.min_time != G_MAXUINT32)
586 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
587 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
588 g_source_attach (source, monitor->priv->context);
590 monitor->priv->timeout = source;
591 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
598 foreach_rate_limiter_update (gpointer key,
602 RateLimiter *limiter = value;
603 ForEachData *data = user_data;
605 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
609 update_rate_limiter_timeout (GFileMonitor *monitor,
615 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
616 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
617 return; /* Nothing to do, we already fire earlier than that */
619 data.min_time = G_MAXUINT32;
620 data.monitor = monitor;
621 data.time_now = get_time_msecs ();
622 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
623 foreach_rate_limiter_update,
626 /* Remove old timeout */
627 if (monitor->priv->timeout)
629 g_source_destroy (monitor->priv->timeout);
630 g_source_unref (monitor->priv->timeout);
631 monitor->priv->timeout_fires_at = 0;
632 monitor->priv->timeout = NULL;
635 /* Set up new timeout */
636 if (data.min_time != G_MAXUINT32)
638 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
639 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
640 g_source_attach (source, monitor->priv->context);
642 monitor->priv->timeout = source;
643 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
648 * g_file_monitor_emit_event:
649 * @monitor: a #GFileMonitor.
651 * @other_file: a #GFile.
652 * @event_type: a set of #GFileMonitorEvent flags.
654 * Emits the #GFileMonitor::changed signal if a change
655 * has taken place. Should be called from file monitor
656 * implementations only.
658 * The signal will be emitted from an idle handler (in the <link
659 * linkend="g-main-context-push-thread-default">thread-default main
663 g_file_monitor_emit_event (GFileMonitor *monitor,
666 GFileMonitorEvent event_type)
668 guint32 time_now, since_last;
670 RateLimiter *limiter;
672 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
673 g_return_if_fail (G_IS_FILE (child));
675 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
677 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
681 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
682 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
683 limiter->send_virtual_changes_done_at = 0;
685 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
686 update_rate_limiter_timeout (monitor, 0);
688 emit_in_idle (monitor, child, other_file, event_type);
692 /* Changed event, rate limit */
693 time_now = get_time_msecs ();
698 since_last = time_difference (limiter->last_sent_change_time, time_now);
699 if (since_last < monitor->priv->rate_limit_msec)
701 /* We ignore this change, but arm a timer so that we can fire it later if we
702 don't get any other events (that kill this timeout) */
704 if (limiter->send_delayed_change_at == 0)
706 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
707 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
713 limiter = new_limiter (monitor, child);
717 emit_in_idle (monitor, child, other_file, event_type);
719 limiter->last_sent_change_time = time_now;
720 limiter->send_delayed_change_at = 0;
721 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
722 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
725 /* Schedule a virtual change done. This is removed if we get a real one, and
726 postponed if we get more change events. */
728 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
729 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);