mirror of
https://git.proxmox.com/git/fwupd
synced 2025-07-27 12:54:58 +00:00
Fix get-updates and activate to use GUID in addition to device-id
Also merge fu_engine_get_device and fu_engine_get_device_by_id
This commit is contained in:
parent
0f89a0d2f0
commit
f8fb2dc436
@ -2399,19 +2399,17 @@ fu_engine_get_plugins (FuEngine *self)
|
||||
}
|
||||
|
||||
/**
|
||||
* fu_engine_get_device_by_id:
|
||||
* fu_engine_get_device:
|
||||
* @self: A #FuEngine
|
||||
* @device_id: A string
|
||||
* @error: A #GError or NULL
|
||||
* @device_id: A device ID
|
||||
* @error: A #GError, or %NULL
|
||||
*
|
||||
* Returns the device from the engine or NULL if not found
|
||||
* Gets a specific device.
|
||||
*
|
||||
* Returns: (transfer full): a #FuDevice
|
||||
*
|
||||
* Since: 1.5.0
|
||||
* Returns: (transfer full): a device, or %NULL if not found
|
||||
**/
|
||||
FuDevice *
|
||||
fu_engine_get_device_by_id (FuEngine *self, const gchar *device_id, GError **error)
|
||||
fu_engine_get_device (FuEngine *self, const gchar *device_id, GError **error)
|
||||
{
|
||||
g_autoptr(FuDevice) device1 = NULL;
|
||||
g_autoptr(FuDevice) device2 = NULL;
|
||||
@ -2498,7 +2496,7 @@ fu_engine_update_prepare (FuEngine *self,
|
||||
g_autoptr(FuDevice) device = NULL;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
|
||||
@ -2536,7 +2534,7 @@ fu_engine_update_cleanup (FuEngine *self,
|
||||
g_autoptr(FuDevice) device = NULL;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
str = fu_device_to_string (device);
|
||||
@ -2567,7 +2565,7 @@ fu_engine_update_detach (FuEngine *self, const gchar *device_id, GError **error)
|
||||
g_autoptr(FuDevice) device = NULL;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
str = fu_device_to_string (device);
|
||||
@ -2590,7 +2588,7 @@ fu_engine_update_attach (FuEngine *self, const gchar *device_id, GError **error)
|
||||
g_autoptr(FuDevice) device = NULL;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL) {
|
||||
g_prefix_error (error, "failed to get device after update: ");
|
||||
return FALSE;
|
||||
@ -2649,7 +2647,7 @@ fu_engine_update_reload (FuEngine *self, const gchar *device_id, GError **error)
|
||||
g_autoptr(FuDevice) device = NULL;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL) {
|
||||
g_prefix_error (error, "failed to get device after update: ");
|
||||
return FALSE;
|
||||
@ -2691,7 +2689,7 @@ fu_engine_update (FuEngine *self,
|
||||
return FALSE;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device = fu_engine_get_device (self, device_id, error);
|
||||
if (device == NULL) {
|
||||
g_prefix_error (error, "failed to get device after detach: ");
|
||||
return FALSE;
|
||||
@ -2838,7 +2836,7 @@ fu_engine_install_blob (FuEngine *self,
|
||||
return FALSE;
|
||||
|
||||
/* the device and plugin both may have changed */
|
||||
device_tmp = fu_engine_get_device_by_id (self, device_id, error);
|
||||
device_tmp = fu_engine_get_device (self, device_id, error);
|
||||
if (device_tmp == NULL)
|
||||
return FALSE;
|
||||
if (!fu_device_has_flag (device_tmp, FWUPD_DEVICE_FLAG_ANOTHER_WRITE_REQUIRED))
|
||||
@ -3964,27 +3962,6 @@ fu_engine_get_devices (FuEngine *self, GError **error)
|
||||
return g_steal_pointer (&devices);
|
||||
}
|
||||
|
||||
/**
|
||||
* fu_engine_get_device:
|
||||
* @self: A #FuEngine
|
||||
* @device_id: A device ID
|
||||
* @error: A #GError, or %NULL
|
||||
*
|
||||
* Gets a specific device.
|
||||
*
|
||||
* Returns: (transfer full): a device, or %NULL if not found
|
||||
**/
|
||||
FuDevice *
|
||||
fu_engine_get_device (FuEngine *self, const gchar *device_id, GError **error)
|
||||
{
|
||||
FuDevice *device;
|
||||
|
||||
device = fu_device_list_get_by_id (self->device_list, device_id, error);
|
||||
if (device == NULL)
|
||||
return NULL;
|
||||
return device;
|
||||
}
|
||||
|
||||
/**
|
||||
* fu_engine_get_devices_by_guid:
|
||||
* @self: A #FuEngine
|
||||
|
@ -58,9 +58,6 @@ XbSilo *fu_engine_get_silo_from_blob (FuEngine *self,
|
||||
GError **error);
|
||||
guint64 fu_engine_get_archive_size_max (FuEngine *self);
|
||||
GPtrArray *fu_engine_get_plugins (FuEngine *self);
|
||||
FuDevice *fu_engine_get_device_by_id (FuEngine *self,
|
||||
const gchar *device_id,
|
||||
GError **error);
|
||||
GPtrArray *fu_engine_get_devices (FuEngine *self,
|
||||
GError **error);
|
||||
FuDevice *fu_engine_get_device (FuEngine *self,
|
||||
|
186
src/fu-tool.c
186
src/fu-tool.c
@ -314,6 +314,97 @@ fu_util_get_tree_title (FuUtilPrivate *priv)
|
||||
return g_strdup (fu_engine_get_host_product (priv->engine));
|
||||
}
|
||||
|
||||
static FuDevice *
|
||||
fu_util_prompt_for_device (FuUtilPrivate *priv, GPtrArray *devices_opt, GError **error)
|
||||
{
|
||||
FuDevice *dev;
|
||||
guint idx;
|
||||
g_autoptr(GPtrArray) devices = NULL;
|
||||
g_autoptr(GPtrArray) devices_filtered = NULL;
|
||||
|
||||
/* get devices from daemon */
|
||||
if (devices_opt != NULL) {
|
||||
devices = g_ptr_array_ref (devices_opt);
|
||||
} else {
|
||||
devices = fu_engine_get_devices (priv->engine, error);
|
||||
if (devices == NULL)
|
||||
return NULL;
|
||||
}
|
||||
fwupd_device_array_ensure_parents (devices);
|
||||
|
||||
/* filter results */
|
||||
devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
for (guint i = 0; i < devices->len; i++) {
|
||||
dev = g_ptr_array_index (devices, i);
|
||||
if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev)))
|
||||
continue;
|
||||
g_ptr_array_add (devices_filtered, g_object_ref (dev));
|
||||
}
|
||||
|
||||
/* nothing */
|
||||
if (devices_filtered->len == 0) {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_NOTHING_TO_DO,
|
||||
"No supported devices");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* exactly one */
|
||||
if (devices_filtered->len == 1) {
|
||||
dev = g_ptr_array_index (devices_filtered, 0);
|
||||
/* TRANSLATORS: Device has been chosen by the daemon for the user */
|
||||
g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev));
|
||||
return g_object_ref (dev);
|
||||
}
|
||||
|
||||
/* TRANSLATORS: get interactive prompt */
|
||||
g_print ("%s\n", _("Choose a device:"));
|
||||
/* TRANSLATORS: this is to abort the interactive prompt */
|
||||
g_print ("0.\t%s\n", _("Cancel"));
|
||||
for (guint i = 0; i < devices_filtered->len; i++) {
|
||||
dev = g_ptr_array_index (devices_filtered, i);
|
||||
g_print ("%u.\t%s (%s)\n",
|
||||
i + 1,
|
||||
fu_device_get_id (dev),
|
||||
fu_device_get_name (dev));
|
||||
}
|
||||
idx = fu_util_prompt_for_number (devices_filtered->len);
|
||||
if (idx == 0) {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_NOTHING_TO_DO,
|
||||
"Request canceled");
|
||||
return NULL;
|
||||
}
|
||||
dev = g_ptr_array_index (devices_filtered, idx - 1);
|
||||
return g_object_ref (dev);
|
||||
}
|
||||
|
||||
static FuDevice *
|
||||
fu_util_get_device (FuUtilPrivate *priv, const gchar *id, GError **error)
|
||||
{
|
||||
if (fwupd_guid_is_valid (id)) {
|
||||
g_autoptr(GPtrArray) devices = NULL;
|
||||
devices = fu_engine_get_devices_by_guid (priv->engine, id, error);
|
||||
if (devices == NULL)
|
||||
return NULL;
|
||||
return fu_util_prompt_for_device (priv, devices, error);
|
||||
}
|
||||
|
||||
/* did this look like a GUID? */
|
||||
for (guint i = 0; id[i] != '\0'; i++) {
|
||||
if (id[i] == '-') {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_ARGS,
|
||||
"Invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return fu_engine_get_device (priv->engine, id, error);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
{
|
||||
@ -335,7 +426,7 @@ fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
return FALSE;
|
||||
} else if (g_strv_length (values) == 1) {
|
||||
FuDevice *device;
|
||||
device = fu_engine_get_device_by_id (priv->engine, values[0], error);
|
||||
device = fu_util_get_device (priv, values[0], error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
@ -530,73 +621,6 @@ fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
return fu_util_save_current_state (priv, error);
|
||||
}
|
||||
|
||||
static FuDevice *
|
||||
fu_util_prompt_for_device (FuUtilPrivate *priv, GPtrArray *devices_opt, GError **error)
|
||||
{
|
||||
FuDevice *dev;
|
||||
guint idx;
|
||||
g_autoptr(GPtrArray) devices = NULL;
|
||||
g_autoptr(GPtrArray) devices_filtered = NULL;
|
||||
|
||||
/* get devices from daemon */
|
||||
if (devices_opt != NULL) {
|
||||
devices = g_ptr_array_ref (devices_opt);
|
||||
} else {
|
||||
devices = fu_engine_get_devices (priv->engine, error);
|
||||
if (devices == NULL)
|
||||
return NULL;
|
||||
}
|
||||
fwupd_device_array_ensure_parents (devices);
|
||||
|
||||
/* filter results */
|
||||
devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
for (guint i = 0; i < devices->len; i++) {
|
||||
dev = g_ptr_array_index (devices, i);
|
||||
if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev)))
|
||||
continue;
|
||||
g_ptr_array_add (devices_filtered, g_object_ref (dev));
|
||||
}
|
||||
|
||||
/* nothing */
|
||||
if (devices_filtered->len == 0) {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_NOTHING_TO_DO,
|
||||
"No supported devices");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* exactly one */
|
||||
if (devices_filtered->len == 1) {
|
||||
dev = g_ptr_array_index (devices_filtered, 0);
|
||||
/* TRANSLATORS: Device has been chosen by the daemon for the user */
|
||||
g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev));
|
||||
return g_object_ref (dev);
|
||||
}
|
||||
|
||||
/* TRANSLATORS: get interactive prompt */
|
||||
g_print ("%s\n", _("Choose a device:"));
|
||||
/* TRANSLATORS: this is to abort the interactive prompt */
|
||||
g_print ("0.\t%s\n", _("Cancel"));
|
||||
for (guint i = 0; i < devices_filtered->len; i++) {
|
||||
dev = g_ptr_array_index (devices_filtered, i);
|
||||
g_print ("%u.\t%s (%s)\n",
|
||||
i + 1,
|
||||
fu_device_get_id (dev),
|
||||
fu_device_get_name (dev));
|
||||
}
|
||||
idx = fu_util_prompt_for_number (devices_filtered->len);
|
||||
if (idx == 0) {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_NOTHING_TO_DO,
|
||||
"Request canceled");
|
||||
return NULL;
|
||||
}
|
||||
dev = g_ptr_array_index (devices_filtered, idx - 1);
|
||||
return g_object_ref (dev);
|
||||
}
|
||||
|
||||
static void
|
||||
fu_util_update_device_changed_cb (FwupdClient *client,
|
||||
FwupdDevice *device,
|
||||
@ -661,30 +685,6 @@ fu_util_display_current_message (FuUtilPrivate *priv)
|
||||
g_clear_pointer (&priv->current_message, g_free);
|
||||
}
|
||||
|
||||
static FuDevice *
|
||||
fu_util_get_device (FuUtilPrivate *priv, const gchar *id, GError **error)
|
||||
{
|
||||
if (fwupd_guid_is_valid (id)) {
|
||||
g_autoptr(GPtrArray) devices = NULL;
|
||||
devices = fu_engine_get_devices_by_guid (priv->engine, id, error);
|
||||
if (devices == NULL)
|
||||
return NULL;
|
||||
return fu_util_prompt_for_device (priv, devices, error);
|
||||
}
|
||||
|
||||
/* did this look like a GUID? */
|
||||
for (guint i = 0; id[i] != '\0'; i++) {
|
||||
if (id[i] == '-') {
|
||||
g_set_error_literal (error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_ARGS,
|
||||
"Invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return fu_engine_get_device (priv->engine, id, error);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
{
|
||||
@ -1456,7 +1456,7 @@ fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
return FALSE;
|
||||
} else if (g_strv_length (values) == 1) {
|
||||
FuDevice *device;
|
||||
device = fu_engine_get_device_by_id (priv->engine, values[0], error);
|
||||
device = fu_util_get_device (priv, values[0], error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
|
@ -1383,10 +1383,7 @@ fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
if (devices == NULL)
|
||||
return FALSE;
|
||||
} else if (g_strv_length (values) == 1) {
|
||||
FwupdDevice *device = fwupd_client_get_device_by_id (priv->client,
|
||||
values[0],
|
||||
NULL,
|
||||
error);
|
||||
FwupdDevice *device = fu_util_get_device_by_id (priv, values[0], error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
@ -1891,10 +1888,7 @@ fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
|
||||
}
|
||||
}
|
||||
} else if (g_strv_length (values) == 1) {
|
||||
FwupdDevice *device = fwupd_client_get_device_by_id (priv->client,
|
||||
values[0],
|
||||
NULL,
|
||||
error);
|
||||
FwupdDevice *device = fu_util_get_device_by_id (priv, values[0], error);
|
||||
if (device == NULL)
|
||||
return FALSE;
|
||||
devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
|
||||
|
Loading…
Reference in New Issue
Block a user