[gvfs/gnome-2-28] Gently handle NULL GduDevice objects



commit 48cf9c174f347f92c3fab7ff50b38effda0eb78a
Author: Tomas Bzatek <tbzatek redhat com>
Date:   Fri Oct 30 18:06:26 2009 +0100

    Gently handle NULL GduDevice objects
    
    This patch adds bunch of tests for NULL GduDevice objects. According to
    gdu docs, there's a possibility to have GduPresentable and no associated
    GduDevice. It's not common but true for several special cases -
    like GduLinuxMdDrive.
    
    Signed-off-by: David Zeuthen <davidz redhat com>

 monitor/gdu/ggduvolume.c        |  109 +++++++++++++++++++++++++++------------
 monitor/gdu/ggduvolumemonitor.c |   85 ++++++++++++++++++++----------
 2 files changed, 132 insertions(+), 62 deletions(-)
---
diff --git a/monitor/gdu/ggduvolume.c b/monitor/gdu/ggduvolume.c
index 865a7b7..28839fd 100644
--- a/monitor/gdu/ggduvolume.c
+++ b/monitor/gdu/ggduvolume.c
@@ -172,7 +172,7 @@ static gboolean
 update_volume (GGduVolume *volume)
 {
   GduDevice *device;
-  GduPool *pool;
+  GduPool *pool = NULL;
   time_t now;
   gboolean changed;
   gboolean old_can_mount;
@@ -217,10 +217,11 @@ update_volume (GGduVolume *volume)
 
   /* in with the new */
   device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->gdu_volume));
-  pool = gdu_device_get_pool (device);
+  if (device != NULL)
+    pool = gdu_device_get_pool (device);
 
   keep_cleartext_volume = FALSE;
-  if (gdu_device_is_luks (device))
+  if (device != NULL && gdu_device_is_luks (device))
     {
       const gchar *holder_objpath;
 
@@ -287,13 +288,17 @@ update_volume (GGduVolume *volume)
       volume->name = gdu_presentable_get_name (GDU_PRESENTABLE (volume->cleartext_gdu_volume));
 
       g_free (volume->device_file);
-      volume->device_file = g_strdup (gdu_device_get_device_file (luks_cleartext_volume_device));
+      if (luks_cleartext_volume_device != NULL)
+        volume->device_file = g_strdup (gdu_device_get_device_file (luks_cleartext_volume_device));
+      else
+        volume->device_file = NULL;
 
       volume->can_mount = TRUE;
 
       volume->should_automount = FALSE;
 
-      g_object_unref (luks_cleartext_volume_device);
+      if (luks_cleartext_volume_device != NULL)
+        g_object_unref (luks_cleartext_volume_device);
     }
   else
     {
@@ -321,7 +326,10 @@ update_volume (GGduVolume *volume)
         }
 
       g_free (volume->device_file);
-      volume->device_file = g_strdup (gdu_device_get_device_file (device));
+      if (device != NULL)
+        volume->device_file = g_strdup (gdu_device_get_device_file (device));
+      else
+        volume->device_file = NULL;
 
       volume->can_mount = TRUE;
 
@@ -341,14 +349,16 @@ update_volume (GGduVolume *volume)
        * nopolicy is only FALSE for "physical" devices - e.g. only "physical" devices will
        * be set to be automounted.
        */
-      if (gdu_device_get_presentation_nopolicy (device))
+      if (device != NULL && gdu_device_get_presentation_nopolicy (device))
         volume->should_automount = FALSE;
 
       g_free (activation_uri);
     }
 
-  g_object_unref (pool);
-  g_object_unref (device);
+  if (pool != NULL)
+    g_object_unref (pool);
+  if (device != NULL)
+    g_object_unref (device);
 
   /* ---------------------------------------------------------------------------------------------------- */
 
@@ -1082,6 +1092,20 @@ g_gdu_volume_mount (GVolume             *_volume,
     }
 
   device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->gdu_volume));
