2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
9 * Generic policy interface for SAT solver
19 #include "solver_private.h"
22 #include "poolvendor.h"
26 /*-----------------------------------------------------------------*/
29 * prep for prune_best_version
34 prune_to_best_version_sortcmp(const void *ap, const void *bp, void *dp)
42 sa = pool->solvables + a;
43 sb = pool->solvables + b;
44 r = sa->name - sb->name;
48 /* different names. We use real strcmp here so that the result
49 * is not depending on some random solvable order */
50 na = pool_id2str(pool, sa->name);
51 nb = pool_id2str(pool, sb->name);
52 return strcmp(na, nb);
54 /* the same name, bring installed solvables to the front */
57 if (sa->repo == pool->installed)
59 if (sb->repo != pool->installed)
62 else if (sb->repo == pool->installed)
65 /* sort by repository sub-prio (installed repo handled above) */
66 r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
69 /* no idea about the order, sort by id */
75 * prune to repository with highest priority.
76 * does not prune installed solvables.
80 prune_to_highest_prio(Pool *pool, Queue *plist)
84 int bestprio = 0, bestprioset = 0;
86 /* prune to highest priority */
87 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
89 s = pool->solvables + plist->elements[i];
90 if (pool->installed && s->repo == pool->installed)
92 if (!bestprioset || s->repo->priority > bestprio)
94 bestprio = s->repo->priority;
100 for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
102 s = pool->solvables + plist->elements[i];
103 if (s->repo->priority == bestprio || (pool->installed && s->repo == pool->installed))
104 plist->elements[j++] = plist->elements[i];
110 prune_to_highest_prio_per_name(Pool *pool, Queue *plist)
117 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
118 queue_push(&pq, plist->elements[0]);
119 name = pool->solvables[pq.elements[0]].name;
120 for (i = 1, j = 0; i < plist->count; i++)
122 if (pool->solvables[plist->elements[i]].name != name)
125 prune_to_highest_prio(pool, &pq);
126 for (k = 0; k < pq.count; k++)
127 plist->elements[j++] = pq.elements[k];
129 queue_push(&pq, plist->elements[i]);
130 name = pool->solvables[pq.elements[0]].name;
134 prune_to_highest_prio(pool, &pq);
135 for (k = 0; k < pq.count; k++)
136 plist->elements[j++] = pq.elements[k];
143 * prune to recommended/suggested packages.
144 * does not prune installed packages (they are also somewhat recommended).
148 prune_to_recommended(Solver *solv, Queue *plist)
150 Pool *pool = solv->pool;
153 Id p, pp, rec, *recp, sug, *sugp;
158 for (i = 0; i < plist->count; i++)
160 p = plist->elements[i];
161 s = pool->solvables + p;
162 if (pool->installed && s->repo == pool->installed)
166 if (plist->count - ninst < 2)
169 /* update our recommendsmap/suggestsmap */
170 if (solv->recommends_index < 0)
172 MAPZERO(&solv->recommendsmap);
173 MAPZERO(&solv->suggestsmap);
174 solv->recommends_index = 0;
176 while (solv->recommends_index < solv->decisionq.count)
178 p = solv->decisionq.elements[solv->recommends_index++];
181 s = pool->solvables + p;
184 recp = s->repo->idarraydata + s->recommends;
185 while ((rec = *recp++) != 0)
186 FOR_PROVIDES(p, pp, rec)
187 MAPSET(&solv->recommendsmap, p);
191 sugp = s->repo->idarraydata + s->suggests;
192 while ((sug = *sugp++) != 0)
193 FOR_PROVIDES(p, pp, sug)
194 MAPSET(&solv->suggestsmap, p);
198 /* prune to recommended/supplemented */
200 for (i = j = 0; i < plist->count; i++)
202 p = plist->elements[i];
203 s = pool->solvables + p;
204 if (pool->installed && s->repo == pool->installed)
208 plist->elements[j++] = p;
211 if (!MAPTST(&solv->recommendsmap, p))
212 if (!solver_is_supplementing(solv, s))
216 for (k = 0; j < ninst; k++)
218 s = pool->solvables + plist->elements[k];
219 if (pool->installed && s->repo == pool->installed)
220 plist->elements[j++] = plist->elements[k];
223 plist->elements[j++] = p;
228 /* anything left to prune? */
229 if (plist->count - ninst < 2)
232 /* prune to suggested/enhanced */
234 for (i = j = 0; i < plist->count; i++)
236 p = plist->elements[i];
237 s = pool->solvables + p;
238 if (pool->installed && s->repo == pool->installed)
242 plist->elements[j++] = p;
245 if (!MAPTST(&solv->suggestsmap, p))
246 if (!solver_is_enhancing(solv, s))
250 for (k = 0; j < ninst; k++)
252 s = pool->solvables + plist->elements[k];
253 if (pool->installed && s->repo == pool->installed)
254 plist->elements[j++] = plist->elements[k];
257 plist->elements[j++] = p;
264 prune_to_best_arch(const Pool *pool, Queue *plist)
270 if (!pool->id2arch || plist->count < 2)
273 for (i = 0; i < plist->count; i++)
275 s = pool->solvables + plist->elements[i];
277 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
278 if (a && a != 1 && (!bestscore || a < bestscore))
283 for (i = j = 0; i < plist->count; i++)
285 s = pool->solvables + plist->elements[i];
287 if (a > pool->lastarch)
289 a = pool->id2arch[a];
290 /* a == 1 -> noarch */
291 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
293 plist->elements[j++] = plist->elements[i];
310 /* This is Tarjan's SCC algorithm, slightly modified */
312 trj_visit(struct trj_data *trj, Id node)
315 Pool *pool = trj->pool;
316 Queue *plist = trj->plist;
317 Id myidx, stackstart;
320 Id p, pp, obs, *obsp;
322 low[node] = myidx = trj->idx++;
323 trj->stack[(stackstart = trj->nstack++)] = node;
325 s = pool->solvables + plist->elements[node];
328 obsp = s->repo->idarraydata + s->obsoletes;
329 while ((obs = *obsp++) != 0)
331 FOR_PROVIDES(p, pp, obs)
333 Solvable *ps = pool->solvables + p;
334 if (ps->name == s->name)
336 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
338 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
340 /* hmm, expensive. should use hash if plist is big */
341 for (i = 0; i < plist->count; i++)
343 if (node != i && plist->elements[i] == p)
360 if (l < trj->firstidx)
363 /* this means we have reached an old SCC found earlier.
364 * delete it as we obsolete it */
367 if (low[trj->stack[k]] == l)
368 low[trj->stack[k]] = -1;
373 else if (l < low[node])
380 if (low[node] == myidx) /* found a SCC? */
382 /* we're only interested in SCCs that contain the first node,
383 * as all others are "obsoleted" */
384 if (myidx != trj->firstidx)
386 for (i = stackstart; i < trj->nstack; i++)
387 low[trj->stack[i]] = myidx;
388 trj->nstack = stackstart; /* empty stack */
393 * remove entries from plist that are obsoleted by other entries
394 * with different name.
397 prune_obsoleted(Pool *pool, Queue *plist)
399 Id data_buf[2 * 16], *data;
404 if (plist->count <= 16)
406 memset(data_buf, 0, sizeof(data_buf));
410 data = solv_calloc(plist->count, 2 * sizeof(Id));
415 trj.stack = data + plist->count - 1; /* -1 so we can index with idx (which starts with 1) */
416 for (i = 0; i < plist->count; i++)
420 s = pool->solvables + plist->elements[i];
423 trj.firstidx = trj.nstack = trj.idx;
428 Id myidx = trj.idx++;
430 trj.stack[myidx] = i;
433 for (i = j = 0; i < plist->count; i++)
435 plist->elements[j++] = plist->elements[i];
437 if (data != data_buf)
441 /* this is prune_obsoleted special-cased for two elements */
443 prune_obsoleted_2(Pool *pool, Queue *plist)
447 Id p, pp, obs, *obsp;
451 for (i = 0; i < 2; i++)
453 s = pool->solvables + plist->elements[i];
454 other = plist->elements[1 - i];
457 obsp = s->repo->idarraydata + s->obsoletes;
458 while ((obs = *obsp++) != 0)
460 FOR_PROVIDES(p, pp, obs)
465 ps = pool->solvables + p;
466 if (ps->name == s->name)
468 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
470 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
480 if (obmap == 0 || obmap == 3)
483 plist->elements[0] = plist->elements[1];
488 * bring those elements to the front of the queue that
489 * have a installed solvable with the same name
492 move_installed_to_front(Pool *pool, Queue *plist)
498 for (i = j = 0; i < plist->count; i++)
500 s = pool->solvables + plist->elements[i];
501 if (s->repo != pool->installed)
503 FOR_PROVIDES(p, pp, s->name)
505 Solvable *ps = pool->solvables + p;
506 if (s->name == ps->name && ps->repo == pool->installed)
513 if (s->repo == pool->installed)
517 p = plist->elements[i];
519 plist->elements[i] = plist->elements[j];
521 memmove(plist->elements + j + 1, plist->elements + j, (i - j) * sizeof(Id));
522 plist->elements[j] = p;
524 else if (j + 2 == plist->count)
525 break; /* no need to check last element if all prev ones are installed */
532 * prune_to_best_version
534 * sort list of packages (given through plist) by name and evr
535 * return result through plist
538 prune_to_best_version(Pool *pool, Queue *plist)
543 if (plist->count < 2) /* no need to prune for a single entry */
545 POOL_DEBUG(SOLV_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
547 /* sort by name first, prefer installed */
548 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
550 /* now find best 'per name' */
552 for (i = j = 0; i < plist->count; i++)
554 s = pool->solvables + plist->elements[i];
556 POOL_DEBUG(SOLV_DEBUG_POLICY, "- %s[%s]\n",
557 pool_solvable2str(pool, s),
558 (pool->installed && s->repo == pool->installed) ? "installed" : "not installed");
560 if (!best) /* if no best yet, the current is best */
566 /* name switch: finish group, re-init */
567 if (best->name != s->name) /* new name */
569 plist->elements[j++] = best - pool->solvables; /* move old best to front */
570 best = s; /* take current as new best */
574 if (best->evr != s->evr) /* compare evr */
576 if (pool_evrcmp(pool, best->evr, s->evr, EVRCMP_COMPARE) < 0)
580 plist->elements[j++] = best - pool->solvables; /* finish last group */
583 /* we reduced the list to one package per name, now look at
584 * package obsoletes */
585 if (plist->count > 1)
587 if (plist->count == 2)
588 prune_obsoleted_2(pool, plist);
590 prune_obsoleted(pool, plist);
592 if (plist->count > 1 && pool->installed)
593 move_installed_to_front(pool, plist);
597 /* legacy, do not use anymore!
598 * (rates arch higher than version, but thats a policy)
602 prune_best_arch_name_version(const Solver *solv, Pool *pool, Queue *plist)
604 if (plist->count > 1)
605 prune_to_best_arch(pool, plist);
606 if (plist->count > 1)
607 prune_to_best_version(pool, plist);
610 /* installed packages involed in a dup operation can only be kept
611 * if they are identical to a non-installed one */
613 prune_installed_dup_packages(Solver *solv, Queue *plist)
615 Pool *pool = solv->pool;
618 for (i = j = 0; i < plist->count; i++)
620 Id p = plist->elements[i];
621 Solvable *s = pool->solvables + p;
622 if (s->repo == pool->installed && (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
624 for (k = 0; k < plist->count; k++)
626 Solvable *s2 = pool->solvables + plist->elements[k];
627 if (s2->repo != pool->installed && solvable_identical(s, s2))
630 if (k == plist->count)
631 continue; /* no identical package found, ignore installed package */
633 plist->elements[j++] = p;
640 * POLICY_MODE_CHOOSE: default, do all pruning steps
641 * POLICY_MODE_RECOMMEND: leave out prune_to_recommended
642 * POLICY_MODE_SUGGEST: leave out prune_to_recommended, do prio pruning just per name
645 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
647 Pool *pool = solv->pool;
648 if (plist->count > 1)
650 if (mode != POLICY_MODE_SUGGEST)
651 prune_to_highest_prio(pool, plist);
653 prune_to_highest_prio_per_name(pool, plist);
654 /* installed dup packages need special treatment as prio pruning
655 * does not prune installed packages */
656 if (plist->count > 1 && pool->installed && (solv->dupmap_all || solv->dupinvolvedmap.size))
657 prune_installed_dup_packages(solv, plist);
659 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
660 prune_to_recommended(solv, plist);
661 prune_best_arch_name_version(solv, pool, plist);
665 /* check if there is an illegal architecture change if
666 * installed solvable s1 is replaced by s2 */
668 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
670 Pool *pool = solv->pool;
671 Id a1 = s1->arch, a2 = s2->arch;
673 /* we allow changes to/from noarch */
674 if (a1 == a2 || a1 == pool->noarchid || a2 == pool->noarchid)
678 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
679 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
680 if (((a1 ^ a2) & 0xffff0000) != 0)
685 /* check if there is an illegal vendor change if
686 * installed solvable s1 is replaced by s2 */
688 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
690 Pool *pool = solv->pool;
692 Id vendormask1, vendormask2;
694 if (pool->custom_vendorcheck)
695 return pool->custom_vendorcheck(pool, s1, s2);
697 /* treat a missing vendor as empty string */
698 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
699 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
702 vendormask1 = pool_vendor2mask(pool, v1);
704 return 1; /* can't match */
705 vendormask2 = pool_vendor2mask(pool, v2);
706 if ((vendormask1 & vendormask2) != 0)
708 return 1; /* no class matches */
711 /* check if it is illegal to replace installed
712 * package "is" with package "s" (which must obsolete "is")
715 policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
717 Pool *pool = solv->pool;
719 int duppkg = solv->dupmap_all ? 1 : 0;
720 if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !(duppkg ? solv->dup_allowdowngrade : solv->allowdowngrade))
722 if (is->name == s->name && pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
723 ret |= POLICY_ILLEGAL_DOWNGRADE;
725 if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !(duppkg ? solv->dup_allowarchchange : solv->allowarchchange))
727 if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
728 ret |= POLICY_ILLEGAL_ARCHCHANGE;
730 if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !(duppkg ? solv->dup_allowvendorchange : solv->allowvendorchange))
732 if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
733 ret |= POLICY_ILLEGAL_VENDORCHANGE;
735 if (!(ignore & POLICY_ILLEGAL_NAMECHANGE) && !(duppkg ? solv->dup_allownamechange : solv->allownamechange))
737 if (is->name != s->name)
738 ret |= POLICY_ILLEGAL_NAMECHANGE;
743 /*-------------------------------------------------------------------
745 * create reverse obsoletes map for installed solvables
747 * For each installed solvable find which packages with *different* names
748 * obsolete the solvable.
749 * This index is used in policy_findupdatepackages() below.
752 policy_create_obsolete_index(Solver *solv)
754 Pool *pool = solv->pool;
756 Repo *installed = solv->installed;
757 Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
760 solv->obsoletes = solv_free(solv->obsoletes);
761 solv->obsoletes_data = solv_free(solv->obsoletes_data);
762 if (!installed || installed->start == installed->end)
764 cnt = installed->end - installed->start;
765 solv->obsoletes = obsoletes = solv_calloc(cnt, sizeof(Id));
766 for (i = 1; i < pool->nsolvables; i++)
768 s = pool->solvables + i;
771 if (!pool_installable(pool, s))
773 obsp = s->repo->idarraydata + s->obsoletes;
774 while ((obs = *obsp++) != 0)
776 FOR_PROVIDES(p, pp, obs)
778 Solvable *ps = pool->solvables + p;;
779 if (ps->repo != installed)
781 if (ps->name == s->name)
783 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
785 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
787 obsoletes[p - installed->start]++;
792 for (i = 0; i < cnt; i++)
795 n += obsoletes[i] + 1;
798 solv->obsoletes_data = obsoletes_data = solv_calloc(n + 1, sizeof(Id));
799 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
800 for (i = pool->nsolvables - 1; i > 0; i--)
802 s = pool->solvables + i;
805 if (!pool_installable(pool, s))
807 obsp = s->repo->idarraydata + s->obsoletes;
808 while ((obs = *obsp++) != 0)
810 FOR_PROVIDES(p, pp, obs)
812 Solvable *ps = pool->solvables + p;;
813 if (ps->repo != installed)
815 if (ps->name == s->name)
817 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
819 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
821 if (obsoletes_data[obsoletes[p - installed->start]] != i)
822 obsoletes_data[--obsoletes[p - installed->start]] = i;
830 * find update candidates
832 * s: installed solvable to be updated
833 * qs: [out] queue to hold Ids of candidates
834 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
839 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
841 /* installed packages get a special upgrade allowed rule */
842 Pool *pool = solv->pool;
843 Id p, pp, n, p2, pp2;
847 int allowdowngrade = allow_all ? 1 : solv->allowdowngrade;
848 int allownamechange = allow_all ? 1 : solv->allownamechange;
849 int allowarchchange = allow_all ? 1 : solv->allowarchchange;
850 int allowvendorchange = allow_all ? 1 : solv->allowvendorchange;
853 allowdowngrade = solv->dup_allowdowngrade;
854 allownamechange = solv->dup_allownamechange;
855 allowarchchange = solv->dup_allowarchchange;
856 allowvendorchange = solv->dup_allowvendorchange;
861 n = s - pool->solvables;
864 * look for updates for s
866 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
868 if (p == n) /* skip itself */
871 ps = pool->solvables + p;
872 if (s->name == ps->name) /* name match */
874 if (!allowdowngrade && pool_evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
877 else if (!allownamechange)
879 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
881 obsp = ps->repo->idarraydata + ps->obsoletes;
882 while ((obs = *obsp++) != 0) /* for all obsoletes */
884 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
886 Solvable *ps2 = pool->solvables + p2;
887 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
889 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps2))
891 if (p2 == n) /* match ! */
897 if (!obs) /* continue if no match */
899 /* here we have 'p' with a matching provides/obsoletes combination
900 * thus flagging p as a valid update candidate for s
906 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
908 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
912 if (!allownamechange)
914 /* if we have found some valid candidates and noupdateprovide is not set, we're
915 done. otherwise we fallback to all obsoletes */
916 if (!solv->noupdateprovide && haveprovobs)
918 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
921 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
923 ps = pool->solvables + p;
924 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
926 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))