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"
24 #include "linkedpkg.h"
29 /*-----------------------------------------------------------------*/
32 * prep for prune_best_version
37 prune_to_best_version_sortcmp(const void *ap, const void *bp, void *dp)
45 sa = pool->solvables + a;
46 sb = pool->solvables + b;
47 r = sa->name - sb->name;
51 /* different names. We use real strcmp here so that the result
52 * is not depending on some random solvable order */
53 na = pool_id2str(pool, sa->name);
54 nb = pool_id2str(pool, sb->name);
55 return strcmp(na, nb);
57 if (sa->arch != sb->arch)
60 aa = pool_arch2score(pool, sa->arch);
61 ab = pool_arch2score(pool, sb->arch);
62 if (aa != ab && aa > 1 && ab > 1)
63 return aa < ab ? -1 : 1; /* lowest score first */
66 /* the same name, bring installed solvables to the front */
69 if (sa->repo == pool->installed)
71 if (sb->repo != pool->installed)
74 else if (sb->repo == pool->installed)
77 /* sort by repository sub-prio (installed repo handled above) */
78 r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
81 /* no idea about the order, sort by id */
87 * prune to repository with highest priority.
88 * does not prune installed solvables.
92 prune_to_highest_prio(Pool *pool, Queue *plist)
96 int bestprio = 0, bestprioset = 0;
98 /* prune to highest priority */
99 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
101 s = pool->solvables + plist->elements[i];
102 if (pool->installed && s->repo == pool->installed)
104 if (!bestprioset || s->repo->priority > bestprio)
106 bestprio = s->repo->priority;
112 for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
114 s = pool->solvables + plist->elements[i];
115 if (s->repo->priority == bestprio || (pool->installed && s->repo == pool->installed))
116 plist->elements[j++] = plist->elements[i];
122 /* installed packages involed in a dup operation can only be kept
123 * if they are identical to a non-installed one */
125 solver_prune_installed_dup_packages(Solver *solv, Queue *plist)
127 Pool *pool = solv->pool;
128 int i, j, bestprio = 0;
130 /* find bestprio (again) */
131 for (i = 0; i < plist->count; i++)
133 Solvable *s = pool->solvables + plist->elements[i];
134 if (s->repo != pool->installed)
136 bestprio = s->repo->priority;
140 if (i == plist->count)
141 return; /* only installed packages, could not find prio */
142 for (i = j = 0; i < plist->count; i++)
144 Id p = plist->elements[i];
145 Solvable *s = pool->solvables + p;
146 if (s->repo != pool->installed && s->repo->priority < bestprio)
148 if (s->repo == pool->installed && (solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
152 FOR_PROVIDES(p2, pp2, s->name)
154 Solvable *s2 = pool->solvables + p2;
155 if (s2->repo == pool->installed || s2->evr != s->evr || s2->repo->priority < bestprio)
157 if (!solvable_identical(s, s2))
160 if (s2->repo->priority > bestprio)
163 bestprio = s2->repo->priority;
168 continue; /* no identical package found, ignore installed package */
170 plist->elements[j++] = p;
177 * like prune_to_highest_prio, but calls solver prune_installed_dup_packages
178 * when there are dup packages
181 solver_prune_to_highest_prio(Solver *solv, Queue *plist)
183 prune_to_highest_prio(solv->pool, plist);
184 if (plist->count > 1 && solv->pool->installed && (solv->dupinvolvedmap_all || solv->dupinvolvedmap.size))
185 solver_prune_installed_dup_packages(solv, plist);
190 solver_prune_to_highest_prio_per_name(Solver *solv, Queue *plist)
192 Pool *pool = solv->pool;
198 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
199 queue_push(&pq, plist->elements[0]);
200 name = pool->solvables[pq.elements[0]].name;
201 for (i = 1, j = 0; i < plist->count; i++)
203 if (pool->solvables[plist->elements[i]].name != name)
205 name = pool->solvables[plist->elements[i]].name;
207 solver_prune_to_highest_prio(solv, &pq);
208 for (k = 0; k < pq.count; k++)
209 plist->elements[j++] = pq.elements[k];
212 queue_push(&pq, plist->elements[i]);
215 solver_prune_to_highest_prio(solv, &pq);
216 for (k = 0; k < pq.count; k++)
217 plist->elements[j++] = pq.elements[k];
223 #ifdef ENABLE_COMPLEX_DEPS
225 /* simple fixed-size hash for package ids */
226 #define CPLXDEPHASH_EMPTY(elements) (memset(elements, 0, sizeof(Id) * 256))
227 #define CPLXDEPHASH_SET(elements, p) (elements[(p) & 255] |= (1 << ((p) >> 8 & 31)))
228 #define CPLXDEPHASH_TST(elements, p) (elements[(p) & 255] && (elements[(p) & 255] & (1 << ((p) >> 8 & 31))))
231 check_complex_dep(Solver *solv, Id dep, Map *m, Queue **cqp)
233 Pool *pool = solv->pool;
240 printf("check_complex_dep %s\n", pool_dep2str(pool, dep));
242 i = pool_normalize_complex_dep(pool, dep, &q, CPLXDEPS_EXPAND);
243 if (i == 0 || i == 1)
249 for (i = 0; i < qcnt; i++)
251 /* we rely on the fact that blocks are ordered here.
252 * if we reach a positive element, we know that we
253 * saw all negative ones */
254 for (; (p = q.elements[i]) < 0; i++)
256 if (solv->decisionmap[-p] < 0)
258 if (solv->decisionmap[-p] == 0)
259 queue_push(&q, -p); /* undecided negative literal */
264 printf("complex dep block cannot be true or no pos literals\n");
266 while (q.elements[i])
269 queue_truncate(&q, qcnt);
274 /* all negative literals installed, add positive literals to map */
275 for (; (p = q.elements[i]) != 0; i++)
280 /* at least one undecided negative literal, postpone */
284 printf("add new complex dep block\n");
285 for (j = qcnt; j < q.count; j++)
286 printf(" - %s\n", pool_solvid2str(pool, q.elements[j]));
288 while (q.elements[i])
292 cq = solv_calloc(1, sizeof(Queue));
294 queue_insertn(cq, 0, 256, 0); /* allocate hash area */
297 for (j = qcnt; j < q.count; j++)
300 /* check if we already have this (dep, p) entry */
301 for (k = 256; k < cq->count; k += 2)
302 if (cq->elements[k + 1] == dep && cq->elements[k] == p)
306 /* a new one. add to cq and hash */
307 queue_push2(cq, p, dep);
308 CPLXDEPHASH_SET(cq->elements, p);
311 queue_truncate(&q, qcnt);
318 recheck_complex_deps(Solver *solv, Id p, Map *m, Queue **cqp)
324 printf("recheck_complex_deps for package %s\n", pool_solvid2str(solv->pool, p));
326 /* make sure that we don't have a false hit */
327 for (i = 256; i < cq->count; i += 2)
328 if (cq->elements[i] == p)
331 return; /* false alert */
332 if (solv->decisionmap[p] <= 0)
333 return; /* just in case... */
335 /* rebuild the hash, call check_complex_dep for our package */
336 CPLXDEPHASH_EMPTY(cq->elements);
337 for (i = 256; i < cq->count; i += 2)
338 if ((pp = cq->elements[i]) == p)
340 Id dep = cq->elements[i + 1];
341 queue_deleten(cq, i, 2);
343 check_complex_dep(solv, dep, m, &cq);
346 CPLXDEPHASH_SET(cq->elements, pp);
353 policy_update_recommendsmap(Solver *solv)
355 Pool *pool = solv->pool;
357 Id p, pp, rec, *recp, sug, *sugp;
359 if (solv->recommends_index < 0)
361 MAPZERO(&solv->recommendsmap);
362 MAPZERO(&solv->suggestsmap);
363 #ifdef ENABLE_COMPLEX_DEPS
364 if (solv->recommendscplxq)
366 queue_free(solv->recommendscplxq);
367 solv->recommendscplxq = solv_free(solv->recommendscplxq);
369 if (solv->suggestscplxq)
371 queue_free(solv->suggestscplxq);
372 solv->suggestscplxq = solv_free(solv->suggestscplxq);
375 solv->recommends_index = 0;
377 while (solv->recommends_index < solv->decisionq.count)
379 p = solv->decisionq.elements[solv->recommends_index++];
382 s = pool->solvables + p;
383 #ifdef ENABLE_COMPLEX_DEPS
384 /* re-check postponed complex blocks */
385 if (solv->recommendscplxq && CPLXDEPHASH_TST(solv->recommendscplxq->elements, p))
386 recheck_complex_deps(solv, p, &solv->recommendsmap, &solv->recommendscplxq);
387 if (solv->suggestscplxq && CPLXDEPHASH_TST(solv->suggestscplxq->elements, p))
388 recheck_complex_deps(solv, p, &solv->suggestsmap, &solv->suggestscplxq);
392 recp = s->repo->idarraydata + s->recommends;
393 while ((rec = *recp++) != 0)
395 #ifdef ENABLE_COMPLEX_DEPS
396 if (pool_is_complex_dep(pool, rec))
398 check_complex_dep(solv, rec, &solv->recommendsmap, &solv->recommendscplxq);
402 FOR_PROVIDES(p, pp, rec)
403 MAPSET(&solv->recommendsmap, p);
408 sugp = s->repo->idarraydata + s->suggests;
409 while ((sug = *sugp++) != 0)
411 #ifdef ENABLE_COMPLEX_DEPS
412 if (pool_is_complex_dep(pool, sug))
414 check_complex_dep(solv, sug, &solv->suggestsmap, &solv->suggestscplxq);
418 FOR_PROVIDES(p, pp, sug)
419 MAPSET(&solv->suggestsmap, p);
425 /* bring suggested/enhanced packages to front
426 * installed packages count as suggested */
428 prefer_suggested(Solver *solv, Queue *plist)
430 Pool *pool = solv->pool;
433 /* update our recommendsmap/suggestsmap */
434 if (solv->recommends_index < solv->decisionq.count)
435 policy_update_recommendsmap(solv);
437 for (i = 0, count = plist->count; i < count; i++)
439 Id p = plist->elements[i];
440 Solvable *s = pool->solvables + p;
441 if ((pool->installed && s->repo == pool->installed) ||
442 MAPTST(&solv->suggestsmap, p) ||
443 solver_is_enhancing(solv, s))
444 continue; /* good package */
446 if (i < plist->count - 1)
448 memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
449 plist->elements[plist->count - 1] = p;
457 sort_by_favorq_cmp(const void *ap, const void *bp, void *dp)
459 const Id *a = ap, *b = bp, *d = dp;
460 return d[b[0]] - d[a[0]];
464 sort_by_favorq(Queue *favorq, Id *el, int cnt)
467 /* map to offsets into favorq */
468 for (i = 0; i < cnt; i++)
471 /* lookup p in sorted favorq */
472 int med = 0, low = 0;
473 int high = favorq->count / 2;
476 med = (low + high) / 2;
477 Id pp = favorq->elements[2 * med];
485 while(med && favorq->elements[2 * med - 2] == p)
487 if (favorq->elements[2 * med] == p)
492 /* sort by position */
493 solv_sort(el, cnt, sizeof(Id), sort_by_favorq_cmp, favorq->elements);
495 for (i = 0; i < cnt; i++)
497 el[i] = favorq->elements[el[i] - 1];
500 /* bring favored packages to front and disfavored packages to back */
502 policy_prefer_favored(Solver *solv, Queue *plist)
504 int i, fav, disfav, count;
505 if (!solv->favormap.size)
507 for (i = fav = disfav = 0, count = plist->count; i < count; i++)
509 Id p = plist->elements[i];
510 if (!MAPTST(&solv->favormap, p))
512 if (solv->isdisfavormap.size && MAPTST(&solv->isdisfavormap, p))
514 /* disfavored package. bring to back */
515 if (i < plist->count - 1)
517 memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
518 plist->elements[plist->count - 1] = p;
526 /* favored package. bring to front */
528 memmove(plist->elements + fav + 1, plist->elements + fav, (i - fav) * sizeof(Id));
529 plist->elements[fav++] = p;
532 /* if we have multiple favored/disfavored packages, sort by favorq index */
534 sort_by_favorq(solv->favorq, plist->elements, fav);
536 sort_by_favorq(solv->favorq, plist->elements + plist->count - disfav, disfav);
540 * prune to recommended/suggested packages.
541 * does not prune installed packages (they are also somewhat recommended).
544 prune_to_recommended(Solver *solv, Queue *plist)
546 Pool *pool = solv->pool;
554 for (i = 0; i < plist->count; i++)
556 p = plist->elements[i];
557 s = pool->solvables + p;
558 if (pool->installed && s->repo == pool->installed)
562 if (plist->count - ninst < 2)
565 /* update our recommendsmap/suggestsmap */
566 if (solv->recommends_index < solv->decisionq.count)
567 policy_update_recommendsmap(solv);
569 /* prune to recommended/supplemented */
571 for (i = j = 0; i < plist->count; i++)
573 p = plist->elements[i];
574 s = pool->solvables + p;
575 if (pool->installed && s->repo == pool->installed)
579 plist->elements[j++] = p;
582 if (!MAPTST(&solv->recommendsmap, p))
583 if (!solver_is_supplementing(solv, s))
587 for (k = 0; j < ninst; k++)
589 s = pool->solvables + plist->elements[k];
590 if (pool->installed && s->repo == pool->installed)
591 plist->elements[j++] = plist->elements[k];
594 plist->elements[j++] = p;
600 /* anything left to prune? */
601 if (plist->count - ninst < 2)
604 /* prune to suggested/enhanced */
606 for (i = j = 0; i < plist->count; i++)
608 p = plist->elements[i];
609 s = pool->solvables + p;
610 if (pool->installed && s->repo == pool->installed)
614 plist->elements[j++] = p;
617 if (!MAPTST(&solv->suggestsmap, p))
618 if (!solver_is_enhancing(solv, s))
622 for (k = 0; j < ninst; k++)
624 s = pool->solvables + plist->elements[k];
625 if (pool->installed && s->repo == pool->installed)
626 plist->elements[j++] = plist->elements[k];
629 plist->elements[j++] = p;
637 prune_to_best_arch(const Pool *pool, Queue *plist)
643 if (!pool->id2arch || plist->count < 2)
646 for (i = 0; i < plist->count; i++)
648 s = pool->solvables + plist->elements[i];
649 a = pool_arch2score(pool, s->arch);
650 if (a && a != 1 && (!bestscore || a < bestscore))
655 for (i = j = 0; i < plist->count; i++)
657 s = pool->solvables + plist->elements[i];
658 a = pool_arch2score(pool, s->arch);
661 /* a == 1 -> noarch */
662 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
664 plist->elements[j++] = plist->elements[i];
681 /* This is Tarjan's SCC algorithm, slightly modified */
683 trj_visit(struct trj_data *trj, Id node)
686 Pool *pool = trj->pool;
687 Queue *plist = trj->plist;
688 Id myidx, stackstart;
691 Id p, pp, obs, *obsp;
693 low[node] = myidx = trj->idx++;
694 trj->stack[(stackstart = trj->nstack++)] = node;
696 s = pool->solvables + plist->elements[node];
699 obsp = s->repo->idarraydata + s->obsoletes;
700 while ((obs = *obsp++) != 0)
702 FOR_PROVIDES(p, pp, obs)
704 Solvable *ps = pool->solvables + p;
705 if (ps->name == s->name)
707 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
709 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
711 /* hmm, expensive. should use hash if plist is big */
712 for (i = 0; i < plist->count; i++)
714 if (node != i && plist->elements[i] == p)
731 if (l < trj->firstidx)
734 /* this means we have reached an old SCC found earlier.
735 * delete it as we obsolete it */
738 if (low[trj->stack[k]] == l)
739 low[trj->stack[k]] = -1;
744 else if (l < low[node])
751 if (low[node] == myidx) /* found a SCC? */
753 /* we're only interested in SCCs that contain the first node,
754 * as all others are "obsoleted" */
755 if (myidx != trj->firstidx)
757 for (i = stackstart; i < trj->nstack; i++)
758 low[trj->stack[i]] = myidx;
759 trj->nstack = stackstart; /* empty stack */
764 * remove entries from plist that are obsoleted by other entries
765 * with different name.
768 prune_obsoleted(Pool *pool, Queue *plist)
770 Id data_buf[2 * 16], *data;
775 if (plist->count <= 16)
777 memset(data_buf, 0, sizeof(data_buf));
781 data = solv_calloc(plist->count, 2 * sizeof(Id));
786 trj.stack = data + plist->count - 1; /* -1 so we can index with idx (which starts with 1) */
787 for (i = 0; i < plist->count; i++)
791 s = pool->solvables + plist->elements[i];
794 trj.firstidx = trj.nstack = trj.idx;
799 Id myidx = trj.idx++;
801 trj.stack[myidx] = i;
804 for (i = j = 0; i < plist->count; i++)
806 plist->elements[j++] = plist->elements[i];
808 if (data != data_buf)
812 /* this is prune_obsoleted special-cased for two elements */
814 prune_obsoleted_2(Pool *pool, Queue *plist)
818 Id p, pp, obs, *obsp;
822 for (i = 0; i < 2; i++)
824 s = pool->solvables + plist->elements[i];
825 other = plist->elements[1 - i];
828 obsp = s->repo->idarraydata + s->obsoletes;
829 while ((obs = *obsp++) != 0)
831 FOR_PROVIDES(p, pp, obs)
836 ps = pool->solvables + p;
837 if (ps->name == s->name)
839 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
841 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
851 if (obmap == 0 || obmap == 3)
854 plist->elements[0] = plist->elements[1];
859 * bring those elements to the front of the queue that
860 * have a installed solvable with the same name
863 move_installed_to_front(Pool *pool, Queue *plist)
869 for (i = j = 0; i < plist->count; i++)
871 s = pool->solvables + plist->elements[i];
872 if (s->repo != pool->installed)
874 FOR_PROVIDES(p, pp, s->name)
876 Solvable *ps = pool->solvables + p;
877 if (s->name == ps->name && ps->repo == pool->installed)
884 if (s->repo == pool->installed)
888 p = plist->elements[i];
890 plist->elements[i] = plist->elements[j];
892 memmove(plist->elements + j + 1, plist->elements + j, (i - j) * sizeof(Id));
893 plist->elements[j] = p;
895 else if (j + 2 == plist->count)
896 break; /* no need to check last element if all prev ones are installed */
903 * prune_to_best_version
905 * sort list of packages (given through plist) by name and evr
906 * return result through plist
909 prune_to_best_version(Pool *pool, Queue *plist)
914 if (plist->count < 2) /* no need to prune for a single entry */
916 POOL_DEBUG(SOLV_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
918 /* sort by name first, prefer installed */
919 solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
921 /* now find best 'per name' */
923 for (i = j = 0; i < plist->count; i++)
925 s = pool->solvables + plist->elements[i];
927 POOL_DEBUG(SOLV_DEBUG_POLICY, "- %s[%s]\n",
928 pool_solvable2str(pool, s),
929 (pool->installed && s->repo == pool->installed) ? "installed" : "not installed");
931 if (!best) /* if no best yet, the current is best */
937 /* name switch: finish group, re-init */
938 if (best->name != s->name) /* new name */
940 plist->elements[j++] = best - pool->solvables; /* move old best to front */
941 best = s; /* take current as new best */
944 r = best->evr != s->evr ? pool_evrcmp(pool, best->evr, s->evr, EVRCMP_COMPARE) : 0;
945 #ifdef ENABLE_LINKED_PKGS
946 if (r == 0 && has_package_link(pool, s))
947 r = pool_link_evrcmp(pool, best, s);
952 plist->elements[j++] = best - pool->solvables; /* finish last group */
955 /* we reduced the list to one package per name, now look at
956 * package obsoletes */
957 if (plist->count > 1)
959 if (plist->count == 2)
960 prune_obsoleted_2(pool, plist);
962 prune_obsoleted(pool, plist);
964 if (plist->count > 1 && pool->installed)
965 move_installed_to_front(pool, plist);
970 sort_by_name_evr_sortcmp(const void *ap, const void *bp, void *dp)
973 Id a, *aa = (Id *)ap;
974 Id b, *bb = (Id *)bp;
975 Id r = aa[1] - bb[1];
977 return r < 0 ? -1 : 1;
980 a = aa[2] < 0 ? -aa[2] : aa[2];
981 b = bb[2] < 0 ? -bb[2] : bb[2];
982 r = pool_evrcmp(pool, b, a, pool->disttype != DISTTYPE_DEB ? EVRCMP_MATCH_RELEASE : EVRCMP_COMPARE);
983 if (!r && (aa[2] < 0 || bb[2] < 0))
993 /* common end of sort_by_srcversion and sort_by_common_dep */
995 sort_by_name_evr_array(Pool *pool, Queue *plist, int count, int ent)
998 int i, j, bad, havebad;
999 Id *pp, *elements = plist->elements;
1003 queue_truncate(plist, count);
1006 solv_sort(elements + count * 2, ent, sizeof(Id) * 3, sort_by_name_evr_sortcmp, pool);
1009 for (i = 0, pp = elements + count * 2; i < ent; i++, pp += 3)
1011 if (lastname && pp[1] == lastname)
1013 if (pp[0] != pp[-3] && sort_by_name_evr_sortcmp(pp - 3, pp, pool) == -1)
1016 printf("%s - %s: bad %s %s - %s\n", pool_solvid2str(pool, elements[pp[-3]]), pool_solvid2str(pool, elements[pp[0]]), pool_dep2str(pool, lastname), pool_id2str(pool, pp[-1] < 0 ? -pp[-1] : pp[-1]), pool_id2str(pool, pp[2] < 0 ? -pp[2] : pp[2]));
1027 elements[count + pp[0]] += bad;
1031 for (i = 0; i < count; i++)
1032 printf("%s badness %d\n", pool_solvid2str(pool, elements[i]), elements[count + i]);
1037 /* simple stable insertion sort */
1038 if (pool->installed)
1039 for (i = 0; i < count; i++)
1040 if (pool->solvables[elements[i]].repo == pool->installed)
1041 elements[i + count] = 0;
1042 for (i = 1; i < count; i++)
1043 for (j = i, pp = elements + count + j; j > 0; j--, pp--)
1046 Id *pp2 = pp - count;
1057 queue_truncate(plist, count);
1062 sort_by_srcversion(Pool *pool, Queue *plist)
1064 int i, count = plist->count, ent = 0;
1065 queue_insertn(plist, count, count, 0);
1066 for (i = 0; i < count; i++)
1068 Id name, evr, p = plist->elements[i];
1069 Solvable *s = pool->solvables + p;
1070 if (solvable_lookup_void(s, SOLVABLE_SOURCENAME))
1073 name = solvable_lookup_id(s, SOLVABLE_SOURCENAME);
1074 if (solvable_lookup_void(s, SOLVABLE_SOURCEEVR))
1077 evr = solvable_lookup_id(s, SOLVABLE_SOURCEEVR);
1078 if (!name || !evr || ISRELDEP(evr))
1080 queue_push(plist, i);
1081 queue_push2(plist, name, evr);
1084 sort_by_name_evr_array(pool, plist, count, ent);
1089 sort_by_common_dep(Pool *pool, Queue *plist)
1091 int i, count = plist->count, ent = 0;
1093 queue_insertn(plist, count, count, 0);
1094 for (i = 0; i < count; i++)
1096 Id p = plist->elements[i];
1097 Solvable *s = pool->solvables + p;
1100 for (dp = s->repo->idarraydata + s->provides; (id = *dp++) != 0; )
1105 rd = GETRELDEP(pool, id);
1106 if ((rd->flags == REL_EQ || rd->flags == (REL_EQ | REL_LT) || rd->flags == REL_LT) && !ISRELDEP(rd->evr))
1108 if (rd->flags == REL_EQ)
1111 const char *s = pool_id2str(pool, rd->evr);
1114 while ((*s >= 'a' && *s <= 'f') || (*s >= '0' && *s <= '9'))
1120 queue_push(plist, i);
1121 queue_push2(plist, rd->name, rd->flags == REL_LT ? -rd->evr : rd->evr);
1126 sort_by_name_evr_array(pool, plist, count, ent);
1129 /* check if we have an update candidate */
1131 dislike_old_versions(Pool *pool, Queue *plist)
1135 for (i = 0, count = plist->count; i < count; i++)
1137 Id p = plist->elements[i];
1138 Solvable *s = pool->solvables + p;
1139 Repo *repo = s->repo;
1143 if (!repo || repo == pool->installed)
1145 FOR_PROVIDES(q, qq, s->name)
1147 Solvable *qs = pool->solvables + q;
1150 if (s->name != qs->name || s->arch != qs->arch)
1152 if (repo->priority != qs->repo->priority)
1154 if (repo->priority > qs->repo->priority)
1159 if (pool_evrcmp(pool, qs->evr, s->evr, EVRCMP_COMPARE) > 0)
1168 if (i < plist->count - 1)
1170 memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
1171 plist->elements[plist->count - 1] = p;
1179 /* special lang package handling for urpm */
1180 /* see https://bugs.mageia.org/show_bug.cgi?id=18315 */
1183 urpm_reorder_cmp(const void *ap, const void *bp, void *dp)
1185 return ((Id *)bp)[1] - ((Id *)ap)[1];
1189 urpm_reorder(Solver *solv, Queue *plist)
1191 Pool *pool = solv->pool;
1192 int i, count = plist->count;
1193 /* add locale score to packages */
1194 queue_insertn(plist, count, count, 0);
1195 for (i = count - 1; i >= 0; i--)
1197 Solvable *s = pool->solvables + plist->elements[i];
1199 const char *sn = pool_id2str(pool, s->name);
1201 if (!strncmp(sn, "kernel-", 7))
1203 const char *devel = strstr(sn, "-devel-");
1204 if (devel && strlen(sn) < 256)
1208 memcpy(kn, sn, devel - sn);
1209 strcpy(kn + (devel - sn), devel + 6);
1210 knid = pool_str2id(pool, kn, 0);
1213 FOR_PROVIDES(p, pp, knid)
1215 if (solv->decisionmap[p] > 0)
1220 else if (pool->installed && pool->solvables[p].repo == pool->installed)
1226 else if ((sn = strstr(sn, "-kernel-")) != 0)
1229 if (strlen(sn) < 256 - 8 && *sn >= '0' && *sn <= '9' && sn[1] == '.')
1231 const char *flavor = strchr(sn, '-');
1234 const char *release = strchr(flavor + 1, '-');
1239 memcpy(kn, "kernel", 8);
1240 memcpy(kn + 6, flavor, release - flavor + 1);
1241 memcpy(kn + 6 + (release - flavor) + 1, sn, flavor - sn);
1242 strcpy(kn + 6 + (release + 1 - sn), release);
1243 knid = pool_str2id(pool, kn, 0);
1246 FOR_PROVIDES(p, pp, knid)
1248 if (solv->decisionmap[p] > 0)
1253 if (pool->installed && pool->solvables[p].repo == pool->installed)
1261 if (score == 1 && s->requires)
1265 for (idp = s->repo->idarraydata + s->requires; (id = *idp) != 0; idp++)
1267 while (ISRELDEP(id))
1269 Reldep *rd = GETRELDEP(pool, id);
1272 deps = strstr(pool_id2str(pool, id), "locales-");
1275 if (!strncmp(deps + 8, "en", 2))
1280 FOR_PROVIDES(p, pp, id)
1282 if (solv->decisionmap[p] > 0)
1287 if (pool->installed && pool->solvables[p].repo == pool->installed)
1294 plist->elements[i * 2] = plist->elements[i];
1295 plist->elements[i * 2 + 1] = score;
1297 solv_sort(plist->elements, count, sizeof(Id) * 2, urpm_reorder_cmp, pool);
1298 for (i = 0; i < count; i++)
1299 plist->elements[i] = plist->elements[2 * i];
1300 queue_truncate(plist, count);
1305 * POLICY_MODE_CHOOSE: default, do all pruning steps
1306 * POLICY_MODE_RECOMMEND: leave out prune_to_recommended
1307 * POLICY_MODE_SUGGEST: leave out prune_to_recommended, do prio pruning just per name
1310 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
1312 Pool *pool = solv->pool;
1313 if (mode == POLICY_MODE_SUPPLEMENT)
1316 dislike_old_versions(pool, plist);
1317 sort_by_common_dep(pool, plist);
1318 if (solv->urpmreorder)
1319 urpm_reorder(solv, plist);
1320 prefer_suggested(solv, plist);
1321 policy_prefer_favored(solv, plist);
1324 if (plist->count > 1)
1326 if (mode != POLICY_MODE_SUGGEST)
1327 solver_prune_to_highest_prio(solv, plist);
1329 solver_prune_to_highest_prio_per_name(solv, plist);
1331 if (plist->count > 1)
1332 prune_to_best_arch(pool, plist);
1333 if (plist->count > 1)
1334 prune_to_best_version(pool, plist);
1335 if (plist->count > 1 && (mode == POLICY_MODE_CHOOSE || mode == POLICY_MODE_CHOOSE_NOREORDER))
1337 prune_to_recommended(solv, plist);
1338 if (plist->count > 1 && mode != POLICY_MODE_CHOOSE_NOREORDER)
1340 /* do some fancy reordering */
1342 sort_by_srcversion(pool, plist);
1344 dislike_old_versions(pool, plist);
1345 sort_by_common_dep(pool, plist);
1346 if (solv->urpmreorder)
1347 urpm_reorder(solv, plist);
1348 prefer_suggested(solv, plist);
1349 policy_prefer_favored(solv, plist);
1355 pool_best_solvables(Pool *pool, Queue *plist, int flags)
1357 if (plist->count > 1)
1358 prune_to_highest_prio(pool, plist);
1359 if (plist->count > 1)
1360 prune_to_best_arch(pool, plist);
1361 if (plist->count > 1)
1362 prune_to_best_version(pool, plist);
1363 if (plist->count > 1)
1365 dislike_old_versions(pool, plist);
1366 sort_by_common_dep(pool, plist);
1371 /* check if there is an illegal architecture change if
1372 * installed solvable s1 is replaced by s2 */
1374 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
1376 Pool *pool = solv->pool;
1377 Id a1 = s1->arch, a2 = s2->arch;
1379 /* we allow changes to/from noarch */
1380 if (a1 == a2 || a1 == pool->noarchid || a2 == pool->noarchid)
1384 a1 = pool_arch2score(pool, a1);
1385 a2 = pool_arch2score(pool, a2);
1386 if (((a1 ^ a2) & 0xffff0000) != 0)
1391 /* check if there is an illegal vendor change if
1392 * installed solvable s1 is replaced by s2 */
1394 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
1396 Pool *pool = solv->pool;
1398 Id vendormask1, vendormask2;
1400 if (pool->custom_vendorcheck)
1401 return pool->custom_vendorcheck(pool, s1, s2);
1403 /* treat a missing vendor as empty string */
1404 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
1405 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
1408 vendormask1 = pool_vendor2mask(pool, v1);
1410 return 1; /* can't match */
1411 vendormask2 = pool_vendor2mask(pool, v2);
1412 if ((vendormask1 & vendormask2) != 0)
1414 return 1; /* no class matches */
1417 /* check if it is illegal to replace installed
1418 * package "is" with package "s" (which must obsolete "is")
1421 policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
1423 Pool *pool = solv->pool;
1425 int duppkg = solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, is - pool->solvables));
1426 if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !(duppkg ? solv->dup_allowdowngrade : solv->allowdowngrade))
1428 if (is->name == s->name && pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
1429 ret |= POLICY_ILLEGAL_DOWNGRADE;
1431 if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !(duppkg ? solv->dup_allowarchchange : solv->allowarchchange))
1433 if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
1434 ret |= POLICY_ILLEGAL_ARCHCHANGE;
1436 if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !(duppkg ? solv->dup_allowvendorchange : solv->allowvendorchange))
1438 if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
1439 ret |= POLICY_ILLEGAL_VENDORCHANGE;
1441 if (!(ignore & POLICY_ILLEGAL_NAMECHANGE) && !(duppkg ? solv->dup_allownamechange : solv->allownamechange))
1443 if (is->name != s->name)
1444 ret |= POLICY_ILLEGAL_NAMECHANGE;
1449 /*-------------------------------------------------------------------
1451 * create reverse obsoletes map for installed solvables
1453 * For each installed solvable find which packages with *different* names
1454 * obsolete the solvable.
1455 * This index is used in policy_findupdatepackages() below.
1458 policy_create_obsolete_index(Solver *solv)
1460 Pool *pool = solv->pool;
1462 Repo *installed = solv->installed;
1463 Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
1466 solv->obsoletes = solv_free(solv->obsoletes);
1467 solv->obsoletes_data = solv_free(solv->obsoletes_data);
1468 if (!installed || installed->start == installed->end)
1470 cnt = installed->end - installed->start;
1471 solv->obsoletes = obsoletes = solv_calloc(cnt, sizeof(Id));
1472 for (i = 1; i < pool->nsolvables; i++)
1474 s = pool->solvables + i;
1477 if (!pool_installable(pool, s))
1479 obsp = s->repo->idarraydata + s->obsoletes;
1480 while ((obs = *obsp++) != 0)
1482 FOR_PROVIDES(p, pp, obs)
1484 Solvable *ps = pool->solvables + p;;
1485 if (ps->repo != installed)
1487 if (ps->name == s->name)
1489 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1491 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1493 obsoletes[p - installed->start]++;
1498 for (i = 0; i < cnt; i++)
1501 n += obsoletes[i] + 1;
1504 solv->obsoletes_data = obsoletes_data = solv_calloc(n + 1, sizeof(Id));
1505 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
1506 for (i = pool->nsolvables - 1; i > 0; i--)
1508 s = pool->solvables + i;
1511 if (!pool_installable(pool, s))
1513 obsp = s->repo->idarraydata + s->obsoletes;
1514 while ((obs = *obsp++) != 0)
1516 FOR_PROVIDES(p, pp, obs)
1518 Solvable *ps = pool->solvables + p;;
1519 if (ps->repo != installed)
1521 if (ps->name == s->name)
1523 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1525 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1527 if (obsoletes_data[obsoletes[p - installed->start]] != i)
1528 obsoletes_data[--obsoletes[p - installed->start]] = i;
1536 * find update candidates
1538 * s: installed solvable to be updated
1539 * qs: [out] queue to hold Ids of candidates
1540 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1545 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
1547 /* installed packages get a special upgrade allowed rule */
1548 Pool *pool = solv->pool;
1549 Id p, pp, n, p2, pp2;
1552 int haveprovobs = 0;
1553 int allowdowngrade = allow_all ? 1 : solv->allowdowngrade;
1554 int allownamechange = allow_all ? 1 : solv->allownamechange;
1555 int allowarchchange = allow_all ? 1 : solv->allowarchchange;
1556 int allowvendorchange = allow_all ? 1 : solv->allowvendorchange;
1559 allowdowngrade = solv->dup_allowdowngrade;
1560 allownamechange = solv->dup_allownamechange;
1561 allowarchchange = solv->dup_allowarchchange;
1562 allowvendorchange = solv->dup_allowvendorchange;
1567 n = s - pool->solvables;
1570 * look for updates for s
1572 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
1574 if (p == n) /* skip itself */
1577 ps = pool->solvables + p;
1578 if (s->name == ps->name) /* name match */
1580 if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
1582 if (!allowdowngrade && pool_evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
1585 else if (!allownamechange)
1587 else if ((!solv->noupdateprovide || solv->needupdateprovide) && ps->obsoletes) /* provides/obsoletes combination ? */
1589 /* check if package ps obsoletes installed package s */
1590 /* implicitobsoleteusescolors is somewhat wrong here, but we nevertheless
1591 * use it to limit our update candidates */
1592 if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s, ps))
1594 obsp = ps->repo->idarraydata + ps->obsoletes;
1595 while ((obs = *obsp++) != 0) /* for all obsoletes */
1597 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1599 Solvable *ps2 = pool->solvables + p2;
1600 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
1602 if (p2 == n) /* match ! */
1605 if (p2) /* match! */
1608 if (!obs) /* continue if no match */
1610 /* here we have 'p' with a matching provides/obsoletes combination
1611 * thus flagging p as a valid update candidate for s
1617 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
1619 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
1623 if (!allownamechange)
1625 /* if we have found some valid candidates and noupdateprovide is not set, we're
1626 done. otherwise we fallback to all obsoletes */
1627 if (solv->needupdateprovide || (!solv->noupdateprovide && haveprovobs))
1629 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
1632 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
1634 ps = pool->solvables + p;
1635 if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
1637 if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
1639 /* implicitobsoleteusescolors is somewhat wrong here, but we nevertheless
1640 * use it to limit our update candidates */
1641 if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
1649 policy_illegal2str(Solver *solv, int illegal, Solvable *s, Solvable *rs)
1651 Pool *pool = solv->pool;
1653 if (illegal == POLICY_ILLEGAL_DOWNGRADE)
1655 str = pool_tmpjoin(pool, "downgrade of ", pool_solvable2str(pool, s), 0);
1656 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
1658 if (illegal == POLICY_ILLEGAL_NAMECHANGE)
1660 str = pool_tmpjoin(pool, "name change of ", pool_solvable2str(pool, s), 0);
1661 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
1663 if (illegal == POLICY_ILLEGAL_ARCHCHANGE)
1665 str = pool_tmpjoin(pool, "architecture change of ", pool_solvable2str(pool, s), 0);
1666 return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
1668 if (illegal == POLICY_ILLEGAL_VENDORCHANGE)
1670 str = pool_tmpjoin(pool, "vendor change from '", pool_id2str(pool, s->vendor), "' (");
1673 str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to '");
1674 str = pool_tmpappend(pool, str, pool_id2str(pool, rs->vendor), "' (");
1677 str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to no vendor (");
1678 return pool_tmpappend(pool, str, pool_solvable2str(pool, rs), ")");
1680 return "unknown illegal change";