[Libosinfo] [libosinfo PATCH 6/6] db: Introduce osinfo_db_identify_image()

Fabiano Fidêncio fidencio at redhat.com
Wed Oct 31 21:45:38 UTC 2018


osinfo_db_identify_image() is a helper written in a similar way than
osinfo_db_identify_media() and provides a way for libosinfo consumers
to, as stated by the new function name, identify the image provided.

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

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

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 9f37411..734ea30 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -531,6 +531,8 @@ LIBOSINFO_0.2.13 {
 
 LIBOSINFO_1.3.0 {
     global:
+	osinfo_db_identify_image;
+
 	osinfo_error_quark;
 
 	osinfo_image_create_from_location;
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index fa14c6d..23192e5 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -26,6 +26,7 @@
 
 #include <osinfo/osinfo.h>
 #include "osinfo_media_private.h"
+#include "osinfo_image_private.h"
 #include <gio/gio.h>
 #include <string.h>
 #include <glib/gi18n-lib.h>
@@ -715,6 +716,116 @@ gboolean osinfo_db_identify_media(OsinfoDb *db, OsinfoMedia *media)
     return TRUE;
 }
 
+static void fill_image(OsinfoDb *db,
+                       OsinfoImage *image,
+                       OsinfoImage *matched_image,
+                       OsinfoOs *os)
+{
+    const gchar *id;
+    const gchar *arch;
+    const gchar *url;
+
+    id = osinfo_entity_get_id(OSINFO_ENTITY(matched_image));
+    g_object_set(G_OBJECT(image), "id", id, NULL);
+
+    arch = osinfo_image_get_architecture(matched_image);
+    if (arch != NULL)
+        g_object_set(G_OBJECT(image), "architecture", arch, NULL);
+
+    url = osinfo_image_get_url(matched_image);
+    if (url != NULL)
+        g_object_set(G_OBJECT(image), "url", url, NULL);
+
+    if (os != NULL)
+        osinfo_image_set_os(image, os);
+}
+
+static OsinfoOs *
+osinfo_db_guess_os_from_image_internal(OsinfoDb *db,
+                                       OsinfoImage *image,
+                                       OsinfoImage **matched_image)
+{
+    OsinfoOs *ret = NULL;
+    GList *oses = NULL;
+    GList *os_iter;
+    const gchar *image_osinfo;
+    const gchar *image_product_name;
+
+    g_return_val_if_fail(OSINFO_IS_DB(db), NULL);
+    g_return_val_if_fail(image != NULL, NULL);
+
+    image_osinfo = osinfo_image_get_osinfo(image);
+    image_product_name = osinfo_image_get_product_name(image);
+
+    oses = osinfo_list_get_elements(OSINFO_LIST(db->priv->oses));
+
+    for (os_iter = oses; os_iter != NULL; os_iter = os_iter->next) {
+        OsinfoOs *os = OSINFO_OS(os_iter->data);
+
+        if (!g_str_equal(image_osinfo,
+                         osinfo_product_get_short_id(OSINFO_PRODUCT(os))))
+            continue;
+
+        OsinfoImageList *image_list = osinfo_os_get_image_list(os);
+        GList *images = osinfo_list_get_elements(OSINFO_LIST(image_list));
+        GList *image_iter;
+
+        for (image_iter = images; image_iter != NULL; image_iter = image_iter->next) {
+            OsinfoImage *os_image = OSINFO_IMAGE(image_iter->data);
+            const gchar *os_image_product_name = osinfo_image_get_product_name(os_image);
+
+            if (g_str_equal(image_product_name, os_image_product_name)) {
+                ret = os;
+                if (matched_image != NULL)
+                    *matched_image = os_image;
+                break;
+            }
+        }
+
+        g_list_free(images);
+        g_object_unref(image_list);
+
+        if (ret != NULL)
+            break;
+    }
+
+    g_list_free(oses);
+
+    return ret;
+}
+
+/**
+ * osinfo_db_identify_image:
+ * @db: an #OsinfoDb database
+ * @image: the installation image
+ * data
+ *
+ * Try to match a newly created @image with a image description from @db.
+ * If found, @image will be filled with the corresponding information
+ * stored in @db. In particular, after a call to osinfo_db_identify_image(), if
+ * the image could be identified, its OsinfoEntify::id and OsinfoImage::os
+ * properties will be set.
+ *
+ * Returns: TRUE if @image was found in @db, FALSE otherwise
+ */
+gboolean osinfo_db_identify_image(OsinfoDb *db, OsinfoImage *image)
+{
+    OsinfoImage *matched_image = NULL;
+    OsinfoOs *matched_os;
+
+    g_return_val_if_fail(OSINFO_IS_IMAGE(image), FALSE);
+    g_return_val_if_fail(OSINFO_IS_DB(db), FALSE);
+
+    matched_os = osinfo_db_guess_os_from_image_internal(db, image,
+                                                        &matched_image);
+    if (matched_os == NULL) {
+        return FALSE;
+    }
+
+    fill_image(db, image, matched_image, matched_os);
+
+    return TRUE;
+}
 
 /**
  * osinfo_db_guess_os_from_tree:
diff --git a/osinfo/osinfo_db.h b/osinfo/osinfo_db.h
index f2df973..e62388f 100644
--- a/osinfo/osinfo_db.h
+++ b/osinfo/osinfo_db.h
@@ -111,6 +111,9 @@ OsinfoOs *osinfo_db_guess_os_from_tree(OsinfoDb *db,
                                        OsinfoTree *tree,
                                        OsinfoTree **matched_tree);
 
+gboolean osinfo_db_identify_image(OsinfoDb *db,
+                                  OsinfoImage *image);
+
 // Get me all unique values for property "vendor" among operating systems
 GList *osinfo_db_unique_values_for_property_in_os(OsinfoDb *db, const gchar *propName);
 
-- 
2.19.1




More information about the Libosinfo mailing list