Add udisks_client_settle() and use it in udisksctl(1) to show device files
authorDavid Zeuthen <davidz@redhat.com>
Thu, 4 Aug 2011 16:46:58 +0000 (12:46 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Thu, 4 Aug 2011 16:46:58 +0000 (12:46 -0400)
... instead of object paths.

Signed-off-by: David Zeuthen <davidz@redhat.com>
doc/udisks2-sections.txt
tools/udisksctl.c
udisks/udisksclient.c
udisks/udisksclient.h

index ac56ef7..4258be9 100644 (file)
@@ -41,6 +41,7 @@ udisks_client_new_finish
 udisks_client_new_sync
 udisks_client_get_object_manager
 udisks_client_get_manager
+udisks_client_settle
 <SUBSECTION Standard>
 UDISKS_TYPE_CLIENT
 UDISKS_CLIENT
index df3da6f..7f17a13 100644 (file)
@@ -1253,6 +1253,7 @@ handle_command_unlock_lock (gint        *argc,
     {
       GError *error;
       gchar *cleartext_object_path;
+      UDisksObject *cleartext_object;
 
       error = NULL;
       if (!udisks_encrypted_call_unlock_sync (encrypted,
@@ -1276,10 +1277,14 @@ handle_command_unlock_lock (gint        *argc,
           g_object_unref (object);
           goto out;
         }
+      udisks_client_settle (client);
+
+      cleartext_object = UDISKS_OBJECT (g_dbus_object_manager_get_object (udisks_client_get_object_manager (client),
+                                                                          (cleartext_object_path)));
       g_print ("Unlocked %s as %s.\n",
                udisks_block_device_get_device (block),
-               cleartext_object_path);
-      /* TODO: lookup cleartext_object_path and print device */
+               udisks_block_device_get_device (udisks_object_get_block_device (cleartext_object)));
+      g_object_unref (cleartext_object);
       g_free (cleartext_object_path);
     }
   else
@@ -1597,7 +1602,8 @@ handle_command_loop (gint        *argc,
 
   if (is_setup)
     {
-      gchar *resulting_device_object_path;
+      gchar *resulting_object_path;
+      UDisksObject *resulting_object;
       GUnixFDList *fd_list;
       gint fd;
       gboolean rc;
@@ -1626,7 +1632,7 @@ handle_command_loop (gint        *argc,
                                                 g_variant_new_handle (0),
                                                 options,
                                                 fd_list,
-                                                &resulting_device_object_path,
+                                                &resulting_object_path,
                                                 NULL,                       /* out_fd_list */
                                                 NULL,                       /* GCancellable */
                                                 &error);
@@ -1646,12 +1652,15 @@ handle_command_loop (gint        *argc,
           g_error_free (error);
           goto out;
         }
+      udisks_client_settle (client);
 
+      resulting_object = UDISKS_OBJECT (g_dbus_object_manager_get_object (udisks_client_get_object_manager (client),
+                                                                          (resulting_object_path)));
       g_print ("Mapped file %s as %s.\n",
                opt_loop_file,
-               resulting_device_object_path);
-      /* TODO: lookup cleartext_object_path and print device */
-      g_free (resulting_device_object_path);
+               udisks_block_device_get_device (udisks_object_get_block_device (resulting_object)));
+      g_object_unref (resulting_object);
+      g_free (resulting_object_path);
     }
   else
     {
index 1729a7e..ae96576 100644 (file)
@@ -50,6 +50,8 @@ struct _UDisksClient
   GError *initialization_error;
 
   GDBusObjectManager *object_manager;
+
+  GMainContext *context;
 };
 
 typedef struct
@@ -82,6 +84,9 @@ udisks_client_finalize (GObject *object)
 
   g_object_unref (client->object_manager);
 
+  if (client->context != NULL)
+    g_main_context_unref (client->context);
+
   G_OBJECT_CLASS (udisks_client_parent_class)->finalize (object);
 }
 
@@ -260,6 +265,10 @@ initable_init (GInitable     *initable,
     }
   g_assert (client->initialization_error == NULL);
 
+  client->context = g_main_context_get_thread_default ();
+  if (client->context != NULL)
+    g_main_context_ref (client->context);
+
   client->object_manager = udisks_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                                           G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
                                                                           "org.freedesktop.UDisks2",
@@ -339,3 +348,20 @@ udisks_client_get_manager (UDisksClient *client)
  out:
   return ret;
 }
+
+/**
+ * udisks_client_settle:
+ * @client: A #UDisksClient.
+ *
+ * Blocks until all pending D-Bus messages have been delivered.
+ *
+ * This is useful when using synchronous method calls since e.g. D-Bus
+ * signals received while waiting for the reply are queued up and
+ * dispatched after the synchronous call ends.
+ */
+void
+udisks_client_settle (UDisksClient *client)
+{
+  while (g_main_context_iteration (client->context, FALSE /* may_block */))
+    ;
+}
index adc73bd..066127f 100644 (file)
@@ -44,6 +44,7 @@ UDisksClient       *udisks_client_new_sync           (GCancellable        *cance
                                                       GError             **error);
 GDBusObjectManager *udisks_client_get_object_manager (UDisksClient        *client);
 UDisksManager      *udisks_client_get_manager        (UDisksClient        *client);
+void                udisks_client_settle             (UDisksClient        *client);
 
 G_END_DECLS