[Libosinfo] [libosinfo PATCH v2 14/15] os: Deal with "removed" devices

Fabiano Fidêncio fidencio at redhat.com
Mon Nov 12 10:31:01 UTC 2018


This commit introduces some logic to deal with "removed" devices and the
way chosen to do so was to filter out the "removed" devices from the
full list of devices (for both DeviceLinks and Device lists).

https://gitlab.com/libosinfo/osinfo-db/issues/13

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/osinfo_os.c | 79 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 2 deletions(-)

diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index b00d773..897c7e6 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -211,10 +211,18 @@ add_entity_to_list_check(OsinfoEntity *ent1, /* OsinfoDeviceLink */
                          gboolean include_removed)
 {
     gboolean ret = FALSE;
+    gboolean removed = FALSE;
 
     if (filter == NULL || osinfo_filter_matches(filter, ent2))
         ret = TRUE;
 
+    if (osinfo_entity_get_param_value_boolean_with_default
+            (ent1, OSINFO_DEVICELINK_PROP_REMOVED, FALSE))
+        removed = TRUE;
+
+    if (ret && removed && !include_removed)
+        ret = FALSE;
+
     return ret;
 }
 
@@ -285,6 +293,12 @@ static void get_all_devices_cb(OsinfoProduct *product, gpointer user_data)
 }
 
 
+static OsinfoDeviceLinkList *
+osinfo_os_get_all_device_links_internal(OsinfoOs *os,
+                                        OsinfoFilter *filter,
+                                        gboolean include_removed);
+
+
 /**
  * osinfo_os_get_all_devices:
  * @os: an operating system
@@ -302,6 +316,12 @@ OsinfoDeviceList *osinfo_os_get_all_devices(OsinfoOs *os, OsinfoFilter *filter)
         .filter = filter,
         .devices = osinfo_devicelist_new()
     };
+    OsinfoDeviceLinkList *devlinks;
+    OsinfoDeviceLinkList *removed_devlinks;
+    OsinfoDeviceList *removed_devs;
+    OsinfoDeviceList *new_list;
+    OsinfoFilter *removed_filter;
+    GList *list, *removed_list;
 
     osinfo_product_foreach_related(OSINFO_PRODUCT(os),
                                    OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM |
@@ -309,7 +329,39 @@ OsinfoDeviceList *osinfo_os_get_all_devices(OsinfoOs *os, OsinfoFilter *filter)
                                    get_all_devices_cb,
                                    &foreach_data);
 
-    return foreach_data.devices;
+    devlinks = osinfo_os_get_all_device_links_internal(os, filter, TRUE);
+
+    removed_filter = osinfo_filter_new();
+    osinfo_filter_add_constraint(removed_filter,
+                                 OSINFO_DEVICELINK_PROP_REMOVED,
+                                 "true");
+
+    removed_devlinks = OSINFO_DEVICELINKLIST
+        (osinfo_list_new_filtered(OSINFO_LIST(devlinks), removed_filter));
+
+    removed_devs = osinfo_devicelinklist_get_devices(removed_devlinks, NULL);
+
+    list = osinfo_list_get_elements(OSINFO_LIST(foreach_data.devices));
+    removed_list = osinfo_list_get_elements(OSINFO_LIST(removed_devs));
+
+    new_list = osinfo_devicelist_new();
+    for (GList *it = list; it != NULL; it = it->next) {
+        OsinfoDevice *dev = OSINFO_DEVICE(it->data);
+        if (g_list_find(removed_list, dev))
+            continue;
+
+        osinfo_list_add(OSINFO_LIST(new_list), OSINFO_ENTITY(dev));
+    }
+
+    g_list_free(list);
+    g_list_free(removed_list);
+    g_object_unref(devlinks);
+    g_object_unref(removed_devlinks);
+    g_object_unref(removed_devs);
+    g_object_unref(removed_filter);
+    g_object_unref(foreach_data.devices);
+
+    return new_list;
 }
 
 /**
@@ -422,6 +474,8 @@ osinfo_os_get_all_device_links_internal(OsinfoOs *os,
         .filter = filter,
         .device_links = osinfo_devicelinklist_new()
     };
+    OsinfoDeviceLinkList *devlinks;
+    GList *list;
 
     osinfo_product_foreach_related(OSINFO_PRODUCT(os),
                                    OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM |
@@ -429,7 +483,28 @@ osinfo_os_get_all_device_links_internal(OsinfoOs *os,
                                    get_all_device_links_cb,
                                    &foreach_data);
 
-    return foreach_data.device_links;
+    if (include_removed)
+        return foreach_data.device_links;
+
+    devlinks = osinfo_devicelinklist_new();
+
+    list = osinfo_list_get_elements(OSINFO_LIST(foreach_data.device_links));
+    for (GList *it = list; it != NULL; it = it->next) {
+        OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(it->data);
+
+        if (osinfo_entity_get_param_value_boolean_with_default
+                (OSINFO_ENTITY(devlink),
+                 OSINFO_DEVICELINK_PROP_REMOVED,
+                 FALSE))
+            continue;
+
+        osinfo_list_add(OSINFO_LIST(devlinks), OSINFO_ENTITY(devlink));
+    }
+
+    g_object_unref(foreach_data.device_links);
+    g_list_free(list);
+
+    return devlinks;
 }
 
 /**
-- 
2.19.1




More information about the Libosinfo mailing list