+
+  if (device == NULL)
+    {
+      simple = g_simple_async_result_new_error (G_OBJECT (volume),
+                                                callback,
+                                                user_data,
+                                                G_IO_ERROR,
+                                                G_IO_ERROR_FAILED,
+                                                "Underlying device missing");
+      g_simple_async_result_complete (simple);
+      g_object_unref (simple);
+      goto out;
+    }
+
   pool = gdu_device_get_pool (device);
 
   /* Makes no sense to mount
@@ -1129,11 +1153,14 @@ g_gdu_volume_mount (GVolume             *_volume,
 
       luks_cleartext_volume_device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->cleartext_gdu_volume));
 
-      object_path_of_cleartext_device = gdu_device_get_object_path (luks_cleartext_volume_device);
+      if (luks_cleartext_volume_device != NULL)
+        {
+          object_path_of_cleartext_device = gdu_device_get_object_path (luks_cleartext_volume_device);
 
-      mount_cleartext_device (data, object_path_of_cleartext_device);
+          mount_cleartext_device (data, object_path_of_cleartext_device);
 
-      g_object_unref (luks_cleartext_volume_device);
+          g_object_unref (luks_cleartext_volume_device);
+        }
       goto out;
     }
 
@@ -1551,17 +1578,20 @@ g_gdu_volume_get_identifier (GVolume     *_volume,
     {
       device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->gdu_volume));
 
-      label = gdu_device_id_get_label (device);
-      uuid = gdu_device_id_get_uuid (device);
+      if (device != NULL)
+        {
+          label = gdu_device_id_get_label (device);
+          uuid = gdu_device_id_get_uuid (device);
 
-      g_object_unref (device);
+          g_object_unref (device);
 
-      if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE) == 0)
-        id = g_strdup (volume->device_file);
-      else if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_LABEL) == 0)
-        id = strlen (label) > 0 ? g_strdup (label) : NULL;
-      else if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UUID) == 0)
-        id = strlen (uuid) > 0 ? g_strdup (uuid) : NULL;
+          if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE) == 0)
+            id = g_strdup (volume->device_file);
+          else if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_LABEL) == 0)
+            id = strlen (label) > 0 ? g_strdup (label) : NULL;
+          else if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UUID) == 0)
+            id = strlen (uuid) > 0 ? g_strdup (uuid) : NULL;
+        }
     }
 
   return id;
@@ -1577,19 +1607,24 @@ g_gdu_volume_enumerate_identifiers (GVolume *_volume)
   const gchar *uuid;
 
   p = g_ptr_array_new ();
+  label = NULL;
 
   if (volume->gdu_volume != NULL)
     {
       device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->gdu_volume));
-      label = gdu_device_id_get_label (device);
-      uuid = gdu_device_id_get_uuid (device);
-      g_object_unref (device);
-
-      g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE));
-      if (strlen (label) > 0)
-        g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL));
-      if (strlen (uuid) > 0)
-        g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID));
+
+      if (device != NULL)
+        {
+          label = gdu_device_id_get_label (device);
+          uuid = gdu_device_id_get_uuid (device);
+          g_object_unref (device);
+
+          g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE));
+          if (strlen (label) > 0)
+            g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL));
+          if (strlen (uuid) > 0)
+            g_ptr_array_add (p, g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID));
+        }
     }
 
   g_ptr_array_add (p, NULL);
@@ -1638,8 +1673,11 @@ g_gdu_volume_has_device_file (GGduVolume   *volume,
     {
       GduDevice *luks_cleartext_volume_device;
       luks_cleartext_volume_device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->cleartext_gdu_volume));
-      _device_file = gdu_device_get_device_file (luks_cleartext_volume_device);
-      g_object_unref (luks_cleartext_volume_device);
+      if (luks_cleartext_volume_device != NULL)
+        {
+          _device_file = gdu_device_get_device_file (luks_cleartext_volume_device);
+          g_object_unref (luks_cleartext_volume_device);
+        }
     }
 
   return g_strcmp0 (_device_file, device_file) == 0;
@@ -1682,8 +1720,11 @@ g_gdu_volume_has_uuid (GGduVolume  *volume,
     {
       GduDevice *luks_cleartext_volume_device;
       luks_cleartext_volume_device = gdu_presentable_get_device (GDU_PRESENTABLE (volume->cleartext_gdu_volume));
-      _uuid = gdu_device_id_get_uuid (luks_cleartext_volume_device);
-      g_object_unref (luks_cleartext_volume_device);
+      if (luks_cleartext_volume_device != NULL)
+        {
+          _uuid = gdu_device_id_get_uuid (luks_cleartext_volume_device);
+          g_object_unref (luks_cleartext_volume_device);
+        }
     }
 
   return g_strcmp0 (_uuid, uuid) == 0;
diff --git a/monitor/gdu/ggduvolumemonitor.c b/monitor/gdu/ggduvolumemonitor.c
index e81dce2..398fed4 100644
--- a/monitor/gdu/ggduvolumemonitor.c
+++ b/monitor/gdu/ggduvolumemonitor.c
@@ -779,9 +779,10 @@ should_volume_be_ignored (GduPool *pool, GduVolume *volume, GList *fstab_mount_p
   const gchar *type;
 
   ret = TRUE;
-  device = NULL;
 
   device = gdu_presentable_get_device (GDU_PRESENTABLE (volume));
+  if (device == NULL)
+    goto out;
 
   if (gdu_device_get_presentation_hide (device))
     goto out;
@@ -827,7 +828,8 @@ should_volume_be_ignored (GduPool *pool, GduVolume *volume, GList *fstab_mount_p
 
  out:
 
-  g_object_unref (device);
+  if (device != NULL)
+    g_object_unref (device);
   return ret;
 }
 
@@ -1269,15 +1271,18 @@ update_volumes (GGduVolumeMonitor *monitor,
 
       d = gdu_presentable_get_device (p);
 
-      volume = find_volume_for_device_file (monitor, gdu_device_get_device_file (d));
-      if (volume != NULL)
+      if (d != NULL)
         {
-          /*g_debug ("removing volume %s", gdu_device_get_device_file (d));*/
-          g_gdu_volume_removed (volume);
-          monitor->volumes = g_list_remove (monitor->volumes, volume);
-          *removed_volumes = g_list_prepend (*removed_volumes, volume);
+          volume = find_volume_for_device_file (monitor, gdu_device_get_device_file (d));
+          if (volume != NULL)
+            {
+              /*g_debug ("removing volume %s", gdu_device_get_device_file (d));*/
+              g_gdu_volume_removed (volume);
+              monitor->volumes = g_list_remove (monitor->volumes, volume);
+              *removed_volumes = g_list_prepend (*removed_volumes, volume);
+            }
+          g_object_unref (d);
         }
