1 /*
2  * Copyright (C) 2012-2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <assert.h>
22 #include "hy-goal-private.hpp"
23 #include "hy-iutil-private.hpp"
24 #include "hy-query-private.hpp"
25 #include "hy-selector.h"
26 #include "dnf-advisorypkg.h"
27 #include "sack/advisorypkg.hpp"
28 #include "sack/packageset.hpp"
29 #include "sack/query.hpp"
30 #include "repo/DependencySplitter.hpp"
31 #include "repo/solvable/Dependency.hpp"
32 #include "repo/solvable/DependencyContainer.hpp"
33 #include "goal/Goal.hpp"
34 
35 Id
query_get_index_item(HyQuery query,int index)36 query_get_index_item(HyQuery query, int index)
37 {
38     return query->getIndexItem(index);
39 }
40 
41 void
hy_query_apply(HyQuery q)42 hy_query_apply(HyQuery q)
43 {
44     q->apply();
45 }
46 
47 HyQuery
hy_query_create(DnfSack * sack)48 hy_query_create(DnfSack *sack)
49 {
50     return new libdnf::Query(sack);
51 }
52 
53 HyQuery
hy_query_create_flags(DnfSack * sack,int flags)54 hy_query_create_flags(DnfSack *sack, int flags)
55 {
56     libdnf::Query::ExcludeFlags newFlags = libdnf::Query::ExcludeFlags::APPLY_EXCLUDES;
57     if (flags & HY_IGNORE_EXCLUDES) {
58         newFlags = libdnf::Query::ExcludeFlags::IGNORE_EXCLUDES;
59     }
60     return new libdnf::Query(sack, newFlags);
61 }
62 
63 HyQuery
hy_query_from_nevra(HyNevra nevra,DnfSack * sack,bool icase)64 hy_query_from_nevra(HyNevra nevra, DnfSack *sack, bool icase)
65 {
66     HyQuery query = hy_query_create(sack);
67     hy_add_filter_nevra_object(query, nevra, icase);
68     return query;
69 }
70 
71 void
hy_query_free(HyQuery q)72 hy_query_free(HyQuery q)
73 {
74     delete q;
75 }
76 
77 void
hy_query_clear(HyQuery q)78 hy_query_clear(HyQuery q)
79 {
80     q->clear();
81 }
82 
83 HyQuery
hy_query_clone(HyQuery q)84 hy_query_clone(HyQuery q)
85 {
86     return new libdnf::Query(*q);
87 }
88 
89 int
hy_query_filter(HyQuery q,int keyname,int cmp_type,const char * match)90 hy_query_filter(HyQuery q, int keyname, int cmp_type, const char *match)
91 {
92     return q->addFilter(keyname, cmp_type, match);
93 }
94 
95 int
hy_query_filter_empty(HyQuery q)96 hy_query_filter_empty(HyQuery q)
97 {
98     return q->addFilter(HY_PKG_EMPTY, HY_EQ, 1);
99 }
100 
101 int
hy_query_filter_in(HyQuery q,int keyname,int cmp_type,const char ** matches)102 hy_query_filter_in(HyQuery q, int keyname, int cmp_type,
103                    const char **matches)
104 {
105     return q->addFilter(keyname, cmp_type, matches);
106 }
107 
108 int
hy_query_filter_num(HyQuery q,int keyname,int cmp_type,int match)109 hy_query_filter_num(HyQuery q, int keyname, int cmp_type, int match)
110 {
111     return q->addFilter(keyname, cmp_type, match);
112 }
113 
114 int
hy_query_filter_num_in(HyQuery q,int keyname,int cmp_type,int nmatches,const int * matches)115 hy_query_filter_num_in(HyQuery q, int keyname, int cmp_type, int nmatches, const int *matches)
116 {
117     return q->addFilter(keyname, cmp_type, nmatches, matches);
118 }
119 
120 int
hy_query_filter_package_in(HyQuery q,int keyname,int cmp_type,DnfPackageSet * pset)121 hy_query_filter_package_in(HyQuery q, int keyname, int cmp_type,
122                            DnfPackageSet *pset)
123 {
124     return q->addFilter(keyname, cmp_type, pset);
125 }
126 
127 int
hy_query_filter_reldep(HyQuery q,int keyname,DnfReldep * reldep)128 hy_query_filter_reldep(HyQuery q, int keyname, DnfReldep *reldep)
129 {
130     return q->addFilter(keyname, reldep);
131 }
132 
133 int
hy_query_filter_reldep_in(HyQuery q,int keyname,DnfReldepList * reldeplist)134 hy_query_filter_reldep_in(HyQuery q, int keyname, DnfReldepList *reldeplist)
135 {
136     return q->addFilter( keyname, reldeplist);
137 }
138 
139 int
hy_query_filter_provides(HyQuery q,int cmp_type,const char * name,const char * evr)140 hy_query_filter_provides(HyQuery q, int cmp_type, const char *name, const char *evr)
141 {
142     libdnf::Dependency reldep(q->getSack(), name, evr, cmp_type);
143     return q->addFilter(HY_PKG_PROVIDES, &reldep);
144 }
145 
146 int
hy_query_filter_provides_in(HyQuery q,char ** reldep_strs)147 hy_query_filter_provides_in(HyQuery q, char **reldep_strs)
148 {
149     libdnf::DependencyContainer reldeplist(q->getSack());
150     for (int i = 0; reldep_strs[i] != NULL; ++i) {
151         if (!reldeplist.addReldep(reldep_strs[i])) {
152             return DNF_ERROR_BAD_QUERY;
153         }
154     }
155     q->addFilter(HY_PKG_PROVIDES, &reldeplist);
156     return 0;
157 }
158 
159 /**
160  * Narrows to only those installed packages for which there is a downgrading package.
161  */
162 void
hy_query_filter_downgradable(HyQuery q,int val)163 hy_query_filter_downgradable(HyQuery q, int val)
164 {
165     q->addFilter(HY_PKG_DOWNGRADABLE, HY_EQ, val);
166 }
167 
168 /**
169  * Narrows to only packages downgrading installed packages.
170  */
171 void
hy_query_filter_downgrades(HyQuery q,int val)172 hy_query_filter_downgrades(HyQuery q, int val)
173 {
174     q->addFilter(HY_PKG_DOWNGRADES, HY_EQ, val);
175 }
176 
177 /**
178  * Narrows to only those installed packages for which there is an updating package.
179  */
180 void
hy_query_filter_upgradable(HyQuery q,int val)181 hy_query_filter_upgradable(HyQuery q, int val)
182 {
183     q->addFilter(HY_PKG_UPGRADABLE, HY_EQ, val);
184 }
185 
186 /**
187  * Narrows to only packages updating installed packages.
188  */
189 void
hy_query_filter_upgrades(HyQuery q,int val)190 hy_query_filter_upgrades(HyQuery q, int val)
191 {
192     q->addFilter(HY_PKG_UPGRADES, HY_EQ, val);
193 }
194 
195 /**
196  * Narrows to only the highest version of a package per arch.
197  */
198 void
hy_query_filter_latest_per_arch(HyQuery q,int val)199 hy_query_filter_latest_per_arch(HyQuery q, int val)
200 {
201     q->addFilter(HY_PKG_LATEST_PER_ARCH, HY_EQ, val);
202 }
203 
204 /**
205  * Narrows to only the highest version of a package.
206  */
207 void
hy_query_filter_latest(HyQuery q,int val)208 hy_query_filter_latest(HyQuery q, int val)
209 {
210     q->addFilter(HY_PKG_LATEST, HY_EQ, val);
211 }
212 
213 GPtrArray *
hy_query_run(HyQuery q)214 hy_query_run(HyQuery q)
215 {
216     return q->run();
217 }
218 
219 DnfPackageSet *
hy_query_run_set(HyQuery q)220 hy_query_run_set(HyQuery q)
221 {
222     return new libdnf::PackageSet(*q->runSet());
223 }
224 
225 /**
226  * hy_query_union:
227  * @q:     a #HyQuery instance
228  * @other: other #HyQuery instance
229  *
230  * Unites query with other query (aka logical or).
231  *
232  * Returns: Nothing.
233  *
234  * Since: 0.7.0
235  */
236 void
hy_query_union(HyQuery q,HyQuery other)237 hy_query_union(HyQuery q, HyQuery other)
238 {
239     q->queryUnion(*other);
240 }
241 
242 /**
243  * hy_query_intersection:
244  * @q:     a #HyQuery instance
245  * @other: other #HyQuery instance
246  *
247  * Intersects query with other query (aka logical and).
248  *
249  * Returns: Nothing.
250  *
251  * Since: 0.7.0
252  */
253 void
hy_query_intersection(HyQuery q,HyQuery other)254 hy_query_intersection(HyQuery q, HyQuery other)
255 {
256     q->queryIntersection(*other);
257 }
258 
259 /**
260  * hy_query_difference:
261  * @q:     a #HyQuery instance
262  * @other: other #HyQuery instance
263  *
264  * Computes difference between query and other query (aka q and not other).
265  *
266  * Returns: Nothing.
267  *
268  * Since: 0.7.0
269  */
270 void
hy_query_difference(HyQuery q,HyQuery other)271 hy_query_difference(HyQuery q, HyQuery other)
272 {
273     q->queryDifference(*other);
274 }
275 
276 bool
hy_query_is_empty(HyQuery query)277 hy_query_is_empty(HyQuery query)
278 {
279     return query->empty();
280 }
281 
282 bool
hy_query_is_applied(const HyQuery query)283 hy_query_is_applied(const HyQuery query)
284 {
285     return query->getApplied();
286 }
287 
288 const Map *
hy_query_get_result(const HyQuery query)289 hy_query_get_result(const HyQuery query)
290 {
291     return query->getResult();
292 }
293 
294 DnfSack *
hy_query_get_sack(HyQuery query)295 hy_query_get_sack(HyQuery query)
296 {
297     return query->getSack();
298 }
299 
300 void
hy_add_filter_nevra_object(HyQuery query,HyNevra nevra,bool icase)301 hy_add_filter_nevra_object(HyQuery query, HyNevra nevra, bool icase)
302 {
303     query->addFilter(nevra, icase);
304 }
305 
306 void
hy_add_filter_extras(HyQuery query)307 hy_add_filter_extras(HyQuery query)
308 {
309     query->filterExtras();
310 }
311 
312 void
hy_filter_recent(HyQuery query,const long unsigned int recent_limit)313 hy_filter_recent(HyQuery query, const long unsigned int recent_limit)
314 {
315     query->filterRecent(recent_limit);
316 }
317 
318 void
hy_filter_duplicated(HyQuery query)319 hy_filter_duplicated(HyQuery query)
320 {
321     query->filterDuplicated();
322 }
323 
324 GPtrArray *
hy_query_get_advisory_pkgs(HyQuery query,int cmp_type)325 hy_query_get_advisory_pkgs(HyQuery query, int cmp_type)
326 {
327     std::vector<libdnf::AdvisoryPkg> advisory_pkgs;
328 
329     query->getAdvisoryPkgs(cmp_type, advisory_pkgs);
330     GPtrArray * advisorylist = g_ptr_array_new_full (advisory_pkgs.size(), (GDestroyNotify) dnf_advisorypkg_free);
331     for (auto & advisory_pkg : advisory_pkgs) {
332         g_ptr_array_add(advisorylist, new libdnf::AdvisoryPkg(advisory_pkg));
333     }
334     return advisorylist;
335 }
336 
337 HySelector
hy_query_to_selector(HyQuery query)338 hy_query_to_selector(HyQuery query)
339 {
340     HySelector selector = hy_selector_create(query->getSack());
341     DnfPackageSet *pset = hy_query_run_set(query);
342     hy_selector_pkg_set(selector, pset);
343     delete pset;
344     return selector;
345 }
346