dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0004-context-improve-retrieving-repository-configuration.patch

2acfc6
From be8449aa177473a834a5b2c401a8a3fcc61522b4 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Wed, 2 Dec 2020 08:00:07 +0100
2acfc6
Subject: [PATCH 1/9] Option: Add reset() method
2acfc6
2acfc6
The method resets the option to its initial state.
2acfc6
Can be used, for example, before reloading the configuration in daemon
2acfc6
mode (PackageKit).
2acfc6
---
2acfc6
 libdnf/conf/Option.hpp           |  2 ++
2acfc6
 libdnf/conf/OptionBool.hpp       |  9 ++++++++-
2acfc6
 libdnf/conf/OptionChild.hpp      | 14 ++++++++++++++
2acfc6
 libdnf/conf/OptionEnum.hpp       | 15 +++++++++++++++
2acfc6
 libdnf/conf/OptionNumber.hpp     | 10 +++++++++-
2acfc6
 libdnf/conf/OptionString.cpp     | 11 ++++++++---
2acfc6
 libdnf/conf/OptionString.hpp     |  8 ++++++++
2acfc6
 libdnf/conf/OptionStringList.hpp |  9 ++++++++-
2acfc6
 8 files changed, 72 insertions(+), 6 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/conf/Option.hpp b/libdnf/conf/Option.hpp
2acfc6
index e9a9dfc84..849871fe7 100644
2acfc6
--- a/libdnf/conf/Option.hpp
2acfc6
+++ b/libdnf/conf/Option.hpp
2acfc6
@@ -62,6 +62,8 @@ class Option {
2acfc6
     virtual void set(Priority priority, const std::string & value) = 0;
2acfc6
     virtual std::string getValueString() const = 0;
2acfc6
     virtual bool empty() const noexcept;
2acfc6
+    /// Resets the option to its initial state.
2acfc6
+    virtual void reset() = 0;
2acfc6
     virtual ~Option() = default;
2acfc6
 
2acfc6
 protected:
2acfc6
diff --git a/libdnf/conf/OptionBool.hpp b/libdnf/conf/OptionBool.hpp
2acfc6
index c27ab0b79..a5e647807 100644
2acfc6
--- a/libdnf/conf/OptionBool.hpp
2acfc6
+++ b/libdnf/conf/OptionBool.hpp
2acfc6
@@ -47,6 +47,7 @@ class OptionBool : public Option {
2acfc6
     std::string getValueString() const override;
2acfc6
     const char * const * getTrueValues() const noexcept;
2acfc6
     const char * const * getFalseValues() const noexcept;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
     const char * const * const trueValues;
2acfc6
@@ -84,7 +85,13 @@ inline const char * const * OptionBool::getTrueValues() const noexcept
2acfc6
 
2acfc6
 inline const char * const * OptionBool::getFalseValues() const noexcept
2acfc6
 {
2acfc6
-    return falseValues ? falseValues : defFalseValues; 
2acfc6
+    return falseValues ? falseValues : defFalseValues;
2acfc6
+}
2acfc6
+
2acfc6
+inline void OptionBool::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = Priority::DEFAULT;
2acfc6
 }
2acfc6
 
2acfc6
 }
2acfc6
diff --git a/libdnf/conf/OptionChild.hpp b/libdnf/conf/OptionChild.hpp
2acfc6
index 5d1503cb6..3056345f9 100644
2acfc6
--- a/libdnf/conf/OptionChild.hpp
2acfc6
+++ b/libdnf/conf/OptionChild.hpp
2acfc6
@@ -39,6 +39,7 @@ class OptionChild : public Option {
2acfc6
     const typename ParentOptionType::ValueType getDefaultValue() const;
2acfc6
     std::string getValueString() const override;
2acfc6
     bool empty() const noexcept override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 private:
2acfc6
     const ParentOptionType * parent;
2acfc6
@@ -56,6 +57,7 @@ class OptionChild
2acfc6
     const std::string & getDefaultValue() const;
2acfc6
     std::string getValueString() const override;
2acfc6
     bool empty() const noexcept override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 private:
2acfc6
     const ParentOptionType * parent;
2acfc6
@@ -119,6 +121,12 @@ inline bool OptionChild<ParentOptionType, Enable>::empty() const noexcept
2acfc6
     return priority == Priority::EMPTY && parent->empty();
2acfc6
 }
2acfc6
 
2acfc6
+template <class ParentOptionType, class Enable>
2acfc6
+inline void OptionChild<ParentOptionType, Enable>::reset()
2acfc6
+{
2acfc6
+    priority = Priority::EMPTY;
2acfc6
+}
2acfc6
+
2acfc6
 template <class ParentOptionType>
2acfc6
 inline OptionChild<ParentOptionType, typename std::enable_if<std::is_same<typename ParentOptionType::ValueType, std::string>::value>::type>::OptionChild(const ParentOptionType & parent)
2acfc6
 : parent(&parent) {}
2acfc6
@@ -171,6 +179,12 @@ inline bool OptionChild
2acfc6
     return priority == Priority::EMPTY && parent->empty();
2acfc6
 }
2acfc6
 
2acfc6
+template <class ParentOptionType>
2acfc6
+inline void OptionChild<ParentOptionType, typename std::enable_if<std::is_same<typename ParentOptionType::ValueType, std::string>::value>::type>::reset()
2acfc6
+{
2acfc6
+    priority = Priority::EMPTY;
2acfc6
+}
2acfc6
+
2acfc6
 }
2acfc6
 
2acfc6
 #endif
2acfc6
diff --git a/libdnf/conf/OptionEnum.hpp b/libdnf/conf/OptionEnum.hpp
2acfc6
index c63156cb3..d2f710f20 100644
2acfc6
--- a/libdnf/conf/OptionEnum.hpp
2acfc6
+++ b/libdnf/conf/OptionEnum.hpp
2acfc6
@@ -49,6 +49,7 @@ class OptionEnum : public Option {
2acfc6
     T getDefaultValue() const;
2acfc6
     std::string toString(ValueType value) const;
2acfc6
     std::string getValueString() const override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
     FromStringFunc fromStringUser;
2acfc6
@@ -74,6 +75,7 @@ class OptionEnum<std::string> : public Option {
2acfc6
     const std::string & getValue() const;
2acfc6
     const std::string & getDefaultValue() const;
2acfc6
     std::string getValueString() const override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
     FromStringFunc fromStringUser;
2acfc6
@@ -88,6 +90,13 @@ inline OptionEnum<T> * OptionEnum<T>::clone() const
2acfc6
     return new OptionEnum<T>(*this);
2acfc6
 }
2acfc6
 
2acfc6
+template <typename T>
2acfc6
+inline void OptionEnum<T>::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = Priority::DEFAULT;
2acfc6
+}
2acfc6
+
2acfc6
 inline OptionEnum<std::string> * OptionEnum<std::string>::clone() const
2acfc6
 {
2acfc6
     return new OptionEnum<std::string>(*this);
2acfc6
@@ -108,6 +117,12 @@ inline std::string OptionEnum<std::string>::getValueString() const
2acfc6
     return value;
2acfc6
 }
2acfc6
 
2acfc6
+inline void OptionEnum<std::string>::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = Priority::DEFAULT;
2acfc6
+}
2acfc6
+
2acfc6
 }
2acfc6
 
2acfc6
 #endif
2acfc6
diff --git a/libdnf/conf/OptionNumber.hpp b/libdnf/conf/OptionNumber.hpp
2acfc6
index 98988fd50..f7a7b3d6e 100644
2acfc6
--- a/libdnf/conf/OptionNumber.hpp
2acfc6
+++ b/libdnf/conf/OptionNumber.hpp
2acfc6
@@ -50,6 +50,7 @@ class OptionNumber : public Option {
2acfc6
     T getDefaultValue() const;
2acfc6
     std::string toString(ValueType value) const;
2acfc6
     std::string getValueString() const override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
     FromStringFunc fromStringUser;
2acfc6
@@ -80,7 +81,14 @@ inline T OptionNumber<T>::getDefaultValue() const
2acfc6
 template <typename T>
2acfc6
 inline std::string OptionNumber<T>::getValueString() const
2acfc6
 {
2acfc6
-    return toString(value); 
2acfc6
+    return toString(value);
2acfc6
+}
2acfc6
+
2acfc6
+template <typename T>
2acfc6
+inline void OptionNumber<T>::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = Priority::DEFAULT;
2acfc6
 }
2acfc6
 
2acfc6
 extern template class OptionNumber<std::int32_t>;
2acfc6
diff --git a/libdnf/conf/OptionString.cpp b/libdnf/conf/OptionString.cpp
2acfc6
index d27194f7a..b42e6c633 100644
2acfc6
--- a/libdnf/conf/OptionString.cpp
2acfc6
+++ b/libdnf/conf/OptionString.cpp
2acfc6
@@ -27,18 +27,21 @@
2acfc6
 namespace libdnf {
2acfc6
 
2acfc6
 OptionString::OptionString(const std::string & defaultValue)
2acfc6
-: Option(Priority::DEFAULT), defaultValue(defaultValue), value(defaultValue) {}
2acfc6
+: Option(Priority::DEFAULT), initPriority(Priority::DEFAULT), defaultValue(defaultValue), value(defaultValue) {}
2acfc6
 
2acfc6
 OptionString::OptionString(const char * defaultValue)
2acfc6
 {
2acfc6
     if (defaultValue) {
2acfc6
         this->value = this->defaultValue = defaultValue;
2acfc6
-        this->priority = Priority::DEFAULT;
2acfc6
+        this->initPriority = this->priority = Priority::DEFAULT;
2acfc6
+    } else {
2acfc6
+        this->initPriority = Priority::EMPTY;
2acfc6
     }
2acfc6
 }
2acfc6
 
2acfc6
 OptionString::OptionString(const std::string & defaultValue, const std::string & regex, bool icase)
2acfc6
-: Option(Priority::DEFAULT), regex(regex), icase(icase), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); }
2acfc6
+: Option(Priority::DEFAULT), initPriority(Priority::DEFAULT), regex(regex), icase(icase)
2acfc6
+, defaultValue(defaultValue), value(defaultValue) { test(defaultValue); }
2acfc6
 
2acfc6
 OptionString::OptionString(const char * defaultValue, const std::string & regex, bool icase)
2acfc6
 : regex(regex), icase(icase)
2acfc6
@@ -48,6 +51,8 @@ OptionString::OptionString(const char * defaultValue, const std::string & regex,
2acfc6
         test(this->defaultValue);
2acfc6
         this->value = this->defaultValue;
2acfc6
         this->priority = Priority::DEFAULT;
2acfc6
+    } else {
2acfc6
+        this->initPriority = Priority::EMPTY;
2acfc6
     }
2acfc6
 }
2acfc6
 
2acfc6
diff --git a/libdnf/conf/OptionString.hpp b/libdnf/conf/OptionString.hpp
2acfc6
index 2e26305c4..03fef8bcf 100644
2acfc6
--- a/libdnf/conf/OptionString.hpp
2acfc6
+++ b/libdnf/conf/OptionString.hpp
2acfc6
@@ -42,8 +42,10 @@ class OptionString : public Option {
2acfc6
     const std::string & getValue() const;
2acfc6
     const std::string & getDefaultValue() const noexcept;
2acfc6
     std::string getValueString() const override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
+    Priority initPriority;
2acfc6
     std::string regex;
2acfc6
     bool icase;
2acfc6
     std::string defaultValue;
2acfc6
@@ -70,6 +72,12 @@ inline std::string OptionString::fromString(const std::string & value) const
2acfc6
     return value;
2acfc6
 }
2acfc6
 
2acfc6
+inline void OptionString::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = initPriority;
2acfc6
+}
2acfc6
+
2acfc6
 }
2acfc6
 
2acfc6
 #endif
2acfc6
diff --git a/libdnf/conf/OptionStringList.hpp b/libdnf/conf/OptionStringList.hpp
2acfc6
index 942e56b16..20debaa8c 100644
2acfc6
--- a/libdnf/conf/OptionStringList.hpp
2acfc6
+++ b/libdnf/conf/OptionStringList.hpp
2acfc6
@@ -45,6 +45,7 @@ class OptionStringList : public Option {
2acfc6
     const ValueType & getDefaultValue() const;
2acfc6
     std::string toString(const ValueType & value) const;
2acfc6
     std::string getValueString() const override;
2acfc6
+    void reset() override;
2acfc6
 
2acfc6
 protected:
2acfc6
     std::string regex;
2acfc6
@@ -70,7 +71,13 @@ inline const OptionStringList::ValueType & OptionStringList::getDefaultValue() c
2acfc6
 
2acfc6
 inline std::string OptionStringList::getValueString() const
2acfc6
 {
2acfc6
-    return toString(value); 
2acfc6
+    return toString(value);
2acfc6
+}
2acfc6
+
2acfc6
+inline void OptionStringList::reset()
2acfc6
+{
2acfc6
+    value = defaultValue;
2acfc6
+    priority = Priority::DEFAULT;
2acfc6
 }
2acfc6
 
2acfc6
 }
2acfc6
2acfc6
From 372a000414875f323147cd342dd8b4c8c7ebe260 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Tue, 1 Dec 2020 08:29:53 +0100
2acfc6
Subject: [PATCH 2/9] Add OptionBinds::getOption() method
2acfc6
2acfc6
Sometime we want direct access to the underlying Option.
2acfc6
E.g. we want to get its original value (not just a string representation)
2acfc6
or find out the Option type.
2acfc6
---
2acfc6
 libdnf/conf/OptionBinds.cpp | 9 +++++++++
2acfc6
 libdnf/conf/OptionBinds.hpp | 2 ++
2acfc6
 2 files changed, 11 insertions(+)
2acfc6
2acfc6
diff --git a/libdnf/conf/OptionBinds.cpp b/libdnf/conf/OptionBinds.cpp
2acfc6
index f7c67540b..ab53518a3 100644
2acfc6
--- a/libdnf/conf/OptionBinds.cpp
2acfc6
+++ b/libdnf/conf/OptionBinds.cpp
2acfc6
@@ -66,6 +66,15 @@ bool OptionBinds::Item::getAddValue() const
2acfc6
     return addValue;
2acfc6
 }
2acfc6
 
2acfc6
+const Option & OptionBinds::Item::getOption() const
2acfc6
+{
2acfc6
+    return *option;
2acfc6
+}
2acfc6
+
2acfc6
+Option & OptionBinds::Item::getOption()
2acfc6
+{
2acfc6
+    return *option;
2acfc6
+}
2acfc6
 
2acfc6
 // =========== OptionBinds class ===============
2acfc6
 
