richardphibel / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone
2c1f71
From 7d8f9cfcdf7725fef2c99ecb2dedcdff1e9506d7 Mon Sep 17 00:00:00 2001
2c1f71
From: Jaroslav Rohel <jrohel@redhat.com>
2c1f71
Date: Wed, 13 Apr 2022 12:26:10 +0200
2c1f71
Subject: [PATCH 26/34] context: Substitute all repository config options
2c1f71
 (RhBug:2076853)
2c1f71
2c1f71
It also solves the problem: Substitution of variables in `baseurl`
2c1f71
does not work in microdnf and PackageKit unless `metalink` or `mirrorlist`
2c1f71
is set at the same time.
2c1f71
---
2c1f71
 libdnf/dnf-repo.cpp | 34 +++++++++++++++++++++++++---------
2c1f71
 1 file changed, 25 insertions(+), 9 deletions(-)
2c1f71
2c1f71
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2c1f71
index 710045fb..9d42e3e3 100644
2c1f71
--- a/libdnf/dnf-repo.cpp
2c1f71
+++ b/libdnf/dnf-repo.cpp
2c1f71
@@ -83,6 +83,7 @@ typedef struct
2c1f71
     LrHandle        *repo_handle;
2c1f71
     LrResult        *repo_result;
2c1f71
     LrUrlVars       *urlvars;
2c1f71
+    bool            unit_test_mode;  /* ugly hack for unit tests */
2c1f71
 } DnfRepoPrivate;
2c1f71
 
2c1f71
 G_DEFINE_TYPE_WITH_PRIVATE(DnfRepo, dnf_repo, G_TYPE_OBJECT)
2c1f71
@@ -847,8 +848,11 @@ dnf_repo_conf_reset(libdnf::ConfigRepo &config)
2c1f71
 
2c1f71
 /* Loads repository configuration from GKeyFile */
2c1f71
 static void
2c1f71
-dnf_repo_conf_from_gkeyfile(libdnf::ConfigRepo &config, const char *repoId, GKeyFile *gkeyFile)
2c1f71
+dnf_repo_conf_from_gkeyfile(DnfRepo *repo, const char *repoId, GKeyFile *gkeyFile)
2c1f71
 {
2c1f71
+    DnfRepoPrivate *priv = GET_PRIVATE(repo);
2c1f71
+    auto & config = *priv->repo->getConfig();
2c1f71
+
2c1f71
     // Reset to the initial state before reloading the configuration.
2c1f71
     dnf_repo_conf_reset(config);
2c1f71
 
2c1f71
@@ -883,20 +887,31 @@ dnf_repo_conf_from_gkeyfile(libdnf::ConfigRepo &config, const char *repoId, GKey
2c1f71
                         // list can be ['value1', 'value2, value3'] therefore we first join
2c1f71
                         // to have 'value1, value2, value3'
2c1f71
                         g_autofree gchar * tmp_strval = g_strjoinv(",", list);
2c1f71
+
2c1f71
+                        // Substitute vars.
2c1f71
+                        g_autofree gchar *subst_value = dnf_repo_substitute(repo, tmp_strval);
2c1f71
+
2c1f71
+                        if (strcmp(key, "baseurl") == 0 && strstr(tmp_strval, "file://$testdatadir") != NULL) {
2c1f71
+                            priv->unit_test_mode = true;
2c1f71
+                        }
2c1f71
+
2c1f71
                         try {
2c1f71
-                            optionItem.newString(libdnf::Option::Priority::REPOCONFIG, tmp_strval);
2c1f71
+                            optionItem.newString(libdnf::Option::Priority::REPOCONFIG, subst_value);
2c1f71
                         } catch (const std::exception & ex) {
2c1f71
-                            g_debug("Invalid configuration value: %s = %s in %s; %s", key, value.c_str(), repoId, ex.what());
2c1f71
+                            g_debug("Invalid configuration value: %s = %s in %s; %s", key, subst_value, repoId, ex.what());
2c1f71
                         }
2c1f71
                     }
2c1f71
 
2c1f71
                 } else {
2c1f71
-
2c1f71
                     // process other (non list) options
2c1f71
+
2c1f71
+                    // Substitute vars.
2c1f71
+                    g_autofree gchar *subst_value = dnf_repo_substitute(repo, value.c_str());
2c1f71
+
2c1f71
                     try {
2c1f71
-                        optionItem.newString(libdnf::Option::Priority::REPOCONFIG, value);
2c1f71
+                        optionItem.newString(libdnf::Option::Priority::REPOCONFIG, subst_value);
2c1f71
                     } catch (const std::exception & ex) {
2c1f71
-                        g_debug("Invalid configuration value: %s = %s in %s; %s", key, value.c_str(), repoId, ex.what());
2c1f71
+                        g_debug("Invalid configuration value: %s = %s in %s; %s", key, subst_value, repoId, ex.what());
2c1f71
                     }
2c1f71
 
2c1f71
                 }
2c1f71
@@ -950,7 +965,7 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, gboolean reloadFromGKeyFile, GError **e
2c1f71
 
2c1f71
     // Reload repository configuration from keyfile.
2c1f71
     if (reloadFromGKeyFile) {
2c1f71
-        dnf_repo_conf_from_gkeyfile(*conf, repoId, priv->keyfile);
2c1f71
+        dnf_repo_conf_from_gkeyfile(repo, repoId, priv->keyfile);
2c1f71
         dnf_repo_apply_setopts(*conf, repoId);
2c1f71
     }
2c1f71
 
2c1f71
@@ -996,8 +1011,9 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, gboolean reloadFromGKeyFile, GError **e
2c1f71
         g_autofree gchar *url = NULL;
2c1f71
         url = lr_prepend_url_protocol(baseurls[0]);
2c1f71
         if (url != NULL && strncasecmp(url, "file://", 7) == 0) {
2c1f71
-            if (g_strstr_len(url, -1, "$testdatadir") == NULL)
2c1f71
+            if (!priv->unit_test_mode) {
2c1f71
                 priv->kind = DNF_REPO_KIND_LOCAL;
2c1f71
+            }
2c1f71
             g_free(priv->location);
2c1f71
             g_free(priv->keyring);
2c1f71
             priv->location = dnf_repo_substitute(repo, url + 7);
2c1f71
@@ -1224,7 +1240,7 @@ dnf_repo_setup(DnfRepo *repo, GError **error) try
2c1f71
     auto repoId = priv->repo->getId().c_str();
2c1f71
 
2c1f71
     auto conf = priv->repo->getConfig();
2c1f71
-    dnf_repo_conf_from_gkeyfile(*conf, repoId, priv->keyfile);
2c1f71
+    dnf_repo_conf_from_gkeyfile(repo, repoId, priv->keyfile);
2c1f71
     dnf_repo_apply_setopts(*conf, repoId);
2c1f71
 
2c1f71
     auto sslverify = conf->sslverify().getValue();
2c1f71
-- 
2c1f71
2.31.1
2c1f71