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_free_full (priv
->pending_file_changes
, (GDestroyNotify
) file_change_free
);
199 priv
->pending_file_changes
= NULL
;
201 /* Make sure we cancel on last unref */
202 g_file_monitor_cancel (monitor
);
204 G_OBJECT_CLASS (g_file_monitor_parent_class
)->dispose (object
);
208 g_file_monitor_class_init (GFileMonitorClass
*klass
)
210 GObjectClass
*object_class
;
212 g_type_class_add_private (klass
, sizeof (GFileMonitorPrivate
));
214 object_class
= G_OBJECT_CLASS (klass
);
215 object_class
->finalize
= g_file_monitor_finalize
;
216 object_class
->dispose
= g_file_monitor_dispose
;
217 object_class
->get_property
= g_file_monitor_get_property
;
218 object_class
->set_property
= g_file_monitor_set_property
;
221 * GFileMonitor::changed:
222 * @monitor: a #GFileMonitor.
224 * @other_file: (allow-none): a #GFile or #NULL.
225 * @event_type: a #GFileMonitorEvent.
227 * Emitted when @file has been changed.
229 * If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
230 * #G_FILE_MONITOR_SEND_MOVED, @file will be set to a #GFile containing the
231 * old path, and @other_file will be set to a #GFile containing the new path.
233 * In all the other cases, @other_file will be set to #NULL.
236 g_signal_new (I_("changed"),
239 G_STRUCT_OFFSET (GFileMonitorClass
, changed
),
243 G_TYPE_FILE
, G_TYPE_FILE
, G_TYPE_FILE_MONITOR_EVENT
);
245 g_object_class_install_property (object_class
,
247 g_param_spec_int ("rate-limit",
249 P_("The limit of the monitor to watch for changes, in milliseconds"),
251 DEFAULT_RATE_LIMIT_MSECS
,
253 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
255 g_object_class_install_property (object_class
,
257 g_param_spec_boolean ("cancelled",
259 P_("Whether the monitor has been cancelled"),
262 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
266 g_file_monitor_init (GFileMonitor
*monitor
)
268 monitor
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (monitor
,
270 GFileMonitorPrivate
);
271 monitor
->priv
->rate_limit_msec
= DEFAULT_RATE_LIMIT_MSECS
;
272 monitor
->priv
->rate_limiter
= g_hash_table_new_full (g_file_hash
, (GEqualFunc
)g_file_equal
,
273 NULL
, (GDestroyNotify
) rate_limiter_free
);
274 monitor
->priv
->context
= g_main_context_ref_thread_default ();
278 * g_file_monitor_is_cancelled:
279 * @monitor: a #GFileMonitor
281 * Returns whether the monitor is canceled.
283 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
286 g_file_monitor_is_cancelled (GFileMonitor
*monitor
)
290 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor
), FALSE
);
293 res
= monitor
->priv
->cancelled
;
294 G_UNLOCK (cancelled
);
300 * g_file_monitor_cancel:
301 * @monitor: a #GFileMonitor.
303 * Cancels a file monitor.
305 * Returns: %TRUE if monitor was cancelled.
308 g_file_monitor_cancel (GFileMonitor
* monitor
)
310 GFileMonitorClass
*klass
;
312 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor
), FALSE
);
315 if (monitor
->priv
->cancelled
)
317 G_UNLOCK (cancelled
);
321 monitor
->priv
->cancelled
= TRUE
;
322 G_UNLOCK (cancelled
);
324 g_object_notify (G_OBJECT (monitor
), "cancelled");
326 klass
= G_FILE_MONITOR_GET_CLASS (monitor
);
327 return (* klass
->cancel
) (monitor
);
331 * g_file_monitor_set_rate_limit:
332 * @monitor: a #GFileMonitor.
333 * @limit_msecs: a non-negative integer with the limit in milliseconds
334 * to poll for changes
336 * Sets the rate limit to which the @monitor will report
337 * consecutive change events to the same file.
340 g_file_monitor_set_rate_limit (GFileMonitor
*monitor
,
343 GFileMonitorPrivate
*priv
;
345 g_return_if_fail (G_IS_FILE_MONITOR (monitor
));
346 g_return_if_fail (limit_msecs
>= 0);
348 priv
= monitor
->priv
;
349 if (priv
->rate_limit_msec
!= limit_msecs
)
351 monitor
->priv
->rate_limit_msec
= limit_msecs
;
352 g_object_notify (G_OBJECT (monitor
), "rate-limit");
359 GFileMonitorEvent event_type
;
363 file_change_free (FileChange
*change
)
365 g_object_unref (change
->child
);
366 if (change
->other_file
)
367 g_object_unref (change
->other_file
);
369 g_slice_free (FileChange
, change
);
373 emit_cb (gpointer data
)
375 GFileMonitor
*monitor
= G_FILE_MONITOR (data
);
376 GSList
*pending
, *iter
;
378 pending
= g_slist_reverse (monitor
->priv
->pending_file_changes
);
379 monitor
->priv
->pending_file_changes
= NULL
;
380 if (monitor
->priv
->pending_file_change_source
)
382 g_source_unref (monitor
->priv
->pending_file_change_source
);
383 monitor
->priv
->pending_file_change_source
= NULL
;
386 g_object_ref (monitor
);
387 for (iter
= pending
; iter
; iter
= iter
->next
)
389 FileChange
*change
= iter
->data
;
390 g_signal_emit (monitor
, signals
[CHANGED
], 0,
391 change
->child
, change
->other_file
, change
->event_type
);
392 file_change_free (change
);
394 g_slist_free (pending
);
395 g_object_unref (monitor
);
401 emit_in_idle (GFileMonitor
*monitor
,
404 GFileMonitorEvent event_type
)
408 GFileMonitorPrivate
*priv
;
410 priv
= monitor
->priv
;
412 change
= g_slice_new (FileChange
);
414 change
->child
= g_object_ref (child
);
416 change
->other_file
= g_object_ref (other_file
);
418 change
->other_file
= NULL
;
419 change
->event_type
= event_type
;
421 if (!priv
->pending_file_change_source
)
423 source
= g_idle_source_new ();
424 priv
->pending_file_change_source
= source
;
425 g_source_set_priority (source
, 0);
427 /* We don't ref monitor here - instead dispose will free any
430 g_source_set_callback (source
, emit_cb
, monitor
, NULL
);
431 g_source_attach (source
, monitor
->priv
->context
);
433 /* We reverse this in the processor */
434 priv
->pending_file_changes
= g_slist_prepend (priv
->pending_file_changes
, change
);
438 get_time_msecs (void)
440 return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND
;
444 time_difference (guint32 from
, guint32 to
)
451 /* Change event rate limiting support: */
454 new_limiter (GFileMonitor
*monitor
,
457 RateLimiter
*limiter
;
459 limiter
= g_slice_new0 (RateLimiter
);
460 limiter
->file
= g_object_ref (file
);
461 g_hash_table_insert (monitor
->priv
->rate_limiter
, file
, limiter
);
467 rate_limiter_send_virtual_changes_done_now (GFileMonitor
*monitor
,
468 RateLimiter
*limiter
)
470 if (limiter
->send_virtual_changes_done_at
!= 0)
472 emit_in_idle (monitor
, limiter
->file
, NULL
,
473 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
);
474 limiter
->send_virtual_changes_done_at
= 0;
479 rate_limiter_send_delayed_change_now (GFileMonitor
*monitor
,
480 RateLimiter
*limiter
,
483 if (limiter
->send_delayed_change_at
!= 0)
485 emit_in_idle (monitor
,
487 G_FILE_MONITOR_EVENT_CHANGED
);
488 limiter
->send_delayed_change_at
= 0;
489 limiter
->last_sent_change_time
= time_now
;
496 GFileMonitor
*monitor
;
500 calc_min_time (GFileMonitor
*monitor
,
501 RateLimiter
*limiter
,
510 if (limiter
->last_sent_change_time
!= 0)
512 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
513 expire_at
= limiter
->last_sent_change_time
+ 2 * monitor
->priv
->rate_limit_msec
;
515 if (time_difference (time_now
, expire_at
) > 0)
518 *min_time
= MIN (*min_time
,
519 time_difference (time_now
, expire_at
));
523 if (limiter
->send_delayed_change_at
!= 0)
526 *min_time
= MIN (*min_time
,
527 time_difference (time_now
, limiter
->send_delayed_change_at
));
530 if (limiter
->send_virtual_changes_done_at
!= 0)
533 *min_time
= MIN (*min_time
,
534 time_difference (time_now
, limiter
->send_virtual_changes_done_at
));
541 foreach_rate_limiter_fire (gpointer key
,
545 RateLimiter
*limiter
= value
;
546 ForEachData
*data
= user_data
;
548 if (limiter
->send_delayed_change_at
!= 0 &&
549 time_difference (data
->time_now
, limiter
->send_delayed_change_at
) == 0)
550 rate_limiter_send_delayed_change_now (data
->monitor
, limiter
, data
->time_now
);
552 if (limiter
->send_virtual_changes_done_at
!= 0 &&
553 time_difference (data
->time_now
, limiter
->send_virtual_changes_done_at
) == 0)
554 rate_limiter_send_virtual_changes_done_now (data
->monitor
, limiter
);
556 return calc_min_time (data
->monitor
, limiter
, data
->time_now
, &data
->min_time
);
560 rate_limiter_timeout (gpointer timeout_data
)
562 GFileMonitor
*monitor
= timeout_data
;
566 data
.min_time
= G_MAXUINT32
;
567 data
.monitor
= monitor
;
568 data
.time_now
= get_time_msecs ();
569 g_hash_table_foreach_remove (monitor
->priv
->rate_limiter
,
570 foreach_rate_limiter_fire
,
573 /* Remove old timeout */
574 if (monitor
->priv
->timeout
)
576 g_source_destroy (monitor
->priv
->timeout
);
577 g_source_unref (monitor
->priv
->timeout
);
578 monitor
->priv
->timeout
= NULL
;
579 monitor
->priv
->timeout_fires_at
= 0;
582 /* Set up new timeout */
583 if (data
.min_time
!= G_MAXUINT32
)
585 source
= g_timeout_source_new (data
.min_time
+ 1); /* + 1 to make sure we've really passed the time */
586 g_source_set_callback (source
, rate_limiter_timeout
, monitor
, NULL
);
587 g_source_attach (source
, monitor
->priv
->context
);
589 monitor
->priv
->timeout
= source
;
590 monitor
->priv
->timeout_fires_at
= data
.time_now
+ data
.min_time
;
597 foreach_rate_limiter_update (gpointer key
,
601 RateLimiter
*limiter
= value
;
602 ForEachData
*data
= user_data
;
604 return calc_min_time (data
->monitor
, limiter
, data
->time_now
, &data
->min_time
);
608 update_rate_limiter_timeout (GFileMonitor
*monitor
,
614 if (monitor
->priv
->timeout_fires_at
!= 0 && new_time
!= 0 &&
615 time_difference (new_time
, monitor
->priv
->timeout_fires_at
) == 0)
616 return; /* Nothing to do, we already fire earlier than that */
618 data
.min_time
= G_MAXUINT32
;
619 data
.monitor
= monitor
;
620 data
.time_now
= get_time_msecs ();
621 g_hash_table_foreach_remove (monitor
->priv
->rate_limiter
,
622 foreach_rate_limiter_update
,
625 /* Remove old timeout */
626 if (monitor
->priv
->timeout
)
628 g_source_destroy (monitor
->priv
->timeout
);
629 g_source_unref (monitor
->priv
->timeout
);
630 monitor
->priv
->timeout_fires_at
= 0;
631 monitor
->priv
->timeout
= NULL
;
634 /* Set up new timeout */
635 if (data
.min_time
!= G_MAXUINT32
)
637 source
= g_timeout_source_new (data
.min_time
+ 1); /* + 1 to make sure we've really passed the time */
638 g_source_set_callback (source
, rate_limiter_timeout
, monitor
, NULL
);
639 g_source_attach (source
, monitor
->priv
->context
);
641 monitor
->priv
->timeout
= source
;
642 monitor
->priv
->timeout_fires_at
= data
.time_now
+ data
.min_time
;
647 * g_file_monitor_emit_event:
648 * @monitor: a #GFileMonitor.
650 * @other_file: a #GFile.
651 * @event_type: a set of #GFileMonitorEvent flags.
653 * Emits the #GFileMonitor::changed signal if a change
654 * has taken place. Should be called from file monitor
655 * implementations only.
657 * The signal will be emitted from an idle handler (in the <link
658 * linkend="g-main-context-push-thread-default">thread-default main
662 g_file_monitor_emit_event (GFileMonitor
*monitor
,
665 GFileMonitorEvent event_type
)
667 guint32 time_now
, since_last
;
669 RateLimiter
*limiter
;
671 g_return_if_fail (G_IS_FILE_MONITOR (monitor
));
672 g_return_if_fail (G_IS_FILE (child
));
674 limiter
= g_hash_table_lookup (monitor
->priv
->rate_limiter
, child
);
676 if (event_type
!= G_FILE_MONITOR_EVENT_CHANGED
)
680 rate_limiter_send_delayed_change_now (monitor
, limiter
, get_time_msecs ());
681 if (event_type
== G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
)
682 limiter
->send_virtual_changes_done_at
= 0;
684 rate_limiter_send_virtual_changes_done_now (monitor
, limiter
);
685 update_rate_limiter_timeout (monitor
, 0);
687 emit_in_idle (monitor
, child
, other_file
, event_type
);
691 /* Changed event, rate limit */
692 time_now
= get_time_msecs ();
697 since_last
= time_difference (limiter
->last_sent_change_time
, time_now
);
698 if (since_last
< monitor
->priv
->rate_limit_msec
)
700 /* We ignore this change, but arm a timer so that we can fire it later if we
701 don't get any other events (that kill this timeout) */
703 if (limiter
->send_delayed_change_at
== 0)
705 limiter
->send_delayed_change_at
= time_now
+ monitor
->priv
->rate_limit_msec
;
706 update_rate_limiter_timeout (monitor
, limiter
->send_delayed_change_at
);
712 limiter
= new_limiter (monitor
, child
);
716 emit_in_idle (monitor
, child
, other_file
, event_type
);
718 limiter
->last_sent_change_time
= time_now
;
719 limiter
->send_delayed_change_at
= 0;
720 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
721 update_rate_limiter_timeout (monitor
, time_now
+ 2 * monitor
->priv
->rate_limit_msec
);
724 /* Schedule a virtual change done. This is removed if we get a real one, and
725 postponed if we get more change events. */
727 limiter
->send_virtual_changes_done_at
= time_now
+ DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS
* 1000;
728 update_rate_limiter_timeout (monitor
, limiter
->send_virtual_changes_done_at
);