GNOME Bugzilla – Bug 340746
Add a file monitor source
Last modified: 2008-01-07 04:39:29 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.
Created attachment 64862 [details] [review] EggFileMonitor API
Created attachment 64863 [details] EggFileMonitor implementation
Created attachment 64864 [details] simple test case
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.
Created attachment 64968 [details] EggFileMonitor API new attempt for a EggFileMonitor API
Created attachment 64969 [details] EggFileMonitor implementation new implementation of the EggFileMonitor, using hooks for plugging in an external file monitoring system
Created attachment 64970 [details] Gnome-VFS plug test simple test case, using gnome-vfs as a replacement of the internal file check implementation
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;
This is obsolete now, since gio has file monitoring.