[glib/wip/oholy/remote-attribute-fixes: 4/7] glocalfile: Do not call statfs/statvfs several times



commit efba472642bd5c344a37cbcbe416e45287376809
Author: Ondrej Holy <oholy redhat com>
Date:   Mon Jun 15 17:24:37 2020 +0200

    glocalfile: Do not call statfs/statvfs several times
    
    statfs/statvfs is called several times when querying filesystem info.
    This is because the G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE attribute is set
    over is_remote_fs function, which calls statfs/statvfs again. Let's use
    the already known fstype instead of redundant statfs/statvfs calls.
    This also changes g_local_file_is_remote implementation to use
    g_local_file_query_filesystem_info to obtain fstype, which allows to
    remove duplicated code from is_remote_fs function.

 gio/glocalfile.c | 53 ++++++++++++++---------------------------------------
 1 file changed, 14 insertions(+), 39 deletions(-)
---
diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 69d4c27dd..51b7d2bdd 100644
--- a/gio/glocalfile.c
+++ b/gio/glocalfile.c
@@ -111,7 +111,7 @@ G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
                                                g_local_file_file_iface_init))
 
 static char *find_mountpoint_for (const char *file, dev_t dev, gboolean resolve_basename_symlink);
-static gboolean is_remote_fs (const gchar *filename);
+static gboolean is_remote_fs_type (const gchar *fsname);
 
 static void
 g_local_file_finalize (GObject *object)
@@ -1116,7 +1116,7 @@ g_local_file_query_filesystem_info (GFile         *file,
   if (g_file_attribute_matcher_matches (attribute_matcher,
                                        G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
       g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE,
-                                         is_remote_fs (local->filename));
+                                         is_remote_fs_type (fstype));
 
   g_file_attribute_matcher_unref (attribute_matcher);
   
@@ -2519,43 +2519,8 @@ g_local_file_is_remote (const gchar *filename)
 #else
 
 static gboolean
-is_remote_fs (const gchar *filename)
+is_remote_fs_type (const gchar *fsname)
 {
-  const char *fsname = NULL;
-
-#ifdef USE_STATFS
-  struct statfs statfs_buffer;
-  int statfs_result = 0;
-
-#if STATFS_ARGS == 2
-  statfs_result = statfs (filename, &statfs_buffer);
-#elif STATFS_ARGS == 4
-  statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
-#endif
-
-#elif defined(USE_STATVFS)
-  struct statvfs statfs_buffer;
-  int statfs_result = 0;
-
-  statfs_result = statvfs (filename, &statfs_buffer);
-#else
-  return FALSE;
-#endif
-
-  if (statfs_result == -1)
-    return FALSE;
-
-#ifdef USE_STATFS
-#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
-  fsname = statfs_buffer.f_fstypename;
-#else
-  fsname = get_fs_type (statfs_buffer.f_type);
-#endif
-
-#elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
-  fsname = statfs_buffer.f_basetype;
-#endif
-
   if (fsname != NULL)
     {
       if (strcmp (fsname, "nfs") == 0)
@@ -2579,7 +2544,17 @@ g_local_file_is_remote (const gchar *filename)
     {
       if (g_once_init_enter (&initialized))
         {
-          remote_home = is_remote_fs (home);
+          GFile *file;
+          GFileInfo *info;
+          const gchar *fs_type = NULL;
+
+          file = _g_local_file_new (home);
+          info = g_local_file_query_filesystem_info (file, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, NULL, NULL);
+          if (info != NULL)
+            fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
+          remote_home = is_remote_fs_type (fs_type);
+          g_object_unref (info);
+
           g_once_init_leave (&initialized, TRUE);
         }
       return remote_home;


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