gio: Fix regression encoding an array of doubles
[platform/upstream/glib.git] / gio / inotify / inotify-helper.c
index 39f2feb..abf66c8 100644 (file)
@@ -16,8 +16,7 @@
 
    You should have received a copy of the GNU Library General Public
    License along with the Gnome Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.
+   see <http://www.gnu.org/licenses/>.
 
    Authors: 
                 John McCutchan <john@johnmccutchan.com>
 #include <sys/inotify.h>
 #include <gio/glocalfile.h>
 #include <gio/gfilemonitor.h>
+#include <gio/gfile.h>
 #include "inotify-helper.h"
 #include "inotify-missing.h"
 #include "inotify-path.h"
-#include "inotify-diag.h"
-
-#include "gioalias.h"
-
 
 static gboolean ih_debug_enabled = FALSE;
 #define IH_W if (ih_debug_enabled) g_warning 
 
-static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
+static void ih_event_callback (ik_event_t  *event,
+                              inotify_sub *sub,
+                              gboolean     file_event);
 static void ih_not_missing_callback (inotify_sub *sub);
 
 /* We share this lock with inotify-kernel.c and inotify-missing.c
@@ -56,7 +54,7 @@ static void ih_not_missing_callback (inotify_sub *sub);
  *
  * We take the lock in all public functions
  */
-G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
+G_LOCK_DEFINE (inotify_lock);
 
 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
 
@@ -66,7 +64,7 @@ static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
  * Initializes the inotify backend.  This must be called before
  * any other functions in this module.
  *
- * Return value: #TRUE if initialization succeeded, #FALSE otherwise
+ * Returns: #TRUE if initialization succeeded, #FALSE otherwise
  */
 gboolean
 _ih_startup (void)
@@ -85,12 +83,10 @@ _ih_startup (void)
   result = _ip_startup (ih_event_callback);
   if (!result)
     {
-      g_warning ("Could not initialize inotify\n");
       G_UNLOCK (inotify_lock);
       return FALSE;
     }
   _im_startup (ih_not_missing_callback);
-  _id_startup ();
 
   IH_W ("started gvfs inotify backend\n");
   
@@ -101,7 +97,7 @@ _ih_startup (void)
   return TRUE;
 }
 
-/**
+/*
  * Adds a subscription to be monitored.
  */
 gboolean
@@ -116,7 +112,7 @@ _ih_sub_add (inotify_sub *sub)
   return TRUE;
 }
 
-/**
+/*
  * Cancels a subscription which was being monitored.
  */
 gboolean
@@ -137,31 +133,95 @@ _ih_sub_cancel (inotify_sub *sub)
   return TRUE;
 }
 
+static char *
+_ih_fullpath_from_event (ik_event_t *event,
+                        const char *dirname,
+                        const char *filename)
+{
+  char *fullpath;
+
+  if (filename)
+    fullpath = g_strdup_printf ("%s/%s", dirname, filename);
+  else if (event->name)
+    fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
+  else
+    fullpath = g_strdup_printf ("%s/", dirname);
+
+   return fullpath;
+}
+
+
+static gboolean
+ih_event_is_paired_move (ik_event_t *event)
+{
+  if (event->pair)
+    {
+      ik_event_t *paired = event->pair;
+      /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
+      return (event->mask | paired->mask) & IN_MOVE;
+    }
+
+    return FALSE;
+}
 
 static void
 ih_event_callback (ik_event_t  *event, 
-                   inotify_sub *sub)
+                   inotify_sub *sub,
+                  gboolean     file_event)
 {
   gchar *fullpath;
   GFileMonitorEvent eflags;
-  GFile* parent;
   GFile* child;
-  
+  GFile* other;
+
   eflags = ih_mask_to_EventFlags (event->mask);
-  parent = g_file_new_for_path (sub->dirname);
-  if (event->name)
-    fullpath = g_strdup_printf ("%s/%s", sub->dirname, event->name);
-  else
-    fullpath = g_strdup_printf ("%s/", sub->dirname);
-  
+  fullpath = _ih_fullpath_from_event (event, sub->dirname,
+                                     file_event ? sub->filename : NULL);
   child = g_file_new_for_path (fullpath);
   g_free (fullpath);
 
+  if (ih_event_is_paired_move (event) && sub->pair_moves)
+    {
+      const char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
+      fullpath = _ih_fullpath_from_event (event->pair, parent_dir, NULL);
+      other = g_file_new_for_path (fullpath);
+      g_free (fullpath);
+      eflags = G_FILE_MONITOR_EVENT_MOVED;
+      event->pair = NULL; /* prevents the paired event to be emitted as well */
+    }
+  else
+    other = NULL;
+
   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
-                            child, NULL, eflags);
+                            child, other, eflags);
+
+  /* For paired moves or moves whose mask has been changed from IN_MOVED_TO to
+   * IN_CREATE, notify also that it's probably the last change to the file,
+   * emitting CHANGES_DONE_HINT.
+   * The first (first part of the if's guard below) is the case of a normal
+   * move within the monitored tree and in the same mounted volume.
+   * The latter (second part of the guard) is the case of a move within the
+   * same mounted volume, but from a not monitored directory.
+   *
+   * It's not needed in cases like moves across mounted volumes as the IN_CREATE
+   * will be followed by a IN_MODIFY and IN_CLOSE_WRITE events.
+   * Also not needed if sub->pair_moves is set as EVENT_MOVED will be emitted
+   * instead of EVENT_CREATED which implies no further modification will be
+   * applied to the file
+   * See: https://bugzilla.gnome.org/show_bug.cgi?id=640077
+   */
+  if ((!sub->pair_moves &&
+        event->is_second_in_pair && (event->mask & IN_MOVED_TO)) ||
+      (!ih_event_is_paired_move (event) &&
+       (event->original_mask & IN_MOVED_TO) && (event->mask & IN_CREATE)))
+    {
+      g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
+          child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
+    }
 
   g_object_unref (child);
-  g_object_unref (parent);
+  if (other)
+    g_object_unref (other);
 }
 
 static void
@@ -170,10 +230,7 @@ ih_not_missing_callback (inotify_sub *sub)
   gchar *fullpath;
   GFileMonitorEvent eflags;
   guint32 mask;
-  GFile* parent;
   GFile* child;
-  
-  parent = g_file_new_for_path (sub->dirname);
 
   if (sub->filename)
     {
@@ -200,7 +257,6 @@ ih_not_missing_callback (inotify_sub *sub)
                             child, NULL, eflags);
 
   g_object_unref (child);
-  g_object_unref (parent);
 }
 
 /* Transforms a inotify event to a GVFS event. */