-      g_object_unref (d);
     }
 
   for (l = added; l != NULL; l = l->next)
@@ -1285,9 +1290,12 @@ update_volumes (GGduVolumeMonitor *monitor,
       GduPresentable *p = GDU_PRESENTABLE (l->data);
       GduDevice *d;
 
+      volume = NULL;
       d = gdu_presentable_get_device (p);
 
-      volume = find_volume_for_device_file (monitor, gdu_device_get_device_file (d));
+      if (d != NULL)
+        volume = find_volume_for_device_file (monitor, gdu_device_get_device_file (d));
+
       if (volume == NULL)
         {
           GduPresentable *toplevel_presentable;
@@ -1297,12 +1305,16 @@ update_volumes (GGduVolumeMonitor *monitor,
             {
               GduDevice *toplevel_device;
 
+              drive = NULL;
               toplevel_device = gdu_presentable_get_device (toplevel_presentable);
-              drive = find_drive_by_device_file (monitor, gdu_device_get_device_file (toplevel_device));
-              /*g_debug ("adding volume %s (drive %s)",
-                       gdu_device_get_device_file (d),
-                       gdu_device_get_device_file (toplevel_device));*/
-              g_object_unref (toplevel_device);
+              if (toplevel_device != NULL)
+                {
+                  drive = find_drive_by_device_file (monitor, gdu_device_get_device_file (toplevel_device));
+                  /*g_debug ("adding volume %s (drive %s)",
+                           gdu_device_get_device_file (d),
+                           gdu_device_get_device_file (toplevel_device));*/
+                  g_object_unref (toplevel_device);
+                }
               g_object_unref (toplevel_presentable);
             }
           else
@@ -1320,9 +1332,10 @@ update_volumes (GGduVolumeMonitor *monitor,
               monitor->volumes = g_list_prepend (monitor->volumes, volume);
               *added_volumes = g_list_prepend (*added_volumes, g_object_ref (volume));
             }
-        }
+         }
 
-      g_object_unref (d);
+       if (d != NULL)
+         g_object_unref (d);
     }
 
   g_list_free (added);
