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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gfilemonitor.h"
25 #include "gioenumtypes.h"
32 typedef struct _FileChange FileChange;
33 static void file_change_free (FileChange *change);
36 * SECTION:gfilemonitor
37 * @short_description: File Monitor
40 * Monitors a file or directory for changes.
42 * To obtain a #GFileMonitor for a file or directory, use
43 * g_file_monitor(), g_file_monitor_file(), or
44 * g_file_monitor_directory().
46 * To get informed about changes to the file or directory you are
47 * monitoring, connect to the #GFileMonitor::changed signal. The
48 * signal will be emitted in the
49 * [thread-default main context][g-main-context-push-thread-default]
50 * of the thread that the monitor was created in
51 * (though if the global default main context is blocked, this may
52 * cause notifications to be blocked even if the thread-default
53 * context is still running).
56 G_LOCK_DEFINE_STATIC(cancelled);
65 guint32 last_sent_change_time; /* 0 == not sent */
66 guint32 send_delayed_change_at; /* 0 == never */
67 guint32 send_virtual_changes_done_at; /* 0 == never */
70 struct _GFileMonitorPrivate {
74 /* Rate limiting change events */
75 GHashTable *rate_limiter;
78 GSource *pending_file_change_source;
79 GSList *pending_file_changes; /* FileChange */
82 guint32 timeout_fires_at;
84 GMainContext *context;
94 /* work around a limitation of the aliasing foo */
97 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
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 monitor->priv->context = g_value_dup_boxed (value);
117 if (monitor->priv->context == NULL)
118 monitor->priv->context = g_main_context_ref_thread_default ();
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 g_file_monitor_get_property (GObject *object,
133 GFileMonitor *monitor;
134 GFileMonitorPrivate *priv;
136 monitor = G_FILE_MONITOR (object);
137 priv = monitor->priv;
141 case PROP_RATE_LIMIT:
142 g_value_set_int (value, priv->rate_limit_msec);
147 g_value_set_boolean (value, priv->cancelled);
148 G_UNLOCK (cancelled);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 #define DEFAULT_RATE_LIMIT_MSECS 800
158 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
160 static guint signals[LAST_SIGNAL] = { 0 };
163 rate_limiter_free (RateLimiter *limiter)
165 g_object_unref (limiter->file);
166 g_slice_free (RateLimiter, limiter);
170 g_file_monitor_finalize (GObject *object)
172 GFileMonitor *monitor;
174 monitor = G_FILE_MONITOR (object);
176 if (monitor->priv->timeout)
178 g_source_destroy (monitor->priv->timeout);
179 g_source_unref (monitor->priv->timeout);
182 g_hash_table_destroy (monitor->priv->rate_limiter);
184 g_main_context_unref (monitor->priv->context);
185 g_mutex_clear (&monitor->priv->mutex);
187 G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
191 g_file_monitor_dispose (GObject *object)
193 GFileMonitor *monitor;
194 GFileMonitorPrivate *priv;
196 monitor = G_FILE_MONITOR (object);
197 priv = monitor->priv;
199 if (priv->pending_file_change_source)
201 g_source_destroy (priv->pending_file_change_source);
202 g_source_unref (priv->pending_file_change_source);
203 priv->pending_file_change_source = NULL;
205 g_slist_free_full (priv->pending_file_changes, (GDestroyNotify) file_change_free);
206 priv->pending_file_changes = NULL;
208 /* Make sure we cancel on last unref */
209 g_file_monitor_cancel (monitor);
211 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
215 g_file_monitor_class_init (GFileMonitorClass *klass)
217 GObjectClass *object_class;
219 object_class = G_OBJECT_CLASS (klass);
220 object_class->finalize = g_file_monitor_finalize;
221 object_class->dispose = g_file_monitor_dispose;
222 object_class->get_property = g_file_monitor_get_property;
223 object_class->set_property = g_file_monitor_set_property;
226 * GFileMonitor::changed:
227 * @monitor: a #GFileMonitor.
229 * @other_file: (allow-none): a #GFile or #NULL.
230 * @event_type: a #GFileMonitorEvent.
232 * Emitted when @file has been changed.
234 * If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
235 * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
236 * old path, and @other_file will be set to a #GFile containing the new path.
238 * In all the other cases, @other_file will be set to #NULL.
241 g_signal_new (I_("changed"),
244 G_STRUCT_OFFSET (GFileMonitorClass, changed),
248 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
250 g_object_class_install_property (object_class,
252 g_param_spec_int ("rate-limit",
254 P_("The limit of the monitor to watch for changes, in milliseconds"),
256 DEFAULT_RATE_LIMIT_MSECS,
258 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
260 g_object_class_install_property (object_class,
262 g_param_spec_boolean ("cancelled",
264 P_("Whether the monitor has been cancelled"),
267 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
269 g_object_class_install_property (object_class,
271 g_param_spec_boxed ("context",
273 P_("The main context to dispatch from"),
274 G_TYPE_MAIN_CONTEXT, G_PARAM_WRITABLE |
275 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
279 g_file_monitor_init (GFileMonitor *monitor)
281 monitor->priv = g_file_monitor_get_instance_private (monitor);
282 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
283 monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
284 NULL, (GDestroyNotify) rate_limiter_free);
285 g_mutex_init (&monitor->priv->mutex);
289 * g_file_monitor_is_cancelled:
290 * @monitor: a #GFileMonitor
292 * Returns whether the monitor is canceled.
294 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
297 g_file_monitor_is_cancelled (GFileMonitor *monitor)
301 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
304 res = monitor->priv->cancelled;
305 G_UNLOCK (cancelled);
311 * g_file_monitor_cancel:
312 * @monitor: a #GFileMonitor.
314 * Cancels a file monitor.
316 * Returns: %TRUE if monitor was cancelled.
319 g_file_monitor_cancel (GFileMonitor* monitor)
321 GFileMonitorClass *klass;
323 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
326 if (monitor->priv->cancelled)
328 G_UNLOCK (cancelled);
332 monitor->priv->cancelled = TRUE;
333 G_UNLOCK (cancelled);
335 g_object_notify (G_OBJECT (monitor), "cancelled");
337 klass = G_FILE_MONITOR_GET_CLASS (monitor);
338 return (* klass->cancel) (monitor);
342 * g_file_monitor_set_rate_limit:
343 * @monitor: a #GFileMonitor.
344 * @limit_msecs: a non-negative integer with the limit in milliseconds
345 * to poll for changes
347 * Sets the rate limit to which the @monitor will report
348 * consecutive change events to the same file.
351 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
354 GFileMonitorPrivate *priv;
356 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
357 g_return_if_fail (limit_msecs >= 0);
359 priv = monitor->priv;
360 if (priv->rate_limit_msec != limit_msecs)
362 monitor->priv->rate_limit_msec = limit_msecs;
363 g_object_notify (G_OBJECT (monitor), "rate-limit");
370 GFileMonitorEvent event_type;
374 file_change_free (FileChange *change)
376 g_object_unref (change->child);
377 if (change->other_file)
378 g_object_unref (change->other_file);
380 g_slice_free (FileChange, change);
384 emit_cb (gpointer data)
386 GFileMonitor *monitor = G_FILE_MONITOR (data);
387 GSList *pending, *iter;
389 g_mutex_lock (&monitor->priv->mutex);
390 pending = g_slist_reverse (monitor->priv->pending_file_changes);
391 monitor->priv->pending_file_changes = NULL;
392 if (monitor->priv->pending_file_change_source)
394 g_source_unref (monitor->priv->pending_file_change_source);
395 monitor->priv->pending_file_change_source = NULL;
397 g_mutex_unlock (&monitor->priv->mutex);
399 g_object_ref (monitor);
400 for (iter = pending; iter; iter = iter->next)
402 FileChange *change = iter->data;
404 g_signal_emit (monitor, signals[CHANGED], 0,
405 change->child, change->other_file, change->event_type);
406 file_change_free (change);
408 g_slist_free (pending);
409 g_object_unref (monitor);
415 emit_in_idle (GFileMonitor *monitor,
418 GFileMonitorEvent event_type)
422 GFileMonitorPrivate *priv;
424 priv = monitor->priv;
426 change = g_slice_new (FileChange);
428 change->child = g_object_ref (child);
430 change->other_file = g_object_ref (other_file);
432 change->other_file = NULL;
433 change->event_type = event_type;
435 g_mutex_lock (&monitor->priv->mutex);
436 if (!priv->pending_file_change_source)
438 source = g_idle_source_new ();
439 priv->pending_file_change_source = source;
440 g_source_set_priority (source, 0);
442 /* We don't ref monitor here - instead dispose will free any
445 g_source_set_callback (source, emit_cb, monitor, NULL);
446 g_source_set_name (source, "[gio] emit_cb");
447 g_source_attach (source, monitor->priv->context);
449 /* We reverse this in the processor */
450 priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
451 g_mutex_unlock (&monitor->priv->mutex);
455 get_time_msecs (void)
457 return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND;
461 time_difference (guint32 from, guint32 to)
468 /* Change event rate limiting support: */
471 new_limiter (GFileMonitor *monitor,
474 RateLimiter *limiter;
476 limiter = g_slice_new0 (RateLimiter);
477 limiter->file = g_object_ref (file);
478 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
484 rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
485 RateLimiter *limiter)
487 if (limiter->send_virtual_changes_done_at != 0)
489 emit_in_idle (monitor, limiter->file, NULL,
490 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
491 limiter->send_virtual_changes_done_at = 0;
496 rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
497 RateLimiter *limiter,
500 if (limiter->send_delayed_change_at != 0)
502 emit_in_idle (monitor,
504 G_FILE_MONITOR_EVENT_CHANGED);
505 limiter->send_delayed_change_at = 0;
506 limiter->last_sent_change_time = time_now;
513 GFileMonitor *monitor;
517 calc_min_time (GFileMonitor *monitor,
518 RateLimiter *limiter,
527 if (limiter->last_sent_change_time != 0)
529 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
530 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
532 if (time_difference (time_now, expire_at) > 0)
535 *min_time = MIN (*min_time,
536 time_difference (time_now, expire_at));
540 if (limiter->send_delayed_change_at != 0)
543 *min_time = MIN (*min_time,
544 time_difference (time_now, limiter->send_delayed_change_at));
547 if (limiter->send_virtual_changes_done_at != 0)
550 *min_time = MIN (*min_time,
551 time_difference (time_now, limiter->send_virtual_changes_done_at));
558 foreach_rate_limiter_fire (gpointer key,
562 RateLimiter *limiter = value;
563 ForEachData *data = user_data;
565 if (limiter->send_delayed_change_at != 0 &&
566 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
567 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
569 if (limiter->send_virtual_changes_done_at != 0 &&
570 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
571 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
573 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
577 rate_limiter_timeout (gpointer timeout_data)
579 GFileMonitor *monitor = timeout_data;
583 data.min_time = G_MAXUINT32;
584 data.monitor = monitor;
585 data.time_now = get_time_msecs ();
586 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
587 foreach_rate_limiter_fire,
590 /* Remove old timeout */
591 if (monitor->priv->timeout)
593 g_source_destroy (monitor->priv->timeout);
594 g_source_unref (monitor->priv->timeout);
595 monitor->priv->timeout = NULL;
596 monitor->priv->timeout_fires_at = 0;
599 /* Set up new timeout */
600 if (data.min_time != G_MAXUINT32)
602 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
603 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
604 g_source_attach (source, monitor->priv->context);
606 monitor->priv->timeout = source;
607 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
614 foreach_rate_limiter_update (gpointer key,
618 RateLimiter *limiter = value;
619 ForEachData *data = user_data;
621 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
625 update_rate_limiter_timeout (GFileMonitor *monitor,
631 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
632 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
633 return; /* Nothing to do, we already fire earlier than that */
635 data.min_time = G_MAXUINT32;
636 data.monitor = monitor;
637 data.time_now = get_time_msecs ();
638 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
639 foreach_rate_limiter_update,
642 /* Remove old timeout */
643 if (monitor->priv->timeout)
645 g_source_destroy (monitor->priv->timeout);
646 g_source_unref (monitor->priv->timeout);
647 monitor->priv->timeout_fires_at = 0;
648 monitor->priv->timeout = NULL;
651 /* Set up new timeout */
652 if (data.min_time != G_MAXUINT32)
654 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
655 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
656 g_source_attach (source, monitor->priv->context);
658 monitor->priv->timeout = source;
659 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
664 * g_file_monitor_emit_event:
665 * @monitor: a #GFileMonitor.
667 * @other_file: a #GFile.
668 * @event_type: a set of #GFileMonitorEvent flags.
670 * Emits the #GFileMonitor::changed signal if a change
671 * has taken place. Should be called from file monitor
672 * implementations only.
674 * The signal will be emitted from an idle handler (in the
675 * [thread-default main context][g-main-context-push-thread-default]).
678 g_file_monitor_emit_event (GFileMonitor *monitor,
681 GFileMonitorEvent event_type)
683 guint32 time_now, since_last;
685 RateLimiter *limiter;
687 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
688 g_return_if_fail (G_IS_FILE (child));
690 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
692 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
696 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
697 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
698 limiter->send_virtual_changes_done_at = 0;
700 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
701 update_rate_limiter_timeout (monitor, 0);
703 emit_in_idle (monitor, child, other_file, event_type);
707 /* Changed event, rate limit */
708 time_now = get_time_msecs ();
713 since_last = time_difference (limiter->last_sent_change_time, time_now);
714 if (since_last < monitor->priv->rate_limit_msec)
716 /* We ignore this change, but arm a timer so that we can fire it later if we
717 don't get any other events (that kill this timeout) */
719 if (limiter->send_delayed_change_at == 0)
721 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
722 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
728 limiter = new_limiter (monitor, child);
732 emit_in_idle (monitor, child, other_file, event_type);
734 limiter->last_sent_change_time = time_now;
735 limiter->send_delayed_change_at = 0;
736 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
737 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
740 /* Schedule a virtual change done. This is removed if we get a real one, and
741 postponed if we get more change events. */
743 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
744 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);