[Libosinfo] [libosinfo PATCH v3 03/10] Introduce OsinfoImage object

Fabiano Fidêncio fidencio at redhat.com
Wed Nov 14 14:49:44 UTC 2018


OsinfoImage object has been created to represent pre-installed images
distributed by the OSes.

We've decided to go for the simplespet approach possible that's just
exposing the Image URL for an existing OS and then Apps that want to
deal with the Image itself can just use libguestfs for that as they
already do a quite good job on this field.

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

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/Makefile.am    |   2 +
 osinfo/libosinfo.syms |   7 ++
 osinfo/osinfo.h       |   1 +
 osinfo/osinfo_image.c | 268 ++++++++++++++++++++++++++++++++++++++++++
 osinfo/osinfo_image.h |  91 ++++++++++++++
 po/POTFILES.in        |   1 +
 6 files changed, 370 insertions(+)
 create mode 100644 osinfo/osinfo_image.c
 create mode 100644 osinfo/osinfo_image.h

diff --git a/osinfo/Makefile.am b/osinfo/Makefile.am
index 86f6344..800770e 100644
--- a/osinfo/Makefile.am
+++ b/osinfo/Makefile.am
@@ -98,6 +98,7 @@ libosinfo_1_0_include_HEADERS =		\
   osinfo_resourceslist.h		\
   osinfo_tree.h				\
   osinfo_treelist.h			\
+  osinfo_image.h			\
   $(NULL)
 
 nodist_libosinfo_1_0_include_HEADERS =	\
@@ -141,6 +142,7 @@ libosinfo_c_files =		\
   osinfo_resourceslist.c		\
   osinfo_tree.c				\
   osinfo_treelist.c			\
+  osinfo_image.c			\
   osinfo_db.c				\
   osinfo_loader.c			\
   $(NULL)
diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index db9b8b2..dee8c6b 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -532,6 +532,13 @@ LIBOSINFO_0.2.13 {
 LIBOSINFO_1.3.0 {
     global:
 	osinfo_error_quark;
+
+	osinfo_image_error_quark;
+	osinfo_image_get_architecture;
+	osinfo_image_get_format;
+	osinfo_image_get_type;
+	osinfo_image_get_url;
+	osinfo_image_new;
 } LIBOSINFO_0.2.13;
 
 /* Symbols in next release...
diff --git a/osinfo/osinfo.h b/osinfo/osinfo.h
index 8d0a595..9e8e604 100644
--- a/osinfo/osinfo.h
+++ b/osinfo/osinfo.h
@@ -65,6 +65,7 @@
 #include <osinfo/osinfo_loader.h>
 #include <osinfo/osinfo_os_variant.h>
 #include <osinfo/osinfo_os_variantlist.h>
+#include <osinfo/osinfo_image.h>
 #include <osinfo/osinfo_version.h>
 
 #endif
diff --git a/osinfo/osinfo_image.c b/osinfo/osinfo_image.c
new file mode 100644
index 0000000..234b328
--- /dev/null
+++ b/osinfo/osinfo_image.c
@@ -0,0 +1,268 @@
+/*
+ * libosinfo: An installed image of a (guest) OS
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *  Fabiano Fidêncio <fidencio at redhat.com>
+ */
+
+#include <config.h>
+
+#include <osinfo/osinfo.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib/gi18n-lib.h>
+
+GQuark
+osinfo_image_error_quark(void)
+{
+    static GQuark quark = 0;
+
+    if (!quark)
+        quark = g_quark_from_static_string("osinfo-image-error");
+
+    return quark;
+}
+
+G_DEFINE_TYPE(OsinfoImage, osinfo_image, OSINFO_TYPE_ENTITY);
+
+#define OSINFO_IMAGE_GET_PRIVATE(obj)                    \
+    (G_TYPE_INSTANCE_GET_PRIVATE((obj),                \
+                                  OSINFO_TYPE_IMAGE,     \
+                                  OsinfoImagePrivate))
+
+/**
+ * SECTION:osinfo_image
+ * @short_description: An installation image for a (guest) OS
+ * @see_also: #OsinfoOs
+ *
+ * #OsinfoImage is an entity representing an installation image
+ * a (guest) operating system.
+ */
+
+struct _OsinfoImagePrivate
+{
+    gboolean unused;
+};
+
+enum {
+    PROP_0,
+
+    PROP_ARCHITECTURE,
+    PROP_FORMAT,
+    PROP_URL,
+};
+
+static void
+osinfo_image_get_property(GObject *object,
+                         guint property_id,
+                         GValue *value,
+                         GParamSpec *pspec)
+{
+    OsinfoImage *image = OSINFO_IMAGE(object);
+
+    switch (property_id) {
+    case PROP_ARCHITECTURE:
+        g_value_set_string(value, osinfo_image_get_architecture(image));
+        break;
+
+    case PROP_FORMAT:
+        g_value_set_string(value, osinfo_image_get_format(image));
+        break;
+
+    case PROP_URL:
+        g_value_set_string(value, osinfo_image_get_url(image));
+        break;
+
+    default:
+        /* We don't have any other property... */
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+        break;
+    }
+}
+
+static void
+osinfo_image_set_property(GObject      *object,
+                         guint         property_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+    OsinfoImage *image = OSINFO_IMAGE(object);
+
+    switch (property_id) {
+    case PROP_ARCHITECTURE:
+        osinfo_entity_set_param(OSINFO_ENTITY(image),
+                                OSINFO_IMAGE_PROP_ARCHITECTURE,
+                                g_value_get_string(value));
+        break;
+
+    case PROP_FORMAT:
+        osinfo_entity_set_param(OSINFO_ENTITY(image),
+                                OSINFO_IMAGE_PROP_FORMAT,
+                                g_value_get_string(value));
+        break;
+
+    case PROP_URL:
+        osinfo_entity_set_param(OSINFO_ENTITY(image),
+                                OSINFO_IMAGE_PROP_URL,
+                                g_value_get_string(value));
+        break;
+
+    default:
+        /* We don't have any other property... */
+        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+        break;
+    }
+}
+
+static void
+osinfo_image_finalize(GObject *object)
+{
+    /* Chain up to the parent class */
+    G_OBJECT_CLASS(osinfo_image_parent_class)->finalize(object);
+}
+
+/* Init functions */
+static void
+osinfo_image_class_init(OsinfoImageClass *klass)
+{
+    GObjectClass *g_klass = G_OBJECT_CLASS(klass);
+    GParamSpec *pspec;
+
+    g_klass->finalize = osinfo_image_finalize;
+    g_klass->get_property = osinfo_image_get_property;
+    g_klass->set_property = osinfo_image_set_property;
+    g_type_class_add_private(klass, sizeof(OsinfoImagePrivate));
+
+    /**
+     * OsinfoImage:architecture:
+     *
+     * The target hardware architecture of this image.
+     */
+    pspec = g_param_spec_string("architecture",
+                                "ARCHITECTURE",
+                                _("CPU Architecture"),
+                                NULL /* default value */,
+                                G_PARAM_READWRITE |
+                                G_PARAM_STATIC_STRINGS);
+    g_object_class_install_property(g_klass, PROP_ARCHITECTURE, pspec);
+
+    /**
+     * OsinfoImage:format:
+     *
+     * The image format.
+     */
+    pspec = g_param_spec_string("format",
+                                "FORMAT",
+                                _("The image format"),
+                                NULL /* default value */,
+                                G_PARAM_READWRITE |
+                                G_PARAM_STATIC_STRINGS);
+    g_object_class_install_property(g_klass, PROP_FORMAT, pspec);
+
+    /**
+     * OsinfoImage:url:
+     *
+     * The URL to this image.
+     */
+    pspec = g_param_spec_string("url",
+                                "URL",
+                                _("The URL to this image"),
+                                NULL /* default value */,
+                                G_PARAM_READWRITE |
+                                G_PARAM_STATIC_STRINGS);
+    g_object_class_install_property(g_klass, PROP_URL, pspec);
+}
+
+static void
+osinfo_image_init(OsinfoImage *image)
+{
+    image->priv = OSINFO_IMAGE_GET_PRIVATE(image);
+}
+
+OsinfoImage *osinfo_image_new(const gchar *id,
+                              const gchar *architecture,
+                              const gchar *format)
+{
+    OsinfoImage *image;
+
+    image = g_object_new(OSINFO_TYPE_IMAGE,
+                        "id", id,
+                        NULL);
+
+    osinfo_entity_set_param(OSINFO_ENTITY(image),
+                            OSINFO_IMAGE_PROP_ARCHITECTURE,
+                            architecture);
+
+    osinfo_entity_set_param(OSINFO_ENTITY(image),
+                            OSINFO_IMAGE_PROP_FORMAT,
+                            format);
+
+    return image;
+}
+
+/**
+ * osinfo_image_get_architecture:
+ * @image: an #OsinfoImage instance
+ *
+ * Retrieves the target hardware architecture of the OS @image provides.
+ *
+ * Returns: (transfer none): the hardware architecture, or NULL
+ */
+const gchar *osinfo_image_get_architecture(OsinfoImage *image)
+{
+    return osinfo_entity_get_param_value(OSINFO_ENTITY(image),
+                                         OSINFO_IMAGE_PROP_ARCHITECTURE);
+}
+
+/**
+ * osinfo_image_get_format:
+ * @image: an #OsinfoImage instance
+ *
+ * The format of the @image
+ *
+ * Returns: (transfer none): the format, or NULL
+ */
+const gchar *osinfo_image_get_format(OsinfoImage *image)
+{
+    return osinfo_entity_get_param_value(OSINFO_ENTITY(image),
+                                         OSINFO_IMAGE_PROP_FORMAT);
+}
+
+/**
+ * osinfo_image_get_url:
+ * @image: an #OsinfoImage instance
+ *
+ * The URL to the @image
+ *
+ * Returns: (transfer none): the URL, or NULL
+ */
+const gchar *osinfo_image_get_url(OsinfoImage *image)
+{
+    return osinfo_entity_get_param_value(OSINFO_ENTITY(image),
+                                         OSINFO_IMAGE_PROP_URL);
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
diff --git a/osinfo/osinfo_image.h b/osinfo/osinfo_image.h
new file mode 100644
index 0000000..3478ea4
--- /dev/null
+++ b/osinfo/osinfo_image.h
@@ -0,0 +1,91 @@
+/*
+ * libosinfo: An installed image of a (guest) OS
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *  Fabiano Fidêncio <fidencio at redhat.com>
+ */
+
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <osinfo/osinfo_entity.h>
+
+#ifndef __OSINFO_IMAGE_H__
+#define __OSINFO_IMAGE_H__
+
+GQuark
+osinfo_image_error_quark (void) G_GNUC_CONST;
+
+#define OSINFO_IMAGE_ERROR (osinfo_image_error_quark ())
+
+
+/*
+ * Type macros.
+ */
+#define OSINFO_TYPE_IMAGE                  (osinfo_image_get_type ())
+#define OSINFO_IMAGE(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), OSINFO_TYPE_IMAGE, OsinfoImage))
+#define OSINFO_IS_IMAGE(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), OSINFO_TYPE_IMAGE))
+#define OSINFO_IMAGE_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), OSINFO_TYPE_IMAGE, OsinfoImageClass))
+#define OSINFO_IS_IMAGE_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), OSINFO_TYPE_IMAGE))
+#define OSINFO_IMAGE_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), OSINFO_TYPE_IMAGE, OsinfoImageClass))
+
+typedef struct _OsinfoImage        OsinfoImage;
+
+typedef struct _OsinfoImageClass   OsinfoImageClass;
+
+typedef struct _OsinfoImagePrivate OsinfoImagePrivate;
+
+#define OSINFO_IMAGE_PROP_ARCHITECTURE      "architecture"
+#define OSINFO_IMAGE_PROP_FORMAT            "format"
+#define OSINFO_IMAGE_PROP_URL               "url"
+
+/* object */
+struct _OsinfoImage
+{
+    OsinfoEntity parent_instance;
+
+    /* public */
+
+    /* private */
+    OsinfoImagePrivate *priv;
+};
+
+/* class */
+struct _OsinfoImageClass
+{
+    /*< private >*/
+    OsinfoEntityClass parent_class;
+
+    /* class members */
+};
+
+GType osinfo_image_get_type(void);
+
+OsinfoImage *osinfo_image_new(const gchar *id, const gchar *architecture, const gchar *format);
+const gchar *osinfo_image_get_architecture(OsinfoImage *image);
+const gchar *osinfo_image_get_format(OsinfoImage *image);
+const gchar *osinfo_image_get_url(OsinfoImage *image);
+
+#endif /* __OSINFO_IMAGE_H__ */
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fc81f42..2a714df 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -3,6 +3,7 @@ osinfo/osinfo_deployment.c
 osinfo/osinfo_devicelink.c
 osinfo/osinfo_devicelinkfilter.c
 osinfo/osinfo_entity.c
+osinfo/osinfo_image.c
 osinfo/osinfo_install_config_param.c
 osinfo/osinfo_install_script.c
 osinfo/osinfo_list.c
-- 
2.19.1




More information about the Libosinfo mailing list