UDisksClient: add get_block_for_drive() method
authorDavid Zeuthen <davidz@redhat.com>
Tue, 27 Sep 2011 16:08:37 +0000 (12:08 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Tue, 27 Sep 2011 16:08:37 +0000 (12:08 -0400)
Signed-off-by: David Zeuthen <davidz@redhat.com>
doc/udisks2-sections.txt
udisks/udisksclient.c
udisks/udisksclient.h

index f41bafb..36ad7e5 100644 (file)
@@ -42,6 +42,7 @@ udisks_client_new_sync
 udisks_client_get_object_manager
 udisks_client_get_manager
 udisks_client_settle
+udisks_client_get_block_for_drive
 <SUBSECTION Standard>
 UDISKS_TYPE_CLIENT
 UDISKS_CLIENT
index 3f02ae0..d573178 100644 (file)
@@ -365,3 +365,84 @@ udisks_client_settle (UDisksClient *client)
   while (g_main_context_iteration (client->context, FALSE /* may_block */))
     ;
 }
+
+
+static GList *
+get_top_level_blocks_for_drive (UDisksClient *client,
+                                const gchar  *drive_object_path)
+{
+  GList *ret;
+  GList *l;
+  GList *object_proxies;
+  GDBusObjectManager *object_manager;
+
+  object_manager = udisks_client_get_object_manager (client);
+  object_proxies = g_dbus_object_manager_get_objects (object_manager);
+
+  ret = NULL;
+  for (l = object_proxies; l != NULL; l = l->next)
+    {
+      UDisksObject *object = UDISKS_OBJECT (l->data);
+      UDisksBlock *block;
+
+      block = udisks_object_get_block (object);
+      if (block == NULL)
+        continue;
+
+      if (g_strcmp0 (udisks_block_get_drive (block), drive_object_path) == 0 &&
+          !udisks_block_get_part_entry (block))
+        {
+          ret = g_list_append (ret, g_object_ref (object));
+        }
+      g_object_unref (block);
+    }
+  g_list_foreach (object_proxies, (GFunc) g_object_unref, NULL);
+  g_list_free (object_proxies);
+  return ret;
+}
+
+/**
+ * udisks_client_get_block_for_drive:
+ * @client: A #UDisksClient.
+ * @drive: A #UDisksDrive.
+ * @get_physical: %TRUE to get a physical device, %FALSE to get the logical device.
+ *
+ * Gets a block device corresponding to @drive.
+ *
+ * If your application does not care about multipath setups, the
+ * @get_physical parameter does not matter. Otherwise, use %TRUE for
+ * to get one of the physical paths for @drive (if any) and %FALSE to
+ * get the mapped device (if any).
+ *
+ * Returns: (transfer full): A #UDisksBlock or %NULL if the requested
+ * kind of block device is not available - use g_object_unref() to
+ * free the returned object.
+ */
+UDisksBlock *
+udisks_client_get_block_for_drive (UDisksClient        *client,
+                                   UDisksDrive         *drive,
+                                   gboolean             get_physical)
+{
+  UDisksBlock *ret = NULL;
+  GList *blocks;
+  GList *l;
+
+  blocks = get_top_level_blocks_for_drive (client, g_dbus_object_get_object_path (g_dbus_interface_get_object (G_DBUS_INTERFACE (drive))));
+  for (l = blocks; l != NULL; l = l->next)
+    {
+      UDisksObject *object = UDISKS_OBJECT (l->data);
+      UDisksBlock *block;
+      block = udisks_object_peek_block (object);
+      if (block != NULL)
+        {
+          /* TODO: actually look at @get_physical */
+          ret = g_object_ref (block);
+          goto out;
+        }
+    }
+
+ out:
+  g_list_foreach (blocks, (GFunc) g_object_unref, NULL);
+  g_list_free (blocks);
+  return ret;
+}
index 066127f..6c0361a 100644 (file)
@@ -46,6 +46,10 @@ GDBusObjectManager *udisks_client_get_object_manager (UDisksClient        *clien
 UDisksManager      *udisks_client_get_manager        (UDisksClient        *client);
 void                udisks_client_settle             (UDisksClient        *client);
 
+UDisksBlock        *udisks_client_get_block_for_drive (UDisksClient        *client,
+                                                       UDisksDrive         *drive,
+                                                       gboolean             get_physical);
+
 G_END_DECLS
 
 #endif /* __UDISKS_CLIENT_H__ */