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
21 #include "poolvendor.h"
25 /*-----------------------------------------------------------------*/
28 * prep for prune_best_version_arch
33 prune_to_best_version_sortcmp(const void *ap, const void *bp, void *dp)
36 Pool *pool = solv->pool;
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 = id2str(pool, sa->name);
51 nb = id2str(pool, sb->name);
52 return strcmp(na, nb);
54 /* the same name, bring installed solvables to the front */
57 if (sa->repo == solv->installed)
59 if (sb->repo != solv->installed)
62 else if (sb->repo == solv->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];
111 * prune to recommended/suggested packages.
112 * does not prune installed packages (they are also somewhat recommended).
116 prune_to_recommended(Solver *solv, Queue *plist)
118 Pool *pool = solv->pool;
121 Id p, pp, rec, *recp, sug, *sugp;
126 for (i = 0; i < plist->count; i++)
128 p = plist->elements[i];
129 s = pool->solvables + p;
130 if (pool->installed && s->repo == pool->installed)
134 if (plist->count - ninst < 2)
137 /* update our recommendsmap/suggestsmap */
138 if (solv->recommends_index < 0)
140 MAPZERO(&solv->recommendsmap);
141 MAPZERO(&solv->suggestsmap);
142 solv->recommends_index = 0;
144 while (solv->recommends_index < solv->decisionq.count)
146 p = solv->decisionq.elements[solv->recommends_index++];
149 s = pool->solvables + p;
152 recp = s->repo->idarraydata + s->recommends;
153 while ((rec = *recp++) != 0)
154 FOR_PROVIDES(p, pp, rec)
155 MAPSET(&solv->recommendsmap, p);
159 sugp = s->repo->idarraydata + s->suggests;
160 while ((sug = *sugp++) != 0)
161 FOR_PROVIDES(p, pp, sug)
162 MAPSET(&solv->suggestsmap, p);
166 /* prune to recommended/supplemented */
168 for (i = j = 0; i < plist->count; i++)
170 p = plist->elements[i];
171 s = pool->solvables + p;
172 if (pool->installed && s->repo == pool->installed)
176 plist->elements[j++] = p;
179 if (!MAPTST(&solv->recommendsmap, p))
180 if (!solver_is_supplementing(solv, s))
184 for (k = 0; j < ninst; k++)
186 s = pool->solvables + plist->elements[k];
187 if (pool->installed && s->repo == pool->installed)
188 plist->elements[j++] = plist->elements[k];
191 plist->elements[j++] = p;
196 /* anything left to prune? */
197 if (plist->count - ninst < 2)
200 /* prune to suggested/enhanced*/
202 for (i = j = 0; i < plist->count; i++)
204 p = plist->elements[i];
205 s = pool->solvables + p;
206 if (pool->installed && s->repo == pool->installed)
210 plist->elements[j++] = p;
213 if (!MAPTST(&solv->suggestsmap, p))
214 if (!solver_is_enhancing(solv, s))
218 for (k = 0; j < ninst; k++)
220 s = pool->solvables + plist->elements[k];
221 if (pool->installed && s->repo == pool->installed)
222 plist->elements[j++] = plist->elements[k];
225 plist->elements[j++] = p;
232 prune_to_best_arch(Pool *pool, Queue *plist)
238 if (!pool->id2arch || plist->count < 2)
241 for (i = 0; i < plist->count; i++)
243 s = pool->solvables + plist->elements[i];
245 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
246 if (a && a != 1 && (!bestscore || a < bestscore))
249 for (i = j = 0; i < plist->count; i++)
251 s = pool->solvables + plist->elements[i];
253 if (a > pool->lastarch)
255 a = pool->id2arch[a];
256 /* a == 1 -> noarch */
257 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
259 plist->elements[j++] = plist->elements[i];
266 * prune_to_best_version
268 * sort list of packages (given through plist) by name and evr
269 * return result through plist
272 prune_to_best_version(Solver *solv, Queue *plist)
274 Pool *pool = solv->pool;
279 if (plist->count < 2) /* no need to prune for a single entry */
281 POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
283 /* sort by name first, prefer installed */
284 sat_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, solv);
286 /* delete obsoleted. hmm, looks expensive! */
287 /* FIXME maybe also check provides depending on noupdateprovide? */
288 /* FIXME do not prune cycles */
289 for (i = 0; i < plist->count; i++)
291 Id p, pp, obs, *obsp;
292 s = pool->solvables + plist->elements[i];
295 obsp = s->repo->idarraydata + s->obsoletes;
296 while ((obs = *obsp++) != 0)
298 FOR_PROVIDES(p, pp, obs)
300 if (pool->solvables[p].name == s->name)
302 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
304 for (j = 0; j < plist->count; j++)
308 if (plist->elements[j] == p)
309 plist->elements[j] = 0;
314 /* delete zeroed out queue entries */
315 for (i = j = 0; i < plist->count; i++)
316 if (plist->elements[i])
317 plist->elements[j++] = plist->elements[i];
320 /* now find best 'per name' */
322 for (i = j = 0; i < plist->count; i++)
324 s = pool->solvables + plist->elements[i];
326 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
327 solvable2str(pool, s),
328 (solv->installed && s->repo == solv->installed) ? "installed" : "not installed");
330 if (!best) /* if no best yet, the current is best */
332 best = plist->elements[i];
336 /* name switch: re-init */
337 if (pool->solvables[best].name != s->name) /* new name */
339 plist->elements[j++] = best; /* move old best to front */
340 best = plist->elements[i]; /* take current as new best */
344 if (pool->solvables[best].evr != s->evr) /* compare evr */
346 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_COMPARE) < 0)
347 best = plist->elements[i];
352 best = plist->elements[0];
354 plist->elements[j++] = best;
359 /* legacy, do not use anymore!
360 * (rates arch higher than version, but thats a policy)
364 prune_best_arch_name_version(Solver *solv, Pool *pool, Queue *plist)
366 if (solv && solv->bestSolvableCb)
367 { /* The application is responsible for */
368 return solv->bestSolvableCb(solv->pool, plist);
371 if (plist->count > 1)
372 prune_to_best_arch(pool, plist);
373 if (plist->count > 1)
374 prune_to_best_version(solv, plist);
379 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
381 Pool *pool = solv->pool;
382 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
383 prune_to_highest_prio(pool, plist);
384 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
385 prune_to_recommended(solv, plist);
386 prune_best_arch_name_version(solv, pool, plist);
391 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
393 Pool *pool = solv->pool;
394 Id a1 = s1->arch, a2 = s2->arch;
396 if (solv && solv->archCheckCb)
397 { /* The application is responsible for */
398 return solv->archCheckCb(solv->pool, s1, s2);
401 /* we allow changes to/from noarch */
402 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
406 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
407 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
408 if (((a1 ^ a2) & 0xffff0000) != 0)
414 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
416 Pool *pool = solv->pool;
417 Id vendormask1, vendormask2;
419 if (solv && solv->vendorCheckCb)
420 { /* The application is responsible for */
421 return solv->vendorCheckCb(solv->pool, s1, s2);
424 if (s1->vendor == s2->vendor)
426 vendormask1 = pool_vendor2mask(pool, s1->vendor);
429 vendormask2 = pool_vendor2mask(pool, s2->vendor);
430 if ((vendormask1 & vendormask2) != 0)
436 /*-------------------------------------------------------------------
438 * create reverse obsoletes map for installed solvables
440 * For each installed solvable find which packages with *different* names
441 * obsolete the solvable.
442 * This index is used in policy_findupdatepackages() below.
445 policy_create_obsolete_index(Solver *solv)
447 Pool *pool = solv->pool;
449 Repo *installed = solv->installed;
450 Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
453 if (!installed || installed->start == installed->end)
455 cnt = installed->end - installed->start;
456 solv->obsoletes = obsoletes = sat_calloc(cnt, sizeof(Id));
457 for (i = 1; i < pool->nsolvables; i++)
459 s = pool->solvables + i;
462 if (!pool_installable(pool, s))
464 obsp = s->repo->idarraydata + s->obsoletes;
465 while ((obs = *obsp++) != 0)
467 FOR_PROVIDES(p, pp, obs)
469 if (pool->solvables[p].repo != installed)
471 if (pool->solvables[p].name == s->name)
473 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
475 obsoletes[p - installed->start]++;
480 for (i = 0; i < cnt; i++)
483 n += obsoletes[i] + 1;
486 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
487 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
488 for (i = pool->nsolvables - 1; i > 0; i--)
490 s = pool->solvables + i;
493 if (!pool_installable(pool, s))
495 obsp = s->repo->idarraydata + s->obsoletes;
496 while ((obs = *obsp++) != 0)
498 FOR_PROVIDES(p, pp, obs)
500 if (pool->solvables[p].repo != installed)
502 if (pool->solvables[p].name == s->name)
504 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
506 p -= installed->start;
507 if (obsoletes_data[obsoletes[p]] != i)
508 obsoletes_data[--obsoletes[p]] = i;
516 * find update candidates
518 * s: solvable to be updated
519 * qs: [out] queue to hold Ids of candidates
520 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
524 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
526 /* installed packages get a special upgrade allowed rule */
527 Pool *pool = solv->pool;
528 Id p, pp, n, p2, pp2;
534 if (solv && solv->updateCandidateCb)
535 { /* The application is responsible for */
536 return solv->updateCandidateCb(solv->pool, s, qs);
543 n = s - pool->solvables;
546 * look for updates for s
548 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
550 if (p == n) /* skip itself */
553 ps = pool->solvables + p;
554 if (s->name == ps->name) /* name match */
556 if (!allow_all && !solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
559 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
561 obsp = ps->repo->idarraydata + ps->obsoletes;
562 while ((obs = *obsp++) != 0) /* for all obsoletes */
564 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
566 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
568 if (p2 == n) /* match ! */
574 if (!obs) /* continue if no match */
576 /* here we have 'p' with a matching provides/obsoletes combination
577 * thus flagging p as a valid update candidate for s
582 if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
584 if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
588 /* if we have found some valid candidates and noupdateprovide is not set, we're
589 done. otherwise we fallback to all obsoletes */
590 if (!solv->noupdateprovide && qs->count)
592 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
595 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
597 ps = pool->solvables + p;
598 if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
600 if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))