1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
3 /* inotify-helper.c - GVFS Monitor based on inotify.
5 Copyright (C) 2007 John McCutchan
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
23 John McCutchan <john@johnmccutchan.com>
30 #include <sys/ioctl.h>
31 /* Just include the local header to stop all the pain */
32 #include "local_inotify.h"
34 #ifdef HAVE_SYS_INOTIFY_H
35 /* We don't actually include the libc header, because there has been
36 * problems with libc versions that was built without inotify support.
37 * Instead we use the local version.
39 #include "local_inotify.h"
40 #elif defined (HAVE_LINUX_INOTIFY_H)
41 #include <linux/inotify.h>
44 #include <gio/glocalfile.h>
45 #include <gio/gfilemonitor.h>
46 #include "inotify-helper.h"
47 #include "inotify-missing.h"
48 #include "inotify-path.h"
49 #include "inotify-diag.h"
54 static gboolean ih_debug_enabled = FALSE;
55 #define IH_W if (ih_debug_enabled) g_warning
57 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
58 static void ih_not_missing_callback (inotify_sub *sub);
60 /* We share this lock with inotify-kernel.c and inotify-missing.c
62 * inotify-kernel.c takes the lock when it reads events from
63 * the kernel and when it processes those events
65 * inotify-missing.c takes the lock when it is scanning the missing
68 * We take the lock in all public functions
70 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
72 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
77 * Initializes the inotify backend. This must be called before
78 * any other functions in this module.
80 * Return value: #TRUE if initialization succeeded, #FALSE otherwise
85 static gboolean initialized = FALSE;
86 static gboolean result = FALSE;
88 G_LOCK (inotify_lock);
90 if (initialized == TRUE)
92 G_UNLOCK (inotify_lock);
96 result = _ip_startup (ih_event_callback);
99 g_warning ("Could not initialize inotify\n");
100 G_UNLOCK (inotify_lock);
103 _im_startup (ih_not_missing_callback);
106 IH_W ("started gvfs inotify backend\n");
110 G_UNLOCK (inotify_lock);
116 * Adds a subscription to be monitored.
119 _ih_sub_add (inotify_sub * sub)
121 G_LOCK (inotify_lock);
123 if (!_ip_start_watching (sub))
126 G_UNLOCK (inotify_lock);
131 * Cancels a subscription which was being monitored.
134 _ih_sub_cancel (inotify_sub * sub)
136 G_LOCK (inotify_lock);
140 IH_W ("cancelling %s\n", sub->dirname);
141 sub->cancelled = TRUE;
143 _ip_stop_watching (sub);
146 G_UNLOCK (inotify_lock);
153 ih_event_callback (ik_event_t *event, inotify_sub *sub)
156 GFileMonitorEvent eflags;
160 eflags = ih_mask_to_EventFlags (event->mask);
161 parent = g_file_new_for_path (sub->dirname);
163 fullpath = g_strdup_printf ("%s/%s", sub->dirname, event->name);
165 fullpath = g_strdup_printf ("%s/", sub->dirname);
167 child = g_file_new_for_path (fullpath);
170 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
171 child, NULL, eflags);
173 g_object_unref (child);
174 g_object_unref (parent);
178 ih_not_missing_callback (inotify_sub *sub)
181 GFileMonitorEvent eflags;
186 parent = g_file_new_for_path (sub->dirname);
190 fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
191 g_warning ("Missing callback called fullpath = %s\n", fullpath);
192 if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
201 fullpath = g_strdup_printf ("%s", sub->dirname);
202 mask = IN_CREATE|IN_ISDIR;
205 eflags = ih_mask_to_EventFlags (mask);
206 child = g_file_new_for_path (fullpath);
209 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
210 child, NULL, eflags);
212 g_object_unref (child);
213 g_object_unref (parent);
216 /* Transforms a inotify event to a GVFS event. */
217 static GFileMonitorEvent
218 ih_mask_to_EventFlags (guint32 mask)
224 return G_FILE_MONITOR_EVENT_CHANGED;
226 return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
228 return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
233 return G_FILE_MONITOR_EVENT_DELETED;
236 return G_FILE_MONITOR_EVENT_CREATED;
238 return G_FILE_MONITOR_EVENT_UNMOUNTED;
241 case IN_CLOSE_NOWRITE: