diff options
author | Richard Hughes <richard@hughsie.com> | 2023-01-23 16:35:01 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2023-01-24 14:53:24 +0000 |
commit | ce7e4fba5766b86630499f7568d60d85857b6425 (patch) | |
tree | ef691967f4b93446553edddd9b7fa78d0519890b | |
parent | 7b2cd2fbded8456dc5ee6021a5e74e24f325fe2f (diff) | |
download | gusb-ce7e4fba5766b86630499f7568d60d85857b6425.tar.gz |
Emit remove devices with matching tags when loading contexts
If a program, say fwupd, has references to existing devices it is probably not
expected for the physical backing device to just vanish.
-rw-r--r-- | gusb/gusb-context.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c index f6a52c8..49e0384 100644 --- a/gusb/gusb-context.c +++ b/gusb/gusb-context.c @@ -401,14 +401,23 @@ g_usb_context_load_with_tag(GUsbContext *self, { GUsbContextPrivate *priv = GET_PRIVATE(self); JsonArray *json_array; + g_autoptr(GPtrArray) devices_to_remove = g_ptr_array_new_with_free_func((GDestroyNotify) g_object_unref); g_return_val_if_fail(G_USB_IS_CONTEXT(self), FALSE); g_return_val_if_fail(json_object != NULL, FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - /* remove existing devices */ - g_ptr_array_set_size(priv->devices, 0); - g_ptr_array_set_size(priv->devices_removed, 0); + /* remove existing devices with this tag: two step */ + for (guint i = 0; i < priv->devices->len; i++) { + GUsbDevice *device = g_ptr_array_index(priv->devices, i); + if (tag == NULL || g_usb_device_has_tag(device, tag)) + g_ptr_array_add(devices_to_remove, g_object_ref(device)); + } + for (guint i = 0; i < devices_to_remove->len; i++) { + GUsbDevice *device = g_ptr_array_index(devices_to_remove, i); + g_usb_context_emit_device_remove(self, device); + g_ptr_array_remove(priv->devices, device); + } if (!json_object_has_member(json_object, "UsbDevices")) { g_set_error_literal(error, |