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 <sys/inotify.h>
33 #include <gio/glocalfile.h>
34 #include <gio/gfilemonitor.h>
35 #include <gio/gfile.h>
36 #include "inotify-helper.h"
37 #include "inotify-missing.h"
38 #include "inotify-path.h"
39 #include "inotify-diag.h"
44 static gboolean ih_debug_enabled = FALSE;
45 #define IH_W if (ih_debug_enabled) g_warning
47 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
48 static void ih_not_missing_callback (inotify_sub *sub);
50 /* We share this lock with inotify-kernel.c and inotify-missing.c
52 * inotify-kernel.c takes the lock when it reads events from
53 * the kernel and when it processes those events
55 * inotify-missing.c takes the lock when it is scanning the missing
58 * We take the lock in all public functions
60 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
62 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
67 * Initializes the inotify backend. This must be called before
68 * any other functions in this module.
70 * Return value: #TRUE if initialization succeeded, #FALSE otherwise
75 static gboolean initialized = FALSE;
76 static gboolean result = FALSE;
78 G_LOCK (inotify_lock);
80 if (initialized == TRUE)
82 G_UNLOCK (inotify_lock);
86 result = _ip_startup (ih_event_callback);
89 G_UNLOCK (inotify_lock);
92 _im_startup (ih_not_missing_callback);
95 IH_W ("started gvfs inotify backend\n");
99 G_UNLOCK (inotify_lock);
105 * Adds a subscription to be monitored.
108 _ih_sub_add (inotify_sub *sub)
110 G_LOCK (inotify_lock);
112 if (!_ip_start_watching (sub))
115 G_UNLOCK (inotify_lock);
120 * Cancels a subscription which was being monitored.
123 _ih_sub_cancel (inotify_sub *sub)
125 G_LOCK (inotify_lock);
129 IH_W ("cancelling %s\n", sub->dirname);
130 sub->cancelled = TRUE;
132 _ip_stop_watching (sub);
135 G_UNLOCK (inotify_lock);
141 _ih_fullpath_from_event (ik_event_t *event, char *dirname)
146 fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
148 fullpath = g_strdup_printf ("%s/", dirname);
155 ih_event_is_paired_move (ik_event_t *event)
159 ik_event_t *paired = event->pair;
160 /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
161 return (event->mask | paired->mask) & IN_MOVE;
168 ih_event_callback (ik_event_t *event,
172 GFileMonitorEvent eflags;
177 eflags = ih_mask_to_EventFlags (event->mask);
178 parent = g_file_new_for_path (sub->dirname);
179 fullpath = _ih_fullpath_from_event (event, sub->dirname);
180 child = g_file_new_for_path (fullpath);
183 if (ih_event_is_paired_move (event) && sub->pair_moves)
185 char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
186 fullpath = _ih_fullpath_from_event (event->pair, parent_dir);
187 other = g_file_new_for_path (fullpath);
189 eflags = G_FILE_MONITOR_EVENT_MOVED;
190 event->pair = NULL; /* prevents the paired event to be emitted as well */
195 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
196 child, other, eflags);
198 g_object_unref (child);
199 g_object_unref (parent);
201 g_object_unref (other);
205 ih_not_missing_callback (inotify_sub *sub)
208 GFileMonitorEvent eflags;
213 parent = g_file_new_for_path (sub->dirname);
217 fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
218 g_warning ("Missing callback called fullpath = %s\n", fullpath);
219 if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
228 fullpath = g_strdup_printf ("%s", sub->dirname);
229 mask = IN_CREATE|IN_ISDIR;
232 eflags = ih_mask_to_EventFlags (mask);
233 child = g_file_new_for_path (fullpath);
236 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
237 child, NULL, eflags);
239 g_object_unref (child);
240 g_object_unref (parent);
243 /* Transforms a inotify event to a GVFS event. */
244 static GFileMonitorEvent
245 ih_mask_to_EventFlags (guint32 mask)
251 return G_FILE_MONITOR_EVENT_CHANGED;
253 return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
255 return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
260 return G_FILE_MONITOR_EVENT_DELETED;
263 return G_FILE_MONITOR_EVENT_CREATED;
265 return G_FILE_MONITOR_EVENT_UNMOUNTED;
268 case IN_CLOSE_NOWRITE: