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 static Solver *prune_to_best_version_sortcmp_data;
27 /*-----------------------------------------------------------------*/
30 * prep for prune_best_version_arch
35 prune_to_best_version_sortcmp(const void *ap, const void *bp)
37 Solver *solv = prune_to_best_version_sortcmp_data;
38 Pool *pool = solv->pool;
44 sa = pool->solvables + a;
45 sb = pool->solvables + b;
46 r = sa->name - sb->name;
50 /* different names. We use real strcmp here so that the result
51 * is not depending on some random solvable order */
52 na = id2str(pool, sa->name);
53 nb = id2str(pool, sb->name);
54 /* bring patterns to the front */
55 /* XXX: no longer needed? */
56 if (!strncmp(na, "pattern:", 8))
58 if (strncmp(nb, "pattern:", 8))
61 else if (!strncmp(nb, "pattern:", 8))
63 if (strncmp(na, "pattern:", 8))
66 return strcmp(na, nb);
68 /* the same name, bring installed solvables to the front */
71 if (sa->repo == solv->installed)
73 if (sb->repo != solv->installed)
76 else if (sb->repo == solv->installed)
79 /* sort by repository sub-prio (installed repo handled above) */
80 r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
83 /* no idea about the order, sort by id */
89 * prune to repository with highest priority.
90 * does not prune installed solvables.
94 prune_to_highest_prio(Pool *pool, Queue *plist)
100 /* prune to highest priority */
101 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
103 s = pool->solvables + plist->elements[i];
104 if (pool->installed && s->repo == pool->installed)
106 if (i == 0 || s->repo->priority > bestprio)
107 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];
120 * prune to recommended/suggested packages.
121 * does not prune installed packages (they are also somewhat recommended).
125 prune_to_recommended(Solver *solv, Queue *plist)
127 Pool *pool = solv->pool;
130 Id p, pp, rec, *recp, sug, *sugp;
135 for (i = 0; i < plist->count; i++)
137 p = plist->elements[i];
138 s = pool->solvables + p;
139 if (pool->installed && s->repo == pool->installed)
143 if (plist->count - ninst < 2)
146 /* update our recommendsmap/suggestsmap */
147 if (solv->recommends_index < 0)
149 MAPZERO(&solv->recommendsmap);
150 MAPZERO(&solv->suggestsmap);
151 solv->recommends_index = 0;
153 while (solv->recommends_index < solv->decisionq.count)
155 p = solv->decisionq.elements[solv->recommends_index++];
158 s = pool->solvables + p;
161 recp = s->repo->idarraydata + s->recommends;
162 while ((rec = *recp++) != 0)
163 FOR_PROVIDES(p, pp, rec)
164 MAPSET(&solv->recommendsmap, p);
168 sugp = s->repo->idarraydata + s->suggests;
169 while ((sug = *sugp++) != 0)
170 FOR_PROVIDES(p, pp, sug)
171 MAPSET(&solv->suggestsmap, p);
175 /* prune to recommended/supplemented */
177 for (i = j = 0; i < plist->count; i++)
179 p = plist->elements[i];
180 s = pool->solvables + p;
181 if (pool->installed && s->repo == pool->installed)
185 plist->elements[j++] = p;
188 if (!MAPTST(&solv->recommendsmap, p))
189 if (!solver_is_supplementing(solv, s))
193 for (k = 0; j < ninst; k++)
195 s = pool->solvables + plist->elements[k];
196 if (pool->installed && s->repo == pool->installed)
197 plist->elements[j++] = plist->elements[k];
200 plist->elements[j++] = p;
205 /* anything left to prune? */
206 if (plist->count - ninst < 2)
209 /* prune to suggested/enhanced*/
211 for (i = j = 0; i < plist->count; i++)
213 p = plist->elements[i];
214 s = pool->solvables + p;
215 if (pool->installed && s->repo == pool->installed)
219 plist->elements[j++] = p;
222 if (!MAPTST(&solv->suggestsmap, p))
223 if (!solver_is_enhancing(solv, s))
227 for (k = 0; j < ninst; k++)
229 s = pool->solvables + plist->elements[k];
230 if (pool->installed && s->repo == pool->installed)
231 plist->elements[j++] = plist->elements[k];
234 plist->elements[j++] = p;
241 prune_to_best_arch(Pool *pool, Queue *plist)
247 if (!pool->id2arch || plist->count < 2)
250 for (i = 0; i < plist->count; i++)
252 s = pool->solvables + plist->elements[i];
254 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
255 if (a && a != 1 && (!bestscore || a < bestscore))
258 for (i = j = 0; i < plist->count; i++)
260 s = pool->solvables + plist->elements[i];
262 if (a > pool->lastarch)
264 a = pool->id2arch[a];
265 /* a == 1 -> noarch */
266 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
268 plist->elements[j++] = plist->elements[i];
275 * prune_to_best_version
277 * sort list of packages (given through plist) by name and evr
278 * return result through plist
281 prune_to_best_version(Solver *solv, Queue *plist)
283 Pool *pool = solv->pool;
288 if (plist->count < 2) /* no need to prune for a single entry */
290 POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
292 prune_to_best_version_sortcmp_data = solv;
293 /* sort by name first, prefer installed */
294 qsort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp);
296 /* delete obsoleted. hmm, looks expensive! */
297 /* FIXME maybe also check provides depending on noupdateprovide? */
298 /* FIXME do not prune cycles */
299 for (i = 0; i < plist->count; i++)
301 Id p, pp, obs, *obsp;
302 s = pool->solvables + plist->elements[i];
305 obsp = s->repo->idarraydata + s->obsoletes;
306 while ((obs = *obsp++) != 0)
308 FOR_PROVIDES(p, pp, obs)
310 if (pool->solvables[p].name == s->name)
312 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
314 for (j = 0; j < plist->count; j++)
318 if (plist->elements[j] == p)
319 plist->elements[j] = 0;
324 /* delete zeroed out queue entries */
325 for (i = j = 0; i < plist->count; i++)
326 if (plist->elements[i])
327 plist->elements[j++] = plist->elements[i];
330 /* now find best 'per name' */
332 for (i = j = 0; i < plist->count; i++)
334 s = pool->solvables + plist->elements[i];
336 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
337 solvable2str(pool, s),
338 (solv->installed && s->repo == solv->installed) ? "installed" : "not installed");
340 if (!best) /* if no best yet, the current is best */
342 best = plist->elements[i];
346 /* name switch: re-init */
347 if (pool->solvables[best].name != s->name) /* new name */
349 plist->elements[j++] = best; /* move old best to front */
350 best = plist->elements[i]; /* take current as new best */
354 if (pool->solvables[best].evr != s->evr) /* compare evr */
356 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_COMPARE) < 0)
357 best = plist->elements[i];
362 best = plist->elements[0];
364 plist->elements[j++] = best;
369 /* legacy, do not use anymore!
370 * (rates arch higher than version, but thats a policy)
374 prune_best_arch_name_version(Solver *solv, Pool *pool, Queue *plist)
376 if (solv && solv->bestSolvableCb)
377 { /* The application is responsible for */
378 return solv->bestSolvableCb(solv->pool, plist);
381 if (plist->count > 1)
382 prune_to_best_arch(pool, plist);
383 if (plist->count > 1)
384 prune_to_best_version(solv, plist);
389 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
391 Pool *pool = solv->pool;
392 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
393 prune_to_highest_prio(pool, plist);
394 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
395 prune_to_recommended(solv, plist);
396 prune_best_arch_name_version(solv, pool, plist);
401 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
403 Pool *pool = solv->pool;
404 Id a1 = s1->arch, a2 = s2->arch;
406 if (solv && solv->archCheckCb)
407 { /* The application is responsible for */
408 return solv->archCheckCb(solv->pool, s1, s2);
411 /* we allow changes to/from noarch */
412 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
416 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
417 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
418 if (((a1 ^ a2) & 0xffff0000) != 0)
424 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
426 Pool *pool = solv->pool;
427 Id vendormask1, vendormask2;
429 if (solv && solv->vendorCheckCb)
430 { /* The application is responsible for */
431 return solv->vendorCheckCb(solv->pool, s1, s2);
434 if (s1->vendor == s2->vendor)
436 vendormask1 = pool_vendor2mask(pool, s1->vendor);
439 vendormask2 = pool_vendor2mask(pool, s2->vendor);
440 if ((vendormask1 & vendormask2) == 0)
447 * find update candidates
449 * s: solvable to be updated
450 * qs: [out] queue to hold Ids of candidates
451 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
455 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
457 /* installed packages get a special upgrade allowed rule */
458 Pool *pool = solv->pool;
459 Id p, pp, n, p2, pp2;
465 if (solv && solv->updateCandidateCb)
466 { /* The application is responsible for */
467 return solv->updateCandidateCb(solv->pool, s, qs);
474 n = s - pool->solvables;
477 * look for updates for s
479 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
481 if (p == n) /* skip itself */
484 ps = pool->solvables + p;
485 if (s->name == ps->name) /* name match */
487 if (!allow_all && !solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
490 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
492 obsp = ps->repo->idarraydata + ps->obsoletes;
493 while ((obs = *obsp++) != 0) /* for all obsoletes */
495 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
497 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
499 if (p2 == n) /* match ! */
505 if (!obs) /* continue if no match */
507 /* here we have 'p' with a matching provides/obsoletes combination
508 * thus flagging p as a valid update candidate for s
513 if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
515 if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
519 /* if we have found some valid candidates and noupdateprovide is not set, we're
520 done. otherwise we fallback to all obsoletes */
521 if (!solv->noupdateprovide && qs->count)
523 if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
526 for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
528 ps = pool->solvables + p;
529 if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
531 if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))