Skip to content

Commit

Permalink
Merge pull request #1226 from jelly/set-default-btrfs-subvol-id
Browse files Browse the repository at this point in the history
modules/btrfs: add SetDefaultSubvolumeID
  • Loading branch information
vojtechtrefny authored Nov 24, 2023
2 parents ec23467 + 33d2271 commit 4f24c90
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions modules/btrfs/data/org.freedesktop.UDisks2.btrfs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@
<arg name="options" type="a{sv}" direction="in"/>
</method>

<!--
SetDefaultSubvolumeID:
@id: New default subvolume id.
@options: Additional options.
@since: 2.11.0
Sets the default subvolume id.
No additional options are currently defined.
-->
<method name="SetDefaultSubvolumeID">
<arg name="id" direction="in" type="u"/>
<arg name="options" type="a{sv}" direction="in"/>
</method>

<property name="label" type="s" access="read"/>
<property name="uuid" type="s" access="read"/>
<property name="num_devices" type="t" access="read"/>
Expand Down
56 changes: 56 additions & 0 deletions modules/btrfs/udiskslinuxfilesystembtrfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,61 @@ handle_get_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
return TRUE;
}

static gboolean
handle_set_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
guint arg_id,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
GError *error = NULL;
UDisksDaemon *daemon;
gchar *mount_point = NULL;

object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}

daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));

/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to set the default BTRFS subvolume"),
invocation);

/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}

if (! bd_btrfs_set_default_subvolume (mount_point, arg_id, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}

/* Complete DBus call. */
udisks_filesystem_btrfs_complete_set_default_subvolume_id (fs_btrfs, invocation);

out:
/* Release the resources */
g_clear_object (&object);
g_free (mount_point);

/* Indicate that we handled the method invocation */
return TRUE;
}

static gboolean
handle_create_snapshot (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
Expand Down Expand Up @@ -895,6 +950,7 @@ udisks_linux_filesystem_btrfs_iface_init (UDisksFilesystemBTRFSIface *iface)
iface->handle_remove_subvolume = handle_remove_subvolume;
iface->handle_get_subvolumes = handle_get_subvolumes;
iface->handle_get_default_subvolume_id = handle_get_default_subvolume_id;
iface->handle_set_default_subvolume_id = handle_set_default_subvolume_id;
iface->handle_create_snapshot = handle_create_snapshot;
iface->handle_repair = handle_repair;
iface->handle_resize = handle_resize;
Expand Down
9 changes: 8 additions & 1 deletion src/tests/dbus-tests/test_btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def test_subvolume_mount(self):
dbus_mnt = self.ay_to_str(dbus_mounts.value[0]) # mountpoints are arrays of bytes
self.assertEqual(dbus_mnt, mnt_path)

def test_get_default_subvolume_id(self):
def test_default_subvolume_id(self):
dev = self._get_devices(1)[0]
self.addCleanup(self._clean_format, dev.obj)

Expand All @@ -399,3 +399,10 @@ def test_get_default_subvolume_id(self):
default_id = dev.obj.GetDefaultSubvolumeID(self.no_options,
dbus_interface=self.iface_prefix + '.Filesystem.BTRFS')
self.assertEqual(default_id, 5)

dev.obj.SetDefaultSubvolumeID(dbus.UInt32(5), self.no_options,
dbus_interface=self.iface_prefix + '.Filesystem.BTRFS')

default_id = dev.obj.GetDefaultSubvolumeID(self.no_options,
dbus_interface=self.iface_prefix + '.Filesystem.BTRFS')
self.assertEqual(default_id, 5)

0 comments on commit 4f24c90

Please sign in to comment.