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