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"
23 #include "sat_debug.h"
26 static Pool *prune_best_version_arch_sortcmp_data;
28 /*-----------------------------------------------------------------*/
31 * prep for prune_best_version_arch
36 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
38 Pool *pool = prune_best_version_arch_sortcmp_data;
42 r = pool->solvables[a].name - pool->solvables[b].name;
46 /* different names. We use real strcmp here so that the result
47 * is not depending on some random solvable order */
48 na = id2str(pool, pool->solvables[a].name);
49 nb = id2str(pool, pool->solvables[b].name);
50 /* bring selections and patterns to the front */
51 if (!strncmp(na, "pattern:", 8))
53 if (strncmp(nb, "pattern:", 8))
56 else if (!strncmp(nb, "pattern:", 8))
58 if (strncmp(na, "pattern:", 8))
61 if (!strncmp(na, "selection:", 10))
63 if (strncmp(nb, "selection:", 10))
66 else if (!strncmp(nb, "selection:", 10))
68 if (strncmp(na, "selection:", 10))
71 return strcmp(na, nb);
77 prune_to_highest_prio(Pool *pool, Queue *plist)
83 /* prune to highest priority */
84 for (i = 0; i < plist->count; i++)
86 s = pool->solvables + plist->elements[i];
87 if (i == 0 || s->repo->priority > bestprio)
88 bestprio = s->repo->priority;
90 for (i = j = 0; i < plist->count; i++)
92 s = pool->solvables + plist->elements[i];
93 if (s->repo->priority == bestprio)
94 plist->elements[j++] = plist->elements[i];
100 * prune_to_recommended
102 * XXX: should we prune to requires/suggests that are already
103 * fulfilled by other packages?
106 prune_to_recommended(Solver *solv, Queue *plist)
108 Pool *pool = solv->pool;
111 Id p, *pp, rec, *recp, sug, *sugp;
113 if (solv->recommends_index < 0)
115 MAPZERO(&solv->recommendsmap);
116 MAPZERO(&solv->suggestsmap);
117 solv->recommends_index = 0;
119 while (solv->recommends_index < solv->decisionq.count)
121 p = solv->decisionq.elements[solv->recommends_index++];
124 s = pool->solvables + p;
127 recp = s->repo->idarraydata + s->recommends;
128 while ((rec = *recp++) != 0)
129 FOR_PROVIDES(p, pp, rec)
130 MAPSET(&solv->recommendsmap, p);
134 sugp = s->repo->idarraydata + s->suggests;
135 while ((sug = *sugp++) != 0)
136 FOR_PROVIDES(p, pp, sug)
137 MAPSET(&solv->suggestsmap, p);
140 /* prune to recommended/supplemented */
141 for (i = j = 0; i < plist->count; i++)
143 p = plist->elements[i];
144 if (MAPTST(&solv->recommendsmap, p))
146 plist->elements[j++] = p;
149 if (solver_is_supplementing(solv, pool->solvables + p))
150 plist->elements[j++] = p;
155 /* prune to suggested/enhanced*/
156 if (plist->count < 2)
158 for (i = j = 0; i < plist->count; i++)
160 p = plist->elements[i];
161 if (MAPTST(&solv->suggestsmap, p))
163 plist->elements[j++] = p;
166 if (solver_is_enhancing(solv, pool->solvables + p))
167 plist->elements[j++] = p;
174 prune_to_best_arch(Pool *pool, Queue *plist)
180 if (!pool->id2arch || plist->count < 2)
183 for (i = 0; i < plist->count; i++)
185 s = pool->solvables + plist->elements[i];
187 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
188 if (a && a != 1 && (!bestscore || a < bestscore))
191 for (i = j = 0; i < plist->count; i++)
193 s = pool->solvables + plist->elements[i];
195 if (a > pool->lastarch)
197 a = pool->id2arch[a];
198 /* a == 1 -> noarch */
199 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
201 plist->elements[j++] = plist->elements[i];
208 * prune_best_version_arch
210 * sort list of packages (given through plist) by name and evr
211 * return result through plist
215 /* FIXME: should prefer installed if identical version */
218 prune_to_best_version(Pool *pool, Queue *plist)
224 if (plist->count < 2) /* no need to prune for a single entry */
226 sat_debug (DEBUG_2, "prune_to_best_version %d\n", plist->count);
228 /* prune to best architecture */
231 prune_best_version_arch_sortcmp_data = pool;
232 /* sort by name first */
233 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
235 /* delete obsoleted. hmm, looks expensive! */
236 /* FIXME maybe also check provides depending on noupdateprovide? */
237 /* FIXME do not prune cycles */
238 for (i = 0; i < plist->count; i++)
240 Id p, *pp, obs, *obsp;
241 s = pool->solvables + plist->elements[i];
244 obsp = s->repo->idarraydata + s->obsoletes;
245 while ((obs = *obsp++) != 0)
247 FOR_PROVIDES(p, pp, obs)
249 if (pool->solvables[p].name == s->name)
251 for (j = 0; j < plist->count; j++)
255 if (plist->elements[j] == p)
256 plist->elements[j] = 0;
261 for (i = j = 0; i < plist->count; i++)
262 if (plist->elements[i])
263 plist->elements[j++] = plist->elements[i];
266 /* now find best 'per name' */
267 for (i = j = 0; i < plist->count; i++)
269 s = pool->solvables + plist->elements[i];
271 sat_debug (DEBUG_2, "- %s\n", solvable2str(pool, s));
273 if (!best) /* if no best yet, the current is best */
275 best = plist->elements[i];
279 /* name switch: re-init */
280 if (pool->solvables[best].name != s->name) /* new name */
282 plist->elements[j++] = best; /* move old best to front */
283 best = plist->elements[i]; /* take current as new best */
287 if (pool->solvables[best].evr != s->evr) /* compare evr */
289 if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
290 best = plist->elements[i];
295 best = plist->elements[0];
297 plist->elements[j++] = best;
302 prune_best_version_arch(Pool *pool, Queue *plist)
304 if (plist->count > 1)
305 prune_to_best_arch(pool, plist);
306 if (plist->count > 1)
307 prune_to_best_version(pool, plist);
312 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
314 Pool *pool = solv->pool;
315 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
316 prune_to_highest_prio(pool, plist);
317 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
318 prune_to_recommended(solv, plist);
319 /* FIXME: do this different! */
321 queue_push(plist, inst);
322 if (plist->count > 1)
323 prune_to_best_arch(pool, plist);
324 if (plist->count > 1)
325 prune_to_best_version(pool, plist);
330 policy_illegal_archchange(Pool *pool, Solvable *s1, Solvable *s2)
332 Id a1 = s1->arch, a2 = s2->arch;
334 /* we allow changes to/from noarch */
335 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
339 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
340 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
341 if (((a1 ^ a2) & 0xffff0000) != 0)
347 policy_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
349 Id vendormask1, vendormask2;
350 if (s1->vendor == s2->vendor)
352 vendormask1 = pool_vendor2mask(pool, s1->vendor);
355 vendormask2 = pool_vendor2mask(pool, s2->vendor);
356 if ((vendormask1 & vendormask2) == 0)
362 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allowall)
364 /* installed packages get a special upgrade allowed rule */
365 Pool *pool = solv->pool;
366 Id p, *pp, n, p2, *pp2;
376 n = s - pool->solvables;
377 vendormask = pool_vendor2mask(pool, s->vendor);
380 * look for updates for s
382 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
384 if (p == n) /* skip itself */
387 ps = pool->solvables + p;
388 if (s->name == ps->name) /* name match */
392 if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr) > 0)
394 if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(pool, s, ps))
396 if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(pool, s, ps))
400 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
402 obsp = ps->repo->idarraydata + ps->obsoletes;
403 while ((obs = *obsp++) != 0) /* for all obsoletes */
405 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
407 if (p2 == n) /* match ! */
413 if (!obs) /* continue if no match */
415 /* here we have 'p' with a matching provides/obsoletes combination
416 * thus flagging p as a valid update candidate for s
423 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
425 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)