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, see <http://www.gnu.org/licenses/>.
18 * Authors: Alexander Larsson <alexl@redhat.com>
19 * John McCutchan <john@johnmccutchan.com>
20 * Sebastian Dröge <slomo@circular-chaos.org>
25 #include <gio/gfilemonitor.h>
26 #include <gio/gfile.h>
28 #include "fam-helper.h"
30 static FAMConnection* fam_connection = NULL;
31 static gint fam_watch_id = 0;
32 G_LOCK_DEFINE_STATIC(fam_connection);
43 /* This uses int as the argument type because the
44 real type differs between implementations:
45 gamin has "typedef enum FAMCodes {....} FAMCodes;"
46 fam has "enum FAMCodes { ... }".
48 static GFileMonitorEvent
49 fam_event_to_file_monitor_event (int code)
54 return G_FILE_MONITOR_EVENT_CHANGED;
57 return G_FILE_MONITOR_EVENT_DELETED;
60 return G_FILE_MONITOR_EVENT_CREATED;
69 fam_do_iter_unlocked (void)
71 while (fam_connection != NULL && FAMPending (fam_connection))
77 if (FAMNextEvent (fam_connection, &ev) != 1)
79 FAMClose (fam_connection);
80 g_free (fam_connection);
81 g_source_remove (fam_watch_id);
83 fam_connection = NULL;
87 sub = (fam_sub*)ev.userdata;
88 cancelled = sub->cancelled;
89 if (ev.code == FAMAcknowledge && cancelled)
100 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
101 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
103 GFile *child, *parent;
105 /* unsupported event */
109 if (ev.filename[0] == '/')
110 path = g_strdup (ev.filename);
112 path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
114 child = g_file_new_for_path (path);
115 parent = g_file_get_parent (child);
116 g_file_monitor_emit_event (monitor, child, NULL, eflags);
118 g_object_unref (child);
119 g_object_unref (parent);
124 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
125 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
130 path = g_strdup (ev.filename);
131 child = g_file_new_for_path (path);
132 g_file_monitor_emit_event (monitor, child, NULL, eflags);
134 g_object_unref (child);
142 fam_callback (GIOChannel *source,
143 GIOCondition condition,
147 G_LOCK (fam_connection);
149 res = fam_do_iter_unlocked ();
151 G_UNLOCK (fam_connection);
156 _fam_sub_startup (void)
160 G_LOCK (fam_connection);
162 if (fam_connection == NULL)
164 fam_connection = g_new0 (FAMConnection, 1);
165 if (FAMOpen2 (fam_connection, "gvfs user") != 0)
167 g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno);
168 g_free (fam_connection);
169 fam_connection = NULL;
170 G_UNLOCK (fam_connection);
173 #ifdef HAVE_FAM_NO_EXISTS
174 /* This is a gamin extension that avoids sending all the Exists event for dir monitors */
175 FAMNoExists (fam_connection);
177 ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection));
178 fam_watch_id = g_io_add_watch (ioc,
179 G_IO_IN | G_IO_HUP | G_IO_ERR,
180 fam_callback, fam_connection);
181 g_io_channel_unref (ioc);
184 G_UNLOCK (fam_connection);
190 _fam_sub_shutdown (void)
192 G_LOCK (fam_connection);
194 if (fam_connection != NULL)
196 FAMClose (fam_connection);
197 g_free (fam_connection);
198 g_source_remove (fam_watch_id);
200 fam_connection = NULL;
203 G_UNLOCK (fam_connection);
207 _fam_sub_add (const gchar *pathname,
213 if (!_fam_sub_startup ())
216 G_LOCK (fam_connection);
217 /* We need to queue up incoming messages to avoid blocking on write
218 * if there are many monitors being canceled */
219 fam_do_iter_unlocked ();
221 if (fam_connection == NULL)
223 G_UNLOCK (fam_connection);
227 sub = g_new0 (fam_sub, 1);
228 sub->pathname = g_strdup (pathname);
229 sub->directory = directory;
230 sub->user_data = user_data;
233 FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub);
235 FAMMonitorFile (fam_connection, pathname, &sub->request, sub);
237 G_UNLOCK (fam_connection);
243 _fam_sub_cancel (fam_sub* sub)
248 sub->cancelled = TRUE;
250 G_LOCK (fam_connection);
251 /* We need to queue up incoming messages to avoid blocking on write
252 * if there are many monitors being canceled */
253 fam_do_iter_unlocked ();
255 if (fam_connection == NULL)
257 G_UNLOCK (fam_connection);
261 FAMCancelMonitor (fam_connection, &sub->request);
263 G_UNLOCK (fam_connection);
269 _fam_sub_free (fam_sub* sub)
271 g_free (sub->pathname);