[Libosinfo] [libosinfo PATCH 08/18] os: Add osinfo_os_get_all_device_links()

Fabiano Fidêncio fidencio at redhat.com
Thu Nov 8 06:55:27 UTC 2018


This function is a helper for the commits that are about to come related
to properly dealing with "deprecated" devices.

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

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/libosinfo.syms |  4 ++
 osinfo/osinfo_os.c    | 93 +++++++++++++++++++++++++++++++++++++++++++
 osinfo/osinfo_os.h    |  2 +
 3 files changed, 99 insertions(+)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index db9b8b2..1c53f71 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -532,6 +532,10 @@ LIBOSINFO_0.2.13 {
 LIBOSINFO_1.3.0 {
     global:
 	osinfo_error_quark;
+
+	osinfo_os_get_all_device_links;
+
+	osinfo_os_is_device_deprecated;
 } LIBOSINFO_0.2.13;
 
 /* Symbols in next release...
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 4f74331..d955dd2 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -351,6 +351,99 @@ OsinfoDeviceLinkList *osinfo_os_get_device_links(OsinfoOs *os, OsinfoFilter *fil
 }
 
 
+struct GetAllDeviceLinksData {
+    OsinfoFilter *filter;
+    OsinfoDeviceLinkList *device_links;
+};
+
+static void get_all_device_links_cb(OsinfoProduct *product, gpointer user_data)
+{
+    OsinfoDeviceLinkList *device_links;
+    OsinfoList *tmp_list;
+    struct GetAllDeviceLinksData *foreach_data;
+
+    g_return_if_fail(OSINFO_IS_OS(product));
+
+    foreach_data = (struct GetAllDeviceLinksData *)user_data;
+    device_links = osinfo_os_get_device_links(OSINFO_OS(product),
+                                              foreach_data->filter);
+    tmp_list = osinfo_list_new_union(OSINFO_LIST(foreach_data->device_links),
+                                     OSINFO_LIST(device_links));
+    g_object_unref(foreach_data->device_links);
+    g_object_unref(device_links);
+    foreach_data->device_links = OSINFO_DEVICELINKLIST(tmp_list);
+}
+
+/**
+ * osinfo_os_get_all_device_links:
+ * @os: an operating system
+ * @filter: (allow-none)(transfer none): an optional device property filter
+ *
+ * Get all devicelinks matching a given filter but unlike
+ * osinfo_os_get_device_links this function also retrieves devices from all
+ * derived and cloned operating systems.
+ *
+ * Returns: (transfer full): A list of OsinfoDeviceLink
+ */
+OsinfoDeviceLinkList *osinfo_os_get_all_device_links(OsinfoOs *os, OsinfoFilter *filter)
+{
+    struct GetAllDeviceLinksData foreach_data = {
+        .filter = filter,
+        .device_links = osinfo_devicelinklist_new()
+    };
+
+    osinfo_product_foreach_related(OSINFO_PRODUCT(os),
+                                   OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM |
+                                   OSINFO_PRODUCT_FOREACH_FLAG_CLONES,
+                                   get_all_device_links_cb,
+                                   &foreach_data);
+
+    return foreach_data.device_links;
+}
+
+
+/**
+ * osinfo_os_is_device_deprecated:
+ * @os: an operating system
+ * @device: the device to be checked
+ *
+ * Whether the @device is deprecated or not.
+ *
+ * Returns: #TRUE if @device is deprecated, #FALSE otherwise
+ */
+gboolean
+osinfo_os_is_device_deprecated(OsinfoOs *os, OsinfoDevice *device)
+{
+    OsinfoDeviceLinkList *devlinks, *deprecated_devlinks;
+    OsinfoDeviceList *devs;
+    OsinfoFilter *filter;
+    GList *list = NULL;
+    gboolean ret;
+
+    g_return_val_if_fail(OSINFO_IS_OS(os), FALSE);
+    g_return_val_if_fail(OSINFO_IS_DEVICE(device), FALSE);
+
+    devlinks = osinfo_os_get_all_device_links(os, NULL);
+
+    filter = osinfo_filter_new();
+    osinfo_filter_add_constraint(filter, OSINFO_DEVICELINK_PROP_DEPRECATED, "true");
+    deprecated_devlinks = OSINFO_DEVICELINKLIST
+        (osinfo_list_new_filtered(OSINFO_LIST(devlinks), filter));
+
+    devs = osinfo_devicelinklist_get_devices(deprecated_devlinks, NULL);
+    list = osinfo_list_get_elements(OSINFO_LIST(devs));
+
+    ret = g_list_find(list, device) != NULL;
+
+    g_object_unref(devlinks);
+    g_object_unref(deprecated_devlinks);
+    g_object_unref(filter);
+    g_list_free(list);
+
+    return ret;
+}
+
+
 /**
  * osinfo_os_add_device:
  * @os: an operating system
diff --git a/osinfo/osinfo_os.h b/osinfo/osinfo_os.h
index 96bd6e3..2493851 100644
--- a/osinfo/osinfo_os.h
+++ b/osinfo/osinfo_os.h
@@ -107,6 +107,8 @@ OsinfoDeviceList *osinfo_os_get_devices_by_property(OsinfoOs *os,
                                                     const char *value,
                                                     gboolean inherited);
 OsinfoDeviceLinkList *osinfo_os_get_device_links(OsinfoOs *os, OsinfoFilter *filter);
+OsinfoDeviceLinkList *osinfo_os_get_all_device_links(OsinfoOs *os, OsinfoFilter *filter);
+gboolean osinfo_os_is_device_deprecated(OsinfoOs *os, OsinfoDevice *device);
 
 OsinfoDeviceLink *osinfo_os_add_device(OsinfoOs *os, OsinfoDevice *dev);
 const gchar *osinfo_os_get_family(OsinfoOs *os);
-- 
2.19.1




More information about the Libosinfo mailing list