[glib/wip/msanchez/libmount: 5/8] Refactor common code into create_unix_mount_entry ()



commit 084f6d00db09488a402755f61f04502ae3b36984
Author: Mario Sanchez Prada <mario endlessm com>
Date:   Thu May 19 19:12:37 2016 +0100

    Refactor common code into create_unix_mount_entry ()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=522053

 gio/gunixmounts.c |  142 ++++++++++++++++++++++++++---------------------------
 1 files changed, 70 insertions(+), 72 deletions(-)
---
diff --git a/gio/gunixmounts.c b/gio/gunixmounts.c
index e580626..39a14d8 100644
--- a/gio/gunixmounts.c
+++ b/gio/gunixmounts.c
@@ -326,6 +326,27 @@ guess_system_internal (const char *mountpoint,
 
 /* GUnixMounts (ie: mtab) implementations {{{1 */
 
+static GUnixMountEntry *
+create_unix_mount_entry (const char *device_path,
+                         const char *mount_path,
+                         const char *filesystem_type,
+                         gboolean    is_read_only)
+{
+  GUnixMountEntry *mount_entry = NULL;
+
+  mount_entry = g_new0 (GUnixMountEntry, 1);
+  mount_entry->device_path = g_strdup (device_path);
+  mount_entry->mount_path = g_strdup (mount_path);
+  mount_entry->filesystem_type = g_strdup (filesystem_type);
+  mount_entry->is_read_only = is_read_only;
+
+  mount_entry->is_system_internal =
+    guess_system_internal (mount_entry->mount_path,
+                           mount_entry->filesystem_type,
+                           mount_entry->device_path);
+  return mount_entry;
+}
+
 /* mntent.h (Linux, GNU, NSS) {{{2 */
 #ifdef HAVE_MNTENT_H
 
@@ -353,25 +374,17 @@ _g_get_unix_mounts (void)
   iter = mnt_new_iter (MNT_ITER_FORWARD);
   while (mnt_table_next_fs (table, iter, &fs) == 0)
     {
-      const char *fs_source = NULL;
-      const char *fs_target = NULL;
+      const char *device_path = NULL;
       char *mount_options = NULL;
       unsigned long mount_flags = 0;
+      gboolean is_read_only = FALSE;
 
       if (!mnt_table_is_fs_mounted (table, fs))
         continue;
 
-      fs_source = mnt_fs_get_source(fs);
-      fs_target = mnt_fs_get_target(fs);
-
-      mount_entry = g_new0 (GUnixMountEntry, 1);
-      mount_entry->mount_path = g_strdup (fs_target);
-
-      if (g_strcmp0 (fs_source, "/dev/root") == 0)
-        mount_entry->device_path = g_strdup (_resolve_dev_root ());
-      else
-        mount_entry->device_path = g_strdup (fs_source);
-      mount_entry->filesystem_type = g_strdup (mnt_fs_get_fstype (fs));
+      device_path = mnt_fs_get_source(fs);
+      if (g_strcmp0 (device_path, "/dev/root") == 0)
+        device_path = _resolve_dev_root ();
 
       mount_options = mnt_fs_strdup_options(fs);
       if (mount_options)
@@ -379,12 +392,12 @@ _g_get_unix_mounts (void)
           mnt_optstr_get_flags (mount_options, &mount_flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
           g_free (mount_options);
         }
-      mount_entry->is_read_only = (mount_flags & MS_RDONLY) ? 1 : 0;
+      is_read_only = (mount_flags & MS_RDONLY) ? TRUE : FALSE;
 
-      mount_entry->is_system_internal =
-        guess_system_internal (mount_entry->mount_path,
-                               mount_entry->filesystem_type,
-                               mount_entry->device_path);
+      mount_entry = create_unix_mount_entry (device_path,
+                                             mnt_fs_get_target(fs),
+                                             mnt_fs_get_fstype (fs),
+                                             is_read_only);
 
       return_list = g_list_prepend (return_list, mount_entry);
     }
@@ -444,6 +457,9 @@ _g_get_unix_mounts (void)
   while ((mntent = getmntent (file)) != NULL)
 #endif
     {
+      const char *device_path = NULL;
+      gboolean is_read_only = FALSE;
+
       /* ignore any mnt_fsname that is repeated and begins with a '/'
        *
        * We do this to avoid being fooled by --bind mounts, since
@@ -458,29 +474,26 @@ _g_get_unix_mounts (void)
          mntent->mnt_fsname[0] == '/' &&
          g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
         continue;
-      
-      mount_entry = g_new0 (GUnixMountEntry, 1);
-      mount_entry->mount_path = g_strdup (mntent->mnt_dir);
+
       if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
-        mount_entry->device_path = g_strdup (_resolve_dev_root ());
+        device_path = _resolve_dev_root ();
       else
-        mount_entry->device_path = g_strdup (mntent->mnt_fsname);
-      mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
-      
+        device_path = mntent->mnt_fsname;
+
 #if defined (HAVE_HASMNTOPT)
       if (hasmntopt (mntent, MNTOPT_RO) != NULL)
-       mount_entry->is_read_only = TRUE;
+       is_read_only = TRUE;
 #endif
-      
-      mount_entry->is_system_internal =
-       guess_system_internal (mount_entry->mount_path,
-                              mount_entry->filesystem_type,
-                              mount_entry->device_path);
-      
+
+      mount_entry = create_unix_mount_entry (device_path,
+                                             mntent->mnt_dir,
+                                             mntent->mnt_type,
+                                             is_read_only);
+
       g_hash_table_insert (mounts_hash,
                           mount_entry->device_path,
                           mount_entry->device_path);
-      
+
       return_list = g_list_prepend (return_list, mount_entry);
     }
   g_hash_table_destroy (mounts_hash);
@@ -551,22 +564,18 @@ _g_get_unix_mounts (void)
   G_LOCK (getmntent);
   while (! getmntent (file, &mntent))
     {
-      mount_entry = g_new0 (GUnixMountEntry, 1);
-      
-      mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
-      mount_entry->device_path = g_strdup (mntent.mnt_special);
-      mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
-      
+      gboolean is_read_only = FALSE;
+
 #if defined (HAVE_HASMNTOPT)
       if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
-       mount_entry->is_read_only = TRUE;
+       is_read_only = TRUE;
 #endif
 
-      mount_entry->is_system_internal =
-       guess_system_internal (mount_entry->mount_path,
-                              mount_entry->filesystem_type,
-                              mount_entry->device_path);
-      
+      mount_entry = create_unix_mount_entry (mntent.mnt_special,
+                                             mntent.mnt_mountp,
+                                             mntent.mnt_fstype,
+                                             is_read_only);
+
       return_list = g_list_prepend (return_list, mount_entry);
     }
   
@@ -621,25 +630,18 @@ _g_get_unix_mounts (void)
   return_list = NULL;
   while (vmount_number > 0)
     {
-      mount_entry = g_new0 (GUnixMountEntry, 1);
-      
-      mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
-      mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
-      /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
-      mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
+      gboolean is_read_only = FALSE;
 
       fs_info = getvfsbytype (vmount_info->vmt_gfstype);
-      
-      if (fs_info == NULL)
-       mount_entry->filesystem_type = g_strdup ("unknown");
-      else
-       mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
 
-      mount_entry->is_system_internal =
-       guess_system_internal (mount_entry->mount_path,
-                              mount_entry->filesystem_type,
-                              mount_entry->device_path);
-      
+      /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
+      is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
+
+      mount_entry = create_unix_mount_entry (vmt2dataptr (vmount_info, VMT_OBJECT),
+                                             vmt2dataptr (vmount_info, VMT_STUB),
+                                             fs_info == NULL ? "unknown" : fs_info->vfsent_name,
+                                             is_read_only);
+
       return_list = g_list_prepend (return_list, mount_entry);
       
       vmount_info = (struct vmount *)( (char*)vmount_info 
@@ -699,11 +701,7 @@ _g_get_unix_mounts (void)
   
   for (i = 0; i < num_mounts; i++)
     {
-      mount_entry = g_new0 (GUnixMountEntry, 1);
-      
-      mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
-      mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
-      mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
+      gboolean is_read_only = FALSE;
 
 #if defined(USE_STATVFS)
       if (mntent[i].f_flag & ST_RDONLY)
@@ -712,13 +710,13 @@ _g_get_unix_mounts (void)
 #else
       #error statfs juggling failed
 #endif
-        mount_entry->is_read_only = TRUE;
+        is_read_only = TRUE;
+
+      mount_entry = create_unix_mount_entry (mntent[i].f_mntfromname,
+                                             mntent[i].f_mntonname,
+                                             mntent[i].f_fstypename,
+                                             is_read_only);
 
-      mount_entry->is_system_internal =
-        guess_system_internal (mount_entry->mount_path,
-                               mount_entry->filesystem_type,
-                               mount_entry->device_path);
-      
       return_list = g_list_prepend (return_list, mount_entry);
     }
 


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