dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0026-context-Substitute-all-repository-config-options-RhB.patch

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