]>
Commit | Line | Data |
---|---|---|
1c159e0a JT |
1 | /* |
2 | * Copyright (c) 2017 Jon Turney | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | */ | |
13 | ||
14 | #include "libsolv.h" | |
16994679 KB |
15 | #include "package_db.h" |
16 | #include "package_meta.h" | |
1c159e0a JT |
17 | |
18 | #include "solv/solver.h" | |
19 | #include "solv/solverdebug.h" | |
20 | #include "solv/evr.h" | |
21 | ||
22 | #include "LogSingleton.h" | |
1d553f34 | 23 | #include <iomanip> |
1c159e0a JT |
24 | |
25 | // --------------------------------------------------------------------------- | |
26 | // Utility functions for mapping between Operators and Relation Ids | |
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | static Id | |
30 | Operator2RelId(PackageSpecification::_operators op) | |
31 | { | |
32 | switch (op) | |
33 | { | |
34 | case PackageSpecification::Equals: | |
35 | return REL_EQ; | |
36 | case PackageSpecification::LessThan: | |
37 | return REL_LT; | |
38 | case PackageSpecification::MoreThan: | |
39 | return REL_GT; | |
40 | case PackageSpecification::LessThanEquals: | |
41 | return REL_LT | REL_EQ; | |
42 | case PackageSpecification::MoreThanEquals: | |
43 | return REL_GT | REL_EQ; | |
44 | } | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
49 | static PackageSpecification::_operators | |
50 | RelId2Operator(Id id) | |
51 | { | |
52 | switch (id) | |
53 | { | |
54 | case REL_EQ: | |
55 | return PackageSpecification::Equals; | |
56 | case REL_LT: | |
57 | return PackageSpecification::LessThan; | |
58 | case REL_GT: | |
59 | return PackageSpecification::MoreThan; | |
60 | case REL_LT | REL_EQ: | |
61 | return PackageSpecification::LessThanEquals; | |
62 | case REL_GT | REL_EQ: | |
63 | return PackageSpecification::MoreThanEquals; | |
64 | } | |
65 | ||
66 | return PackageSpecification::Equals; | |
67 | } | |
68 | ||
69 | // --------------------------------------------------------------------------- | |
70 | // implements class SolvableVersion | |
71 | // | |
72 | // a wrapper around a libsolv Solvable | |
73 | // --------------------------------------------------------------------------- | |
74 | ||
bfe593cf KB |
75 | Id |
76 | SolvableVersion::name_id () const | |
77 | { | |
78 | Solvable *solvable = pool_id2solvable(pool, id); | |
79 | return solvable->name; | |
80 | } | |
81 | ||
1c159e0a JT |
82 | const std::string |
83 | SolvableVersion::Name () const | |
84 | { | |
e2ec1fa2 KB |
85 | if (!id) |
86 | return ""; | |
bfe593cf | 87 | return pool_id2str(pool, name_id()); |
1c159e0a JT |
88 | } |
89 | ||
90 | const std::string | |
91 | SolvableVersion::Canonical_version() const | |
92 | { | |
e2ec1fa2 KB |
93 | if (!id) |
94 | return ""; | |
1c159e0a JT |
95 | Solvable *solvable = pool_id2solvable(pool, id); |
96 | return std::string(pool_id2str(pool, solvable->evr)); | |
97 | } | |
98 | ||
99 | package_type_t | |
100 | SolvableVersion::Type () const | |
101 | { | |
e2ec1fa2 KB |
102 | if (!id) |
103 | return package_binary; | |
1c159e0a JT |
104 | Solvable *solvable = pool_id2solvable(pool, id); |
105 | if (solvable->arch == ARCH_SRC) | |
106 | return package_source; | |
107 | else | |
108 | return package_binary; | |
109 | } | |
110 | ||
111 | const PackageDepends | |
112 | SolvableVersion::depends() const | |
5ebe14e4 KB |
113 | { |
114 | return deplist(SOLVABLE_REQUIRES); | |
115 | } | |
116 | ||
117 | const PackageDepends | |
118 | SolvableVersion::obsoletes() const | |
119 | { | |
120 | return deplist(SOLVABLE_OBSOLETES); | |
121 | } | |
122 | ||
123 | // helper function which returns the deplist for a given key, as a PackageDepends | |
124 | const PackageDepends | |
125 | SolvableVersion::deplist(Id keyname) const | |
1c159e0a | 126 | { |
e2ec1fa2 KB |
127 | static PackageDepends empty_package; |
128 | if (!id) | |
129 | return empty_package; | |
1c159e0a JT |
130 | Solvable *solvable = pool_id2solvable(pool, id); |
131 | ||
132 | Queue q; | |
133 | queue_init(&q); | |
134 | ||
5ebe14e4 | 135 | if (repo_lookup_idarray(solvable->repo, id, keyname, &q)) |
1c159e0a JT |
136 | { |
137 | // convert | |
138 | PackageDepends dep; | |
139 | ||
140 | for (int i = 0; i < q.count; i++) | |
141 | { | |
142 | #ifdef DEBUG | |
143 | Log (LOG_PLAIN) << "dep " << std::hex << q.elements[i] << ": " << pool_dep2str(pool, q.elements[i]) << endLog; | |
144 | #endif | |
145 | ||
146 | const char *name = pool_id2str(pool, q.elements[i]); | |
147 | PackageSpecification *spec = new PackageSpecification (name); | |
148 | ||
149 | if (ISRELDEP(id)) | |
150 | { | |
151 | Reldep *rd = GETRELDEP(pool, id); | |
152 | spec->setOperator(RelId2Operator(rd->flags)); | |
153 | spec->setVersion(pool_id2str(pool, rd->evr)); | |
154 | } | |
155 | ||
156 | dep.push_back (spec); | |
157 | } | |
158 | ||
159 | queue_empty(&q); | |
160 | ||
161 | return dep; | |
162 | } | |
163 | ||
164 | // otherwise, return an empty depends list | |
1c159e0a JT |
165 | return empty_package; |
166 | } | |
167 | ||
168 | const std::string | |
169 | SolvableVersion::SDesc () const | |
170 | { | |
e2ec1fa2 KB |
171 | if (!id) |
172 | return ""; | |
1c159e0a JT |
173 | Solvable *solvable = pool_id2solvable(pool, id); |
174 | const char *sdesc = repo_lookup_str(solvable->repo, id, SOLVABLE_SUMMARY); | |
c4b8d6dd JT |
175 | |
176 | if (!sdesc) | |
177 | return ""; | |
178 | ||
1c159e0a JT |
179 | return sdesc; |
180 | } | |
181 | ||
c59b92e8 JT |
182 | const std::string |
183 | SolvableVersion::sourcePackageName () const | |
184 | { | |
185 | if (!id) | |
186 | return ""; | |
187 | ||
188 | // extract source package name | |
189 | Solvable *solvable = pool_id2solvable(pool, id); | |
190 | Id spkg = repo_lookup_id(solvable->repo, id, SOLVABLE_SOURCENAME); | |
191 | ||
192 | // has no such attribute | |
193 | if (!spkg) | |
194 | return ""; | |
195 | ||
196 | return std::string(pool_id2str(pool, spkg)); | |
197 | } | |
198 | ||
1c159e0a JT |
199 | SolvableVersion |
200 | SolvableVersion::sourcePackage () const | |
201 | { | |
202 | if (!id) | |
203 | return SolvableVersion(); | |
204 | ||
205 | // extract source package id | |
206 | Solvable *solvable = pool_id2solvable(pool, id); | |
207 | Id spkg_attr = pool_str2id(pool, "solvable:sourceid", 1); | |
208 | Id spkg_id = repo_lookup_id(solvable->repo, id, spkg_attr); | |
209 | ||
210 | // has no such attribute | |
211 | if (!spkg_id) | |
212 | return SolvableVersion(); | |
213 | ||
214 | return SolvableVersion(spkg_id, pool); | |
215 | } | |
216 | ||
c59b92e8 JT |
217 | void |
218 | SolvableVersion::fixup_spkg_id (SolvableVersion spkg_id) const | |
219 | { | |
e2ec1fa2 KB |
220 | if (!id) |
221 | return; | |
c59b92e8 JT |
222 | Solvable *solvable = pool_id2solvable(pool, id); |
223 | Repodata *data = repo_last_repodata(solvable->repo); | |
224 | Id handle = id; | |
225 | Id spkg_attr = pool_str2id(pool, "solvable:sourceid", 1); | |
226 | repodata_set_id(data, handle, spkg_attr, spkg_id.id); | |
227 | } | |
228 | ||
1c159e0a JT |
229 | packagesource * |
230 | SolvableVersion::source() const | |
231 | { | |
c4b8d6dd | 232 | static packagesource empty_source = packagesource(); |
1c159e0a | 233 | if (!id) { |
1c159e0a JT |
234 | return &empty_source; |
235 | } | |
236 | ||
237 | Solvable *solvable = pool_id2solvable(pool, id); | |
238 | Id psrc_attr = pool_str2id(pool, "solvable:packagesource", 1); | |
c4b8d6dd | 239 | return (packagesource *)repo_lookup_num(solvable->repo, id, psrc_attr, (unsigned long long)&empty_source); |
1c159e0a JT |
240 | } |
241 | ||
242 | bool | |
243 | SolvableVersion::accessible () const | |
244 | { | |
de8fc0b8 JT |
245 | // The 'accessible' check used to test if an archive was available locally or |
246 | // from a mirror. | |
1c159e0a | 247 | // |
de8fc0b8 JT |
248 | // This seems utterly pointless. as binary packages which aren't 'accessible' |
249 | // never get to appear in the package list. | |
250 | // | |
251 | // For source packages, it's equivalent to the bool conversion operator.) | |
252 | // | |
253 | if (id != 0) | |
254 | return TRUE; | |
255 | else | |
256 | return FALSE; | |
1c159e0a JT |
257 | } |
258 | ||
259 | package_stability_t | |
260 | SolvableVersion::Stability () const | |
261 | { | |
e2ec1fa2 KB |
262 | if (!id) |
263 | return TRUST_UNKNOWN; | |
1c159e0a JT |
264 | Solvable *solvable = pool_id2solvable(pool, id); |
265 | Id stability_attr = pool_str2id(pool, "solvable:stability", 1); | |
266 | return (package_stability_t)repo_lookup_num(solvable->repo, id, stability_attr, TRUST_UNKNOWN); | |
267 | } | |
268 | ||
269 | bool | |
270 | SolvableVersion::operator <(SolvableVersion const &rhs) const | |
271 | { | |
272 | return (compareVersions(*this, rhs) < 0); | |
273 | } | |
274 | ||
275 | bool | |
276 | SolvableVersion::operator ==(SolvableVersion const &rhs) const | |
277 | { | |
278 | return (compareVersions(*this, rhs) == 0); | |
279 | } | |
280 | ||
281 | bool | |
282 | SolvableVersion::operator !=(SolvableVersion const &rhs) const | |
283 | { | |
284 | return (compareVersions(*this, rhs) != 0); | |
285 | } | |
286 | ||
287 | int | |
288 | SolvableVersion::compareVersions(const SolvableVersion &a, | |
289 | const SolvableVersion &b) | |
290 | { | |
291 | if (a.id == b.id) | |
292 | return 0; | |
293 | ||
294 | // if a and b are different, at least one of them has a pool | |
295 | Pool *pool = a.pool ? a.pool : b.pool; | |
296 | ||
297 | Solvable *sa = a.id ? pool_id2solvable(a.pool, a.id) : NULL; | |
298 | Solvable *sb = b.id ? pool_id2solvable(b.pool, b.id) : NULL; | |
299 | ||
300 | // empty versions compare as if their version is the empty string | |
301 | Id evra = sa ? sa->evr : pool_str2id(pool, "", 1); | |
302 | Id evrb = sb ? sb->evr : pool_str2id(pool, "", 1); | |
303 | ||
304 | return pool_evrcmp(pool, evra, evrb, EVRCMP_COMPARE); | |
305 | } | |
306 | ||
307 | // --------------------------------------------------------------------------- | |
308 | // implements class SolverPool | |
309 | // | |
310 | // a simplified wrapper for libsolv | |
311 | // --------------------------------------------------------------------------- | |
312 | ||
313 | static | |
314 | void debug_callback(Pool *pool, void *data, int type, const char *str) | |
315 | { | |
316 | if (type & (SOLV_FATAL|SOLV_ERROR)) | |
317 | LogPlainPrintf("libsolv: %s", str); | |
318 | else | |
319 | LogBabblePrintf("libsolv: %s", str); | |
320 | } | |
321 | ||
322 | SolverPool::SolverPool() | |
ab67dafa JT |
323 | { |
324 | init(); | |
325 | } | |
326 | ||
327 | void | |
328 | SolverPool::init() | |
1c159e0a JT |
329 | { |
330 | /* create a pool */ | |
331 | pool = pool_create(); | |
332 | ||
333 | pool_setdebugcallback(pool, debug_callback, NULL); | |
334 | ||
335 | int level = 1; | |
336 | #if DEBUG | |
337 | level = 3; | |
338 | #endif | |
339 | pool_setdebuglevel(pool, level); | |
340 | ||
341 | /* create the repo to hold installed packages */ | |
342 | SolvRepo *installed = getRepo("_installed"); | |
343 | pool_set_installed(pool, installed->repo); | |
344 | } | |
345 | ||
ab67dafa JT |
346 | void |
347 | SolverPool::clear() | |
348 | { | |
349 | repos.clear(); | |
350 | pool_free(pool); | |
351 | pool = NULL; | |
352 | ||
353 | init(); | |
354 | } | |
355 | ||
1c159e0a JT |
356 | SolvRepo * |
357 | SolverPool::getRepo(const std::string &name, bool test) | |
358 | { | |
359 | RepoList::iterator i = repos.find(name); | |
360 | if (i != repos.end()) | |
361 | return i->second; | |
362 | ||
363 | /* create repo if not found */ | |
364 | SolvRepo *r = new(SolvRepo); | |
365 | r->repo = repo_create(pool, name.c_str()); | |
366 | ||
367 | /* create attribute store, with no local pool */ | |
368 | r->data = repo_add_repodata(r->repo, 0); | |
369 | ||
370 | /* remember if this is a test stability repo */ | |
371 | r->test = test; | |
372 | ||
3c3d695d KB |
373 | /* set default priority */ |
374 | r->repo->priority = SolvRepo::priorityNormal; | |
375 | ||
1c159e0a JT |
376 | repos[name] = r; |
377 | ||
378 | return r; | |
379 | } | |
380 | ||
381 | /* | |
382 | Helper function to convert a PackageDepends list to libsolv dependencies. | |
383 | */ | |
384 | Id | |
385 | SolverPool::makedeps(Repo *repo, PackageDepends *requires) | |
386 | { | |
387 | Id deps = 0; | |
388 | ||
389 | for (PackageDepends::iterator i = requires->begin(); | |
390 | i != requires->end(); | |
391 | i++) | |
392 | { | |
393 | Id name = pool_str2id(pool, (*i)->packageName().c_str(), 1); | |
394 | ||
395 | if ((*i)->version().size() == 0) | |
396 | { | |
397 | // no relation, so dependency is just on package name | |
398 | deps = repo_addid_dep(repo, deps, name, 0); | |
399 | } | |
400 | else | |
401 | { | |
402 | // otherwise, dependency is on package name with a version condition | |
403 | Id evr = pool_str2id(pool, (*i)->version().c_str(), 1); | |
404 | int rel = pool_rel2id(pool, name, evr, Operator2RelId((*i)->op()), 1); | |
405 | ||
406 | deps = repo_addid_dep(repo, deps, rel, 0); | |
407 | } | |
408 | } | |
409 | ||
410 | return deps; | |
411 | } | |
412 | ||
413 | SolvableVersion | |
414 | SolverPool::addPackage(const std::string& pkgname, const addPackageData &pkgdata) | |
415 | { | |
416 | std::string repoName = pkgdata.reponame; | |
417 | bool test = false; | |
418 | ||
3c3d695d KB |
419 | /* It's simplest to place test packages into a separate repo, and |
420 | then arrange for that repo to have low priority, if we don't want | |
421 | to install those packages by default */ | |
1c159e0a | 422 | |
88f3d803 | 423 | if (pkgdata.stability == TRUST_TEST && repoName != "_installed") |
1c159e0a JT |
424 | { |
425 | repoName = pkgdata.reponame + "_test_"; | |
426 | test = true; | |
427 | } | |
428 | ||
429 | SolvRepo *r = getRepo(repoName, test); | |
430 | Repo *repo = r->repo; | |
431 | ||
432 | /* create a solvable */ | |
433 | Id s = repo_add_solvable(repo); | |
434 | Solvable *solvable = pool_id2solvable(pool, s); | |
435 | ||
436 | /* initialize solvable for this packageo/version/etc. */ | |
437 | solvable->name = pool_str2id(pool, pkgname.c_str(), 1); | |
438 | solvable->arch = (pkgdata.type == package_binary) ? ARCH_ANY : ARCH_SRC; | |
439 | solvable->evr = pool_str2id(repo->pool, pkgdata.version.c_str(), 1); | |
440 | solvable->vendor = pool_str2id(repo->pool, pkgdata.vendor.c_str(), 1); | |
441 | solvable->provides = repo_addid_dep(repo, solvable->provides, pool_rel2id(pool, solvable->name, solvable->evr, REL_EQ, 1), 0); | |
442 | if (pkgdata.requires) | |
443 | solvable->requires = makedeps(repo, pkgdata.requires); | |
20b98f20 JT |
444 | if (pkgdata.obsoletes) |
445 | solvable->obsoletes = makedeps(repo, pkgdata.obsoletes); | |
1c159e0a JT |
446 | |
447 | /* a solvable can also store arbitrary attributes not needed for dependency | |
448 | resolution, if we need them */ | |
449 | ||
450 | Repodata *data = r->data; | |
451 | Id handle = s; | |
452 | #if DEBUG | |
453 | Log (LOG_PLAIN) << "solvable " << s << " name " << pkgname << endLog; | |
454 | #endif | |
455 | ||
456 | /* store short description attribute */ | |
457 | repodata_set_str(data, handle, SOLVABLE_SUMMARY, pkgdata.sdesc.c_str()); | |
458 | /* store long description attribute */ | |
459 | repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, pkgdata.ldesc.c_str()); | |
460 | ||
461 | /* store source-package attribute */ | |
462 | const std::string sname = pkgdata.spkg.packageName(); | |
463 | if (!sname.empty()) | |
464 | repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_str2id(pool, sname.c_str(), 1)); | |
465 | else | |
466 | repodata_set_void(data, handle, SOLVABLE_SOURCENAME); | |
467 | /* solvable:sourceevr may also be available from spkg but assumed to be same | |
468 | as evr for the moment */ | |
469 | ||
470 | /* store source-package id */ | |
c59b92e8 JT |
471 | /* (If the source-package hasn't been seen yet, we don't know what it's Id |
472 | will be. That gets fixed up later.) */ | |
473 | if (pkgdata.spkg_id) | |
474 | { | |
475 | Id spkg_attr = pool_str2id(pool, "solvable:sourceid", 1); | |
476 | repodata_set_id(data, handle, spkg_attr, pkgdata.spkg_id.id); | |
477 | } | |
1c159e0a JT |
478 | |
479 | /* we could store packagesource information as attributes ... | |
480 | ||
481 | e.g. | |
482 | size SOLVABLE_DOWNLOADSIZE | |
483 | pathname SOLVABLE_MEDIAFILE | |
484 | site SOLVABLE_MEDIABASE | |
485 | checksum SOLVABLE_CHECKSUM | |
486 | ||
487 | ... but for the moment, we just store a pointer to a packagesource object | |
488 | */ | |
489 | Id psrc_attr = pool_str2id(pool, "solvable:packagesource", 1); | |
490 | packagesource *psrc = new packagesource(pkgdata.archive); | |
491 | repodata_set_num(data, handle, psrc_attr, (intptr_t)psrc); | |
492 | ||
493 | /* store stability level attribute */ | |
494 | Id stability_attr = pool_str2id(pool, "solvable:stability", 1); | |
495 | repodata_set_num(data, handle, stability_attr, pkgdata.stability); | |
496 | ||
497 | #if 0 | |
498 | repodata_internalize(data); | |
499 | ||
500 | /* debug: verify the attributes we've just set get retrieved correctly */ | |
501 | SolvableVersion sv = SolvableVersion(s, pool); | |
502 | const std::string check_sdesc = sv.SDesc(); | |
503 | if (pkgdata.sdesc.compare(check_sdesc) != 0) { | |
504 | Log (LOG_PLAIN) << pkgname << " has sdesc mismatch: '" << pkgdata.sdesc << "' and '" | |
505 | << check_sdesc << "'" << endLog; | |
506 | } | |
507 | if (!sname.empty()) { | |
508 | SolvableVersion check_spkg = sv.sourcePackage(); | |
509 | Solvable *check_spkg_solvable = pool_id2solvable(pool, check_spkg.id); | |
510 | std::string check_sname = pool_id2str(pool, check_spkg_solvable->name); | |
511 | if (sname.compare(check_sname) != 0) { | |
512 | Log (LOG_PLAIN) << pkgname << " has spkg mismatch: '" << pkgdata.spkg.packageName() | |
513 | << "' and '" << check_sname << "'" << endLog; | |
514 | } | |
515 | } | |
516 | packagesource *check_archive = sv.source(); | |
517 | if (check_archive != psrc) | |
518 | Log (LOG_PLAIN) << pkgname << " has archive mismatch: " << psrc | |
519 | << " and " << check_archive << endLog; | |
520 | package_stability_t check_stability = sv.Stability(); | |
521 | if (check_stability != pkgdata.stability) { | |
522 | Log (LOG_PLAIN) << pkgname << " has stability mismatch: " << pkgdata.stability | |
523 | << " and " << check_stability << endLog; | |
524 | } | |
525 | #endif | |
526 | ||
527 | return SolvableVersion(s, pool); | |
528 | } | |
529 | ||
530 | void | |
531 | SolverPool::internalize() | |
532 | { | |
533 | /* Make attribute data available to queries */ | |
534 | for (RepoList::iterator i = repos.begin(); | |
535 | i != repos.end(); | |
536 | i++) | |
537 | { | |
538 | repodata_internalize(i->second->data); | |
539 | } | |
540 | } | |
1d553f34 | 541 | |
16994679 KB |
542 | void |
543 | SolverTasks::setTasks() | |
544 | { | |
545 | // go through all packages, adding changed ones to the solver task list | |
546 | packagedb db; | |
2506055b | 547 | tasks.clear(); |
16994679 KB |
548 | |
549 | for (packagedb::packagecollection::iterator p = db.packages.begin (); | |
550 | p != db.packages.end (); ++p) | |
551 | { | |
552 | packagemeta *pkg = p->second; | |
553 | ||
554 | // decode UI state to action | |
bfe593cf KB |
555 | // keep and skip need attention only when they differ from the |
556 | // solver's solution | |
16994679 KB |
557 | if (pkg->installed != pkg->desired) |
558 | { | |
559 | if (pkg->desired) | |
560 | add(pkg->desired, taskInstall); // install/upgrade | |
561 | else | |
562 | add(pkg->installed, taskUninstall); // uninstall | |
563 | } | |
564 | else if (pkg->installed) | |
565 | { | |
566 | if (pkg->picked()) | |
567 | add(pkg->installed, taskReinstall); // reinstall | |
eb09ab39 | 568 | else if (pkg->installed != pkg->default_version) |
16994679 KB |
569 | add(pkg->installed, taskKeep); // keep |
570 | } | |
bfe593cf KB |
571 | else if (pkg->default_version) |
572 | add(pkg->default_version, taskSkip); // skip | |
16994679 KB |
573 | |
574 | // only install action makes sense for source packages | |
575 | if (pkg->srcpicked()) | |
576 | { | |
577 | if (pkg->desired) | |
578 | add(pkg->desired.sourcePackage(), taskInstall); | |
579 | else | |
580 | add(pkg->installed.sourcePackage(), taskInstall); | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
1d553f34 JT |
585 | void |
586 | SolverPool::use_test_packages(bool use_test_packages) | |
587 | { | |
3c3d695d KB |
588 | // Give repos containing test packages higher priority than normal |
589 | // if wanted, otherwise lower priority. | |
590 | SolvRepo::priority_t p = use_test_packages ? SolvRepo::priorityNormal : SolvRepo::priorityLow; | |
1d553f34 JT |
591 | for (RepoList::iterator i = repos.begin(); |
592 | i != repos.end(); | |
593 | i++) | |
594 | { | |
595 | if (i->second->test) | |
3c3d695d | 596 | i->second->setPriority(p); |
1d553f34 JT |
597 | } |
598 | } | |
599 | ||
600 | // --------------------------------------------------------------------------- | |
601 | // implements class SolverSolution | |
602 | // | |
603 | // A wrapper around the libsolv solver | |
604 | // --------------------------------------------------------------------------- | |
605 | ||
2506055b JT |
606 | SolverSolution::SolverSolution(SolverPool &_pool) : pool(_pool), solv(NULL) |
607 | { | |
608 | queue_init(&job); | |
609 | } | |
610 | ||
1d553f34 | 611 | SolverSolution::~SolverSolution() |
ab67dafa JT |
612 | { |
613 | clear(); | |
614 | } | |
615 | ||
616 | void | |
617 | SolverSolution::clear() | |
1d553f34 JT |
618 | { |
619 | if (solv) | |
620 | { | |
621 | solver_free(solv); | |
622 | solv = NULL; | |
623 | } | |
2506055b | 624 | queue_free(&job); |
1d553f34 JT |
625 | } |
626 | ||
7c94d449 KB |
627 | void |
628 | SolverSolution::trans2db() const | |
629 | { | |
630 | packagedb db; | |
631 | // First reset all packages to the "no change" state | |
632 | for (packagedb::packagecollection::iterator i = db.packages.begin(); | |
633 | i != db.packages.end(); i++) | |
634 | { | |
635 | packagemeta *pkg = i->second; | |
eb09ab39 | 636 | pkg->desired = pkg->default_version = pkg->installed; |
7c94d449 KB |
637 | pkg->pick(false); |
638 | } | |
639 | // Now make changes according to trans. transErase requires some | |
640 | // care; it could either be a "plain" uninstall, or it could be | |
641 | // paired with a transInstall for an upgrade/downgrade or reinstall. | |
642 | for (SolverTransactionList::const_iterator i = trans.begin(); | |
643 | i != trans.end(); i++) | |
644 | { | |
645 | const packageversion & pv = i->version; | |
646 | packagemeta *pkg = db.findBinary(PackageSpecification(pv.Name())); | |
647 | if (!pkg) | |
648 | // Can't happen - throw an exception? | |
649 | { | |
650 | Log (LOG_PLAIN) << "Can't happen. No packagemeta for " | |
651 | << pv.Name() << endLog; | |
652 | return; | |
653 | } | |
654 | switch (i->type) | |
655 | { | |
656 | case SolverTransaction::transInstall: | |
657 | if (pv.Type() == package_binary) | |
658 | { | |
eb09ab39 | 659 | pkg->desired = pkg->default_version = pv; |
7c94d449 KB |
660 | pkg->pick(true); |
661 | } | |
662 | else // source package | |
663 | pkg->srcpick(true); | |
664 | break; | |
665 | case SolverTransaction::transErase: | |
666 | // Only relevant if pkg is still in its "no change" state | |
667 | if (pkg->desired == pkg->installed && !pkg->picked()) | |
eb09ab39 | 668 | pkg->desired = pkg->default_version = packageversion(); |
7c94d449 KB |
669 | break; |
670 | default: | |
671 | break; | |
672 | } | |
673 | } | |
674 | } | |
675 | ||
9a7507eb KB |
676 | void |
677 | SolverSolution::db2trans() | |
678 | { | |
679 | trans.clear(); | |
680 | packagedb db; | |
681 | ||
682 | for (packagedb::packagecollection::iterator p = db.packages.begin (); | |
683 | p != db.packages.end (); ++p) | |
684 | { | |
685 | packagemeta *pkg = p->second; | |
686 | if (pkg->desired && pkg->picked()) // install/upgrade/reinstall | |
687 | { | |
688 | trans.push_back(SolverTransaction(pkg->desired, SolverTransaction::transInstall)); | |
689 | if (pkg->installed) | |
690 | trans.push_back(SolverTransaction(pkg->installed, SolverTransaction::transErase)); | |
691 | } | |
692 | else if (!pkg->desired && pkg->installed) // uninstall | |
693 | trans.push_back(SolverTransaction(pkg->installed, SolverTransaction::transErase)); | |
694 | ||
695 | if (pkg->srcpicked()) | |
696 | { | |
697 | if (pkg->desired) | |
698 | trans.push_back(SolverTransaction(pkg->desired.sourcePackage(), SolverTransaction::transInstall)); | |
699 | else | |
700 | trans.push_back(SolverTransaction(pkg->installed.sourcePackage(), SolverTransaction::transInstall)); | |
701 | } | |
702 | } | |
703 | } | |
704 | ||
1d553f34 JT |
705 | static |
706 | std::ostream &operator<<(std::ostream &stream, | |
707 | SolverTransaction::transType type) | |
708 | { | |
709 | switch (type) | |
710 | { | |
711 | case SolverTransaction::transInstall: | |
712 | stream << "install"; | |
713 | break; | |
714 | case SolverTransaction::transErase: | |
715 | stream << "erase"; | |
716 | break; | |
717 | default: | |
718 | stream << "unknown"; | |
719 | } | |
720 | return stream; | |
721 | } | |
722 | ||
2506055b JT |
723 | void |
724 | SolverSolution::tasksToJobs(SolverTasks &tasks, updateMode update, Queue &job) | |
1d553f34 | 725 | { |
1d553f34 JT |
726 | // solver accepts a queue containing pairs of (cmd, id) tasks |
727 | // cmd is job and selection flags ORed together | |
728 | for (SolverTasks::taskList::const_iterator i = tasks.tasks.begin(); | |
729 | i != tasks.tasks.end(); | |
730 | i++) | |
731 | { | |
732 | const SolvableVersion &sv = (*i).first; | |
733 | ||
734 | switch ((*i).second) | |
735 | { | |
736 | case SolverTasks::taskInstall: | |
737 | queue_push2(&job, SOLVER_INSTALL | SOLVER_SOLVABLE, sv.id); | |
738 | break; | |
739 | case SolverTasks::taskUninstall: | |
740 | queue_push2(&job, SOLVER_ERASE | SOLVER_SOLVABLE, sv.id); | |
741 | break; | |
742 | case SolverTasks::taskReinstall: | |
743 | // we don't know how to ask solver for this, so we just add the erase | |
744 | // and install later | |
745 | break; | |
9deed604 KB |
746 | case SolverTasks::taskKeep: |
747 | queue_push2(&job, SOLVER_LOCK | SOLVER_SOLVABLE, sv.id); | |
748 | break; | |
bfe593cf KB |
749 | case SolverTasks::taskSkip: |
750 | queue_push2(&job, SOLVER_LOCK | SOLVER_SOLVABLE_NAME, sv.name_id()); | |
751 | break; | |
1d553f34 JT |
752 | default: |
753 | Log (LOG_PLAIN) << "unknown task " << (*i).second << endLog; | |
754 | } | |
755 | } | |
756 | ||
45f5ab69 JT |
757 | // Ask solver to update packages |
758 | switch (update) | |
759 | { | |
760 | case keep: | |
761 | break; | |
762 | ||
763 | case updateBest: | |
764 | // Update to best version | |
765 | queue_push2(&job, SOLVER_UPDATE | SOLVER_SOLVABLE_ALL, 0); | |
766 | break; | |
767 | ||
768 | case updateForce: | |
769 | // Bring installed, non-orphaned packages in sync with the ones in the | |
770 | // repository | |
771 | queue_push2(&job, SOLVER_DISTUPGRADE | SOLVER_SOLVABLE_ALL, 0); | |
772 | break; | |
773 | } | |
1d553f34 | 774 | |
ca8245b2 KB |
775 | // Ask solver to check dependencies of installed packages. |
776 | queue_push2(&job, SOLVER_VERIFY | SOLVER_SOLVABLE_ALL, 0); | |
2506055b JT |
777 | } |
778 | ||
779 | bool | |
780 | SolverSolution::update(SolverTasks &tasks, updateMode update, bool use_test_packages) | |
781 | { | |
782 | Log (LOG_PLAIN) << "solving: " << tasks.tasks.size() << " tasks," << | |
783 | " update: " << (update ? "yes" : "no") << "," << | |
784 | " use test packages: " << (use_test_packages ? "yes" : "no") << endLog; | |
785 | ||
786 | pool.use_test_packages(use_test_packages); | |
787 | ||
788 | queue_free(&job); | |
789 | tasksToJobs(tasks, update, job); | |
ca8245b2 | 790 | |
1d553f34 JT |
791 | if (!solv) |
792 | solv = solver_create(pool.pool); | |
793 | ||
2506055b JT |
794 | return solve(); |
795 | } | |
796 | ||
797 | bool | |
798 | SolverSolution::solve() | |
799 | { | |
1d553f34 JT |
800 | solver_set_flag(solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, 1); |
801 | solver_set_flag(solv, SOLVER_FLAG_ALLOW_DOWNGRADE, 0); | |
45f5ab69 JT |
802 | solver_set_flag(solv, SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE, 1); |
803 | solver_set_flag(solv, SOLVER_FLAG_DUP_ALLOW_DOWNGRADE, 1); | |
1d553f34 | 804 | solver_solve(solv, &job); |
1d553f34 JT |
805 | |
806 | int pcnt = solver_problem_count(solv); | |
807 | solver_printdecisions(solv); | |
808 | ||
2506055b JT |
809 | solutionToTransactionList(); |
810 | ||
811 | return (pcnt == 0); | |
812 | } | |
813 | ||
814 | void | |
815 | SolverSolution::solutionToTransactionList() | |
816 | { | |
1d553f34 JT |
817 | // get transactions for solution |
818 | Transaction *t = solver_create_transaction(solv); | |
819 | transaction_order(t, 0); | |
820 | transaction_print(t); | |
821 | ||
822 | // massage into SolverTransactions form | |
823 | trans.clear(); | |
1d553f34 JT |
824 | for (int i = 0; i < t->steps.count; i++) |
825 | { | |
826 | Id id = t->steps.elements[i]; | |
827 | SolverTransaction::transType tt = type(t, i); | |
45f5ab69 JT |
828 | if (tt != SolverTransaction::transIgnore) |
829 | trans.push_back(SolverTransaction(SolvableVersion(id, pool.pool), tt)); | |
1d553f34 JT |
830 | } |
831 | ||
2506055b JT |
832 | transaction_free(t); |
833 | ||
834 | dumpTransactionList(); | |
835 | } | |
836 | ||
837 | void | |
838 | SolverSolution::augmentTasks(SolverTasks &tasks) | |
839 | { | |
1d553f34 JT |
840 | // add install and remove tasks for anything marked as reinstall |
841 | for (SolverTasks::taskList::const_iterator i = tasks.tasks.begin(); | |
842 | i != tasks.tasks.end(); | |
843 | i++) | |
844 | { | |
845 | const SolvableVersion &sv = (*i).first; | |
846 | ||
847 | if (((*i).second) == SolverTasks::taskReinstall) | |
848 | { | |
849 | trans.push_back(SolverTransaction(SolvableVersion(sv.id, pool.pool), | |
850 | SolverTransaction::transErase)); | |
851 | trans.push_back(SolverTransaction(SolvableVersion(sv.id, pool.pool), | |
852 | SolverTransaction::transInstall)); | |
853 | } | |
854 | } | |
2506055b | 855 | } |
1d553f34 | 856 | |
2506055b JT |
857 | void |
858 | SolverSolution::addSource(bool include_source) | |
859 | { | |
1d553f34 JT |
860 | // if include_source mode is on, also install source for everything we are |
861 | // installing | |
862 | if (include_source) | |
863 | { | |
864 | // (this uses indicies into the vector, as iterators might become | |
865 | // invalidated by doing push_back) | |
866 | size_t n = trans.size(); | |
867 | for (size_t i = 0; i < n; i++) | |
868 | { | |
869 | if (trans[i].type == SolverTransaction::transInstall) | |
870 | { | |
871 | SolvableVersion src_version = trans[i].version.sourcePackage(); | |
872 | if (src_version) | |
873 | trans.push_back(SolverTransaction(src_version, | |
874 | SolverTransaction::transInstall)); | |
875 | } | |
876 | } | |
877 | } | |
2506055b | 878 | } |
1d553f34 | 879 | |
2506055b JT |
880 | void |
881 | SolverSolution::dumpTransactionList() const | |
882 | { | |
1d553f34 JT |
883 | if (trans.size()) |
884 | { | |
885 | Log (LOG_PLAIN) << "Augmented Transaction List:" << endLog; | |
2506055b | 886 | for (SolverTransactionList::const_iterator i = trans.begin (); |
1d553f34 JT |
887 | i != trans.end (); |
888 | ++i) | |
889 | { | |
890 | Log (LOG_PLAIN) << std::setw(4) << std::distance(trans.begin(), i) | |
891 | << std::setw(8) << i->type | |
892 | << std::setw(48) << i->version.Name() | |
893 | << std::setw(20) << i->version.Canonical_version() << endLog; | |
894 | } | |
895 | } | |
2506055b JT |
896 | else |
897 | Log (LOG_PLAIN) << "Augmented Transaction List: is empty" << endLog; | |
898 | } | |
1d553f34 | 899 | |
2506055b JT |
900 | void SolverSolution::applyDefaultProblemSolutions() |
901 | { | |
902 | // adjust the task list with the default solutions | |
903 | int pcnt = solver_problem_count(solv); | |
904 | for (Id problem = 1; problem <= pcnt; problem++) | |
905 | { | |
906 | int scnt = solver_solution_count(solv, problem); | |
907 | solver_take_solution(solv, problem, scnt, &job); | |
908 | } | |
1d553f34 | 909 | |
2506055b JT |
910 | // re-solve |
911 | if (!solve()) | |
912 | Log (LOG_PLAIN) << "default solutions did not solve all problems!" << endLog; | |
1d553f34 JT |
913 | } |
914 | ||
915 | const SolverTransactionList & | |
916 | SolverSolution::transactions() const | |
917 | { | |
918 | return trans; | |
919 | } | |
920 | ||
921 | // Construct a string reporting the problems and solutions | |
922 | std::string | |
923 | SolverSolution::report() const | |
924 | { | |
d76f912c | 925 | packagedb db; |
1d553f34 JT |
926 | std::string r = ""; |
927 | int pcnt = solver_problem_count(solv); | |
928 | for (Id problem = 1; problem <= pcnt; problem++) | |
929 | { | |
930 | r += "Problem " + std::to_string(problem) + "/" + std::to_string(pcnt); | |
931 | r += "\n"; | |
932 | ||
933 | Id probr = solver_findproblemrule(solv, problem); | |
934 | Id dep, source, target; | |
935 | SolverRuleinfo type = solver_ruleinfo(solv, probr, &source, &target, &dep); | |
d76f912c KB |
936 | if (source == db.basepkg.id) |
937 | r += "package " + std::string(pool_dep2str(pool.pool, dep)) + " is a Base package and is therefore required"; | |
938 | else | |
939 | r += solver_problemruleinfo2str(solv, type, source, target, dep); | |
1d553f34 JT |
940 | r += "\n"; |
941 | ||
942 | int scnt = solver_solution_count(solv, problem); | |
943 | for (Id solution = 1; solution <= scnt; solution++) | |
944 | { | |
945 | r += "Solution " + std::to_string(solution) + "/" + std::to_string(scnt); | |
2506055b | 946 | if (solution == scnt) r += " (default)"; |
1d553f34 JT |
947 | r += "\n"; |
948 | ||
949 | Id p, rp, element; | |
950 | element = 0; | |
951 | while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0) | |
952 | { | |
953 | r += " - "; | |
d76f912c KB |
954 | if (p == db.basepkg.id) |
955 | r += "allow deinstallation of Base packages"; | |
956 | else | |
957 | r += solver_solutionelement2str(solv, p, rp); | |
1d553f34 JT |
958 | r += "\n"; |
959 | } | |
960 | } | |
961 | } | |
d14b4325 JT |
962 | |
963 | // since package arch isn't set usefully at the moment, remove all ".any" from | |
964 | // package names in the problem report string. | |
965 | std::string any = ".any"; | |
966 | size_t pos; | |
967 | while ((pos = r.find(any)) != std::string::npos) | |
968 | { | |
969 | r.replace(pos, any.length(), ""); | |
970 | } | |
971 | ||
1d553f34 JT |
972 | return r; |
973 | } | |
974 | ||
975 | // helper function to map transaction type | |
976 | SolverTransaction::transType | |
977 | SolverSolution::type(Transaction *trans, int pos) | |
978 | { | |
979 | Id tt = transaction_type(trans, trans->steps.elements[pos], | |
980 | SOLVER_TRANSACTION_SHOW_ACTIVE); | |
981 | ||
982 | // if active side of transaction is nothing, ask again for passive side of | |
983 | // transaction | |
984 | if (tt == SOLVER_TRANSACTION_IGNORE) | |
985 | tt = transaction_type(trans, trans->steps.elements[pos], 0); | |
986 | ||
987 | switch(tt) | |
988 | { | |
989 | case SOLVER_TRANSACTION_INSTALL: | |
990 | case SOLVER_TRANSACTION_REINSTALL: | |
991 | case SOLVER_TRANSACTION_UPGRADE: | |
992 | case SOLVER_TRANSACTION_DOWNGRADE: | |
993 | return SolverTransaction::transInstall; | |
994 | case SOLVER_TRANSACTION_ERASE: | |
995 | case SOLVER_TRANSACTION_REINSTALLED: | |
996 | case SOLVER_TRANSACTION_UPGRADED: | |
997 | case SOLVER_TRANSACTION_DOWNGRADED: | |
998 | return SolverTransaction::transErase; | |
999 | default: | |
1000 | Log (LOG_PLAIN) << "unknown transaction type " << std::hex << tt << endLog; | |
45f5ab69 JT |
1001 | case SOLVER_TRANSACTION_CHANGE: |
1002 | case SOLVER_TRANSACTION_CHANGED: | |
1d553f34 JT |
1003 | case SOLVER_TRANSACTION_IGNORE: |
1004 | return SolverTransaction::transIgnore; | |
1005 | } | |
1006 | }; |