예제 #1
0
static UDisksObject *
lookup_object_for_block (UDisksClient  *client,
                         dev_t          block_device)
{
  UDisksObject *ret;
  GList *objects;
  GList *l;

  ret = NULL;

  objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client));
  for (l = objects; l != NULL; l = l->next)
    {
      UDisksObject *object = UDISKS_OBJECT (l->data);
      UDisksBlock *block;

      block = udisks_object_peek_block (object);
      if (block != NULL)
        {
          if (block_device == udisks_block_get_device_number (block))
            {
              ret = g_object_ref (object);
              goto out;
            }
        }
    }

 out:
  g_list_foreach (objects, (GFunc) g_object_unref, NULL);
  g_list_free (objects);

  return ret;
}
예제 #2
0
static GDBusInterface *
get_udisk_iface (UDisksObject *object)
{
  UDisksBlock *block = udisks_object_peek_block (object);
  if (block)
    {
      /* don't include unused loop or nbd devices */
      if (udisks_block_get_size (block) == 0 &&
          (g_str_has_prefix (udisks_block_get_device (block), "/dev/loop") ||
           g_str_has_prefix (udisks_block_get_device (block), "/dev/nbd")))
        return NULL;

      return G_DBUS_INTERFACE (block);
    }

  UDisksDrive *drive = udisks_object_peek_drive (object);
  if (drive)
    {
      return G_DBUS_INTERFACE (drive);
    }

  UDisksMDRaid *mdraid = udisks_object_peek_mdraid (object);
  if (mdraid)
    {
      return G_DBUS_INTERFACE (mdraid);
    }

  return NULL;
}
예제 #3
0
static UDisksBlock *
peek_block_for_logical_volume (UDisksLogicalVolume *volume,
                               UDisksDaemon        *daemon)
{
  UDisksBlock *ret = NULL;
  GDBusObject *object;
  GList *l, *objects = NULL;
  UDisksBlockLVM2 *block_lvm2;

  object = g_dbus_interface_get_object (G_DBUS_INTERFACE (volume));
  if (object == NULL)
    goto out;

  objects = udisks_daemon_get_objects (daemon);
  for (l = objects; l != NULL; l = l->next)
    {
      block_lvm2 = udisks_object_peek_block_lvm2 (UDISKS_OBJECT(l->data));
      if (block_lvm2 &&
          g_strcmp0 (udisks_block_lvm2_get_logical_volume (block_lvm2),
                     g_dbus_object_get_object_path (object)) == 0)
        {
          ret = udisks_object_peek_block (UDISKS_OBJECT(l->data));
          goto out;
        }
    }

 out:
  g_list_free_full (objects, g_object_unref);
  return ret;
}
예제 #4
0
void
storage_mdraid_update (StorageMDRaid *mdraid)
{
    UDisksMDRaid *udisks_mdraid = mdraid->udisks_mdraid;
    CockpitStorageMDRaid *iface = COCKPIT_STORAGE_MDRAID (mdraid);
    StorageProvider *provider = storage_object_get_provider (mdraid->object);
    UDisksClient *udisks_client = storage_provider_get_udisks_client (provider);

    cockpit_storage_mdraid_set_uuid (iface, udisks_mdraid_get_uuid (udisks_mdraid));
    cockpit_storage_mdraid_set_name (iface, udisks_mdraid_get_name (udisks_mdraid));
    cockpit_storage_mdraid_set_level (iface, udisks_mdraid_get_level (udisks_mdraid));
    cockpit_storage_mdraid_set_num_devices (iface, udisks_mdraid_get_num_devices (udisks_mdraid));
    cockpit_storage_mdraid_set_size (iface, udisks_mdraid_get_size (udisks_mdraid));
    cockpit_storage_mdraid_set_sync_action (iface, udisks_mdraid_get_sync_action (udisks_mdraid));
    cockpit_storage_mdraid_set_sync_completed (iface, udisks_mdraid_get_sync_completed (udisks_mdraid));
    cockpit_storage_mdraid_set_sync_rate (iface, udisks_mdraid_get_sync_rate (udisks_mdraid));
    cockpit_storage_mdraid_set_sync_remaining_time (iface, udisks_mdraid_get_sync_remaining_time (udisks_mdraid));
    cockpit_storage_mdraid_set_degraded (iface, udisks_mdraid_get_degraded (udisks_mdraid));
    {
        gs_free gchar *loc = g_locale_to_utf8 (udisks_mdraid_get_bitmap_location (udisks_mdraid),
                                               -1, NULL, NULL, NULL);
        cockpit_storage_mdraid_set_bitmap_location (iface, loc);
    }
    cockpit_storage_mdraid_set_chunk_size (iface, udisks_mdraid_get_chunk_size (udisks_mdraid));

    GVariantBuilder devices;
    g_variant_builder_init (&devices, G_VARIANT_TYPE("a(oiast)"));

    GVariantIter iter;
    gint disk_slot;
    const gchar *disk_block_objpath;
    gs_unref_variant GVariant *disk_states = NULL;
    guint64 disk_num_errors;
    g_variant_iter_init (&iter, udisks_mdraid_get_active_devices (udisks_mdraid));
    while (g_variant_iter_next (&iter, "(&oi@asta{sv})",
                                &disk_block_objpath,
                                &disk_slot,
                                &disk_states,
                                &disk_num_errors,
                                NULL))
    {
        UDisksObject *udisks_object;
        UDisksBlock *udisks_block;
        StorageObject *object;

        if ((udisks_object = udisks_client_peek_object (udisks_client, disk_block_objpath))
                && (udisks_block = udisks_object_peek_block (udisks_object))
                && (object = storage_provider_lookup_for_udisks_block (provider, udisks_block)))
        {
            g_variant_builder_add (&devices, "(oi@ast)",
                                   g_dbus_object_get_object_path (G_DBUS_OBJECT(object)),
                                   disk_slot,
                                   disk_states,
                                   disk_num_errors);
        }
    }
    cockpit_storage_mdraid_set_active_devices (iface, g_variant_builder_end (&devices));
}
예제 #5
0
static gboolean
walk_block_parents (UDisksClient *client,
                    GDBusObjectManager  *objman,
                    UDisksBlock *block,
                    ObjectWalker *walker,
                    gpointer user_data,
                    GError **error)
{
  /* Parents are
     - of a block that is a logical volume, the logical volume object
     - of a clear text device, the encrypted device.

     XXX - support the whole tree.
  */

  while (block)
    {
      const gchar *path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (block));
      LvmLogicalVolumeBlock *lvm_block =
        LVM_LOGICAL_VOLUME_BLOCK (g_dbus_object_manager_get_interface (objman, path,
                                                                       "com.redhat.lvm2.LogicalVolumeBlock"));

      const gchar *logical_volume_path =
        (lvm_block ? lvm_logical_volume_block_get_logical_volume (lvm_block) : "/");
      const gchar *crypto_path = udisks_block_get_crypto_backing_device (block);

      if (g_strcmp0 (logical_volume_path, "/") != 0)
        {
          gs_unref_object LvmObject *logical_volume_object =
            LVM_OBJECT (g_dbus_object_manager_get_object (objman, logical_volume_path));

          if (logical_volume_object)
            {
              if (!walker (client, G_DBUS_OBJECT (logical_volume_object), user_data, error))
                return FALSE;
            }
          block = NULL;
        }
      else if (g_strcmp0 (crypto_path, "/") != 0)
        {
          UDisksObject *crypto_object = udisks_client_peek_object (client, crypto_path);
          if (crypto_object)
            {
              if (!walker (client, G_DBUS_OBJECT (crypto_object), user_data, error))
                return FALSE;
            }
          block = udisks_object_peek_block (crypto_object);
        }
      else
        block = NULL;
    }

  return TRUE;
}
예제 #6
0
static UDisksBlock *
create_partition (StorageBlock *block,
                  guint64 offset,
                  guint64 size,
                  const gchar *type,
                  GError **error)
{
  UDisksObject *udisks_object =
    (UDisksObject *) g_dbus_interface_get_object (G_DBUS_INTERFACE (block->udisks_block));
  if (udisks_object == NULL)
    {
      g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED,
                   "No object!?");
      return NULL;
    }

  UDisksPartitionTable *table = udisks_object_peek_partition_table (udisks_object);
  if (table == NULL)
    {
      g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED,
                   "Block device has no partition table");
      return NULL;
    }

  gs_free gchar *part_object_path = NULL;

  if (!udisks_partition_table_call_create_partition_sync (table,
                                                          offset,
                                                          size,
                                                          type,
                                                          "",
                                                          g_variant_new ("a{sv}", NULL),
                                                          &part_object_path,
                                                          NULL,
                                                          error))
    return NULL;

  StorageProvider *provider = storage_object_get_provider (block->object);
  UDisksClient *client = storage_provider_get_udisks_client (provider);
  udisks_client_settle (client);

  gs_unref_object UDisksObject *partition_object =
      udisks_client_get_object (client, part_object_path);
  UDisksBlock *partition_block = udisks_object_peek_block (partition_object);
  if (partition_block == NULL)
    {
      g_set_error (error, COCKPIT_ERROR, COCKPIT_ERROR_FAILED,
                   "Partition has no associated block device");
      return NULL;
    }

  return partition_block;
}
예제 #7
0
const gchar *
storage_provider_translate_path (StorageProvider *provider,
                                 const gchar *udisks_or_lvm_path)
{
  StorageObject *object = NULL;

  if (udisks_or_lvm_path == NULL)
    udisks_or_lvm_path = "/";

  gs_unref_object UDisksObject *udisks_object = udisks_client_get_object (provider->udisks_client,
                                                                          udisks_or_lvm_path);
  if (udisks_object != NULL)
    {

      UDisksDrive *udisks_drive = udisks_object_peek_drive (udisks_object);
      if (udisks_drive != NULL)
        object = storage_provider_lookup_for_udisks_drive (provider, udisks_drive);

      UDisksBlock *udisks_block = udisks_object_peek_block (udisks_object);
      if (udisks_block != NULL)
        object = storage_provider_lookup_for_udisks_block (provider, udisks_block);

      UDisksMDRaid *udisks_raid = udisks_object_peek_mdraid (udisks_object);
      if (udisks_raid != NULL)
        object = storage_provider_lookup_for_udisks_mdraid (provider, udisks_raid);
    }

  gs_unref_object LvmObject *lvm_object = LVM_OBJECT (g_dbus_object_manager_get_object (provider->lvm_objman,
                                                                                        udisks_or_lvm_path));
  if (lvm_object)
    {
      LvmVolumeGroup *lvm_volume_group = lvm_object_peek_volume_group (lvm_object);
      if (lvm_volume_group != NULL)
        object = storage_provider_lookup_for_lvm_volume_group (provider, lvm_volume_group);

      LvmLogicalVolume *lvm_logical_volume = lvm_object_peek_logical_volume (lvm_object);
      if (lvm_logical_volume != NULL)
        object = storage_provider_lookup_for_lvm_logical_volume (provider, lvm_logical_volume);
    }

  if (object != NULL)
    {
      // g_debug ("%s -> %s", udisks_or_lvm_path, g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
      return g_dbus_object_get_object_path (G_DBUS_OBJECT (object));
    }
  else
    {
      // g_debug ("%s -> nothing", udisks_or_lvm_path);
      return "/";
    }
}
예제 #8
0
/**
 * udisks_linux_drive_object_get_block:
 * @object: A #UDisksLinuxDriveObject.
 * @get_hw: If the drive is multipath, set to %TRUE to get a path device instead of the multipath device.
 *
 * Gets a #UDisksLinuxBlockObject representing a block device associated with @object.
 *
 * Returns: A #UDisksLinuxBlockObject or %NULL. The returned object
 * must be freed with g_object_unref().
 */
