After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 340746 - Add a file monitor source
Add a file monitor source
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: mainloop
2.11.x
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks: 336671
 
 
Reported: 2006-05-05 14:34 UTC by Emmanuele Bassi (:ebassi)
Modified: 2008-01-07 04:39 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
EggFileMonitor API (2.06 KB, patch)
2006-05-05 14:35 UTC, Emmanuele Bassi (:ebassi)
none Details | Review
EggFileMonitor implementation (7.53 KB, text/plain)
2006-05-05 14:36 UTC, Emmanuele Bassi (:ebassi)
  Details
simple test case (903 bytes, text/plain)
2006-05-05 14:37 UTC, Emmanuele Bassi (:ebassi)
  Details
EggFileMonitor API (2.17 KB, text/plain)
2006-05-07 18:32 UTC, Emmanuele Bassi (:ebassi)
  Details
EggFileMonitor implementation (10.97 KB, text/plain)
2006-05-07 18:33 UTC, Emmanuele Bassi (:ebassi)
  Details
Gnome-VFS plug test (3.39 KB, text/plain)
2006-05-07 18:34 UTC, Emmanuele Bassi (:ebassi)
  Details
EggFileMonitor implementation (12.88 KB, text/plain)
2006-05-08 21:48 UTC, Emmanuele Bassi (:ebassi)
  Details

Description Emmanuele Bassi (:ebassi) 2006-05-05 14:34:44 UTC
Simple file monitor GSource, using stat() to check for the existence of a file given its URI.

What's missing is the way to plug another file monitoring system, like gnome-vfs, into it; The idea would be to add a default hook which dispatches the source callback:

/* example using gnome-vfs */
static void
monitor_cb (GnomeVFSHandle           *handle,
            const gchar              *monitor_uri,
            const gchar              *info_uri,
            GnomeVFSMonitorEventType  event_type,
            EggFileMonitorSource       *source)
{
  gboolean res;

  switch (event_type)
    {
    case GNOME_VFS_MONITOR_EVENT_CHANGED:
      source->last_event = G_FILE_CHANGED_EVENT;
      break;
    case GNOME_VFS_MONITOR_EVENT_DELETED:
      source->last_event = G_FILE_REMOVED_EVENT:
      break;
    case GNOME_VFS_MONITOR_EVENT_CREATED:
      source->last_event = G_FILE_CREATED_EVENT;
      break;
    }

  res = egg_file_monitor_source_dispatch (source);
  if (!res)
    gnome_vfs_monitor_cancel (handle);
}

The hooks are something like this:

static gboolean
fm_add_monitor (EggFileMonitorSource *source)
{
  GnomeVFSHandle *handle;
  GnomeVFSResult res;

  res = gnome_vfs_monitor_add (&handle,
                               source->uri,
                               GNOME_VFS_MONITOR_FILE,
                               monitor_cb,
                               source);

  source->user_data = handle;

  return (res == GNOME_VFS_OK);
}

static gboolean
fm_cancel_monitor (EggFileMonitorSource *source)
{
  GnomeVFSResult res;

  res = gnome_vfs_monitor_cancel (source->user_data);

  return (res == GNOME_VFS_OK);
}

...
  static const EggFileMonitorHooks fm_hooks =
  {
    fm_add_monitor,
    fm_cancel_monitor,
    NULL,
  };

  egg_set_file_monitor_hooks (fm_hooks);
...

there are two problems/stylistic issues:

 1. EggFileMonitorSource must be public, which breaks the convention;
 2. we need a public dispatcher for the source callback, egg_file_monitor_source_dispatch(), for it to be called inside the monitor callback;

I don't know how to force the dispatch of a single GSource, in any way different that this.
Comment 1 Emmanuele Bassi (:ebassi) 2006-05-05 14:35:52 UTC
Created attachment 64862 [details] [review]
EggFileMonitor API
Comment 2 Emmanuele Bassi (:ebassi) 2006-05-05 14:36:42 UTC
Created attachment 64863 [details]
EggFileMonitor implementation
Comment 3 Emmanuele Bassi (:ebassi) 2006-05-05 14:37:08 UTC
Created attachment 64864 [details]
simple test case
Comment 4 Emmanuele Bassi (:ebassi) 2006-05-07 18:31:20 UTC
slightly better approach: I'm using hooks in key places, but obviously the whole plugging stuff is a bit involved.

the machinery works like this: the external file event notification system changes a flag inside its own callback, and the source checks for it inside the registered hook.  ugly, but works.

the internal implementation stat()s the file with an increasing timeout, in case the file has been created/changed; I'd like to add a compile-time check on the presence of inotify, to poll() the inotify fd instead; this would make things much easier.

this obviously is a *simple* file monitoring interface: for the big stuff or for directories, gnome-vfs would still be the right way to go.
Comment 5 Emmanuele Bassi (:ebassi) 2006-05-07 18:32:25 UTC
Created attachment 64968 [details]
EggFileMonitor API

new attempt for a EggFileMonitor API
Comment 6 Emmanuele Bassi (:ebassi) 2006-05-07 18:33:36 UTC
Created attachment 64969 [details]
EggFileMonitor implementation

new implementation of the EggFileMonitor, using hooks for plugging in an external file monitoring system
Comment 7 Emmanuele Bassi (:ebassi) 2006-05-07 18:34:29 UTC
Created attachment 64970 [details]
Gnome-VFS plug test

simple test case, using gnome-vfs as a replacement of the internal file check implementation
Comment 8 Emmanuele Bassi (:ebassi) 2006-05-08 21:48:40 UTC
Created attachment 65051 [details]
EggFileMonitor implementation

Changes:
  * cleaned up the code;
  * improved documentation;
  * increased POLL_DELTA_MAX to 1 sec, with the POLL_DELTA_QUANTUM set to
    100 msec; this gives a larger delay, but reduces the stat()s of the
    default implementation to one per second after ~4.5 seconds;
Comment 9 Matthias Clasen 2008-01-07 04:39:29 UTC
This is obsolete now, since gio has file monitoring.