[Libosinfo] [libosinfo PATCH 5/7] os: Add support to Firmware

Fabiano Fidêncio fidencio at redhat.com
Tue May 7 13:16:51 UTC 2019


Let's add APIs for both adding a firmware to or retrieving the list of
firmwares from an OS.

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

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 7f6dd65..c00bb35 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -584,6 +584,9 @@ LIBOSINFO_1.5.0 {
 	osinfo_install_config_get_installation_url;
 	osinfo_install_config_set_installation_url;
 
+	osinfo_os_add_firmware;
+	osinfo_os_get_firmware_list;
+
 	osinfo_product_get_short_id_list;
 
 	osinfo_tree_get_os;
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index e458a45..fb4c3f7 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -51,6 +51,7 @@ struct _OsinfoOsPrivate
     // Value: List of device_link structs
     GList *deviceLinks;
 
+    OsinfoFirmwareList *firmwares;
     OsinfoMediaList *medias;
     OsinfoTreeList *trees;
     OsinfoImageList *images;
@@ -112,6 +113,7 @@ osinfo_os_finalize(GObject *object)
     OsinfoOs *os = OSINFO_OS(object);
 
     g_list_free_full(os->priv->deviceLinks, g_object_unref);
+    g_object_unref(os->priv->firmwares);
     g_object_unref(os->priv->medias);
     g_object_unref(os->priv->trees);
     g_object_unref(os->priv->images);
@@ -180,6 +182,7 @@ osinfo_os_init(OsinfoOs *os)
     os->priv = OSINFO_OS_GET_PRIVATE(os);
 
     os->priv->deviceLinks = NULL;
+    os->priv->firmwares = osinfo_firmwarelist_new();
     os->priv->medias = osinfo_medialist_new();
     os->priv->trees = osinfo_treelist_new();
     os->priv->images = osinfo_imagelist_new();
@@ -1142,3 +1145,96 @@ void osinfo_os_add_device_driver(OsinfoOs *os, OsinfoDeviceDriver *driver)
     osinfo_list_add(OSINFO_LIST(os->priv->device_drivers),
                     OSINFO_ENTITY(driver));
 }
+
+struct GetAllFirmwaresData {
+    OsinfoFilter *filter;
+    OsinfoFilter *unsupported_filter;
+    OsinfoFirmwareList *firmwares;
+    OsinfoFirmwareList *unsupported_firmwares;
+};
+
+static void get_all_firmwares_cb(OsinfoProduct *product, gpointer user_data)
+{
+    OsinfoList *tmp_list;
+    OsinfoList *tmp_union_list;
+    OsinfoOs *os = OSINFO_OS(product);
+    gsize len, i;
+    gsize unsupported_len, j;
+    struct GetAllFirmwaresData *foreach_data = user_data;
+
+    g_return_if_fail(OSINFO_IS_OS(os));
+
+    tmp_list = osinfo_list_new_filtered(OSINFO_LIST(os->priv->firmwares),
+                                        foreach_data->unsupported_filter);
+    tmp_union_list = osinfo_list_new_union(tmp_list,
+                                           OSINFO_LIST(foreach_data->unsupported_firmwares));
+    g_object_unref(tmp_list);
+    g_object_unref(foreach_data->unsupported_firmwares);
+    foreach_data->unsupported_firmwares = OSINFO_FIRMWARELIST(tmp_union_list);
+
+    len = osinfo_list_get_length(OSINFO_LIST(os->priv->firmwares));
+    unsupported_len = osinfo_list_get_length(OSINFO_LIST(foreach_data->unsupported_firmwares));
+    for (i = 0; i < len; i++) {
+        OsinfoFirmware *firmware = OSINFO_FIRMWARE(osinfo_list_get_nth(OSINFO_LIST(os->priv->firmwares), i));
+        const gchar *arch = osinfo_firmware_get_architecture(firmware);
+        const gchar *type = osinfo_firmware_get_firmware_type(firmware);
+        gboolean add = TRUE;
+
+        if (foreach_data->filter && !osinfo_filter_matches(foreach_data->filter, OSINFO_ENTITY(firmware)))
+            add = FALSE;
+
+        if (add) {
+            for (j = 0; j < unsupported_len; j++) {
+                OsinfoFirmware *unsupported = OSINFO_FIRMWARE(osinfo_list_get_nth(OSINFO_LIST(foreach_data->unsupported_firmwares), j));
+                const gchar *unsupported_arch = osinfo_firmware_get_architecture(unsupported);
+                const gchar *unsupported_type = osinfo_firmware_get_firmware_type(unsupported);
+
+                if (g_str_equal(arch, unsupported_arch) &&
+                    g_str_equal(type, unsupported_type)) {
+                    add = FALSE;
+                    break;
+                }
+            }
+        }
+
+        if (add)
+            osinfo_list_add(OSINFO_LIST(foreach_data->firmwares), OSINFO_ENTITY(firmware));
+    }
+}
+
+OsinfoFirmwareList *osinfo_os_get_firmware_list(OsinfoOs *os, OsinfoFilter *filter)
+{
+    g_return_val_if_fail(OSINFO_IS_OS(os), NULL);
+    g_return_val_if_fail(!filter || OSINFO_IS_FILTER(filter), NULL);
+
+    struct GetAllFirmwaresData foreach_data = {
+        .filter = filter,
+        .unsupported_filter = NULL,
+        .firmwares = osinfo_firmwarelist_new(),
+        .unsupported_firmwares = osinfo_firmwarelist_new()
+    };
+
+    foreach_data.unsupported_filter = osinfo_filter_new();
+    osinfo_filter_add_constraint(foreach_data.unsupported_filter,
+                                 OSINFO_FIRMWARE_PROP_SUPPORTED,
+                                 "false");
+
+    osinfo_product_foreach_related(OSINFO_PRODUCT(os),
+                                   OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM |
+                                   OSINFO_PRODUCT_FOREACH_FLAG_CLONES,
+                                   get_all_firmwares_cb,
+                                   &foreach_data);
+
+    g_object_unref(foreach_data.unsupported_filter);
+    g_object_unref(foreach_data.unsupported_firmwares);
+
+    return foreach_data.firmwares;
+}
+
+void osinfo_os_add_firmware(OsinfoOs *os, OsinfoFirmware *firmware)
+{
+    g_return_if_fail(OSINFO_IS_OS(os));
+    g_return_if_fail(OSINFO_IS_FIRMWARE(firmware));
+
+    osinfo_list_add(OSINFO_LIST(os->priv->firmwares), OSINFO_ENTITY(firmware));
+}
diff --git a/osinfo/osinfo_os.h b/osinfo/osinfo_os.h
index 7c10475..ed9c5c9 100644
--- a/osinfo/osinfo_os.h
+++ b/osinfo/osinfo_os.h
@@ -142,4 +142,7 @@ void osinfo_os_add_install_script(OsinfoOs *os, OsinfoInstallScript *script);
 OsinfoDeviceDriverList *osinfo_os_get_device_drivers(OsinfoOs *os);
 void osinfo_os_add_device_driver(OsinfoOs *os, OsinfoDeviceDriver *driver);
 
+OsinfoFirmwareList *osinfo_os_get_firmware_list(OsinfoOs *os, OsinfoFilter *filter);
+void osinfo_os_add_firmware(OsinfoOs *os, OsinfoFirmware *firmware);
+
 #endif /* __OSINFO_OS_H__ */
-- 
2.21.0




More information about the Libosinfo mailing list