UDisksLinuxBlockObject *
udisks_linux_drive_object_get_block (UDisksLinuxDriveObject   *object,
                                     gboolean                  get_hw)
{
  GDBusObjectManagerServer *object_manager;
  UDisksLinuxBlockObject *ret;
  GList *objects;
  GList *l;

  /* TODO: actually look at @get_hw */

  ret = NULL;

  object_manager = udisks_daemon_get_object_manager (object->daemon);
  objects = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (object_manager));
  for (l = objects; l != NULL; l = l->next)
    {
      GDBusObjectSkeleton *iter_object = G_DBUS_OBJECT_SKELETON (l->data);
      UDisksBlock *block;
      UDisksLinuxDevice *device;
      gboolean is_disk;

      if (!UDISKS_IS_LINUX_BLOCK_OBJECT (iter_object))
        continue;

      device = udisks_linux_block_object_get_device (UDISKS_LINUX_BLOCK_OBJECT (iter_object));
      is_disk = (g_strcmp0 (g_udev_device_get_devtype (device->udev_device), "disk") == 0);
      g_object_unref (device);

      if (!is_disk)
        continue;

      block = udisks_object_peek_block (UDISKS_OBJECT (iter_object));
      if (g_strcmp0 (udisks_block_get_drive (block),
                     g_dbus_object_get_object_path (G_DBUS_OBJECT (object))) == 0)
        {
          ret = g_object_ref (iter_object);
          goto out;
        }
    }

 out:
  g_list_foreach (objects, (GFunc) g_object_unref, NULL);
  g_list_free (objects);
  return ret;
}
예제 #9
0
static gboolean
is_block_unlocked (GList *objects, const gchar *crypto_object_path)
{
  gboolean ret = FALSE;
  GList *l;
  for (l = objects; l != NULL; l = l->next)
    {
      UDisksObject *object = UDISKS_OBJECT (l->data);
      UDisksBlock *block;
      block = udisks_object_peek_block (object);
      if (block != NULL)
        {
          if (g_strcmp0 (udisks_block_get_crypto_backing_device (block), crypto_object_path) == 0)
            {
              ret = TRUE;
              goto out;
            }
        }
    }
 out:
  return ret;
}
예제 #10
0
static void
provider_update_block (StorageProvider *provider,
                       const gchar *path)
{
  UDisksObject *udisks_object = udisks_client_peek_object (provider->udisks_client, path);
  if (udisks_object == NULL)
    return;

  UDisksBlock *udisks_block = udisks_object_peek_block (udisks_object);
  if (udisks_block == NULL)
    return;

  StorageObject *storage_object = storage_provider_lookup_for_udisks_block (provider, udisks_block);
  if (storage_object == NULL)
    return;

  CockpitStorageBlock *storage_block = cockpit_object_peek_storage_block (COCKPIT_OBJECT (storage_object));
  if (storage_block == NULL)
    return;

  storage_block_update (STORAGE_BLOCK (storage_block));
}
예제 #11
0
static gboolean
walk_block (UDisksClient *client,
            UDisksBlock *block,
            BlockWalker *walker,
            gpointer user_data,
            GError **error)
{
  gboolean is_leaf = TRUE;

  UDisksObject *object = (UDisksObject *)g_dbus_interface_get_object (G_DBUS_INTERFACE(block));
  if (object != NULL)
    {
      // Recurse for all primary and extended partitions if this is a
      // partition table, or for all logical partitions if this is a
      // extended partition.

      UDisksPartitionTable *table;
      gboolean is_container;

      UDisksPartition *partition = udisks_object_peek_partition (object);
      if (partition && udisks_partition_get_is_container (partition))
        {
          table = udisks_client_get_partition_table (client, partition);
          is_container = TRUE;
        }
      else
        {
          table = udisks_object_peek_partition_table (object);
          is_container = FALSE;
        }

      if (table)
        {
          GList *ps, *l;
          ps = udisks_client_get_partitions (client, table);
          for (l = ps; l != NULL; l = l->next)
            {
              UDisksPartition *p = UDISKS_PARTITION (l->data);
              UDisksObject *o = (UDisksObject *) g_dbus_interface_get_object (G_DBUS_INTERFACE (p));
              UDisksBlock *b = o ? udisks_object_peek_block (o) : NULL;
              if (b && !is_container == !udisks_partition_get_is_contained (p))
                {
                  is_leaf = FALSE;
                  if (!walk_block (client, b, walker, user_data, error))
                    {
                      g_list_free_full (ps, g_object_unref);
                      return FALSE;
                    }
                }
            }
          g_list_free_full (ps, g_object_unref);
        }
    }

  gs_unref_object UDisksBlock *cleartext = udisks_client_get_cleartext_block (client, block);
  if (cleartext)
    {
      is_leaf = FALSE;
      if (!walk_block (client, cleartext, walker, user_data, error))
        return FALSE;
    }

  return walker (client, block, is_leaf, user_data, error);
}
예제 #12
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, "/");
}
예제 #13
0
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);
}
예제 #14
0
/**
 * udisks_daemon_util_setup_by_user:
 * @daemon: A #UDisksDaemon.
 * @object: The #GDBusObject that the call is on or %NULL.
 * @user: The user in question.
 *
 * Checks whether the device represented by @object (if any) has been
 * setup by @user.
 *
 * Returns: %TRUE if @object has been set-up by @user, %FALSE if not.
 */
