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 (solv && solv->bestSolvableCb)
605 { /* The application is responsible for */
606 return solv->bestSolvableCb(solv->pool, plist);
609 if (plist->count > 1)
610 prune_to_best_arch(pool, plist);
611 if (plist->count > 1)
612 prune_to_best_version(pool, plist);
615 /* installed packages involed in a dup operation can only be kept
616 * if they are identical to a non-installed one */
618 prune_installed_dup_packages(Solver *solv, Queue *plist)
620 Pool *pool = solv->pool;
623 for (i = j = 0; i < plist->count; i++)
625 Id p = plist->elements[i];
626 Solvable *s = pool->solvables + p;
627 if (s->repo == pool->installed && (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
629 for (k = 0; k < plist->count; k++)
631 Solvable *s2 = pool->solvables + plist->elements[k];
632 if (s2->repo != pool->installed && solvable_identical(s, s2))
635 if (k == plist->count)
636 continue; /* no identical package found, ignore installed package */
638 plist->elements[j++] = p;
645 * POLICY_MODE_CHOOSE: default, do all pruning steps
646 * POLICY_MODE_RECOMMEND: leave out prune_to_recommended
647 * POLICY_MODE_SUGGEST: leave out prune_to_recommended, do prio pruning just per name
650 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
652 Pool *pool = solv->pool;
653 if (plist->count > 1)
655 if (mode != POLICY_MODE_SUGGEST)
656 prune_to_highest_prio(pool, plist);
658 prune_to_highest_prio_per_name(pool, plist);
659 /* installed dup packages need special treatment as prio pruning
660 * does not prune installed packages */
661 if (plist->count > 1 && pool->installed && (solv->dupmap_all || solv->dupinvolvedmap.size))
662 prune_installed_dup_packages(solv, plist);
664 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
665 prune_to_recommended(solv, plist);
666 prune_best_arch_name_version(solv, pool, plist);
670 /* check if there is an illegal architecture change if
671 * installed solvable s1 is replaced by s2 */
673 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
675 Pool *pool = solv->pool;
676 Id a1 = s1->arch, a2 = s2->arch;
678 if (solv && solv->archCheckCb)
679 { /* The application is responsible for */
680 return solv->archCheckCb(solv->pool, s1, s2);
683 /* we allow changes to/from noarch */
684 if (a1 == a2 || a1 == pool->noarchid || a2 == pool->noarchid)
688 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
689 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
690 if (((a1 ^ a2) & 0xffff0000) != 0)
695 /* check if there is an illegal vendor change if
696 * installed solvable s1 is replaced by s2 */
698 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
700 Pool *pool = solv->pool;
702 Id vendormask1, vendormask2;
704 if (solv->vendorCheckCb)
705 { /* The application is responsible for */
706 return solv->vendorCheckCb(pool, s1, s2);
708 /* treat a missing vendor as empty string */
709 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
710 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
713 vendormask1 = pool_vendor2mask(pool, v1);
715 return 1; /* can't match */
716 vendormask2 = pool_vendor2mask(pool, v2);
717 if ((vendormask1 & vendormask2) != 0)
719 return 1; /* no class matches */
722 /* check if it is illegal to replace installed
723 * package "is" with package "s" (which must obsolete "is")
726 policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
728 Pool *pool = solv->pool;
730 int duppkg = solv->dupmap_all ? 1 : 0;
731 if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !(duppkg ? solv->dup_allowdowngrade : solv->allowdowngrade))
733 if (is->name == s->name && pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
734 ret |= POLICY_ILLEGAL_DOWNGRADE;
736 if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !(duppkg ? solv->dup_allowarchchange : !solv->allowarchchange))
738 if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
739 ret |= POLICY_ILLEGAL_ARCHCHANGE;
741 if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !(duppkg ? solv->dup_allowvendorchange : !solv->allowvendorchange))
743 if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
744 ret |= POLICY_ILLEGAL_VENDORCHANGE;
746 if (!(ignore & POLICY_ILLEGAL_NAMECHANGE) && !(duppkg ? solv->dup_allownamechange : !solv->allownamechange))
748 if (is->name != s->name)
749 ret |= POLICY_ILLEGAL_NAMECHANGE;
754 /*-------------------------------------------------------------------
756 * create reverse obsoletes map for installed solvables
758 * For each installed solvable find which packages with *different* names
759 * obsolete the solvable.
760 * This index is used in policy_findupdatepackages() below.
763 policy_create_obsolete_index(Solver *solv)
765 Pool *pool = solv->pool;
767 Repo *installed = solv->installed;
768 Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
771 if (!installed || installed->start == installed->end)
773 cnt = installed->end - installed->start;
774 solv->obsoletes = obsoletes = solv_calloc(cnt, sizeof(Id));
775 for (i = 1; i < pool->nsolvables; i++)
777 s = pool->solvables + i;
780 if (!pool_installable(pool, s))
782 obsp = s->repo->idarraydata + s->obsoletes;
783 while ((obs = *obsp++) != 0)
785 FOR_PROVIDES(p, pp, obs)
787 Solvable *ps = pool->solvables + p;;
788 if (ps->repo != installed)
790 if (ps->name == s->name)
792 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
794 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
796 obsoletes[p - installed->start]++;
801 for (i = 0; i < cnt; i++)
804 n += obsoletes[i] + 1;
807 solv->obsoletes_data = obsoletes_data = solv_calloc(n + 1, sizeof(Id));
808 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
809 for (i = pool->nsolvables - 1; i > 0; i--)
811 s = pool->solvables + i;
814 if (!pool_installable(pool, s))
816 obsp = s->repo->idarraydata + s->obsoletes;
817 while ((obs = *obsp++) != 0)
819 FOR_PROVIDES(p, pp, obs)
821 Solvable *ps = pool->solvables + p;;
822 if (ps->repo != installed)
824 if (ps->name == s->name)
826 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
828 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
830 if (obsoletes_data[obsoletes[p - installed->start]] != i)
831 obsoletes_data[--obsoletes[p - installed->start]] = i;
839 * find update candidates
841 * s: installed solvable to be updated
842 * qs: [out] queue to hold Ids of candidates
843 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
848 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
850 /* installed packages get a special upgrade allowed rule */
851 Pool *pool = solv->pool;
852 Id p, pp, n, p2, pp2;
856 int allowdowngrade = allow_all ? 1 : solv->allowdowngrade;
857 int allownamechange = allow_all ? 1 : solv->allownamechange;
858 int allowarchchange = allow_all ? 1 : solv->allowarchchange;
859 int allowvendorchange = allow_all ? 1 : solv->allowvendorchange;
862 allowdowngrade = solv->dup_allowdowngrade;
863 allownamechange = solv->dup_allownamechange;
864 allowarchchange = solv->dup_allowarchchange;
865 allowvendorchange = solv->dup_allowvendorchange;
870 if (solv && solv->updateCandidateCb)
871 { /* The application is responsible for */
872 return solv->updateCandidateCb(solv->pool, s, qs);
875 n = s - pool->solvables;
878 * look for updates for s
880 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
882 if (p == n) /* skip itself */
885 ps = pool->solvables + p;
886 if (s->name == ps->name) /* name match */
888 if (!allowdowngrade && pool_evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
891 else if (!allownamechange)
893 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
895 obsp = ps->repo->idarraydata + ps->obsoletes;
896 while ((obs = *obsp++) != 0) /* for all obsoletes */
898 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
900 Solvable *ps2 = pool->solvables + p2;
901 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
903 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps2))
905 if (p2 == n) /* match ! */
911 if (!obs) /* continue if no match */
913 /* here we have 'p' with a matching provides/obsoletes combination
914 * thus flagging p as a valid update candidate for s
920 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
922 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
926 if (!allownamechange)
928 /* if we have found some valid candidates and noupdateprovide is not set, we're
929 done. otherwise we fallback to all obsoletes */
930 if (!solv->noupdateprovide && haveprovobs)
932 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
935 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
937 ps = pool->solvables + p;
938 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
940 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))