Send CHANGES_DONE_HINT on file moves if no IN_CLOSE_WRITE is emitted
[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 #include "inotify-diag.h"
40
41 static gboolean ih_debug_enabled = FALSE;
42 #define IH_W if (ih_debug_enabled) g_warning 
43
44 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
45 static void ih_not_missing_callback (inotify_sub *sub);
46
47 /* We share this lock with inotify-kernel.c and inotify-missing.c
48  *
49  * inotify-kernel.c takes the lock when it reads events from
50  * the kernel and when it processes those events
51  *
52  * inotify-missing.c takes the lock when it is scanning the missing
53  * list.
54  *
55  * We take the lock in all public functions
56  */
57 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
58
59 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
60
61 /**
62  * _ih_startup:
63  *
64  * Initializes the inotify backend.  This must be called before
65  * any other functions in this module.
66  *
67  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
68  */
69 gboolean
70 _ih_startup (void)
71 {
72   static gboolean initialized = FALSE;
73   static gboolean result = FALSE;
74   
75   G_LOCK (inotify_lock);
76   
77   if (initialized == TRUE)
78     {
79       G_UNLOCK (inotify_lock);
80       return result;
81     }
82
83   result = _ip_startup (ih_event_callback);
84   if (!result)
85     {
86       G_UNLOCK (inotify_lock);
87       return FALSE;
88     }
89   _im_startup (ih_not_missing_callback);
90   _id_startup ();
91
92   IH_W ("started gvfs inotify backend\n");
93   
94   initialized = TRUE;
95   
96   G_UNLOCK (inotify_lock);
97   
98   return TRUE;
99 }
100
101 /*
102  * Adds a subscription to be monitored.
103  */
104 gboolean
105 _ih_sub_add (inotify_sub *sub)
106 {
107   G_LOCK (inotify_lock);
108         
109   if (!_ip_start_watching (sub))
110     _im_add (sub);
111   
112   G_UNLOCK (inotify_lock);
113   return TRUE;
114 }
115
116 /*
117  * Cancels a subscription which was being monitored.
118  */
119 gboolean
120 _ih_sub_cancel (inotify_sub *sub)
121 {
122   G_LOCK (inotify_lock);
123
124   if (!sub->cancelled)
125     {
126       IH_W ("cancelling %s\n", sub->dirname);
127       sub->cancelled = TRUE;
128       _im_rm (sub);
129       _ip_stop_watching (sub);
130     }
131   
132   G_UNLOCK (inotify_lock);
133   
134   return TRUE;
135 }
136
137 static char *
138 _ih_fullpath_from_event (ik_event_t *event, const char *dirname)
139 {
140   char *fullpath;
141
142   if (event->name)
143     fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
144   else
145     fullpath = g_strdup_printf ("%s/", dirname);
146
147    return fullpath;
148 }
149
150
151 static gboolean
152 ih_event_is_paired_move (ik_event_t *event)
153 {
154   if (event->pair)
155     {
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;
159     }
160
161     return FALSE;
162 }
163
164 static void
165 ih_event_callback (ik_event_t  *event, 
166                    inotify_sub *sub)
167 {
168   gchar *fullpath;
169   GFileMonitorEvent eflags;
170   GFile* child;
171   GFile* other;
172
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);
176   g_free (fullpath);
177
178   if (ih_event_is_paired_move (event) && sub->pair_moves)
179     {
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);
183       g_free (fullpath);
184       eflags = G_FILE_MONITOR_EVENT_MOVED;
185       event->pair = NULL; /* prevents the paired event to be emitted as well */
186     }
187   else
188     other = NULL;
189
190   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
191                              child, other, eflags);
192
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.
200    *
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
207    */
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)))
212     {
213       g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
214           child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
215     }
216
217   g_object_unref (child);
218   if (other)
219     g_object_unref (other);
220 }
221
222 static void
223 ih_not_missing_callback (inotify_sub *sub)
224 {
225   gchar *fullpath;
226   GFileMonitorEvent eflags;
227   guint32 mask;
228   GFile* child;
229
230   if (sub->filename)
231     {
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))
235         {
236           g_free (fullpath);
237           return;
238         }
239       mask = IN_CREATE;
240     }
241   else
242     {
243       fullpath = g_strdup_printf ("%s", sub->dirname);
244       mask = IN_CREATE|IN_ISDIR;
245     }
246
247   eflags = ih_mask_to_EventFlags (mask);
248   child = g_file_new_for_path (fullpath);
249   g_free (fullpath);
250
251   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
252                              child, NULL, eflags);
253
254   g_object_unref (child);
255 }
256
257 /* Transforms a inotify event to a GVFS event. */
258 static GFileMonitorEvent
259 ih_mask_to_EventFlags (guint32 mask)
260 {
261   mask &= ~IN_ISDIR;
262   switch (mask)
263     {
264     case IN_MODIFY:
265       return G_FILE_MONITOR_EVENT_CHANGED;
266     case IN_CLOSE_WRITE:
267       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
268     case IN_ATTRIB:
269       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
270     case IN_MOVE_SELF:
271     case IN_MOVED_FROM:
272     case IN_DELETE:
273     case IN_DELETE_SELF:
274       return G_FILE_MONITOR_EVENT_DELETED;
275     case IN_CREATE:
276     case IN_MOVED_TO:
277       return G_FILE_MONITOR_EVENT_CREATED;
278     case IN_UNMOUNT:
279       return G_FILE_MONITOR_EVENT_UNMOUNTED;
280     case IN_Q_OVERFLOW:
281     case IN_OPEN:
282     case IN_CLOSE_NOWRITE:
283     case IN_ACCESS:
284     case IN_IGNORED:
285     default:
286       return -1;
287     }
288 }