[gvfs/gnome-3-24] gdaemonfile: Fix relative path handling



commit 09fb94bda9d1a1d57bc1158dbf9135f27fd38bbd
Author: Ondrej Holy <oholy redhat com>
Date:   Tue Aug 15 10:12:33 2017 +0200

    gdaemonfile: Fix relative path handling
    
    g_daemon_file_get_relative_path() fails for files with different
    mount_prefix and always return NULL. It happen when comparing two files
    from different origins, e.g. g_mount_get_root() and g_file_new_for_uri().
    
    On the other hand, g_daemon_file_prefix_matches() can succeed in cases,
    where paths don't have the same prefixes, because it just compares
    mount_prefix of a parent with a path of a descendant, but a path of the
    parent is ignored.
    
    The code concatenates mount_prefix with a path, so the comparison never
    succeeds if mount_prefix is set, because mount_prefix is already part of
    the path in GDaemonFile. Let's ignore mount_prefix when comparing and
    always compare the paths, so we can significantly simplify the code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786217

 client/gdaemonfile.c |   70 +++++++++++--------------------------------------
 1 files changed, 16 insertions(+), 54 deletions(-)
---
diff --git a/client/gdaemonfile.c b/client/gdaemonfile.c
index 2807c80..d51c4c0 100644
--- a/client/gdaemonfile.c
+++ b/client/gdaemonfile.c
@@ -321,32 +321,21 @@ g_daemon_file_prefix_matches (GFile *parent,
   GDaemonFile *descendant_daemon = G_DAEMON_FILE (descendant);
   const char *remainder;
 
-  if (descendant_daemon->mount_spec == parent_daemon->mount_spec)
+  /* If descendant was created with g_file_new_for_uri(), it's
+   * mount_prefix is /, but parent might have a different mount_prefix,
+   * for example if obtained by g_mount_get_root()
+   */
+  if (descendant_daemon->mount_spec == parent_daemon->mount_spec ||
+      g_mount_spec_match_with_path (parent_daemon->mount_spec,
+                                    descendant_daemon->mount_spec,
+                                    descendant_daemon->path))
     {
       remainder = match_prefix (descendant_daemon->path, parent_daemon->path);
       if (remainder != NULL && *remainder == '/')
         return TRUE;
-      else
-        return FALSE;
     }
-  else
-    {
-      /* If descendant was created with g_file_new_for_uri(), it's
-         mount_prefix is /, but parent might have a different mount_prefix,
-         for example if obtained by g_mount_get_root()
-      */
-      char *full_path;
-      gboolean ok;
-
-      full_path = g_build_path ("/", descendant_daemon->mount_spec->mount_prefix,
-                                descendant_daemon->path, NULL);
-      ok = g_mount_spec_match_with_path (parent_daemon->mount_spec,
-                                         descendant_daemon->mount_spec,
-                                         full_path);
 
-      g_free (full_path);
-      return ok;
-    }
+  return FALSE;
 }
 
 static char *
@@ -356,7 +345,11 @@ g_daemon_file_get_relative_path (GFile *parent,
   GDaemonFile *parent_daemon = G_DAEMON_FILE (parent);
   GDaemonFile *descendant_daemon = G_DAEMON_FILE (descendant);
 
-  if (descendant_daemon->mount_spec == parent_daemon->mount_spec)
+  /* See comment in g_daemon_file_prefix_matches */
+  if (descendant_daemon->mount_spec == parent_daemon->mount_spec ||
+      g_mount_spec_match_with_path (parent_daemon->mount_spec,
+                                    descendant_daemon->mount_spec,
+                                    descendant_daemon->path))
     {
       const char *remainder;
 
@@ -364,40 +357,9 @@ g_daemon_file_get_relative_path (GFile *parent,
 
       if (remainder != NULL && *remainder == '/')
         return g_strdup (remainder + 1);
-      else
-        return NULL;
     }
-  else
-    {
-      char *full_path_descendant;
-      char *full_path_parent;
-      char *ret;
-      const char *remainder;
 
-      full_path_descendant = g_build_path ("/", descendant_daemon->mount_spec->mount_prefix,
-                                           descendant_daemon->path, NULL);
-
-      if (!g_mount_spec_match_with_path (parent_daemon->mount_spec,
-                                         descendant_daemon->mount_spec,
-                                         full_path_descendant))
-        {
-          g_free (full_path_descendant);
-          return NULL;
-        }
-
-      full_path_parent = g_build_path ("/", parent_daemon->mount_spec->mount_prefix,
-                                       parent_daemon->path, NULL);
-
-      remainder = match_prefix (full_path_descendant, full_path_parent);
-      if (remainder != NULL && *remainder == '/')
-        ret = g_strdup (remainder + 1);
-      else
-        ret = NULL;
-
-      g_free (full_path_parent);
-      g_free (full_path_descendant);
-      return ret;
-    }
+  return NULL;
 }
 
 static GFile *


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]