Add Block.OpenForBackup() method used for creating disk images
authorDavid Zeuthen <davidz@redhat.com>
Thu, 1 Dec 2011 17:19:04 +0000 (12:19 -0500)
committerDavid Zeuthen <davidz@redhat.com>
Thu, 1 Dec 2011 17:19:04 +0000 (12:19 -0500)
See

 http://people.freedesktop.org/~david/gdu2-create-disk-image-1.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-2.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-3.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-4.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-5.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-6.png
 http://people.freedesktop.org/~david/gdu2-create-disk-image-7.png

Signed-off-by: David Zeuthen <davidz@redhat.com>
data/org.freedesktop.UDisks2.xml
doc/udisks2-sections.txt
policy/org.freedesktop.udisks2.policy.in
src/udiskslinuxblock.c

index d4e5f65..5d53416 100644 (file)
       <arg name="options" direction="in" type="a{sv}"/>
     </method>
 
+    <!--
+        OpenForBackup:
+        @options: Options (currently unused except for <link linkend="udisks-std-options">standard options</link>).
+        @fd: An index for the returned file descriptor.
+
+        Gets a read-only file descriptor for the device intended for a
+        byte-by-byte imaging of the device. This can only be done if
+        the device is not already in use.
+    -->
+    <method name="OpenForBackup">
+      <annotation name="org.gtk.GDBus.C.UnixFD" value="1"/>
+      <arg name="options" direction="in" type="a{sv}"/>
+      <arg name="fd" direction="out" type="h"/>
+    </method>
+
   </interface>
 
   <!-- ********************************************************************** -->
index 9cf1cc3..602df25 100644 (file)
@@ -914,6 +914,10 @@ udisks_block_call_format
 udisks_block_call_format_finish
 udisks_block_call_format_sync
 udisks_block_complete_format
+udisks_block_call_open_for_backup
+udisks_block_call_open_for_backup_finish
+udisks_block_call_open_for_backup_sync
+udisks_block_complete_open
 udisks_block_get_configuration
 udisks_block_get_crypto_backing_device
 udisks_block_get_device
index d134bfd..e95115a 100644 (file)
     </defaults>
   </action>
 
+  <!-- Open a device for reading (for creating disk images) -->
+  <action id="org.freedesktop.udisks2.open-device">
+    <_description>Open a device</_description>
+    <_message>Authentication is required to open a device</_message>
+    <defaults>
+      <allow_any>auth_admin</allow_any>
+      <allow_inactive>auth_admin</allow_inactive>
+      <allow_active>auth_admin_keep</allow_active>
+    </defaults>
+  </action>
+
+  <action id="org.freedesktop.udisks2.open-device-system">
+    <_description>Open a system device</_description>
+    <_message>Authentication is required to open a device</_message>
+    <defaults>
+      <allow_any>auth_admin</allow_any>
+      <allow_inactive>auth_admin</allow_inactive>
+      <allow_active>auth_admin_keep</allow_active>
+    </defaults>
+  </action>
+
   <!-- Manage system-wide configuration files such as /etc/fstab or
        /etc/crypttab ... including files referenced by these files.
 
index a3251bb..86067bd 100644 (file)
@@ -33,6 +33,7 @@
 #include <mntent.h>
 
 #include <glib/gstdio.h>
+#include <gio/gunixfdlist.h>
 
 #include "udiskslogging.h"
 #include "udiskslinuxblock.h"
@@ -2081,6 +2082,55 @@ handle_format (UDisksBlock           *block,
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static gboolean
+handle_open_for_backup (UDisksBlock           *block,
+                        GDBusMethodInvocation *invocation,
+                        GUnixFDList           *fd_list,
+                        GVariant              *options)
+{
+  UDisksObject *object;
+  UDisksDaemon *daemon;
+  const gchar *action_id;
+  const gchar *device;
+  GUnixFDList *out_fd_list = NULL;
+  gint fd;
+
+  object = UDISKS_OBJECT (g_dbus_interface_get_object (G_DBUS_INTERFACE (block)));
+  daemon = udisks_linux_block_object_get_daemon (UDISKS_LINUX_BLOCK_OBJECT (object));
+
+  action_id = "org.freedesktop.udisks2.open-device";
+  if (udisks_block_get_hint_system (block))
+    action_id = "org.freedesktop.udisks2.open-device-system";
+
+  if (!udisks_daemon_util_check_authorization_sync (daemon,
+                                                    object,
+                                                    action_id,
+                                                    options,
+                                                    N_("Authentication is required to open $(udisks2.device) for backup"),
+                                                    invocation))
+    goto out;
+
+
+  device = udisks_block_get_device (UDISKS_BLOCK (block));
+
+  fd = open (device, O_RDONLY | O_CLOEXEC | O_EXCL);
+  if (fd == -1)
+    {
+      g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
+                                             "Error opening %s: %m", device);
+      goto out;
+    }
+
+  out_fd_list = g_unix_fd_list_new_from_array (&fd, 1);
+  udisks_block_complete_open_for_backup (block, invocation, out_fd_list, g_variant_new_handle (0));
+
+ out:
+  g_clear_object (&out_fd_list);
+  return TRUE; /* returning true means that we handled the method invocation */
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 static void
 block_iface_init (UDisksBlockIface *iface)
 {
@@ -2089,4 +2139,5 @@ block_iface_init (UDisksBlockIface *iface)
   iface->handle_remove_configuration_item = handle_remove_configuration_item;
   iface->handle_update_configuration_item = handle_update_configuration_item;
   iface->handle_format                    = handle_format;
+  iface->handle_open_for_backup           = handle_open_for_backup;
 }