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"
41 static gboolean ih_debug_enabled = FALSE;
42 #define IH_W if (ih_debug_enabled) g_warning
44 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
45 static void ih_not_missing_callback (inotify_sub *sub);
47 /* We share this lock with inotify-kernel.c and inotify-missing.c
49 * inotify-kernel.c takes the lock when it reads events from
50 * the kernel and when it processes those events
52 * inotify-missing.c takes the lock when it is scanning the missing
55 * We take the lock in all public functions
57 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
59 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
64 * Initializes the inotify backend. This must be called before
65 * any other functions in this module.
67 * Return value: #TRUE if initialization succeeded, #FALSE otherwise
72 static gboolean initialized = FALSE;
73 static gboolean result = FALSE;
75 G_LOCK (inotify_lock);
77 if (initialized == TRUE)
79 G_UNLOCK (inotify_lock);
83 result = _ip_startup (ih_event_callback);
86 G_UNLOCK (inotify_lock);
89 _im_startup (ih_not_missing_callback);
92 IH_W ("started gvfs inotify backend\n");
96 G_UNLOCK (inotify_lock);
102 * Adds a subscription to be monitored.
105 _ih_sub_add (inotify_sub *sub)
107 G_LOCK (inotify_lock);
109 if (!_ip_start_watching (sub))
112 G_UNLOCK (inotify_lock);
117 * Cancels a subscription which was being monitored.
120 _ih_sub_cancel (inotify_sub *sub)
122 G_LOCK (inotify_lock);
126 IH_W ("cancelling %s\n", sub->dirname);
127 sub->cancelled = TRUE;
129 _ip_stop_watching (sub);
132 G_UNLOCK (inotify_lock);
138 _ih_fullpath_from_event (ik_event_t *event, const char *dirname)
143 fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
145 fullpath = g_strdup_printf ("%s/", dirname);
152 ih_event_is_paired_move (ik_event_t *event)
156 ik_event_t *paired = event->pair;
157 /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
158 return (event->mask | paired->mask) & IN_MOVE;
165 ih_event_callback (ik_event_t *event,
169 GFileMonitorEvent eflags;
173 eflags = ih_mask_to_EventFlags (event->mask);
174 fullpath = _ih_fullpath_from_event (event, sub->dirname);
175 child = g_file_new_for_path (fullpath);
178 if (ih_event_is_paired_move (event) && sub->pair_moves)
180 const char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
181 fullpath = _ih_fullpath_from_event (event->pair, parent_dir);
182 other = g_file_new_for_path (fullpath);
184 eflags = G_FILE_MONITOR_EVENT_MOVED;
185 event->pair = NULL; /* prevents the paired event to be emitted as well */
190 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
191 child, other, eflags);
193 /* For paired moves or moves whose mask has been changed from IN_MOVED_TO to
194 * IN_CREATE, notify also that it's probably the last change to the file,
195 * emitting CHANGES_DONE_HINT.
196 * The first (first part of the if's guard below) is the case of a normal
197 * move within the monitored tree and in the same mounted volume.
198 * The latter (second part of the guard) is the case of a move within the
199 * same mounted volume, but from a not monitored directory.
201 * It's not needed in cases like moves across mounted volumes as the IN_CREATE
202 * will be followed by a IN_MODIFY and IN_CLOSE_WRITE events.
203 * Also not needed if sub->pair_moves is set as EVENT_MOVED will be emitted
204 * instead of EVENT_CREATED which implies no further modification will be
205 * applied to the file
206 * See: https://bugzilla.gnome.org/show_bug.cgi?id=640077
208 if ((!sub->pair_moves &&
209 event->is_second_in_pair && (event->mask & IN_MOVED_TO)) ||
210 (!ih_event_is_paired_move (event) &&
211 (event->original_mask & IN_MOVED_TO) && (event->mask & IN_CREATE)))
213 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
214 child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
217 g_object_unref (child);
219 g_object_unref (other);
223 ih_not_missing_callback (inotify_sub *sub)
226 GFileMonitorEvent eflags;
232 fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
233 g_warning ("Missing callback called fullpath = %s\n", fullpath);
234 if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
243 fullpath = g_strdup_printf ("%s", sub->dirname);
244 mask = IN_CREATE|IN_ISDIR;
247 eflags = ih_mask_to_EventFlags (mask);
248 child = g_file_new_for_path (fullpath);
251 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
252 child, NULL, eflags);
254 g_object_unref (child);
257 /* Transforms a inotify event to a GVFS event. */
258 static GFileMonitorEvent
259 ih_mask_to_EventFlags (guint32 mask)
265 return G_FILE_MONITOR_EVENT_CHANGED;
267 return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
269 return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
274 return G_FILE_MONITOR_EVENT_DELETED;
277 return G_FILE_MONITOR_EVENT_CREATED;
279 return G_FILE_MONITOR_EVENT_UNMOUNTED;
282 case IN_CLOSE_NOWRITE: