docs: use "Returns:" consistently
[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    see <http://www.gnu.org/licenses/>.
20
21    Authors: 
22                  John McCutchan <john@johnmccutchan.com>
23 */
24
25 #include "config.h"
26 #include <errno.h>
27 #include <time.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 /* Just include the local header to stop all the pain */
31 #include <sys/inotify.h>
32 #include <gio/glocalfile.h>
33 #include <gio/gfilemonitor.h>
34 #include <gio/gfile.h>
35 #include "inotify-helper.h"
36 #include "inotify-missing.h"
37 #include "inotify-path.h"
38
39 static gboolean ih_debug_enabled = FALSE;
40 #define IH_W if (ih_debug_enabled) g_warning 
41
42 static void ih_event_callback (ik_event_t  *event,
43                                inotify_sub *sub,
44                                gboolean     file_event);
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_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  * Returns: #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
91   IH_W ("started gvfs inotify backend\n");
92   
93   initialized = TRUE;
94   
95   G_UNLOCK (inotify_lock);
96   
97   return TRUE;
98 }
99
100 /*
101  * Adds a subscription to be monitored.
102  */
103 gboolean
104 _ih_sub_add (inotify_sub *sub)
105 {
106   G_LOCK (inotify_lock);
107         
108   if (!_ip_start_watching (sub))
109     _im_add (sub);
110   
111   G_UNLOCK (inotify_lock);
112   return TRUE;
113 }
114
115 /*
116  * Cancels a subscription which was being monitored.
117  */
118 gboolean
119 _ih_sub_cancel (inotify_sub *sub)
120 {
121   G_LOCK (inotify_lock);
122
123   if (!sub->cancelled)
124     {
125       IH_W ("cancelling %s\n", sub->dirname);
126       sub->cancelled = TRUE;
127       _im_rm (sub);
128       _ip_stop_watching (sub);
129     }
130   
131   G_UNLOCK (inotify_lock);
132   
133   return TRUE;
134 }
135
136 static char *
137 _ih_fullpath_from_event (ik_event_t *event,
138                          const char *dirname,
139                          const char *filename)
140 {
141   char *fullpath;
142
143   if (filename)
144     fullpath = g_strdup_printf ("%s/%s", dirname, filename);
145   else if (event->name)
146     fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
147   else
148     fullpath = g_strdup_printf ("%s/", dirname);
149
150    return fullpath;
151 }
152
153
154 static gboolean
155 ih_event_is_paired_move (ik_event_t *event)
156 {
157   if (event->pair)
158     {
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;
162     }
163
164     return FALSE;
165 }
166
167 static void
168 ih_event_callback (ik_event_t  *event, 
169                    inotify_sub *sub,
170                    gboolean     file_event)
171 {
172   gchar *fullpath;
173   GFileMonitorEvent eflags;
174   GFile* child;
175   GFile* other;
176
177   eflags = ih_mask_to_EventFlags (event->mask);
178   fullpath = _ih_fullpath_from_event (event, sub->dirname,
179                                       file_event ? sub->filename : NULL);
180   child = g_file_new_for_path (fullpath);
181   g_free (fullpath);
182
183   if (ih_event_is_paired_move (event) && sub->pair_moves)
184     {
185       const char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
186       fullpath = _ih_fullpath_from_event (event->pair, parent_dir, NULL);
187       other = g_file_new_for_path (fullpath);
188       g_free (fullpath);
189       eflags = G_FILE_MONITOR_EVENT_MOVED;
190       event->pair = NULL; /* prevents the paired event to be emitted as well */
191     }
192   else
193     other = NULL;
194
195   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
196                              child, other, eflags);
197
198   /* For paired moves or moves whose mask has been changed from IN_MOVED_TO to
199    * IN_CREATE, notify also that it's probably the last change to the file,
200    * emitting CHANGES_DONE_HINT.
201    * The first (first part of the if's guard below) is the case of a normal
202    * move within the monitored tree and in the same mounted volume.
203    * The latter (second part of the guard) is the case of a move within the
204    * same mounted volume, but from a not monitored directory.
205    *
206    * It's not needed in cases like moves across mounted volumes as the IN_CREATE
207    * will be followed by a IN_MODIFY and IN_CLOSE_WRITE events.
208    * Also not needed if sub->pair_moves is set as EVENT_MOVED will be emitted
209    * instead of EVENT_CREATED which implies no further modification will be
210    * applied to the file
211    * See: https://bugzilla.gnome.org/show_bug.cgi?id=640077
212    */
213   if ((!sub->pair_moves &&
214         event->is_second_in_pair && (event->mask & IN_MOVED_TO)) ||
215       (!ih_event_is_paired_move (event) &&
216        (event->original_mask & IN_MOVED_TO) && (event->mask & IN_CREATE)))
217     {
218       g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
219           child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
220     }
221
222   g_object_unref (child);
223   if (other)
224     g_object_unref (other);
225 }
226
227 static void
228 ih_not_missing_callback (inotify_sub *sub)
229 {
230   gchar *fullpath;
231   GFileMonitorEvent eflags;
232   guint32 mask;
233   GFile* child;
234
235   if (sub->filename)
236     {
237       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
238       g_warning ("Missing callback called fullpath = %s\n", fullpath);
239       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
240         {
241           g_free (fullpath);
242           return;
243         }
244       mask = IN_CREATE;
245     }
246   else
247     {
248       fullpath = g_strdup_printf ("%s", sub->dirname);
249       mask = IN_CREATE|IN_ISDIR;
250     }
251
252   eflags = ih_mask_to_EventFlags (mask);
253   child = g_file_new_for_path (fullpath);
254   g_free (fullpath);
255
256   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
257                              child, NULL, eflags);
258
259   g_object_unref (child);
260 }
261
262 /* Transforms a inotify event to a GVFS event. */
263 static GFileMonitorEvent
264 ih_mask_to_EventFlags (guint32 mask)
265 {
266   mask &= ~IN_ISDIR;
267   switch (mask)
268     {
269     case IN_MODIFY:
270       return G_FILE_MONITOR_EVENT_CHANGED;
271     case IN_CLOSE_WRITE:
272       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
273     case IN_ATTRIB:
274       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
275     case IN_MOVE_SELF:
276     case IN_MOVED_FROM:
277     case IN_DELETE:
278     case IN_DELETE_SELF:
279       return G_FILE_MONITOR_EVENT_DELETED;
280     case IN_CREATE:
281     case IN_MOVED_TO:
282       return G_FILE_MONITOR_EVENT_CREATED;
283     case IN_UNMOUNT:
284       return G_FILE_MONITOR_EVENT_UNMOUNTED;
285     case IN_Q_OVERFLOW:
286     case IN_OPEN:
287     case IN_CLOSE_NOWRITE:
288     case IN_ACCESS:
289     case IN_IGNORED:
290     default:
291       return -1;
292     }
293 }