2acfc6
diff --git a/libdnf/conf/OptionBinds.hpp b/libdnf/conf/OptionBinds.hpp
2acfc6
index 715c37e26..515120b93 100644
2acfc6
--- a/libdnf/conf/OptionBinds.hpp
2acfc6
+++ b/libdnf/conf/OptionBinds.hpp
2acfc6
@@ -55,6 +55,8 @@ class OptionBinds {
2acfc6
         void newString(Option::Priority priority, const std::string & value);
2acfc6
         std::string getValueString() const;
2acfc6
         bool getAddValue() const;
2acfc6
+        const Option & getOption() const;
2acfc6
+        Option & getOption();
2acfc6
 
2acfc6
     private:
2acfc6
         friend class OptionBinds;
2acfc6
2acfc6
From 3a686c378978c90538a6ac5d9826d52ce7c8daf6 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Tue, 1 Dec 2020 08:37:14 +0100
2acfc6
Subject: [PATCH 3/9] [context] Add dnf_repo_conf_from_gkeyfile() and
2acfc6
 dnf_repo_conf_reset()
2acfc6
2acfc6
dnf_repo_conf_from_gkeyfile():
2acfc6
The function reloads repository configuration from GKeyFile.
2acfc6
2acfc6
dnf_repo_conf_reset():
2acfc6
Resets repository configuration options previously readed from repository
2acfc6
configuration file to initial state.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++
2acfc6
 1 file changed, 64 insertions(+)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index 00f4bbf7b..9f283df55 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -936,6 +936,70 @@ dnf_repo_get_boolean(GKeyFile *keyfile,
2acfc6
     return false;
2acfc6
 }
2acfc6
 
2acfc6
+/* Resets repository configuration options previously readed from repository
2acfc6
+ * configuration file to initial state. */
2acfc6
+static void
2acfc6
+dnf_repo_conf_reset(libdnf::ConfigRepo &config)
2acfc6
+{
2acfc6
+    for (auto & item : config.optBinds()) {
2acfc6
+        auto & itemOption = item.second;
2acfc6
+        if (itemOption.getPriority() == libdnf::Option::Priority::REPOCONFIG) {
2acfc6
+            itemOption.getOption().reset();
2acfc6
+        }
2acfc6
+    }
2acfc6
+}
2acfc6
+
2acfc6
+/* Loads repository configuration from GKeyFile */
2acfc6
+static void
2acfc6
+dnf_repo_conf_from_gkeyfile(libdnf::ConfigRepo &config, const char *repoId, GKeyFile *gkeyFile)
2acfc6
+{
2acfc6
+    // Reset to the initial state before reloading the configuration.
2acfc6
+    dnf_repo_conf_reset(config);
2acfc6
+
2acfc6
+    g_auto(GStrv) keys = g_key_file_get_keys(gkeyFile, repoId, NULL, NULL);
2acfc6
+    for (auto it = keys; *it != NULL; ++it) {
2acfc6
+        auto key = *it;
2acfc6
+        g_autofree gchar *str = g_key_file_get_value(gkeyFile, repoId, key, NULL);
2acfc6
+        if (str) {
2acfc6
+            try {
2acfc6
+                auto & optionItem = config.optBinds().at(key);
2acfc6
+
2acfc6
+                if (dynamic_cast<libdnf::OptionStringList*>(&optionItem.getOption()) ||
2acfc6
+                    dynamic_cast<libdnf::OptionChild<libdnf::OptionStringList>*>(&optionItem.getOption())
2acfc6
+                ) {
2acfc6
+
2acfc6
+                    // reload list option from gKeyFile using g_key_file_get_string_list()
2acfc6
+                    // g_key_file_get_value () is problematic for multiline lists
2acfc6
+                    g_auto(GStrv) list = g_key_file_get_string_list(gkeyFile, repoId, key, NULL, NULL);
2acfc6
+                    if (list) {
2acfc6
+                        // list can be ['value1', 'value2, value3'] therefore we first join
2acfc6
+                        // to have 'value1, value2, value3'
2acfc6
+                        g_autofree gchar * tmp_strval = g_strjoinv(",", list);
2acfc6
+                        try {
2acfc6
+                            optionItem.newString(libdnf::Option::Priority::REPOCONFIG, tmp_strval);
2acfc6
+                        } catch (const std::exception & ex) {
2acfc6
+                            g_debug("Invalid configuration value: %s = %s in %s; %s", key, str, repoId, ex.what());
2acfc6
+                        }
2acfc6
+                    }
2acfc6
+
2acfc6
+                } else {
2acfc6
+
2acfc6
+                    // process other (non list) options
2acfc6
+                    try {
2acfc6
+                        optionItem.newString(libdnf::Option::Priority::REPOCONFIG, str);
2acfc6
+                    } catch (const std::exception & ex) {
2acfc6
+                        g_debug("Invalid configuration value: %s = %s in %s; %s", key, str, repoId, ex.what());
2acfc6
+                    }
2acfc6
+
2acfc6
+                }
2acfc6
+
2acfc6
+            } catch (const std::exception &) {
2acfc6
+                g_debug("Unknown configuration option: %s = %s in %s", key, str, repoId);
2acfc6
+            }
2acfc6
+        }
2acfc6
+    }
2acfc6
+}
2acfc6
+
2acfc6
 /* Initialize (or potentially reset) repo & LrHandle from keyfile values. */
2acfc6
 static gboolean
2acfc6
 dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
2acfc6
From 5f1c06a66fcdb2c2340c11c07c5ba0ea3abf4b77 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Wed, 2 Dec 2020 11:37:26 +0100
2acfc6
Subject: [PATCH 4/9] [context] Use dnf_repo_conf_from_gkeyfile() for repo
2acfc6
 configuration reload
2acfc6
2acfc6
The dnf_repo_set_key_file_data() uses dnf_repo_conf_from_gkeyfile() now.
2acfc6
All occurrences of the direct use 'repo->getConfig()->.*set' and
2acfc6
newString() were removed.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 121 +++++++++-----------------------------------
2acfc6
 1 file changed, 25 insertions(+), 96 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index 9f283df55..2837580f7 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -1006,7 +1006,6 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
 {
2acfc6
     DnfRepoPrivate *priv = GET_PRIVATE(repo);
2acfc6
     guint cost;
2acfc6
-    gboolean module_hotfixes = false;
2acfc6
     g_autofree gchar *metadata_expire_str = NULL;
2acfc6
     g_autofree gchar *mirrorlist = NULL;
2acfc6
     g_autofree gchar *mirrorlisturl = NULL;
2acfc6
@@ -1016,48 +1015,28 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     g_autofree gchar *usr = NULL;
2acfc6
     g_autofree gchar *usr_pwd = NULL;
2acfc6
     g_autofree gchar *usr_pwd_proxy = NULL;
2acfc6
-    g_auto(GStrv) baseurls;
2acfc6
 
2acfc6
     auto repoId = priv->repo->getId().c_str();
2acfc6
     g_debug("setting keyfile data for %s", repoId);
2acfc6
 
2acfc6
-    /* skip_if_unavailable is optional */
2acfc6
-    if (g_key_file_has_key(priv->keyfile, repoId, "skip_if_unavailable", NULL)) {
2acfc6
-        bool skip = dnf_repo_get_boolean(priv->keyfile, repoId, "skip_if_unavailable");
2acfc6
-        priv->repo->getConfig()->skip_if_unavailable().set(libdnf::Option::Priority::REPOCONFIG, skip);
2acfc6
-    }
2acfc6
+    auto conf = priv->repo->getConfig();
2acfc6
 
2acfc6
-    /* priority is optional */
2acfc6
-    g_autofree gchar * priority_str = g_key_file_get_string(priv->keyfile, repoId, "priority", NULL);
2acfc6
-    if (priority_str) {
2acfc6
-        priv->repo->getConfig()->priority().set(libdnf::Option::Priority::REPOCONFIG, priority_str);
2acfc6
-    }
2acfc6
+    // Reload repository configuration from keyfile.
2acfc6
+    dnf_repo_conf_from_gkeyfile(*conf, repoId, priv->keyfile);
2acfc6
 
2acfc6
     /* cost is optional */
2acfc6
     cost = g_key_file_get_integer(priv->keyfile, repoId, "cost", NULL);
2acfc6
     if (cost != 0)
2acfc6
         dnf_repo_set_cost(repo, cost);
2acfc6
 
2acfc6
-    module_hotfixes = g_key_file_get_boolean(priv->keyfile, repoId, "module_hotfixes", NULL);
2acfc6
-    priv->repo->getConfig()->module_hotfixes().set(libdnf::Option::Priority::REPOCONFIG, module_hotfixes);
2acfc6
-
2acfc6
     /* baseurl is optional; if missing, unset it */
2acfc6
-    baseurls = g_key_file_get_string_list(priv->keyfile, repoId, "baseurl", NULL, NULL);
2acfc6
-    if (baseurls) {
2acfc6
-        // baseruls can be ['value1', 'value2, value3'] therefore we first join to have 'value1, value2, value3'
2acfc6
-        g_autofree gchar * tmp_strval = g_strjoinv(",", baseurls);
2acfc6
-
2acfc6
-        auto & bindBaseurls = priv->repo->getConfig()->optBinds().at("baseurl");
2acfc6
-        bindBaseurls.newString(libdnf::Option::Priority::REPOCONFIG, tmp_strval);
2acfc6
-
2acfc6
-        auto & repoBaseurls = priv->repo->getConfig()->baseurl();
2acfc6
-        if (!repoBaseurls.getValue().empty()){
2acfc6
-            auto len = repoBaseurls.getValue().size();
2acfc6
-            g_strfreev(baseurls);
2acfc6
-            baseurls = g_new0(char *, len + 1);
2acfc6
-            for (size_t i = 0; i < len; ++i) {
2acfc6
-                baseurls[i] = g_strdup(repoBaseurls.getValue()[i].c_str());
2acfc6
-            }
2acfc6
+    g_auto(GStrv) baseurls = NULL;
2acfc6
+    auto & repoBaseurls = conf->baseurl().getValue();
2acfc6
+    if (!repoBaseurls.empty()){
2acfc6
+        auto len = repoBaseurls.size();
2acfc6
+        baseurls = g_new0(char *, len + 1);
2acfc6
+        for (size_t i = 0; i < len; ++i) {
2acfc6
+            baseurls[i] = g_strdup(repoBaseurls[i].c_str());
2acfc6
         }
2acfc6
     }
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_URLS, baseurls))
2acfc6
@@ -1093,18 +1072,6 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_METALINKURL, metalinkurl))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
-    /* needed in order for addCountmeFlag() to use the same persistdir as DNF
2acfc6
-     * would */
2acfc6
-    if (metalinkurl)
2acfc6
-        priv->repo->getConfig()->metalink().set(libdnf::Option::Priority::REPOCONFIG, metalinkurl);
2acfc6
-    if (mirrorlisturl)
2acfc6
-        priv->repo->getConfig()->mirrorlist().set(libdnf::Option::Priority::REPOCONFIG, mirrorlisturl);
2acfc6
-
2acfc6
-    if (g_key_file_has_key(priv->keyfile, repoId, "countme", NULL)) {
2acfc6
-        bool countme = dnf_repo_get_boolean(priv->keyfile, repoId, "countme");
2acfc6
-        priv->repo->getConfig()->countme().set(libdnf::Option::Priority::REPOCONFIG, countme);
2acfc6
-    }
2acfc6
-
2acfc6
     /* file:// */
