static void udisks_client_get_object_info_for_loop (UDisksClient *client, UDisksLoop *loop, UDisksBlock *block, UDisksPartition *partition, UDisksObjectInfo *info) { guint64 size = 0; gchar *size_str = NULL; gchar *s; size = udisks_block_get_size (block); if (size > 0) size_str = udisks_client_get_size_for_display (client, size, FALSE, FALSE); info->icon = g_themed_icon_new_with_default_fallbacks ("drive-removable-media"); info->icon_symbolic = g_themed_icon_new_with_default_fallbacks ("drive-removable-media-symbolic"); info->name = udisks_loop_dup_backing_file (loop); if (size_str != NULL) { info->description = g_strdup_printf (_("%s Loop Device"), size_str); } else { info->description = g_strdup (_("Loop Device")); } if (partition != NULL) { /* Translators: Used to describe a partition of a loop device. * The %d is the partition number. * The %s is the description for the loop device (e.g. "5 GB Loop Device"). */ s = g_strdup_printf (C_("part-loop", "Partition %d of %s"), udisks_partition_get_number (partition), info->description); g_free (info->description); info->description = s; } /* Translators: String used for one-liner description of a loop device. * The first %s is the description of the object (e.g. "2 GB Loop Device"). * The second %s is the name of the backing file (e.g. "/home/davidz/file.iso"). * The third %s is the special device file (e.g. "/dev/loop2"). */ info->one_liner = g_strdup_printf (C_("one-liner-loop", "%s — %s (%s)"), info->description, info->name, udisks_block_get_preferred_device (block)); info->sort_key = g_strdup_printf ("03_loop_%s_%d", last_segment (g_dbus_object_get_object_path (G_DBUS_OBJECT (info->object))), partition != NULL ? udisks_partition_get_number (partition) : 0); }
void storage_block_update (StorageBlock *block) { CockpitStorageBlock *iface = COCKPIT_STORAGE_BLOCK (block); StorageProvider *provider; UDisksClient *udisks_client; UDisksObject *udisks_object; UDisksBlock *udisks_block; UDisksPartition *udisks_partition = NULL; UDisksPartitionTable *udisks_partition_table = NULL; UDisksFilesystem *udisks_filesystem = NULL; UDisksPhysicalVolume *udisks_physical_volume = NULL; provider = storage_object_get_provider (block->object); udisks_client = storage_provider_get_udisks_client (provider); udisks_block = block->udisks_block; udisks_object = (UDisksObject *)g_dbus_interface_get_object (G_DBUS_INTERFACE (udisks_block)); if (udisks_object != NULL) { udisks_partition = udisks_object_peek_partition (udisks_object); udisks_partition_table = udisks_object_peek_partition_table (udisks_object); udisks_filesystem = udisks_object_peek_filesystem (udisks_object); udisks_physical_volume = udisks_object_peek_physical_volume (udisks_object); } { gs_free gchar *d = g_filename_display_name (udisks_block_get_preferred_device (udisks_block)); cockpit_storage_block_set_device (iface, d); } cockpit_storage_block_set_size (iface, udisks_block_get_size (udisks_block)); cockpit_storage_block_set_id_usage (iface, udisks_block_get_id_usage (udisks_block)); cockpit_storage_block_set_id_type (iface, udisks_block_get_id_type (udisks_block)); cockpit_storage_block_set_id_version (iface, udisks_block_get_id_version (udisks_block)); cockpit_storage_block_set_id_label (iface, udisks_block_get_id_label (udisks_block)); cockpit_storage_block_set_id_uuid (iface, udisks_block_get_id_uuid (udisks_block)); if (udisks_partition == NULL) { cockpit_storage_block_set_partition_number (iface, 0); cockpit_storage_block_set_partition_table (iface, "/"); } else { UDisksPartitionTable *table_for_partition; const gchar *objpath = "/"; GDBusObject *o; UDisksBlock *b; StorageObject *so; table_for_partition = udisks_client_get_partition_table (udisks_client, udisks_partition); if (table_for_partition != NULL) { o = g_dbus_interface_get_object (G_DBUS_INTERFACE (table_for_partition)); if (o != NULL) { b = udisks_object_peek_block (UDISKS_OBJECT (o)); if (b != NULL) { so = storage_provider_lookup_for_udisks_block (provider, b); if (so != NULL) { objpath = g_dbus_object_get_object_path (G_DBUS_OBJECT (so)); } } } } cockpit_storage_block_set_partition_table (iface, objpath); cockpit_storage_block_set_partition_number (iface, udisks_partition_get_number (udisks_partition)); } GVariantBuilder partitions; g_variant_builder_init (&partitions, G_VARIANT_TYPE ("a(otts)")); if (udisks_partition_table != NULL) { GList *ps, *l; ps = udisks_client_get_partitions (udisks_client, udisks_partition_table); for (l = ps; l != NULL; l = l->next) { UDisksPartition *p = UDISKS_PARTITION (l->data); GDBusObject *o; UDisksBlock *b; StorageObject *so; o = g_dbus_interface_get_object (G_DBUS_INTERFACE (p)); if (o != NULL) { b = udisks_object_peek_block (UDISKS_OBJECT (o)); if (b != NULL) { so = storage_provider_lookup_for_udisks_block (provider, b); if (so != NULL) { const gchar *type = "p"; if (udisks_partition_get_is_container (p)) type = "x"; else if (udisks_partition_get_is_contained (p)) type = "l"; g_variant_builder_add (&partitions, "(otts)", g_dbus_object_get_object_path (G_DBUS_OBJECT (so)), udisks_partition_get_offset (p), udisks_partition_get_size (p), type); } } } } g_list_free_full (ps, g_object_unref); const gchar *type = udisks_partition_table_get_type_ (udisks_partition_table); if (type == NULL || type[0] == '\0') type = "unknown"; cockpit_storage_block_set_partition_table_type (iface, type); } else cockpit_storage_block_set_partition_table_type (iface, ""); cockpit_storage_block_set_partitions (iface, g_variant_builder_end (&partitions)); cockpit_storage_block_set_drive (iface, storage_provider_translate_path (provider, udisks_block_get_drive (udisks_block))); cockpit_storage_block_set_crypto_backing_device (iface, storage_provider_translate_path (provider, udisks_block_get_crypto_backing_device (udisks_block))); cockpit_storage_block_set_mdraid (iface, storage_provider_translate_path (provider, udisks_block_get_mdraid (udisks_block))); cockpit_storage_block_set_mdraid_member (iface, storage_provider_translate_path (provider, udisks_block_get_mdraid_member (udisks_block))); if (udisks_filesystem) { const gchar *const *p = udisks_filesystem_get_mount_points (udisks_filesystem); gchar *u[g_strv_length ((gchar **)p) + 1]; int i; for (i = 0; p[i]; i++) u[i] = g_filename_display_name (p[i]); u[i] = NULL; cockpit_storage_block_set_mounted_at (iface, (const gchar *const *)u); for (i = 0; u[i]; i++) g_free (u[i]); } GVariantIter iter; g_variant_iter_init (&iter, udisks_block_get_configuration (udisks_block)); const gchar *type; GVariant *details; gboolean got_fstab = FALSE, got_crypttab = FALSE; while (g_variant_iter_next (&iter, "(&s*)", &type, &details)) { if (strcmp (type, "fstab") == 0 && !got_fstab) { got_fstab = TRUE; const gchar *dir = variant_lookup (details, "dir"); if (dir) { gs_free gchar *dir_display = g_filename_display_name (dir); cockpit_storage_block_set_mount_point (iface, dir_display); } const gchar *opts = variant_lookup (details, "opts"); if (opts) { gs_free gchar *opts_locale = g_locale_to_utf8 (opts, -1, NULL, NULL, NULL); if (opts_locale) cockpit_storage_block_set_mount_options (iface, opts_locale); else g_warning ("Can't convert fstab options into UTF8"); } } else if (strcmp (type, "crypttab") == 0 && !got_crypttab) { got_crypttab = TRUE; const gchar *opts = variant_lookup (details, "options"); if (opts) { gs_free gchar *opts_locale = g_locale_to_utf8 (opts, -1, NULL, NULL, NULL); if (opts_locale) cockpit_storage_block_set_crypto_options (iface, opts_locale); else g_warning ("Can't convert crypttab options into UTF8"); } } g_variant_unref (details); } cockpit_storage_block_set_logical_volume (iface, storage_provider_translate_path (provider, udisks_block_get_logical_volume (udisks_block))); if (udisks_physical_volume) { cockpit_storage_block_set_pv_group (iface, storage_provider_translate_path (provider, udisks_physical_volume_get_volume_group (udisks_physical_volume))); cockpit_storage_block_set_pv_size (iface, udisks_physical_volume_get_size (udisks_physical_volume)); cockpit_storage_block_set_pv_free_size (iface, udisks_physical_volume_get_free_size (udisks_physical_volume)); } else cockpit_storage_block_set_pv_group (iface, "/"); }
static void udisks_client_get_object_info_for_drive (UDisksClient *client, UDisksDrive *drive, UDisksPartition *partition, UDisksObjectInfo *info) { const gchar *vendor; const gchar *model; const gchar *media; const gchar *const *media_compat; gboolean media_available; gboolean media_removable; gint rotation_rate; guint64 size; gchar *size_str; guint n; GString *desc_str; DriveType desc_type; gchar *hyphenated_connection_bus; const gchar *connection_bus; UDisksBlock *block = NULL; gchar *s; const gchar *cs; UDisksBlock *block_for_partition = NULL; g_return_if_fail (UDISKS_IS_DRIVE (drive)); size_str = NULL; vendor = udisks_drive_get_vendor (drive); model = udisks_drive_get_model (drive); size = udisks_drive_get_size (drive); media_removable = udisks_drive_get_media_removable (drive); media_available = udisks_drive_get_media_available (drive); rotation_rate = udisks_drive_get_rotation_rate (drive); if (size > 0) size_str = udisks_client_get_size_for_display (client, size, FALSE, FALSE); media = udisks_drive_get_media (drive); media_compat = udisks_drive_get_media_compatibility (drive); connection_bus = udisks_drive_get_connection_bus (drive); if (strlen (connection_bus) > 0) hyphenated_connection_bus = g_strdup_printf ("-%s", connection_bus); else hyphenated_connection_bus = g_strdup (""); /* Name is easy - that's just "$vendor $model" */ if (strlen (vendor) == 0) vendor = NULL; if (strlen (model) == 0) model = NULL; info->name = g_strdup_printf ("%s%s%s", vendor != NULL ? vendor : "", vendor != NULL ? " " : "", model != NULL ? model : ""); desc_type = DRIVE_TYPE_UNSET; desc_str = g_string_new (NULL); for (n = 0; n < G_N_ELEMENTS (media_data) - 1; n++) { /* media_compat */ if (strv_has (media_compat, media_data[n].id)) { if (info->icon == NULL) info->icon = g_themed_icon_new_with_default_fallbacks (media_data[n].drive_icon); if (info->icon_symbolic == NULL) info->icon_symbolic = g_themed_icon_new_with_default_fallbacks (media_data[n].drive_icon_symbolic); if (strstr (desc_str->str, media_data[n].media_family) == NULL) { if (desc_str->len > 0) g_string_append (desc_str, "/"); g_string_append (desc_str, g_dpgettext2 (GETTEXT_PACKAGE, "media-type", media_data[n].media_family)); } desc_type = media_data[n].media_type; } if (media_removable && media_available) { /* media */ if (g_strcmp0 (media, media_data[n].id) == 0) { if (info->media_description == NULL) { switch (media_data[n].media_type) { case DRIVE_TYPE_UNSET: g_assert_not_reached (); break; case DRIVE_TYPE_DRIVE: /* Translators: Used to describe drive without removable media. The %s is the type, e.g. 'Thumb' */ info->media_description = g_strdup_printf (C_("drive-with-fixed-media", "%s Drive"), g_dpgettext2 (GETTEXT_PACKAGE, "media-type", media_data[n].media_name)); break; case DRIVE_TYPE_DISK: /* Translators: Used to describe generic media. The %s is the type, e.g. 'Zip' or 'Floppy' */ info->media_description = g_strdup_printf (C_("drive-with-generic-media", "%s Disk"), g_dpgettext2 (GETTEXT_PACKAGE, "media-type", media_data[n].media_name)); break; case DRIVE_TYPE_CARD: /* Translators: Used to describe flash media. The %s is the type, e.g. 'SD' or 'CompactFlash' */ info->media_description = g_strdup_printf (C_("flash-media", "%s Card"), g_dpgettext2 (GETTEXT_PACKAGE, "media-type", media_data[n].media_name)); break; case DRIVE_TYPE_DISC: /* Translators: Used to describe optical discs. The %s is the type, e.g. 'CD-R' or 'DVD-ROM' */ info->media_description = g_strdup_printf (C_("optical-media", "%s Disc"), g_dpgettext2 (GETTEXT_PACKAGE, "media-type", media_data[n].media_name)); break; } } if (info->media_icon == NULL) info->media_icon = g_themed_icon_new_with_default_fallbacks (media_data[n].media_icon); if (info->media_icon_symbolic == NULL) info->media_icon_symbolic = g_themed_icon_new_with_default_fallbacks (media_data[n].media_icon_symbolic); } } } switch (desc_type) { case DRIVE_TYPE_UNSET: if (media_removable) { if (size_str != NULL) { /* Translators: Used to describe a drive. The %s is the size, e.g. '20 GB' */ info->description = g_strdup_printf (C_("drive-with-size", "%s Drive"), size_str); } else { /* Translators: Used to describe a drive we know very little about (removable media or size not known) */ info->description = g_strdup (C_("generic-drive", "Drive")); } } else { if (rotation_rate == 0) { if (size_str != NULL) { /* Translators: Used to describe a non-rotating drive (rotation rate either unknown * or it's a solid-state drive). The %s is the size, e.g. '20 GB'. */ info->description = g_strdup_printf (C_("disk-non-rotational", "%s Disk"), size_str); } else { /* Translators: Used to describe a non-rotating drive (rotation rate either unknown * or it's a solid-state drive). The drive is either using removable media or its * size not known. */ info->description = g_strdup (C_("disk-non-rotational", "Disk")); } } else { if (size_str != NULL) { /* Translators: Used to describe a hard-disk drive (HDD). The %s is the size, e.g. '20 GB'. */ info->description = g_strdup_printf (C_("disk-hdd", "%s Hard Disk"), size_str); } else { /* Translators: Used to describe a hard-disk drive (HDD) (removable media or size not known) */ info->description = g_strdup (C_("disk-hdd", "Hard Disk")); } } } break; case DRIVE_TYPE_CARD: /* Translators: Used to describe a card reader. The %s is the card type e.g. 'CompactFlash'. */ info->description = g_strdup_printf (C_("drive-card-reader", "%s Card Reader"), desc_str->str); break; case DRIVE_TYPE_DRIVE: /* explicit fall-through */ case DRIVE_TYPE_DISK: /* explicit fall-through */ case DRIVE_TYPE_DISC: if (!media_removable && size_str != NULL) { /* Translators: Used to describe drive. The first %s is the size e.g. '20 GB' and the * second %s is the drive type e.g. 'Thumb'. */ info->description = g_strdup_printf (C_("drive-with-size-and-type", "%s %s Drive"), size_str, desc_str->str); } else { /* Translators: Used to describe drive. The first %s is the drive type e.g. 'Thumb'. */ info->description = g_strdup_printf (C_("drive-with-type", "%s Drive"), desc_str->str); } break; } g_string_free (desc_str, TRUE); /* fallback for icon */ if (info->icon == NULL) { if (media_removable) { s = g_strdup_printf ("drive-removable-media%s", hyphenated_connection_bus); } else { if (rotation_rate == 0) s = g_strdup_printf ("drive-harddisk-solidstate%s", hyphenated_connection_bus); else s = g_strdup_printf ("drive-harddisk%s", hyphenated_connection_bus); } info->icon = g_themed_icon_new_with_default_fallbacks (s); g_free (s); } /* fallback for icon_symbolic */ if (info->icon_symbolic == NULL) { if (media_removable) { s = g_strdup_printf ("drive-removable-media%s-symbolic", hyphenated_connection_bus); } else { if (rotation_rate == 0) s = g_strdup_printf ("drive-harddisk-solidstate%s-symbolic", hyphenated_connection_bus); else s = g_strdup_printf ("drive-harddisk%s-symbolic", hyphenated_connection_bus); } info->icon_symbolic = g_themed_icon_new_with_default_fallbacks (s); g_free (s); } /* fallback for media_icon */ if (media_removable && media_available && info->media_icon == NULL) { if (media_removable) { s = g_strdup_printf ("drive-removable-media%s", hyphenated_connection_bus); } else { if (rotation_rate == 0) s = g_strdup_printf ("drive-harddisk-solidstate%s", hyphenated_connection_bus); else s = g_strdup_printf ("drive-harddisk%s", hyphenated_connection_bus); } info->media_icon = g_themed_icon_new_with_default_fallbacks (s); g_free (s); } /* fallback for media_icon_symbolic */ if (media_removable && media_available && info->media_icon_symbolic == NULL) { if (media_removable) { s = g_strdup_printf ("drive-removable-media%s-symbolic", hyphenated_connection_bus); } else { if (rotation_rate == 0) s = g_strdup_printf ("drive-harddisk-solidstate%s-symbolic", hyphenated_connection_bus); else s = g_strdup_printf ("drive-harddisk%s-symbolic", hyphenated_connection_bus); } info->media_icon_symbolic = g_themed_icon_new_with_default_fallbacks (s); g_free (s); } /* prepend a qualifier to the media description, based on the disc state */ if (udisks_drive_get_optical_blank (drive)) { /* Translators: String used for a blank disc. The %s is the disc type e.g. "CD-RW Disc" */ s = g_strdup_printf (C_("optical-media", "Blank %s"), info->media_description); g_free (info->media_description); info->media_description = s; } else if (udisks_drive_get_optical_num_audio_tracks (drive) > 0 && udisks_drive_get_optical_num_data_tracks (drive) > 0) { /* Translators: String used for a mixed disc. The %s is the disc type e.g. "CD-ROM Disc" */ s = g_strdup_printf (C_("optical-media", "Mixed %s"), info->media_description); g_free (info->media_description); info->media_description = s; } else if (udisks_drive_get_optical_num_audio_tracks (drive) > 0 && udisks_drive_get_optical_num_data_tracks (drive) == 0) { /* Translators: String used for an audio disc. The %s is the disc type e.g. "CD-ROM Disc" */ s = g_strdup_printf (C_("optical-media", "Audio %s"), info->media_description); g_free (info->media_description); info->media_description = s; } /* Apply UDISKS_NAME, UDISKS_ICON_NAME, UDISKS_SYMBOLIC_ICON_NAME hints, if available */ block = udisks_client_get_block_for_drive (client, drive, TRUE); if (block != NULL) { cs = udisks_block_get_hint_name (block); if (cs != NULL && strlen (cs) > 0) { g_free (info->description); g_free (info->media_description); info->description = g_strdup (cs); info->media_description = g_strdup (cs); } cs = udisks_block_get_hint_icon_name (block); if (cs != NULL && strlen (cs) > 0) { g_clear_object (&info->icon); g_clear_object (&info->media_icon); info->icon = g_themed_icon_new_with_default_fallbacks (cs); info->media_icon = g_themed_icon_new_with_default_fallbacks (cs); } cs = udisks_block_get_hint_symbolic_icon_name (block); if (cs != NULL && strlen (cs) > 0) { g_clear_object (&info->icon_symbolic); g_clear_object (&info->media_icon_symbolic); info->icon_symbolic = g_themed_icon_new_with_default_fallbacks (cs); info->media_icon_symbolic = g_themed_icon_new_with_default_fallbacks (cs); } } if (partition != NULL) { GDBusObject *object_for_partition; object_for_partition = g_dbus_interface_get_object (G_DBUS_INTERFACE (partition)); if (object_for_partition != NULL) block_for_partition = udisks_object_peek_block (UDISKS_OBJECT (object_for_partition)); } if (block_for_partition == NULL) block_for_partition = block; if (partition != NULL) { /* Translators: Used to describe a partition of a drive. * The %d is the partition number. * The %s is the description for the drive (e.g. "2 GB Thumb Drive"). */ s = g_strdup_printf (C_("part-drive", "Partition %d of %s"), udisks_partition_get_number (partition), info->description); g_free (info->description); info->description = s; } /* calculate and set one-liner */ if (block != NULL) { const gchar *drive_revision = udisks_drive_get_revision (drive); if (strlen (drive_revision) > 0) { /* Translators: String used for one-liner description of drive. * The first %s is the description of the object (e.g. "80 GB Disk" or "Partition 2 of 2 GB Thumb Drive"). * The second %s is the name of the object (e.g. "INTEL SSDSA2MH080G1GC"). * The third %s is the fw revision (e.g "45ABX21"). * The fourth %s is the special device file (e.g. "/dev/sda"). */ info->one_liner = g_strdup_printf (C_("one-liner-drive", "%s — %s [%s] (%s)"), info->description, info->name, drive_revision, udisks_block_get_preferred_device (block_for_partition)); } else { /* Translators: String used for one-liner description of drive w/o known fw revision. * The first %s is the description of the object (e.g. "80 GB Disk"). * The second %s is the name of the object (e.g. "INTEL SSDSA2MH080G1GC"). * The third %s is the special device file (e.g. "/dev/sda"). */ info->one_liner = g_strdup_printf (C_("one-liner-drive", "%s — %s (%s)"), info->description, info->name, udisks_block_get_preferred_device (block_for_partition)); } } g_free (hyphenated_connection_bus); g_free (size_str); info->sort_key = g_strdup_printf ("00_drive_%s", udisks_drive_get_sort_key (drive)); g_clear_object (&block); }
static void udisks_client_get_object_info_for_mdraid (UDisksClient *client, UDisksMDRaid *mdraid, UDisksPartition *partition, UDisksObjectInfo *info) { UDisksBlock *block = NULL; guint64 size = 0; gchar *size_str = NULL; const gchar *name; const gchar *level; gchar *s; block = udisks_client_get_block_for_mdraid (client, mdraid); size = udisks_mdraid_get_size (mdraid); if (size > 0) size_str = udisks_client_get_size_for_display (client, size, FALSE, FALSE); name = udisks_mdraid_get_name (mdraid); s = strstr (name, ":"); if (s != NULL && strlen (s) > 1) info->name = g_strdup (s + 1); else info->name = g_strdup (name); info->icon = g_themed_icon_new_with_default_fallbacks ("drive-multidisk"); info->icon_symbolic = g_themed_icon_new_with_default_fallbacks ("drive-multidisk-symbolic"); level = udisks_mdraid_get_level (mdraid); if (size_str != NULL) { /* Translators: Used to format the description for a RAID array. * The first %s is the size (e.g. '42.0 GB'). * The second %s is the level (e.g. 'RAID-5 Array'). */ info->description = g_strdup_printf (C_("mdraid-desc", "%s %s"), size_str, format_mdraid_level (level)); } else { info->description = g_strdup (format_mdraid_level (level)); } if (partition != NULL) { /* Translators: Used to describe a partition of a RAID Array. * The %d is the partition number. * The %s is the description for the drive (e.g. "2 TB RAID-5"). */ s = g_strdup_printf (C_("part-raid", "Partition %d of %s"), udisks_partition_get_number (partition), info->description); g_free (info->description); info->description = s; } if (strlen (info->name) > 0) { if (block != NULL) { /* Translators: String used for one-liner description of running RAID array. * The first %s is the array name (e.g. "AlphaGo"). * The second %s is the size and level (e.g. "2 TB RAID-5"). * The third %s is the special device file (e.g. "/dev/sda"). */ info->one_liner = g_strdup_printf (C_("one-liner-mdraid-running", "%s — %s (%s)"), info->name, info->description, udisks_block_get_preferred_device (block)); } else { /* Translators: String used for one-liner description of non-running RAID array. * The first %s is the array name (e.g. "AlphaGo"). * The second %s is the size and level (e.g. "2 TB RAID-5"). */ info->one_liner = g_strdup_printf (C_("one-liner-mdraid-not-running", "%s — %s"), info->name, info->description); } } else { if (block != NULL) { /* Translators: String used for one-liner description of running RAID array w/o a name. * The first %s is the array name (e.g. "AlphaGo"). * The second %s is the size and level (e.g. "2 TB RAID-5"). * The third %s is the special device file (e.g. "/dev/sda"). */ info->one_liner = g_strdup_printf (C_("one-liner-mdraid-no-name-running", "%s — %s"), info->description, udisks_block_get_preferred_device (block)); } else { /* Translators: String used for one-liner description of non-running RAID array w/o a name. * The first %s is the array name (e.g. "AlphaGo"). * The second %s is the size and level (e.g. "2 TB RAID-5"). */ info->one_liner = g_strdup_printf (C_("one-liner-mdraid-no-name-not-running", "%s"), info->description); } } g_clear_object (&block); info->sort_key = g_strdup_printf ("01_mdraid_%s_%d", udisks_mdraid_get_uuid (mdraid), partition != NULL ? udisks_partition_get_number (partition) : 0); }
/** * udisks_daemon_util_check_authorization_sync: * @daemon: A #UDisksDaemon. * @object: (allow-none): The #GDBusObject that the call is on or %NULL. * @action_id: The action id to check for. * @options: (allow-none): A #GVariant to check for the <quote>auth.no_user_interaction</quote> option or %NULL. * @message: The message to convey (use N_). * @invocation: The invocation to check for. * * Checks if the caller represented by @invocation is authorized for * the action identified by @action_id, optionally displaying @message * if authentication is needed. Additionally, if the caller is not * authorized, the appropriate error is already returned to the caller * via @invocation. * * The calling thread is blocked for the duration of the authorization * check which could be a very long time since it may involve * presenting an authentication dialog and having a human user use * it. If <quote>auth.no_user_interaction</quote> in @options is %TRUE * no authentication dialog will be presented and the check is not * expected to take a long time. * * See <xref linkend="udisks-polkit-details"/> for the variables that * can be used in @message but note that not all variables can be used * in all checks. For example, any check involving a #UDisksDrive or a * #UDisksBlock object can safely include the fragment * <quote>$(drive)</quote> since it will always expand to the name of * the drive, e.g. <quote>INTEL SSDSA2MH080G1GC (/dev/sda1)</quote> or * the block device file e.g. <quote>/dev/vg_lucifer/lv_root</quote> * or <quote>/dev/sda1</quote>. However this won't work for operations * that isn't on a drive or block device, for example calls on the * <link linkend="gdbus-interface-org-freedesktop-UDisks2-Manager.top_of_page">Manager</link> * object. * * Returns: %TRUE if caller is authorized, %FALSE if not. */ gboolean udisks_daemon_util_check_authorization_sync (UDisksDaemon *daemon, UDisksObject *object, const gchar *action_id, GVariant *options, const gchar *message, GDBusMethodInvocation *invocation) { PolkitAuthority *authority = NULL; PolkitSubject *subject = NULL; PolkitDetails *details = NULL; PolkitCheckAuthorizationFlags flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE; PolkitAuthorizationResult *result = NULL; GError *error = NULL; gboolean ret = FALSE; UDisksBlock *block = NULL; UDisksDrive *drive = NULL; UDisksPartition *partition = NULL; UDisksObject *block_object = NULL; UDisksObject *drive_object = NULL; gboolean auth_no_user_interaction = FALSE; const gchar *details_device = NULL; gchar *details_drive = NULL; authority = udisks_daemon_get_authority (daemon); if (authority == NULL) { ret = check_authorization_no_polkit (daemon, object, action_id, options, message, invocation); goto out; } subject = polkit_system_bus_name_new (g_dbus_method_invocation_get_sender (invocation)); if (options != NULL) { g_variant_lookup (options, "auth.no_user_interaction", "b", &auth_no_user_interaction); } if (!auth_no_user_interaction) flags = POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION; details = polkit_details_new (); polkit_details_insert (details, "polkit.message", message); polkit_details_insert (details, "polkit.gettext_domain", "udisks2"); /* Find drive associated with the block device, if any */ if (object != NULL) { block = udisks_object_get_block (object); if (block != NULL) { block_object = g_object_ref (object); drive_object = udisks_daemon_find_object (daemon, udisks_block_get_drive (block)); if (drive_object != NULL) drive = udisks_object_get_drive (drive_object); } partition = udisks_object_get_partition (object); if (drive == NULL) drive = udisks_object_get_drive (object); } if (block != NULL) details_device = udisks_block_get_preferred_device (block); /* If we have a drive, use vendor/model in the message (in addition to Block:preferred-device) */ if (drive != NULL) { gchar *s; const gchar *vendor; const gchar *model; vendor = udisks_drive_get_vendor (drive); model = udisks_drive_get_model (drive); if (vendor == NULL) vendor = ""; if (model == NULL) model = ""; if (strlen (vendor) > 0 && strlen (model) > 0) s = g_strdup_printf ("%s %s", vendor, model); else if (strlen (vendor) > 0) s = g_strdup (vendor); else s = g_strdup (model); if (block != NULL) { details_drive = g_strdup_printf ("%s (%s)", s, udisks_block_get_preferred_device (block)); } else { details_drive = s; s = NULL; } g_free (s); _safe_polkit_details_insert (details, "drive.wwn", udisks_drive_get_wwn (drive)); _safe_polkit_details_insert (details, "drive.serial", udisks_drive_get_serial (drive)); _safe_polkit_details_insert (details, "drive.vendor", udisks_drive_get_vendor (drive)); _safe_polkit_details_insert (details, "drive.model", udisks_drive_get_model (drive)); _safe_polkit_details_insert (details, "drive.revision", udisks_drive_get_revision (drive)); if (udisks_drive_get_removable (drive)) { const gchar *const *media_compat; GString *media_compat_str; const gchar *sep = ","; polkit_details_insert (details, "drive.removable", "true"); _safe_polkit_details_insert (details, "drive.removable.bus", udisks_drive_get_connection_bus (drive)); media_compat_str = g_string_new (NULL); media_compat = udisks_drive_get_media_compatibility (drive); if (media_compat) { guint i; for (i = 0; media_compat[i] && strlen(media_compat[i]); i++) { if (i) g_string_append (media_compat_str, sep); g_string_append (media_compat_str, media_compat[i]); } } _safe_polkit_details_insert (details, "drive.removable.media", media_compat_str->str); g_string_free (media_compat_str, TRUE); } } if (block != NULL) { _safe_polkit_details_insert (details, "id.type", udisks_block_get_id_type (block)); _safe_polkit_details_insert (details, "id.usage", udisks_block_get_id_usage (block)); _safe_polkit_details_insert (details, "id.version", udisks_block_get_id_version (block)); _safe_polkit_details_insert (details, "id.label", udisks_block_get_id_label (block)); _safe_polkit_details_insert (details, "id.uuid", udisks_block_get_id_uuid (block)); } if (partition != NULL) { _safe_polkit_details_insert_int (details, "partition.number", udisks_partition_get_number (partition)); _safe_polkit_details_insert (details, "partition.type", udisks_partition_get_type_ (partition)); _safe_polkit_details_insert_uint64 (details, "partition.flags", udisks_partition_get_flags (partition)); _safe_polkit_details_insert (details, "partition.name", udisks_partition_get_name (partition)); _safe_polkit_details_insert (details, "partition.uuid", udisks_partition_get_uuid (partition)); } /* Fall back to Block:preferred-device */ if (details_drive == NULL && block != NULL) details_drive = udisks_block_dup_preferred_device (block); if (details_device != NULL) polkit_details_insert (details, "device", details_device); if (details_drive != NULL) polkit_details_insert (details, "drive", details_drive); error = NULL; result = polkit_authority_check_authorization_sync (authority, subject, action_id, details, flags, NULL, /* GCancellable* */ &error); if (result == NULL) { if (error->domain != POLKIT_ERROR) { /* assume polkit authority is not available (e.g. could be the service * manager returning org.freedesktop.systemd1.Masked) */ g_error_free (error); ret = check_authorization_no_polkit (daemon, object, action_id, options, message, invocation); } else { g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED, "Error checking authorization: %s (%s, %d)", error->message, g_quark_to_string (error->domain), error->code); g_error_free (error); } goto out; } if (!polkit_authorization_result_get_is_authorized (result)) { if (polkit_authorization_result_get_dismissed (result)) g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, UDISKS_ERROR_NOT_AUTHORIZED_DISMISSED, "The authentication dialog was dismissed"); else g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, polkit_authorization_result_get_is_challenge (result) ? UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN : UDISKS_ERROR_NOT_AUTHORIZED, "Not authorized to perform operation"); goto out; } ret = TRUE; out: g_free (details_drive); g_clear_object (&block_object); g_clear_object (&drive_object); g_clear_object (&block); g_clear_object (&partition); g_clear_object (&drive); g_clear_object (&subject); g_clear_object (&details); g_clear_object (&result); return ret; }
/** * udisks_linux_drive_object_is_not_in_use: * @object: A #UDisksLinuxDriveObject. * @cancellable: (allow-none): A #GCancellable or %NULL. * @error: A #GError or %NULL. * * Checks if the drive represented by @object is in use and sets * @error if so. * * Returns: %TRUE if @object is not is use, %FALSE if @error is set. */ gboolean udisks_linux_drive_object_is_not_in_use (UDisksLinuxDriveObject *object, GCancellable *cancellable, GError **error) { GDBusObjectManagerServer *object_manager; const gchar *drive_object_path; gboolean ret = TRUE; GList *objects = NULL; GList *l; g_return_val_if_fail (UDISKS_IS_LINUX_DRIVE_OBJECT (object), FALSE); g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); drive_object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); object_manager = udisks_daemon_get_object_manager (object->daemon); objects = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (object_manager)); /* Visit all block devices related to the drive... */ for (l = objects; l != NULL; l = l->next) { GDBusObjectSkeleton *iter_object = G_DBUS_OBJECT_SKELETON (l->data); UDisksBlock *block; UDisksFilesystem *filesystem; if (!UDISKS_IS_LINUX_BLOCK_OBJECT (iter_object)) continue; block = udisks_object_peek_block (UDISKS_OBJECT (iter_object)); filesystem = udisks_object_peek_filesystem (UDISKS_OBJECT (iter_object)); if (g_strcmp0 (udisks_block_get_drive (block), drive_object_path) != 0) continue; /* bail if block device is mounted */ if (filesystem != NULL) { if (g_strv_length ((gchar **) udisks_filesystem_get_mount_points (filesystem)) > 0) { g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_DEVICE_BUSY, "Device %s is mounted", udisks_block_get_preferred_device (block)); ret = FALSE; goto out; } } /* bail if block device is unlocked (LUKS) */ if (is_block_unlocked (objects, g_dbus_object_get_object_path (G_DBUS_OBJECT (iter_object)))) { g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_DEVICE_BUSY, "Encrypted device %s is unlocked", udisks_block_get_preferred_device (block)); ret = FALSE; goto out; } } out: g_list_free_full (objects, g_object_unref); return ret; }