[virt-tools-list] [libosinfo 5/8] Add osinfo_db_identify_media

Christophe Fergeau cfergeau at redhat.com
Mon Dec 3 11:23:34 UTC 2012


---
 osinfo/libosinfo.syms |   2 +
 osinfo/osinfo_db.c    | 105 +++++++++++++++++++++++++++++++++-----------------
 osinfo/osinfo_db.h    |   2 +
 test/test-isodetect.c |  10 ++---
 tools/osinfo-detect.c |  12 ++++--
 5 files changed, 87 insertions(+), 44 deletions(-)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 2824678..d45e58e 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -328,6 +328,8 @@ LIBOSINFO_0.2.2 {
 	osinfo_product_relationship_get_type;
 	osinfo_path_format_get_type;
 
+	osinfo_db_identify_media;
+
 	osinfo_entity_get_param_value_enum;
 	osinfo_entity_set_param_enum;
 
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index 665554c..1e8a93c 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -391,40 +391,6 @@ static gint media_volume_compare (gconstpointer a, gconstpointer b)
         return 1;
 }
 
-static void fill_media (OsinfoMedia *media, OsinfoMedia *matched_media)
-{
-    gboolean is_installer;
-    gboolean is_live;
-    gint reboots;
-    const gchar *kernel_path;
-    const gchar *initrd_path;
-    const gchar *arch;
-    const gchar *url;
-
-    arch = osinfo_media_get_architecture(matched_media);
-    if (arch != NULL)
-        g_object_set(G_OBJECT(media), "architecture", arch, NULL);
-    url = osinfo_media_get_url(matched_media);
-    if (url != NULL)
-        g_object_set(G_OBJECT(media), "url", url, NULL);
-
-    kernel_path = osinfo_media_get_kernel_path(matched_media);
-    if (kernel_path != NULL)
-        g_object_set(G_OBJECT(media), "kernel_path", kernel_path, NULL);
-
-    initrd_path = osinfo_media_get_initrd_path(matched_media);
-    if (initrd_path != NULL)
-        g_object_set(G_OBJECT(media), "initrd_path", initrd_path, NULL);
-    is_installer = osinfo_media_get_installer(matched_media);
-    is_live = osinfo_media_get_live(matched_media);
-    reboots = osinfo_media_get_installer_reboots(matched_media);
-    g_object_set(G_OBJECT(media),
-                 "installer", is_installer,
-                 "live", is_live,
-                 "installer-reboots", reboots,
-                 NULL);
-}
-
 /**
  * osinfo_db_guess_os_from_media:
  * @db: the database
@@ -477,7 +443,6 @@ OsinfoOs *osinfo_db_guess_os_from_media(OsinfoDb *db,
                 match_regex (os_system, media_system) &&
                 match_regex (os_publisher, media_publisher)) {
                 ret = os;
-                fill_media(media, os_media);
                 if (matched_media != NULL)
                     *matched_media = os_media;
                 break;
@@ -496,6 +461,76 @@ OsinfoOs *osinfo_db_guess_os_from_media(OsinfoDb *db,
     return ret;
 }
 
+static void fill_media (OsinfoMedia *media, OsinfoMedia *matched_media, OsinfoOs *os)
+{
+    gboolean is_installer;
+    gboolean is_live;
+    gint reboots;
+    const gchar *kernel_path;
+    const gchar *initrd_path;
+    const gchar *arch;
+    const gchar *url;
+
+    arch = osinfo_media_get_architecture(matched_media);
+    if (arch != NULL)
+        g_object_set(G_OBJECT(media), "architecture", arch, NULL);
+    url = osinfo_media_get_url(matched_media);
+    if (url != NULL)
+        g_object_set(G_OBJECT(media), "url", url, NULL);
+
+    kernel_path = osinfo_media_get_kernel_path(matched_media);
+    if (kernel_path != NULL)
+        g_object_set(G_OBJECT(media), "kernel_path", kernel_path, NULL);
+
+    initrd_path = osinfo_media_get_initrd_path(matched_media);
+    if (initrd_path != NULL)
+        g_object_set(G_OBJECT(media), "initrd_path", initrd_path, NULL);
+    is_installer = osinfo_media_get_installer(matched_media);
+    is_live = osinfo_media_get_live(matched_media);
+    g_object_set(G_OBJECT(media),
+                 "installer", is_installer,
+                 "live", is_live,
+                 NULL);
+    if (is_installer) {
+        reboots = osinfo_media_get_installer_reboots(matched_media);
+        g_object_set(G_OBJECT(media), "installer-reboots", reboots, NULL);
+    }
+    if (os != NULL)
+        osinfo_media_set_os(media, os);
+}
+
+/**
+ * osinfo_db_identify_media:
+ * @db: a #OsinfoDb database
+ * @media: the installation media
+ * data
+ *
+ * Try to match the @media created using osinfo_media_create_from_location()
+ * with a media description from @db. If found, @media will be filled with
+ * the corresponding information stored in @db. If non-NULL, @os will be
+ * filled with information about the OS corresponding to @media.
+ *
+ * Returns: (transfer none): TRUE if @media was found in @db, FALSE
+ * otherwise
+ */
+gboolean osinfo_db_identify_media(OsinfoDb *db, OsinfoMedia *media)
+{
+    OsinfoMedia *matched_media;
+    OsinfoOs *matched_os;
+
+    g_return_val_if_fail(OSINFO_IS_MEDIA(media), FALSE);
+    g_return_val_if_fail(OSINFO_IS_DB(db), FALSE);
+
+    matched_os = osinfo_db_guess_os_from_media(db, media, &matched_media);
+    if (matched_os == NULL) {
+        return FALSE;
+    }
+
+    fill_media(media, matched_media, matched_os);
+
+    return TRUE;
+}
+
 
 /**
  * osinfo_db_guess_os_from_tree:
diff --git a/osinfo/osinfo_db.h b/osinfo/osinfo_db.h
index d789802..2b7c74c 100644
--- a/osinfo/osinfo_db.h
+++ b/osinfo/osinfo_db.h
@@ -99,6 +99,8 @@ void osinfo_db_add_install_script(OsinfoDb *db, OsinfoInstallScript *script);
 OsinfoOs *osinfo_db_guess_os_from_media(OsinfoDb *db,
                                         OsinfoMedia *media,
                                         OsinfoMedia **matched_media);
+gboolean osinfo_db_identify_media(OsinfoDb *db,
+                                  OsinfoMedia *media);
 
 OsinfoOs *osinfo_db_guess_os_from_tree(OsinfoDb *db,
                                        OsinfoTree *tree,
diff --git a/test/test-isodetect.c b/test/test-isodetect.c
index 9033b32..f627300 100644
--- a/test/test-isodetect.c
+++ b/test/test-isodetect.c
@@ -252,18 +252,18 @@ static void test_one(const gchar *vendor)
     tmp = isos;
     while (tmp) {
         struct ISOInfo *info  = tmp->data;
-        OsinfoMedia *media = NULL;
-        OsinfoOs *os = osinfo_db_guess_os_from_media(db,
-                                                     info->media,
-                                                     &media);
+        gboolean matched = osinfo_db_identify_media(db, info->media);
+        OsinfoOs *os;
 
-        fail_unless(os != NULL, "ISO %s was not matched by OS %s",
+        fail_unless(matched, "ISO %s was not matched by OS %s",
                     info->filename, info->shortid);
 
+        g_object_get(info->media, "os", &os, NULL);
         const gchar *shortid = osinfo_product_get_short_id(OSINFO_PRODUCT(os));
         fail_unless(g_str_equal(shortid, info->shortid),
                     "ISO %s matched OS %s instead of expected %s",
                     info->filename, shortid, info->shortid);
+        g_object_unref(G_OBJECT(os));
 
         tmp = tmp->next;
     }
diff --git a/tools/osinfo-detect.c b/tools/osinfo-detect.c
index 944e72d..0caa4ca 100644
--- a/tools/osinfo-detect.c
+++ b/tools/osinfo-detect.c
@@ -118,8 +118,11 @@ static void print_bootable(gboolean bootable)
             g_print(_("Media is not bootable.\n"));
 }
 
-static void print_os_media(OsinfoOs *os, OsinfoMedia *media)
+static void print_media(OsinfoMedia *media)
 {
+    OsinfoOs *os;
+
+    g_object_get(G_OBJECT(media), "os", &os, NULL);
     if (os == NULL)
         return;
 
@@ -140,6 +143,7 @@ static void print_os_media(OsinfoOs *os, OsinfoMedia *media)
         if (osinfo_media_get_live (media))
             g_print(_("Media is live media for OS '%s'\n"), name);
     }
+    g_object_unref(os);
 }
 
 static void print_os_tree(OsinfoOs *os, OsinfoTree *tree, OsinfoTree *matched_tree)
@@ -182,7 +186,6 @@ gint main(gint argc, gchar **argv)
     GError *error = NULL;
     OsinfoLoader *loader = NULL;
     OsinfoDb *db = NULL;
-    OsinfoOs *os = NULL;
     gint ret = 0;
 
     setlocale(LC_ALL, "");
@@ -240,9 +243,10 @@ gint main(gint argc, gchar **argv)
         } else {
             print_bootable(TRUE);
         }
-        os = osinfo_db_guess_os_from_media(db, media, NULL);
-        print_os_media(os, media);
+        osinfo_db_identify_media(db, media);
+        print_media(media);
     } else if (type == URL_TYPE_TREE) {
+        OsinfoOs *os = NULL;
         OsinfoTree *tree = NULL;
         OsinfoTree *matched_tree = NULL;
         tree = osinfo_tree_create_from_location(argv[1], NULL, &error);
-- 
1.8.0.1




More information about the virt-tools-list mailing list