2 * Copyright (c) 2007-2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
21 #include "solver_private.h"
28 #include "solverdebug.h"
30 #define RULES_BLOCK 63
32 static void addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep);
33 static void solver_createcleandepsmap(Solver *solv);
35 /*-------------------------------------------------------------------
36 * Check if dependency is possible
38 * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap
39 * used in solver_addrpmrulesforweak and solver_createcleandepsmap
43 dep_possible(Solver *solv, Id dep, Map *m)
45 Pool *pool = solv->pool;
50 Reldep *rd = GETRELDEP(pool, dep);
53 if (rd->flags == REL_AND)
55 if (!dep_possible(solv, rd->name, m))
57 return dep_possible(solv, rd->evr, m);
59 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
60 return solver_splitprovides(solv, rd->evr);
61 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
62 return solver_dep_installed(solv, rd->evr);
65 FOR_PROVIDES(p, pp, dep)
73 /********************************************************************
77 * - unify rules, remove duplicates
80 /*-------------------------------------------------------------------
82 * compare rules for unification sort
87 unifyrules_sortcmp(const void *ap, const void *bp, void *dp)
97 return x; /* p differs */
100 if (a->d == 0 && b->d == 0)
101 return a->w2 - b->w2; /* assertion: return w2 diff */
103 if (a->d == 0) /* a is assertion, b not */
105 x = a->w2 - pool->whatprovidesdata[b->d];
109 if (b->d == 0) /* b is assertion, a not */
111 x = pool->whatprovidesdata[a->d] - b->w2;
115 /* compare whatprovidesdata */
116 ad = pool->whatprovidesdata + a->d;
117 bd = pool->whatprovidesdata + b->d;
119 if ((x = *ad++ - *bd++) != 0)
125 solver_samerule(Solver *solv, Rule *r1, Rule *r2)
127 return unifyrules_sortcmp(r1, r2, solv->pool);
131 /*-------------------------------------------------------------------
134 * go over all rules and remove duplicates
138 solver_unifyrules(Solver *solv)
140 Pool *pool = solv->pool;
144 if (solv->nrules <= 2) /* nothing to unify */
147 /* sort rules first */
148 solv_sort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp, solv->pool);
155 for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
157 if (jr && !unifyrules_sortcmp(ir, jr, pool))
158 continue; /* prune! */
159 jr = solv->rules + j++; /* keep! */
164 /* reduced count from nrules to j rules */
165 POOL_DEBUG(SOLV_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
167 /* adapt rule buffer */
169 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
172 * debug: log rule statistics
174 IF_POOLDEBUG (SOLV_DEBUG_STATS)
181 for (i = 1; i < solv->nrules; i++)
188 dp = solv->pool->whatprovidesdata + r->d;
193 POOL_DEBUG(SOLV_DEBUG_STATS, " binary: %d\n", binr);
194 POOL_DEBUG(SOLV_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
205 hashrule(Solver *solv, Id p, Id d, int n)
207 unsigned int x = (unsigned int)p;
211 return (x * 37) ^ (unsigned int)d;
212 dp = solv->pool->whatprovidesdata + d;
214 x = (x * 37) ^ (unsigned int)*dp++;
220 /*-------------------------------------------------------------------
226 * p = direct literal; always < 0 for installed rpm rules
227 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
230 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
232 * p < 0 : pkg id of A
233 * d > 0 : Offset in whatprovidesdata (list of providers of b)
235 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
236 * p < 0 : pkg id of A
237 * d < 0 : Id of solvable (e.g. B1)
239 * d == 0: unary rule, assertion => (A) or (-A)
241 * Install: p > 0, d = 0 (A) user requested install
242 * Remove: p < 0, d = 0 (-A) user requested remove (also: uninstallable)
243 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
244 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
245 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
246 * also used for obsoletes
247 * ?: p > 0, d < 0 (A|-B)
248 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
252 * Direct assertion (no watch needed) --> d = 0, w1 = p, w2 = 0
253 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
254 * every other : w1 = p, w2 = whatprovidesdata[d];
255 * Disabled rule: w1 = 0
257 * always returns a rule for non-rpm rules
261 solver_addrule(Solver *solv, Id p, Id d)
263 Pool *pool = solv->pool;
267 int n = 0; /* number of literals in rule - 1
268 0 = direct assertion (single literal)
273 /* it often happenes that requires lead to adding the same rpm rule
274 * multiple times, so we prune those duplicates right away to make
275 * the work for unifyrules a bit easier */
277 if (!solv->rpmrules_end) /* we add rpm rules */
279 r = solv->rules + solv->nrules - 1; /* get the last added rule */
280 if (r->p == p && r->d == d && (d != 0 || !r->w2))
285 * compute number of literals (n) in rule
290 /* always a binary rule */
292 return 0; /* ignore self conflict */
297 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
299 return 0; /* rule is self-fulfilling */
301 if (n == 1) /* convert to binary rule */
305 if (n == 1 && p > d && !solv->rpmrules_end)
307 /* smallest literal first so we can find dups */
308 n = p; p = d; d = n; /* p <-> d */
309 n = 1; /* re-set n, was used as temp var */
313 * check for duplicate
316 /* check if the last added rule (r) is exactly the same as what we're looking for. */
317 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
318 return r; /* binary rule */
320 /* have n-ary rule with same first literal, check other literals */
321 if (r && n > 1 && r->d && r->p == p)
323 /* Rule where d is an offset in whatprovidesdata */
327 dp2 = pool->whatprovidesdata + r->d;
328 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
339 /* extend rule buffer */
340 solv->rules = solv_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
341 r = solv->rules + solv->nrules++; /* point to rule space */
350 /* direct assertion, no watch needed */
366 r->w2 = pool->whatprovidesdata[d];
371 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
373 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, " Add rule: ");
374 solver_printrule(solv, SOLV_DEBUG_RULE_CREATION, r);
381 /******************************************************************************
383 *** rpm rule part: create rules representing the package dependencies
388 * special multiversion patch conflict handling:
389 * a patch conflict is also satisfied if some other
390 * version with the same name/arch that doesn't conflict
391 * gets installed. The generated rule is thus:
392 * -patch|-cpack|opack1|opack2|...
395 makemultiversionconflict(Solver *solv, Id n, Id con)
397 Pool *pool = solv->pool;
402 sn = pool->solvables + n;
403 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
405 FOR_PROVIDES(p, pp, sn->name)
407 s = pool->solvables + p;
408 if (s->name != sn->name || s->arch != sn->arch)
410 if (!MAPTST(&solv->noobsoletes, p))
412 if (pool_match_nevr(pool, pool->solvables + p, con))
414 /* here we have a multiversion solvable that doesn't conflict */
415 /* thus we're not in conflict if it is installed */
419 return -n; /* no other package found, generate normal conflict */
420 return pool_queuetowhatprovides(pool, &q);
424 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
426 if (!solv->ruleinfoq)
427 solver_addrule(solv, p, d);
429 addrpmruleinfo(solv, p, d, type, dep);
432 /*-------------------------------------------------------------------
434 * add (install) rules for solvable
436 * s: Solvable for which to add rules
437 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
439 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
440 * solvables, the edges their dependencies.
441 * Starting from an installed solvable, this will create all rules
442 * representing the graph created by the solvables dependencies.
444 * for unfulfilled requirements, conflicts, obsoletes,....
445 * add a negative assertion for solvables that are not installable
447 * It will also create rules for all solvables referenced by 's'
448 * i.e. descend to all providers of requirements of 's'
453 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
455 Pool *pool = solv->pool;
456 Repo *installed = solv->installed;
458 /* 'work' queue. keeps Ids of solvables we still have to work on.
459 And buffer for it. */
464 /* if to add rules for broken deps ('rpm -V' functionality)
468 /* Id var and pointer for each dependency
469 * (not used in parallel)
476 Id p, pp; /* whatprovides loops */
477 Id *dp; /* ptr to 'whatprovides' */
478 Id n; /* Id for current solvable 's' */
480 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
481 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
483 /* loop until there's no more work left */
488 * s: Pointer to solvable
491 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
494 if (MAPTST(m, n)) /* continue if already visited */
496 MAPSET(m, n); /* mark as visited */
499 s = pool->solvables + n; /* s = Solvable in question */
502 if (installed /* Installed system available */
503 && s->repo == installed /* solvable is installed */
504 && !solv->fixmap_all /* NOT repair errors in rpm dependency graph */
505 && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
507 dontfix = 1; /* dont care about broken rpm deps */
511 && s->arch != ARCH_SRC
512 && s->arch != ARCH_NOSRC
513 && !pool_installable(pool, s))
515 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables));
516 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
519 /* yet another SUSE hack, sigh */
520 if (pool->nscallback && !strncmp("product:", pool_id2str(pool, s->name), 8))
522 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
523 if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
525 addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
526 addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy));
527 if (m && !MAPTST(m, buddy))
528 queue_push(&workq, buddy);
532 /*-----------------------------------------
533 * check requires of s
538 reqp = s->repo->idarraydata + s->requires;
539 while ((req = *reqp++) != 0) /* go through all requires */
541 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
544 /* find list of solvables providing 'req' */
545 dp = pool_whatprovides_ptr(pool, req);
547 if (*dp == SYSTEMSOLVABLE) /* always installed */
552 /* the strategy here is to not insist on dependencies
553 * that are already broken. so if we find one provider
554 * that was already installed, we know that the
555 * dependency was not broken before so we enforce it */
557 /* check if any of the providers for 'req' is installed */
558 for (i = 0; (p = dp[i]) != 0; i++)
560 if (pool->solvables[p].repo == installed)
561 break; /* provider was installed */
563 /* didn't find an installed provider: previously broken dependency */
566 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
573 /* nothing provides req! */
574 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables), pool_dep2str(pool, req));
575 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
579 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
581 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION," %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
582 for (i = 0; dp[i]; i++)
583 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, " provided by %s\n", pool_solvid2str(pool, dp[i]));
586 /* add 'requires' dependency */
587 /* rule: (-requestor|provider1|provider2|...|providerN) */
588 addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
590 /* descend the dependency tree
591 push all non-visited providers on the work queue */
597 queue_push(&workq, *dp);
601 } /* while, requirements of n */
603 } /* if, requirements */
605 /* that's all we check for src packages */
606 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
609 /*-----------------------------------------
610 * check conflicts of s
617 /* we treat conflicts in patches a bit differen:
619 * - multiversion handling
620 * XXX: we should really handle this different, looking
621 * at the name is a bad hack
623 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
625 conp = s->repo->idarraydata + s->conflicts;
626 /* foreach conflicts of 's' */
627 while ((con = *conp++) != 0)
629 /* foreach providers of a conflict of 's' */
630 FOR_PROVIDES(p, pp, con)
632 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
634 /* dontfix: dont care about conflicts with already installed packs */
635 if (dontfix && pool->solvables[p].repo == installed)
637 /* p == n: self conflict */
638 if (p == n && !pool->allowselfconflicts)
642 Reldep *rd = GETRELDEP(pool, con);
643 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
646 p = 0; /* make it a negative assertion, aka 'uninstallable' */
648 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
650 /* our patch conflicts with a noobsoletes (aka multiversion) package */
651 p = -makemultiversionconflict(solv, p, con);
653 /* rule: -n|-p: either solvable _or_ provider of conflict */
654 addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
659 /*-----------------------------------------
660 * check obsoletes and implicit obsoletes of a package
661 * if ignoreinstalledsobsoletes is not set, we're also checking
662 * obsoletes of installed packages (like newer rpm versions)
664 if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
666 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
667 int isinstalled = (installed && s->repo == installed);
668 if (s->obsoletes && !noobs)
670 obsp = s->repo->idarraydata + s->obsoletes;
671 /* foreach obsoletes */
672 while ((obs = *obsp++) != 0)
674 /* foreach provider of an obsoletes of 's' */
675 FOR_PROVIDES(p, pp, obs)
677 Solvable *ps = pool->solvables + p;
680 if (isinstalled && dontfix && ps->repo == installed)
681 continue; /* don't repair installed/installed problems */
682 if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
683 && !pool_match_nevr(pool, ps, obs))
685 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
688 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
690 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
694 /* check implicit obsoletes
695 * for installed packages we only need to check installed/installed problems (and
696 * only when dontfix is not set), as the others are picked up when looking at the
697 * uninstalled package.
699 if (!isinstalled || !dontfix)
701 FOR_PROVIDES(p, pp, s->name)
703 Solvable *ps = pool->solvables + p;
706 if (isinstalled && ps->repo != installed)
708 /* we still obsolete packages with same nevra, like rpm does */
709 /* (actually, rpm mixes those packages. yuck...) */
710 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
712 if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
714 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
716 if (s->name == ps->name)
717 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
719 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
724 /*-----------------------------------------
725 * add recommends to the work queue
727 if (s->recommends && m)
729 recp = s->repo->idarraydata + s->recommends;
730 while ((rec = *recp++) != 0)
732 FOR_PROVIDES(p, pp, rec)
734 queue_push(&workq, p);
737 if (s->suggests && m)
739 sugp = s->repo->idarraydata + s->suggests;
740 while ((sug = *sugp++) != 0)
742 FOR_PROVIDES(p, pp, sug)
744 queue_push(&workq, p);
752 /*-------------------------------------------------------------------
754 * Add rules for packages possibly selected in by weak dependencies
756 * m: already added solvables
760 solver_addrpmrulesforweak(Solver *solv, Map *m)
762 Pool *pool = solv->pool;
767 /* foreach solvable in pool */
768 for (i = n = 1; n < pool->nsolvables; i++, n++)
770 if (i == pool->nsolvables) /* wrap i */
772 if (MAPTST(m, i)) /* already added that one */
775 s = pool->solvables + i;
776 if (!pool_installable(pool, s)) /* only look at installable ones */
782 /* find possible supplements */
783 supp = s->repo->idarraydata + s->supplements;
784 while ((sup = *supp++) != 0)
785 if (dep_possible(solv, sup, m))
789 /* if nothing found, check for enhances */
790 if (!sup && s->enhances)
792 supp = s->repo->idarraydata + s->enhances;
793 while ((sup = *supp++) != 0)
794 if (dep_possible(solv, sup, m))
797 /* if nothing found, goto next solvables */
800 solver_addrpmrulesforsolvable(solv, s, m);
801 n = 0; /* check all solvables again because we added solvables to m */
806 /*-------------------------------------------------------------------
808 * add package rules for possible updates
811 * m: map of already visited solvables
812 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
816 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
818 Pool *pool = solv->pool;
820 /* queue and buffer for it */
824 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
825 /* find update candidates for 's' */
826 policy_findupdatepackages(solv, s, &qs, allow_all);
827 /* add rule for 's' if not already done */
828 if (!MAPTST(m, s - pool->solvables))
829 solver_addrpmrulesforsolvable(solv, s, m);
830 /* foreach update candidate, add rule if not already done */
831 for (i = 0; i < qs.count; i++)
832 if (!MAPTST(m, qs.elements[i]))
833 solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
838 /***********************************************************************
840 *** Update/Feature rule part
842 *** Those rules make sure an installed package isn't silently deleted
847 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
849 Pool *pool = solv->pool;
852 policy_findupdatepackages(solv, s, qs, allow_all ? allow_all : 2);
856 return 0; /* orphaned, don't create feature rule */
857 /* check if this is an orphaned package */
858 policy_findupdatepackages(solv, s, qs, 1);
860 return 0; /* orphaned, don't create update rule */
862 return -SYSTEMSOLVABLE; /* supported but not installable */
865 return s - pool->solvables;
866 /* check if it is ok to keep the installed package */
867 for (i = 0; i < qs->count; i++)
869 Solvable *ns = pool->solvables + qs->elements[i];
870 if (s->evr == ns->evr && solvable_identical(s, ns))
871 return s - pool->solvables;
873 /* nope, it must be some other package */
874 return -SYSTEMSOLVABLE;
877 /* add packages from the dup repositories to the update candidates
878 * this isn't needed for the global dup mode as all packages are
879 * from dup repos in that case */
881 addduppackages(Solver *solv, Solvable *s, Queue *qs)
886 int oldnoupdateprovide = solv->noupdateprovide;
888 queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
889 solv->noupdateprovide = 1;
890 policy_findupdatepackages(solv, s, &dupqs, 2);
891 solv->noupdateprovide = oldnoupdateprovide;
892 for (i = 0; i < dupqs.count; i++)
894 p = dupqs.elements[i];
895 if (MAPTST(&solv->dupmap, p))
896 queue_pushunique(qs, p);
901 /*-------------------------------------------------------------------
903 * add rule for update
904 * (A|A1|A2|A3...) An = update candidates for A
906 * s = (installed) solvable
910 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
912 /* installed packages get a special upgrade allowed rule */
913 Pool *pool = solv->pool;
918 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
919 p = s - pool->solvables;
920 /* find update candidates for 's' */
921 if (solv->dupmap_all)
922 p = finddistupgradepackages(solv, s, &qs, allow_all);
924 policy_findupdatepackages(solv, s, &qs, allow_all);
925 if (!allow_all && !solv->dupmap_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
926 addduppackages(solv, s, &qs);
928 if (!allow_all && qs.count && solv->noobsoletes.size)
932 d = pool_queuetowhatprovides(pool, &qs);
933 /* filter out all noobsoletes packages as they don't update */
934 for (i = j = 0; i < qs.count; i++)
936 if (MAPTST(&solv->noobsoletes, qs.elements[i]))
938 /* it's ok if they have same nevra */
939 Solvable *ps = pool->solvables + qs.elements[i];
940 if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
943 qs.elements[j++] = qs.elements[i];
947 if (d && solv->installed && s->repo == solv->installed &&
948 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
950 if (!solv->multiversionupdaters)
951 solv->multiversionupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
952 solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
954 if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
956 queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
962 if (qs.count && p == -SYSTEMSOLVABLE)
963 p = queue_shift(&qs);
964 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
966 solver_addrule(solv, p, d); /* allow update of s */
970 disableupdaterule(Solver *solv, Id p)
974 MAPSET(&solv->noupdate, p - solv->installed->start);
975 r = solv->rules + solv->updaterules + (p - solv->installed->start);
976 if (r->p && r->d >= 0)
977 solver_disablerule(solv, r);
978 r = solv->rules + solv->featurerules + (p - solv->installed->start);
979 if (r->p && r->d >= 0)
980 solver_disablerule(solv, r);
984 reenableupdaterule(Solver *solv, Id p)
986 Pool *pool = solv->pool;
989 MAPCLR(&solv->noupdate, p - solv->installed->start);
990 r = solv->rules + solv->updaterules + (p - solv->installed->start);
995 solver_enablerule(solv, r);
996 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
998 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
999 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1003 r = solv->rules + solv->featurerules + (p - solv->installed->start);
1004 if (r->p && r->d < 0)
1006 solver_enablerule(solv, r);
1007 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1009 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1010 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1016 /***********************************************************************
1018 *** Infarch rule part
1020 *** Infarch rules make sure the solver uses the best architecture of
1021 *** a package if multiple archetectures are available
1026 solver_addinfarchrules(Solver *solv, Map *addedmap)
1028 Pool *pool = solv->pool;
1030 Id p, pp, a, aa, bestarch;
1031 Solvable *s, *ps, *bests;
1032 Queue badq, allowedarchs;
1035 queue_init(&allowedarchs);
1036 solv->infarchrules = solv->nrules;
1037 for (i = 1; i < pool->nsolvables; i++)
1039 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1041 s = pool->solvables + i;
1045 queue_empty(&allowedarchs);
1046 FOR_PROVIDES(p, pp, s->name)
1048 ps = pool->solvables + p;
1049 if (ps->name != s->name || !MAPTST(addedmap, p))
1056 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1057 if (a != 1 && pool->installed && ps->repo == pool->installed)
1059 if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1060 queue_pushunique(&allowedarchs, ps->arch); /* also ok to keep this architecture */
1061 continue; /* ignore installed solvables when calculating the best arch */
1063 if (a && a != 1 && (!bestarch || a < bestarch))
1071 /* speed up common case where installed package already has best arch */
1072 if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1073 allowedarchs.count--; /* installed arch is best */
1075 FOR_PROVIDES(p, pp, s->name)
1077 ps = pool->solvables + p;
1078 if (ps->name != s->name || !MAPTST(addedmap, p))
1081 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1082 if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1084 if (pool->installed && ps->repo == pool->installed)
1085 continue; /* always ok to keep an installed package */
1086 for (j = 0; j < allowedarchs.count; j++)
1088 aa = allowedarchs.elements[j];
1091 aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1092 if (aa && ((a ^ aa) & 0xffff0000) == 0)
1093 break; /* compatible */
1095 if (j == allowedarchs.count)
1096 queue_push(&badq, p);
1101 /* block all solvables in the badq! */
1102 for (j = 0; j < badq.count; j++)
1104 p = badq.elements[j];
1105 solver_addrule(solv, -p, 0);
1109 queue_free(&allowedarchs);
1110 solv->infarchrules_end = solv->nrules;
1114 disableinfarchrule(Solver *solv, Id name)
1116 Pool *pool = solv->pool;
1119 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1121 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1122 solver_disablerule(solv, r);
1127 reenableinfarchrule(Solver *solv, Id name)
1129 Pool *pool = solv->pool;
1132 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1134 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1136 solver_enablerule(solv, r);
1137 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1139 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1140 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1147 /***********************************************************************
1151 *** Dup rules make sure a package is selected from the specified dup
1152 *** repositories if an update candidate is included in one of them.
1157 solver_createdupmaps(Solver *solv)
1159 Queue *job = &solv->job;
1160 Pool *pool = solv->pool;
1162 Id how, what, p, pi, pp, obs, *obsp;
1166 map_init(&solv->dupmap, pool->nsolvables);
1167 map_init(&solv->dupinvolvedmap, pool->nsolvables);
1168 for (i = 0; i < job->count; i += 2)
1170 how = job->elements[i];
1171 what = job->elements[i + 1];
1172 switch (how & SOLVER_JOBMASK)
1174 case SOLVER_DISTUPGRADE:
1175 if ((how & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_REPO)
1177 if (what <= 0 || what > pool->nrepos)
1179 repo = pool_id2repo(pool, what);
1180 FOR_REPO_SOLVABLES(repo, p, s)
1182 MAPSET(&solv->dupmap, p);
1183 FOR_PROVIDES(pi, pp, s->name)
1185 ps = pool->solvables + pi;
1186 if (ps->name != s->name)
1188 MAPSET(&solv->dupinvolvedmap, pi);
1192 /* FIXME: check obsoletes/provides combination */
1193 obsp = s->repo->idarraydata + s->obsoletes;
1194 while ((obs = *obsp++) != 0)
1196 FOR_PROVIDES(pi, pp, obs)
1198 Solvable *pis = pool->solvables + pi;
1199 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pis, obs))
1201 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, pis))
1203 MAPSET(&solv->dupinvolvedmap, pi);
1213 MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1217 solver_freedupmaps(Solver *solv)
1219 map_free(&solv->dupmap);
1220 /* we no longer free solv->dupinvolvedmap as we need it in
1221 * policy's priority pruning code. sigh. */
1225 solver_addduprules(Solver *solv, Map *addedmap)
1227 Pool *pool = solv->pool;
1232 solv->duprules = solv->nrules;
1233 for (i = 1; i < pool->nsolvables; i++)
1235 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1237 s = pool->solvables + i;
1239 FOR_PROVIDES(p, pp, s->name)
1241 ps = pool->solvables + p;
1242 if (ps->name != s->name || !MAPTST(addedmap, p))
1248 if (!MAPTST(&solv->dupinvolvedmap, p))
1250 if (solv->installed && ps->repo == solv->installed)
1252 if (!solv->updatemap.size)
1253 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1254 MAPSET(&solv->updatemap, p - solv->installed->start);
1255 if (!MAPTST(&solv->dupmap, p))
1258 /* is installed identical to a good one? */
1259 FOR_PROVIDES(ip, ipp, ps->name)
1261 Solvable *is = pool->solvables + ip;
1262 if (!MAPTST(&solv->dupmap, ip))
1264 if (is->evr == ps->evr && solvable_identical(ps, is))
1268 solver_addrule(solv, -p, 0); /* no match, sorry */
1271 else if (!MAPTST(&solv->dupmap, p))
1272 solver_addrule(solv, -p, 0);
1275 solv->duprules_end = solv->nrules;
1280 disableduprule(Solver *solv, Id name)
1282 Pool *pool = solv->pool;
1285 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1287 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1288 solver_disablerule(solv, r);
1293 reenableduprule(Solver *solv, Id name)
1295 Pool *pool = solv->pool;
1298 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1300 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1302 solver_enablerule(solv, r);
1303 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1305 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1306 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1313 /***********************************************************************
1315 *** Policy rule disabling/reenabling
1317 *** Disable all policy rules that conflict with our jobs. If a job
1318 *** gets disabled later on, reenable the involved policy rules again.
1322 #define DISABLE_UPDATE 1
1323 #define DISABLE_INFARCH 2
1324 #define DISABLE_DUP 3
1327 * add all installed packages that package p obsoletes to Queue q.
1328 * Package p is not installed and not in oobs map.
1329 * Entries may get added multiple times.
1332 add_obsoletes(Solver *solv, Id p, Queue *q)
1334 Pool *pool = solv->pool;
1335 Repo *installed = solv->installed;
1337 Solvable *s = pool->solvables + p;
1341 /* we already know: p is not installed, p is not noobs */
1342 FOR_PROVIDES(p2, pp2, s->name)
1344 Solvable *ps = pool->solvables + p2;
1345 if (ps->repo != installed)
1347 if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1349 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1356 obsp = s->repo->idarraydata + s->obsoletes;
1357 while ((obs = *obsp++) != 0)
1358 FOR_PROVIDES(p2, pp2, obs)
1360 Solvable *ps = pool->solvables + p2;
1361 if (ps->repo != installed)
1363 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1365 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1375 * Call add_obsoletes and intersect the result with the
1376 * elements in Queue q starting at qstart.
1377 * Assumes that it's the first call if qstart == q->count.
1378 * May use auxillary map m for the intersection process, all
1379 * elements of q starting at qstart must have their bit cleared.
1380 * (This is also true after the function returns.)
1383 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
1386 int qcount = q->count;
1388 add_obsoletes(solv, p, q);
1389 if (qcount == qstart)
1390 return; /* first call */
1391 if (qcount == q->count)
1393 else if (qcount == qstart + 1)
1395 /* easy if there's just one element */
1397 for (i = qcount; i < q->count; i++)
1398 if (q->elements[i] == q->elements[qstart])
1400 j++; /* keep the element */
1404 else if (!m->size && q->count - qstart <= 8)
1406 /* faster than a map most of the time */
1408 for (i = j = qstart; i < qcount; i++)
1410 Id ip = q->elements[i];
1411 for (k = qcount; k < q->count; k++)
1412 if (q->elements[k] == ip)
1414 q->elements[j++] = ip;
1421 /* for the really pathologic cases we use the map */
1422 Repo *installed = solv->installed;
1424 map_init(m, installed->end - installed->start);
1425 for (i = qcount; i < q->count; i++)
1426 MAPSET(m, q->elements[i] - installed->start);
1427 for (i = j = qstart; i < qcount; i++)
1428 if (MAPTST(m, q->elements[i] - installed->start))
1430 MAPCLR(m, q->elements[i] - installed->start);
1431 q->elements[j++] = q->elements[i];
1434 queue_truncate(q, j);
1438 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1440 Pool *pool = solv->pool;
1444 int i, j, set, qstart;
1447 installed = solv->installed;
1448 select = how & SOLVER_SELECTMASK;
1449 switch (how & SOLVER_JOBMASK)
1451 case SOLVER_INSTALL:
1452 set = how & SOLVER_SETMASK;
1453 if (!(set & SOLVER_NOAUTOSET))
1455 /* automatically add set bits by analysing the job */
1456 if (select == SOLVER_SOLVABLE)
1457 set |= SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
1458 else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
1460 Reldep *rd = GETRELDEP(pool, what);
1461 if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
1463 #if !defined(DEBIAN_SEMANTICS)
1464 const char *evr = pool_id2str(pool, rd->evr);
1465 if (strchr(evr, '-'))
1466 set |= SOLVER_SETEVR;
1468 set |= SOLVER_SETEV;
1470 set |= SOLVER_SETEVR;
1473 if (rd->flags <= 7 && ISRELDEP(rd->name))
1474 rd = GETRELDEP(pool, rd->name);
1475 if (rd->flags == REL_ARCH)
1476 set |= SOLVER_SETARCH;
1480 set &= ~SOLVER_NOAUTOSET;
1483 if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
1485 if (select == SOLVER_SOLVABLE)
1486 queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
1489 int qcnt = q->count;
1490 FOR_JOB_SELECT(p, pp, select, what)
1492 s = pool->solvables + p;
1494 for (i = qcnt; i < q->count; i += 2)
1495 if (q->elements[i + 1] == s->name)
1499 queue_push2(q, DISABLE_INFARCH, s->name);
1503 if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
1505 if (select == SOLVER_SOLVABLE)
1506 queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
1509 int qcnt = q->count;
1510 FOR_JOB_SELECT(p, pp, select, what)
1512 s = pool->solvables + p;
1514 for (i = qcnt; i < q->count; i += 2)
1515 if (q->elements[i + 1] == s->name)
1519 queue_push2(q, DISABLE_DUP, s->name);
1523 if (!installed || installed->end == installed->start)
1525 /* now the hard part: disable some update rules */
1527 /* first check if we have noobs or installed packages in the job */
1529 FOR_JOB_SELECT(p, pp, select, what)
1531 if (pool->solvables[p].repo == installed)
1533 else if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1537 if (j) /* have installed packages */
1539 /* this is for dupmap_all jobs, it can go away if we create
1540 * duprules for them */
1541 if (i == 1 && (set & SOLVER_SETREPO) != 0)
1542 queue_push2(q, DISABLE_UPDATE, j);
1548 FOR_JOB_SELECT(p, pp, select, what)
1550 intersect_obsoletes(solv, p, q, qstart, &omap);
1551 if (q->count == qstart)
1557 if (qstart == q->count)
1558 return; /* nothing to prune */
1560 /* convert result to (DISABLE_UPDATE, p) pairs */
1562 for (j = qstart; j < i; j++)
1563 queue_push(q, q->elements[j]);
1564 for (j = qstart; j < q->count; j += 2)
1566 q->elements[j] = DISABLE_UPDATE;
1567 q->elements[j + 1] = q->elements[i++];
1570 /* now that we know which installed packages are obsoleted check each of them */
1571 if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
1572 return; /* all is set, nothing to do */
1574 for (i = j = qstart; i < q->count; i += 2)
1576 Solvable *is = pool->solvables + q->elements[i + 1];
1577 FOR_JOB_SELECT(p, pp, select, what)
1580 s = pool->solvables + p;
1581 if ((set & SOLVER_SETEVR) != 0)
1582 illegal |= POLICY_ILLEGAL_DOWNGRADE; /* ignore */
1583 if ((set & SOLVER_SETARCH) != 0)
1584 illegal |= POLICY_ILLEGAL_ARCHCHANGE; /* ignore */
1585 if ((set & SOLVER_SETVENDOR) != 0)
1586 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
1587 illegal = policy_is_illegal(solv, is, s, illegal);
1588 if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
1590 /* it's ok if the EV is different */
1591 if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
1599 /* no package conflicts with the update rule */
1600 /* thus keep the DISABLE_UPDATE */
1601 q->elements[j + 1] = q->elements[i + 1];
1605 queue_truncate(q, j);
1611 FOR_JOB_SELECT(p, pp, select, what)
1612 if (pool->solvables[p].repo == installed)
1613 queue_push2(q, DISABLE_UPDATE, p);
1620 /* disable all policy rules that are in conflict with our job list */
1622 solver_disablepolicyrules(Solver *solv)
1624 Queue *job = &solv->job;
1631 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1633 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1635 r = solv->rules + i;
1636 if (r->d < 0) /* disabled? */
1638 j = solv->ruletojob.elements[i - solv->jobrules];
1642 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1644 if (solv->cleandepsmap.size)
1646 solver_createcleandepsmap(solv);
1647 for (i = solv->installed->start; i < solv->installed->end; i++)
1648 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1649 queue_push2(&allq, DISABLE_UPDATE, i);
1651 MAPZERO(&solv->noupdate);
1652 for (i = 0; i < allq.count; i += 2)
1654 Id type = allq.elements[i], arg = allq.elements[i + 1];
1657 case DISABLE_UPDATE:
1658 disableupdaterule(solv, arg);
1660 case DISABLE_INFARCH:
1661 disableinfarchrule(solv, arg);
1664 disableduprule(solv, arg);
1673 /* we just disabled job #jobidx, now reenable all policy rules that were
1674 * disabled because of this job */
1676 solver_reenablepolicyrules(Solver *solv, int jobidx)
1678 Queue *job = &solv->job;
1683 Id qbuf[32], allqbuf[128];
1685 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1686 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1687 jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
1690 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1692 r = solv->rules + i;
1693 if (r->d < 0) /* disabled? */
1695 j = solv->ruletojob.elements[i - solv->jobrules];
1699 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1701 if (solv->cleandepsmap.size)
1703 solver_createcleandepsmap(solv);
1704 for (i = solv->installed->start; i < solv->installed->end; i++)
1705 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1706 queue_push2(&allq, DISABLE_UPDATE, i);
1708 for (j = 0; j < q.count; j += 2)
1710 Id type = q.elements[j], arg = q.elements[j + 1];
1711 for (i = 0; i < allq.count; i += 2)
1712 if (allq.elements[i] == type && allq.elements[i + 1] == arg)
1715 continue; /* still disabled */
1718 case DISABLE_UPDATE:
1719 reenableupdaterule(solv, arg);
1721 case DISABLE_INFARCH:
1722 reenableinfarchrule(solv, arg);
1725 reenableduprule(solv, arg);
1734 /***********************************************************************
1736 *** Rule info part, tell the user what the rule is about.
1741 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1743 Pool *pool = solv->pool;
1747 /* check if this creates the rule we're searching for */
1748 r = solv->rules + solv->ruleinfoq->elements[0];
1750 od = r->d < 0 ? -r->d - 1 : r->d;
1755 if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1757 w2 = pool->whatprovidesdata[d];
1761 if (p > 0 && d < 0) /* this hack is used for buddy deps */
1773 Id *dp = pool->whatprovidesdata + d;
1774 Id *odp = pool->whatprovidesdata + od;
1776 if (*dp++ != *odp++)
1782 /* handle multiversion conflict rules */
1783 if (p < 0 && pool->whatprovidesdata[d] < 0)
1785 w2 = pool->whatprovidesdata[d];
1786 /* XXX: free memory */
1796 if (w2 != op || p != ow2)
1801 if (p != op || w2 != ow2)
1805 /* yep, rule matches. record info */
1806 queue_push(solv->ruleinfoq, type);
1807 if (type == SOLVER_RULE_RPM_SAME_NAME)
1809 /* we normalize same name order */
1810 queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
1811 queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
1815 queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
1816 queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
1818 queue_push(solv->ruleinfoq, dep);
1822 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
1824 const Id *a = ap, *b = bp;
1843 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
1845 Pool *pool = solv->pool;
1846 Rule *r = solv->rules + rid;
1850 if (rid <= 0 || rid >= solv->rpmrules_end)
1852 Id type, from, to, dep;
1853 type = solver_ruleinfo(solv, rid, &from, &to, &dep);
1854 queue_push(rq, type);
1855 queue_push(rq, from);
1857 queue_push(rq, dep);
1862 queue_push(rq, rid);
1863 solv->ruleinfoq = rq;
1864 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1865 /* also try reverse direction for conflicts */
1866 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1867 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1868 solv->ruleinfoq = 0;
1870 /* now sort & unify em */
1873 solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
1874 /* throw out identical entries */
1875 for (i = j = 0; i < rq->count; i += 4)
1879 if (rq->elements[i] == rq->elements[j - 4] &&
1880 rq->elements[i + 1] == rq->elements[j - 3] &&
1881 rq->elements[i + 2] == rq->elements[j - 2] &&
1882 rq->elements[i + 3] == rq->elements[j - 1])
1885 rq->elements[j++] = rq->elements[i];
1886 rq->elements[j++] = rq->elements[i + 1];
1887 rq->elements[j++] = rq->elements[i + 2];
1888 rq->elements[j++] = rq->elements[i + 3];
1895 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
1897 Pool *pool = solv->pool;
1898 Rule *r = solv->rules + rid;
1899 SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
1907 if (rid > 0 && rid < solv->rpmrules_end)
1913 return SOLVER_RULE_RPM;
1917 queue_push(&rq, rid);
1918 solv->ruleinfoq = &rq;
1919 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1920 /* also try reverse direction for conflicts */
1921 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1922 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1923 solv->ruleinfoq = 0;
1924 type = SOLVER_RULE_RPM;
1925 for (i = 1; i < rq.count; i += 4)
1928 qt = rq.elements[i];
1929 qp = rq.elements[i + 1];
1930 qo = rq.elements[i + 2];
1931 qd = rq.elements[i + 3];
1932 if (type == SOLVER_RULE_RPM || type > qt)
1946 if (rid >= solv->jobrules && rid < solv->jobrules_end)
1948 Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
1952 *top = solv->job.elements[jidx];
1954 *depp = solv->job.elements[jidx + 1];
1955 if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (solv->job.elements[jidx] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
1956 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
1957 return SOLVER_RULE_JOB;
1959 if (rid >= solv->updaterules && rid < solv->updaterules_end)
1962 *fromp = solv->installed->start + (rid - solv->updaterules);
1963 return SOLVER_RULE_UPDATE;
1965 if (rid >= solv->featurerules && rid < solv->featurerules_end)
1968 *fromp = solv->installed->start + (rid - solv->featurerules);
1969 return SOLVER_RULE_FEATURE;
1971 if (rid >= solv->duprules && rid < solv->duprules_end)
1976 *depp = pool->solvables[-r->p].name;
1977 return SOLVER_RULE_DISTUPGRADE;
1979 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
1984 *depp = pool->solvables[-r->p].name;
1985 return SOLVER_RULE_INFARCH;
1987 if (rid >= solv->choicerules && rid < solv->choicerules_end)
1989 return SOLVER_RULE_CHOICE;
1991 if (rid >= solv->learntrules)
1993 return SOLVER_RULE_LEARNT;
1995 return SOLVER_RULE_UNKNOWN;
1999 solver_ruleclass(Solver *solv, Id rid)
2002 return SOLVER_RULE_UNKNOWN;
2003 if (rid > 0 && rid < solv->rpmrules_end)
2004 return SOLVER_RULE_RPM;
2005 if (rid >= solv->jobrules && rid < solv->jobrules_end)
2006 return SOLVER_RULE_JOB;
2007 if (rid >= solv->updaterules && rid < solv->updaterules_end)
2008 return SOLVER_RULE_UPDATE;
2009 if (rid >= solv->featurerules && rid < solv->featurerules_end)
2010 return SOLVER_RULE_FEATURE;
2011 if (rid >= solv->duprules && rid < solv->duprules_end)
2012 return SOLVER_RULE_DISTUPGRADE;
2013 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2014 return SOLVER_RULE_INFARCH;
2015 if (rid >= solv->choicerules && rid < solv->choicerules_end)
2016 return SOLVER_RULE_CHOICE;
2017 if (rid >= solv->learntrules)
2018 return SOLVER_RULE_LEARNT;
2019 return SOLVER_RULE_UNKNOWN;
2023 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2025 Pool *pool = solv->pool;
2030 r = solv->rules + rid;
2031 FOR_RULELITERALS(p, pp, r)
2032 if (p != -SYSTEMSOLVABLE)
2035 queue_push(q, -SYSTEMSOLVABLE); /* hmm, better to return an empty result? */
2039 solver_rule2jobidx(Solver *solv, Id rid)
2041 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2043 return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2047 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2050 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2056 idx = solv->ruletojob.elements[rid - solv->jobrules];
2058 *whatp = solv->job.elements[idx + 1];
2059 return solv->job.elements[idx];
2063 solver_addchoicerules(Solver *solv)
2065 Pool *pool = solv->pool;
2069 int i, j, rid, havechoice;
2074 solv->choicerules = solv->nrules;
2075 if (!pool->installed)
2077 solv->choicerules_end = solv->nrules;
2080 solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2083 map_init(&m, pool->nsolvables);
2084 map_init(&mneg, pool->nsolvables);
2085 /* set up negative assertion map from infarch and dup rules */
2086 for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2087 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2088 MAPSET(&mneg, -r->p);
2089 for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2090 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2091 MAPSET(&mneg, -r->p);
2092 for (rid = 1; rid < solv->rpmrules_end ; rid++)
2094 r = solv->rules + rid;
2095 if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2096 continue; /* only look at requires rules */
2097 /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2101 FOR_RULELITERALS(p, pp, r)
2105 s = pool->solvables + p;
2108 if (s->repo == pool->installed)
2113 /* check if this package is "blocked" by a installed package */
2115 FOR_PROVIDES(p2, pp2, s->name)
2117 s2 = pool->solvables + p2;
2118 if (s2->repo != pool->installed)
2120 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2122 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2128 /* found installed package p2 that we can update to p */
2129 if (MAPTST(&mneg, p))
2131 if (policy_is_illegal(solv, s2, s, 0))
2133 queue_push(&qi, p2);
2139 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2141 while ((obs = *obsp++) != 0)
2143 FOR_PROVIDES(p2, pp2, obs)
2145 s2 = pool->solvables + p2;
2146 if (s2->repo != pool->installed)
2148 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2150 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2159 /* found installed package p2 that we can update to p */
2160 if (MAPTST(&mneg, p))
2162 if (policy_is_illegal(solv, s2, s, 0))
2164 queue_push(&qi, p2);
2169 /* package p is independent of the installed ones */
2172 if (!havechoice || !q.count)
2173 continue; /* no choice */
2175 /* now check the update rules of the installed package.
2176 * if all packages of the update rules are contained in
2177 * the dependency rules, there's no need to set up the choice rule */
2179 FOR_RULELITERALS(p, pp, r)
2182 for (i = 0; i < qi.count; i++)
2185 if (!qi.elements[i])
2187 ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2189 ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2192 FOR_RULELITERALS(p, pp, ur)
2197 for (j = i + 1; j < qi.count; j++)
2198 if (qi.elements[i] == qi.elements[j])
2204 printf("skipping choice ");
2205 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2209 d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
2210 solver_addrule(solv, r->p, d);
2211 queue_push(&solv->weakruleq, solv->nrules - 1);
2212 solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
2215 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2216 printf("WEAK CHOICE ");
2217 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
2224 solv->choicerules_end = solv->nrules;
2227 /* called when a choice rule is disabled by analyze_unsolvable. We also
2228 * have to disable all other choice rules so that the best packages get
2231 solver_disablechoicerules(Solver *solv, Rule *r)
2234 Pool *pool = solv->pool;
2238 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2239 map_init(&m, pool->nsolvables);
2240 FOR_RULELITERALS(p, pp, or)
2243 FOR_RULELITERALS(p, pp, r)
2246 for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
2248 r = solv->rules + rid;
2251 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2252 FOR_RULELITERALS(p, pp, or)
2253 if (p > 0 && MAPTST(&m, p))
2256 solver_disablerule(solv, r);
2260 #undef CLEANDEPSDEBUG
2263 * This functions collects all packages that are looked at
2264 * when a dependency is checked. We need it to "pin" installed
2265 * packages when removing a supplemented package in createcleandepsmap.
2266 * Here's an not uncommon example:
2267 * A contains "Supplements: packageand(B, C)"
2268 * B contains "Requires: A"
2269 * Now if we remove C, the supplements is no longer true,
2270 * thus we also remove A. Without the dep_pkgcheck function, we
2271 * would now also remove B, but this is wrong, as adding back
2272 * C doesn't make the supplements true again. Thus we "pin" B
2274 * There's probably a better way to do this, but I haven't come
2278 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
2280 Pool *pool = solv->pool;
2285 Reldep *rd = GETRELDEP(pool, dep);
2288 if (rd->flags == REL_AND)
2290 dep_pkgcheck(solv, rd->name, m, q);
2291 dep_pkgcheck(solv, rd->evr, m, q);
2294 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2296 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2300 FOR_PROVIDES(p, pp, dep)
2306 * Find all installed packages that are no longer
2307 * needed regarding the current solver job.
2310 * - remove pass: remove all packages that could have
2311 * been dragged in by the obsoleted packages.
2312 * i.e. if package A is obsolete and contains "Requires: B",
2313 * also remove B, as installing A will have pulled in B.
2314 * after this pass, we have a set of still installed packages
2315 * with broken dependencies.
2317 * now add back all packages that the still installed packages
2320 * The cleandeps packages are the packages removed in the first
2321 * pass and not added back in the second pass.
2324 solver_createcleandepsmap(Solver *solv)
2326 Pool *pool = solv->pool;
2327 Repo *installed = solv->installed;
2328 Queue *job = &solv->job;
2333 Id rid, how, what, select;
2335 Id req, *reqp, sup, *supp;
2340 if (installed->end == installed->start)
2342 map_init(&userinstalled, installed->end - installed->start);
2343 map_init(&im, pool->nsolvables);
2344 map_init(&installedm, pool->nsolvables);
2345 map_empty(&solv->cleandepsmap);
2348 for (i = 0; i < job->count; i += 2)
2350 how = job->elements[i];
2351 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2353 what = job->elements[i + 1];
2354 select = how & SOLVER_SELECTMASK;
2355 FOR_JOB_SELECT(p, pp, select, what)
2356 if (pool->solvables[p].repo == installed)
2357 MAPSET(&userinstalled, p - installed->start);
2360 /* add all positive elements (e.g. locks) to "userinstalled" */
2361 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2363 r = solv->rules + rid;
2366 i = solv->ruletojob.elements[rid - solv->jobrules];
2367 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2369 FOR_RULELITERALS(p, jp, r)
2370 if (p > 0 && pool->solvables[p].repo == installed)
2371 MAPSET(&userinstalled, p - installed->start);
2374 /* add all cleandeps candidates to iq */
2375 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2377 r = solv->rules + rid;
2380 if (r->d == 0 && r->p < 0 && r->w2 == 0)
2383 if (pool->solvables[p].repo != installed)
2385 MAPCLR(&userinstalled, p - installed->start);
2386 i = solv->ruletojob.elements[rid - solv->jobrules];
2387 how = job->elements[i];
2388 if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
2393 i = solv->ruletojob.elements[rid - solv->jobrules];
2394 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2396 /* check if the literals all obsolete some installed package */
2400 /* just one installed literal */
2401 if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
2404 if (solv->noobsoletes.size)
2406 FOR_RULELITERALS(p, jp, r)
2407 if (MAPTST(&solv->noobsoletes, p))
2415 FOR_RULELITERALS(p, jp, r)
2419 queue_truncate(&iq, iqstart); /* abort */
2422 if (pool->solvables[p].repo == installed)
2424 if (iq.count == iqstart)
2428 for (i = iqstart; i < iq.count; i++)
2429 if (iq.elements[i] == p)
2431 queue_truncate(&iq, iqstart);
2437 intersect_obsoletes(solv, p, &iq, iqstart, &om);
2438 if (iq.count == iqstart)
2446 if (solv->cleandeps_updatepkgs)
2447 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
2448 queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
2450 for (p = installed->start; p < installed->end; p++)
2452 if (pool->solvables[p].repo != installed)
2454 MAPSET(&installedm, p);
2458 #ifdef CLEANDEPSDEBUG
2459 printf("REMOVE PASS\n");
2465 /* supplements pass */
2466 for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2468 if (!MAPTST(&installedm, ip))
2470 s = pool->solvables + ip;
2471 if (!s->supplements)
2473 if (!MAPTST(&im, ip))
2475 supp = s->repo->idarraydata + s->supplements;
2476 while ((sup = *supp++) != 0)
2477 if (dep_possible(solv, sup, &im))
2481 supp = s->repo->idarraydata + s->supplements;
2482 while ((sup = *supp++) != 0)
2483 if (dep_possible(solv, sup, &installedm))
2485 /* no longer supplemented, also erase */
2486 int iqcount = iq.count;
2487 /* pin packages, see comment above dep_pkgcheck */
2488 dep_pkgcheck(solv, sup, &im, &iq);
2489 for (i = iqcount; i < iq.count; i++)
2491 Id pqp = iq.elements[i];
2492 if (pool->solvables[pqp].repo == installed)
2493 MAPSET(&userinstalled, pqp - installed->start);
2495 queue_truncate(&iq, iqcount);
2496 #ifdef CLEANDEPSDEBUG
2497 printf("%s supplemented\n", pool_solvid2str(pool, ip));
2499 queue_push(&iq, ip);
2506 ip = queue_shift(&iq);
2507 s = pool->solvables + ip;
2508 if (!MAPTST(&im, ip))
2510 if (!MAPTST(&installedm, ip))
2512 if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
2515 #ifdef CLEANDEPSDEBUG
2516 printf("removing %s\n", pool_solvable2str(pool, s));
2520 reqp = s->repo->idarraydata + s->requires;
2521 while ((req = *reqp++) != 0)
2523 if (req == SOLVABLE_PREREQMARKER)
2526 /* count number of installed packages that match */
2528 FOR_PROVIDES(p, pp, req)
2529 if (MAPTST(&installedm, p))
2534 FOR_PROVIDES(p, pp, req)
2538 #ifdef CLEANDEPSDEBUG
2539 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2548 reqp = s->repo->idarraydata + s->recommends;
2549 while ((req = *reqp++) != 0)
2553 FOR_PROVIDES(p, pp, req)
2554 if (MAPTST(&installedm, p))
2559 FOR_PROVIDES(p, pp, req)
2563 #ifdef CLEANDEPSDEBUG
2564 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2573 /* turn userinstalled into remove set for pruning */
2574 map_empty(&userinstalled);
2575 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2577 r = solv->rules + rid;
2578 if (r->p >= 0 || r->d != 0 || r->w2 != 0)
2579 continue; /* disabled or not erase */
2582 if (pool->solvables[p].repo == installed)
2583 MAPSET(&userinstalled, p - installed->start);
2585 for (p = installed->start; p < installed->end; p++)
2588 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2590 r = solv->rules + rid;
2593 FOR_RULELITERALS(p, jp, r)
2597 /* also put directly addressed packages on the install queue
2598 * so we can mark patterns as installed */
2599 for (i = 0; i < job->count; i += 2)
2601 how = job->elements[i];
2602 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2604 what = job->elements[i + 1];
2605 select = how & SOLVER_SELECTMASK;
2606 if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
2607 queue_push(&iq, what);
2611 #ifdef CLEANDEPSDEBUG
2612 printf("ADDBACK PASS\n");
2618 /* supplements pass */
2619 for (ip = installed->start; ip < installed->end; ip++)
2621 if (!MAPTST(&installedm, ip))
2623 if (MAPTST(&userinstalled, ip - installed->start))
2625 s = pool->solvables + ip;
2626 if (!s->supplements)
2628 if (MAPTST(&im, ip))
2630 supp = s->repo->idarraydata + s->supplements;
2631 while ((sup = *supp++) != 0)
2632 if (dep_possible(solv, sup, &im))
2636 #ifdef CLEANDEPSDEBUG
2637 printf("%s supplemented\n", pool_solvid2str(pool, ip));
2640 queue_push(&iq, ip);
2646 ip = queue_shift(&iq);
2647 s = pool->solvables + ip;
2648 #ifdef CLEANDEPSDEBUG
2649 printf("adding back %s\n", pool_solvable2str(pool, s));
2653 reqp = s->repo->idarraydata + s->requires;
2654 while ((req = *reqp++) != 0)
2656 FOR_PROVIDES(p, pp, req)
2658 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
2662 if (MAPTST(&userinstalled, p - installed->start))
2664 #ifdef CLEANDEPSDEBUG
2665 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2675 reqp = s->repo->idarraydata + s->recommends;
2676 while ((req = *reqp++) != 0)
2678 FOR_PROVIDES(p, pp, req)
2680 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
2684 if (MAPTST(&userinstalled, p - installed->start))
2686 #ifdef CLEANDEPSDEBUG
2687 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2698 if (solv->cleandeps_updatepkgs)
2699 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
2700 MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
2701 if (solv->cleandeps_mistakes)
2702 for (i = 0; i < solv->cleandeps_mistakes->count; i++)
2703 MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
2704 for (p = installed->start; p < installed->end; p++)
2706 if (pool->solvables[p].repo != installed)
2708 if (!MAPTST(&im, p))
2709 MAPSET(&solv->cleandepsmap, p - installed->start);
2712 map_free(&installedm);
2713 map_free(&userinstalled);