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 * Authors: Alexander Larsson <alexl@redhat.com>
21 * John McCutchan <john@johnmccutchan.com>
22 * Sebastian Dröge <slomo@circular-chaos.org>
27 #include <gio/gfilemonitor.h>
29 #include "fam-helper.h"
31 static FAMConnection* fam_connection = NULL;
32 static gint fam_watch_id = 0;
33 G_LOCK_DEFINE_STATIC(fam_connection);
44 /* This uses int as the argument type because the
45 real type differs between implementations:
46 gamin has "typedef enum FAMCodes {....} FAMCodes;"
47 fam has "enum FAMCodes { ... }".
49 static GFileMonitorEvent
50 fam_event_to_file_monitor_event (int code)
55 return G_FILE_MONITOR_EVENT_CHANGED;
58 return G_FILE_MONITOR_EVENT_DELETED;
61 return G_FILE_MONITOR_EVENT_CREATED;
70 fam_do_iter_unlocked (void)
72 while (fam_connection != NULL && FAMPending (fam_connection)) {
77 if (FAMNextEvent (fam_connection, &ev) != 1) {
78 FAMClose (fam_connection);
79 g_free (fam_connection);
80 g_source_remove (fam_watch_id);
82 fam_connection = NULL;
86 sub = (fam_sub*)ev.userdata;
87 cancelled = sub->cancelled;
88 if (ev.code == FAMAcknowledge && cancelled)
99 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
100 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
102 GFile *child, *parent;
104 /* unsupported event */
108 if (ev.filename[0] == '/')
109 path = g_strdup (ev.filename);
111 path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
113 child = g_file_new_for_path (path);
114 parent = g_file_get_parent (child);
115 g_file_monitor_emit_event (monitor, child, NULL, eflags);
117 g_object_unref (child);
118 g_object_unref (parent);
121 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
122 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
127 path = g_strdup (ev.filename);
128 child = g_file_new_for_path (path);
129 g_file_monitor_emit_event (monitor, child, NULL, eflags);
131 g_object_unref (child);
139 fam_callback (GIOChannel *source,
140 GIOCondition condition,
144 G_LOCK (fam_connection);
146 res = fam_do_iter_unlocked ();
148 G_UNLOCK (fam_connection);
153 _fam_sub_startup (void)
157 G_LOCK (fam_connection);
159 if (fam_connection == NULL) {
160 fam_connection = g_new0 (FAMConnection, 1);
161 if (FAMOpen2 (fam_connection, "gvfs user") != 0) {
162 g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno);
163 g_free (fam_connection);
164 fam_connection = NULL;
165 G_UNLOCK (fam_connection);
168 #ifdef HAVE_FAM_NO_EXISTS
169 /* This is a gamin extension that avoids sending all the Exists event for dir monitors */
170 FAMNoExists (fam_connection);
172 ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection));
173 fam_watch_id = g_io_add_watch (ioc,
174 G_IO_IN | G_IO_HUP | G_IO_ERR,
175 fam_callback, fam_connection);
176 g_io_channel_unref (ioc);
179 G_UNLOCK (fam_connection);
185 _fam_sub_add (const gchar* pathname,
191 if (!_fam_sub_startup ())
194 sub = g_new0 (fam_sub, 1);
195 sub->pathname = g_strdup (pathname);
196 sub->directory = directory;
197 sub->user_data = user_data;
199 G_LOCK (fam_connection);
200 /* We need to queue up incoming messages to avoid blocking on write
201 * if there are many monitors being canceled */
202 fam_do_iter_unlocked ();
204 if (fam_connection == NULL) {
205 G_UNLOCK (fam_connection);
210 FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub);
212 FAMMonitorFile (fam_connection, pathname, &sub->request, sub);
214 G_UNLOCK (fam_connection);
219 _fam_sub_cancel (fam_sub* sub)
224 sub->cancelled = TRUE;
226 G_LOCK (fam_connection);
227 /* We need to queue up incoming messages to avoid blocking on write
228 * if there are many monitors being canceled */
229 fam_do_iter_unlocked ();
231 if (fam_connection == NULL) {
232 G_UNLOCK (fam_connection);
236 FAMCancelMonitor (fam_connection, &sub->request);
238 G_UNLOCK (fam_connection);
244 _fam_sub_free (fam_sub* sub)
246 g_free (sub->pathname);