Drop GVFS_INOTIFY_DIAG debug feature
[platform/upstream/glib.git] / gio / inotify / inotify-helper.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
2
3 /* inotify-helper.c - GVFS Monitor based on inotify.
4
5    Copyright (C) 2007 John McCutchan
6
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.
11
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.
16
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.
21
22    Authors: 
23                  John McCutchan <john@johnmccutchan.com>
24 */
25
26 #include "config.h"
27 #include <errno.h>
28 #include <time.h>
29 #include <string.h>
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
40 static gboolean ih_debug_enabled = FALSE;
41 #define IH_W if (ih_debug_enabled) g_warning 
42
43 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
44 static void ih_not_missing_callback (inotify_sub *sub);
45
46 /* We share this lock with inotify-kernel.c and inotify-missing.c
47  *
48  * inotify-kernel.c takes the lock when it reads events from
49  * the kernel and when it processes those events
50  *
51  * inotify-missing.c takes the lock when it is scanning the missing
52  * list.
53  *
54  * We take the lock in all public functions
55  */
56 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
57
58 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
59
60 /**
61  * _ih_startup:
62  *
63  * Initializes the inotify backend.  This must be called before
64  * any other functions in this module.
65  *
66  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
67  */
68 gboolean
69 _ih_startup (void)
70 {
71   static gboolean initialized = FALSE;
72   static gboolean result = FALSE;
73   
74   G_LOCK (inotify_lock);
75   
76   if (initialized == TRUE)
77     {
78       G_UNLOCK (inotify_lock);
79       return result;
80     }
81
82   result = _ip_startup (ih_event_callback);
83   if (!result)
84     {
85       G_UNLOCK (inotify_lock);
86       return FALSE;
87     }
88   _im_startup (ih_not_missing_callback);
89
90   IH_W ("started gvfs inotify backend\n");
91   
92   initialized = TRUE;
93   
94   G_UNLOCK (inotify_lock);
95   
96   return TRUE;
97 }
98
99 /*
100  * Adds a subscription to be monitored.
101  */
102 gboolean
103 _ih_sub_add (inotify_sub *sub)
104 {
105   G_LOCK (inotify_lock);
106         
107   if (!_ip_start_watching (sub))
108     _im_add (sub);
109   
110   G_UNLOCK (inotify_lock);
111   return TRUE;
112 }
113
114 /*
115  * Cancels a subscription which was being monitored.
116  */
117 gboolean
118 _ih_sub_cancel (inotify_sub *sub)
119 {
120   G_LOCK (inotify_lock);
121
122   if (!sub->cancelled)
123     {
124       IH_W ("cancelling %s\n", sub->dirname);
125       sub->cancelled = TRUE;
126       _im_rm (sub);
127       _ip_stop_watching (sub);
128     }
129   
130   G_UNLOCK (inotify_lock);
131   
132   return TRUE;
133 }
134
135 static char *
136 _ih_fullpath_from_event (ik_event_t *event, const char *dirname)
137 {
138   char *fullpath;
139
140   if (event->name)
141     fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
142   else
143     fullpath = g_strdup_printf ("%s/", dirname);
144
145    return fullpath;
146 }
147
148
149 static gboolean
150 ih_event_is_paired_move (ik_event_t *event)
151 {
152   if (event->pair)
153     {
154       ik_event_t *paired = event->pair;
155       /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
156       return (event->mask | paired->mask) & IN_MOVE;
157     }
158
159     return FALSE;
160 }
161
162 static void
163 ih_event_callback (ik_event_t  *event, 
164                    inotify_sub *sub)
165 {
166   gchar *fullpath;
167   GFileMonitorEvent eflags;
168   GFile* child;
169   GFile* other;
170
171   eflags = ih_mask_to_EventFlags (event->mask);
172   fullpath = _ih_fullpath_from_event (event, sub->dirname);
173   child = g_file_new_for_path (fullpath);
174   g_free (fullpath);
175
176   if (ih_event_is_paired_move (event) && sub->pair_moves)
177     {
178       const char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
179       fullpath = _ih_fullpath_from_event (event->pair, parent_dir);
180       other = g_file_new_for_path (fullpath);
181       g_free (fullpath);
182       eflags = G_FILE_MONITOR_EVENT_MOVED;
183       event->pair = NULL; /* prevents the paired event to be emitted as well */
184     }
185   else
186     other = NULL;
187
188   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
189                              child, other, eflags);
190
191   /* For paired moves or moves whose mask has been changed from IN_MOVED_TO to
192    * IN_CREATE, notify also that it's probably the last change to the file,
193    * emitting CHANGES_DONE_HINT.
194    * The first (first part of the if's guard below) is the case of a normal
195    * move within the monitored tree and in the same mounted volume.
196    * The latter (second part of the guard) is the case of a move within the
197    * same mounted volume, but from a not monitored directory.
198    *
199    * It's not needed in cases like moves across mounted volumes as the IN_CREATE
200    * will be followed by a IN_MODIFY and IN_CLOSE_WRITE events.
201    * Also not needed if sub->pair_moves is set as EVENT_MOVED will be emitted
202    * instead of EVENT_CREATED which implies no further modification will be
203    * applied to the file
204    * See: https://bugzilla.gnome.org/show_bug.cgi?id=640077
205    */
206   if ((!sub->pair_moves &&
207         event->is_second_in_pair && (event->mask & IN_MOVED_TO)) ||
208       (!ih_event_is_paired_move (event) &&
209        (event->original_mask & IN_MOVED_TO) && (event->mask & IN_CREATE)))
210     {
211       g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
212           child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
213     }
214
215   g_object_unref (child);
216   if (other)
217     g_object_unref (other);
218 }
219
220 static void
221 ih_not_missing_callback (inotify_sub *sub)
222 {
223   gchar *fullpath;
224   GFileMonitorEvent eflags;
225   guint32 mask;
226   GFile* child;
227
228   if (sub->filename)
229     {
230       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
231       g_warning ("Missing callback called fullpath = %s\n", fullpath);
232       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
233         {
234           g_free (fullpath);
235           return;
236         }
237       mask = IN_CREATE;
238     }
239   else
240     {
241       fullpath = g_strdup_printf ("%s", sub->dirname);
242       mask = IN_CREATE|IN_ISDIR;
243     }
244
245   eflags = ih_mask_to_EventFlags (mask);
246   child = g_file_new_for_path (fullpath);
247   g_free (fullpath);
248
249   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
250                              child, NULL, eflags);
251
252   g_object_unref (child);
253 }
254
255 /* Transforms a inotify event to a GVFS event. */
256 static GFileMonitorEvent
257 ih_mask_to_EventFlags (guint32 mask)
258 {
259   mask &= ~IN_ISDIR;
260   switch (mask)
261     {
262     case IN_MODIFY:
263       return G_FILE_MONITOR_EVENT_CHANGED;
264     case IN_CLOSE_WRITE:
265       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
266     case IN_ATTRIB:
267       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
268     case IN_MOVE_SELF:
269     case IN_MOVED_FROM:
270     case IN_DELETE:
271     case IN_DELETE_SELF:
272       return G_FILE_MONITOR_EVENT_DELETED;
273     case IN_CREATE:
274     case IN_MOVED_TO:
275       return G_FILE_MONITOR_EVENT_CREATED;
276     case IN_UNMOUNT:
277       return G_FILE_MONITOR_EVENT_UNMOUNTED;
278     case IN_Q_OVERFLOW:
279     case IN_OPEN:
280     case IN_CLOSE_NOWRITE:
281     case IN_ACCESS:
282     case IN_IGNORED:
283     default:
284       return -1;
285     }
286 }