2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gfilemonitor.h"
|
2008-01-07 14:56:10 +01:00
|
|
|
#include "gioenumtypes.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gfile.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gvfs.h"
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2009-03-17 22:59:18 +01:00
|
|
|
struct _FileChange;
|
|
|
|
typedef struct _FileChange FileChange;
|
|
|
|
static void file_change_free (FileChange *change);
|
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gfilemonitor
|
|
|
|
* @short_description: File Monitor
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2008-01-07 14:42:08 +01:00
|
|
|
* Monitors a file or directory for changes.
|
2008-01-25 18:38:04 +01:00
|
|
|
*
|
|
|
|
* To obtain a #GFileMonitor for a file or directory, use
|
2008-08-05 19:51:13 +02:00
|
|
|
* g_file_monitor(), g_file_monitor_file(), or
|
|
|
|
* g_file_monitor_directory().
|
2008-01-25 18:38:04 +01:00
|
|
|
*
|
2009-06-17 02:22:58 +02:00
|
|
|
* To get informed about changes to the file or directory you are
|
|
|
|
* monitoring, connect to the #GFileMonitor::changed signal. The
|
2014-02-08 18:26:56 +01:00
|
|
|
* signal will be emitted in the
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
* of the thread that the monitor was created in
|
2009-06-17 02:22:58 +02:00
|
|
|
* (though if the global default main context is blocked, this may
|
|
|
|
* cause notifications to be blocked even if the thread-default
|
|
|
|
* context is still running).
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
|
|
|
|
2008-03-05 12:50:27 +01:00
|
|
|
G_LOCK_DEFINE_STATIC(cancelled);
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
enum {
|
|
|
|
CHANGED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
typedef struct {
|
|
|
|
GFile *file;
|
|
|
|
guint32 last_sent_change_time; /* 0 == not sent */
|
|
|
|
guint32 send_delayed_change_at; /* 0 == never */
|
|
|
|
guint32 send_virtual_changes_done_at; /* 0 == never */
|
|
|
|
} RateLimiter;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
struct _GFileMonitorPrivate {
|
|
|
|
gboolean cancelled;
|
|
|
|
int rate_limit_msec;
|
|
|
|
|
|
|
|
/* Rate limiting change events */
|
2008-01-07 14:42:08 +01:00
|
|
|
GHashTable *rate_limiter;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2012-10-05 15:32:24 +02:00
|
|
|
GMutex mutex;
|
2009-06-19 16:30:14 +02:00
|
|
|
GSource *pending_file_change_source;
|
2009-03-17 22:59:18 +01:00
|
|
|
GSList *pending_file_changes; /* FileChange */
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
GSource *timeout;
|
|
|
|
guint32 timeout_fires_at;
|
2009-06-17 02:22:58 +02:00
|
|
|
|
|
|
|
GMainContext *context;
|
2007-11-26 17:13:05 +01:00
|
|
|
};
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_RATE_LIMIT,
|
2013-07-25 20:17:09 +02:00
|
|
|
PROP_CANCELLED,
|
|
|
|
PROP_CONTEXT
|
2007-12-01 07:12:45 +01:00
|
|
|
};
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
/* work around a limitation of the aliasing foo */
|
|
|
|
#undef g_file_monitor
|
|
|
|
|
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GFileMonitor *monitor;
|
|
|
|
|
|
|
|
monitor = G_FILE_MONITOR (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_RATE_LIMIT:
|
|
|
|
g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
|
|
|
|
break;
|
|
|
|
|
2013-07-25 20:17:09 +02:00
|
|
|
case PROP_CONTEXT:
|
|
|
|
monitor->priv->context = g_value_dup_boxed (value);
|
|
|
|
if (monitor->priv->context == NULL)
|
|
|
|
monitor->priv->context = g_main_context_ref_thread_default ();
|
|
|
|
break;
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_file_monitor_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GFileMonitor *monitor;
|
|
|
|
GFileMonitorPrivate *priv;
|
|
|
|
|
|
|
|
monitor = G_FILE_MONITOR (object);
|
|
|
|
priv = monitor->priv;
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_RATE_LIMIT:
|
|
|
|
g_value_set_int (value, priv->rate_limit_msec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CANCELLED:
|
2008-03-05 12:50:27 +01:00
|
|
|
G_LOCK (cancelled);
|
2007-12-01 07:12:45 +01:00
|
|
|
g_value_set_boolean (value, priv->cancelled);
|
2008-03-05 12:50:27 +01:00
|
|
|
G_UNLOCK (cancelled);
|
2007-12-01 07:12:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
#define DEFAULT_RATE_LIMIT_MSECS 800
|
|
|
|
#define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
static void
|
|
|
|
rate_limiter_free (RateLimiter *limiter)
|
|
|
|
{
|
|
|
|
g_object_unref (limiter->file);
|
|
|
|
g_slice_free (RateLimiter, limiter);
|
|
|
|
}
|
2007-12-01 07:12:45 +01:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GFileMonitor *monitor;
|
|
|
|
|
|
|
|
monitor = G_FILE_MONITOR (object);
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (monitor->priv->timeout)
|
|
|
|
{
|
|
|
|
g_source_destroy (monitor->priv->timeout);
|
|
|
|
g_source_unref (monitor->priv->timeout);
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
g_hash_table_destroy (monitor->priv->rate_limiter);
|
2008-06-16 11:54:04 +02:00
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
g_main_context_unref (monitor->priv->context);
|
2012-10-05 15:32:24 +02:00
|
|
|
g_mutex_clear (&monitor->priv->mutex);
|
2009-06-17 02:22:58 +02:00
|
|
|
|
2008-06-16 11:54:04 +02:00
|
|
|
G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_file_monitor_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GFileMonitor *monitor;
|
2009-03-17 22:59:18 +01:00
|
|
|
GFileMonitorPrivate *priv;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
monitor = G_FILE_MONITOR (object);
|
2009-03-17 22:59:18 +01:00
|
|
|
priv = monitor->priv;
|
|
|
|
|
2009-06-19 16:30:14 +02:00
|
|
|
if (priv->pending_file_change_source)
|
2009-03-17 22:59:18 +01:00
|
|
|
{
|
2009-06-19 16:30:14 +02:00
|
|
|
g_source_destroy (priv->pending_file_change_source);
|
|
|
|
g_source_unref (priv->pending_file_change_source);
|
|
|
|
priv->pending_file_change_source = NULL;
|
2009-03-17 22:59:18 +01:00
|
|
|
}
|
2012-01-05 04:27:20 +01:00
|
|
|
g_slist_free_full (priv->pending_file_changes, (GDestroyNotify) file_change_free);
|
2009-03-17 22:59:18 +01:00
|
|
|
priv->pending_file_changes = NULL;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
/* Make sure we cancel on last unref */
|
2008-03-05 12:50:27 +01:00
|
|
|
g_file_monitor_cancel (monitor);
|
2008-06-16 11:54:04 +02:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_file_monitor_class_init (GFileMonitorClass *klass)
|
|
|
|
{
|
2007-12-01 07:12:45 +01:00
|
|
|
GObjectClass *object_class;
|
2013-06-11 01:29:58 +02:00
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
object_class->finalize = g_file_monitor_finalize;
|
|
|
|
object_class->dispose = g_file_monitor_dispose;
|
|
|
|
object_class->get_property = g_file_monitor_get_property;
|
|
|
|
object_class->set_property = g_file_monitor_set_property;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* GFileMonitor::changed:
|
|
|
|
* @monitor: a #GFileMonitor.
|
|
|
|
* @file: a #GFile.
|
2012-01-07 18:54:53 +01:00
|
|
|
* @other_file: (allow-none): a #GFile or #NULL.
|
2007-11-27 15:00:13 +01:00
|
|
|
* @event_type: a #GFileMonitorEvent.
|
2010-11-25 18:38:33 +01:00
|
|
|
*
|
|
|
|
* Emitted when @file has been changed.
|
|
|
|
*
|
|
|
|
* If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
|
2012-03-27 18:45:27 +02:00
|
|
|
* #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
|
2010-11-25 18:38:33 +01:00
|
|
|
* old path, and @other_file will be set to a #GFile containing the new path.
|
|
|
|
*
|
|
|
|
* In all the other cases, @other_file will be set to #NULL.
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
2007-11-26 17:13:05 +01:00
|
|
|
signals[CHANGED] =
|
|
|
|
g_signal_new (I_("changed"),
|
|
|
|
G_TYPE_FILE_MONITOR,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GFileMonitorClass, changed),
|
|
|
|
NULL, NULL,
|
2011-07-19 19:18:10 +02:00
|
|
|
NULL,
|
2007-12-01 07:12:45 +01:00
|
|
|
G_TYPE_NONE, 3,
|
2008-01-07 14:56:10 +01:00
|
|
|
G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
|
2007-12-01 07:12:45 +01:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_RATE_LIMIT,
|
|
|
|
g_param_spec_int ("rate-limit",
|
|
|
|
P_("Rate limit"),
|
|
|
|
P_("The limit of the monitor to watch for changes, in milliseconds"),
|
|
|
|
0, G_MAXINT,
|
|
|
|
DEFAULT_RATE_LIMIT_MSECS,
|
|
|
|
G_PARAM_READWRITE|
|
|
|
|
G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_CANCELLED,
|
|
|
|
g_param_spec_boolean ("cancelled",
|
|
|
|
P_("Cancelled"),
|
|
|
|
P_("Whether the monitor has been cancelled"),
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READABLE|
|
|
|
|
G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
|
2013-07-25 20:17:09 +02:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_CONTEXT,
|
|
|
|
g_param_spec_boxed ("context",
|
|
|
|
P_("Context"),
|
|
|
|
P_("The main context to dispatch from"),
|
|
|
|
G_TYPE_MAIN_CONTEXT, G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_file_monitor_init (GFileMonitor *monitor)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
monitor->priv = g_file_monitor_get_instance_private (monitor);
|
2007-11-26 17:13:05 +01:00
|
|
|
monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
|
2008-01-07 14:42:08 +01:00
|
|
|
monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
|
|
|
|
NULL, (GDestroyNotify) rate_limiter_free);
|
2012-10-05 15:32:24 +02:00
|
|
|
g_mutex_init (&monitor->priv->mutex);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_is_cancelled:
|
2007-11-28 07:01:13 +01:00
|
|
|
* @monitor: a #GFileMonitor
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 07:01:13 +01:00
|
|
|
* Returns whether the monitor is canceled.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* Returns: %TRUE if monitor is canceled. %FALSE otherwise.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_file_monitor_is_cancelled (GFileMonitor *monitor)
|
|
|
|
{
|
2008-03-05 12:50:27 +01:00
|
|
|
gboolean res;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-03-14 12:06:01 +01:00
|
|
|
g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
|
|
|
|
|
2008-03-05 12:50:27 +01:00
|
|
|
G_LOCK (cancelled);
|
|
|
|
res = monitor->priv->cancelled;
|
|
|
|
G_UNLOCK (cancelled);
|
|
|
|
|
|
|
|
return res;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_cancel:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @monitor: a #GFileMonitor.
|
|
|
|
*
|
|
|
|
* Cancels a file monitor.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if monitor was cancelled.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_file_monitor_cancel (GFileMonitor* monitor)
|
|
|
|
{
|
|
|
|
GFileMonitorClass *klass;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
|
|
|
|
|
2008-03-05 12:50:27 +01:00
|
|
|
G_LOCK (cancelled);
|
2007-11-26 17:13:05 +01:00
|
|
|
if (monitor->priv->cancelled)
|
2008-03-05 12:50:27 +01:00
|
|
|
{
|
|
|
|
G_UNLOCK (cancelled);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
monitor->priv->cancelled = TRUE;
|
2008-03-05 12:50:27 +01:00
|
|
|
G_UNLOCK (cancelled);
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
g_object_notify (G_OBJECT (monitor), "cancelled");
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
klass = G_FILE_MONITOR_GET_CLASS (monitor);
|
|
|
|
return (* klass->cancel) (monitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_set_rate_limit:
|
|
|
|
* @monitor: a #GFileMonitor.
|
2010-11-28 23:14:49 +01:00
|
|
|
* @limit_msecs: a non-negative integer with the limit in milliseconds
|
|
|
|
* to poll for changes
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-12-12 13:19:02 +01:00
|
|
|
* Sets the rate limit to which the @monitor will report
|
2010-11-28 23:14:49 +01:00
|
|
|
* consecutive change events to the same file.
|
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
void
|
|
|
|
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
|
2010-11-28 23:14:49 +01:00
|
|
|
gint limit_msecs)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2007-12-01 07:12:45 +01:00
|
|
|
GFileMonitorPrivate *priv;
|
2010-11-28 23:14:49 +01:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
g_return_if_fail (G_IS_FILE_MONITOR (monitor));
|
2010-11-28 23:14:49 +01:00
|
|
|
g_return_if_fail (limit_msecs >= 0);
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
priv = monitor->priv;
|
|
|
|
if (priv->rate_limit_msec != limit_msecs)
|
|
|
|
{
|
|
|
|
monitor->priv->rate_limit_msec = limit_msecs;
|
|
|
|
g_object_notify (G_OBJECT (monitor), "rate-limit");
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2009-03-17 22:59:18 +01:00
|
|
|
struct _FileChange {
|
2008-02-25 13:34:30 +01:00
|
|
|
GFile *child;
|
|
|
|
GFile *other_file;
|
|
|
|
GFileMonitorEvent event_type;
|
2009-03-17 22:59:18 +01:00
|
|
|
};
|
2008-02-25 13:34:30 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
file_change_free (FileChange *change)
|
|
|
|
{
|
|
|
|
g_object_unref (change->child);
|
|
|
|
if (change->other_file)
|
|
|
|
g_object_unref (change->other_file);
|
|
|
|
|
|
|
|
g_slice_free (FileChange, change);
|
|
|
|
}
|
|
|
|
|
2009-03-17 22:59:18 +01:00
|
|
|
static gboolean
|
|
|
|
emit_cb (gpointer data)
|
|
|
|
{
|
|
|
|
GFileMonitor *monitor = G_FILE_MONITOR (data);
|
|
|
|
GSList *pending, *iter;
|
2012-10-05 15:32:24 +02:00
|
|
|
|
|
|
|
g_mutex_lock (&monitor->priv->mutex);
|
2009-03-17 22:59:18 +01:00
|
|
|
pending = g_slist_reverse (monitor->priv->pending_file_changes);
|
|
|
|
monitor->priv->pending_file_changes = NULL;
|
2009-06-19 16:30:14 +02:00
|
|
|
if (monitor->priv->pending_file_change_source)
|
|
|
|
{
|
|
|
|
g_source_unref (monitor->priv->pending_file_change_source);
|
|
|
|
monitor->priv->pending_file_change_source = NULL;
|
|
|
|
}
|
2012-10-05 15:32:24 +02:00
|
|
|
g_mutex_unlock (&monitor->priv->mutex);
|
2009-03-17 22:59:18 +01:00
|
|
|
|
2009-05-06 10:20:43 +02:00
|
|
|
g_object_ref (monitor);
|
2009-03-17 22:59:18 +01:00
|
|
|
for (iter = pending; iter; iter = iter->next)
|
|
|
|
{
|
|
|
|
FileChange *change = iter->data;
|
2012-10-05 15:32:24 +02:00
|
|
|
|
2009-03-17 22:59:18 +01:00
|
|
|
g_signal_emit (monitor, signals[CHANGED], 0,
|
|
|
|
change->child, change->other_file, change->event_type);
|
|
|
|
file_change_free (change);
|
|
|
|
}
|
|
|
|
g_slist_free (pending);
|
2009-05-06 10:20:43 +02:00
|
|
|
g_object_unref (monitor);
|
|
|
|
|
2009-03-17 22:59:18 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-02-25 13:34:30 +01:00
|
|
|
static void
|
|
|
|
emit_in_idle (GFileMonitor *monitor,
|
|
|
|
GFile *child,
|
|
|
|
GFile *other_file,
|
|
|
|
GFileMonitorEvent event_type)
|
|
|
|
{
|
|
|
|
GSource *source;
|
|
|
|
FileChange *change;
|
2009-03-17 22:59:18 +01:00
|
|
|
GFileMonitorPrivate *priv;
|
|
|
|
|
|
|
|
priv = monitor->priv;
|
2008-02-25 13:34:30 +01:00
|
|
|
|
|
|
|
change = g_slice_new (FileChange);
|
|
|
|
|
|
|
|
change->child = g_object_ref (child);
|
|
|
|
if (other_file)
|
|
|
|
change->other_file = g_object_ref (other_file);
|
|
|
|
else
|
|
|
|
change->other_file = NULL;
|
|
|
|
change->event_type = event_type;
|
|
|
|
|
2012-10-05 15:32:24 +02:00
|
|
|
g_mutex_lock (&monitor->priv->mutex);
|
2009-06-19 16:30:14 +02:00
|
|
|
if (!priv->pending_file_change_source)
|
2009-03-17 22:59:18 +01:00
|
|
|
{
|
|
|
|
source = g_idle_source_new ();
|
2009-06-19 16:30:14 +02:00
|
|
|
priv->pending_file_change_source = source;
|
2009-03-17 22:59:18 +01:00
|
|
|
g_source_set_priority (source, 0);
|
|
|
|
|
2009-06-19 16:30:14 +02:00
|
|
|
/* We don't ref monitor here - instead dispose will free any
|
2009-03-17 22:59:18 +01:00
|
|
|
* pending idles.
|
|
|
|
*/
|
|
|
|
g_source_set_callback (source, emit_cb, monitor, NULL);
|
2014-03-22 13:15:45 +01:00
|
|
|
g_source_set_name (source, "[gio] emit_cb");
|
2009-06-17 02:22:58 +02:00
|
|
|
g_source_attach (source, monitor->priv->context);
|
2009-03-17 22:59:18 +01:00
|
|
|
}
|
|
|
|
/* We reverse this in the processor */
|
|
|
|
priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
|
2012-10-05 15:32:24 +02:00
|
|
|
g_mutex_unlock (&monitor->priv->mutex);
|
2008-02-25 13:34:30 +01:00
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static guint32
|
|
|
|
get_time_msecs (void)
|
|
|
|
{
|
2011-08-31 21:49:35 +02:00
|
|
|
return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint32
|
|
|
|
time_difference (guint32 from, guint32 to)
|
|
|
|
{
|
|
|
|
if (from > to)
|
|
|
|
return 0;
|
|
|
|
return to - from;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change event rate limiting support: */
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
static RateLimiter *
|
|
|
|
new_limiter (GFileMonitor *monitor,
|
|
|
|
GFile *file)
|
|
|
|
{
|
|
|
|
RateLimiter *limiter;
|
|
|
|
|
|
|
|
limiter = g_slice_new0 (RateLimiter);
|
|
|
|
limiter->file = g_object_ref (file);
|
|
|
|
g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
|
|
|
|
|
|
|
|
return limiter;
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
2008-01-07 14:42:08 +01:00
|
|
|
rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
|
|
|
|
RateLimiter *limiter)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_virtual_changes_done_at != 0)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-02-25 13:34:30 +01:00
|
|
|
emit_in_idle (monitor, limiter->file, NULL,
|
|
|
|
G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
|
2008-01-07 14:42:08 +01:00
|
|
|
limiter->send_virtual_changes_done_at = 0;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-07 14:42:08 +01:00
|
|
|
rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
|
|
|
|
RateLimiter *limiter,
|
|
|
|
guint32 time_now)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_delayed_change_at != 0)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-02-25 13:34:30 +01:00
|
|
|
emit_in_idle (monitor,
|
|
|
|
limiter->file, NULL,
|
|
|
|
G_FILE_MONITOR_EVENT_CHANGED);
|
2008-01-07 14:42:08 +01:00
|
|
|
limiter->send_delayed_change_at = 0;
|
|
|
|
limiter->last_sent_change_time = time_now;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
typedef struct {
|
|
|
|
guint32 min_time;
|
|
|
|
guint32 time_now;
|
|
|
|
GFileMonitor *monitor;
|
|
|
|
} ForEachData;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static gboolean
|
2008-01-07 14:42:08 +01:00
|
|
|
calc_min_time (GFileMonitor *monitor,
|
|
|
|
RateLimiter *limiter,
|
|
|
|
guint32 time_now,
|
|
|
|
guint32 *min_time)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
gboolean delete_me;
|
|
|
|
guint32 expire_at;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
delete_me = TRUE;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->last_sent_change_time != 0)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2011-08-29 20:49:32 +02:00
|
|
|
/* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
|
2008-01-07 14:42:08 +01:00
|
|
|
expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
|
|
|
|
|
|
|
|
if (time_difference (time_now, expire_at) > 0)
|
|
|
|
{
|
|
|
|
delete_me = FALSE;
|
|
|
|
*min_time = MIN (*min_time,
|
|
|
|
time_difference (time_now, expire_at));
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_delayed_change_at != 0)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
delete_me = FALSE;
|
|
|
|
*min_time = MIN (*min_time,
|
|
|
|
time_difference (time_now, limiter->send_delayed_change_at));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_virtual_changes_done_at != 0)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
delete_me = FALSE;
|
|
|
|
*min_time = MIN (*min_time,
|
|
|
|
time_difference (time_now, limiter->send_virtual_changes_done_at));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
2008-01-07 14:42:08 +01:00
|
|
|
|
|
|
|
return delete_me;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2008-01-07 14:42:08 +01:00
|
|
|
foreach_rate_limiter_fire (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
RateLimiter *limiter = value;
|
|
|
|
ForEachData *data = user_data;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_delayed_change_at != 0 &&
|
|
|
|
time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
|
|
|
|
rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_virtual_changes_done_at != 0 &&
|
|
|
|
time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
|
|
|
|
rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
|
|
|
|
|
|
|
|
return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
static gboolean
|
|
|
|
rate_limiter_timeout (gpointer timeout_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
GFileMonitor *monitor = timeout_data;
|
|
|
|
ForEachData data;
|
2007-11-26 17:13:05 +01:00
|
|
|
GSource *source;
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
data.min_time = G_MAXUINT32;
|
|
|
|
data.monitor = monitor;
|
|
|
|
data.time_now = get_time_msecs ();
|
|
|
|
g_hash_table_foreach_remove (monitor->priv->rate_limiter,
|
|
|
|
foreach_rate_limiter_fire,
|
|
|
|
&data);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
/* Remove old timeout */
|
|
|
|
if (monitor->priv->timeout)
|
|
|
|
{
|
|
|
|
g_source_destroy (monitor->priv->timeout);
|
|
|
|
g_source_unref (monitor->priv->timeout);
|
|
|
|
monitor->priv->timeout = NULL;
|
|
|
|
monitor->priv->timeout_fires_at = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up new timeout */
|
|
|
|
if (data.min_time != G_MAXUINT32)
|
|
|
|
{
|
|
|
|
source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
|
|
|
|
g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
|
2009-06-17 02:22:58 +02:00
|
|
|
g_source_attach (source, monitor->priv->context);
|
2008-01-07 14:42:08 +01:00
|
|
|
|
|
|
|
monitor->priv->timeout = source;
|
|
|
|
monitor->priv->timeout_fires_at = data.time_now + data.min_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
foreach_rate_limiter_update (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
RateLimiter *limiter = value;
|
|
|
|
ForEachData *data = user_data;
|
|
|
|
|
|
|
|
return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-07 14:42:08 +01:00
|
|
|
update_rate_limiter_timeout (GFileMonitor *monitor,
|
|
|
|
guint new_time)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
ForEachData data;
|
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
|
|
|
|
time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
|
|
|
|
return; /* Nothing to do, we already fire earlier than that */
|
|
|
|
|
|
|
|
data.min_time = G_MAXUINT32;
|
|
|
|
data.monitor = monitor;
|
|
|
|
data.time_now = get_time_msecs ();
|
|
|
|
g_hash_table_foreach_remove (monitor->priv->rate_limiter,
|
|
|
|
foreach_rate_limiter_update,
|
|
|
|
&data);
|
|
|
|
|
|
|
|
/* Remove old timeout */
|
|
|
|
if (monitor->priv->timeout)
|
|
|
|
{
|
|
|
|
g_source_destroy (monitor->priv->timeout);
|
|
|
|
g_source_unref (monitor->priv->timeout);
|
|
|
|
monitor->priv->timeout_fires_at = 0;
|
|
|
|
monitor->priv->timeout = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up new timeout */
|
|
|
|
if (data.min_time != G_MAXUINT32)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
|
|
|
|
g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
|
2009-06-17 02:22:58 +02:00
|
|
|
g_source_attach (source, monitor->priv->context);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
monitor->priv->timeout = source;
|
|
|
|
monitor->priv->timeout_fires_at = data.time_now + data.min_time;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_emit_event:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @monitor: a #GFileMonitor.
|
2008-01-07 14:42:08 +01:00
|
|
|
* @child: a #GFile.
|
2007-11-27 15:00:13 +01:00
|
|
|
* @other_file: a #GFile.
|
2008-01-07 14:42:08 +01:00
|
|
|
* @event_type: a set of #GFileMonitorEvent flags.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2008-01-07 14:42:08 +01:00
|
|
|
* Emits the #GFileMonitor::changed signal if a change
|
|
|
|
* has taken place. Should be called from file monitor
|
|
|
|
* implementations only.
|
2008-02-25 13:34:30 +01:00
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
* The signal will be emitted from an idle handler (in the
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]).
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
void
|
2008-01-21 04:49:20 +01:00
|
|
|
g_file_monitor_emit_event (GFileMonitor *monitor,
|
|
|
|
GFile *child,
|
|
|
|
GFile *other_file,
|
|
|
|
GFileMonitorEvent event_type)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
guint32 time_now, since_last;
|
|
|
|
gboolean emit_now;
|
2008-01-07 14:42:08 +01:00
|
|
|
RateLimiter *limiter;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
g_return_if_fail (G_IS_FILE_MONITOR (monitor));
|
2008-01-07 14:42:08 +01:00
|
|
|
g_return_if_fail (G_IS_FILE (child));
|
|
|
|
|
|
|
|
limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
|
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter)
|
|
|
|
{
|
|
|
|
rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
|
|
|
|
if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
|
|
|
|
limiter->send_virtual_changes_done_at = 0;
|
|
|
|
else
|
|
|
|
rate_limiter_send_virtual_changes_done_now (monitor, limiter);
|
|
|
|
update_rate_limiter_timeout (monitor, 0);
|
|
|
|
}
|
2008-02-25 13:34:30 +01:00
|
|
|
emit_in_idle (monitor, child, other_file, event_type);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
/* Changed event, rate limit */
|
2007-11-26 17:13:05 +01:00
|
|
|
time_now = get_time_msecs ();
|
|
|
|
emit_now = TRUE;
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2008-01-07 14:42:08 +01:00
|
|
|
since_last = time_difference (limiter->last_sent_change_time, time_now);
|
2007-11-26 17:13:05 +01:00
|
|
|
if (since_last < monitor->priv->rate_limit_msec)
|
|
|
|
{
|
|
|
|
/* We ignore this change, but arm a timer so that we can fire it later if we
|
|
|
|
don't get any other events (that kill this timeout) */
|
|
|
|
emit_now = FALSE;
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter->send_delayed_change_at == 0)
|
|
|
|
{
|
|
|
|
limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
|
|
|
|
update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
|
|
|
|
}
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
if (limiter == NULL)
|
|
|
|
limiter = new_limiter (monitor, child);
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
if (emit_now)
|
|
|
|
{
|
2008-02-25 13:34:30 +01:00
|
|
|
emit_in_idle (monitor, child, other_file, event_type);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-01-07 14:42:08 +01:00
|
|
|
limiter->last_sent_change_time = time_now;
|
|
|
|
limiter->send_delayed_change_at = 0;
|
2011-08-29 20:49:32 +02:00
|
|
|
/* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
|
2008-01-07 14:42:08 +01:00
|
|
|
update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
2008-01-07 14:42:08 +01:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
/* Schedule a virtual change done. This is removed if we get a real one, and
|
|
|
|
postponed if we get more change events. */
|
2008-01-07 14:42:08 +01:00
|
|
|
|
|
|
|
limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
|
|
|
|
update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
}
|