[Libosinfo] [libosinfo PATCH v2 5/7] db: Add identify_tree() API
Fabiano Fidêncio
fidencio at redhat.com
Wed Jun 26 11:27:08 UTC 2019
Similarly to identify_media() API, let's add a osinfo_db_identify_tree()
API, which is going to simplify a bit tree detection code on the apps
level.
Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
osinfo/libosinfo.syms | 2 +
osinfo/osinfo_db.c | 105 ++++++++++++++++++++++++++++++++++++++++++
osinfo/osinfo_db.h | 2 +
tests/test-db.c | 30 ++++++++++++
4 files changed, 139 insertions(+)
diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 4e96f16..016d167 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -586,6 +586,8 @@ LIBOSINFO_1.5.0 {
LIBOSINFO_1.6.0 {
global:
+ osinfo_db_identify_tree;
+
osinfo_media_create_from_location_with_flags;
osinfo_media_create_from_location_with_flags_async;
osinfo_media_create_from_location_with_flags_finish;
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index c4cd1e4..56b868c 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -875,6 +875,111 @@ OsinfoOs *osinfo_db_guess_os_from_tree(OsinfoDb *db,
return ret;
}
+static void fill_tree(OsinfoDb *db, OsinfoTree *tree,
+ OsinfoTree *matched_tree,
+ OsinfoOs *os)
+{
+ const gchar *id;
+ const gchar *kernel_path;
+ const gchar *initrd_path;
+ const gchar *boot_iso_path;
+ const gchar *arch;
+ const gchar *url;
+ gboolean has_treeinfo;
+ GList *variants, *node;
+
+ id = osinfo_entity_get_id(OSINFO_ENTITY(matched_tree));
+ g_object_set(G_OBJECT(tree), "id", id, NULL);
+
+ arch = osinfo_tree_get_architecture(matched_tree);
+ if (arch != NULL)
+ g_object_set(G_OBJECT(tree), "architecture", arch, NULL);
+ url = osinfo_tree_get_url(matched_tree);
+ if (url != NULL)
+ g_object_set(G_OBJECT(tree), "url", url, NULL);
+ variants = osinfo_entity_get_param_value_list(OSINFO_ENTITY(matched_tree),
+ "variant");
+ for (node = variants; node != NULL; node = node->next)
+ osinfo_entity_add_param(OSINFO_ENTITY(tree),
+ "variant",
+ (gchar *) node->data);
+ g_list_free(variants);
+ kernel_path = osinfo_tree_get_kernel_path(matched_tree);
+ if (kernel_path != NULL)
+ g_object_set(G_OBJECT(tree), "kernel", kernel_path, NULL);
+
+ initrd_path = osinfo_tree_get_initrd_path(matched_tree);
+ if (initrd_path != NULL)
+ g_object_set(G_OBJECT(tree), "initrd", initrd_path, NULL);
+
+ boot_iso_path = osinfo_tree_get_boot_iso_path(matched_tree);
+ if (boot_iso_path != NULL)
+ g_object_set(G_OBJECT(tree), "boot-iso", boot_iso_path, NULL);
+
+ has_treeinfo = osinfo_tree_has_treeinfo(matched_tree);
+ if (has_treeinfo) {
+ const gchar *treeinfo_family;
+ const gchar *treeinfo_variant;
+ const gchar *treeinfo_version;
+ const gchar *treeinfo_arch;
+
+ treeinfo_family = osinfo_tree_get_treeinfo_family(matched_tree);
+ if (treeinfo_family != NULL)
+ g_object_set(G_OBJECT(tree), "treeinfo-family", treeinfo_family, NULL);
+
+ treeinfo_variant = osinfo_tree_get_treeinfo_variant(matched_tree);
+ if (treeinfo_variant != NULL)
+ g_object_set(G_OBJECT(tree), "treeinfo-variant", treeinfo_variant, NULL);
+
+ treeinfo_version = osinfo_tree_get_treeinfo_version(matched_tree);
+ if (treeinfo_version != NULL)
+ g_object_set(G_OBJECT(tree), "treeinfo-version", treeinfo_version, NULL);
+
+ treeinfo_arch = osinfo_tree_get_treeinfo_arch(matched_tree);
+ if (treeinfo_arch != NULL)
+ g_object_set(G_OBJECT(tree), "treeinfo-arch", treeinfo_arch, NULL);
+
+ g_object_set(G_OBJECT(tree), "has-treeinfo", TRUE, NULL);
+ }
+
+ if (os != NULL)
+ osinfo_tree_set_os(tree, os);
+}
+
+/**
+ * osinfo_db_identify_tree:
+ * @db: an #OsinfoDb database
+ * @tree: the installation tree
+ * data
+ *
+ * Try to match a newly created @tree with a tree description from @db.
+ * If found, @tree will be filled with the corresponding information
+ * stored in @db. In particular, after a call to osinfo_db_identify_tree(), if
+ * the tree could be identified, its OsinfoEntify::id and OsinfoMedia::os
+ * properties will be set.
+ *
+ * Returns: TRUE if @tree was found in @db, FALSE otherwise
+ */
+gboolean osinfo_db_identify_tree(OsinfoDb *db,
+ OsinfoTree *tree)
+{
+ OsinfoTree *matched_tree;
+ OsinfoOs *matched_os;
+
+ g_return_val_if_fail(OSINFO_IS_TREE(tree), FALSE);
+ g_return_val_if_fail(OSINFO_IS_DB(db), FALSE);
+
+ matched_os = osinfo_db_guess_os_from_tree(db, tree,
+ &matched_tree);
+ if (matched_os == NULL) {
+ return FALSE;
+ }
+
+ fill_tree(db, tree, matched_tree, matched_os);
+
+ return TRUE;
+}
+
struct osinfo_db_populate_values_args {
GHashTable *values;
const gchar *property;
diff --git a/osinfo/osinfo_db.h b/osinfo/osinfo_db.h
index 90d7493..7702739 100644
--- a/osinfo/osinfo_db.h
+++ b/osinfo/osinfo_db.h
@@ -110,6 +110,8 @@ gboolean osinfo_db_identify_media(OsinfoDb *db,
OsinfoOs *osinfo_db_guess_os_from_tree(OsinfoDb *db,
OsinfoTree *tree,
OsinfoTree **matched_tree);
+gboolean osinfo_db_identify_tree(OsinfoDb *db,
+ OsinfoTree *tree);
// 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);
diff --git a/tests/test-db.c b/tests/test-db.c
index abe8ad5..b7f83d7 100644
--- a/tests/test-db.c
+++ b/tests/test-db.c
@@ -528,6 +528,35 @@ test_guess_os_from_tree(void)
}
+static void
+test_identify_tree(void)
+{
+ OsinfoLoader *loader = osinfo_loader_new();
+ OsinfoDb *db;
+ OsinfoTree *tree;
+
+ GError *error = NULL;
+
+ osinfo_loader_process_path(loader, SRCDIR "/tests/dbdata", &error);
+ g_assert_no_error(error);
+ db = osinfo_loader_get_db(loader);
+
+ /* Matching against an "all" architecture" */
+ tree = create_tree("x86_64", FALSE);
+ g_assert_true(osinfo_db_identify_tree(db, tree));
+ g_assert_cmpstr(osinfo_tree_get_architecture(tree), ==, "all");
+ g_object_unref(tree);
+
+ /* Matching against a known architecture, which has to have precendence */
+ tree = create_tree("i686", TRUE);
+ g_assert_true(osinfo_db_identify_tree(db, tree));
+ g_assert_cmpstr(osinfo_tree_get_architecture(tree), ==, "i686");
+ g_object_unref(tree);
+
+ g_object_unref(loader);
+}
+
+
int
main(int argc, char *argv[])
{
@@ -543,6 +572,7 @@ main(int argc, char *argv[])
g_test_add_func("/db/rel_os", test_rel_os);
g_test_add_func("/db/identify_media", test_identify_media);
g_test_add_func("/db/guess_os_from_tree", test_guess_os_from_tree);
+ g_test_add_func("/db/identify_tree", test_identify_tree);
/* Upfront so we don't confuse valgrind */
osinfo_entity_get_type();
--
2.21.0
More information about the Libosinfo
mailing list