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 /******************************************************************************
383 *** rpm rule part: create rules representing the package dependencies
388 * special multiversion patch conflict handling:
389 * a patch conflict is also satisfied if some other
390 * version with the same name/arch that doesn't conflict
391 * gets installed. The generated rule is thus:
392 * -patch|-cpack|opack1|opack2|...
395 makemultiversionconflict(Solver *solv, Id n, Id con)
397 Pool *pool = solv->pool;
402 sn = pool->solvables + n;
403 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
405 FOR_PROVIDES(p, pp, sn->name)
407 s = pool->solvables + p;
408 if (s->name != sn->name || s->arch != sn->arch)
410 if (!MAPTST(&solv->noobsoletes, p))
412 if (pool_match_nevr(pool, pool->solvables + p, con))
414 /* here we have a multiversion solvable that doesn't conflict */
415 /* thus we're not in conflict if it is installed */
419 return -n; /* no other package found, generate normal conflict */
420 return pool_queuetowhatprovides(pool, &q);
424 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
426 if (!solv->ruleinfoq)
427 solver_addrule(solv, p, d);
429 addrpmruleinfo(solv, p, d, type, dep);
432 /*-------------------------------------------------------------------
434 * add (install) rules for solvable
436 * s: Solvable for which to add rules
437 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
439 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
440 * solvables, the edges their dependencies.
441 * Starting from an installed solvable, this will create all rules
442 * representing the graph created by the solvables dependencies.
444 * for unfulfilled requirements, conflicts, obsoletes,....
445 * add a negative assertion for solvables that are not installable
447 * It will also create rules for all solvables referenced by 's'
448 * i.e. descend to all providers of requirements of 's'
453 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
455 Pool *pool = solv->pool;
456 Repo *installed = solv->installed;
458 /* 'work' queue. keeps Ids of solvables we still have to work on.
459 And buffer for it. */
464 /* if to add rules for broken deps ('rpm -V' functionality)
468 /* Id var and pointer for each dependency
469 * (not used in parallel)
476 Id p, pp; /* whatprovides loops */
477 Id *dp; /* ptr to 'whatprovides' */
478 Id n; /* Id for current solvable 's' */
480 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
481 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
483 /* loop until there's no more work left */
488 * s: Pointer to solvable
491 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
494 if (MAPTST(m, n)) /* continue if already visited */
496 MAPSET(m, n); /* mark as visited */
499 s = pool->solvables + n; /* s = Solvable in question */
502 if (installed /* Installed system available */
503 && s->repo == installed /* solvable is installed */
504 && !solv->fixmap_all /* NOT repair errors in rpm dependency graph */
505 && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
507 dontfix = 1; /* dont care about broken rpm deps */
511 && s->arch != ARCH_SRC
512 && s->arch != ARCH_NOSRC
513 && !pool_installable(pool, s))
515 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables));
516 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
519 /* yet another SUSE hack, sigh */
520 if (pool->nscallback && !strncmp("product:", pool_id2str(pool, s->name), 8))
522 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
523 if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
525 addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
526 addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy));
527 if (m && !MAPTST(m, buddy))
528 queue_push(&workq, buddy);
532 /*-----------------------------------------
533 * check requires of s
538 reqp = s->repo->idarraydata + s->requires;
539 while ((req = *reqp++) != 0) /* go through all requires */
541 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
544 /* find list of solvables providing 'req' */
545 dp = pool_whatprovides_ptr(pool, req);
547 if (*dp == SYSTEMSOLVABLE) /* always installed */
552 /* the strategy here is to not insist on dependencies
553 * that are already broken. so if we find one provider
554 * that was already installed, we know that the
555 * dependency was not broken before so we enforce it */
557 /* check if any of the providers for 'req' is installed */
558 for (i = 0; (p = dp[i]) != 0; i++)
560 if (pool->solvables[p].repo == installed)
561 break; /* provider was installed */
563 /* didn't find an installed provider: previously broken dependency */
566 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
573 /* nothing provides req! */
574 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables), pool_dep2str(pool, req));
575 addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
579 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
581 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION," %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
582 for (i = 0; dp[i]; i++)
583 POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, " provided by %s\n", pool_solvid2str(pool, dp[i]));
586 /* add 'requires' dependency */
587 /* rule: (-requestor|provider1|provider2|...|providerN) */
588 addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
590 /* descend the dependency tree
591 push all non-visited providers on the work queue */
597 queue_push(&workq, *dp);
601 } /* while, requirements of n */
603 } /* if, requirements */
605 /* that's all we check for src packages */
606 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
609 /*-----------------------------------------
610 * check conflicts of s
617 /* we treat conflicts in patches a bit differen:
619 * - multiversion handling
620 * XXX: we should really handle this different, looking
621 * at the name is a bad hack
623 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
625 conp = s->repo->idarraydata + s->conflicts;
626 /* foreach conflicts of 's' */
627 while ((con = *conp++) != 0)
629 /* foreach providers of a conflict of 's' */
630 FOR_PROVIDES(p, pp, con)
632 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
634 /* dontfix: dont care about conflicts with already installed packs */
635 if (dontfix && pool->solvables[p].repo == installed)
637 /* p == n: self conflict */
638 if (p == n && pool->forbidselfconflicts)
642 Reldep *rd = GETRELDEP(pool, con);
643 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
646 p = 0; /* make it a negative assertion, aka 'uninstallable' */
648 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
650 /* our patch conflicts with a noobsoletes (aka multiversion) package */
651 p = -makemultiversionconflict(solv, p, con);
653 /* rule: -n|-p: either solvable _or_ provider of conflict */
654 addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
659 /*-----------------------------------------
660 * check obsoletes and implicit obsoletes of a package
661 * if ignoreinstalledsobsoletes is not set, we're also checking
662 * obsoletes of installed packages (like newer rpm versions)
664 if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
666 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
667 int isinstalled = (installed && s->repo == installed);
668 if (s->obsoletes && (!noobs || solv->keepexplicitobsoletes))
670 obsp = s->repo->idarraydata + s->obsoletes;
671 /* foreach obsoletes */
672 while ((obs = *obsp++) != 0)
674 /* foreach provider of an obsoletes of 's' */
675 FOR_PROVIDES(p, pp, obs)
677 Solvable *ps = pool->solvables + p;
680 if (isinstalled && dontfix && ps->repo == installed)
681 continue; /* don't repair installed/installed problems */
682 if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
683 && !pool_match_nevr(pool, ps, obs))
685 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
688 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
690 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
694 /* check implicit obsoletes
695 * for installed packages we only need to check installed/installed problems (and
696 * only when dontfix is not set), as the others are picked up when looking at the
697 * uninstalled package.
699 if (!isinstalled || !dontfix)
701 FOR_PROVIDES(p, pp, s->name)
703 Solvable *ps = pool->solvables + p;
706 if (isinstalled && ps->repo != installed)
708 /* we still obsolete packages with same nevra, like rpm does */
709 /* (actually, rpm mixes those packages. yuck...) */
710 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
712 if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
714 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
716 if (s->name == ps->name)
717 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
719 addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
724 /*-----------------------------------------
725 * add recommends to the work queue
727 if (s->recommends && m)
729 recp = s->repo->idarraydata + s->recommends;
730 while ((rec = *recp++) != 0)
732 FOR_PROVIDES(p, pp, rec)
734 queue_push(&workq, p);
737 if (s->suggests && m)
739 sugp = s->repo->idarraydata + s->suggests;
740 while ((sug = *sugp++) != 0)
742 FOR_PROVIDES(p, pp, sug)
744 queue_push(&workq, p);
752 /*-------------------------------------------------------------------
754 * Add rules for packages possibly selected in by weak dependencies
756 * m: already added solvables
760 solver_addrpmrulesforweak(Solver *solv, Map *m)
762 Pool *pool = solv->pool;
767 /* foreach solvable in pool */
768 for (i = n = 1; n < pool->nsolvables; i++, n++)
770 if (i == pool->nsolvables) /* wrap i */
772 if (MAPTST(m, i)) /* already added that one */
775 s = pool->solvables + i;
778 if (s->repo != pool->installed && !pool_installable(pool, s))
779 continue; /* only look at installable ones */
784 /* find possible supplements */
785 supp = s->repo->idarraydata + s->supplements;
786 while ((sup = *supp++) != 0)
787 if (dep_possible(solv, sup, m))
791 /* if nothing found, check for enhances */
792 if (!sup && s->enhances)
794 supp = s->repo->idarraydata + s->enhances;
795 while ((sup = *supp++) != 0)
796 if (dep_possible(solv, sup, m))
799 /* if nothing found, goto next solvables */
802 solver_addrpmrulesforsolvable(solv, s, m);
803 n = 0; /* check all solvables again because we added solvables to m */
808 /*-------------------------------------------------------------------
810 * add package rules for possible updates
813 * m: map of already visited solvables
814 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
818 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
820 Pool *pool = solv->pool;
822 /* queue and buffer for it */
826 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
827 /* find update candidates for 's' */
828 policy_findupdatepackages(solv, s, &qs, allow_all);
829 /* add rule for 's' if not already done */
830 if (!MAPTST(m, s - pool->solvables))
831 solver_addrpmrulesforsolvable(solv, s, m);
832 /* foreach update candidate, add rule if not already done */
833 for (i = 0; i < qs.count; i++)
834 if (!MAPTST(m, qs.elements[i]))
835 solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
840 /***********************************************************************
842 *** Update/Feature rule part
844 *** Those rules make sure an installed package isn't silently deleted
849 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
851 Pool *pool = solv->pool;
854 policy_findupdatepackages(solv, s, qs, allow_all ? allow_all : 2);
858 return 0; /* orphaned, don't create feature rule */
859 /* check if this is an orphaned package */
860 policy_findupdatepackages(solv, s, qs, 1);
862 return 0; /* orphaned, don't create update rule */
864 return -SYSTEMSOLVABLE; /* supported but not installable */
867 return s - pool->solvables;
868 /* check if it is ok to keep the installed package */
869 for (i = 0; i < qs->count; i++)
871 Solvable *ns = pool->solvables + qs->elements[i];
872 if (s->evr == ns->evr && solvable_identical(s, ns))
873 return s - pool->solvables;
875 /* nope, it must be some other package */
876 return -SYSTEMSOLVABLE;
879 /* add packages from the dup repositories to the update candidates
880 * this isn't needed for the global dup mode as all packages are
881 * from dup repos in that case */
883 addduppackages(Solver *solv, Solvable *s, Queue *qs)
888 int oldnoupdateprovide = solv->noupdateprovide;
890 queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
891 solv->noupdateprovide = 1;
892 policy_findupdatepackages(solv, s, &dupqs, 2);
893 solv->noupdateprovide = oldnoupdateprovide;
894 for (i = 0; i < dupqs.count; i++)
896 p = dupqs.elements[i];
897 if (MAPTST(&solv->dupmap, p))
898 queue_pushunique(qs, p);
903 /*-------------------------------------------------------------------
905 * add rule for update
906 * (A|A1|A2|A3...) An = update candidates for A
908 * s = (installed) solvable
912 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
914 /* installed packages get a special upgrade allowed rule */
915 Pool *pool = solv->pool;
920 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
921 p = s - pool->solvables;
922 /* find update candidates for 's' */
923 if (solv->dupmap_all)
924 p = finddistupgradepackages(solv, s, &qs, allow_all);
926 policy_findupdatepackages(solv, s, &qs, allow_all);
927 if (!allow_all && !solv->dupmap_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
928 addduppackages(solv, s, &qs);
930 if (!allow_all && qs.count && solv->noobsoletes.size)
934 d = pool_queuetowhatprovides(pool, &qs);
935 /* filter out all noobsoletes packages as they don't update */
936 for (i = j = 0; i < qs.count; i++)
938 if (MAPTST(&solv->noobsoletes, qs.elements[i]))
940 /* it's ok if they have same nevra */
941 Solvable *ps = pool->solvables + qs.elements[i];
942 if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
945 qs.elements[j++] = qs.elements[i];
949 if (d && solv->installed && s->repo == solv->installed &&
950 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
952 if (!solv->multiversionupdaters)
953 solv->multiversionupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
954 solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
956 if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
958 queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
963 else if (p != -SYSTEMSOLVABLE)
965 /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
967 solver_addrule(solv, p, d); /* allow update of s */
971 if (qs.count && p == -SYSTEMSOLVABLE)
972 p = queue_shift(&qs);
973 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
975 solver_addrule(solv, p, d); /* allow update of s */
979 disableupdaterule(Solver *solv, Id p)
983 MAPSET(&solv->noupdate, p - solv->installed->start);
984 r = solv->rules + solv->updaterules + (p - solv->installed->start);
985 if (r->p && r->d >= 0)
986 solver_disablerule(solv, r);
987 r = solv->rules + solv->featurerules + (p - solv->installed->start);
988 if (r->p && r->d >= 0)
989 solver_disablerule(solv, r);
990 if (solv->bestrules_pkg)
993 ni = solv->bestrules_end - solv->bestrules;
994 for (i = 0; i < ni; i++)
995 if (solv->bestrules_pkg[i] == p)
996 solver_disablerule(solv, solv->rules + solv->bestrules + i);
1001 reenableupdaterule(Solver *solv, Id p)
1003 Pool *pool = solv->pool;
1006 MAPCLR(&solv->noupdate, p - solv->installed->start);
1007 r = solv->rules + solv->updaterules + (p - solv->installed->start);
1012 solver_enablerule(solv, r);
1013 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1015 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1016 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1022 r = solv->rules + solv->featurerules + (p - solv->installed->start);
1023 if (r->p && r->d < 0)
1025 solver_enablerule(solv, r);
1026 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1028 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1029 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1033 if (solv->bestrules_pkg)
1036 ni = solv->bestrules_end - solv->bestrules;
1037 for (i = 0; i < ni; i++)
1038 if (solv->bestrules_pkg[i] == p)
1039 solver_enablerule(solv, solv->rules + solv->bestrules + i);
1044 /***********************************************************************
1046 *** Infarch rule part
1048 *** Infarch rules make sure the solver uses the best architecture of
1049 *** a package if multiple archetectures are available
1054 solver_addinfarchrules(Solver *solv, Map *addedmap)
1056 Pool *pool = solv->pool;
1058 Id p, pp, a, aa, bestarch;
1059 Solvable *s, *ps, *bests;
1060 Queue badq, allowedarchs;
1063 queue_init(&allowedarchs);
1064 solv->infarchrules = solv->nrules;
1065 for (i = 1; i < pool->nsolvables; i++)
1067 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1069 s = pool->solvables + i;
1073 queue_empty(&allowedarchs);
1074 FOR_PROVIDES(p, pp, s->name)
1076 ps = pool->solvables + p;
1077 if (ps->name != s->name || !MAPTST(addedmap, p))
1084 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1085 if (a != 1 && pool->installed && ps->repo == pool->installed)
1087 if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1088 queue_pushunique(&allowedarchs, ps->arch); /* also ok to keep this architecture */
1089 continue; /* ignore installed solvables when calculating the best arch */
1091 if (a && a != 1 && (!bestarch || a < bestarch))
1099 /* speed up common case where installed package already has best arch */
1100 if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1101 allowedarchs.count--; /* installed arch is best */
1103 FOR_PROVIDES(p, pp, s->name)
1105 ps = pool->solvables + p;
1106 if (ps->name != s->name || !MAPTST(addedmap, p))
1109 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1110 if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1112 if (pool->installed && ps->repo == pool->installed)
1113 continue; /* always ok to keep an installed package */
1114 for (j = 0; j < allowedarchs.count; j++)
1116 aa = allowedarchs.elements[j];
1119 aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1120 if (aa && ((a ^ aa) & 0xffff0000) == 0)
1121 break; /* compatible */
1123 if (j == allowedarchs.count)
1124 queue_push(&badq, p);
1129 /* block all solvables in the badq! */
1130 for (j = 0; j < badq.count; j++)
1132 p = badq.elements[j];
1133 solver_addrule(solv, -p, 0);
1137 queue_free(&allowedarchs);
1138 solv->infarchrules_end = solv->nrules;
1142 disableinfarchrule(Solver *solv, Id name)
1144 Pool *pool = solv->pool;
1147 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1149 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1150 solver_disablerule(solv, r);
1155 reenableinfarchrule(Solver *solv, Id name)
1157 Pool *pool = solv->pool;
1160 for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1162 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1164 solver_enablerule(solv, r);
1165 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1167 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1168 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1175 /***********************************************************************
1179 *** Dup rules make sure a package is selected from the specified dup
1180 *** repositories if an update candidate is included in one of them.
1185 solver_createdupmaps(Solver *solv)
1187 Queue *job = &solv->job;
1188 Pool *pool = solv->pool;
1190 Repo *installed = solv->installed;
1191 Id select, how, what, p, pi, pp, pip, obs, *obsp;
1195 map_init(&solv->dupmap, pool->nsolvables);
1196 map_init(&solv->dupinvolvedmap, pool->nsolvables);
1197 for (i = 0; i < job->count; i += 2)
1199 how = job->elements[i];
1200 select = job->elements[i] & SOLVER_SELECTMASK;
1201 what = job->elements[i + 1];
1202 switch (how & SOLVER_JOBMASK)
1204 case SOLVER_DISTUPGRADE:
1205 if (select != SOLVER_SOLVABLE_REPO)
1211 FOR_JOB_SELECT(p, pp, select, what)
1212 if (pool->solvables[p].repo == installed)
1215 haveinstalled = p != 0;
1216 FOR_JOB_SELECT(p, pp, select, what)
1218 Solvable *s = pool->solvables + p;
1221 if (haveinstalled && s->repo != installed)
1223 if (s->repo != installed && !pool_installable(pool, s))
1225 MAPSET(&solv->dupinvolvedmap, p);
1227 MAPSET(&solv->dupmap, p);
1228 FOR_PROVIDES(pi, pip, s->name)
1230 ps = pool->solvables + pi;
1231 if (ps->name != s->name)
1233 MAPSET(&solv->dupinvolvedmap, pi);
1234 if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1236 if (!solv->bestupdatemap.size)
1237 map_grow(&solv->bestupdatemap, installed->end - installed->start);
1238 MAPSET(&solv->bestupdatemap, pi - installed->start);
1240 if (haveinstalled && ps->repo != installed)
1241 MAPSET(&solv->dupmap, pi);
1245 if (solv->obsoletes && solv->obsoletes[p - installed->start])
1248 for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1250 ps = pool->solvables + pi;
1251 if (ps->repo == installed)
1253 MAPSET(&solv->dupinvolvedmap, pi);
1254 MAPSET(&solv->dupmap, pi);
1258 else if (s->obsoletes)
1260 /* XXX: check obsoletes/provides combination */
1261 obsp = s->repo->idarraydata + s->obsoletes;
1262 while ((obs = *obsp++) != 0)
1264 FOR_PROVIDES(pi, pp, obs)
1266 Solvable *ps = pool->solvables + pi;
1267 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1269 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1271 MAPSET(&solv->dupinvolvedmap, pi);
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);
1284 if (what <= 0 || what > pool->nrepos)
1286 repo = pool_id2repo(pool, what);
1287 FOR_REPO_SOLVABLES(repo, p, s)
1289 if (repo != installed && !pool_installable(pool, s))
1291 MAPSET(&solv->dupmap, p);
1292 FOR_PROVIDES(pi, pip, s->name)
1294 ps = pool->solvables + pi;
1295 if (ps->name != s->name)
1297 MAPSET(&solv->dupinvolvedmap, pi);
1298 if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1300 if (!solv->bestupdatemap.size)
1301 map_grow(&solv->bestupdatemap, installed->end - installed->start);
1302 MAPSET(&solv->bestupdatemap, pi - installed->start);
1307 /* XXX: check obsoletes/provides combination */
1308 obsp = s->repo->idarraydata + s->obsoletes;
1309 while ((obs = *obsp++) != 0)
1311 FOR_PROVIDES(pi, pp, obs)
1313 Solvable *ps = pool->solvables + pi;
1314 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1316 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1318 MAPSET(&solv->dupinvolvedmap, pi);
1319 if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1321 if (!solv->bestupdatemap.size)
1322 map_grow(&solv->bestupdatemap, installed->end - installed->start);
1323 MAPSET(&solv->bestupdatemap, pi - installed->start);
1334 MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1338 solver_freedupmaps(Solver *solv)
1340 map_free(&solv->dupmap);
1341 /* we no longer free solv->dupinvolvedmap as we need it in
1342 * policy's priority pruning code. sigh. */
1346 solver_addduprules(Solver *solv, Map *addedmap)
1348 Pool *pool = solv->pool;
1353 solv->duprules = solv->nrules;
1354 for (i = 1; i < pool->nsolvables; i++)
1356 if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1358 s = pool->solvables + i;
1360 FOR_PROVIDES(p, pp, s->name)
1362 ps = pool->solvables + p;
1363 if (ps->name != s->name || !MAPTST(addedmap, p))
1369 if (!MAPTST(&solv->dupinvolvedmap, p))
1371 if (solv->installed && ps->repo == solv->installed)
1373 if (!solv->updatemap.size)
1374 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1375 MAPSET(&solv->updatemap, p - solv->installed->start);
1376 if (!MAPTST(&solv->dupmap, p))
1379 /* is installed identical to a good one? */
1380 FOR_PROVIDES(ip, ipp, ps->name)
1382 Solvable *is = pool->solvables + ip;
1383 if (!MAPTST(&solv->dupmap, ip))
1385 if (is->evr == ps->evr && solvable_identical(ps, is))
1389 solver_addrule(solv, -p, 0); /* no match, sorry */
1391 MAPSET(&solv->dupmap, p); /* for best rules processing */
1394 else if (!MAPTST(&solv->dupmap, p))
1395 solver_addrule(solv, -p, 0);
1398 solv->duprules_end = solv->nrules;
1403 disableduprule(Solver *solv, Id name)
1405 Pool *pool = solv->pool;
1408 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1410 if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1411 solver_disablerule(solv, r);
1416 reenableduprule(Solver *solv, Id name)
1418 Pool *pool = solv->pool;
1421 for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1423 if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1425 solver_enablerule(solv, r);
1426 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1428 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1429 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1436 /***********************************************************************
1438 *** Policy rule disabling/reenabling
1440 *** Disable all policy rules that conflict with our jobs. If a job
1441 *** gets disabled later on, reenable the involved policy rules again.
1445 #define DISABLE_UPDATE 1
1446 #define DISABLE_INFARCH 2
1447 #define DISABLE_DUP 3
1450 * add all installed packages that package p obsoletes to Queue q.
1451 * Package p is not installed. Also, we know that if
1452 * solv->keepexplicitobsoletes is not set, p is not in the noobs map.
1453 * Entries may get added multiple times.
1456 add_obsoletes(Solver *solv, Id p, Queue *q)
1458 Pool *pool = solv->pool;
1459 Repo *installed = solv->installed;
1461 Solvable *s = pool->solvables + p;
1465 if (!solv->keepexplicitobsoletes || !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p)))
1467 FOR_PROVIDES(p2, pp2, s->name)
1469 Solvable *ps = pool->solvables + p2;
1470 if (ps->repo != installed)
1472 if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1474 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1482 obsp = s->repo->idarraydata + s->obsoletes;
1483 while ((obs = *obsp++) != 0)
1484 FOR_PROVIDES(p2, pp2, obs)
1486 Solvable *ps = pool->solvables + p2;
1487 if (ps->repo != installed)
1489 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1491 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1501 * Call add_obsoletes and intersect the result with the
1502 * elements in Queue q starting at qstart.
1503 * Assumes that it's the first call if qstart == q->count.
1504 * May use auxillary map m for the intersection process, all
1505 * elements of q starting at qstart must have their bit cleared.
1506 * (This is also true after the function returns.)
1509 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
1512 int qcount = q->count;
1514 add_obsoletes(solv, p, q);
1515 if (qcount == qstart)
1516 return; /* first call */
1517 if (qcount == q->count)
1519 else if (qcount == qstart + 1)
1521 /* easy if there's just one element */
1523 for (i = qcount; i < q->count; i++)
1524 if (q->elements[i] == q->elements[qstart])
1526 j++; /* keep the element */
1530 else if (!m->size && q->count - qstart <= 8)
1532 /* faster than a map most of the time */
1534 for (i = j = qstart; i < qcount; i++)
1536 Id ip = q->elements[i];
1537 for (k = qcount; k < q->count; k++)
1538 if (q->elements[k] == ip)
1540 q->elements[j++] = ip;
1547 /* for the really pathologic cases we use the map */
1548 Repo *installed = solv->installed;
1550 map_init(m, installed->end - installed->start);
1551 for (i = qcount; i < q->count; i++)
1552 MAPSET(m, q->elements[i] - installed->start);
1553 for (i = j = qstart; i < qcount; i++)
1554 if (MAPTST(m, q->elements[i] - installed->start))
1556 MAPCLR(m, q->elements[i] - installed->start);
1557 q->elements[j++] = q->elements[i];
1560 queue_truncate(q, j);
1564 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1566 Pool *pool = solv->pool;
1570 int i, j, set, qstart;
1573 installed = solv->installed;
1574 select = how & SOLVER_SELECTMASK;
1575 switch (how & SOLVER_JOBMASK)
1577 case SOLVER_INSTALL:
1578 set = how & SOLVER_SETMASK;
1579 if (!(set & SOLVER_NOAUTOSET))
1581 /* automatically add set bits by analysing the job */
1582 if (select == SOLVER_SOLVABLE_NAME)
1583 set |= SOLVER_SETNAME;
1584 if (select == SOLVER_SOLVABLE)
1585 set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
1586 else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
1588 Reldep *rd = GETRELDEP(pool, what);
1589 if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
1591 if (pool->disttype != DISTTYPE_DEB)
1593 const char *evr = pool_id2str(pool, rd->evr);
1594 if (strchr(evr, '-'))
1595 set |= SOLVER_SETEVR;
1597 set |= SOLVER_SETEV;
1600 set |= SOLVER_SETEVR;
1602 if (rd->flags <= 7 && ISRELDEP(rd->name))
1603 rd = GETRELDEP(pool, rd->name);
1604 if (rd->flags == REL_ARCH)
1605 set |= SOLVER_SETARCH;
1609 set &= ~SOLVER_NOAUTOSET;
1612 if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
1614 if (select == SOLVER_SOLVABLE)
1615 queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
1618 int qcnt = q->count;
1619 /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
1620 they are not useful for SOLVER_INSTALL jobs anyway */
1621 FOR_JOB_SELECT(p, pp, select, what)
1623 s = pool->solvables + p;
1625 for (i = qcnt; i < q->count; i += 2)
1626 if (q->elements[i + 1] == s->name)
1630 queue_push2(q, DISABLE_INFARCH, s->name);
1634 if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
1636 if (select == SOLVER_SOLVABLE)
1637 queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
1640 int qcnt = q->count;
1641 FOR_JOB_SELECT(p, pp, select, what)
1643 s = pool->solvables + p;
1645 for (i = qcnt; i < q->count; i += 2)
1646 if (q->elements[i + 1] == s->name)
1650 queue_push2(q, DISABLE_DUP, s->name);
1654 if (!installed || installed->end == installed->start)
1656 /* now the hard part: disable some update rules */
1658 /* first check if we have noobs or installed packages in the job */
1660 FOR_JOB_SELECT(p, pp, select, what)
1662 if (pool->solvables[p].repo == installed)
1664 else if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && !solv->keepexplicitobsoletes)
1668 if (j) /* have installed packages */
1670 /* this is for dupmap_all jobs, it can go away if we create
1671 * duprules for them */
1672 if (i == 1 && (set & SOLVER_SETREPO) != 0)
1673 queue_push2(q, DISABLE_UPDATE, j);
1679 FOR_JOB_SELECT(p, pp, select, what)
1681 intersect_obsoletes(solv, p, q, qstart, &omap);
1682 if (q->count == qstart)
1688 if (qstart == q->count)
1689 return; /* nothing to prune */
1691 /* convert result to (DISABLE_UPDATE, p) pairs */
1693 for (j = qstart; j < i; j++)
1694 queue_push(q, q->elements[j]);
1695 for (j = qstart; j < q->count; j += 2)
1697 q->elements[j] = DISABLE_UPDATE;
1698 q->elements[j + 1] = q->elements[i++];
1701 /* now that we know which installed packages are obsoleted check each of them */
1702 if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
1703 return; /* all is set, nothing to do */
1705 for (i = j = qstart; i < q->count; i += 2)
1707 Solvable *is = pool->solvables + q->elements[i + 1];
1708 FOR_JOB_SELECT(p, pp, select, what)
1711 s = pool->solvables + p;
1712 if ((set & SOLVER_SETEVR) != 0)
1713 illegal |= POLICY_ILLEGAL_DOWNGRADE; /* ignore */
1714 if ((set & SOLVER_SETNAME) != 0)
1715 illegal |= POLICY_ILLEGAL_NAMECHANGE; /* ignore */
1716 if ((set & SOLVER_SETARCH) != 0)
1717 illegal |= POLICY_ILLEGAL_ARCHCHANGE; /* ignore */
1718 if ((set & SOLVER_SETVENDOR) != 0)
1719 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
1720 illegal = policy_is_illegal(solv, is, s, illegal);
1721 if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
1723 /* it's ok if the EV is different */
1724 if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
1732 /* no package conflicts with the update rule */
1733 /* thus keep the DISABLE_UPDATE */
1734 q->elements[j + 1] = q->elements[i + 1];
1738 queue_truncate(q, j);
1744 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
1745 FOR_REPO_SOLVABLES(installed, p, s)
1746 queue_push2(q, DISABLE_UPDATE, p);
1747 FOR_JOB_SELECT(p, pp, select, what)
1748 if (pool->solvables[p].repo == installed)
1749 queue_push2(q, DISABLE_UPDATE, p);
1756 /* disable all policy rules that are in conflict with our job list */
1758 solver_disablepolicyrules(Solver *solv)
1760 Queue *job = &solv->job;
1767 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1769 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1771 r = solv->rules + i;
1772 if (r->d < 0) /* disabled? */
1774 j = solv->ruletojob.elements[i - solv->jobrules];
1778 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1780 if (solv->cleandepsmap.size)
1782 solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1783 for (i = solv->installed->start; i < solv->installed->end; i++)
1784 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1785 queue_push2(&allq, DISABLE_UPDATE, i);
1787 MAPZERO(&solv->noupdate);
1788 for (i = 0; i < allq.count; i += 2)
1790 Id type = allq.elements[i], arg = allq.elements[i + 1];
1793 case DISABLE_UPDATE:
1794 disableupdaterule(solv, arg);
1796 case DISABLE_INFARCH:
1797 disableinfarchrule(solv, arg);
1800 disableduprule(solv, arg);
1809 /* we just disabled job #jobidx, now reenable all policy rules that were
1810 * disabled because of this job */
1812 solver_reenablepolicyrules(Solver *solv, int jobidx)
1814 Queue *job = &solv->job;
1819 Id qbuf[32], allqbuf[128];
1821 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1822 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1823 jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
1826 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1828 r = solv->rules + i;
1829 if (r->d < 0) /* disabled? */
1831 j = solv->ruletojob.elements[i - solv->jobrules];
1835 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1837 if (solv->cleandepsmap.size)
1839 solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1840 for (i = solv->installed->start; i < solv->installed->end; i++)
1841 if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1842 queue_push2(&allq, DISABLE_UPDATE, i);
1844 for (j = 0; j < q.count; j += 2)
1846 Id type = q.elements[j], arg = q.elements[j + 1];
1847 for (i = 0; i < allq.count; i += 2)
1848 if (allq.elements[i] == type && allq.elements[i + 1] == arg)
1851 continue; /* still disabled */
1854 case DISABLE_UPDATE:
1855 reenableupdaterule(solv, arg);
1857 case DISABLE_INFARCH:
1858 reenableinfarchrule(solv, arg);
1861 reenableduprule(solv, arg);
1869 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
1870 * disabled because of this */
1872 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
1874 Queue *job = &solv->job;
1881 queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1882 for (i = solv->jobrules; i < solv->jobrules_end; i++)
1884 r = solv->rules + i;
1885 if (r->d < 0) /* disabled? */
1887 j = solv->ruletojob.elements[i - solv->jobrules];
1891 jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1893 for (i = 0; i < allq.count; i += 2)
1894 if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
1896 if (i == allq.count)
1897 reenableupdaterule(solv, pkg);
1902 /***********************************************************************
1904 *** Rule info part, tell the user what the rule is about.
1909 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1911 Pool *pool = solv->pool;
1915 /* check if this creates the rule we're searching for */
1916 r = solv->rules + solv->ruleinfoq->elements[0];
1918 od = r->d < 0 ? -r->d - 1 : r->d;
1923 if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1925 w2 = pool->whatprovidesdata[d];
1929 if (p > 0 && d < 0) /* this hack is used for buddy deps */
1941 Id *dp = pool->whatprovidesdata + d;
1942 Id *odp = pool->whatprovidesdata + od;
1944 if (*dp++ != *odp++)
1950 /* handle multiversion conflict rules */
1951 if (p < 0 && pool->whatprovidesdata[d] < 0)
1953 w2 = pool->whatprovidesdata[d];
1954 /* XXX: free memory */
1964 if (w2 != op || p != ow2)
1969 if (p != op || w2 != ow2)
1973 /* yep, rule matches. record info */
1974 queue_push(solv->ruleinfoq, type);
1975 if (type == SOLVER_RULE_RPM_SAME_NAME)
1977 /* we normalize same name order */
1978 queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
1979 queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
1983 queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
1984 queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
1986 queue_push(solv->ruleinfoq, dep);
1990 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
1992 const Id *a = ap, *b = bp;
2011 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2013 Pool *pool = solv->pool;
2014 Rule *r = solv->rules + rid;
2018 if (rid <= 0 || rid >= solv->rpmrules_end)
2020 Id type, from, to, dep;
2021 type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2022 queue_push(rq, type);
2023 queue_push(rq, from);
2025 queue_push(rq, dep);
2030 queue_push(rq, rid);
2031 solv->ruleinfoq = rq;
2032 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2033 /* also try reverse direction for conflicts */
2034 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2035 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2036 solv->ruleinfoq = 0;
2038 /* now sort & unify em */
2041 solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2042 /* throw out identical entries */
2043 for (i = j = 0; i < rq->count; i += 4)
2047 if (rq->elements[i] == rq->elements[j - 4] &&
2048 rq->elements[i + 1] == rq->elements[j - 3] &&
2049 rq->elements[i + 2] == rq->elements[j - 2] &&
2050 rq->elements[i + 3] == rq->elements[j - 1])
2053 rq->elements[j++] = rq->elements[i];
2054 rq->elements[j++] = rq->elements[i + 1];
2055 rq->elements[j++] = rq->elements[i + 2];
2056 rq->elements[j++] = rq->elements[i + 3];
2063 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2065 Pool *pool = solv->pool;
2066 Rule *r = solv->rules + rid;
2067 SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2075 if (rid > 0 && rid < solv->rpmrules_end)
2081 return SOLVER_RULE_RPM;
2085 queue_push(&rq, rid);
2086 solv->ruleinfoq = &rq;
2087 solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2088 /* also try reverse direction for conflicts */
2089 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2090 solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2091 solv->ruleinfoq = 0;
2092 type = SOLVER_RULE_RPM;
2093 for (i = 1; i < rq.count; i += 4)
2096 qt = rq.elements[i];
2097 qp = rq.elements[i + 1];
2098 qo = rq.elements[i + 2];
2099 qd = rq.elements[i + 3];
2100 if (type == SOLVER_RULE_RPM || type > qt)
2114 if (rid >= solv->jobrules && rid < solv->jobrules_end)
2116 Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2120 *top = solv->job.elements[jidx];
2122 *depp = solv->job.elements[jidx + 1];
2123 if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2125 if ((solv->job.elements[jidx] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
2126 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2127 if ((solv->job.elements[jidx] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_PROVIDES)
2128 return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2130 return SOLVER_RULE_JOB;
2132 if (rid >= solv->updaterules && rid < solv->updaterules_end)
2135 *fromp = solv->installed->start + (rid - solv->updaterules);
2136 return SOLVER_RULE_UPDATE;
2138 if (rid >= solv->featurerules && rid < solv->featurerules_end)
2141 *fromp = solv->installed->start + (rid - solv->featurerules);
2142 return SOLVER_RULE_FEATURE;
2144 if (rid >= solv->duprules && rid < solv->duprules_end)
2149 *depp = pool->solvables[-r->p].name;
2150 return SOLVER_RULE_DISTUPGRADE;
2152 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2157 *depp = pool->solvables[-r->p].name;
2158 return SOLVER_RULE_INFARCH;
2160 if (rid >= solv->bestrules && rid < solv->bestrules_end)
2162 return SOLVER_RULE_BEST;
2164 if (rid >= solv->choicerules && rid < solv->choicerules_end)
2166 return SOLVER_RULE_CHOICE;
2168 if (rid >= solv->learntrules)
2170 return SOLVER_RULE_LEARNT;
2172 return SOLVER_RULE_UNKNOWN;
2176 solver_ruleclass(Solver *solv, Id rid)
2179 return SOLVER_RULE_UNKNOWN;
2180 if (rid > 0 && rid < solv->rpmrules_end)
2181 return SOLVER_RULE_RPM;
2182 if (rid >= solv->jobrules && rid < solv->jobrules_end)
2183 return SOLVER_RULE_JOB;
2184 if (rid >= solv->updaterules && rid < solv->updaterules_end)
2185 return SOLVER_RULE_UPDATE;
2186 if (rid >= solv->featurerules && rid < solv->featurerules_end)
2187 return SOLVER_RULE_FEATURE;
2188 if (rid >= solv->duprules && rid < solv->duprules_end)
2189 return SOLVER_RULE_DISTUPGRADE;
2190 if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2191 return SOLVER_RULE_INFARCH;
2192 if (rid >= solv->bestrules && rid < solv->bestrules_end)
2193 return SOLVER_RULE_BEST;
2194 if (rid >= solv->choicerules && rid < solv->choicerules_end)
2195 return SOLVER_RULE_CHOICE;
2196 if (rid >= solv->learntrules)
2197 return SOLVER_RULE_LEARNT;
2198 return SOLVER_RULE_UNKNOWN;
2202 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2204 Pool *pool = solv->pool;
2209 r = solv->rules + rid;
2210 FOR_RULELITERALS(p, pp, r)
2211 if (p != -SYSTEMSOLVABLE)
2214 queue_push(q, -SYSTEMSOLVABLE); /* hmm, better to return an empty result? */
2218 solver_rule2jobidx(Solver *solv, Id rid)
2220 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2222 return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2226 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2229 if (rid < solv->jobrules || rid >= solv->jobrules_end)
2235 idx = solv->ruletojob.elements[rid - solv->jobrules];
2237 *whatp = solv->job.elements[idx + 1];
2238 return solv->job.elements[idx];
2242 solver_addchoicerules(Solver *solv)
2244 Pool *pool = solv->pool;
2248 int i, j, rid, havechoice;
2252 Id lastaddedp, lastaddedd;
2255 solv->choicerules = solv->nrules;
2256 if (!pool->installed)
2258 solv->choicerules_end = solv->nrules;
2261 solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2264 map_init(&m, pool->nsolvables);
2265 map_init(&mneg, pool->nsolvables);
2266 /* set up negative assertion map from infarch and dup rules */
2267 for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2268 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2269 MAPSET(&mneg, -r->p);
2270 for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2271 if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2272 MAPSET(&mneg, -r->p);
2276 for (rid = 1; rid < solv->rpmrules_end ; rid++)
2278 r = solv->rules + rid;
2279 if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2280 continue; /* only look at requires rules */
2281 /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2285 FOR_RULELITERALS(p, pp, r)
2289 s = pool->solvables + p;
2292 if (s->repo == pool->installed)
2297 /* check if this package is "blocked" by a installed package */
2299 FOR_PROVIDES(p2, pp2, s->name)
2301 s2 = pool->solvables + p2;
2302 if (s2->repo != pool->installed)
2304 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2306 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2312 /* found installed package p2 that we can update to p */
2313 if (MAPTST(&mneg, p))
2315 if (policy_is_illegal(solv, s2, s, 0))
2317 queue_push(&qi, p2);
2323 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2325 while ((obs = *obsp++) != 0)
2327 FOR_PROVIDES(p2, pp2, obs)
2329 s2 = pool->solvables + p2;
2330 if (s2->repo != pool->installed)
2332 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2334 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2343 /* found installed package p2 that we can update to p */
2344 if (MAPTST(&mneg, p))
2346 if (policy_is_illegal(solv, s2, s, 0))
2348 queue_push(&qi, p2);
2353 /* package p is independent of the installed ones */
2356 if (!havechoice || !q.count)
2357 continue; /* no choice */
2359 /* now check the update rules of the installed package.
2360 * if all packages of the update rules are contained in
2361 * the dependency rules, there's no need to set up the choice rule */
2363 FOR_RULELITERALS(p, pp, r)
2366 for (i = 0; i < qi.count; i++)
2369 if (!qi.elements[i])
2371 ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2373 ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2376 FOR_RULELITERALS(p, pp, ur)
2381 for (j = i + 1; j < qi.count; j++)
2382 if (qi.elements[i] == qi.elements[j])
2388 printf("skipping choice ");
2389 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2394 /* don't add identical rules */
2395 if (lastaddedp == r->p && lastaddedcnt == q.count)
2397 for (i = 0; i < q.count; i++)
2398 if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
2401 continue; /* already added that one */
2404 d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
2405 solver_addrule(solv, r->p, d);
2406 queue_push(&solv->weakruleq, solv->nrules - 1);
2407 solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
2410 lastaddedcnt = q.count;
2413 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2414 printf("WEAK CHOICE ");
2415 solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
2422 solv->choicerules_end = solv->nrules;
2425 /* called when a choice rule is disabled by analyze_unsolvable. We also
2426 * have to disable all other choice rules so that the best packages get
2429 solver_disablechoicerules(Solver *solv, Rule *r)
2432 Pool *pool = solv->pool;
2436 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2437 map_init(&m, pool->nsolvables);
2438 FOR_RULELITERALS(p, pp, or)
2441 FOR_RULELITERALS(p, pp, r)
2444 for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
2446 r = solv->rules + rid;
2449 or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2450 FOR_RULELITERALS(p, pp, or)
2451 if (p > 0 && MAPTST(&m, p))
2454 solver_disablerule(solv, r);
2459 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
2463 for (i = j = 0; i < q->count; i++)
2466 for (cp2 = cp; *cp2; cp2++)
2469 q->elements[j++] = p;
2473 queue_truncate(q, j);
2477 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
2480 for (i = j = 0; i < q->count; i++)
2482 Id p = q->elements[i];
2483 if (MAPTST(&solv->dupmap, p))
2484 q->elements[j++] = p;
2486 queue_truncate(q, j);
2490 solver_addbestrules(Solver *solv, int havebestinstalljobs)
2492 Pool *pool = solv->pool;
2495 Repo *installed = solv->installed;
2501 solv->bestrules = solv->nrules;
2504 solv->bestrules_end = solv->nrules;
2511 if (havebestinstalljobs)
2513 for (i = 0; i < solv->job.count; i += 2)
2515 if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
2519 for (j = 0; j < solv->ruletojob.count; j++)
2520 if (solv->ruletojob.elements[j] == i)
2522 if (j == solv->ruletojob.count)
2524 r = solv->rules + solv->jobrules + j;
2526 FOR_RULELITERALS(p2, pp2, r)
2530 continue; /* orphaned */
2531 /* select best packages, just look at prio and version */
2533 policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2534 if (q.count == oldcnt)
2535 continue; /* nothing filtered */
2536 p2 = queue_shift(&q);
2537 solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2538 queue_push(&r2pkg, -(solv->jobrules + j));
2543 if (solv->bestupdatemap_all || solv->bestupdatemap.size)
2545 FOR_REPO_SOLVABLES(installed, p, s)
2548 if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
2550 if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
2553 if (solv->bestobeypolicy)
2554 r = solv->rules + solv->updaterules + (p - installed->start);
2557 r = solv->rules + solv->featurerules + (p - installed->start);
2558 if (!r->p) /* identical to update rule? */
2559 r = solv->rules + solv->updaterules + (p - installed->start);
2561 if (solv->multiversionupdaters && (d = solv->multiversionupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
2563 /* need to check multiversionupdaters */
2564 if (r->p == p) /* be careful with the dup case */
2566 while ((p2 = pool->whatprovidesdata[d++]) != 0)
2571 FOR_RULELITERALS(p2, pp2, r)
2575 if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2576 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
2577 if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2578 prune_to_dup_packages(solv, p, &q);
2579 /* select best packages, just look at prio and version */
2580 policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2582 continue; /* orphaned */
2583 if (solv->bestobeypolicy)
2585 /* also filter the best of the feature rule packages and add them */
2586 r = solv->rules + solv->featurerules + (p - installed->start);
2591 FOR_RULELITERALS(p2, pp2, r)
2593 queue_push(&q2, p2);
2594 if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2595 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
2596 if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2597 prune_to_dup_packages(solv, p, &q);
2598 policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
2599 for (j = 0; j < q2.count; j++)
2600 queue_pushunique(&q, q2.elements[j]);
2603 p2 = queue_shift(&q);
2604 solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2605 queue_push(&r2pkg, p);
2610 solv->bestrules_pkg = solv_calloc(r2pkg.count, sizeof(Id));
2611 memcpy(solv->bestrules_pkg, r2pkg.elements, r2pkg.count * sizeof(Id));
2613 solv->bestrules_end = solv->nrules;
2619 #undef CLEANDEPSDEBUG
2622 * This functions collects all packages that are looked at
2623 * when a dependency is checked. We need it to "pin" installed
2624 * packages when removing a supplemented package in createcleandepsmap.
2625 * Here's an not uncommon example:
2626 * A contains "Supplements: packageand(B, C)"
2627 * B contains "Requires: A"
2628 * Now if we remove C, the supplements is no longer true,
2629 * thus we also remove A. Without the dep_pkgcheck function, we
2630 * would now also remove B, but this is wrong, as adding back
2631 * C doesn't make the supplements true again. Thus we "pin" B
2633 * There's probably a better way to do this, but I haven't come
2637 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
2639 Pool *pool = solv->pool;
2644 Reldep *rd = GETRELDEP(pool, dep);
2647 if (rd->flags == REL_AND)
2649 dep_pkgcheck(solv, rd->name, m, q);
2650 dep_pkgcheck(solv, rd->evr, m, q);
2653 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2655 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2659 FOR_PROVIDES(p, pp, dep)
2660 if (!m || MAPTST(m, p))
2665 * Find all installed packages that are no longer
2666 * needed regarding the current solver job.
2669 * - remove pass: remove all packages that could have
2670 * been dragged in by the obsoleted packages.
2671 * i.e. if package A is obsolete and contains "Requires: B",
2672 * also remove B, as installing A will have pulled in B.
2673 * after this pass, we have a set of still installed packages
2674 * with broken dependencies.
2676 * now add back all packages that the still installed packages
2679 * The cleandeps packages are the packages removed in the first
2680 * pass and not added back in the second pass.
2682 * If we search for unneeded packages (unneeded is true), we
2683 * simply remove all packages except the userinstalled ones in
2687 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
2689 Pool *pool = solv->pool;
2690 Repo *installed = solv->installed;
2691 Queue *job = &solv->job;
2696 Id rid, how, what, select;
2698 Id req, *reqp, sup, *supp;
2703 map_empty(cleandepsmap);
2704 if (!installed || installed->end == installed->start)
2706 map_init(&userinstalled, installed->end - installed->start);
2707 map_init(&im, pool->nsolvables);
2708 map_init(&installedm, pool->nsolvables);
2711 for (i = 0; i < job->count; i += 2)
2713 how = job->elements[i];
2714 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2716 what = job->elements[i + 1];
2717 select = how & SOLVER_SELECTMASK;
2718 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2719 FOR_REPO_SOLVABLES(installed, p, s)
2720 MAPSET(&userinstalled, p - installed->start);
2721 FOR_JOB_SELECT(p, pp, select, what)
2722 if (pool->solvables[p].repo == installed)
2723 MAPSET(&userinstalled, p - installed->start);
2727 /* also add visible patterns to userinstalled for openSUSE */
2731 dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
2732 while (dataiterator_step(&di))
2737 s = pool->solvables + di.solvid;
2738 if (!s->repo || !s->requires)
2740 if (s->repo != installed && !pool_installable(pool, s))
2742 if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
2744 dp = s->repo->idarraydata + s->requires;
2745 for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
2746 FOR_PROVIDES(p, pp, *dp)
2747 if (pool->solvables[p].repo == installed)
2749 if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
2751 MAPSET(&userinstalled, p - installed->start);
2754 dataiterator_free(&di);
2758 /* all products and their buddies are userinstalled */
2759 for (p = installed->start; p < installed->end; p++)
2761 Solvable *s = pool->solvables + p;
2762 if (s->repo != installed)
2764 if (!strncmp("product:", pool_id2str(pool, s->name), 8))
2766 MAPSET(&userinstalled, p - installed->start);
2767 if (pool->nscallback)
2769 Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, p);
2770 if (buddy >= installed->start && buddy < installed->end && pool->solvables[buddy].repo == installed)
2771 MAPSET(&userinstalled, buddy - installed->start);
2777 /* add all positive elements (e.g. locks) to "userinstalled" */
2778 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2780 r = solv->rules + rid;
2783 i = solv->ruletojob.elements[rid - solv->jobrules];
2784 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2786 FOR_RULELITERALS(p, jp, r)
2787 if (p > 0 && pool->solvables[p].repo == installed)
2788 MAPSET(&userinstalled, p - installed->start);
2791 /* add all cleandeps candidates to iq */
2792 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2794 r = solv->rules + rid;
2797 if (r->d == 0 && r->p < 0 && r->w2 == 0)
2800 if (pool->solvables[p].repo != installed)
2802 MAPCLR(&userinstalled, p - installed->start);
2805 i = solv->ruletojob.elements[rid - solv->jobrules];
2806 how = job->elements[i];
2807 if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
2814 i = solv->ruletojob.elements[rid - solv->jobrules];
2815 if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
2817 /* check if the literals all obsolete some installed package */
2821 /* just one installed literal */
2822 if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
2825 if (solv->noobsoletes.size && !solv->keepexplicitobsoletes)
2827 FOR_RULELITERALS(p, jp, r)
2828 if (MAPTST(&solv->noobsoletes, p))
2836 FOR_RULELITERALS(p, jp, r)
2840 queue_truncate(&iq, iqstart); /* abort */
2843 if (pool->solvables[p].repo == installed)
2845 if (iq.count == iqstart)
2849 for (i = iqstart; i < iq.count; i++)
2850 if (iq.elements[i] == p)
2852 queue_truncate(&iq, iqstart);
2858 intersect_obsoletes(solv, p, &iq, iqstart, &om);
2859 if (iq.count == iqstart)
2870 if (solv->cleandeps_updatepkgs)
2871 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
2872 queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
2876 queue_empty(&iq); /* just in case... */
2878 /* clear userinstalled bit for the packages we really want to delete/update */
2879 for (i = 0; i < iq.count; i++)
2882 if (pool->solvables[p].repo != installed)
2884 MAPCLR(&userinstalled, p - installed->start);
2887 for (p = installed->start; p < installed->end; p++)
2889 if (pool->solvables[p].repo != installed)
2891 MAPSET(&installedm, p);
2892 if (unneeded && !MAPTST(&userinstalled, p - installed->start))
2897 #ifdef CLEANDEPSDEBUG
2898 printf("REMOVE PASS\n");
2907 /* supplements pass */
2908 for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2910 if (!MAPTST(&installedm, ip))
2912 s = pool->solvables + ip;
2913 if (!s->supplements)
2915 if (!MAPTST(&im, ip))
2917 if (MAPTST(&userinstalled, ip - installed->start))
2919 supp = s->repo->idarraydata + s->supplements;
2920 while ((sup = *supp++) != 0)
2921 if (dep_possible(solv, sup, &im))
2925 supp = s->repo->idarraydata + s->supplements;
2926 while ((sup = *supp++) != 0)
2927 if (dep_possible(solv, sup, &installedm))
2929 /* no longer supplemented, also erase */
2930 int iqcount = iq.count;
2931 /* pin packages, see comment above dep_pkgcheck */
2932 dep_pkgcheck(solv, sup, &im, &iq);
2933 for (i = iqcount; i < iq.count; i++)
2935 Id pqp = iq.elements[i];
2936 if (pool->solvables[pqp].repo == installed)
2937 MAPSET(&userinstalled, pqp - installed->start);
2939 queue_truncate(&iq, iqcount);
2940 #ifdef CLEANDEPSDEBUG
2941 printf("%s supplemented\n", pool_solvid2str(pool, ip));
2943 queue_push(&iq, ip);
2950 ip = queue_shift(&iq);
2951 s = pool->solvables + ip;
2952 if (!MAPTST(&im, ip))
2954 if (!MAPTST(&installedm, ip))
2956 if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
2959 #ifdef CLEANDEPSDEBUG
2960 printf("removing %s\n", pool_solvable2str(pool, s));
2964 reqp = s->repo->idarraydata + s->requires;
2965 while ((req = *reqp++) != 0)
2967 if (req == SOLVABLE_PREREQMARKER)
2970 /* count number of installed packages that match */
2972 FOR_PROVIDES(p, pp, req)
2973 if (MAPTST(&installedm, p))
2978 FOR_PROVIDES(p, pp, req)
2982 #ifdef CLEANDEPSDEBUG
2983 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2992 reqp = s->repo->idarraydata + s->recommends;
2993 while ((req = *reqp++) != 0)
2997 FOR_PROVIDES(p, pp, req)
2998 if (MAPTST(&installedm, p))
3003 FOR_PROVIDES(p, pp, req)
3007 #ifdef CLEANDEPSDEBUG
3008 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3017 /* turn userinstalled into remove set for pruning */
3018 map_empty(&userinstalled);
3019 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3021 r = solv->rules + rid;
3022 if (r->p >= 0 || r->d != 0 || r->w2 != 0)
3023 continue; /* disabled or not erase */
3026 if (pool->solvables[p].repo == installed)
3027 MAPSET(&userinstalled, p - installed->start);
3029 for (p = installed->start; p < installed->end; p++)
3032 for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3034 r = solv->rules + rid;
3037 FOR_RULELITERALS(p, jp, r)
3041 /* also put directly addressed packages on the install queue
3042 * so we can mark patterns as installed */
3043 for (i = 0; i < job->count; i += 2)
3045 how = job->elements[i];
3046 if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3048 what = job->elements[i + 1];
3049 select = how & SOLVER_SELECTMASK;
3050 if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
3051 queue_push(&iq, what);
3055 #ifdef CLEANDEPSDEBUG
3056 printf("ADDBACK PASS\n");
3062 /* supplements pass */
3063 for (ip = installed->start; ip < installed->end; ip++)
3065 if (!MAPTST(&installedm, ip))
3067 if (MAPTST(&userinstalled, ip - installed->start))
3069 s = pool->solvables + ip;
3070 if (!s->supplements)
3072 if (MAPTST(&im, ip))
3074 supp = s->repo->idarraydata + s->supplements;
3075 while ((sup = *supp++) != 0)
3076 if (dep_possible(solv, sup, &im))
3080 #ifdef CLEANDEPSDEBUG
3081 printf("%s supplemented\n", pool_solvid2str(pool, ip));
3084 queue_push(&iq, ip);
3090 ip = queue_shift(&iq);
3091 s = pool->solvables + ip;
3092 #ifdef CLEANDEPSDEBUG
3093 printf("adding back %s\n", pool_solvable2str(pool, s));
3097 reqp = s->repo->idarraydata + s->requires;
3098 while ((req = *reqp++) != 0)
3100 FOR_PROVIDES(p, pp, req)
3102 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3106 if (MAPTST(&userinstalled, p - installed->start))
3108 #ifdef CLEANDEPSDEBUG
3109 printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3119 reqp = s->repo->idarraydata + s->recommends;
3120 while ((req = *reqp++) != 0)
3122 FOR_PROVIDES(p, pp, req)
3124 if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3128 if (MAPTST(&userinstalled, p - installed->start))
3130 #ifdef CLEANDEPSDEBUG
3131 printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3142 if (solv->cleandeps_updatepkgs)
3143 for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3144 MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
3145 if (solv->cleandeps_mistakes)
3146 for (i = 0; i < solv->cleandeps_mistakes->count; i++)
3147 MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
3148 for (p = installed->start; p < installed->end; p++)
3150 if (pool->solvables[p].repo != installed)
3152 if (!MAPTST(&im, p))
3153 MAPSET(cleandepsmap, p - installed->start);
3156 map_free(&installedm);
3157 map_free(&userinstalled);
3169 /* Tarjan's SCC algorithm, slightly modifed */
3171 trj_visit(struct trj_data *trj, Id node)
3174 Queue *edges = trj->edges;
3175 Id nnode, myidx, stackstart;
3178 low[node] = myidx = trj->idx++;
3179 low[(stackstart = trj->nstack++)] = node;
3180 for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
3185 if (!edges->elements[edges->elements[nnode]])
3191 trj_visit(trj, nnode);
3196 if (l < trj->firstidx)
3199 for (k = l; low[low[k]] == l; k++)
3202 else if (l < low[node])
3205 if (low[node] == myidx)
3207 if (myidx != trj->firstidx)
3209 for (i = stackstart; i < trj->nstack; i++)
3210 low[low[i]] = myidx;
3211 trj->nstack = stackstart;
3217 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
3219 Repo *installed = solv->installed;
3223 queue_empty(unneededq);
3224 if (!installed || installed->end == installed->start)
3227 map_init(&cleandepsmap, installed->end - installed->start);
3228 solver_createcleandepsmap(solv, &cleandepsmap, 1);
3229 for (i = installed->start; i < installed->end; i++)
3230 if (MAPTST(&cleandepsmap, i - installed->start))
3231 queue_push(unneededq, i);
3233 if (filtered && unneededq->count > 1)
3235 Pool *pool = solv->pool;
3239 int j, pass, count = unneededq->count;
3242 map_init(&installedm, pool->nsolvables);
3243 for (i = installed->start; i < installed->end; i++)
3244 if (pool->solvables[i].repo == installed)
3245 MAPSET(&installedm, i);
3247 nrequires = solv_calloc(count, sizeof(Id));
3249 queue_prealloc(&edges, count * 4 + 10); /* pre-size */
3252 * Go through the solvables in the nodes queue and create edges for
3253 * all requires/recommends/supplements between the nodes.
3254 * The edges are stored in the edges queue, we add 1 to the node
3255 * index so that nodes in the edges queue are != 0 and we can
3256 * terminate the edge list with 0.
3257 * Thus for node element 5, the edges are stored starting at
3258 * edges.elements[6] and are 0-terminated.
3260 /* leave first element zero to make things easier */
3261 /* also add trailing zero */
3262 queue_insertn(&edges, 0, 1 + count + 1);
3264 /* first requires and recommends */
3265 for (i = 0; i < count; i++)
3267 Solvable *s = pool->solvables + unneededq->elements[i];
3268 edges.elements[i + 1] = edges.count;
3269 for (pass = 0; pass < 2; pass++)
3272 unsigned int off = pass == 0 ? s->requires : s->recommends;
3275 for (dp = s->repo->idarraydata + off; *dp; dp++)
3276 FOR_PROVIDES(p, pp, *dp)
3278 Solvable *sp = pool->solvables + p;
3279 if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3281 for (j = 0; j < count; j++)
3282 if (p == unneededq->elements[j])
3286 if (num && edges.elements[edges.count - 1] == j + 1)
3288 queue_push(&edges, j + 1);
3294 queue_push(&edges, 0);
3297 printf("requires + recommends\n");
3298 for (i = 0; i < count; i++)
3301 printf(" %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3302 for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3303 printf(" - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3307 /* then add supplements */
3308 for (i = 0; i < count; i++)
3310 Solvable *s = pool->solvables + unneededq->elements[i];
3315 for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
3316 if (dep_possible(solv, *dp, &installedm))
3320 queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
3321 dep_pkgcheck(solv, *dp, 0, &iq);
3322 for (k = 0; k < iq.count; k++)
3324 Id p = iq.elements[k];
3325 Solvable *sp = pool->solvables + p;
3326 if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3328 for (j = 0; j < count; j++)
3329 if (p == unneededq->elements[j])
3331 /* now add edge from j + 1 to i + 1 */
3332 queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
3333 /* addapt following edge pointers */
3334 for (k = j + 2; k < count + 2; k++)
3335 edges.elements[k]++;
3343 printf("+ supplements\n");
3344 for (i = 0; i < count; i++)
3347 printf(" %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3348 for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3349 printf(" - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3352 map_free(&installedm);
3354 /* now run SCC algo two times, first with requires+recommends+supplements,
3355 * then again without the requires. We run it the second time to get rid
3356 * of packages that got dragged in via recommends/supplements */
3358 * low will contain the result of the SCC search.
3359 * it must be of at least size 2 * (count + 1) and
3360 * must be zero initialized.
3362 * 0 low low ... low stack stack ...stack 0
3365 low = solv_calloc(count + 1, 2 * sizeof(Id));
3366 for (pass = 0; pass < 2; pass++)
3368 struct trj_data trj;
3371 memset(low, 0, (count + 1) * (2 * sizeof(Id)));
3372 for (i = 0; i < count; i++)
3374 edges.elements[i + 1] += nrequires[i];
3375 if (!unneededq->elements[i])
3376 low[i + 1] = -1; /* ignore this node */
3381 trj.idx = count + 1; /* stack starts here */
3382 for (i = 1; i <= count; i++)
3386 if (edges.elements[edges.elements[i]])
3388 trj.firstidx = trj.nstack = trj.idx;
3393 Id myidx = trj.idx++;
3398 /* prune packages */
3399 for (i = 0; i < count; i++)
3400 if (low[i + 1] <= 0)
3401 unneededq->elements[i] = 0;
3404 solv_free(nrequires);
3407 /* finally remove all pruned entries from unneededq */
3408 for (i = j = 0; i < count; i++)
3409 if (unneededq->elements[i])
3410 unneededq->elements[j++] = unneededq->elements[i];
3411 queue_truncate(unneededq, j);
3413 map_free(&cleandepsmap);