2acfc6
     if (baseurls != NULL && baseurls[0] != NULL &&
2acfc6
         mirrorlisturl == NULL && metalinkurl == NULL) {
2acfc6
@@ -1150,42 +1117,20 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
         dnf_repo_set_location_tmp(repo, tmp->str);
2acfc6
     }
2acfc6
 
2acfc6
-    /* gpgkey is optional for gpgcheck=1, but required for repo_gpgcheck=1 */
2acfc6
+    // Sync priv->gpgkeys
2acfc6
     g_strfreev(priv->gpgkeys);
2acfc6
-    priv->gpgkeys = NULL;
2acfc6
-
2acfc6
-    g_auto(GStrv) gpgkeys;
2acfc6
-    gpgkeys = g_key_file_get_string_list(priv->keyfile, repoId, "gpgkey", NULL, NULL);
2acfc6
-
2acfc6
-    if (gpgkeys) {
2acfc6
-        // gpgkeys can be ['value1', 'value2, value3'] therefore we first join to have 'value1, value2, value3'
2acfc6
-        g_autofree gchar * tmp_strval = g_strjoinv(",", gpgkeys);
2acfc6
-
2acfc6
-        auto & bindGpgkeys = priv->repo->getConfig()->optBinds().at("gpgkey");
2acfc6
-        bindGpgkeys.newString(libdnf::Option::Priority::REPOCONFIG, tmp_strval);
2acfc6
-
2acfc6
-        auto & repoGpgkeys = priv->repo->getConfig()->gpgkey();
2acfc6
-        if (!repoGpgkeys.getValue().empty()){
2acfc6
-            auto len = repoGpgkeys.getValue().size();
2acfc6
-            priv->gpgkeys = g_new0(char *, len + 1);
2acfc6
-            for (size_t i = 0; i < len; ++i) {
2acfc6
-                priv->gpgkeys[i] = g_strdup(repoGpgkeys.getValue()[i].c_str());
2acfc6
-            }
2acfc6
-        } else {
2acfc6
-            /* Canonicalize the empty list to NULL for ease of checking elsewhere */
2acfc6
-            g_strfreev(static_cast<gchar **>(g_steal_pointer(&priv->gpgkeys)));
2acfc6
+    auto & repoGpgkeys = conf->gpgkey().getValue();
2acfc6
+    if (!repoGpgkeys.empty()){
2acfc6
+        auto len = repoGpgkeys.size();
2acfc6
+        priv->gpgkeys = g_new0(char *, len + 1);
2acfc6
+        for (size_t i = 0; i < len; ++i) {
2acfc6
+            priv->gpgkeys[i] = g_strdup(repoGpgkeys[i].c_str());
2acfc6
         }
2acfc6
+    } else {
2acfc6
+        priv->gpgkeys = NULL;
2acfc6
     }
2acfc6
 
2acfc6
-    if (g_key_file_has_key(priv->keyfile, repoId, "gpgcheck", NULL)) {
2acfc6
-        auto gpgcheck_pkgs = dnf_repo_get_boolean(priv->keyfile, repoId, "gpgcheck");
2acfc6
-        priv->repo->getConfig()->gpgcheck().set(libdnf::Option::Priority::REPOCONFIG, gpgcheck_pkgs);
2acfc6
-    }
2acfc6
-
2acfc6
-    if (g_key_file_has_key(priv->keyfile, repoId, "repo_gpgcheck", NULL)) {
2acfc6
-        auto gpgcheck_md = dnf_repo_get_boolean(priv->keyfile, repoId, "repo_gpgcheck");
2acfc6
-        priv->repo->getConfig()->repo_gpgcheck().set(libdnf::Option::Priority::REPOCONFIG, gpgcheck_md);
2acfc6
-    }
2acfc6
+    /* gpgkey is optional for gpgcheck=1, but required for repo_gpgcheck=1 */
2acfc6
     auto gpgcheck_md = priv->repo->getConfig()->repo_gpgcheck().getValue();
2acfc6
     if (gpgcheck_md && priv->gpgkeys == NULL) {
2acfc6
         g_set_error_literal(error,
2acfc6
@@ -1199,35 +1144,19 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_GPGCHECK, (long)gpgcheck_md))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
-    auto & repoExcludepkgs = priv->repo->getConfig()->excludepkgs();
2acfc6
-    repoExcludepkgs.set(libdnf::Option::Priority::REPOCONFIG, "");
2acfc6
-
2acfc6
-    auto & bindExcludepkgs = priv->repo->getConfig()->optBinds().at("excludepkgs");
2acfc6
-    if (auto excludepkgs = g_key_file_get_string(priv->keyfile, repoId, "exclude", NULL)) {
2acfc6
-        bindExcludepkgs.newString(libdnf::Option::Priority::REPOCONFIG, excludepkgs);
2acfc6
-        g_free(excludepkgs);
2acfc6
-    }
2acfc6
-    if (auto excludepkgs = g_key_file_get_string(priv->keyfile, repoId, "excludepkgs", NULL)) {
2acfc6
-        bindExcludepkgs.newString(libdnf::Option::Priority::REPOCONFIG, excludepkgs);
2acfc6
-        g_free(excludepkgs);
2acfc6
-    }
2acfc6
-
2acfc6
+    // Sync priv->exclude_packages
2acfc6
     g_strfreev(priv->exclude_packages);
2acfc6
-    if (!repoExcludepkgs.getValue().empty()) {
2acfc6
-        auto len = repoExcludepkgs.getValue().size();
2acfc6
+    auto & repoExcludepkgs = conf->excludepkgs().getValue();
2acfc6
+    if (!repoExcludepkgs.empty()) {
2acfc6
+        auto len = repoExcludepkgs.size();
2acfc6
         priv->exclude_packages = g_new0(char *, len + 1);
2acfc6
         for (size_t i = 0; i < len; ++i) {
2acfc6
-            priv->exclude_packages[i] = g_strdup(repoExcludepkgs.getValue()[i].c_str());
2acfc6
+            priv->exclude_packages[i] = g_strdup(repoExcludepkgs[i].c_str());
2acfc6
         }
2acfc6
     } else {
2acfc6
         priv->exclude_packages = NULL;
2acfc6
     }
2acfc6
 
2acfc6
-    if (auto includepkgs = g_key_file_get_string(priv->keyfile, repoId, "includepkgs", NULL)) {
2acfc6
-        priv->repo->getConfig()->includepkgs().set(libdnf::Option::Priority::REPOCONFIG, includepkgs);
2acfc6
-        g_free(includepkgs);
2acfc6
-    }
2acfc6
-
2acfc6
     /* proxy is optional */
2acfc6
     proxy = g_key_file_get_string(priv->keyfile, repoId, "proxy", NULL);
2acfc6
     auto repoProxy = proxy ? (strcasecmp(proxy, "_none_") == 0 ? NULL : proxy)
2acfc6
2acfc6
From c6afbb4f93eee480c68201297e9c5c7afdf05dd3 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Wed, 2 Dec 2020 13:26:51 +0100
2acfc6
Subject: [PATCH 5/9] [context] Fix: "cost" and  "metadata_expire" repository
2acfc6
 options
2acfc6
2acfc6
Changes in dnf_repo_set_keyfile_data():
2acfc6
Removed the dnf_repo_set_cost() call.
2acfc6
Removed the "metadata_expire" parsing and dnf_repo_set_metadata_expire() call.
2acfc6
2acfc6
The options were set earlier. The function calls were redundant and
2acfc6
set the priority to the wrong RUNTIME value.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 103 --------------------------------------------
2acfc6
 1 file changed, 103 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index 2837580f7..61d496750 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -816,93 +816,6 @@ dnf_repo_set_metadata_expire(DnfRepo *repo, guint metadata_expire)
2acfc6
     priv->repo->getConfig()->metadata_expire().set(libdnf::Option::Priority::RUNTIME, metadata_expire);
2acfc6
 }
2acfc6
 
2acfc6
-/**
2acfc6
- *  dnf_repo_parse_time_from_str
2acfc6
- *  @expression: a expression to be parsed
2acfc6
- *  @out_parsed_time: (out): return location for parsed time
2acfc6
- *  @error: error item
2acfc6
- *
2acfc6
- *  Parse String into an integer value of seconds, or a human
2acfc6
- *  readable variation specifying days, hours, minutes or seconds
2acfc6
- *  until something happens. Note that due to historical president
2acfc6
- *  -1 means "never", so this accepts that and allows
2acfc6
- *  the word never, too.
2acfc6
- *
2acfc6
- *  Valid inputs: 100, 1.5m, 90s, 1.2d, 1d, 0xF, 0.1, -1, never.
2acfc6
- *  Invalid inputs: -10, -0.1, 45.6Z, 1d6h, 1day, 1y.
2acfc6
-
2acfc6
- *  Returns: integer value in seconds
2acfc6
- **/
2acfc6
-
2acfc6
-static gboolean
2acfc6
-dnf_repo_parse_time_from_str(const gchar *expression, guint *out_parsed_time, GError **error)
2acfc6
-{
2acfc6
-    gint multiplier;
2acfc6
-    gdouble parsed_time;
2acfc6
-    gchar *endptr = NULL;
2acfc6
-
2acfc6
-    if (!g_strcmp0(expression, "")) {
2acfc6
-        g_set_error_literal(error,
2acfc6
-                            DNF_ERROR,
2acfc6
-                            DNF_ERROR_FILE_INVALID,
2acfc6
-                            "no metadata value specified");
2acfc6
-        return FALSE;
2acfc6
-    }
2acfc6
-
2acfc6
-    if (g_strcmp0(expression, "-1") == 0 ||
2acfc6
-        g_strcmp0(expression,"never") == 0) {
2acfc6
-        *out_parsed_time = G_MAXUINT;
2acfc6
-        return TRUE; /* Note early return */
2acfc6
-    }
2acfc6
-
2acfc6
-    gchar last_char = expression[ strlen(expression) - 1 ];
2acfc6
-
2acfc6
-    /* check if the input ends with h, m ,d ,s as units */
2acfc6
-    if (g_ascii_isalpha(last_char)) {
2acfc6
-        if (last_char == 'h')
2acfc6
-            multiplier = 60 * 60;
2acfc6
-        else if (last_char == 's')
2acfc6
-            multiplier = 1;
2acfc6
-        else if (last_char == 'm')
2acfc6
-            multiplier = 60;
2acfc6
-        else if (last_char == 'd')
2acfc6
-            multiplier = 60 * 60 * 24;
2acfc6
-        else {
2acfc6
-            g_set_error(error, DNF_ERROR, DNF_ERROR_FILE_INVALID,
2acfc6
-                        "unknown unit %c", last_char);
2acfc6
-            return FALSE;
2acfc6
-        }
2acfc6
-    }
2acfc6
-    else
2acfc6
-        multiplier = 1;
2acfc6
-
2acfc6
-    /* convert expression into a double*/
2acfc6
-    parsed_time = g_ascii_strtod(expression, &endptr);
2acfc6
-
2acfc6
-    /* failed to parse */
2acfc6
-    if (expression == endptr) {
2acfc6
-        g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
2acfc6
-                    "failed to parse time: %s", expression);
2acfc6
-        return FALSE;
2acfc6
-    }
2acfc6
-
2acfc6
-    /* time can not be below zero */
2acfc6
-    if (parsed_time < 0) {
2acfc6
-        g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
2acfc6
-                    "seconds value must not be negative %s",expression );
2acfc6
-        return FALSE;
2acfc6
-    }
2acfc6
-
2acfc6
-    /* time too large */
2acfc6
-    if (parsed_time > G_MAXDOUBLE || (parsed_time * multiplier) > G_MAXUINT){
2acfc6
-        g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
2acfc6
-                    "time too large");
2acfc6
-        return FALSE;
2acfc6
-    }
2acfc6
-
2acfc6
-    *out_parsed_time = (guint) (parsed_time * multiplier);
2acfc6
-    return TRUE;
2acfc6
-}
2acfc6
 /**
2acfc6
  * dnf_repo_get_username_password_string:
2acfc6
  */
