[Libosinfo] [libosinfo PATCH 1/2] image: Add get_os_variants() API

Fabiano Fidêncio fidencio at redhat.com
Thu Apr 11 07:45:42 UTC 2019


osinfo-db schema allows that, but libosinfo never really had an API to
support it.

In order to add get_os_variants(), (get,set)_os APIs were also added.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/libosinfo.syms  |  4 +++
 osinfo/osinfo_image.c  | 82 +++++++++++++++++++++++++++++++++++++++++-
 osinfo/osinfo_image.h  |  4 +++
 osinfo/osinfo_loader.c | 19 +++++++++-
 osinfo/osinfo_os.c     |  1 +
 5 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 0152eff..db8b595 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -570,6 +570,10 @@ LIBOSINFO_1.4.0 {
 
 LIBOSINFO_1.5.0 {
     global:
+	osinfo_image_get_os;
+	osinfo_image_get_os_variants;
+	osinfo_image_set_os;
+
 	osinfo_install_config_get_installation_url;
 	osinfo_install_config_set_installation_url;
 
diff --git a/osinfo/osinfo_image.c b/osinfo/osinfo_image.c
index 31eec76..fcde244 100644
--- a/osinfo/osinfo_image.c
+++ b/osinfo/osinfo_image.c
@@ -47,7 +47,7 @@ G_DEFINE_TYPE(OsinfoImage, osinfo_image, OSINFO_TYPE_ENTITY);
 
 struct _OsinfoImagePrivate
 {
-    gboolean unused;
+    GWeakRef os;
 };
 
 enum {
@@ -131,6 +131,15 @@ osinfo_image_set_property(GObject      *object,
     }
 }
 
+static void osinfo_image_dispose(GObject *obj)
+{
+    OsinfoImage *image = OSINFO_IMAGE(obj);
+
+    g_weak_ref_clear(&image->priv->os);
+
+    G_OBJECT_CLASS(osinfo_image_parent_class)->dispose(obj);
+}
+
 static void
 osinfo_image_finalize(GObject *object)
 {
@@ -145,6 +154,7 @@ osinfo_image_class_init(OsinfoImageClass *klass)
     GObjectClass *g_klass = G_OBJECT_CLASS(klass);
     GParamSpec *pspec;
 
+    g_klass->dispose = osinfo_image_dispose;
     g_klass->finalize = osinfo_image_finalize;
     g_klass->get_property = osinfo_image_get_property;
     g_klass->set_property = osinfo_image_set_property;
@@ -194,6 +204,8 @@ static void
 osinfo_image_init(OsinfoImage *image)
 {
     image->priv = OSINFO_IMAGE_GET_PRIVATE(image);
+
+    g_weak_ref_init(&image->priv->os, NULL);
 }
 
 OsinfoImage *osinfo_image_new(const gchar *id,
@@ -267,3 +279,71 @@ gboolean osinfo_image_get_cloud_init(OsinfoImage *image)
     return osinfo_entity_get_param_value_boolean_with_default
             (OSINFO_ENTITY(image), OSINFO_IMAGE_PROP_CLOUD_INIT, FALSE);
 }
+
+/**
+ * osinfo_image_get_os:
+ * @image: an #OsinfoImage instance
+ *
+ * Returns: (transfer full): the operating system, or NULL
+ */
+OsinfoOs *osinfo_image_get_os(OsinfoImage *image)
+{
+    g_return_val_if_fail(OSINFO_IS_IMAGE(image), NULL);
+
+    return g_weak_ref_get(&image->priv->os);
+}
+
+/**
+ * osinfo_image_set_os
+ * @image: an #OsinfoImage instance
+ * @os: an #OsinfoOs instance
+ *
+ * Sets the #OsinfoOs associated to the #OsinfoImage instance.
+ */
+void osinfo_image_set_os(OsinfoImage *image, OsinfoOs *os)
+{
+    g_return_if_fail(OSINFO_IS_IMAGE(image));
+
+    g_object_ref(os);
+    g_weak_ref_set(&image->priv->os, os);
+    g_object_unref(os);
+}
+
+/**
+ * osinfo_image_get_os_variants:
+ * @image: an #OsinfoImage instance
+ *
+ * Gets the varriants of the associated operating system
+ *
+ * Returns: (transfer full): the operating system variants, or NULL
+ */
+OsinfoOsVariantList *osinfo_image_get_os_variants(OsinfoImage *image)
+{
+    OsinfoOs *os;
+    OsinfoOsVariantList *os_variants;
+    OsinfoOsVariantList *image_variants;
+    GList *ids, *node;
+    OsinfoFilter *filter;
+
+    g_return_val_if_fail(OSINFO_IS_IMAGE(image), NULL);
+    os = osinfo_image_get_os(image);
+    os_variants = osinfo_os_get_variant_list(os);
+    g_object_unref(os);
+
+    ids = osinfo_entity_get_param_value_list(OSINFO_ENTITY(image),
+                                             OSINFO_IMAGE_PROP_VARIANT);
+    filter = osinfo_filter_new();
+    image_variants = osinfo_os_variantlist_new();
+    for (node = ids; node != NULL; node = node->next) {
+        osinfo_filter_clear_constraints(filter);
+        osinfo_filter_add_constraint(filter,
+                                     OSINFO_ENTITY_PROP_ID,
+                                     (const char *) node->data);
+        osinfo_list_add_filtered(OSINFO_LIST(image_variants),
+                                 OSINFO_LIST(os_variants),
+                                 filter);
+    }
+    g_object_unref(os_variants);
+
+    return image_variants;
+}
diff --git a/osinfo/osinfo_image.h b/osinfo/osinfo_image.h
index 6dff3e1..2121f0b 100644
--- a/osinfo/osinfo_image.h
+++ b/osinfo/osinfo_image.h
@@ -47,6 +47,7 @@ typedef struct _OsinfoImagePrivate OsinfoImagePrivate;
 #define OSINFO_IMAGE_PROP_FORMAT            "format"
 #define OSINFO_IMAGE_PROP_URL               "url"
 #define OSINFO_IMAGE_PROP_CLOUD_INIT        "cloud-init"
+#define OSINFO_IMAGE_PROP_VARIANT           "variant"
 
 /* object */
 struct _OsinfoImage
@@ -75,5 +76,8 @@ const gchar *osinfo_image_get_architecture(OsinfoImage *image);
 const gchar *osinfo_image_get_format(OsinfoImage *image);
 const gchar *osinfo_image_get_url(OsinfoImage *image);
 gboolean osinfo_image_get_cloud_init(OsinfoImage *image);
+OsinfoOs *osinfo_image_get_os(OsinfoImage *image);
+void osinfo_image_set_os(OsinfoImage *image, OsinfoOs *os);
+OsinfoOsVariantList *osinfo_image_get_os_variants(OsinfoImage *image);
 
 #endif /* __OSINFO_IMAGE_H__ */
diff --git a/osinfo/osinfo_loader.c b/osinfo/osinfo_loader.c
index 84c25e5..05e9d98 100644
--- a/osinfo/osinfo_loader.c
+++ b/osinfo/osinfo_loader.c
@@ -1311,7 +1311,9 @@ static OsinfoImage *osinfo_loader_image(OsinfoLoader *loader,
         { OSINFO_IMAGE_PROP_URL, G_TYPE_STRING },
         { NULL, G_TYPE_INVALID }
     };
-
+    xmlNodePtr *nodes = NULL;
+    gint nnodes;
+    guint i;
     gchar *arch = (gchar *)xmlGetProp(root, BAD_CAST "arch");
     gchar *format = (gchar *)xmlGetProp(root,
                                         BAD_CAST OSINFO_IMAGE_PROP_FORMAT);
@@ -1321,6 +1323,21 @@ static OsinfoImage *osinfo_loader_image(OsinfoLoader *loader,
     xmlFree(arch);
     xmlFree(format);
 
+    nnodes = osinfo_loader_nodeset("./variant", loader, ctxt, &nodes, err);
+    if (error_is_set(err)) {
+        g_object_unref(image);
+        return NULL;
+    }
+
+    for (i = 0; i < nnodes; i++) {
+        gchar *variant_id = (gchar *)xmlGetProp(nodes[i], BAD_CAST "id");
+        osinfo_entity_add_param(OSINFO_ENTITY(image),
+                                OSINFO_IMAGE_PROP_VARIANT,
+                                variant_id);
+        xmlFree(variant_id);
+    }
+    g_free(nodes);
+
     osinfo_loader_entity(loader, OSINFO_ENTITY(image), keys, ctxt, root, err);
     if (cloud_init) {
         osinfo_entity_set_param(OSINFO_ENTITY(image),
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 47affce..e458a45 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -708,6 +708,7 @@ void osinfo_os_add_image(OsinfoOs *os, OsinfoImage *image)
     g_return_if_fail(OSINFO_IS_IMAGE(image));
 
     osinfo_list_add(OSINFO_LIST(os->priv->images), OSINFO_ENTITY(image));
+    osinfo_image_set_os(image, os);
 }
 
 /**
-- 
2.20.1




More information about the Libosinfo mailing list