@@ -1579,10 +1592,15 @@ update_discs (GGduVolumeMonitor *monitor,
       GduPresentable *p = GDU_PRESENTABLE (l->data);
       GduDevice *d;
 
+      volume = NULL;
+      mount = NULL;
       d = gdu_presentable_get_device (p);
 
-      volume = find_disc_volume_for_device_file (monitor, gdu_device_get_device_file (d));
-      mount = find_disc_mount_for_volume (monitor, volume);
+      if (d != NULL)
+        {
+          volume = find_disc_volume_for_device_file (monitor, gdu_device_get_device_file (d));
+          mount = find_disc_mount_for_volume (monitor, volume);
+        }
 
       if (mount != NULL)
         {
@@ -1600,7 +1618,8 @@ update_discs (GGduVolumeMonitor *monitor,
           *removed_volumes = g_list_prepend (*removed_volumes, volume);
         }
 
-      g_object_unref (d);
+      if (d != NULL)
+        g_object_unref (d);
     }
 
   for (l = added; l != NULL; l = l->next)
@@ -1609,11 +1628,16 @@ update_discs (GGduVolumeMonitor *monitor,
       GduDevice *d;
       gboolean is_blank;
 
+      volume = NULL;
+      is_blank = TRUE;
       d = gdu_presentable_get_device (p);
 
-      is_blank = gdu_device_optical_disc_get_is_blank (d);
+      if (d != NULL)
+        {
+          is_blank = gdu_device_optical_disc_get_is_blank (d);
+          volume = find_disc_volume_for_device_file (monitor, gdu_device_get_device_file (d));
+        }
 
-      volume = find_disc_volume_for_device_file (monitor, gdu_device_get_device_file (d));
       if (volume == NULL)
         {
           GduPresentable *toplevel_presentable;
@@ -1623,12 +1647,16 @@ update_discs (GGduVolumeMonitor *monitor,
             {
               GduDevice *toplevel_device;
 
+              drive = NULL;
               toplevel_device = gdu_presentable_get_device (toplevel_presentable);
-              drive = find_drive_by_device_file (monitor, gdu_device_get_device_file (toplevel_device));
-              /*g_debug ("adding volume %s (drive %s)",
-                       gdu_device_get_device_file (d),
-                       gdu_device_get_device_file (toplevel_device));*/
-              g_object_unref (toplevel_device);
+              if (toplevel_device != NULL)
+                {
+                  drive = find_drive_by_device_file (monitor, gdu_device_get_device_file (toplevel_device));
+                  /*g_debug ("adding volume %s (drive %s)",
+                           gdu_device_get_device_file (d),
+                           gdu_device_get_device_file (toplevel_device));*/
+                  g_object_unref (toplevel_device);
+                }
               g_object_unref (toplevel_presentable);
             }
           else
@@ -1682,7 +1710,8 @@ update_discs (GGduVolumeMonitor *monitor,
             }
         }
 
-      g_object_unref (d);
+      if (d != NULL)
+        g_object_unref (d);
     }
 
   g_list_free (added);



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