2acfc6
@@ -1005,8 +918,6 @@ static gboolean
2acfc6
 dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
 {
2acfc6
     DnfRepoPrivate *priv = GET_PRIVATE(repo);
2acfc6
-    guint cost;
2acfc6
-    g_autofree gchar *metadata_expire_str = NULL;
2acfc6
     g_autofree gchar *mirrorlist = NULL;
2acfc6
     g_autofree gchar *mirrorlisturl = NULL;
2acfc6
     g_autofree gchar *metalinkurl = NULL;
2acfc6
@@ -1024,11 +935,6 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     // Reload repository configuration from keyfile.
2acfc6
     dnf_repo_conf_from_gkeyfile(*conf, repoId, priv->keyfile);
2acfc6
 
2acfc6
-    /* cost is optional */
2acfc6
-    cost = g_key_file_get_integer(priv->keyfile, repoId, "cost", NULL);
2acfc6
-    if (cost != 0)
2acfc6
-        dnf_repo_set_cost(repo, cost);
2acfc6
-
2acfc6
     /* baseurl is optional; if missing, unset it */
2acfc6
     g_auto(GStrv) baseurls = NULL;
2acfc6
     auto & repoBaseurls = conf->baseurl().getValue();
2acfc6
@@ -1042,15 +948,6 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_URLS, baseurls))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
-    /* metadata_expire is optional, if shown, we parse the string to add the time */
2acfc6
-    metadata_expire_str = g_key_file_get_string(priv->keyfile, repoId, "metadata_expire", NULL);
2acfc6
-    if (metadata_expire_str) {
2acfc6
-        guint metadata_expire;
2acfc6
-        if (!dnf_repo_parse_time_from_str(metadata_expire_str, &metadata_expire, error))
2acfc6
-            return FALSE;
2acfc6
-        dnf_repo_set_metadata_expire(repo, metadata_expire);
2acfc6
-    }
2acfc6
-
2acfc6
     /* the "mirrorlist" entry could be either a real mirrorlist, or a metalink entry */
