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 /* XXX: no longer needed? */
52 if (!strncmp(na, "pattern:", 8))
54 if (strncmp(nb, "pattern:", 8))
57 else if (!strncmp(nb, "pattern:", 8))
59 if (strncmp(na, "pattern:", 8))
62 return strcmp(na, nb);
65 if (pool->solvables[a].evr == pool->solvables[b].evr && solv->installed)
67 /* prefer installed solvables */
68 if (pool->solvables[a].repo == solv->installed)
70 if (pool->solvables[b].repo == solv->installed)
78 * prune to repository with highest priority
83 prune_to_highest_prio(Pool *pool, Queue *plist)
89 /* prune to highest priority */
90 for (i = 0; i < plist->count; i++) /* find highest prio in queue */
92 s = pool->solvables + plist->elements[i];
93 if (i == 0 || s->repo->priority > bestprio)
94 bestprio = s->repo->priority;
96 for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
98 s = pool->solvables + plist->elements[i];
99 if (s->repo->priority == bestprio)
100 plist->elements[j++] = plist->elements[i];
107 * prune_to_recommended
109 * XXX: should we prune to requires/suggests that are already
110 * fulfilled by other packages?
114 prune_to_recommended(Solver *solv, Queue *plist)
116 Pool *pool = solv->pool;
119 Id p, *pp, rec, *recp, sug, *sugp;
121 if (solv->recommends_index < 0)
123 MAPZERO(&solv->recommendsmap);
124 MAPZERO(&solv->suggestsmap);
125 solv->recommends_index = 0;
127 while (solv->recommends_index < solv->decisionq.count)
129 p = solv->decisionq.elements[solv->recommends_index++];
132 s = pool->solvables + p;
135 recp = s->repo->idarraydata + s->recommends;
136 while ((rec = *recp++) != 0)
137 FOR_PROVIDES(p, pp, rec)
138 MAPSET(&solv->recommendsmap, p);
142 sugp = s->repo->idarraydata + s->suggests;
143 while ((sug = *sugp++) != 0)
144 FOR_PROVIDES(p, pp, sug)
145 MAPSET(&solv->suggestsmap, p);
148 /* prune to recommended/supplemented */
149 for (i = j = 0; i < plist->count; i++)
151 p = plist->elements[i];
152 if (MAPTST(&solv->recommendsmap, p))
154 plist->elements[j++] = p;
157 if (solver_is_supplementing(solv, pool->solvables + p))
158 plist->elements[j++] = p;
163 /* prune to suggested/enhanced*/
164 if (plist->count < 2)
166 for (i = j = 0; i < plist->count; i++)
168 p = plist->elements[i];
169 if (MAPTST(&solv->suggestsmap, p))
171 plist->elements[j++] = p;
174 if (solver_is_enhancing(solv, pool->solvables + p))
175 plist->elements[j++] = p;
182 prune_to_best_arch(Pool *pool, Queue *plist)
188 if (!pool->id2arch || plist->count < 2)
191 for (i = 0; i < plist->count; i++)
193 s = pool->solvables + plist->elements[i];
195 a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
196 if (a && a != 1 && (!bestscore || a < bestscore))
199 for (i = j = 0; i < plist->count; i++)
201 s = pool->solvables + plist->elements[i];
203 if (a > pool->lastarch)
205 a = pool->id2arch[a];
206 /* a == 1 -> noarch */
207 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
209 plist->elements[j++] = plist->elements[i];
216 * prune_best_version_arch
218 * sort list of packages (given through plist) by name and evr
219 * return result through plist
224 prune_to_best_version(Solver *solv, Queue *plist)
226 Pool *pool = solv->pool;
231 if (plist->count < 2) /* no need to prune for a single entry */
233 POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
235 prune_best_version_arch_sortcmp_data = solv;
236 /* sort by name first, prefer installed */
237 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
239 /* delete obsoleted. hmm, looks expensive! */
240 /* FIXME maybe also check provides depending on noupdateprovide? */
241 /* FIXME do not prune cycles */
242 for (i = 0; i < plist->count; i++)
244 Id p, *pp, obs, *obsp;
245 s = pool->solvables + plist->elements[i];
248 obsp = s->repo->idarraydata + s->obsoletes;
249 while ((obs = *obsp++) != 0)
251 FOR_PROVIDES(p, pp, obs)
253 if (pool->solvables[p].name == s->name)
255 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
257 for (j = 0; j < plist->count; j++)
261 if (plist->elements[j] == p)
262 plist->elements[j] = 0;
267 for (i = j = 0; i < plist->count; i++)
268 if (plist->elements[i])
269 plist->elements[j++] = plist->elements[i];
272 /* now find best 'per name' */
273 for (i = j = 0; i < plist->count; i++)
275 s = pool->solvables + plist->elements[i];
277 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
278 solvable2str(pool, s),
279 (solv->installed && s->repo == solv->installed) ? "installed" : "not installed");
281 if (!best) /* if no best yet, the current is best */
283 best = plist->elements[i];
287 /* name switch: re-init */
288 if (pool->solvables[best].name != s->name) /* new name */
290 plist->elements[j++] = best; /* move old best to front */
291 best = plist->elements[i]; /* take current as new best */
295 if (pool->solvables[best].evr != s->evr) /* compare evr */
297 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_MATCH_RELEASE) < 0)
298 best = plist->elements[i];
303 best = plist->elements[0];
305 plist->elements[j++] = best;
310 /* legacy, do not use anymore!
311 * (rates arch higher than version, but thats a policy)
315 prune_best_arch_name_version(Solver *solv, Pool *pool, Queue *plist)
317 if (solv && solv->bestSolvableCb)
318 { /* The application is responsible for */
319 return solv->bestSolvableCb(solv->pool, plist);
322 if (plist->count > 1)
323 prune_to_best_arch(pool, plist);
324 if (plist->count > 1)
325 prune_to_best_version(solv, plist);
330 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
332 Pool *pool = solv->pool;
333 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
334 prune_to_highest_prio(pool, plist);
335 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
336 prune_to_recommended(solv, plist);
337 /* FIXME: do this different! */
339 queue_push(plist, inst);
341 prune_best_arch_name_version(solv, pool, plist);
346 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
348 Pool *pool = solv->pool;
349 Id a1 = s1->arch, a2 = s2->arch;
351 if (solv && solv->archCheckCb)
352 { /* The application is responsible for */
353 return solv->archCheckCb(solv->pool, s1, s2);
356 /* we allow changes to/from noarch */
357 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
361 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
362 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
363 if (((a1 ^ a2) & 0xffff0000) != 0)
369 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
371 Pool *pool = solv->pool;
372 Id vendormask1, vendormask2;
374 if (solv && solv->vendorCheckCb)
375 { /* The application is responsible for */
376 return solv->vendorCheckCb(solv->pool, s1, s2);
379 if (s1->vendor == s2->vendor)
381 vendormask1 = pool_vendor2mask(pool, s1->vendor);
384 vendormask2 = pool_vendor2mask(pool, s2->vendor);
385 if ((vendormask1 & vendormask2) == 0)
392 * find update candidates
394 * s: solvable to be updated
395 * qs: [out] queue to hold Ids of candidates
396 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
400 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
402 /* installed packages get a special upgrade allowed rule */
403 Pool *pool = solv->pool;
404 Id p, *pp, n, p2, *pp2;
411 if (solv && solv->updateCandidateCb)
412 { /* The application is responsible for */
413 return solv->updateCandidateCb(solv->pool, s, qs);
420 n = s - pool->solvables;
421 vendormask = pool_vendor2mask(pool, s->vendor);
424 * look for updates for s
426 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
428 if (p == n) /* skip itself */
431 ps = pool->solvables + p;
432 if (s->name == ps->name) /* name match */
436 if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_MATCH_RELEASE) > 0)
438 if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
440 if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
444 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
446 obsp = ps->repo->idarraydata + ps->obsoletes;
447 while ((obs = *obsp++) != 0) /* for all obsoletes */
449 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
451 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
453 if (p2 == n) /* match ! */
459 if (!obs) /* continue if no match */
461 /* here we have 'p' with a matching provides/obsoletes combination
462 * thus flagging p as a valid update candidate for s
469 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
471 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)