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 Pool *prune_best_version_arch_sortcmp_data;
27 /*-----------------------------------------------------------------*/
30 * prep for prune_best_version_arch
35 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
37 Pool *pool = prune_best_version_arch_sortcmp_data;
41 r = pool->solvables[a].name - pool->solvables[b].name;
45 /* different names. We use real strcmp here so that the result
46 * is not depending on some random solvable order */
47 na = id2str(pool, pool->solvables[a].name);
48 nb = id2str(pool, pool->solvables[b].name);
49 /* bring selections and patterns to the front */
50 if (!strncmp(na, "pattern:", 8))
52 if (strncmp(nb, "pattern:", 8))
55 else if (!strncmp(nb, "pattern:", 8))
57 if (strncmp(na, "pattern:", 8))
60 if (!strncmp(na, "selection:", 10))
62 if (strncmp(nb, "selection:", 10))
65 else if (!strncmp(nb, "selection:", 10))
67 if (strncmp(na, "selection:", 10))
70 return strcmp(na, nb);
76 prune_to_highest_prio(Pool *pool, Queue *plist)
82 /* prune to highest priority */
83 for (i = 0; i < plist->count; i++)
85 s = pool->solvables + plist->elements[i];
86 if (i == 0 || s->repo->priority > bestprio)
87 bestprio = s->repo->priority;
89 for (i = j = 0; i < plist->count; i++)
91 s = pool->solvables + plist->elements[i];
92 if (s->repo->priority == bestprio)
93 plist->elements[j++] = plist->elements[i];
99 * prune_to_recommended
101 * XXX: should we prune to requires/suggests that are already
102 * fulfilled by other packages?
105 prune_to_recommended(Solver *solv, Queue *plist)
107 Pool *pool = solv->pool;
110 Id p, *pp, rec, *recp, sug, *sugp;
112 if (solv->recommends_index < 0)
114 MAPZERO(&solv->recommendsmap);
115 MAPZERO(&solv->suggestsmap);
116 solv->recommends_index = 0;
118 while (solv->recommends_index < solv->decisionq.count)
120 p = solv->decisionq.elements[solv->recommends_index++];
123 s = pool->solvables + p;
126 recp = s->repo->idarraydata + s->recommends;
127 while ((rec = *recp++) != 0)
128 FOR_PROVIDES(p, pp, rec)
129 MAPSET(&solv->recommendsmap, p);
133 sugp = s->repo->idarraydata + s->suggests;
134 while ((sug = *sugp++) != 0)
135 FOR_PROVIDES(p, pp, sug)
136 MAPSET(&solv->suggestsmap, p);
139 /* prune to recommended/supplemented */
140 for (i = j = 0; i < plist->count; i++)
142 p = plist->elements[i];
143 if (MAPTST(&solv->recommendsmap, p))
145 plist->elements[j++] = p;
148 if (solver_is_supplementing(solv, pool->solvables + p))
149 plist->elements[j++] = p;
154 /* prune to suggested/enhanced*/
155 if (plist->count < 2)
157 for (i = j = 0; i < plist->count; i++)
159 p = plist->elements[i];
160 if (MAPTST(&solv->suggestsmap, p))
162 plist->elements[j++] = p;
165 if (solver_is_enhancing(solv, pool->solvables + p))
166 plist->elements[j++] = p;
173 prune_to_best_arch(Pool *pool, Queue *plist)
179 if (!pool->id2arch || plist->count < 2)
182 for (i = 0; i < plist->count; i++)
184 s = pool->solvables + plist->elements[i];
186 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
187 if (a && a != 1 && (!bestscore || a < bestscore))
190 for (i = j = 0; i < plist->count; i++)
192 s = pool->solvables + plist->elements[i];
194 if (a > pool->lastarch)
196 a = pool->id2arch[a];
197 /* a == 1 -> noarch */
198 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
200 plist->elements[j++] = plist->elements[i];
207 * prune_best_version_arch
209 * sort list of packages (given through plist) by name and evr
210 * return result through plist
214 /* FIXME: should prefer installed if identical version */
217 prune_to_best_version(Pool *pool, Queue *plist)
223 if (plist->count < 2) /* no need to prune for a single entry */
225 POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
227 prune_best_version_arch_sortcmp_data = pool;
228 /* sort by name first */
229 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
231 /* delete obsoleted. hmm, looks expensive! */
232 /* FIXME maybe also check provides depending on noupdateprovide? */
233 /* FIXME do not prune cycles */
234 for (i = 0; i < plist->count; i++)
236 Id p, *pp, obs, *obsp;
237 s = pool->solvables + plist->elements[i];
240 obsp = s->repo->idarraydata + s->obsoletes;
241 while ((obs = *obsp++) != 0)
243 FOR_PROVIDES(p, pp, obs)
245 if (pool->solvables[p].name == s->name)
247 for (j = 0; j < plist->count; j++)
251 if (plist->elements[j] == p)
252 plist->elements[j] = 0;
257 for (i = j = 0; i < plist->count; i++)
258 if (plist->elements[i])
259 plist->elements[j++] = plist->elements[i];
262 /* now find best 'per name' */
263 for (i = j = 0; i < plist->count; i++)
265 s = pool->solvables + plist->elements[i];
267 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s\n", solvable2str(pool, s));
269 if (!best) /* if no best yet, the current is best */
271 best = plist->elements[i];
275 /* name switch: re-init */
276 if (pool->solvables[best].name != s->name) /* new name */
278 plist->elements[j++] = best; /* move old best to front */
279 best = plist->elements[i]; /* take current as new best */
283 if (pool->solvables[best].evr != s->evr) /* compare evr */
285 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_MATCH_RELEASE) < 0)
286 best = plist->elements[i];
291 best = plist->elements[0];
293 plist->elements[j++] = best;
297 /* legacy, do not use anymore! */
299 prune_best_version_arch(Pool *pool, Queue *plist)
301 if (plist->count > 1)
302 prune_to_best_arch(pool, plist);
303 if (plist->count > 1)
304 prune_to_best_version(pool, plist);
309 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
311 Pool *pool = solv->pool;
312 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
313 prune_to_highest_prio(pool, plist);
314 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
315 prune_to_recommended(solv, plist);
316 /* FIXME: do this different! */
318 queue_push(plist, inst);
319 if (plist->count > 1)
320 prune_to_best_arch(pool, plist);
321 if (plist->count > 1)
322 prune_to_best_version(pool, plist);
327 policy_illegal_archchange(Pool *pool, Solvable *s1, Solvable *s2)
329 Id a1 = s1->arch, a2 = s2->arch;
331 /* we allow changes to/from noarch */
332 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
336 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
337 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
338 if (((a1 ^ a2) & 0xffff0000) != 0)
344 policy_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
346 Id vendormask1, vendormask2;
347 if (s1->vendor == s2->vendor)
349 vendormask1 = pool_vendor2mask(pool, s1->vendor);
352 vendormask2 = pool_vendor2mask(pool, s2->vendor);
353 if ((vendormask1 & vendormask2) == 0)
359 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allowall)
361 /* installed packages get a special upgrade allowed rule */
362 Pool *pool = solv->pool;
363 Id p, *pp, n, p2, *pp2;
373 n = s - pool->solvables;
374 vendormask = pool_vendor2mask(pool, s->vendor);
377 * look for updates for s
379 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
381 if (p == n) /* skip itself */
384 ps = pool->solvables + p;
385 if (s->name == ps->name) /* name match */
389 if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_MATCH_RELEASE) > 0)
391 if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(pool, s, ps))
393 if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(pool, s, ps))
397 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
399 obsp = ps->repo->idarraydata + ps->obsoletes;
400 while ((obs = *obsp++) != 0) /* for all obsoletes */
402 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
404 if (p2 == n) /* match ! */
410 if (!obs) /* continue if no match */
412 /* here we have 'p' with a matching provides/obsoletes combination
413 * thus flagging p as a valid update candidate for s
420 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
422 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)