gboolean
udisks_daemon_util_setup_by_user (UDisksDaemon *daemon,
                                  UDisksObject *object,
                                  uid_t         user)
{
  gboolean ret;
  UDisksBlock *block = NULL;
  UDisksPartition *partition = NULL;
  UDisksState *state;
  uid_t setup_by_user;
  UDisksObject *crypto_object;

  ret = FALSE;

  state = udisks_daemon_get_state (daemon);
  block = udisks_object_get_block (object);
  if (block == NULL)
    goto out;
  partition = udisks_object_get_partition (object);

  /* loop devices */
  if (udisks_state_has_loop (state, udisks_block_get_device (block), &setup_by_user))
    {
      if (setup_by_user == user)
        {
          ret = TRUE;
          goto out;
        }
    }

  /* partition of a loop device */
  if (partition != NULL)
    {
      UDisksObject *partition_object = NULL;
      partition_object = udisks_daemon_find_object (daemon, udisks_partition_get_table (partition));
      if (partition_object != NULL)
        {
          if (udisks_daemon_util_setup_by_user (daemon, partition_object, user))
            {
              ret = TRUE;
              g_object_unref (partition_object);
              goto out;
            }
          g_object_unref (partition_object);
        }
    }

  /* LUKS devices */
  crypto_object = udisks_daemon_find_object (daemon, udisks_block_get_crypto_backing_device (block));
  if (crypto_object != NULL)
    {
      UDisksBlock *crypto_block;
      crypto_block = udisks_object_peek_block (crypto_object);
      if (udisks_state_find_unlocked_luks (state,
                                           udisks_block_get_device_number (crypto_block),
                                           &setup_by_user))
        {
          if (setup_by_user == user)
            {
              ret = TRUE;
              g_object_unref (crypto_object);
              goto out;
            }
        }
      g_object_unref (crypto_object);
    }

  /* MDRaid devices */
  if (g_strcmp0 (udisks_block_get_mdraid (block), "/") != 0)
    {
      uid_t started_by_user;
      if (udisks_state_has_mdraid (state, udisks_block_get_device_number (block), &started_by_user))
        {
          if (started_by_user == user)
            {
              ret = TRUE;
              goto out;
            }
        }
    }

 out:
  g_clear_object (&partition);
  g_clear_object (&block);
  return ret;
}
예제 #15
0
/**
 * 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;
}