2acfc6
     mirrorlist = g_key_file_get_string(priv->keyfile, repoId, "mirrorlist", NULL);
2acfc6
     if (mirrorlist) {
2acfc6
2acfc6
From b11ac5204dc4c7048a7b6880813f2f9b1d8eb242 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Wed, 2 Dec 2020 13:21:35 +0100
2acfc6
Subject: [PATCH 6/9] [context] Fix: username, password, proxy, proxy_username,
2acfc6
 proxy_password
2acfc6
2acfc6
- Uses global configuration options when they are not defined
2acfc6
  in the repository configuration.
2acfc6
- proxy_username and proxy_password is urlEncoded before passing to librepo.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 78 ++++++++++++++++++++++++++-------------------
2acfc6
 1 file changed, 46 insertions(+), 32 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index 61d496750..005721ef6 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -817,18 +817,22 @@ dnf_repo_set_metadata_expire(DnfRepo *repo, guint metadata_expire)
2acfc6
 }
2acfc6
 
2acfc6
 /**
2acfc6
- * dnf_repo_get_username_password_string:
2acfc6
- */
2acfc6
-static gchar *
2acfc6
-dnf_repo_get_username_password_string(const gchar *user, const gchar *pass)
2acfc6
-{
2acfc6
-    if (user == NULL && pass == NULL)
2acfc6
-        return NULL;
2acfc6
-    if (user != NULL && pass == NULL)
2acfc6
-        return g_strdup(user);
2acfc6
-    if (user == NULL && pass != NULL)
2acfc6
-        return g_strdup_printf(":%s", pass);
2acfc6
-    return g_strdup_printf("%s:%s", user, pass);
2acfc6
+* @brief Format user password string
2acfc6
+*
2acfc6
+* Returns user and password in user:password form. If encode is True,
2acfc6
+* special characters in user and password are URL encoded.
2acfc6
+*
2acfc6
+* @param user Username
2acfc6
+* @param passwd Password
2acfc6
+* @param encode If quote is True, special characters in user and password are URL encoded.
2acfc6
+* @return User and password in user:password form
2acfc6
+*/
2acfc6
+static std::string formatUserPassString(const std::string & user, const std::string & passwd, bool encode)
2acfc6
+{
2acfc6
+    if (encode)
2acfc6
+        return libdnf::urlEncode(user) + ":" + libdnf::urlEncode(passwd);
2acfc6
+    else
2acfc6
+        return user + ":" + passwd;
2acfc6
 }
2acfc6
 
2acfc6
 static gboolean
2acfc6
@@ -921,11 +925,8 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     g_autofree gchar *mirrorlist = NULL;
2acfc6
     g_autofree gchar *mirrorlisturl = NULL;
2acfc6
     g_autofree gchar *metalinkurl = NULL;
2acfc6
-    g_autofree gchar *proxy = NULL;
2acfc6
-    g_autofree gchar *pwd = NULL;
2acfc6
-    g_autofree gchar *usr = NULL;
2acfc6
-    g_autofree gchar *usr_pwd = NULL;
2acfc6
-    g_autofree gchar *usr_pwd_proxy = NULL;
2acfc6
+    std::string tmp_str;
2acfc6
+    const char *tmp_cstr;
2acfc6
 
2acfc6
     auto repoId = priv->repo->getId().c_str();
2acfc6
     g_debug("setting keyfile data for %s", repoId);
2acfc6
@@ -1054,26 +1055,39 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
         priv->exclude_packages = NULL;
2acfc6
     }
2acfc6
 
2acfc6
-    /* proxy is optional */
2acfc6
-    proxy = g_key_file_get_string(priv->keyfile, repoId, "proxy", NULL);
2acfc6
-    auto repoProxy = proxy ? (strcasecmp(proxy, "_none_") == 0 ? NULL : proxy)
2acfc6
-        : dnf_context_get_http_proxy(priv->context);
2acfc6
-    if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXY, repoProxy))
2acfc6
+    tmp_str = conf->proxy().getValue();
2acfc6
+    tmp_cstr = tmp_str.empty() ? dnf_context_get_http_proxy(priv->context) : tmp_str.c_str();
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXY, tmp_cstr))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
-    /* both parts of the proxy auth are optional */
2acfc6
-    usr = g_key_file_get_string(priv->keyfile, repoId, "proxy_username", NULL);
2acfc6
-    pwd = g_key_file_get_string(priv->keyfile, repoId, "proxy_password", NULL);
2acfc6
-    usr_pwd_proxy = dnf_repo_get_username_password_string(usr, pwd);
2acfc6
-    if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXYUSERPWD, usr_pwd_proxy))
2acfc6
+    // setup proxy username and password
2acfc6
+    tmp_cstr = NULL;
2acfc6
+    if (!conf->proxy_username().empty()) {
2acfc6
+        tmp_str = conf->proxy_username().getValue();
2acfc6
+        if (!tmp_str.empty()) {
2acfc6
+            if (conf->proxy_password().empty()) {
2acfc6
+                g_set_error(error, DNF_ERROR, DNF_ERROR_FILE_INVALID,
2acfc6
+                            "repo '%s': 'proxy_username' is set but not 'proxy_password'", repoId);
2acfc6
+                return FALSE;
2acfc6
+            }
2acfc6
+            tmp_str = formatUserPassString(tmp_str, conf->proxy_password().getValue(), true);
2acfc6
+            tmp_cstr = tmp_str.c_str();
2acfc6
+        }
2acfc6
+    }
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXYUSERPWD, tmp_cstr))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
-    /* both parts of the HTTP auth are optional */
2acfc6
-    usr = g_key_file_get_string(priv->keyfile, repoId, "username", NULL);
2acfc6
-    pwd = g_key_file_get_string(priv->keyfile, repoId, "password", NULL);
2acfc6
-    usr_pwd = dnf_repo_get_username_password_string(usr, pwd);
2acfc6
-    if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERPWD, usr_pwd))
2acfc6
+    // setup username and password
2acfc6
+    tmp_cstr = NULL;
2acfc6
+    tmp_str = conf->username().getValue();
2acfc6
+    if (!tmp_str.empty()) {
2acfc6
+        // TODO Use URL encoded form, needs support in librepo
2acfc6
+        tmp_str = formatUserPassString(tmp_str, conf->password().getValue(), false);
2acfc6
+        tmp_cstr = tmp_str.c_str();
2acfc6
+    }
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERPWD, tmp_cstr))
2acfc6
         return FALSE;
