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, Map *cleandepsmap, int unneeded);
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_rulecmp(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 solver_shrinkrules(Solver *solv, int nrules)
383 solv->nrules = nrules;
384 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
387 /******************************************************************************
389 *** rpm rule part: create rules representing the package dependencies
394 * special multiversion patch conflict handling:
395 * a patch conflict is also satisfied if some other
396 * version with the same name/arch that doesn't conflict
397 * gets installed. The generated rule is thus:
398 * -patch|-cpack|opack1|opack2|...
401 makemultiversionconflict(Solver *solv, Id n, Id con)
403 Pool *pool = solv->pool;
408 sn = pool->solvables + n;
409 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
411 FOR_PROVIDES(p, pp, sn->name)
413 s = pool->solvables + p;
414 if (s->name != sn->name || s->arch != sn->arch)
416 if (!MAPTST(&solv->noobsoletes, p))
418 if (pool_match_nevr(pool, pool->solvables + p, con))
420 /* here we have a multiversion solvable that doesn't conflict */
421 /* thus we're not in conflict if it is installed */
425 return -n; /* no other package found, generate normal conflict */
426 return pool_queuetowhatprovides(pool, &q);
430 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
432 if (!solv->ruleinfoq)
433 solver_addrule(solv, p, d);
435 addrpmruleinfo(solv, p, d, type, dep);
438 /*-------------------------------------------------------------------
440 * add (install) rules for solvable
442 * s: Solvable for which to add rules
443 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
445 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
446 * solvables, the edges their dependencies.
447 * Starting from an installed solvable, this will create all rules
448 * representing the graph created by the solvables dependencies.
450 * for unfulfilled requirements, conflicts, obsoletes,....
451 * add a negative assertion for solvables that are not installable
453 * It will also create rules for all solvables referenced by 's'
454 * i.e. descend to all providers of requirements of 's'
459 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
461 Pool *pool = solv->pool;
462 Repo *installed = solv->installed;
464 /* 'work' queue. keeps Ids of solvables we still have to work on.
465 And buffer for it. */
470 /* if to add rules for broken deps ('rpm -V' functionality)
474 /* Id var and pointer for each dependency
475 * (not used in parallel)
482 Id p, pp; /* whatprovides loops */
483 Id *dp; /* ptr to 'whatprovides' */
484 Id n; /* Id for current solvable 's' */
486 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
487 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
489 /* loop until there's no more work left */
494 * s: Pointer to solvable
497 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
500 if (MAPTST(m, n)) /* continue if already visited */
502 MAPSET(m, n); /* mark as visited */
505 s = pool->solvables + n; /* s = Solvable in question */
508 if (installed /* Installed system available */
509 && s->repo == installed /* solvable is installed */
510 && !solv->fixmap_all /* NOT repair errors in rpm dependency graph */
511 && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
513 dontfix = 1; /* dont care about broken rpm deps */
518 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC
519 ? pool_disabled_solvable(pool, s)
520 : !pool_installable(pool, s))
522 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables));
523 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
527 /* yet another SUSE hack, sigh */
528 if (pool->nscallback && !strncmp("product:", pool_id2str(pool, s->name), 8))
530 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
531 if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
533 addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
534 addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy));
535 if (m && !MAPTST(m, buddy))
536 queue_push(&workq, buddy);
540 /*-----------------------------------------
541 * check requires of s
546 reqp = s->repo->idarraydata + s->requires;
547 while ((req = *reqp++) != 0) /* go through all requires */
549 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
552 /* find list of solvables providing 'req' */
553 dp = pool_whatprovides_ptr(pool, req);
555 if (*dp == SYSTEMSOLVABLE) /* always installed */
560 /* the strategy here is to not insist on dependencies
561 * that are already broken. so if we find one provider
562 * that was already installed, we know that the
563 * dependency was not broken before so we enforce it */
565 /* check if any of the providers for 'req' is installed */
566 for (i = 0; (p = dp[i]) != 0; i++)
568 if (pool->solvables[p].repo == installed)
569 break; /* provider was installed */
571 /* didn't find an installed provider: previously broken dependency */
574 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
581 /* nothing provides req! */
582 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));
583 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
587 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
589 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION," %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
590 for (i = 0; dp[i]; i++)
591 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, " provided by %s\n", pool_solvid2str(pool, dp[i]));
594 /* add 'requires' dependency */
595 /* rule: (-requestor|provider1|provider2|...|providerN) */
596 addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
598 /* descend the dependency tree
599 push all non-visited providers on the work queue */
605 queue_push(&workq, *dp);
609 } /* while, requirements of n */
611 } /* if, requirements */
613 /* that's all we check for src packages */
614 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
617 /*-----------------------------------------
618 * check conflicts of s
625 /* we treat conflicts in patches a bit differen:
627 * - multiversion handling
628 * XXX: we should really handle this different, looking
629 * at the name is a bad hack
631 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
633 conp = s->repo->idarraydata + s->conflicts;
634 /* foreach conflicts of 's' */
635 while ((con = *conp++) != 0)
637 /* foreach providers of a conflict of 's' */
638 FOR_PROVIDES(p, pp, con)
640 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
642 /* dontfix: dont care about conflicts with already installed packs */
643 if (dontfix && pool->solvables[p].repo == installed)
645 /* p == n: self conflict */
646 if (p == n && pool->forbidselfconflicts)
650 Reldep *rd = GETRELDEP(pool, con);
651 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
654 p = 0; /* make it a negative assertion, aka 'uninstallable' */
656 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
658 /* our patch conflicts with a noobsoletes (aka multiversion) package */
659 p = -makemultiversionconflict(solv, p, con);
661 /* rule: -n|-p: either solvable _or_ provider of conflict */
662 addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
667 /*-----------------------------------------
668 * check obsoletes and implicit obsoletes of a package
669 * if ignoreinstalledsobsoletes is not set, we're also checking
670 * obsoletes of installed packages (like newer rpm versions)
672 if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
674 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
675 int isinstalled = (installed && s->repo == installed);
676 if (s->obsoletes && (!noobs || solv->keepexplicitobsoletes))
678 obsp = s->repo->idarraydata + s->obsoletes;
679 /* foreach obsoletes */
680 while ((obs = *obsp++) != 0)
682 /* foreach provider of an obsoletes of 's' */
683 FOR_PROVIDES(p, pp, obs)
685 Solvable *ps = pool->solvables + p;
688 if (isinstalled && dontfix && ps->repo == installed)
689 continue; /* don't repair installed/installed problems */
690 if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
691 && !pool_match_nevr(pool, ps, obs))
693 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
696 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
698 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
702 /* check implicit obsoletes
703 * for installed packages we only need to check installed/installed problems (and
704 * only when dontfix is not set), as the others are picked up when looking at the
705 * uninstalled package.
707 if (!isinstalled || !dontfix)
709 FOR_PROVIDES(p, pp, s->name)
711 Solvable *ps = pool->solvables + p;
714 if (isinstalled && ps->repo != installed)
716 /* we still obsolete packages with same nevra, like rpm does */
717 /* (actually, rpm mixes those packages. yuck...) */
718 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
720 if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
722 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
724 if (s->name == ps->name)
725 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
727 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
732 /*-----------------------------------------
733 * add recommends to the work queue
735 if (s->recommends && m)
737 recp = s->repo->idarraydata + s->recommends;
738 while ((rec = *recp++) != 0)
740 FOR_PROVIDES(p, pp, rec)
742 queue_push(&workq, p);
745 if (s->suggests && m)
747 sugp = s->repo->idarraydata + s->suggests;
748 while ((sug = *sugp++) != 0)
750 FOR_PROVIDES(p, pp, sug)
752 queue_push(&workq, p);
760 /*-------------------------------------------------------------------
762 * Add rules for packages possibly selected in by weak dependencies
764 * m: already added solvables
768 solver_addrpmrulesforweak(Solver *solv, Map *m)
770 Pool *pool = solv->pool;
775 /* foreach solvable in pool */
776 for (i = n = 1; n < pool->nsolvables; i++, n++)
778 if (i == pool->nsolvables) /* wrap i */
780 if (MAPTST(m, i)) /* already added that one */
783 s = pool->solvables + i;
786 if (s->repo != pool->installed && !pool_installable(pool, s))
787 continue; /* only look at installable ones */
792 /* find possible supplements */
793 supp = s->repo->idarraydata + s->supplements;
794 while ((sup = *supp++) != 0)
795 if (dep_possible(solv, sup, m))
799 /* if nothing found, check for enhances */
800 if (!sup && s->enhances)
802 supp = s->repo->idarraydata + s->enhances;
803 while ((sup = *supp++) != 0)
804 if (dep_possible(solv, sup, m))
807 /* if nothing found, goto next solvables */
810 solver_addrpmrulesforsolvable(solv, s, m);
811 n = 0; /* check all solvables again because we added solvables to m */
816 /*-------------------------------------------------------------------
818 * add package rules for possible updates
821 * m: map of already visited solvables
822 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
826 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
828 Pool *pool = solv->pool;
830 /* queue and buffer for it */
834 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
835 /* find update candidates for 's' */
836 policy_findupdatepackages(solv, s, &qs, allow_all);
837 /* add rule for 's' if not already done */
838 if (!MAPTST(m, s - pool->solvables))
839 solver_addrpmrulesforsolvable(solv, s, m);
840 /* foreach update candidate, add rule if not already done */
841 for (i = 0; i < qs.count; i++)
842 if (!MAPTST(m, qs.elements[i]))
843 solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
848 /***********************************************************************
850 *** Update/Feature rule part
852 *** Those rules make sure an installed package isn't silently deleted
857 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
859 Pool *pool = solv->pool;
862 policy_findupdatepackages(solv, s, qs, allow_all ? allow_all : 2);
866 return 0; /* orphaned, don't create feature rule */
867 /* check if this is an orphaned package */
868 policy_findupdatepackages(solv, s, qs, 1);
870 return 0; /* orphaned, don't create update rule */
872 return -SYSTEMSOLVABLE; /* supported but not installable */
875 return s - pool->solvables;
876 /* check if it is ok to keep the installed package */
877 for (i = 0; i < qs->count; i++)
879 Solvable *ns = pool->solvables + qs->elements[i];
880 if (s->evr == ns->evr && solvable_identical(s, ns))
881 return s - pool->solvables;
883 /* nope, it must be some other package */
884 return -SYSTEMSOLVABLE;
887 /* add packages from the dup repositories to the update candidates
888 * this isn't needed for the global dup mode as all packages are
889 * from dup repos in that case */
891 addduppackages(Solver *solv, Solvable *s, Queue *qs)
896 int oldnoupdateprovide = solv->noupdateprovide;
898 queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
899 solv->noupdateprovide = 1;
900 policy_findupdatepackages(solv, s, &dupqs, 2);
901 solv->noupdateprovide = oldnoupdateprovide;
902 for (i = 0; i < dupqs.count; i++)
904 p = dupqs.elements[i];
905 if (MAPTST(&solv->dupmap, p))
906 queue_pushunique(qs, p);
911 /*-------------------------------------------------------------------
913 * add rule for update
914 * (A|A1|A2|A3...) An = update candidates for A
916 * s = (installed) solvable
920 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
922 /* installed packages get a special upgrade allowed rule */
923 Pool *pool = solv->pool;
928 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
929 p = s - pool->solvables;
930 /* find update candidates for 's' */
931 if (solv->dupmap_all)
932 p = finddistupgradepackages(solv, s, &qs, allow_all);
934 policy_findupdatepackages(solv, s, &qs, allow_all);
935 if (!allow_all && !solv->dupmap_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
936 addduppackages(solv, s, &qs);
938 if (!allow_all && qs.count && solv->noobsoletes.size)
942 d = pool_queuetowhatprovides(pool, &qs);
943 /* filter out all noobsoletes packages as they don't update */
944 for (i = j = 0; i < qs.count; i++)
946 if (MAPTST(&solv->noobsoletes, qs.elements[i]))
948 /* it's ok if they have same nevra */
949 Solvable *ps = pool->solvables + qs.elements[i];
950 if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
953 qs.elements[j++] = qs.elements[i];
957 if (d && solv->installed && s->repo == solv->installed &&
958 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
960 if (!solv->multiversionupdaters)
961 solv->multiversionupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
962 solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
964 if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
966 queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
971 else if (p != -SYSTEMSOLVABLE)
973 /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
975 solver_addrule(solv, p, d); /* allow update of s */
979 if (qs.count && p == -SYSTEMSOLVABLE)
980 p = queue_shift(&qs);
981 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
983 solver_addrule(solv, p, d); /* allow update of s */
987 disableupdaterule(Solver *solv, Id p)
991 MAPSET(&solv->noupdate, p - solv->installed->start);
992 r = solv->rules + solv->updaterules + (p - solv->installed->start);
993 if (r->p && r->d >= 0)
994 solver_disablerule(solv, r);
995 r = solv->rules + solv->featurerules + (p - solv->installed->start);
996 if (r->p && r->d >= 0)
997 solver_disablerule(solv, r);
998 if (solv->bestrules_pkg)
1001 ni = solv->bestrules_end - solv->bestrules;
1002 for (i = 0; i < ni; i++)
1003 if (solv->bestrules_pkg[i] == p)
1004 solver_disablerule(solv, solv->rules + solv->bestrules + i);
1009 reenableupdaterule(Solver *solv, Id p)
1011 Pool *pool = solv->pool;
1014 MAPCLR(&solv->noupdate, p - solv->installed->start);
1015 r = solv->rules + solv->updaterules + (p - solv->installed->start);
1020 solver_enablerule(solv, r);
1021 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1023 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1024 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1030 r = solv->rules + solv->featurerules + (p - solv->installed->start);
1031 if (r->p && r->d < 0)
1033 solver_enablerule(solv, r);
1034 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1036 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1037 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1041 if (solv->bestrules_pkg)
1044 ni = solv->bestrules_end - solv->bestrules;
1045 for (i = 0; i < ni; i++)
1046 if (solv->bestrules_pkg[i] == p)
1047 solver_enablerule(solv, solv->rules + solv->bestrules + i);
1052 /***********************************************************************
1054 *** Infarch rule part
1056 *** Infarch rules make sure the solver uses the best architecture of
1057 *** a package if multiple archetectures are available
1062 solver_addinfarchrules(Solver *solv, Map *addedmap)
1064 Pool *pool = solv->pool;
1066 Id p, pp, a, aa, bestarch;
1067 Solvable *s, *ps, *bests;
1068 Queue badq, allowedarchs;
1071 queue_init(&allowedarchs);
1072 solv->infarchrules = solv->nrules;
1073 for (i = 1; i < pool->nsolvables; i++)
1075 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1077 s = pool->solvables + i;
1081 queue_empty(&allowedarchs);
1082 FOR_PROVIDES(p, pp, s->name)
1084 ps = pool->solvables + p;
1085 if (ps->name != s->name || !MAPTST(addedmap, p))
1092 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1093 if (a != 1 && pool->installed && ps->repo == pool->installed)
1095 if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1096 queue_pushunique(&allowedarchs, ps->arch); /* also ok to keep this architecture */
1097 continue; /* ignore installed solvables when calculating the best arch */
1099 if (a && a != 1 && (!bestarch || a < bestarch))
1107 /* speed up common case where installed package already has best arch */
1108 if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1109 allowedarchs.count--; /* installed arch is best */
1111 FOR_PROVIDES(p, pp, s->name)
1113 ps = pool->solvables + p;
1114 if (ps->name != s->name || !MAPTST(addedmap, p))
1117 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1118 if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1120 if (pool->installed && ps->repo == pool->installed)
1121 continue; /* always ok to keep an installed package */
1122 for (j = 0; j < allowedarchs.count; j++)
1124 aa = allowedarchs.elements[j];
1127 aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1128 if (aa && ((a ^ aa) & 0xffff0000) == 0)
1129 break; /* compatible */
1131 if (j == allowedarchs.count)
1132 queue_push(&badq, p);
1137 /* block all solvables in the badq! */
1138 for (j = 0; j < badq.count; j++)
1140 p = badq.elements[j];
1141 solver_addrule(solv, -p, 0);
1145 queue_free(&allowedarchs);
1146 solv->infarchrules_end = solv->nrules;
1150 disableinfarchrule(Solver *solv, Id name)
1152 Pool *pool = solv->pool;
1155 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1157 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1158 solver_disablerule(solv, r);
1163 reenableinfarchrule(Solver *solv, Id name)
1165 Pool *pool = solv->pool;
1168 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1170 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1172 solver_enablerule(solv, r);
1173 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1175 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1176 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1183 /***********************************************************************
1187 *** Dup rules make sure a package is selected from the specified dup
1188 *** repositories if an update candidate is included in one of them.
1193 add_cleandeps_package(Solver *solv, Id p)
1195 if (!solv->cleandeps_updatepkgs)
1197 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
1198 queue_init(solv->cleandeps_updatepkgs);
1200 queue_pushunique(solv->cleandeps_updatepkgs, p);
1204 solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
1206 Pool *pool = solv->pool;
1207 Solvable *ps, *s = pool->solvables + p;
1208 Repo *installed = solv->installed;
1209 Id pi, pip, obs, *obsp;
1211 MAPSET(&solv->dupinvolvedmap, p);
1213 MAPSET(&solv->dupmap, p);
1214 FOR_PROVIDES(pi, pip, s->name)
1216 ps = pool->solvables + pi;
1217 if (ps->name != s->name)
1219 MAPSET(&solv->dupinvolvedmap, pi);
1220 if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1223 for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1224 if (pool->solvables[pi2].repo != installed)
1225 MAPSET(&solv->dupinvolvedmap, pi2);
1227 if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1229 if (!solv->bestupdatemap.size)
1230 map_grow(&solv->bestupdatemap, installed->end - installed->start);
1231 MAPSET(&solv->bestupdatemap, pi - installed->start);
1233 if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1234 add_cleandeps_package(solv, pi);
1235 if (!targeted && ps->repo != installed)
1236 MAPSET(&solv->dupmap, pi);
1238 if (s->repo == installed && solv->obsoletes && solv->obsoletes[p - installed->start])
1241 for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1243 ps = pool->solvables + pi;
1244 if (ps->repo == installed)
1246 MAPSET(&solv->dupinvolvedmap, pi);
1248 MAPSET(&solv->dupmap, pi);
1251 if (targeted && s->repo != installed && s->obsoletes)
1253 /* XXX: check obsoletes/provides combination */
1254 obsp = s->repo->idarraydata + s->obsoletes;
1255 while ((obs = *obsp++) != 0)
1257 FOR_PROVIDES(pi, pip, obs)
1259 Solvable *ps = pool->solvables + pi;
1260 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1262 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1264 MAPSET(&solv->dupinvolvedmap, pi);
1265 if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1268 for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1269 if (pool->solvables[pi2].repo != installed)
1270 MAPSET(&solv->dupinvolvedmap, pi2);
1272 if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1274 if (!solv->bestupdatemap.size)
1275 map_grow(&solv->bestupdatemap, installed->end - installed->start);
1276 MAPSET(&solv->bestupdatemap, pi - installed->start);
1278 if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1279 add_cleandeps_package(solv, pi);
1286 solver_createdupmaps(Solver *solv)
1288 Queue *job = &solv->job;
1289 Pool *pool = solv->pool;
1290 Repo *installed = solv->installed;
1291 Id select, how, what, p, pp;
1295 map_init(&solv->dupmap, pool->nsolvables);
1296 map_init(&solv->dupinvolvedmap, pool->nsolvables);
1297 for (i = 0; i < job->count; i += 2)
1299 how = job->elements[i];
1300 select = job->elements[i] & SOLVER_SELECTMASK;
1301 what = job->elements[i + 1];
1302 switch (how & SOLVER_JOBMASK)
1304 case SOLVER_DISTUPGRADE:
1305 if (select == SOLVER_SOLVABLE_REPO)
1308 if (what <= 0 || what > pool->nrepos)
1310 repo = pool_id2repo(pool, what);
1313 if (repo != installed && !(how & SOLVER_TARGETED) && solv->noautotarget)
1315 targeted = repo != installed || (how & SOLVER_TARGETED) != 0;
1316 FOR_REPO_SOLVABLES(repo, p, s)
1318 if (repo != installed && !pool_installable(pool, s))
1320 solver_addtodupmaps(solv, p, how, targeted);
1325 targeted = how & SOLVER_TARGETED ? 1 : 0;
1326 if (installed && !targeted && !solv->noautotarget)
1328 FOR_JOB_SELECT(p, pp, select, what)
1329 if (pool->solvables[p].repo == installed)
1333 else if (!installed && !solv->noautotarget)
1335 FOR_JOB_SELECT(p, pp, select, what)
1337 Solvable *s = pool->solvables + p;
1340 if (s->repo != installed && !targeted)
1342 if (s->repo != installed && !pool_installable(pool, s))
1344 solver_addtodupmaps(solv, p, how, targeted);
1352 MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1356 solver_freedupmaps(Solver *solv)
1358 map_free(&solv->dupmap);
1359 /* we no longer free solv->dupinvolvedmap as we need it in
1360 * policy's priority pruning code. sigh. */
1364 solver_addduprules(Solver *solv, Map *addedmap)
1366 Pool *pool = solv->pool;
1371 solv->duprules = solv->nrules;
1372 for (i = 1; i < pool->nsolvables; i++)
1374 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1376 s = pool->solvables + i;
1378 FOR_PROVIDES(p, pp, s->name)
1380 ps = pool->solvables + p;
1381 if (ps->name != s->name || !MAPTST(addedmap, p))
1387 if (!MAPTST(&solv->dupinvolvedmap, p))
1389 if (solv->installed && ps->repo == solv->installed)
1391 if (!solv->updatemap.size)
1392 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1393 MAPSET(&solv->updatemap, p - solv->installed->start);
1394 if (!MAPTST(&solv->dupmap, p))
1397 /* is installed identical to a good one? */
1398 FOR_PROVIDES(ip, ipp, ps->name)
1400 Solvable *is = pool->solvables + ip;
1401 if (!MAPTST(&solv->dupmap, ip))
1403 if (is->evr == ps->evr && solvable_identical(ps, is))
1407 solver_addrule(solv, -p, 0); /* no match, sorry */
1409 MAPSET(&solv->dupmap, p); /* for best rules processing */
1412 else if (!MAPTST(&solv->dupmap, p))
1413 solver_addrule(solv, -p, 0);
1416 solv->duprules_end = solv->nrules;
1421 disableduprule(Solver *solv, Id name)
1423 Pool *pool = solv->pool;
1426 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1428 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1429 solver_disablerule(solv, r);
1434 reenableduprule(Solver *solv, Id name)
1436 Pool *pool = solv->pool;
1439 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1441 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1443 solver_enablerule(solv, r);
1444 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1446 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1447 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1454 /***********************************************************************
1456 *** Policy rule disabling/reenabling
1458 *** Disable all policy rules that conflict with our jobs. If a job
1459 *** gets disabled later on, reenable the involved policy rules again.
1463 #define DISABLE_UPDATE 1
1464 #define DISABLE_INFARCH 2
1465 #define DISABLE_DUP 3
1468 * add all installed packages that package p obsoletes to Queue q.
1469 * Package p is not installed. Also, we know that if
1470 * solv->keepexplicitobsoletes is not set, p is not in the noobs map.
1471 * Entries may get added multiple times.
1474 add_obsoletes(Solver *solv, Id p, Queue *q)
1476 Pool *pool = solv->pool;
1477 Repo *installed = solv->installed;
1479 Solvable *s = pool->solvables + p;
1483 if (!solv->keepexplicitobsoletes || !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p)))
1485 FOR_PROVIDES(p2, pp2, s->name)
1487 Solvable *ps = pool->solvables + p2;
1488 if (ps->repo != installed)
1490 if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1492 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1500 obsp = s->repo->idarraydata + s->obsoletes;
1501 while ((obs = *obsp++) != 0)
1502 FOR_PROVIDES(p2, pp2, obs)
1504 Solvable *ps = pool->solvables + p2;
1505 if (ps->repo != installed)
1507 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1509 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1519 * Call add_obsoletes and intersect the result with the
1520 * elements in Queue q starting at qstart.
1521 * Assumes that it's the first call if qstart == q->count.
1522 * May use auxillary map m for the intersection process, all
1523 * elements of q starting at qstart must have their bit cleared.
1524 * (This is also true after the function returns.)
1527 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
1530 int qcount = q->count;
1532 add_obsoletes(solv, p, q);
1533 if (qcount == qstart)
1534 return; /* first call */
1535 if (qcount == q->count)
1537 else if (qcount == qstart + 1)
1539 /* easy if there's just one element */
1541 for (i = qcount; i < q->count; i++)
1542 if (q->elements[i] == q->elements[qstart])
1544 j++; /* keep the element */
1548 else if (!m->size && q->count - qstart <= 8)
1550 /* faster than a map most of the time */
1552 for (i = j = qstart; i < qcount; i++)
1554 Id ip = q->elements[i];
1555 for (k = qcount; k < q->count; k++)
1556 if (q->elements[k] == ip)
1558 q->elements[j++] = ip;
1565 /* for the really pathologic cases we use the map */
1566 Repo *installed = solv->installed;
1568 map_init(m, installed->end - installed->start);
1569 for (i = qcount; i < q->count; i++)
1570 MAPSET(m, q->elements[i] - installed->start);
1571 for (i = j = qstart; i < qcount; i++)
1572 if (MAPTST(m, q->elements[i] - installed->start))
1574 MAPCLR(m, q->elements[i] - installed->start);
1575 q->elements[j++] = q->elements[i];
1578 queue_truncate(q, j);
1582 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1584 Pool *pool = solv->pool;
1588 int i, j, set, qstart;
1591 installed = solv->installed;
1592 select = how & SOLVER_SELECTMASK;
1593 switch (how & SOLVER_JOBMASK)
1595 case SOLVER_INSTALL:
1596 set = how & SOLVER_SETMASK;
1597 if (!(set & SOLVER_NOAUTOSET))
1599 /* automatically add set bits by analysing the job */
1600 if (select == SOLVER_SOLVABLE_NAME)
1601 set |= SOLVER_SETNAME;
1602 if (select == SOLVER_SOLVABLE)
1603 set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
1604 else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
1606 Reldep *rd = GETRELDEP(pool, what);
1607 if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
1609 if (pool->disttype != DISTTYPE_DEB)
1611 const char *evr = pool_id2str(pool, rd->evr);
1612 if (strchr(evr, '-'))
1613 set |= SOLVER_SETEVR;
1615 set |= SOLVER_SETEV;
1618 set |= SOLVER_SETEVR;
1620 if (rd->flags <= 7 && ISRELDEP(rd->name))
1621 rd = GETRELDEP(pool, rd->name);
1622 if (rd->flags == REL_ARCH)
1623 set |= SOLVER_SETARCH;
1627 set &= ~SOLVER_NOAUTOSET;
1630 if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
1632 if (select == SOLVER_SOLVABLE)
1633 queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
1636 int qcnt = q->count;
1637 /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
1638 they are not useful for SOLVER_INSTALL jobs anyway */
1639 FOR_JOB_SELECT(p, pp, select, what)
1641 s = pool->solvables + p;
1643 for (i = qcnt; i < q->count; i += 2)
1644 if (q->elements[i + 1] == s->name)
1648 queue_push2(q, DISABLE_INFARCH, s->name);
1652 if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
1654 if (select == SOLVER_SOLVABLE)
1655 queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
1658 int qcnt = q->count;
1659 FOR_JOB_SELECT(p, pp, select, what)
1661 s = pool->solvables + p;
1663 for (i = qcnt; i < q->count; i += 2)
1664 if (q->elements[i + 1] == s->name)
1668 queue_push2(q, DISABLE_DUP, s->name);
1672 if (!installed || installed->end == installed->start)
1674 /* now the hard part: disable some update rules */
1676 /* first check if we have noobs or installed packages in the job */
1678 FOR_JOB_SELECT(p, pp, select, what)
1680 if (pool->solvables[p].repo == installed)
1682 else if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && !solv->keepexplicitobsoletes)
1686 if (j) /* have installed packages */
1688 /* this is for dupmap_all jobs, it can go away if we create
1689 * duprules for them */
1690 if (i == 1 && (set & SOLVER_SETREPO) != 0)
1691 queue_push2(q, DISABLE_UPDATE, j);
1697 FOR_JOB_SELECT(p, pp, select, what)
1699 intersect_obsoletes(solv, p, q, qstart, &omap);
1700 if (q->count == qstart)
1706 if (qstart == q->count)
1707 return; /* nothing to prune */
1709 /* convert result to (DISABLE_UPDATE, p) pairs */
1711 for (j = qstart; j < i; j++)
1712 queue_push(q, q->elements[j]);
1713 for (j = qstart; j < q->count; j += 2)
1715 q->elements[j] = DISABLE_UPDATE;
1716 q->elements[j + 1] = q->elements[i++];
1719 /* now that we know which installed packages are obsoleted check each of them */
1720 if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
1721 return; /* all is set, nothing to do */
1723 for (i = j = qstart; i < q->count; i += 2)
1725 Solvable *is = pool->solvables + q->elements[i + 1];
1726 FOR_JOB_SELECT(p, pp, select, what)
1729 s = pool->solvables + p;
1730 if ((set & SOLVER_SETEVR) != 0)
1731 illegal |= POLICY_ILLEGAL_DOWNGRADE; /* ignore */
1732 if ((set & SOLVER_SETNAME) != 0)
1733 illegal |= POLICY_ILLEGAL_NAMECHANGE; /* ignore */
1734 if ((set & SOLVER_SETARCH) != 0)
1735 illegal |= POLICY_ILLEGAL_ARCHCHANGE; /* ignore */
1736 if ((set & SOLVER_SETVENDOR) != 0)
1737 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
1738 illegal = policy_is_illegal(solv, is, s, illegal);
1739 if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
1741 /* it's ok if the EV is different */
1742 if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
1750 /* no package conflicts with the update rule */
1751 /* thus keep the DISABLE_UPDATE */
1752 q->elements[j + 1] = q->elements[i + 1];
1756 queue_truncate(q, j);
1762 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
1763 FOR_REPO_SOLVABLES(installed, p, s)
1764 queue_push2(q, DISABLE_UPDATE, p);
1765 FOR_JOB_SELECT(p, pp, select, what)
1766 if (pool->solvables[p].repo == installed)
1767 queue_push2(q, DISABLE_UPDATE, p);
1774 /* disable all policy rules that are in conflict with our job list */
1776 solver_disablepolicyrules(Solver *solv)
1778 Queue *job = &solv->job;
1785 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1787 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1789 r = solv->rules + i;
1790 if (r->d < 0) /* disabled? */
1792 j = solv->ruletojob.elements[i - solv->jobrules];
1796 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1798 if (solv->cleandepsmap.size)
1800 solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1801 for (i = solv->installed->start; i < solv->installed->end; i++)
1802 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1803 queue_push2(&allq, DISABLE_UPDATE, i);
1805 MAPZERO(&solv->noupdate);
1806 for (i = 0; i < allq.count; i += 2)
1808 Id type = allq.elements[i], arg = allq.elements[i + 1];
1811 case DISABLE_UPDATE:
1812 disableupdaterule(solv, arg);
1814 case DISABLE_INFARCH:
1815 disableinfarchrule(solv, arg);
1818 disableduprule(solv, arg);
1827 /* we just disabled job #jobidx, now reenable all policy rules that were
1828 * disabled because of this job */
1830 solver_reenablepolicyrules(Solver *solv, int jobidx)
1832 Queue *job = &solv->job;
1837 Id qbuf[32], allqbuf[128];
1839 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1840 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1841 jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
1844 if (job->count > 64)
1845 queue_prealloc(&allq, job->count * 2);
1846 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1848 r = solv->rules + i;
1849 if (r->d < 0) /* disabled? */
1851 j = solv->ruletojob.elements[i - solv->jobrules];
1855 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1857 if (solv->cleandepsmap.size)
1859 solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1860 for (i = solv->installed->start; i < solv->installed->end; i++)
1861 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1862 queue_push2(&allq, DISABLE_UPDATE, i);
1864 for (j = 0; j < q.count; j += 2)
1866 Id type = q.elements[j], arg = q.elements[j + 1];
1867 for (i = 0; i < allq.count; i += 2)
1868 if (allq.elements[i] == type && allq.elements[i + 1] == arg)
1871 continue; /* still disabled */
1874 case DISABLE_UPDATE:
1875 reenableupdaterule(solv, arg);
1877 case DISABLE_INFARCH:
1878 reenableinfarchrule(solv, arg);
1881 reenableduprule(solv, arg);
1889 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
1890 * disabled because of this */
1892 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
1894 Queue *job = &solv->job;
1901 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1902 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1904 r = solv->rules + i;
1905 if (r->d < 0) /* disabled? */
1907 j = solv->ruletojob.elements[i - solv->jobrules];
1911 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1913 for (i = 0; i < allq.count; i += 2)
1914 if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
1916 if (i == allq.count)
1917 reenableupdaterule(solv, pkg);
1922 /***********************************************************************
1924 *** Rule info part, tell the user what the rule is about.
1929 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1931 Pool *pool = solv->pool;
1935 /* check if this creates the rule we're searching for */
1936 r = solv->rules + solv->ruleinfoq->elements[0];
1938 od = r->d < 0 ? -r->d - 1 : r->d;
1943 if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1945 w2 = pool->whatprovidesdata[d];
1949 if (p > 0 && d < 0) /* this hack is used for buddy deps */
1961 Id *dp = pool->whatprovidesdata + d;
1962 Id *odp = pool->whatprovidesdata + od;
1964 if (*dp++ != *odp++)
1970 /* handle multiversion conflict rules */
1971 if (p < 0 && pool->whatprovidesdata[d] < 0)
1973 w2 = pool->whatprovidesdata[d];
1974 /* XXX: free memory */
1984 if (w2 != op || p != ow2)
1989 if (p != op || w2 != ow2)
1993 /* yep, rule matches. record info */
1994 queue_push(solv->ruleinfoq, type);
1995 if (type == SOLVER_RULE_RPM_SAME_NAME)
1997 /* we normalize same name order */
1998 queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
1999 queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
2003 queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2004 queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
2006 queue_push(solv->ruleinfoq, dep);
2010 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2012 const Id *a = ap, *b = bp;
2031 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2033 Pool *pool = solv->pool;
2034 Rule *r = solv->rules + rid;
2038 if (rid <= 0 || rid >= solv->rpmrules_end)
2040 Id type, from, to, dep;
2041 type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2042 queue_push(rq, type);
2043 queue_push(rq, from);
2045 queue_push(rq, dep);
2050 queue_push(rq, rid);
2051 solv->ruleinfoq = rq;
2052 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2053 /* also try reverse direction for conflicts */
2054 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2055 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2056 solv->ruleinfoq = 0;
2058 /* now sort & unify em */
2061 solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2062 /* throw out identical entries */
2063 for (i = j = 0; i < rq->count; i += 4)
2067 if (rq->elements[i] == rq->elements[j - 4] &&
2068 rq->elements[i + 1] == rq->elements[j - 3] &&
2069 rq->elements[i + 2] == rq->elements[j - 2] &&
2070 rq->elements[i + 3] == rq->elements[j - 1])
2073 rq->elements[j++] = rq->elements[i];
2074 rq->elements[j++] = rq->elements[i + 1];
2075 rq->elements[j++] = rq->elements[i + 2];
2076 rq->elements[j++] = rq->elements[i + 3];
2083 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2085 Pool *pool = solv->pool;
2086 Rule *r = solv->rules + rid;
2087 SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2095 if (rid > 0 && rid < solv->rpmrules_end)
2101 return SOLVER_RULE_RPM;
2105 queue_push(&rq, rid);
2106 solv->ruleinfoq = &rq;
2107 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2108 /* also try reverse direction for conflicts */
2109 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2110 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2111 solv->ruleinfoq = 0;
2112 type = SOLVER_RULE_RPM;
2113 for (i = 1; i < rq.count; i += 4)
2116 qt = rq.elements[i];
2117 qp = rq.elements[i + 1];
2118 qo = rq.elements[i + 2];
2119 qd = rq.elements[i + 3];
2120 if (type == SOLVER_RULE_RPM || type > qt)
2134 if (rid >= solv->jobrules && rid < solv->jobrules_end)
2136 Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2140 *top = solv->job.elements[jidx];
2142 *depp = solv->job.elements[jidx + 1];
2143 if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2145 if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2146 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2147 if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2148 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2149 if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2150 return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2151 if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2152 return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2154 return SOLVER_RULE_JOB;
2156 if (rid >= solv->updaterules && rid < solv->updaterules_end)
2159 *fromp = solv->installed->start + (rid - solv->updaterules);
2160 return SOLVER_RULE_UPDATE;
2162 if (rid >= solv->featurerules && rid < solv->featurerules_end)
2165 *fromp = solv->installed->start + (rid - solv->featurerules);
2166 return SOLVER_RULE_FEATURE;
2168 if (rid >= solv->duprules && rid < solv->duprules_end)
2173 *depp = pool->solvables[-r->p].name;
2174 return SOLVER_RULE_DISTUPGRADE;
2176 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2181 *depp = pool->solvables[-r->p].name;
2182 return SOLVER_RULE_INFARCH;
2184 if (rid >= solv->bestrules && rid < solv->bestrules_end)
2186 return SOLVER_RULE_BEST;
2188 if (rid >= solv->choicerules && rid < solv->choicerules_end)
2190 return SOLVER_RULE_CHOICE;
2192 if (rid >= solv->learntrules)
2194 return SOLVER_RULE_LEARNT;
2196 return SOLVER_RULE_UNKNOWN;
2200 solver_ruleclass(Solver *solv, Id rid)
2203 return SOLVER_RULE_UNKNOWN;
2204 if (rid > 0 && rid < solv->rpmrules_end)
2205 return SOLVER_RULE_RPM;
2206 if (rid >= solv->jobrules && rid < solv->jobrules_end)
2207 return SOLVER_RULE_JOB;
2208 if (rid >= solv->updaterules && rid < solv->updaterules_end)
2209 return SOLVER_RULE_UPDATE;
2210 if (rid >= solv->featurerules && rid < solv->featurerules_end)
2211 return SOLVER_RULE_FEATURE;
2212 if (rid >= solv->duprules && rid < solv->duprules_end)
2213 return SOLVER_RULE_DISTUPGRADE;
2214 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2215 return SOLVER_RULE_INFARCH;
2216 if (rid >= solv->bestrules && rid < solv->bestrules_end)
2217 return SOLVER_RULE_BEST;
2218 if (rid >= solv->choicerules && rid < solv->choicerules_end)
2219 return SOLVER_RULE_CHOICE;
2220 if (rid >= solv->learntrules)
2221 return SOLVER_RULE_LEARNT;
2222 return SOLVER_RULE_UNKNOWN;
2226 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2228 Pool *pool = solv->pool;
2233 r = solv->rules + rid;
2234 FOR_RULELITERALS(p, pp, r)
2235 if (p != -SYSTEMSOLVABLE)
2238 queue_push(q, -SYSTEMSOLVABLE); /* hmm, better to return an empty result? */
2242 solver_rule2jobidx(Solver *solv, Id rid)
2244 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2246 return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2250 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2253 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2259 idx = solv->ruletojob.elements[rid - solv->jobrules];
2261 *whatp = solv->job.elements[idx + 1];
2262 return solv->job.elements[idx];
2266 solver_addchoicerules(Solver *solv)
2268 Pool *pool = solv->pool;
2272 int i, j, rid, havechoice;
2276 Id lastaddedp, lastaddedd;
2279 solv->choicerules = solv->nrules;
2280 if (!pool->installed)
2282 solv->choicerules_end = solv->nrules;
2285 solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2288 map_init(&m, pool->nsolvables);
2289 map_init(&mneg, pool->nsolvables);
2290 /* set up negative assertion map from infarch and dup rules */
2291 for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2292 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2293 MAPSET(&mneg, -r->p);
2294 for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2295 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2296 MAPSET(&mneg, -r->p);
2300 for (rid = 1; rid < solv->rpmrules_end ; rid++)
2302 r = solv->rules + rid;
2303 if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2304 continue; /* only look at requires rules */
2305 /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2309 FOR_RULELITERALS(p, pp, r)
2313 s = pool->solvables + p;
2316 if (s->repo == pool->installed)
2321 /* check if this package is "blocked" by a installed package */
2323 FOR_PROVIDES(p2, pp2, s->name)
2325 s2 = pool->solvables + p2;
2326 if (s2->repo != pool->installed)
2328 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2330 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2336 /* found installed package p2 that we can update to p */
2337 if (MAPTST(&mneg, p))
2339 if (policy_is_illegal(solv, s2, s, 0))
2341 queue_push(&qi, p2);
2347 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2349 while ((obs = *obsp++) != 0)
2351 FOR_PROVIDES(p2, pp2, obs)
2353 s2 = pool->solvables + p2;
2354 if (s2->repo != pool->installed)
2356 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2358 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2367 /* found installed package p2 that we can update to p */
2368 if (MAPTST(&mneg, p))
2370 if (policy_is_illegal(solv, s2, s, 0))
2372 queue_push(&qi, p2);
2377 /* package p is independent of the installed ones */
2380 if (!havechoice || !q.count)
2381 continue; /* no choice */
2383 /* now check the update rules of the installed package.
2384 * if all packages of the update rules are contained in
2385 * the dependency rules, there's no need to set up the choice rule */
2387 FOR_RULELITERALS(p, pp, r)
2390 for (i = 0; i < qi.count; i++)
2393 if (!qi.elements[i])
2395 ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2397 ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2400 FOR_RULELITERALS(p, pp, ur)
2405 for (j = i + 1; j < qi.count; j++)
2406 if (qi.elements[i] == qi.elements[j])
2412 printf("skipping choice ");
2413 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2418 /* don't add identical rules */
2419 if (lastaddedp == r->p && lastaddedcnt == q.count)
2421 for (i = 0; i < q.count; i++)
2422 if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
2425 continue; /* already added that one */
2427 d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
2431 lastaddedcnt = q.count;
2433 solver_addrule(solv, r->p, d);
2434 queue_push(&solv->weakruleq, solv->nrules - 1);
2435 solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
2438 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2439 printf("WEAK CHOICE ");
2440 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
2447 solv->choicerules_end = solv->nrules;
2450 /* called when a choice rule is disabled by analyze_unsolvable. We also
2451 * have to disable all other choice rules so that the best packages get
2454 solver_disablechoicerules(Solver *solv, Rule *r)
2457 Pool *pool = solv->pool;
2461 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2462 map_init(&m, pool->nsolvables);
2463 FOR_RULELITERALS(p, pp, or)
2466 FOR_RULELITERALS(p, pp, r)
2469 for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
2471 r = solv->rules + rid;
2474 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2475 FOR_RULELITERALS(p, pp, or)
2476 if (p > 0 && MAPTST(&m, p))
2479 solver_disablerule(solv, r);
2484 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
2488 for (i = j = 0; i < q->count; i++)
2491 for (cp2 = cp; *cp2; cp2++)
2494 q->elements[j++] = p;
2498 queue_truncate(q, j);
2502 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
2505 for (i = j = 0; i < q->count; i++)
2507 Id p = q->elements[i];
2508 if (MAPTST(&solv->dupmap, p))
2509 q->elements[j++] = p;
2511 queue_truncate(q, j);
2515 solver_addbestrules(Solver *solv, int havebestinstalljobs)
2517 Pool *pool = solv->pool;
2520 Repo *installed = solv->installed;
2526 solv->bestrules = solv->nrules;
2529 solv->bestrules_end = solv->nrules;
2536 if (havebestinstalljobs)
2538 for (i = 0; i < solv->job.count; i += 2)
2540 if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
2544 for (j = 0; j < solv->ruletojob.count; j++)
2545 if (solv->ruletojob.elements[j] == i)
2547 if (j == solv->ruletojob.count)
2549 r = solv->rules + solv->jobrules + j;
2551 FOR_RULELITERALS(p2, pp2, r)
2555 continue; /* orphaned */
2556 /* select best packages, just look at prio and version */
2558 policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2559 if (q.count == oldcnt)
2560 continue; /* nothing filtered */
2561 p2 = queue_shift(&q);
2562 solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2563 queue_push(&r2pkg, -(solv->jobrules + j));
2568 if (solv->bestupdatemap_all || solv->bestupdatemap.size)
2570 FOR_REPO_SOLVABLES(installed, p, s)
2573 if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
2575 if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
2578 if (solv->bestobeypolicy)
2579 r = solv->rules + solv->updaterules + (p - installed->start);
2582 r = solv->rules + solv->featurerules + (p - installed->start);
2583 if (!r->p) /* identical to update rule? */
2584 r = solv->rules + solv->updaterules + (p - installed->start);
2586 if (solv->multiversionupdaters && (d = solv->multiversionupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
2588 /* need to check multiversionupdaters */
2589 if (r->p == p) /* be careful with the dup case */
2591 while ((p2 = pool->whatprovidesdata[d++]) != 0)
2596 FOR_RULELITERALS(p2, pp2, r)
2600 if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2601 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
2602 if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2603 prune_to_dup_packages(solv, p, &q);
2604 /* select best packages, just look at prio and version */
2605 policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2607 continue; /* orphaned */
2608 if (solv->bestobeypolicy)
2610 /* also filter the best of the feature rule packages and add them */
2611 r = solv->rules + solv->featurerules + (p - installed->start);
2616 FOR_RULELITERALS(p2, pp2, r)
2618 queue_push(&q2, p2);
2619 if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2620 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
2621 if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2622 prune_to_dup_packages(solv, p, &q);
2623 policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
2624 for (j = 0; j < q2.count; j++)
2625 queue_pushunique(&q, q2.elements[j]);
2628 p2 = queue_shift(&q);
2629 solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2630 queue_push(&r2pkg, p);
2635 solv->bestrules_pkg = solv_calloc(r2pkg.count, sizeof(Id));
2636 memcpy(solv->bestrules_pkg, r2pkg.elements, r2pkg.count * sizeof(Id));
2638 solv->bestrules_end = solv->nrules;
2644 #undef CLEANDEPSDEBUG
2647 * This functions collects all packages that are looked at
2648 * when a dependency is checked. We need it to "pin" installed
2649 * packages when removing a supplemented package in createcleandepsmap.
2650 * Here's an not uncommon example:
2651 * A contains "Supplements: packageand(B, C)"
2652 * B contains "Requires: A"
2653 * Now if we remove C, the supplements is no longer true,
2654 * thus we also remove A. Without the dep_pkgcheck function, we
2655 * would now also remove B, but this is wrong, as adding back
2656 * C doesn't make the supplements true again. Thus we "pin" B
2658 * There's probably a better way to do this, but I haven't come
2662 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
2664 Pool *pool = solv->pool;
2669 Reldep *rd = GETRELDEP(pool, dep);
2672 if (rd->flags == REL_AND)
2674 dep_pkgcheck(solv, rd->name, m, q);
2675 dep_pkgcheck(solv, rd->evr, m, q);
2678 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2680 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2684 FOR_PROVIDES(p, pp, dep)
2685 if (!m || MAPTST(m, p))
2690 check_xsupp(Solver *solv, Queue *depq, Id dep)
2692 Pool *pool = solv->pool;
2697 Reldep *rd = GETRELDEP(pool, dep);
2700 if (rd->flags == REL_AND)
2702 if (!check_xsupp(solv, depq, rd->name))
2704 return check_xsupp(solv, depq, rd->evr);
2706 if (rd->flags == REL_OR)
2708 if (check_xsupp(solv, depq, rd->name))
2710 return check_xsupp(solv, depq, rd->evr);
2712 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2713 return solver_splitprovides(solv, rd->evr);
2714 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2715 return solver_dep_installed(solv, rd->evr);
2717 if (depq && rd->flags == REL_NAMESPACE)
2720 for (i = 0; i < depq->count; i++)
2721 if (depq->elements[i] == dep || depq->elements[i] == rd->name)
2725 FOR_PROVIDES(p, pp, dep)
2726 if (p == SYSTEMSOLVABLE || pool->solvables[p].repo == solv->installed)
2732 queue_contains(Queue *q, Id id)
2735 for (i = 0; i < q->count; i++)
2736 if (q->elements[i] == id)
2743 * Find all installed packages that are no longer
2744 * needed regarding the current solver job.
2747 * - remove pass: remove all packages that could have
2748 * been dragged in by the obsoleted packages.
2749 * i.e. if package A is obsolete and contains "Requires: B",
2750 * also remove B, as installing A will have pulled in B.
2751 * after this pass, we have a set of still installed packages
2752 * with broken dependencies.
2754 * now add back all packages that the still installed packages
2757 * The cleandeps packages are the packages removed in the first
2758 * pass and not added back in the second pass.
2760 * If we search for unneeded packages (unneeded is true), we
2761 * simply remove all packages except the userinstalled ones in
2765 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
2767 Pool *pool = solv->pool;
2768 Repo *installed = solv->installed;
2769 Queue *job = &solv->job;
2774 Id rid, how, what, select;
2776 Id req, *reqp, sup, *supp;
2778 Queue iq, iqcopy, xsuppq;
2781 map_empty(cleandepsmap);
2782 if (!installed || installed->end == installed->start)
2784 map_init(&userinstalled, installed->end - installed->start);
2785 map_init(&im, pool->nsolvables);
2786 map_init(&installedm, pool->nsolvables);
2788 queue_init(&xsuppq);
2790 for (i = 0; i < job->count; i += 2)
2792 how = job->elements[i];
2793 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2795 what = job->elements[i + 1];
2796 select = how & SOLVER_SELECTMASK;
2797 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2798 FOR_REPO_SOLVABLES(installed, p, s)
2799 MAPSET(&userinstalled, p - installed->start);
2800 FOR_JOB_SELECT(p, pp, select, what)
2801 if (pool->solvables[p].repo == installed)
2802 MAPSET(&userinstalled, p - installed->start);
2804 if ((how & (SOLVER_JOBMASK | SOLVER_SELECTMASK)) == (SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES))
2806 what = job->elements[i + 1];
2809 Reldep *rd = GETRELDEP(pool, what);
2810 if (rd->flags != REL_NAMESPACE)
2814 queue_pushunique(&iq, rd->name);
2817 FOR_PROVIDES(p, pp, what)
2822 queue_pushunique(&iq, what);
2827 /* have special namespace cleandeps erases */
2830 for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2832 s = pool->solvables + ip;
2833 if (s->repo != installed)
2835 if (!s->supplements)
2837 supp = s->repo->idarraydata + s->supplements;
2838 while ((sup = *supp++) != 0)
2839 if (check_xsupp(solv, &iq, sup) && !check_xsupp(solv, 0, sup))
2841 #ifdef CLEANDEPSDEBUG
2842 printf("xsupp %s from %s\n", pool_dep2str(pool, sup), pool_solvid2str(pool, ip));
2844 queue_pushunique(&xsuppq, sup);
2850 /* also add visible patterns to userinstalled for openSUSE */
2854 dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
2855 while (dataiterator_step(&di))
2860 s = pool->solvables + di.solvid;
2861 if (!s->repo || !s->requires)
2863 if (s->repo != installed && !pool_installable(pool, s))
2865 if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
2867 dp = s->repo->idarraydata + s->requires;
2868 for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
2869 FOR_PROVIDES(p, pp, *dp)
2870 if (pool->solvables[p].repo == installed)
2872 if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
2874 MAPSET(&userinstalled, p - installed->start);
2877 dataiterator_free(&di);
2881 /* all products and their buddies are userinstalled */
2882 for (p = installed->start; p < installed->end; p++)
2884 Solvable *s = pool->solvables + p;
2885 if (s->repo != installed)
2887 if (!strncmp("product:", pool_id2str(pool, s->name), 8))
2889 MAPSET(&userinstalled, p - installed->start);
2890 if (pool->nscallback)
2892 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, p);
2893 if (buddy >= installed->start && buddy < installed->end && pool->solvables[buddy].repo == installed)
2894 MAPSET(&userinstalled, buddy - installed->start);
2900 /* add all positive elements (e.g. locks) to "userinstalled" */
2901 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2903 r = solv->rules + rid;
2906 i = solv->ruletojob.elements[rid - solv->jobrules];
2907 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2909 FOR_RULELITERALS(p, jp, r)
2910 if (p > 0 && pool->solvables[p].repo == installed)
2911 MAPSET(&userinstalled, p - installed->start);
2914 /* add all cleandeps candidates to iq */
2915 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2917 r = solv->rules + rid;
2918 if (r->d < 0) /* disabled? */
2920 if (r->d == 0 && r->p < 0 && r->w2 == 0) /* negative assertion (erase job)? */
2923 if (pool->solvables[p].repo != installed)
2925 MAPCLR(&userinstalled, p - installed->start);
2928 i = solv->ruletojob.elements[rid - solv->jobrules];
2929 how = job->elements[i];
2930 if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
2933 else if (r->p > 0) /* install job */
2937 i = solv->ruletojob.elements[rid - solv->jobrules];
2938 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2940 /* check if the literals all obsolete some installed package */
2944 /* just one installed literal */
2945 if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
2948 if (solv->noobsoletes.size && !solv->keepexplicitobsoletes)
2950 FOR_RULELITERALS(p, jp, r)
2951 if (MAPTST(&solv->noobsoletes, p))
2959 FOR_RULELITERALS(p, jp, r)
2963 queue_truncate(&iq, iqstart); /* abort */
2966 if (pool->solvables[p].repo == installed)
2968 if (iq.count == iqstart)
2972 for (i = iqstart; i < iq.count; i++)
2973 if (iq.elements[i] == p)
2975 queue_truncate(&iq, iqstart);
2981 intersect_obsoletes(solv, p, &iq, iqstart, &om);
2982 if (iq.count == iqstart)
2990 queue_init_clone(&iqcopy, &iq);
2994 if (solv->cleandeps_updatepkgs)
2995 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
2996 queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
3000 queue_empty(&iq); /* just in case... */
3002 /* clear userinstalled bit for the packages we really want to delete/update */
3003 for (i = 0; i < iq.count; i++)
3006 if (pool->solvables[p].repo != installed)
3008 MAPCLR(&userinstalled, p - installed->start);
3011 for (p = installed->start; p < installed->end; p++)
3013 if (pool->solvables[p].repo != installed)
3015 MAPSET(&installedm, p);
3016 if (unneeded && !MAPTST(&userinstalled, p - installed->start))
3020 MAPSET(&installedm, SYSTEMSOLVABLE);
3021 MAPSET(&im, SYSTEMSOLVABLE);
3023 #ifdef CLEANDEPSDEBUG
3024 printf("REMOVE PASS\n");
3033 /* supplements pass */
3034 for (ip = installed->start; ip < installed->end; ip++)
3036 if (!MAPTST(&installedm, ip))
3038 s = pool->solvables + ip;
3039 if (!s->supplements)
3041 if (!MAPTST(&im, ip))
3043 if (MAPTST(&userinstalled, ip - installed->start))
3045 supp = s->repo->idarraydata + s->supplements;
3046 while ((sup = *supp++) != 0)
3047 if (dep_possible(solv, sup, &im))
3051 supp = s->repo->idarraydata + s->supplements;
3052 while ((sup = *supp++) != 0)
3053 if (dep_possible(solv, sup, &installedm) || (xsuppq.count && queue_contains(&xsuppq, sup)))
3055 /* no longer supplemented, also erase */
3056 int iqcount = iq.count;
3057 /* pin packages, see comment above dep_pkgcheck */
3058 dep_pkgcheck(solv, sup, &im, &iq);
3059 for (i = iqcount; i < iq.count; i++)
3061 Id pqp = iq.elements[i];
3062 if (pool->solvables[pqp].repo == installed)
3063 MAPSET(&userinstalled, pqp - installed->start);
3065 queue_truncate(&iq, iqcount);
3066 #ifdef CLEANDEPSDEBUG
3067 printf("%s supplemented [%s]\n", pool_solvid2str(pool, ip), pool_dep2str(pool, sup));
3069 queue_push(&iq, ip);
3074 break; /* no supplementing package found, we're done */
3076 ip = queue_shift(&iq);
3077 s = pool->solvables + ip;
3078 if (!MAPTST(&im, ip))
3080 if (!MAPTST(&installedm, ip))
3082 if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
3085 #ifdef CLEANDEPSDEBUG
3086 printf("removing %s\n", pool_solvable2str(pool, s));
3090 reqp = s->repo->idarraydata + s->requires;
3091 while ((req = *reqp++) != 0)
3093 if (req == SOLVABLE_PREREQMARKER)
3096 /* count number of installed packages that match */
3098 FOR_PROVIDES(p, pp, req)
3099 if (MAPTST(&installedm, p))
3104 FOR_PROVIDES(p, pp, req)
3106 if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3108 #ifdef CLEANDEPSDEBUG
3109 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3118 reqp = s->repo->idarraydata + s->recommends;
3119 while ((req = *reqp++) != 0)
3123 FOR_PROVIDES(p, pp, req)
3124 if (MAPTST(&installedm, p))
3129 FOR_PROVIDES(p, pp, req)
3131 if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3133 #ifdef CLEANDEPSDEBUG
3134 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3143 /* turn userinstalled into remove set for pruning */
3144 map_empty(&userinstalled);
3145 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3147 r = solv->rules + rid;
3148 if (r->p >= 0 || r->d != 0 || r->w2 != 0)
3149 continue; /* disabled or not erase */
3152 if (pool->solvables[p].repo == installed)
3153 MAPSET(&userinstalled, p - installed->start);
3155 MAPSET(&im, SYSTEMSOLVABLE); /* in case we cleared it above */
3156 for (p = installed->start; p < installed->end; p++)
3159 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3161 r = solv->rules + rid;
3164 FOR_RULELITERALS(p, jp, r)
3168 /* also put directly addressed packages on the install queue
3169 * so we can mark patterns as installed */
3170 for (i = 0; i < job->count; i += 2)
3172 how = job->elements[i];
3173 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3175 what = job->elements[i + 1];
3176 select = how & SOLVER_SELECTMASK;
3177 if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
3178 queue_push(&iq, what);
3182 #ifdef CLEANDEPSDEBUG
3183 printf("ADDBACK PASS\n");
3189 /* supplements pass */
3190 for (ip = installed->start; ip < installed->end; ip++)
3192 if (!MAPTST(&installedm, ip))
3194 if (MAPTST(&userinstalled, ip - installed->start))
3196 s = pool->solvables + ip;
3197 if (!s->supplements)
3199 if (MAPTST(&im, ip))
3201 supp = s->repo->idarraydata + s->supplements;
3202 while ((sup = *supp++) != 0)
3203 if (dep_possible(solv, sup, &im))
3207 #ifdef CLEANDEPSDEBUG
3208 printf("%s supplemented\n", pool_solvid2str(pool, ip));
3211 queue_push(&iq, ip);
3217 ip = queue_shift(&iq);
3218 s = pool->solvables + ip;
3219 #ifdef CLEANDEPSDEBUG
3220 printf("adding back %s\n", pool_solvable2str(pool, s));
3224 reqp = s->repo->idarraydata + s->requires;
3225 while ((req = *reqp++) != 0)
3227 FOR_PROVIDES(p, pp, req)
3232 FOR_PROVIDES(p, pp, req)
3234 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3238 if (MAPTST(&userinstalled, p - installed->start))
3240 #ifdef CLEANDEPSDEBUG
3241 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3251 reqp = s->repo->idarraydata + s->recommends;
3252 while ((req = *reqp++) != 0)
3254 FOR_PROVIDES(p, pp, req)
3259 FOR_PROVIDES(p, pp, req)
3261 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3265 if (MAPTST(&userinstalled, p - installed->start))
3267 #ifdef CLEANDEPSDEBUG
3268 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3279 /* make sure the updatepkgs and mistakes are not in the cleandeps map */
3280 if (solv->cleandeps_updatepkgs)
3281 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3282 MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
3283 if (solv->cleandeps_mistakes)
3284 for (i = 0; i < solv->cleandeps_mistakes->count; i++)
3285 MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
3286 /* also remove original iq packages */
3287 for (i = 0; i < iqcopy.count; i++)
3288 MAPSET(&im, iqcopy.elements[i]);
3289 queue_free(&iqcopy);
3290 for (p = installed->start; p < installed->end; p++)
3292 if (pool->solvables[p].repo != installed)
3294 if (!MAPTST(&im, p))
3295 MAPSET(cleandepsmap, p - installed->start);
3298 map_free(&installedm);
3299 map_free(&userinstalled);
3300 queue_free(&xsuppq);
3301 #ifdef CLEANDEPSDEBUG
3302 printf("=== final cleandeps map:\n");
3303 for (p = installed->start; p < installed->end; p++)
3304 if (MAPTST(cleandepsmap, p - installed->start))
3305 printf(" - %s\n", pool_solvid2str(pool, p));
3318 /* Tarjan's SCC algorithm, slightly modifed */
3320 trj_visit(struct trj_data *trj, Id node)
3323 Queue *edges = trj->edges;
3324 Id nnode, myidx, stackstart;
3327 low[node] = myidx = trj->idx++;
3328 low[(stackstart = trj->nstack++)] = node;
3329 for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
3334 if (!edges->elements[edges->elements[nnode]])
3340 trj_visit(trj, nnode);
3345 if (l < trj->firstidx)
3348 for (k = l; low[low[k]] == l; k++)
3351 else if (l < low[node])
3354 if (low[node] == myidx)
3356 if (myidx != trj->firstidx)
3358 for (i = stackstart; i < trj->nstack; i++)
3359 low[low[i]] = myidx;
3360 trj->nstack = stackstart;
3366 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
3368 Repo *installed = solv->installed;
3372 queue_empty(unneededq);
3373 if (!installed || installed->end == installed->start)
3376 map_init(&cleandepsmap, installed->end - installed->start);
3377 solver_createcleandepsmap(solv, &cleandepsmap, 1);
3378 for (i = installed->start; i < installed->end; i++)
3379 if (MAPTST(&cleandepsmap, i - installed->start))
3380 queue_push(unneededq, i);
3382 if (filtered && unneededq->count > 1)
3384 Pool *pool = solv->pool;
3388 int j, pass, count = unneededq->count;
3391 map_init(&installedm, pool->nsolvables);
3392 for (i = installed->start; i < installed->end; i++)
3393 if (pool->solvables[i].repo == installed)
3394 MAPSET(&installedm, i);
3396 nrequires = solv_calloc(count, sizeof(Id));
3398 queue_prealloc(&edges, count * 4 + 10); /* pre-size */
3401 * Go through the solvables in the nodes queue and create edges for
3402 * all requires/recommends/supplements between the nodes.
3403 * The edges are stored in the edges queue, we add 1 to the node
3404 * index so that nodes in the edges queue are != 0 and we can
3405 * terminate the edge list with 0.
3406 * Thus for node element 5, the edges are stored starting at
3407 * edges.elements[6] and are 0-terminated.
3409 /* leave first element zero to make things easier */
3410 /* also add trailing zero */
3411 queue_insertn(&edges, 0, 1 + count + 1);
3413 /* first requires and recommends */
3414 for (i = 0; i < count; i++)
3416 Solvable *s = pool->solvables + unneededq->elements[i];
3417 edges.elements[i + 1] = edges.count;
3418 for (pass = 0; pass < 2; pass++)
3421 unsigned int off = pass == 0 ? s->requires : s->recommends;
3424 for (dp = s->repo->idarraydata + off; *dp; dp++)
3425 FOR_PROVIDES(p, pp, *dp)
3427 Solvable *sp = pool->solvables + p;
3428 if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3430 for (j = 0; j < count; j++)
3431 if (p == unneededq->elements[j])
3435 if (num && edges.elements[edges.count - 1] == j + 1)
3437 queue_push(&edges, j + 1);
3443 queue_push(&edges, 0);
3446 printf("requires + recommends\n");
3447 for (i = 0; i < count; i++)
3450 printf(" %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3451 for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3452 printf(" - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3456 /* then add supplements */
3457 for (i = 0; i < count; i++)
3459 Solvable *s = pool->solvables + unneededq->elements[i];
3464 for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
3465 if (dep_possible(solv, *dp, &installedm))
3469 queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
3470 dep_pkgcheck(solv, *dp, 0, &iq);
3471 for (k = 0; k < iq.count; k++)
3473 Id p = iq.elements[k];
3474 Solvable *sp = pool->solvables + p;
3475 if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3477 for (j = 0; j < count; j++)
3478 if (p == unneededq->elements[j])
3480 /* now add edge from j + 1 to i + 1 */
3481 queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
3482 /* addapt following edge pointers */
3483 for (k = j + 2; k < count + 2; k++)
3484 edges.elements[k]++;
3492 printf("+ supplements\n");
3493 for (i = 0; i < count; i++)
3496 printf(" %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3497 for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3498 printf(" - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3501 map_free(&installedm);
3503 /* now run SCC algo two times, first with requires+recommends+supplements,
3504 * then again without the requires. We run it the second time to get rid
3505 * of packages that got dragged in via recommends/supplements */
3507 * low will contain the result of the SCC search.
3508 * it must be of at least size 2 * (count + 1) and
3509 * must be zero initialized.
3511 * 0 low low ... low stack stack ...stack 0
3514 low = solv_calloc(count + 1, 2 * sizeof(Id));
3515 for (pass = 0; pass < 2; pass++)
3517 struct trj_data trj;
3520 memset(low, 0, (count + 1) * (2 * sizeof(Id)));
3521 for (i = 0; i < count; i++)
3523 edges.elements[i + 1] += nrequires[i];
3524 if (!unneededq->elements[i])
3525 low[i + 1] = -1; /* ignore this node */
3530 trj.idx = count + 1; /* stack starts here */
3531 for (i = 1; i <= count; i++)
3535 if (edges.elements[edges.elements[i]])
3537 trj.firstidx = trj.nstack = trj.idx;
3542 Id myidx = trj.idx++;
3547 /* prune packages */
3548 for (i = 0; i < count; i++)
3549 if (low[i + 1] <= 0)
3550 unneededq->elements[i] = 0;
3553 solv_free(nrequires);
3556 /* finally remove all pruned entries from unneededq */
3557 for (i = j = 0; i < count; i++)
3558 if (unneededq->elements[i])
3559 unneededq->elements[j++] = unneededq->elements[i];
3560 queue_truncate(unneededq, j);
3562 map_free(&cleandepsmap);