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_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 Solver *solv = prune_best_version_arch_sortcmp_data;
38 Pool *pool = solv->pool;
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 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 return strcmp(na, nb);
64 if (pool->solvables[a].evr == pool->solvables[b].evr && solv->installed)
66 /* prefer installed solvables */
67 if (pool->solvables[a].repo == solv->installed)
69 if (pool->solvables[b].repo == solv->installed)
77 * prune to repository with highest priority
82 prune_to_highest_prio(Pool *pool, Queue *plist)
88 /* prune to highest priority */
89 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
91 s = pool->solvables + plist->elements[i];
92 if (i == 0 || s->repo->priority > bestprio)
93 bestprio = s->repo->priority;
95 for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
97 s = pool->solvables + plist->elements[i];
98 if (s->repo->priority == bestprio)
99 plist->elements[j++] = plist->elements[i];
106 * prune_to_recommended
108 * XXX: should we prune to requires/suggests that are already
109 * fulfilled by other packages?
113 prune_to_recommended(Solver *solv, Queue *plist)
115 Pool *pool = solv->pool;
118 Id p, *pp, rec, *recp, sug, *sugp;
120 if (solv->recommends_index < 0)
122 MAPZERO(&solv->recommendsmap);
123 MAPZERO(&solv->suggestsmap);
124 solv->recommends_index = 0;
126 while (solv->recommends_index < solv->decisionq.count)
128 p = solv->decisionq.elements[solv->recommends_index++];
131 s = pool->solvables + p;
134 recp = s->repo->idarraydata + s->recommends;
135 while ((rec = *recp++) != 0)
136 FOR_PROVIDES(p, pp, rec)
137 MAPSET(&solv->recommendsmap, p);
141 sugp = s->repo->idarraydata + s->suggests;
142 while ((sug = *sugp++) != 0)
143 FOR_PROVIDES(p, pp, sug)
144 MAPSET(&solv->suggestsmap, p);
147 /* prune to recommended/supplemented */
148 for (i = j = 0; i < plist->count; i++)
150 p = plist->elements[i];
151 if (MAPTST(&solv->recommendsmap, p))
153 plist->elements[j++] = p;
156 if (solver_is_supplementing(solv, pool->solvables + p))
157 plist->elements[j++] = p;
162 /* prune to suggested/enhanced*/
163 if (plist->count < 2)
165 for (i = j = 0; i < plist->count; i++)
167 p = plist->elements[i];
168 if (MAPTST(&solv->suggestsmap, p))
170 plist->elements[j++] = p;
173 if (solver_is_enhancing(solv, pool->solvables + p))
174 plist->elements[j++] = p;
181 prune_to_best_arch(Pool *pool, Queue *plist)
187 if (!pool->id2arch || plist->count < 2)
190 for (i = 0; i < plist->count; i++)
192 s = pool->solvables + plist->elements[i];
194 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
195 if (a && a != 1 && (!bestscore || a < bestscore))
198 for (i = j = 0; i < plist->count; i++)
200 s = pool->solvables + plist->elements[i];
202 if (a > pool->lastarch)
204 a = pool->id2arch[a];
205 /* a == 1 -> noarch */
206 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
208 plist->elements[j++] = plist->elements[i];
215 * prune_best_version_arch
217 * sort list of packages (given through plist) by name and evr
218 * return result through plist
223 prune_to_best_version(Solver *solv, Queue *plist)
225 Pool *pool = solv->pool;
230 if (plist->count < 2) /* no need to prune for a single entry */
232 POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
234 prune_best_version_arch_sortcmp_data = solv;
235 /* sort by name first, prefer installed */
236 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
238 /* delete obsoleted. hmm, looks expensive! */
239 /* FIXME maybe also check provides depending on noupdateprovide? */
240 /* FIXME do not prune cycles */
241 for (i = 0; i < plist->count; i++)
243 Id p, *pp, obs, *obsp;
244 s = pool->solvables + plist->elements[i];
247 obsp = s->repo->idarraydata + s->obsoletes;
248 while ((obs = *obsp++) != 0)
250 FOR_PROVIDES(p, pp, obs)
252 if (pool->solvables[p].name == s->name)
254 for (j = 0; j < plist->count; j++)
258 if (plist->elements[j] == p)
259 plist->elements[j] = 0;
264 for (i = j = 0; i < plist->count; i++)
265 if (plist->elements[i])
266 plist->elements[j++] = plist->elements[i];
269 /* now find best 'per name' */
270 for (i = j = 0; i < plist->count; i++)
272 s = pool->solvables + plist->elements[i];
274 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
275 solvable2str(pool, s),
276 (solv->installed && s->repo == solv->installed) ? "installed" : "not installed");
278 if (!best) /* if no best yet, the current is best */
280 best = plist->elements[i];
284 /* name switch: re-init */
285 if (pool->solvables[best].name != s->name) /* new name */
287 plist->elements[j++] = best; /* move old best to front */
288 best = plist->elements[i]; /* take current as new best */
292 if (pool->solvables[best].evr != s->evr) /* compare evr */
294 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_MATCH_RELEASE) < 0)
295 best = plist->elements[i];
300 best = plist->elements[0];
302 plist->elements[j++] = best;
307 /* legacy, do not use anymore!
308 * (rates arch higher than version, but thats a policy)
312 prune_best_arch_name_version(Solver *solv, Pool *pool, Queue *plist)
314 if (solv && solv->bestSolvableCb)
315 { /* The application is responsible for */
316 return solv->bestSolvableCb (solv->pool, plist);
319 if (plist->count > 1)
320 prune_to_best_arch(pool, plist);
321 if (plist->count > 1)
322 prune_to_best_version(solv, plist);
327 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
329 Pool *pool = solv->pool;
330 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
331 prune_to_highest_prio(pool, plist);
332 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
333 prune_to_recommended(solv, plist);
334 /* FIXME: do this different! */
336 queue_push(plist, inst);
338 prune_best_arch_name_version (solv, pool, plist);
343 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
345 Pool *pool = solv->pool;
346 Id a1 = s1->arch, a2 = s2->arch;
348 if (solv && solv->archCheckCb)
349 { /* The application is responsible for */
350 return solv->archCheckCb (solv->pool, s1, s2);
353 /* we allow changes to/from noarch */
354 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
358 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
359 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
360 if (((a1 ^ a2) & 0xffff0000) != 0)
366 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
368 Pool *pool = solv->pool;
369 Id vendormask1, vendormask2;
371 if (solv && solv->vendorCheckCb)
372 { /* The application is responsible for */
373 return solv->vendorCheckCb (solv->pool, s1, s2);
376 if (s1->vendor == s2->vendor)
378 vendormask1 = pool_vendor2mask(pool, s1->vendor);
381 vendormask2 = pool_vendor2mask(pool, s2->vendor);
382 if ((vendormask1 & vendormask2) == 0)
388 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allowall)
390 /* installed packages get a special upgrade allowed rule */
391 Pool *pool = solv->pool;
392 Id p, *pp, n, p2, *pp2;
399 if (solv && solv->updateCandidateCb)
400 { /* The application is responsible for */
401 return solv->updateCandidateCb (solv->pool, s, qs);
408 n = s - pool->solvables;
409 vendormask = pool_vendor2mask(pool, s->vendor);
412 * look for updates for s
414 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
416 if (p == n) /* skip itself */
419 ps = pool->solvables + p;
420 if (s->name == ps->name) /* name match */
424 if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_MATCH_RELEASE) > 0)
426 if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
428 if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
432 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
434 obsp = ps->repo->idarraydata + ps->obsoletes;
435 while ((obs = *obsp++) != 0) /* for all obsoletes */
437 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
439 if (p2 == n) /* match ! */
445 if (!obs) /* continue if no match */
447 /* here we have 'p' with a matching provides/obsoletes combination
448 * thus flagging p as a valid update candidate for s
455 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
457 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)