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 <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 // libsolv
27 extern "C" {
28 #include <solv/evr.h>
29 #include <solv/policy.h>
30 #include <solv/selection.h>
31 #include <solv/solver.h>
32 #include <solv/solverdebug.h>
33 #include <solv/testcase.h>
34 #include <solv/transaction.h>
35 #include <solv/util.h>
36 #include <solv/poolid.h>
37 }
38 
39 // hawkey
40 #include "catch-error.hpp"
41 #include "dnf-context.hpp"
42 #include "dnf-types.h"
43 #include "hy-goal-private.hpp"
44 #include "hy-iutil-private.hpp"
45 #include "hy-package-private.hpp"
46 #include "hy-packageset-private.hpp"
47 #include "hy-query-private.hpp"
48 #include "hy-repo-private.hpp"
49 #include "dnf-sack-private.hpp"
50 #include "dnf-goal.h"
51 #include "dnf-package.h"
52 #include "hy-selector-private.hpp"
53 #include "hy-util-private.hpp"
54 #include "dnf-package.h"
55 #include "hy-package.h"
56 #include "goal/Goal.hpp"
57 #include "sack/packageset.hpp"
58 
59 #include "utils/bgettext/bgettext-lib.h"
60 
61 #define BLOCK_SIZE 15
62 
63 
64 // public functions
65 
66 HyGoal
hy_goal_clone(HyGoal goal)67 hy_goal_clone(HyGoal goal)
68 {
69     auto goal_clone = new libdnf::Goal(*goal);
70     goal_clone->set_protect_running_kernel(
71         libdnf::getGlobalMainConfig().protect_running_kernel().getValue());
72     return goal_clone;
73 }
74 
75 HyGoal
hy_goal_create(DnfSack * sack)76 hy_goal_create(DnfSack *sack)
77 {
78     auto goal = new libdnf::Goal(sack);
79     goal->set_protect_running_kernel(
80         libdnf::getGlobalMainConfig().protect_running_kernel().getValue());
81     return goal;
82 }
83 
84 void
hy_goal_free(HyGoal goal)85 hy_goal_free(HyGoal goal)
86 {
87     delete goal;
88 }
89 
90 DnfSack *
hy_goal_get_sack(HyGoal goal)91 hy_goal_get_sack(HyGoal goal)
92 {
93     return goal->getSack();
94 }
95 
96 int
hy_goal_distupgrade_all(HyGoal goal)97 hy_goal_distupgrade_all(HyGoal goal)
98 {
99     goal->distupgrade();
100     return 0;
101 }
102 
103 int
hy_goal_distupgrade(HyGoal goal,DnfPackage * new_pkg)104 hy_goal_distupgrade(HyGoal goal, DnfPackage *new_pkg)
105 {
106     goal->distupgrade(new_pkg);
107     return 0;
108 }
109 
110 int
hy_goal_distupgrade_selector(HyGoal goal,HySelector sltr)111 hy_goal_distupgrade_selector(HyGoal goal, HySelector sltr)
112 {
113     try {
114         goal->distupgrade(sltr);
115     } catch (libdnf::Goal::Error & e) {
116         return e.getErrCode();
117     }
118     return 0;
119 }
120 
121 int
hy_goal_downgrade_to(HyGoal goal,DnfPackage * new_pkg)122 hy_goal_downgrade_to(HyGoal goal, DnfPackage *new_pkg)
123 {
124     goal->install(new_pkg, false);
125     return 0;
126 }
127 
128 int
hy_goal_erase(HyGoal goal,DnfPackage * pkg)129 hy_goal_erase(HyGoal goal, DnfPackage *pkg)
130 {
131     goal->erase(pkg);
132     return 0;
133 }
134 
135 int
hy_goal_erase_flags(HyGoal goal,DnfPackage * pkg,int flags)136 hy_goal_erase_flags(HyGoal goal, DnfPackage *pkg, int flags)
137 {
138     goal->erase(pkg, flags);
139     return 0;
140 }
141 
142 int
hy_goal_erase_selector_flags(HyGoal goal,HySelector sltr,int flags)143 hy_goal_erase_selector_flags(HyGoal goal, HySelector sltr, int flags)
144 {
145     try {
146         goal->erase(sltr, flags);
147     } catch (libdnf::Goal::Error & e) {
148         return e.getErrCode();
149     }
150     return 0;
151 }
152 
153 int
hy_goal_has_actions(HyGoal goal,DnfGoalActions action)154 hy_goal_has_actions(HyGoal goal, DnfGoalActions action)
155 {
156     return goal->hasActions(action);
157 }
158 
159 int
hy_goal_install(HyGoal goal,DnfPackage * new_pkg)160 hy_goal_install(HyGoal goal, DnfPackage *new_pkg)
161 {
162     goal->install(new_pkg, false);
163     return 0;
164 }
165 
166 int
hy_goal_install_optional(HyGoal goal,DnfPackage * new_pkg)167 hy_goal_install_optional(HyGoal goal, DnfPackage *new_pkg)
168 {
169     goal->install(new_pkg, true);
170     return 0;
171 }
172 
173 int
hy_goal_lock(HyGoal goal,DnfPackage * pkg,GError ** error)174 hy_goal_lock(HyGoal goal, DnfPackage *pkg, GError **error)
175 {
176     goal->lock(pkg);
177     return 0;
178 }
179 
180 int
hy_goal_favor(HyGoal goal,DnfPackage * pkg)181 hy_goal_favor(HyGoal goal, DnfPackage *pkg)
182 {
183     goal->favor(pkg);
184     return 0;
185 }
186 
187 int
hy_goal_disfavor(HyGoal goal,DnfPackage * pkg)188 hy_goal_disfavor(HyGoal goal, DnfPackage *pkg)
189 {
190     goal->disfavor(pkg);
191     return 0;
192 }
193 
194 gboolean
hy_goal_install_selector(HyGoal goal,HySelector sltr,GError ** error)195 hy_goal_install_selector(HyGoal goal, HySelector sltr, GError **error) try
196 {
197     goal->install(sltr, false);
198     return TRUE;
199 } CATCH_TO_GERROR(FALSE)
200 
201 gboolean
202 hy_goal_install_selector_optional(HyGoal goal, HySelector sltr, GError **error) try
203 {
204     goal->install(sltr, true);
205     return TRUE;
CATCH_TO_GERROR(FALSE)206 } CATCH_TO_GERROR(FALSE)
207 
208 int
209 hy_goal_upgrade_all(HyGoal goal)
210 {
211     goal->upgrade();
212     return 0;
213 }
214 
215 int
hy_goal_upgrade_to(HyGoal goal,DnfPackage * new_pkg)216 hy_goal_upgrade_to(HyGoal goal, DnfPackage *new_pkg)
217 {
218     goal->upgrade(new_pkg);
219     return 0;
220 }
221 
222 int
hy_goal_upgrade_selector(HyGoal goal,HySelector sltr)223 hy_goal_upgrade_selector(HyGoal goal, HySelector sltr)
224 {
225     try {
226         goal->upgrade(sltr);
227     } catch (libdnf::Goal::Error & e) {
228         return e.getErrCode();
229     }
230     return 0;
231 }
232 
233 int
hy_goal_userinstalled(HyGoal goal,DnfPackage * pkg)234 hy_goal_userinstalled(HyGoal goal, DnfPackage *pkg)
235 {
236     goal->userInstalled(pkg);
237     return 0;
238 }
239 
hy_goal_req_length(HyGoal goal)240 int hy_goal_req_length(HyGoal goal)
241 {
242     return goal->jobLength();
243 }
244 
245 int
hy_goal_run_flags(HyGoal goal,DnfGoalActions flags)246 hy_goal_run_flags(HyGoal goal, DnfGoalActions flags)
247 {
248     return goal->run(flags);
249 }
250 
251 int
hy_goal_count_problems(HyGoal goal)252 hy_goal_count_problems(HyGoal goal)
253 {
254     return goal->countProblems();
255 }
256 
257 /**
258  * Reports packages that has a conflict
259  *
260  * available - if available it returns set with available packages with conflicts
261  * available - if package installed it also excludes available packages with same NEVRA
262  *
263  * Returns DnfPackageSet with all packages that have a conflict.
264  */
265 DnfPackageSet *
hy_goal_conflict_all_pkgs(HyGoal goal,DnfPackageState pkg_type)266 hy_goal_conflict_all_pkgs(HyGoal goal, DnfPackageState pkg_type)
267 {
268     return goal->listConflictPkgs(pkg_type).release();
269 }
270 
271 /**
272  * Reports all packages that have broken dependency
273  * available - if available returns only available packages with broken dependencies
274  * available - if package installed it also excludes available packages with same NEVRA
275  * Returns DnfPackageSet with all packages with broken dependency
276  */
277 DnfPackageSet *
hy_goal_broken_dependency_all_pkgs(HyGoal goal,DnfPackageState pkg_type)278 hy_goal_broken_dependency_all_pkgs(HyGoal goal, DnfPackageState pkg_type)
279 {
280     return goal->listBrokenDependencyPkgs(pkg_type).release();
281 }
282 
283 /**
284  * Write all the solving decisions to the hawkey logfile.
285  */
286 int
hy_goal_log_decisions(HyGoal goal)287 hy_goal_log_decisions(HyGoal goal)
288 {
289     return goal->logDecisions();
290 }
291 
292 /**
293  * hy_goal_write_debugdata:
294  * @goal: A #HyGoal
295  * @dir: The directory to write to
296  * @error: A #GError, or %NULL
297  *
298  * Writes details about the testcase to a directory.
299  *
300  * Returns: %FALSE if an error was set
301  *
302  * Since: 0.7.0
303  */
304 bool
hy_goal_write_debugdata(HyGoal goal,const char * dir,GError ** error)305 hy_goal_write_debugdata(HyGoal goal, const char *dir, GError **error) try
306 {
307     goal->writeDebugdata(dir);
308     return true;
309 } CATCH_TO_GERROR(false)
310 
311 GPtrArray *
312 hy_goal_list_erasures(HyGoal goal, GError **error) try
313 {
314     auto pset = goal->listErasures();
315     return packageSet2GPtrArray(&pset);
CATCH_TO_GERROR(NULL)316 } CATCH_TO_GERROR(NULL)
317 
318 GPtrArray *
319 hy_goal_list_installs(HyGoal goal, GError **error) try
320 {
321     auto pset = goal->listInstalls();
322     return packageSet2GPtrArray(&pset);
323 } CATCH_TO_GERROR(NULL)
324 
325 GPtrArray *
326 hy_goal_list_obsoleted(HyGoal goal, GError **error) try
327 {
328     auto pset = goal->listObsoleted();
329     return packageSet2GPtrArray(&pset);
CATCH_TO_GERROR(NULL)330 } CATCH_TO_GERROR(NULL)
331 
332 GPtrArray *
333 hy_goal_list_reinstalls(HyGoal goal, GError **error) try
334 {
335     auto pset = goal->listReinstalls();
336     return packageSet2GPtrArray(&pset);
337 } CATCH_TO_GERROR(NULL)
338 
339 GPtrArray *
340 hy_goal_list_unneeded(HyGoal goal, GError **error) try
341 {
342     auto pset = goal->listUnneeded();
343     return packageSet2GPtrArray(&pset);
CATCH_TO_GERROR(NULL)344 } CATCH_TO_GERROR(NULL)
345 
346 GPtrArray *
347 hy_goal_list_suggested(HyGoal goal, GError **error) try
348 {
349     auto pset = goal->listSuggested();
350     return packageSet2GPtrArray(&pset);
351 } CATCH_TO_GERROR(NULL)
352 
353 GPtrArray *
354 hy_goal_list_upgrades(HyGoal goal, GError **error) try
355 {
356     auto pset = goal->listUpgrades();
357     return packageSet2GPtrArray(&pset);
CATCH_TO_GERROR(NULL)358 } CATCH_TO_GERROR(NULL)
359 
360 GPtrArray *
361 hy_goal_list_downgrades(HyGoal goal, GError **error) try
362 {
363     auto pset = goal->listDowngrades();
364     return packageSet2GPtrArray(&pset);
365 } CATCH_TO_GERROR(NULL)
366 
367 GPtrArray *
368 hy_goal_list_obsoleted_by_package(HyGoal goal, DnfPackage *pkg)
369 {
370     auto pset = goal->listObsoletedByPackage(pkg);
371     return packageSet2GPtrArray(&pset);
372 }
373 
374 int
hy_goal_get_reason(HyGoal goal,DnfPackage * pkg)375 hy_goal_get_reason(HyGoal goal, DnfPackage *pkg)
376 {
377     return goal->getReason(pkg);
378 }
379