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
25 #include "solverdebug.h"
27 #define RULES_BLOCK 63
29 static void addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep);
31 /*-------------------------------------------------------------------
32 * Check if dependency is possible
34 * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap
38 dep_possible(Solver *solv, Id dep, Map *m)
40 Pool *pool = solv->pool;
45 Reldep *rd = GETRELDEP(pool, dep);
46 if (rd->flags == REL_AND)
48 if (!dep_possible(solv, rd->name, m))
50 return dep_possible(solv, rd->evr, m);
52 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
53 return solver_splitprovides(solv, rd->evr);
54 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
55 return solver_dep_installed(solv, rd->evr);
57 FOR_PROVIDES(p, pp, dep)
65 /********************************************************************
69 * - unify rules, remove duplicates
72 /*-------------------------------------------------------------------
74 * compare rules for unification sort
79 unifyrules_sortcmp(const void *ap, const void *bp, void *dp)
89 return x; /* p differs */
92 if (a->d == 0 && b->d == 0)
93 return a->w2 - b->w2; /* assertion: return w2 diff */
95 if (a->d == 0) /* a is assertion, b not */
97 x = a->w2 - pool->whatprovidesdata[b->d];
101 if (b->d == 0) /* b is assertion, a not */
103 x = pool->whatprovidesdata[a->d] - b->w2;
107 /* compare whatprovidesdata */
108 ad = pool->whatprovidesdata + a->d;
109 bd = pool->whatprovidesdata + b->d;
111 if ((x = *ad++ - *bd++) != 0)
117 solver_samerule(Solver *solv, Rule *r1, Rule *r2)
119 return unifyrules_sortcmp(r1, r2, solv->pool);
123 /*-------------------------------------------------------------------
126 * go over all rules and remove duplicates
130 solver_unifyrules(Solver *solv)
132 Pool *pool = solv->pool;
136 if (solv->nrules <= 1) /* nothing to unify */
139 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
141 /* sort rules first */
142 sat_sort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp, solv->pool);
149 for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
151 if (jr && !unifyrules_sortcmp(ir, jr, pool))
152 continue; /* prune! */
153 jr = solv->rules + j++; /* keep! */
158 /* reduced count from nrules to j rules */
159 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
161 /* adapt rule buffer */
163 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
166 * debug: log rule statistics
168 IF_POOLDEBUG (SAT_DEBUG_STATS)
175 for (i = 1; i < solv->nrules; i++)
182 dp = solv->pool->whatprovidesdata + r->d;
187 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
188 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
190 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
200 hashrule(Solver *solv, Id p, Id d, int n)
202 unsigned int x = (unsigned int)p;
206 return (x * 37) ^ (unsigned int)d;
207 dp = solv->pool->whatprovidesdata + d;
209 x = (x * 37) ^ (unsigned int)*dp++;
215 /*-------------------------------------------------------------------
221 * p = direct literal; always < 0 for installed rpm rules
222 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
225 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
227 * p < 0 : pkg id of A
228 * d > 0 : Offset in whatprovidesdata (list of providers of b)
230 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
231 * p < 0 : pkg id of A
232 * d < 0 : Id of solvable (e.g. B1)
234 * d == 0: unary rule, assertion => (A) or (-A)
236 * Install: p > 0, d = 0 (A) user requested install
237 * Remove: p < 0, d = 0 (-A) user requested remove (also: uninstallable)
238 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
239 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
240 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
241 * also used for obsoletes
242 * ?: p > 0, d < 0 (A|-B)
243 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
247 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
248 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
249 * every other : w1 = p, w2 = whatprovidesdata[d];
250 * Disabled rule: w1 = 0
252 * always returns a rule for non-rpm rules
256 solver_addrule(Solver *solv, Id p, Id d)
258 Pool *pool = solv->pool;
262 int n = 0; /* number of literals in rule - 1
263 0 = direct assertion (single literal)
268 /* it often happenes that requires lead to adding the same rpm rule
269 * multiple times, so we prune those duplicates right away to make
270 * the work for unifyrules a bit easier */
272 if (solv->nrules /* we already have rules */
273 && !solv->rpmrules_end) /* but are not done with rpm rules */
275 r = solv->rules + solv->nrules - 1; /* get the last added rule */
276 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
281 * compute number of literals (n) in rule
286 /* always a binary rule */
288 return 0; /* ignore self conflict */
293 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
295 return 0; /* rule is self-fulfilling */
297 if (n == 1) /* have single provider */
298 d = dp[-1]; /* take single literal */
301 if (n == 1 && p > d && !solv->rpmrules_end)
303 /* smallest literal first so we can find dups */
304 n = p; p = d; d = n; /* p <-> d */
305 n = 1; /* re-set n, was used as temp var */
309 * check for duplicate
312 /* check if the last added rule (r) is exactly the same as what we're looking for. */
313 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
314 return r; /* binary rule */
316 /* have n-ary rule with same first literal, check other literals */
317 if (r && n > 1 && r->d && r->p == p)
319 /* Rule where d is an offset in whatprovidesdata */
323 dp2 = pool->whatprovidesdata + r->d;
324 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
335 /* extend rule buffer */
336 solv->rules = sat_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
337 r = solv->rules + solv->nrules++; /* point to rule space */
346 /* direct assertion, no watch needed */
362 r->w2 = pool->whatprovidesdata[d];
367 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
369 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
370 solver_printrule(solv, SAT_DEBUG_RULE_CREATION, r);
377 /******************************************************************************
379 *** rpm rule part: create rules representing the package dependencies
384 * special multiversion patch conflict handling:
385 * a patch conflict is also satisfied, if some other
386 * version with the same name/arch that doesn't conflict
387 * get's installed. The generated rule is thus:
388 * -patch|-cpack|opack1|opack2|...
391 makemultiversionconflict(Solver *solv, Id n, Id con)
393 Pool *pool = solv->pool;
398 sn = pool->solvables + n;
399 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
401 FOR_PROVIDES(p, pp, sn->name)
403 s = pool->solvables + p;
404 if (s->name != sn->name || s->arch != sn->arch)
406 if (!MAPTST(&solv->noobsoletes, p))
408 if (pool_match_nevr(pool, pool->solvables + p, con))
410 /* here we have a multiversion solvable that doesn't conflict */
411 /* thus we're not in conflict if it is installed */
415 return -n; /* no other package found, generate normal conflict */
416 return pool_queuetowhatprovides(pool, &q);
420 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
422 if (!solv->ruleinfoq)
423 solver_addrule(solv, p, d);
425 addrpmruleinfo(solv, p, d, type, dep);
428 /*-------------------------------------------------------------------
430 * add (install) rules for solvable
432 * s: Solvable for which to add rules
433 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
435 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
436 * solvables, the edges their dependencies.
437 * Starting from an installed solvable, this will create all rules
438 * representing the graph created by the solvables dependencies.
440 * for unfulfilled requirements, conflicts, obsoletes,....
441 * add a negative assertion for solvables that are not installable
443 * It will also create rules for all solvables referenced by 's'
444 * i.e. descend to all providers of requirements of 's'
449 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
451 Pool *pool = solv->pool;
452 Repo *installed = solv->installed;
454 /* 'work' queue. keeps Ids of solvables we still have to work on.
455 And buffer for it. */
460 /* if to add rules for broken deps ('rpm -V' functionality)
464 /* Id var and pointer for each dependency
465 * (not used in parallel)
472 Id p, pp; /* whatprovides loops */
473 Id *dp; /* ptr to 'whatprovides' */
474 Id n; /* Id for current solvable 's' */
476 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
478 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
479 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
481 /* loop until there's no more work left */
486 * s: Pointer to solvable
489 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
492 if (MAPTST(m, n)) /* continue if already visited */
494 MAPSET(m, n); /* mark as visited */
497 s = pool->solvables + n; /* s = Solvable in question */
500 if (installed /* Installed system available */
501 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
502 && s->repo == installed) /* solvable is installed? */
504 dontfix = 1; /* dont care about broken rpm deps */
508 && s->arch != ARCH_SRC
509 && s->arch != ARCH_NOSRC
510 && !pool_installable(pool, s))
512 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
513 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
516 /* yet another SUSE hack, sigh */
517 if (pool->nscallback && !strncmp("product:", id2str(pool, s->name), 8))
519 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
520 if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
522 addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
523 addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy));
524 if (m && !MAPTST(m, buddy))
525 queue_push(&workq, buddy);
529 /*-----------------------------------------
530 * check requires of s
535 reqp = s->repo->idarraydata + s->requires;
536 while ((req = *reqp++) != 0) /* go through all requires */
538 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
541 /* find list of solvables providing 'req' */
542 dp = pool_whatprovides_ptr(pool, req);
544 if (*dp == SYSTEMSOLVABLE) /* always installed */
549 /* the strategy here is to not insist on dependencies
550 * that are already broken. so if we find one provider
551 * that was already installed, we know that the
552 * dependency was not broken before so we enforce it */
554 /* check if any of the providers for 'req' is installed */
555 for (i = 0; (p = dp[i]) != 0; i++)
557 if (pool->solvables[p].repo == installed)
558 break; /* provider was installed */
560 /* didn't find an installed provider: previously broken dependency */
563 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
570 /* nothing provides req! */
571 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
572 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
576 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
578 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
579 for (i = 0; dp[i]; i++)
580 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvid2str(pool, dp[i]));
583 /* add 'requires' dependency */
584 /* rule: (-requestor|provider1|provider2|...|providerN) */
585 addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
587 /* descend the dependency tree
588 push all non-visited providers on the work queue */
594 queue_push(&workq, *dp);
598 } /* while, requirements of n */
600 } /* if, requirements */
602 /* that's all we check for src packages */
603 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
606 /*-----------------------------------------
607 * check conflicts of s
614 /* we treat conflicts in patches a bit differen:
616 * - multiversion handling
617 * XXX: we should really handle this different, looking
618 * at the name is a bad hack
620 if (!strncmp("patch:", id2str(pool, s->name), 6))
622 conp = s->repo->idarraydata + s->conflicts;
623 /* foreach conflicts of 's' */
624 while ((con = *conp++) != 0)
626 /* foreach providers of a conflict of 's' */
627 FOR_PROVIDES(p, pp, con)
629 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
631 /* dontfix: dont care about conflicts with already installed packs */
632 if (dontfix && pool->solvables[p].repo == installed)
634 /* p == n: self conflict */
635 if (p == n && !solv->allowselfconflicts)
639 Reldep *rd = GETRELDEP(pool, con);
640 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
643 p = 0; /* make it a negative assertion, aka 'uninstallable' */
645 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
647 /* our patch conflicts with a noobsoletes (aka multiversion) package */
648 p = -makemultiversionconflict(solv, p, con);
650 /* rule: -n|-p: either solvable _or_ provider of conflict */
651 addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
656 /*-----------------------------------------
657 * check obsoletes if not installed
658 * (only installation will trigger the obsoletes in rpm)
660 if (!installed || pool->solvables[n].repo != installed)
661 { /* not installed */
662 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
663 if (s->obsoletes && !noobs)
665 obsp = s->repo->idarraydata + s->obsoletes;
666 /* foreach obsoletes */
667 while ((obs = *obsp++) != 0)
669 /* foreach provider of an obsoletes of 's' */
670 FOR_PROVIDES(p, pp, obs)
672 if (!solv->obsoleteusesprovides /* obsoletes are matched names, not provides */
673 && !pool_match_nevr(pool, pool->solvables + p, obs))
675 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
679 FOR_PROVIDES(p, pp, s->name)
681 Solvable *ps = pool->solvables + p;
682 /* we still obsolete packages with same nevra, like rpm does */
683 /* (actually, rpm mixes those packages. yuck...) */
684 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
686 if (!solv->implicitobsoleteusesprovides && s->name != ps->name)
688 if (s->name == ps->name)
689 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
691 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
695 /*-----------------------------------------
696 * add recommends to the work queue
698 if (s->recommends && m)
700 recp = s->repo->idarraydata + s->recommends;
701 while ((rec = *recp++) != 0)
703 FOR_PROVIDES(p, pp, rec)
705 queue_push(&workq, p);
708 if (s->suggests && m)
710 sugp = s->repo->idarraydata + s->suggests;
711 while ((sug = *sugp++) != 0)
713 FOR_PROVIDES(p, pp, sug)
715 queue_push(&workq, p);
720 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
724 /*-------------------------------------------------------------------
726 * Add package rules for weak rules
728 * m: visited solvables
732 solver_addrpmrulesforweak(Solver *solv, Map *m)
734 Pool *pool = solv->pool;
739 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
740 /* foreach solvable in pool */
741 for (i = n = 1; n < pool->nsolvables; i++, n++)
743 if (i == pool->nsolvables) /* wrap i */
745 if (MAPTST(m, i)) /* been there */
748 s = pool->solvables + i;
749 if (!pool_installable(pool, s)) /* only look at installable ones */
755 /* find possible supplements */
756 supp = s->repo->idarraydata + s->supplements;
757 while ((sup = *supp++) != ID_NULL)
758 if (dep_possible(solv, sup, m))
762 /* if nothing found, check for enhances */
763 if (!sup && s->enhances)
765 supp = s->repo->idarraydata + s->enhances;
766 while ((sup = *supp++) != ID_NULL)
767 if (dep_possible(solv, sup, m))
770 /* if nothing found, goto next solvables */
773 solver_addrpmrulesforsolvable(solv, s, m);
774 n = 0; /* check all solvables again */
776 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
780 /*-------------------------------------------------------------------
782 * add package rules for possible updates
785 * m: map of already visited solvables
786 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
790 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
792 Pool *pool = solv->pool;
794 /* queue and buffer for it */
798 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
800 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
801 /* find update candidates for 's' */
802 policy_findupdatepackages(solv, s, &qs, allow_all);
803 /* add rule for 's' if not already done */
804 if (!MAPTST(m, s - pool->solvables))
805 solver_addrpmrulesforsolvable(solv, s, m);
806 /* foreach update candidate, add rule if not already done */
807 for (i = 0; i < qs.count; i++)
808 if (!MAPTST(m, qs.elements[i]))
809 solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
812 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
816 /***********************************************************************
818 *** Update/Feature rule part
820 *** Those rules make sure an installed package isn't silently deleted
825 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
827 Pool *pool = solv->pool;
830 policy_findupdatepackages(solv, s, qs, allow_all);
834 return 0; /* orphaned, don't create feature rule */
835 /* check if this is an orphaned package */
836 policy_findupdatepackages(solv, s, qs, 1);
838 return 0; /* orphaned, don't create update rule */
840 return -SYSTEMSOLVABLE; /* supported but not installable */
843 return s - pool->solvables;
844 /* check if it is ok to keep the installed package */
845 for (i = 0; i < qs->count; i++)
847 Solvable *ns = pool->solvables + qs->elements[i];
848 if (s->evr == ns->evr && solvable_identical(s, ns))
849 return s - pool->solvables;
851 /* nope, it must be some other package */
852 return -SYSTEMSOLVABLE;
855 /* add packages from the dup repositories to the update candidates
856 * this isn't needed for the global dup mode as all packages are
857 * from dup repos in that case */
859 addduppackages(Solver *solv, Solvable *s, Queue *qs)
864 int oldnoupdateprovide = solv->noupdateprovide;
866 queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
867 solv->noupdateprovide = 1;
868 policy_findupdatepackages(solv, s, &dupqs, 2);
869 solv->noupdateprovide = oldnoupdateprovide;
870 for (i = 0; i < dupqs.count; i++)
872 p = dupqs.elements[i];
873 if (MAPTST(&solv->dupmap, p))
874 queue_pushunique(qs, p);
879 /*-------------------------------------------------------------------
881 * add rule for update
882 * (A|A1|A2|A3...) An = update candidates for A
884 * s = (installed) solvable
888 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
890 /* installed packages get a special upgrade allowed rule */
891 Pool *pool = solv->pool;
896 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
897 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
898 p = s - pool->solvables;
899 /* find update candidates for 's' */
900 if (solv->distupgrade)
901 p = finddistupgradepackages(solv, s, &qs, allow_all);
903 policy_findupdatepackages(solv, s, &qs, allow_all);
904 if (!allow_all && !solv->distupgrade && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
905 addduppackages(solv, s, &qs);
907 if (!allow_all && qs.count && solv->noobsoletes.size)
911 d = pool_queuetowhatprovides(pool, &qs);
912 /* filter out all noobsoletes packages as they don't update */
913 for (i = j = 0; i < qs.count; i++)
915 if (MAPTST(&solv->noobsoletes, qs.elements[i]))
917 /* it's ok if they have same nevra */
918 Solvable *ps = pool->solvables + qs.elements[i];
919 if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
922 qs.elements[j++] = qs.elements[i];
924 if (j == 0 && p == -SYSTEMSOLVABLE && solv->distupgrade)
926 queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
931 if (d && solv->updatesystem && solv->installed && s->repo == solv->installed)
933 if (!solv->multiversionupdaters)
934 solv->multiversionupdaters = sat_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
935 solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
940 if (qs.count && p == -SYSTEMSOLVABLE)
941 p = queue_shift(&qs);
942 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
944 solver_addrule(solv, p, d); /* allow update of s */
945 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
949 disableupdaterule(Solver *solv, Id p)
953 MAPSET(&solv->noupdate, p - solv->installed->start);
954 r = solv->rules + solv->updaterules + (p - solv->installed->start);
955 if (r->p && r->d >= 0)
956 solver_disablerule(solv, r);
957 r = solv->rules + solv->featurerules + (p - solv->installed->start);
958 if (r->p && r->d >= 0)
959 solver_disablerule(solv, r);
963 reenableupdaterule(Solver *solv, Id p)
965 Pool *pool = solv->pool;
968 MAPCLR(&solv->noupdate, p - solv->installed->start);
969 r = solv->rules + solv->updaterules + (p - solv->installed->start);
974 solver_enablerule(solv, r);
975 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
977 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
978 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
982 r = solv->rules + solv->featurerules + (p - solv->installed->start);
983 if (r->p && r->d < 0)
985 solver_enablerule(solv, r);
986 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
988 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
989 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
995 /***********************************************************************
997 *** Infarch rule part
999 *** Infarch rules make sure the solver uses the best architecture of
1000 *** a package if multiple archetectures are available
1005 solver_addinfarchrules(Solver *solv, Map *addedmap)
1007 Pool *pool = solv->pool;
1009 Id p, pp, a, aa, bestarch;
1010 Solvable *s, *ps, *bests;
1011 Queue badq, allowedarchs;
1014 queue_init(&allowedarchs);
1015 solv->infarchrules = solv->nrules;
1016 for (i = 1; i < pool->nsolvables; i++)
1018 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1020 s = pool->solvables + i;
1024 queue_empty(&allowedarchs);
1025 FOR_PROVIDES(p, pp, s->name)
1027 ps = pool->solvables + p;
1028 if (ps->name != s->name || !MAPTST(addedmap, p))
1035 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1036 if (a != 1 && pool->installed && ps->repo == pool->installed)
1038 if (!solv->distupgrade)
1039 queue_pushunique(&allowedarchs, ps->arch); /* also ok to keep this architecture */
1040 continue; /* ignore installed solvables when calculating the best arch */
1042 if (a && a != 1 && (!bestarch || a < bestarch))
1050 /* speed up common case where installed package already has best arch */
1051 if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1052 allowedarchs.count--; /* installed arch is best */
1054 FOR_PROVIDES(p, pp, s->name)
1056 ps = pool->solvables + p;
1057 if (ps->name != s->name || !MAPTST(addedmap, p))
1060 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1061 if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1063 for (j = 0; j < allowedarchs.count; j++)
1065 aa = allowedarchs.elements[j];
1068 aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1069 if (aa && ((a ^ aa) & 0xffff0000) == 0)
1070 break; /* compatible */
1072 if (j == allowedarchs.count)
1073 queue_push(&badq, p);
1078 /* block all solvables in the badq! */
1079 for (j = 0; j < badq.count; j++)
1081 p = badq.elements[j];
1082 solver_addrule(solv, -p, 0);
1086 queue_free(&allowedarchs);
1087 solv->infarchrules_end = solv->nrules;
1091 disableinfarchrule(Solver *solv, Id name)
1093 Pool *pool = solv->pool;
1096 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1098 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1099 solver_disablerule(solv, r);
1104 reenableinfarchrule(Solver *solv, Id name)
1106 Pool *pool = solv->pool;
1109 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1111 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1113 solver_enablerule(solv, r);
1114 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
1116 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1117 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
1124 /***********************************************************************
1128 *** Dup rules make sure a package is selected from the specified dup
1129 *** repositories if an update candidate is included in one of them.
1134 solver_createdupmaps(Solver *solv)
1136 Queue *job = &solv->job;
1137 Pool *pool = solv->pool;
1139 Id how, what, p, pi, pp, obs, *obsp;
1143 map_init(&solv->dupmap, pool->nsolvables);
1144 map_init(&solv->dupinvolvedmap, pool->nsolvables);
1145 for (i = 0; i < job->count; i += 2)
1147 how = job->elements[i];
1148 what = job->elements[i + 1];
1149 switch (how & SOLVER_JOBMASK)
1151 case SOLVER_DISTUPGRADE:
1152 if (what < 0 || what > pool->nrepos)
1154 repo = pool->repos[what];
1155 FOR_REPO_SOLVABLES(repo, p, s)
1157 MAPSET(&solv->dupmap, p);
1158 FOR_PROVIDES(pi, pp, s->name)
1160 ps = pool->solvables + pi;
1161 if (ps->name != s->name)
1163 MAPSET(&solv->dupinvolvedmap, pi);
1167 /* FIXME: check obsoletes/provides combination */
1168 obsp = s->repo->idarraydata + s->obsoletes;
1169 while ((obs = *obsp++) != 0)
1171 FOR_PROVIDES(pi, pp, obs)
1173 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + pi, obs))
1175 MAPSET(&solv->dupinvolvedmap, pi);
1185 MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1189 solver_freedupmaps(Solver *solv)
1191 map_free(&solv->dupmap);
1192 map_free(&solv->dupinvolvedmap);
1196 solver_addduprules(Solver *solv, Map *addedmap)
1198 Pool *pool = solv->pool;
1203 solv->duprules = solv->nrules;
1204 for (i = 1; i < pool->nsolvables; i++)
1206 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1208 s = pool->solvables + i;
1210 FOR_PROVIDES(p, pp, s->name)
1212 ps = pool->solvables + p;
1213 if (ps->name != s->name || !MAPTST(addedmap, p))
1219 if (!MAPTST(&solv->dupinvolvedmap, p))
1221 if (solv->installed && ps->repo == solv->installed)
1223 if (!solv->updatemap.size)
1224 map_init(&solv->updatemap, pool->nsolvables);
1225 MAPSET(&solv->updatemap, p);
1226 if (!MAPTST(&solv->dupmap, p))
1229 /* is installed identical to a good one? */
1230 FOR_PROVIDES(ip, ipp, s->name)
1232 Solvable *is = pool->solvables + ip;
1233 if (!MAPTST(&solv->dupmap, ip))
1235 if (is->evr == s->evr && solvable_identical(s, is))
1239 solver_addrule(solv, -p, 0); /* no match, sorry */
1242 else if (!MAPTST(&solv->dupmap, p))
1243 solver_addrule(solv, -p, 0);
1246 solv->duprules_end = solv->nrules;
1251 disableduprule(Solver *solv, Id name)
1253 Pool *pool = solv->pool;
1256 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1258 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1259 solver_disablerule(solv, r);
1264 reenableduprule(Solver *solv, Id name)
1266 Pool *pool = solv->pool;
1269 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1271 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1273 solver_enablerule(solv, r);
1274 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
1276 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1277 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
1284 /***********************************************************************
1286 *** Policy rule disabling/reenabling
1288 *** Disable all policy rules that conflict with our jobs. If a job
1289 *** gets disabled later on, reenable the involved policy rules again.
1293 #define DISABLE_UPDATE 1
1294 #define DISABLE_INFARCH 2
1295 #define DISABLE_DUP 3
1298 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1300 Pool *pool = solv->pool;
1306 installed = solv->installed;
1307 select = how & SOLVER_SELECTMASK;
1308 switch (how & SOLVER_JOBMASK)
1310 case SOLVER_INSTALL:
1311 if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && solv->infarchrules != solv->infarchrules_end && ISRELDEP(what))
1313 Reldep *rd = GETRELDEP(pool, what);
1314 if (rd->flags == REL_ARCH)
1316 int qcnt = q->count;
1317 FOR_JOB_SELECT(p, pp, select, what)
1319 s = pool->solvables + p;
1321 for (i = qcnt; i < q->count; i += 2)
1322 if (q->elements[i + 1] == s->name)
1326 queue_push(q, DISABLE_INFARCH);
1327 queue_push(q, s->name);
1331 if (select != SOLVER_SOLVABLE)
1333 s = pool->solvables + what;
1334 if (solv->infarchrules != solv->infarchrules_end)
1336 queue_push(q, DISABLE_INFARCH);
1337 queue_push(q, s->name);
1339 if (solv->duprules != solv->duprules_end)
1341 queue_push(q, DISABLE_DUP);
1342 queue_push(q, s->name);
1346 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, what))
1348 if (s->repo == installed)
1350 queue_push(q, DISABLE_UPDATE);
1351 queue_push(q, what);
1357 obsp = s->repo->idarraydata + s->obsoletes;
1358 while ((obs = *obsp++) != 0)
1359 FOR_PROVIDES(p, pp, obs)
1361 Solvable *ps = pool->solvables + p;
1362 if (ps->repo != installed)
1364 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1366 queue_push(q, DISABLE_UPDATE);
1370 FOR_PROVIDES(p, pp, s->name)
1372 Solvable *ps = pool->solvables + p;
1373 if (ps->repo != installed)
1375 if (!solv->implicitobsoleteusesprovides && ps->name != s->name)
1377 queue_push(q, DISABLE_UPDATE);
1384 FOR_JOB_SELECT(p, pp, select, what)
1385 if (pool->solvables[p].repo == installed)
1387 queue_push(q, DISABLE_UPDATE);
1396 /* disable all policy rules that are in conflict with our job list */
1398 solver_disablepolicyrules(Solver *solv)
1400 Queue *job = &solv->job;
1407 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1409 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1411 r = solv->rules + i;
1412 if (r->d < 0) /* disabled? */
1414 j = solv->ruletojob.elements[i - solv->jobrules];
1418 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1420 MAPZERO(&solv->noupdate);
1421 for (i = 0; i < allq.count; i += 2)
1423 Id type = allq.elements[i], arg = allq.elements[i + 1];
1426 case DISABLE_UPDATE:
1427 disableupdaterule(solv, arg);
1429 case DISABLE_INFARCH:
1430 disableinfarchrule(solv, arg);
1433 disableduprule(solv, arg);
1442 /* we just disabled job #jobidx, now reenable all policy rules that were
1443 * disabled because of this job */
1445 solver_reenablepolicyrules(Solver *solv, int jobidx)
1447 Queue *job = &solv->job;
1452 Id qbuf[32], allqbuf[128];
1454 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1455 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1456 jobtodisablelist(solv, job->elements[jobidx], job->elements[jobidx + 1], &q);
1459 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1461 r = solv->rules + i;
1462 if (r->d < 0) /* disabled? */
1464 j = solv->ruletojob.elements[i - solv->jobrules];
1468 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1470 for (j = 0; j < q.count; j += 2)
1472 Id type = q.elements[j], arg = q.elements[j + 1];
1473 for (i = 0; i < allq.count; i += 2)
1474 if (allq.elements[i] == type && allq.elements[i + 1] == arg)
1477 continue; /* still disabled */
1480 case DISABLE_UPDATE:
1481 reenableupdaterule(solv, arg);
1483 case DISABLE_INFARCH:
1484 reenableinfarchrule(solv, arg);
1487 reenableduprule(solv, arg);
1496 /***********************************************************************
1498 *** Rule info part, tell the user what the rule is about.
1503 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1505 Pool *pool = solv->pool;
1509 /* check if this creates the rule we're searching for */
1510 r = solv->rules + solv->ruleinfoq->elements[0];
1512 od = r->d < 0 ? -r->d - 1 : r->d;
1517 if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1519 w2 = pool->whatprovidesdata[d];
1523 if (p > 0 && d < 0) /* this hack is used for buddy deps */
1535 Id *dp = pool->whatprovidesdata + d;
1536 Id *odp = pool->whatprovidesdata + od;
1538 if (*dp++ != *odp++)
1544 /* handle multiversion conflict rules */
1545 if (p < 0 && pool->whatprovidesdata[d] < 0)
1547 w2 = pool->whatprovidesdata[d];
1548 /* XXX: free memory */
1558 if (w2 != op || p != ow2)
1563 if (p != op || w2 != ow2)
1567 /* yep, rule matches. record info */
1568 queue_push(solv->ruleinfoq, type);
1569 if (type == SOLVER_RULE_RPM_SAME_NAME)
1571 /* we normalize same name order */
1572 queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
1573 queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
1577 queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
1578 queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
1580 queue_push(solv->ruleinfoq, dep);
1584 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
1586 const Id *a = ap, *b = bp;
1605 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
1607 Pool *pool = solv->pool;
1608 Rule *r = solv->rules + rid;
1612 if (rid <= 0 || rid >= solv->rpmrules_end)
1614 Id type, from, to, dep;
1615 type = solver_ruleinfo(solv, rid, &from, &to, &dep);
1616 queue_push(rq, type);
1617 queue_push(rq, from);
1619 queue_push(rq, dep);
1624 queue_push(rq, rid);
1625 solv->ruleinfoq = rq;
1626 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1627 /* also try reverse direction for conflicts */
1628 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1629 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1630 solv->ruleinfoq = 0;
1632 /* now sort & unify em */
1635 sat_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
1636 /* throw out identical entries */
1637 for (i = j = 0; i < rq->count; i += 4)
1641 if (rq->elements[i] == rq->elements[j - 4] &&
1642 rq->elements[i + 1] == rq->elements[j - 3] &&
1643 rq->elements[i + 2] == rq->elements[j - 2] &&
1644 rq->elements[i + 3] == rq->elements[j - 1])
1647 rq->elements[j++] = rq->elements[i];
1648 rq->elements[j++] = rq->elements[i + 1];
1649 rq->elements[j++] = rq->elements[i + 2];
1650 rq->elements[j++] = rq->elements[i + 3];
1657 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
1659 Pool *pool = solv->pool;
1660 Rule *r = solv->rules + rid;
1661 SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
1669 if (rid > 0 && rid < solv->rpmrules_end)
1675 return SOLVER_RULE_RPM;
1679 queue_push(&rq, rid);
1680 solv->ruleinfoq = &rq;
1681 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1682 /* also try reverse direction for conflicts */
1683 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1684 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1685 solv->ruleinfoq = 0;
1686 type = SOLVER_RULE_RPM;
1687 for (i = 1; i < rq.count; i += 4)
1690 qt = rq.elements[i];
1691 qp = rq.elements[i + 1];
1692 qo = rq.elements[i + 2];
1693 qd = rq.elements[i + 3];
1694 if (type == SOLVER_RULE_RPM || type > qt)
1708 if (rid >= solv->jobrules && rid < solv->jobrules_end)
1710 Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
1714 *top = solv->job.elements[jidx];
1716 *depp = solv->job.elements[jidx + 1];
1717 if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (solv->job.elements[jidx] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
1718 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
1719 return SOLVER_RULE_JOB;
1721 if (rid >= solv->updaterules && rid < solv->updaterules_end)
1724 *fromp = solv->installed->start + (rid - solv->updaterules);
1725 return SOLVER_RULE_UPDATE;
1727 if (rid >= solv->featurerules && rid < solv->featurerules_end)
1730 *fromp = solv->installed->start + (rid - solv->featurerules);
1731 return SOLVER_RULE_FEATURE;
1733 if (rid >= solv->duprules && rid < solv->duprules_end)
1738 *depp = pool->solvables[-r->p].name;
1739 return SOLVER_RULE_DISTUPGRADE;
1741 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
1746 *depp = pool->solvables[-r->p].name;
1747 return SOLVER_RULE_INFARCH;
1749 if (rid >= solv->learntrules)
1751 return SOLVER_RULE_LEARNT;
1753 return SOLVER_RULE_UNKNOWN;