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 if (sa->arch != sb->arch)
57 aa = (sa->arch <= pool->lastarch) ? pool->id2arch[sa->arch] : 0;
58 ab = (sb->arch <= pool->lastarch) ? pool->id2arch[sb->arch] : 0;
59 if (aa != ab && aa > 1 && ab > 1)
60 return aa - ab; /* lowest score first */
63 /* the same name, bring installed solvables to the front */
66 if (sa->repo == pool->installed)
68 if (sb->repo != pool->installed)
71 else if (sb->repo == pool->installed)
74 /* sort by repository sub-prio (installed repo handled above) */
75 r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
78 /* no idea about the order, sort by id */
84 * prune to repository with highest priority.
85 * does not prune installed solvables.
89 prune_to_highest_prio(Pool *pool, Queue *plist)
93 int bestprio = 0, bestprioset = 0;
95 /* prune to highest priority */
96 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
98 s = pool->solvables + plist->elements[i];
99 if (pool->installed && s->repo == pool->installed)
101 if (!bestprioset || s->repo->priority > bestprio)
103 bestprio = s->repo->priority;
109 for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
111 s = pool->solvables + plist->elements[i];
112 if (s->repo->priority == bestprio || (pool->installed && s->repo == pool->installed))
113 plist->elements[j++] = plist->elements[i];
119 /* installed packages involed in a dup operation can only be kept
120 * if they are identical to a non-installed one */
122 solver_prune_installed_dup_packages(Solver *solv, Queue *plist)
124 Pool *pool = solv->pool;
125 int i, j, bestprio = 0;
127 /* find bestprio (again) */
128 for (i = 0; i < plist->count; i++)
130 Solvable *s = pool->solvables + plist->elements[i];
131 if (s->repo != pool->installed)
133 bestprio = s->repo->priority;
137 if (i == plist->count)
138 return; /* only installed packages, could not find prio */
139 for (i = j = 0; i < plist->count; i++)
141 Id p = plist->elements[i];
142 Solvable *s = pool->solvables + p;
143 if (s->repo != pool->installed && s->repo->priority < bestprio)
145 if (s->repo == pool->installed && (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
149 FOR_PROVIDES(p2, pp2, s->name)
151 Solvable *s2 = pool->solvables + p2;
152 if (s2->repo == pool->installed || s2->evr != s->evr || s2->repo->priority < bestprio)
154 if (!solvable_identical(s, s2))
157 if (s2->repo->priority > bestprio)
160 bestprio = s2->repo->priority;
165 continue; /* no identical package found, ignore installed package */
167 plist->elements[j++] = p;
174 * like prune_to_highest_prio, but calls solver prune_installed_dup_packages
175 * when there are dup packages
178 solver_prune_to_highest_prio(Solver *solv, Queue *plist)
180 prune_to_highest_prio(solv->pool, plist);
181 if (plist->count > 1 && solv->pool->installed && (solv->dupmap_all || solv->dupinvolvedmap.size))
182 solver_prune_installed_dup_packages(solv, plist);
187 solver_prune_to_highest_prio_per_name(Solver *solv, Queue *plist)
189 Pool *pool = solv->pool;
195 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
196 queue_push(&pq, plist->elements[0]);
197 name = pool->solvables[pq.elements[0]].name;
198 for (i = 1, j = 0; i < plist->count; i++)
200 if (pool->solvables[plist->elements[i]].name != name)
203 solver_prune_to_highest_prio(solv, &pq);
204 for (k = 0; k < pq.count; k++)
205 plist->elements[j++] = pq.elements[k];
207 queue_push(&pq, plist->elements[i]);
208 name = pool->solvables[pq.elements[0]].name;
212 solver_prune_to_highest_prio(solv, &pq);
213 for (k = 0; k < pq.count; k++)
214 plist->elements[j++] = pq.elements[k];
221 * prune to recommended/suggested packages.
222 * does not prune installed packages (they are also somewhat recommended).
226 prune_to_recommended(Solver *solv, Queue *plist)
228 Pool *pool = solv->pool;
231 Id p, pp, rec, *recp, sug, *sugp;
236 for (i = 0; i < plist->count; i++)
238 p = plist->elements[i];
239 s = pool->solvables + p;
240 if (pool->installed && s->repo == pool->installed)
244 if (plist->count - ninst < 2)
247 /* update our recommendsmap/suggestsmap */
248 if (solv->recommends_index < 0)
250 MAPZERO(&solv->recommendsmap);
251 MAPZERO(&solv->suggestsmap);
252 solv->recommends_index = 0;
254 while (solv->recommends_index < solv->decisionq.count)
256 p = solv->decisionq.elements[solv->recommends_index++];
259 s = pool->solvables + p;
262 recp = s->repo->idarraydata + s->recommends;
263 while ((rec = *recp++) != 0)
264 FOR_PROVIDES(p, pp, rec)
265 MAPSET(&solv->recommendsmap, p);
269 sugp = s->repo->idarraydata + s->suggests;
270 while ((sug = *sugp++) != 0)
271 FOR_PROVIDES(p, pp, sug)
272 MAPSET(&solv->suggestsmap, p);
276 /* prune to recommended/supplemented */
278 for (i = j = 0; i < plist->count; i++)
280 p = plist->elements[i];
281 s = pool->solvables + p;
282 if (pool->installed && s->repo == pool->installed)
286 plist->elements[j++] = p;
289 if (!MAPTST(&solv->recommendsmap, p))
290 if (!solver_is_supplementing(solv, s))
294 for (k = 0; j < ninst; k++)
296 s = pool->solvables + plist->elements[k];
297 if (pool->installed && s->repo == pool->installed)
298 plist->elements[j++] = plist->elements[k];
301 plist->elements[j++] = p;
306 /* anything left to prune? */
307 if (plist->count - ninst < 2)
310 /* prune to suggested/enhanced */
312 for (i = j = 0; i < plist->count; i++)
314 p = plist->elements[i];
315 s = pool->solvables + p;
316 if (pool->installed && s->repo == pool->installed)
320 plist->elements[j++] = p;
323 if (!MAPTST(&solv->suggestsmap, p))
324 if (!solver_is_enhancing(solv, s))
328 for (k = 0; j < ninst; k++)
330 s = pool->solvables + plist->elements[k];
331 if (pool->installed && s->repo == pool->installed)
332 plist->elements[j++] = plist->elements[k];
335 plist->elements[j++] = p;
342 prune_to_best_arch(const Pool *pool, Queue *plist)
348 if (!pool->id2arch || plist->count < 2)
351 for (i = 0; i < plist->count; i++)
353 s = pool->solvables + plist->elements[i];
355 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
356 if (a && a != 1 && (!bestscore || a < bestscore))
361 for (i = j = 0; i < plist->count; i++)
363 s = pool->solvables + plist->elements[i];
365 if (a > pool->lastarch)
367 a = pool->id2arch[a];
368 /* a == 1 -> noarch */
369 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
371 plist->elements[j++] = plist->elements[i];
388 /* This is Tarjan's SCC algorithm, slightly modified */
390 trj_visit(struct trj_data *trj, Id node)
393 Pool *pool = trj->pool;
394 Queue *plist = trj->plist;
395 Id myidx, stackstart;
398 Id p, pp, obs, *obsp;
400 low[node] = myidx = trj->idx++;
401 trj->stack[(stackstart = trj->nstack++)] = node;
403 s = pool->solvables + plist->elements[node];
406 obsp = s->repo->idarraydata + s->obsoletes;
407 while ((obs = *obsp++) != 0)
409 FOR_PROVIDES(p, pp, obs)
411 Solvable *ps = pool->solvables + p;
412 if (ps->name == s->name)
414 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
416 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
418 /* hmm, expensive. should use hash if plist is big */
419 for (i = 0; i < plist->count; i++)
421 if (node != i && plist->elements[i] == p)
438 if (l < trj->firstidx)
441 /* this means we have reached an old SCC found earlier.
442 * delete it as we obsolete it */
445 if (low[trj->stack[k]] == l)
446 low[trj->stack[k]] = -1;
451 else if (l < low[node])
458 if (low[node] == myidx) /* found a SCC? */
460 /* we're only interested in SCCs that contain the first node,
461 * as all others are "obsoleted" */
462 if (myidx != trj->firstidx)
464 for (i = stackstart; i < trj->nstack; i++)
465 low[trj->stack[i]] = myidx;
466 trj->nstack = stackstart; /* empty stack */
471 * remove entries from plist that are obsoleted by other entries
472 * with different name.
475 prune_obsoleted(Pool *pool, Queue *plist)
477 Id data_buf[2 * 16], *data;
482 if (plist->count <= 16)
484 memset(data_buf, 0, sizeof(data_buf));
488 data = solv_calloc(plist->count, 2 * sizeof(Id));
493 trj.stack = data + plist->count - 1; /* -1 so we can index with idx (which starts with 1) */
494 for (i = 0; i < plist->count; i++)
498 s = pool->solvables + plist->elements[i];
501 trj.firstidx = trj.nstack = trj.idx;
506 Id myidx = trj.idx++;
508 trj.stack[myidx] = i;
511 for (i = j = 0; i < plist->count; i++)
513 plist->elements[j++] = plist->elements[i];
515 if (data != data_buf)
519 /* this is prune_obsoleted special-cased for two elements */
521 prune_obsoleted_2(Pool *pool, Queue *plist)
525 Id p, pp, obs, *obsp;
529 for (i = 0; i < 2; i++)
531 s = pool->solvables + plist->elements[i];
532 other = plist->elements[1 - i];
535 obsp = s->repo->idarraydata + s->obsoletes;
536 while ((obs = *obsp++) != 0)
538 FOR_PROVIDES(p, pp, obs)
543 ps = pool->solvables + p;
544 if (ps->name == s->name)
546 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
548 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
558 if (obmap == 0 || obmap == 3)
561 plist->elements[0] = plist->elements[1];
566 * bring those elements to the front of the queue that
567 * have a installed solvable with the same name
570 move_installed_to_front(Pool *pool, Queue *plist)
576 for (i = j = 0; i < plist->count; i++)
578 s = pool->solvables + plist->elements[i];
579 if (s->repo != pool->installed)
581 FOR_PROVIDES(p, pp, s->name)
583 Solvable *ps = pool->solvables + p;
584 if (s->name == ps->name && ps->repo == pool->installed)
591 if (s->repo == pool->installed)
595 p = plist->elements[i];
597 plist->elements[i] = plist->elements[j];
599 memmove(plist->elements + j + 1, plist->elements + j, (i - j) * sizeof(Id));
600 plist->elements[j] = p;
602 else if (j + 2 == plist->count)
603 break; /* no need to check last element if all prev ones are installed */
610 * prune_to_best_version
612 * sort list of packages (given through plist) by name and evr
613 * return result through plist
616 prune_to_best_version(Pool *pool, Queue *plist)
621 if (plist->count < 2) /* no need to prune for a single entry */
623 POOL_DEBUG(SOLV_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
625 /* sort by name first, prefer installed */
626 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
628 /* now find best 'per name' */
630 for (i = j = 0; i < plist->count; i++)
632 s = pool->solvables + plist->elements[i];
634 POOL_DEBUG(SOLV_DEBUG_POLICY, "- %s[%s]\n",
635 pool_solvable2str(pool, s),
636 (pool->installed && s->repo == pool->installed) ? "installed" : "not installed");
638 if (!best) /* if no best yet, the current is best */
644 /* name switch: finish group, re-init */
645 if (best->name != s->name) /* new name */
647 plist->elements[j++] = best - pool->solvables; /* move old best to front */
648 best = s; /* take current as new best */
652 if (best->evr != s->evr) /* compare evr */
654 if (pool_evrcmp(pool, best->evr, s->evr, EVRCMP_COMPARE) < 0)
658 plist->elements[j++] = best - pool->solvables; /* finish last group */
661 /* we reduced the list to one package per name, now look at
662 * package obsoletes */
663 if (plist->count > 1)
665 if (plist->count == 2)
666 prune_obsoleted_2(pool, plist);
668 prune_obsoleted(pool, plist);
670 if (plist->count > 1 && pool->installed)
671 move_installed_to_front(pool, plist);
676 * POLICY_MODE_CHOOSE: default, do all pruning steps
677 * POLICY_MODE_RECOMMEND: leave out prune_to_recommended
678 * POLICY_MODE_SUGGEST: leave out prune_to_recommended, do prio pruning just per name
681 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
683 Pool *pool = solv->pool;
684 if (plist->count > 1)
686 if (mode != POLICY_MODE_SUGGEST)
687 solver_prune_to_highest_prio(solv, plist);
689 solver_prune_to_highest_prio_per_name(solv, plist);
691 if (plist->count > 1)
692 prune_to_best_arch(pool, plist);
693 if (plist->count > 1)
694 prune_to_best_version(pool, plist);
695 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
696 prune_to_recommended(solv, plist);
700 /* check if there is an illegal architecture change if
701 * installed solvable s1 is replaced by s2 */
703 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
705 Pool *pool = solv->pool;
706 Id a1 = s1->arch, a2 = s2->arch;
708 /* we allow changes to/from noarch */
709 if (a1 == a2 || a1 == pool->noarchid || a2 == pool->noarchid)
713 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
714 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
715 if (((a1 ^ a2) & 0xffff0000) != 0)
720 /* check if there is an illegal vendor change if
721 * installed solvable s1 is replaced by s2 */
723 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
725 Pool *pool = solv->pool;
727 Id vendormask1, vendormask2;
729 if (pool->custom_vendorcheck)
730 return pool->custom_vendorcheck(pool, s1, s2);
732 /* treat a missing vendor as empty string */
733 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
734 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
737 vendormask1 = pool_vendor2mask(pool, v1);
739 return 1; /* can't match */
740 vendormask2 = pool_vendor2mask(pool, v2);
741 if ((vendormask1 & vendormask2) != 0)
743 return 1; /* no class matches */
746 /* check if it is illegal to replace installed
747 * package "is" with package "s" (which must obsolete "is")
750 policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
752 Pool *pool = solv->pool;
754 int duppkg = solv->dupmap_all ? 1 : 0;
755 if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !(duppkg ? solv->dup_allowdowngrade : solv->allowdowngrade))
757 if (is->name == s->name && pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
758 ret |= POLICY_ILLEGAL_DOWNGRADE;
760 if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !(duppkg ? solv->dup_allowarchchange : solv->allowarchchange))
762 if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
763 ret |= POLICY_ILLEGAL_ARCHCHANGE;
765 if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !(duppkg ? solv->dup_allowvendorchange : solv->allowvendorchange))
767 if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
768 ret |= POLICY_ILLEGAL_VENDORCHANGE;
770 if (!(ignore & POLICY_ILLEGAL_NAMECHANGE) && !(duppkg ? solv->dup_allownamechange : solv->allownamechange))
772 if (is->name != s->name)
773 ret |= POLICY_ILLEGAL_NAMECHANGE;
778 /*-------------------------------------------------------------------
780 * create reverse obsoletes map for installed solvables
782 * For each installed solvable find which packages with *different* names
783 * obsolete the solvable.
784 * This index is used in policy_findupdatepackages() below.
787 policy_create_obsolete_index(Solver *solv)
789 Pool *pool = solv->pool;
791 Repo *installed = solv->installed;
792 Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
795 solv->obsoletes = solv_free(solv->obsoletes);
796 solv->obsoletes_data = solv_free(solv->obsoletes_data);
797 if (!installed || installed->start == installed->end)
799 cnt = installed->end - installed->start;
800 solv->obsoletes = obsoletes = solv_calloc(cnt, sizeof(Id));
801 for (i = 1; i < pool->nsolvables; i++)
803 s = pool->solvables + i;
806 if (!pool_installable(pool, s))
808 obsp = s->repo->idarraydata + s->obsoletes;
809 while ((obs = *obsp++) != 0)
811 FOR_PROVIDES(p, pp, obs)
813 Solvable *ps = pool->solvables + p;;
814 if (ps->repo != installed)
816 if (ps->name == s->name)
818 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
820 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
822 obsoletes[p - installed->start]++;
827 for (i = 0; i < cnt; i++)
830 n += obsoletes[i] + 1;
833 solv->obsoletes_data = obsoletes_data = solv_calloc(n + 1, sizeof(Id));
834 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
835 for (i = pool->nsolvables - 1; i > 0; i--)
837 s = pool->solvables + i;
840 if (!pool_installable(pool, s))
842 obsp = s->repo->idarraydata + s->obsoletes;
843 while ((obs = *obsp++) != 0)
845 FOR_PROVIDES(p, pp, obs)
847 Solvable *ps = pool->solvables + p;;
848 if (ps->repo != installed)
850 if (ps->name == s->name)
852 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
854 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
856 if (obsoletes_data[obsoletes[p - installed->start]] != i)
857 obsoletes_data[--obsoletes[p - installed->start]] = i;
865 * find update candidates
867 * s: installed solvable to be updated
868 * qs: [out] queue to hold Ids of candidates
869 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
874 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
876 /* installed packages get a special upgrade allowed rule */
877 Pool *pool = solv->pool;
878 Id p, pp, n, p2, pp2;
882 int allowdowngrade = allow_all ? 1 : solv->allowdowngrade;
883 int allownamechange = allow_all ? 1 : solv->allownamechange;
884 int allowarchchange = allow_all ? 1 : solv->allowarchchange;
885 int allowvendorchange = allow_all ? 1 : solv->allowvendorchange;
888 allowdowngrade = solv->dup_allowdowngrade;
889 allownamechange = solv->dup_allownamechange;
890 allowarchchange = solv->dup_allowarchchange;
891 allowvendorchange = solv->dup_allowvendorchange;
896 n = s - pool->solvables;
899 * look for updates for s
901 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
903 if (p == n) /* skip itself */
906 ps = pool->solvables + p;
907 if (s->name == ps->name) /* name match */
909 /* XXX: check implicitobsoleteusescolors? */
910 if (!allowdowngrade && pool_evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
913 else if (!allownamechange)
915 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
917 obsp = ps->repo->idarraydata + ps->obsoletes;
918 while ((obs = *obsp++) != 0) /* for all obsoletes */
920 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
922 Solvable *ps2 = pool->solvables + p2;
923 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
925 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps2))
927 if (p2 == n) /* match ! */
933 if (!obs) /* continue if no match */
935 /* here we have 'p' with a matching provides/obsoletes combination
936 * thus flagging p as a valid update candidate for s
942 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
944 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
948 if (!allownamechange)
950 /* if we have found some valid candidates and noupdateprovide is not set, we're
951 done. otherwise we fallback to all obsoletes */
952 if (!solv->noupdateprovide && haveprovobs)
954 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
957 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
959 ps = pool->solvables + p;
960 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
962 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
970 policy_illegal2str(Solver *solv, int illegal, Solvable *s, Solvable *rs)
972 Pool *pool = solv->pool;
974 if (illegal == POLICY_ILLEGAL_DOWNGRADE)
976 str = pool_tmpjoin(pool, "downgrade of ", pool_solvable2str(pool, s), 0);
977 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
979 if (illegal == POLICY_ILLEGAL_NAMECHANGE)
981 str = pool_tmpjoin(pool, "name change of ", pool_solvable2str(pool, s), 0);
982 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
984 if (illegal == POLICY_ILLEGAL_ARCHCHANGE)
986 str = pool_tmpjoin(pool, "architecture change of ", pool_solvable2str(pool, s), 0);
987 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
989 if (illegal == POLICY_ILLEGAL_VENDORCHANGE)
991 str = pool_tmpjoin(pool, "vendor change from '", pool_id2str(pool, s->vendor), "' (");
994 str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to '");
995 str = pool_tmpappend(pool, str, pool_id2str(pool, rs->vendor), "' (");
998 str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to no vendor (");
999 return pool_tmpappend(pool, str, pool_solvable2str(pool, rs), ")");
1001 return "unknown illegal change";