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)
66 /* prefer installed solvables first */
67 if (solv->installed && pool->solvables[a].repo == solv->installed)
69 if (solv->installed && 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 for (j = 0; j < plist->count; j++)
259 if (plist->elements[j] == p)
260 plist->elements[j] = 0;
265 for (i = j = 0; i < plist->count; i++)
266 if (plist->elements[i])
267 plist->elements[j++] = plist->elements[i];
270 /* now find best 'per name' */
271 for (i = j = 0; i < plist->count; i++)
273 s = pool->solvables + plist->elements[i];
275 POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
276 solvable2str(pool, s),
277 (solv->installed && s->repo == solv->installed) ? "installed" : "not installed");
279 if (!best) /* if no best yet, the current is best */
281 best = plist->elements[i];
285 /* name switch: re-init */
286 if (pool->solvables[best].name != s->name) /* new name */
288 plist->elements[j++] = best; /* move old best to front */
289 best = plist->elements[i]; /* take current as new best */
293 if (pool->solvables[best].evr != s->evr) /* compare evr */
295 if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_MATCH_RELEASE) < 0)
296 best = plist->elements[i];
301 best = plist->elements[0];
303 plist->elements[j++] = best;
308 /* legacy, do not use anymore!
309 * (rates arch higher than version, but thats a policy)
313 prune_best_arch_name_version(Solver *solv, Pool *pool, Queue *plist)
315 if (solv && solv->bestSolvableCb)
316 { /* The application is responsible for */
317 return solv->bestSolvableCb (solv->pool, plist);
320 if (plist->count > 1)
321 prune_to_best_arch(pool, plist);
322 if (plist->count > 1)
323 prune_to_best_version(solv, plist);
328 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
330 Pool *pool = solv->pool;
331 if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
332 prune_to_highest_prio(pool, plist);
333 if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
334 prune_to_recommended(solv, plist);
335 /* FIXME: do this different! */
337 queue_push(plist, inst);
339 prune_best_arch_name_version (solv, pool, plist);
344 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
346 Pool *pool = solv->pool;
347 Id a1 = s1->arch, a2 = s2->arch;
349 if (solv && solv->archCheckCb)
350 { /* The application is responsible for */
351 return solv->archCheckCb (solv->pool, s1, s2);
354 /* we allow changes to/from noarch */
355 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
359 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
360 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
361 if (((a1 ^ a2) & 0xffff0000) != 0)
367 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
369 Pool *pool = solv->pool;
370 Id vendormask1, vendormask2;
372 if (solv && solv->vendorCheckCb)
373 { /* The application is responsible for */
374 return solv->vendorCheckCb (solv->pool, s1, s2);
377 if (s1->vendor == s2->vendor)
379 vendormask1 = pool_vendor2mask(pool, s1->vendor);
382 vendormask2 = pool_vendor2mask(pool, s2->vendor);
383 if ((vendormask1 & vendormask2) == 0)
389 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allowall)
391 /* installed packages get a special upgrade allowed rule */
392 Pool *pool = solv->pool;
393 Id p, *pp, n, p2, *pp2;
400 if (solv && solv->updateCandidateCb)
401 { /* The application is responsible for */
402 return solv->updateCandidateCb (solv->pool, s, qs);
409 n = s - pool->solvables;
410 vendormask = pool_vendor2mask(pool, s->vendor);
413 * look for updates for s
415 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
417 if (p == n) /* skip itself */
420 ps = pool->solvables + p;
421 if (s->name == ps->name) /* name match */
425 if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_MATCH_RELEASE) > 0)
427 if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
429 if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
433 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
435 obsp = ps->repo->idarraydata + ps->obsoletes;
436 while ((obs = *obsp++) != 0) /* for all obsoletes */
438 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
440 if (p2 == n) /* match ! */
446 if (!obs) /* continue if no match */
448 /* here we have 'p' with a matching provides/obsoletes combination
449 * thus flagging p as a valid update candidate for s
456 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
458 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)