2acfc6
+
2acfc6
     return TRUE;
2acfc6
 }
2acfc6
 
2acfc6
2acfc6
From fd07b29ccaec2648cfc050122e16e4846d7ac4be Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Wed, 2 Dec 2020 13:53:45 +0100
2acfc6
Subject: [PATCH 7/9] [context] Add support for options: minrate, throttle,
2acfc6
 bandwidth, timeout
2acfc6
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 29 +++++++++++++++++++++++++++++
2acfc6
 1 file changed, 29 insertions(+)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index 005721ef6..c6dd027be 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -1055,6 +1055,35 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
         priv->exclude_packages = NULL;
2acfc6
     }
2acfc6
 
2acfc6
+    auto minrate = conf->minrate().getValue();
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_LOWSPEEDLIMIT, static_cast<long>(minrate)))
2acfc6
+        return FALSE;
2acfc6
+
2acfc6
+    auto maxspeed = conf->throttle().getValue();
2acfc6
+    if (maxspeed > 0 && maxspeed <= 1)
2acfc6
+        maxspeed *= conf->bandwidth().getValue();
2acfc6
+    if (maxspeed != 0 && maxspeed < minrate) {
2acfc6
+        g_set_error_literal(error, DNF_ERROR, DNF_ERROR_FILE_INVALID,
2acfc6
+                            "Maximum download speed is lower than minimum. "
2acfc6
+                            "Please change configuration of minrate or throttle");
2acfc6
+        return FALSE;
2acfc6
+    }
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_MAXSPEED, static_cast<int64_t>(maxspeed)))
2acfc6
+        return FALSE;
2acfc6
+
2acfc6
+    long timeout = conf->timeout().getValue();
2acfc6
+    if (timeout > 0) {
2acfc6
+        if (!lr_handle_setopt(priv->repo_handle, error, LRO_CONNECTTIMEOUT, timeout))
2acfc6
+            return FALSE;
2acfc6
+        if (!lr_handle_setopt(priv->repo_handle, error, LRO_LOWSPEEDTIME, timeout))
2acfc6
+            return FALSE;
2acfc6
+    } else {
2acfc6
+        if (!lr_handle_setopt(priv->repo_handle, error, LRO_CONNECTTIMEOUT, LRO_CONNECTTIMEOUT_DEFAULT))
2acfc6
+            return FALSE;
2acfc6
+        if (!lr_handle_setopt(priv->repo_handle, error, LRO_LOWSPEEDTIME, LRO_LOWSPEEDTIME_DEFAULT))
2acfc6
+            return FALSE;
2acfc6
+    }
2acfc6
+
2acfc6
     tmp_str = conf->proxy().getValue();
2acfc6
     tmp_cstr = tmp_str.empty() ? dnf_context_get_http_proxy(priv->context) : tmp_str.c_str();
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXY, tmp_cstr))
2acfc6
2acfc6
From c484de218699dff834fb32133cf502b2d0c64162 Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Thu, 3 Dec 2020 10:55:02 +0100
2acfc6
Subject: [PATCH 8/9] [context] Remove g_key_file_get_string() from
2acfc6
 dnf_repo_set_keyfile_data()
2acfc6
2acfc6
Removes the remaining usage of g_key_file_get_string() from
2acfc6
dnf_repo_set_keyfile_data(). Use the values from ConfigRepo instead.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 23 +++++++++++------------
2acfc6
 1 file changed, 11 insertions(+), 12 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index c6dd027be..c5c50d55c 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -922,9 +922,6 @@ static gboolean
2acfc6
 dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
 {
2acfc6
     DnfRepoPrivate *priv = GET_PRIVATE(repo);
2acfc6
-    g_autofree gchar *mirrorlist = NULL;
2acfc6
-    g_autofree gchar *mirrorlisturl = NULL;
2acfc6
-    g_autofree gchar *metalinkurl = NULL;
2acfc6
     std::string tmp_str;
2acfc6
     const char *tmp_cstr;
2acfc6
 
2acfc6
@@ -949,20 +946,22 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_URLS, baseurls))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
+    const char *mirrorlisturl = NULL;
2acfc6
+    const char *metalinkurl = NULL;
2acfc6
+
2acfc6
     /* the "mirrorlist" entry could be either a real mirrorlist, or a metalink entry */
