dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

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

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