From ce7e4fba5766b86630499f7568d60d85857b6425 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 23 Jan 2023 16:35:01 +0000 Subject: 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. --- gusb/gusb-context.c | 15 ++++++++++++--- 1 file 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, -- cgit v1.2.1