dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0037-context-dnf_context_remove-accepts-package-spec-as-d.patch

2c1f71
From 876393d5d0cd5f806415dcdc90168e58e66da916 Mon Sep 17 00:00:00 2001
2c1f71
From: Jaroslav Rohel <jrohel@redhat.com>
2c1f71
Date: Mon, 28 Mar 2022 07:29:48 +0200
2c1f71
Subject: [PATCH] context: dnf_context_remove accepts `<package-spec>` as dnf,
2c1f71
 unify code
2c1f71
2c1f71
Prior to change, the `dnf_context_remove` function only accepted
2c1f71
the package name (without globs). It was not possible to enter more detailed
2c1f71
specifications and thus, for example, select a specific version of the package
2c1f71
to uninstall - for example, which kernel we want to uninstall.
2c1f71
This patch adds full `<package-spec>` support as in dnf, including support
2c1f71
for globs (wildcards) and searching against 'provides' and 'file provides'.
2c1f71
2c1f71
Better error handling for `hy_goal_upgrade_selector` in` dnf_context_update`.
2c1f71
2c1f71
Unification of the function code `dnf_context_install`, `dnf_context_remove`,
2c1f71
`dnf_context_update`.
2c1f71
2c1f71
= changelog =
2c1f71
msg: context: Support <package-spec> (NEVRA forms, provides, file provides) including globs in the dnf_context_remove func
2c1f71
type: enhancement
2c1f71
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2084602
2c1f71
---
2c1f71
 libdnf/dnf-context.cpp | 46 ++++++++++++++++++++++++------------------
2c1f71
 1 file changed, 26 insertions(+), 20 deletions(-)
2c1f71
2c1f71
diff --git a/libdnf/dnf-context.cpp b/libdnf/dnf-context.cpp
2c1f71
index 6cb0011b..4b055f03 100644
2c1f71
--- a/libdnf/dnf-context.cpp
2c1f71
+++ b/libdnf/dnf-context.cpp
2c1f71
@@ -2391,10 +2391,9 @@ dnf_context_run(DnfContext *context, GCancellable *cancellable, GError **error)
2c1f71
  * Since: 0.1.0
2c1f71
  **/
2c1f71
 gboolean
2c1f71
-dnf_context_install (DnfContext *context, const gchar *name, GError **error) try
2c1f71
+dnf_context_install(DnfContext *context, const gchar *name, GError **error) try
2c1f71
 {
2c1f71
     DnfContextPrivate *priv = GET_PRIVATE (context);
2c1f71
-    g_autoptr(GPtrArray) selector_matches = NULL;
2c1f71
 
2c1f71
     /* create sack and add sources */
2c1f71
     if (priv->sack == NULL) {
2c1f71
@@ -2405,7 +2404,7 @@ dnf_context_install (DnfContext *context, const gchar *name, GError **error) try
2c1f71
 
2c1f71
     g_auto(HySubject) subject = hy_subject_create(name);
2c1f71
     g_auto(HySelector) selector = hy_subject_get_best_selector(subject, priv->sack, NULL, FALSE, NULL);
2c1f71
-    selector_matches = hy_selector_matches(selector);
2c1f71
+    g_autoptr(GPtrArray) selector_matches = hy_selector_matches(selector);
2c1f71
     if (selector_matches->len == 0) {
2c1f71
         g_set_error(error,
2c1f71
                     DNF_ERROR,
2c1f71
@@ -2438,31 +2437,33 @@ gboolean
2c1f71
 dnf_context_remove(DnfContext *context, const gchar *name, GError **error) try
2c1f71
 {
2c1f71
     DnfContextPrivate *priv = GET_PRIVATE(context);
2c1f71
-    GPtrArray *pkglist;
2c1f71
-    hy_autoquery HyQuery query = NULL;
2c1f71
-    gboolean ret = TRUE;
2c1f71
-    guint i;
2c1f71
 
2c1f71
     /* create sack and add repos */
2c1f71
     if (priv->sack == NULL) {
2c1f71
         dnf_state_reset(priv->state);
2c1f71
-        ret = dnf_context_setup_sack(context, priv->state, error);
2c1f71
-        if (!ret)
2c1f71
+        if (!dnf_context_setup_sack(context, priv->state, error))
2c1f71
             return FALSE;
2c1f71
     }
2c1f71
 
2c1f71
-    /* find installed packages to remove */
2c1f71
-    query = hy_query_create(priv->sack);
2c1f71
-    query->installed();
2c1f71
-    hy_query_filter(query, HY_PKG_NAME, HY_EQ, name);
2c1f71
-    pkglist = hy_query_run(query);
2c1f71
+    libdnf::Query query(priv->sack, libdnf::Query::ExcludeFlags::APPLY_EXCLUDES);
2c1f71
+    query.installed();
2c1f71
+    auto ret = query.filterSubject(name, nullptr, false, true, true, true);
2c1f71
+    if (!ret.first) {
2c1f71
+        g_set_error(error,
2c1f71
+                    DNF_ERROR,
2c1f71
+                    DNF_ERROR_PACKAGE_NOT_FOUND,
2c1f71
+                    "No installed package matches '%s'", name);
2c1f71
+        return FALSE;
2c1f71
+    }
2c1f71
+
2c1f71
+    g_autoptr(GPtrArray) packages = query.run();
2c1f71
 
2c1f71
     /* add each package */
2c1f71
-    for (i = 0; i < pkglist->len; i++) {
2c1f71
-        auto pkg = static_cast<DnfPackage *>(g_ptr_array_index(pkglist, i));
2c1f71
+    for (guint i = 0; i < packages->len; i++) {
2c1f71
+        auto pkg = static_cast<DnfPackage *>(g_ptr_array_index(packages, i));
2c1f71
         hy_goal_erase(priv->goal, pkg);
2c1f71
     }
2c1f71
-    g_ptr_array_unref(pkglist);
2c1f71
+
2c1f71
     return TRUE;
2c1f71
 } CATCH_TO_GERROR(FALSE)
2c1f71
 
2c1f71
@@ -2493,8 +2494,7 @@ dnf_context_update(DnfContext *context, const gchar *name, GError **error) try
2c1f71
     }
2c1f71
 
2c1f71
     g_auto(HySubject) subject = hy_subject_create(name);
2c1f71
-    g_auto(HySelector) selector = hy_subject_get_best_selector(subject, priv->sack, NULL, FALSE,
2c1f71
-                                                               NULL);
2c1f71
+    g_auto(HySelector) selector = hy_subject_get_best_selector(subject, priv->sack, NULL, FALSE, NULL);
2c1f71
     g_autoptr(GPtrArray) selector_matches = hy_selector_matches(selector);
2c1f71
     if (selector_matches->len == 0) {
2c1f71
         g_set_error(error,
2c1f71
@@ -2504,8 +2504,14 @@ dnf_context_update(DnfContext *context, const gchar *name, GError **error) try
2c1f71
         return FALSE;
2c1f71
     }
2c1f71
 
2c1f71
-    if (hy_goal_upgrade_selector(priv->goal, selector))
2c1f71
+    int ret = hy_goal_upgrade_selector(priv->goal, selector);
2c1f71
+    if (ret != 0) {
2c1f71
+        g_set_error(error,
2c1f71
+                    DNF_ERROR,
2c1f71
+                    ret,
2c1f71
+                    "Ill-formed Selector '%s'", name);
2c1f71
         return FALSE;
2c1f71
+    }
2c1f71
 
2c1f71
     return TRUE;
2c1f71
 } CATCH_TO_GERROR(FALSE)
2c1f71
-- 
2c1f71
2.36.1
2c1f71