From 63f1e5c27dbfd7b56a91aea95c46624b09aba756 Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Tue, 27 Sep 2011 12:08:37 -0400 Subject: [PATCH] UDisksClient: add get_block_for_drive() method Signed-off-by: David Zeuthen --- doc/udisks2-sections.txt | 1 + udisks/udisksclient.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ udisks/udisksclient.h | 4 +++ 3 files changed, 86 insertions(+) diff --git a/doc/udisks2-sections.txt b/doc/udisks2-sections.txt index f41bafb..36ad7e5 100644 --- a/doc/udisks2-sections.txt +++ b/doc/udisks2-sections.txt @@ -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 UDISKS_TYPE_CLIENT UDISKS_CLIENT diff --git a/udisks/udisksclient.c b/udisks/udisksclient.c index 3f02ae0..d573178 100644 --- a/udisks/udisksclient.c +++ b/udisks/udisksclient.c @@ -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; +} diff --git a/udisks/udisksclient.h b/udisks/udisksclient.h index 066127f..6c0361a 100644 --- a/udisks/udisksclient.h +++ b/udisks/udisksclient.h @@ -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__ */ -- 2.7.4