2acfc6
-    mirrorlist = g_key_file_get_string(priv->keyfile, repoId, "mirrorlist", NULL);
2acfc6
-    if (mirrorlist) {
2acfc6
-        if (strstr(mirrorlist, "metalink"))
2acfc6
-            metalinkurl = static_cast<gchar *>(g_steal_pointer(&mirrorlist));
2acfc6
+    tmp_cstr = conf->mirrorlist().empty() ? NULL : conf->mirrorlist().getValue().c_str();
2acfc6
+    if (tmp_cstr) {
2acfc6
+        if (strstr(tmp_cstr, "metalink"))
2acfc6
+            metalinkurl = tmp_cstr;
2acfc6
         else /* it really is a mirrorlist */
2acfc6
-            mirrorlisturl = static_cast<gchar *>(g_steal_pointer(&mirrorlist));
2acfc6
+            mirrorlisturl = tmp_cstr;
2acfc6
     }
2acfc6
 
2acfc6
     /* let "metalink" entry override metalink-as-mirrorlist entry */
2acfc6
-    if (g_key_file_has_key(priv->keyfile, repoId, "metalink", NULL)) {
2acfc6
-        g_free(metalinkurl);
2acfc6
-        metalinkurl = g_key_file_get_string(priv->keyfile, repoId, "metalink", NULL);
2acfc6
-    }
2acfc6
+    tmp_cstr = conf->metalink().empty() ? NULL : conf->metalink().getValue().c_str();
2acfc6
+    if (tmp_cstr)
2acfc6
+        metalinkurl = tmp_cstr;
2acfc6
 
2acfc6
     /* now set the final values (or unset them) */
2acfc6
     if (!lr_handle_setopt(priv->repo_handle, error, LRO_MIRRORLISTURL, mirrorlisturl))
2acfc6
2acfc6
From ce44d3dced4b800e3b7f80556fac1daf7e7fa49d Mon Sep 17 00:00:00 2001
2acfc6
From: Jaroslav Rohel <jrohel@redhat.com>
2acfc6
Date: Thu, 3 Dec 2020 11:43:18 +0100
2acfc6
Subject: [PATCH 9/9] [context] Remove the extra gpgkey member from
2acfc6
 DnfRepoPrivate
2acfc6
2acfc6
The value stored in ConfigRepo can be used directly.
2acfc6
---
2acfc6
 libdnf/dnf-repo.cpp | 81 ++++++++++++++++++---------------------------
2acfc6
 1 file changed, 33 insertions(+), 48 deletions(-)
2acfc6
2acfc6
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
2acfc6
index c5c50d55c..193999902 100644
2acfc6
--- a/libdnf/dnf-repo.cpp
2acfc6
+++ b/libdnf/dnf-repo.cpp
2acfc6
@@ -63,7 +63,6 @@
2acfc6
 typedef struct
2acfc6
 {
2acfc6
     DnfRepoEnabled   enabled;
2acfc6
-    gchar          **gpgkeys;
2acfc6
     gchar          **exclude_packages;
2acfc6
     gchar           *filename;      /* /etc/yum.repos.d/updates.repo */
2acfc6
     gchar           *location;      /* /var/cache/PackageKit/metadata/fedora */
2acfc6
@@ -97,7 +96,6 @@ dnf_repo_finalize(GObject *object)
2acfc6
     DnfRepoPrivate *priv = GET_PRIVATE(repo);
2acfc6
 
2acfc6
     g_free(priv->filename);
2acfc6
-    g_strfreev(priv->gpgkeys);
2acfc6
     g_strfreev(priv->exclude_packages);
2acfc6
     g_free(priv->location_tmp);
2acfc6
     g_free(priv->location);
2acfc6
@@ -225,16 +223,13 @@ gchar **
2acfc6
 dnf_repo_get_public_keys(DnfRepo *repo)
2acfc6
 {
2acfc6
     DnfRepoPrivate *priv = GET_PRIVATE(repo);
2acfc6
-    g_autoptr(GPtrArray) ret = g_ptr_array_new();
2acfc6
-    for (char **iter = priv->gpgkeys; iter && *iter; iter++) {
2acfc6
-        const char *key = *iter;
2acfc6
-        g_autofree gchar *key_bn = g_path_get_basename(key);
2acfc6
-        /* transfer ownership to ptrarray */
2acfc6
-        g_ptr_array_add(ret, g_build_filename(priv->location, key_bn, NULL));
2acfc6
+    const auto & keys = priv->repo->getConfig()->gpgkey().getValue();
2acfc6
+    gchar **ret = g_new0(gchar *, keys.size() + 1);
2acfc6
+    for (size_t i = 0; i < keys.size(); ++i) {
2acfc6
+        g_autofree gchar *key_bn = g_path_get_basename(keys[i].c_str());
2acfc6
+        ret[i] = g_build_filename(priv->location, key_bn, NULL);
2acfc6
     }
2acfc6
-    g_ptr_array_add(ret, NULL);
2acfc6
-    /* transfer ownership of container and elements to caller */
2acfc6
-    return (gchar**)g_ptr_array_free(static_cast<GPtrArray *>(g_steal_pointer(&ret)), FALSE);
2acfc6
+    return ret;
2acfc6
 }
2acfc6
 
2acfc6
 /**
2acfc6
@@ -1014,22 +1009,9 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
         dnf_repo_set_location_tmp(repo, tmp->str);
2acfc6
     }
2acfc6
 
2acfc6
-    // Sync priv->gpgkeys
2acfc6
-    g_strfreev(priv->gpgkeys);
2acfc6
-    auto & repoGpgkeys = conf->gpgkey().getValue();
2acfc6
-    if (!repoGpgkeys.empty()){
2acfc6
-        auto len = repoGpgkeys.size();
2acfc6
-        priv->gpgkeys = g_new0(char *, len + 1);
2acfc6
-        for (size_t i = 0; i < len; ++i) {
2acfc6
-            priv->gpgkeys[i] = g_strdup(repoGpgkeys[i].c_str());
2acfc6
-        }
2acfc6
-    } else {
2acfc6
-        priv->gpgkeys = NULL;
2acfc6
-    }
2acfc6
-
2acfc6
     /* gpgkey is optional for gpgcheck=1, but required for repo_gpgcheck=1 */
2acfc6
-    auto gpgcheck_md = priv->repo->getConfig()->repo_gpgcheck().getValue();
2acfc6
-    if (gpgcheck_md && priv->gpgkeys == NULL) {
2acfc6
+    auto repo_gpgcheck = conf->repo_gpgcheck().getValue();
2acfc6
+    if (repo_gpgcheck && conf->gpgkey().getValue().empty()) {
2acfc6
         g_set_error_literal(error,
2acfc6
                             DNF_ERROR,
2acfc6
                             DNF_ERROR_FILE_INVALID,
2acfc6
@@ -1038,7 +1020,7 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
2acfc6
     }
2acfc6
 
2acfc6
     /* XXX: setopt() expects a long, so we need a long on the stack */
2acfc6
-    if (!lr_handle_setopt(priv->repo_handle, error, LRO_GPGCHECK, (long)gpgcheck_md))
2acfc6
+    if (!lr_handle_setopt(priv->repo_handle, error, LRO_GPGCHECK, (long)repo_gpgcheck))
2acfc6
         return FALSE;
2acfc6
 
2acfc6
     // Sync priv->exclude_packages
2acfc6
@@ -1750,28 +1732,31 @@ dnf_repo_update(DnfRepo *repo,
2acfc6
         goto out;
2acfc6
     }
2acfc6
 
2acfc6
-    if (priv->gpgkeys &&
2acfc6
-        (priv->repo->getConfig()->repo_gpgcheck().getValue() || priv->repo->getConfig()->gpgcheck().getValue())) {
2acfc6
-        for (char **iter = priv->gpgkeys; iter && *iter; iter++) {
2acfc6
-            const char *gpgkey = *iter;
2acfc6
-            g_autofree char *gpgkey_name = g_path_get_basename(gpgkey);
2acfc6
-            g_autofree char *key_tmp = g_build_filename(priv->location_tmp, gpgkey_name, NULL);
2acfc6
-
2acfc6
-            /* download and import public key */
2acfc6
-            if ((g_str_has_prefix(gpgkey, "https://") ||
2acfc6
-                  g_str_has_prefix(gpgkey, "file://"))) {
2acfc6
-                g_debug("importing public key %s", gpgkey);
2acfc6
-
2acfc6
-                ret = dnf_repo_download_import_public_key(repo, gpgkey, key_tmp, error);
2acfc6
-                if (!ret)
2acfc6
-                    goto out;
2acfc6
-            }
2acfc6
+    {
2acfc6
+        const auto & gpgkeys = priv->repo->getConfig()->gpgkey().getValue();
2acfc6
+        if (!gpgkeys.empty() &&
2acfc6
+            (priv->repo->getConfig()->repo_gpgcheck().getValue() || priv->repo->getConfig()->gpgcheck().getValue())) {
2acfc6
+            for (const auto & key : gpgkeys) {
2acfc6
+                const char *gpgkey = key.c_str();
2acfc6
+                g_autofree char *gpgkey_name = g_path_get_basename(gpgkey);
2acfc6
+                g_autofree char *key_tmp = g_build_filename(priv->location_tmp, gpgkey_name, NULL);
2acfc6
+
2acfc6
+                /* download and import public key */
2acfc6
+                if ((g_str_has_prefix(gpgkey, "https://") ||
2acfc6
+                    g_str_has_prefix(gpgkey, "file://"))) {
2acfc6
+                    g_debug("importing public key %s", gpgkey);
2acfc6
+
2acfc6
+                    ret = dnf_repo_download_import_public_key(repo, gpgkey, key_tmp, error);
2acfc6
+                    if (!ret)
2acfc6
+                        goto out;
2acfc6
+                }
2acfc6
 
2acfc6
-            /* do we autoimport this into librpm? */
2acfc6
-            if ((flags & DNF_REPO_UPDATE_FLAG_IMPORT_PUBKEY) > 0) {
2acfc6
-                ret = dnf_repo_add_public_key(repo, key_tmp, error);
2acfc6
-                if (!ret)
2acfc6
-                    goto out;
2acfc6
+                /* do we autoimport this into librpm? */
2acfc6
+                if ((flags & DNF_REPO_UPDATE_FLAG_IMPORT_PUBKEY) > 0) {
2acfc6
+                    ret = dnf_repo_add_public_key(repo, key_tmp, error);
2acfc6
+                    if (!ret)
2acfc6
+                        goto out;
2acfc6
+                }
2acfc6
             }
2acfc6
         }
2acfc6
     }