dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0001-context-try-to-find-releasever-in-RPMDB.patch

499892
From 9e711da60ac3eabf266356511fc040c69c2bdbce Mon Sep 17 00:00:00 2001
499892
From: Igor Gnatenko <ignatenko@redhat.com>
499892
Date: Tue, 21 Mar 2017 17:47:00 +0100
499892
Subject: [PATCH] context: try to find releasever in RPMDB
499892
499892
That's how YUM/DNF find out releasever..
499892
499892
$ rpm -q --provides $(rpm -q --whatprovides "system-release(releasever)") | grep "^system-release(releasever)"
499892
system-release(releasever) = 7Server
499892
499892
Signed-off-by: Igor Gnatenko <ignatenko@redhat.com>
499892
499892
Closes: #278
499892
Approved by: ignatenkobrain
499892
---
499892
 libdnf/dnf-context.c | 29 ++++++++++++++++++++++++++++-
499892
 1 file changed, 28 insertions(+), 1 deletion(-)
499892
499892
diff --git a/libdnf/dnf-context.c b/libdnf/dnf-context.c
499892
index 6ec1040..3d92a5b 100644
499892
--- a/libdnf/dnf-context.c
499892
+++ b/libdnf/dnf-context.c
499892
@@ -34,6 +34,8 @@
499892
 #include <gio/gio.h>
499892
 #include <rpm/rpmlib.h>
499892
 #include <rpm/rpmmacro.h>
499892
+#include <rpm/rpmts.h>
499892
+#include <rpm/rpmdb.h>
499892
 #include <librepo/librepo.h>
499892
 #ifdef RHSM_SUPPORT
499892
 #include <rhsm/rhsm.h>
499892
@@ -53,6 +55,8 @@
499892
 
499892
 #define MAX_NATIVE_ARCHES    12
499892
 
499892
+#define RELEASEVER_PROV "system-release(releasever)"
499892
+
499892
 /* data taken from https://github.com/rpm-software-management/dnf/blob/master/dnf/arch.py */
499892
 static const struct {
499892
     const gchar    *base;
499892
@@ -1029,13 +1033,36 @@ dnf_context_set_cache_age(DnfContext *context, guint cache_age)
499892
 static gboolean
499892
 dnf_context_set_os_release(DnfContext *context, GError **error)
499892
 {
499892
+    const char *source_root = dnf_context_get_source_root (context);
499892
+
499892
+    gboolean found_in_rpmdb = FALSE;
499892
+    rpmts ts = rpmtsCreate ();
499892
+    rpmtsSetRootDir (ts, source_root);
499892
+    rpmdbMatchIterator mi = rpmtsInitIterator (ts, RPMTAG_PROVIDENAME, RELEASEVER_PROV, 0);
499892
+    Header hdr;
499892
+    while ((hdr = rpmdbNextIterator (mi)) != NULL) {
499892
+        const char *v = headerGetString (hdr, RPMTAG_VERSION);
499892
+        rpmds ds = rpmdsNew (hdr, RPMTAG_PROVIDENAME, 0);
499892
+        while (rpmdsNext (ds) >= 0) {
499892
+            if (strcmp (rpmdsN (ds), RELEASEVER_PROV) == 0 && rpmdsFlags (ds) == RPMSENSE_EQUAL)
499892
+                v = rpmdsEVR (ds);
499892
+        }
499892
+        found_in_rpmdb = TRUE;
499892
+        dnf_context_set_release_ver (context, v);
499892
+        rpmdsFree (ds);
499892
+        break;
499892
+    }
499892
+    rpmdbFreeIterator (mi);
499892
+    rpmtsFree (ts);
499892
+    if (found_in_rpmdb)
499892
+        return TRUE;
499892
+
499892
     g_autofree gchar *contents = NULL;
499892
     g_autofree gchar *maybe_quoted_version = NULL;
499892
     g_autofree gchar *version = NULL;
499892
     g_autofree gchar *os_release = NULL;
499892
     g_autoptr(GString) str = NULL;
499892
     g_autoptr(GKeyFile) key_file = NULL;
499892
-    const char *source_root = dnf_context_get_source_root(context);
499892
 
499892
     os_release = g_build_filename(source_root, "etc/os-release", NULL);
499892
     if (!dnf_get_file_contents_allow_noent(os_release, &contents, NULL, error))
499892
-- 
499892
2.12.1
499892