remove superfluous Pool argument in policy_illegal_ functions
[platform/upstream/libsolv.git] / src / policy.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * Generic policy interface for SAT solver
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include "solver.h"
19 #include "evr.h"
20 #include "policy.h"
21 #include "poolvendor.h"
22 #include "poolarch.h"
23
24
25 static Pool *prune_best_version_arch_sortcmp_data;
26
27 /*-----------------------------------------------------------------*/
28
29 /*
30  * prep for prune_best_version_arch
31  *   sort by name
32  */
33
34 static int
35 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
36 {
37   Pool *pool = prune_best_version_arch_sortcmp_data;
38   int r;
39   Id a = *(Id *)ap;
40   Id b = *(Id *)bp;
41   r = pool->solvables[a].name - pool->solvables[b].name;
42   if (r)
43     {
44       const char *na, *nb;
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))
51         {
52           if (strncmp(nb, "pattern:", 8))
53             return -1;
54         }
55       else if (!strncmp(nb, "pattern:", 8))
56         {
57           if (strncmp(na, "pattern:", 8))
58             return 1;
59         }
60       if (!strncmp(na, "selection:", 10))
61         {
62           if (strncmp(nb, "selection:", 10))
63             return -1;
64         }
65       else if (!strncmp(nb, "selection:", 10))
66         {
67           if (strncmp(na, "selection:", 10))
68             return 1;
69         }
70       return strcmp(na, nb);
71     }
72   return a - b;
73 }
74
75 static void
76 prune_to_highest_prio(Pool *pool, Queue *plist)
77 {
78   int i, j;
79   Solvable *s;
80   int bestprio = 0;
81
82   /* prune to highest priority */
83   for (i = 0; i < plist->count; i++)
84     {
85       s = pool->solvables + plist->elements[i];
86       if (i == 0 || s->repo->priority > bestprio)
87         bestprio = s->repo->priority;
88     }
89   for (i = j = 0; i < plist->count; i++)
90     {
91       s = pool->solvables + plist->elements[i];
92       if (s->repo->priority == bestprio)
93         plist->elements[j++] = plist->elements[i];
94     }
95   plist->count = j;
96 }
97
98 /*
99  * prune_to_recommended
100  *
101  * XXX: should we prune to requires/suggests that are already
102  * fulfilled by other packages?
103  */
104 static void
105 prune_to_recommended(Solver *solv, Queue *plist)
106 {
107   Pool *pool = solv->pool;
108   int i, j;
109   Solvable *s;
110   Id p, *pp, rec, *recp, sug, *sugp;
111
112   if (solv->recommends_index < 0)
113     {
114       MAPZERO(&solv->recommendsmap);
115       MAPZERO(&solv->suggestsmap);
116       solv->recommends_index = 0;
117     }
118   while (solv->recommends_index < solv->decisionq.count)
119     {
120       p = solv->decisionq.elements[solv->recommends_index++];
121       if (p < 0)
122         continue;
123       s = pool->solvables + p;
124       if (s->recommends)
125         {
126           recp = s->repo->idarraydata + s->recommends;
127           while ((rec = *recp++) != 0)
128             FOR_PROVIDES(p, pp, rec)
129               MAPSET(&solv->recommendsmap, p);
130         }
131       if (s->suggests)
132         {
133           sugp = s->repo->idarraydata + s->suggests;
134           while ((sug = *sugp++) != 0)
135             FOR_PROVIDES(p, pp, sug)
136               MAPSET(&solv->suggestsmap, p);
137         }
138     }
139   /* prune to recommended/supplemented */
140   for (i = j = 0; i < plist->count; i++)
141     {
142       p = plist->elements[i];
143       if (MAPTST(&solv->recommendsmap, p))
144         {
145           plist->elements[j++] = p;
146           continue;
147         }
148       if (solver_is_supplementing(solv, pool->solvables + p))
149         plist->elements[j++] = p;
150     }
151   if (j)
152     plist->count = j;
153
154   /* prune to suggested/enhanced*/
155   if (plist->count < 2)
156     return;
157   for (i = j = 0; i < plist->count; i++)
158     {
159       p = plist->elements[i];
160       if (MAPTST(&solv->suggestsmap, p))
161         {
162           plist->elements[j++] = p;
163           continue;
164         }
165       if (solver_is_enhancing(solv, pool->solvables + p))
166         plist->elements[j++] = p;
167     }
168   if (j)
169     plist->count = j;
170 }
171
172 static void
173 prune_to_best_arch(Pool *pool, Queue *plist)
174 {
175   Id a, bestscore;
176   Solvable *s;
177   int i, j;
178
179   if (!pool->id2arch || plist->count < 2)
180     return;
181   bestscore = 0;
182   for (i = 0; i < plist->count; i++)
183     {
184       s = pool->solvables + plist->elements[i];
185       a = s->arch;
186       a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
187       if (a && a != 1 && (!bestscore || a < bestscore))
188         bestscore = a;
189     }
190   for (i = j = 0; i < plist->count; i++)
191     {
192       s = pool->solvables + plist->elements[i];
193       a = s->arch;
194       if (a > pool->lastarch)
195         continue;
196       a = pool->id2arch[a];
197       /* a == 1 -> noarch */
198       if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
199         continue;
200       plist->elements[j++] = plist->elements[i];
201     }
202   if (j)
203     plist->count = j;
204 }
205
206 /*
207  * prune_best_version_arch
208  *
209  * sort list of packages (given through plist) by name and evr
210  * return result through plist
211  *
212  */
213
214 /* FIXME: should prefer installed if identical version */
215
216 static void
217 prune_to_best_version(Pool *pool, Queue *plist)
218 {
219   Id best = ID_NULL;
220   int i, j;
221   Solvable *s;
222
223   if (plist->count < 2)         /* no need to prune for a single entry */
224     return;
225   POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
226
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);
230
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++)
235     {
236       Id p, *pp, obs, *obsp;
237       s = pool->solvables + plist->elements[i];
238       if (!s->obsoletes)
239         continue;
240       obsp = s->repo->idarraydata + s->obsoletes;
241       while ((obs = *obsp++) != 0)
242         {
243           FOR_PROVIDES(p, pp, obs)
244             {
245               if (pool->solvables[p].name == s->name)
246                 continue;
247               for (j = 0; j < plist->count; j++)
248                 {
249                   if (i == j)
250                     continue;
251                   if (plist->elements[j] == p)
252                     plist->elements[j] = 0;
253                 }
254             }
255         }
256     }
257   for (i = j = 0; i < plist->count; i++)
258     if (plist->elements[i])
259       plist->elements[j++] = plist->elements[i];
260   plist->count = j;
261
262   /* now find best 'per name' */
263   for (i = j = 0; i < plist->count; i++)
264     {
265       s = pool->solvables + plist->elements[i];
266
267       POOL_DEBUG(SAT_DEBUG_POLICY, "- %s\n", solvable2str(pool, s));
268
269       if (!best)                       /* if no best yet, the current is best */
270         {
271           best = plist->elements[i];
272           continue;
273         }
274
275       /* name switch: re-init */
276       if (pool->solvables[best].name != s->name)   /* new name */
277         {
278           plist->elements[j++] = best; /* move old best to front */
279           best = plist->elements[i];   /* take current as new best */
280           continue;
281         }
282
283       if (pool->solvables[best].evr != s->evr)   /* compare evr */
284         {
285           if (evrcmp(pool, pool->solvables[best].evr, s->evr, EVRCMP_MATCH_RELEASE) < 0)
286             best = plist->elements[i];
287         }
288     }
289
290   if (best == ID_NULL)
291     best = plist->elements[0];
292
293   plist->elements[j++] = best;
294   plist->count = j;
295 }
296
297 /* legacy, do not use anymore! */
298 void
299 prune_best_version_arch(Solver *solv, Pool *pool, Queue *plist)
300 {
301   if (solv && solv->bestSolvableCb)
302      {   /* The application is responsible for */
303          return solv->bestSolvableCb (plist);
304      }
305
306   if (plist->count > 1)
307     prune_to_best_arch(pool, plist);
308   if (plist->count > 1)
309     prune_to_best_version(pool, plist);
310 }
311
312
313 void
314 policy_filter_unwanted(Solver *solv, Queue *plist, Id inst, int mode)
315 {
316   Pool *pool = solv->pool;
317   if (plist->count > 1 && mode != POLICY_MODE_SUGGEST)
318     prune_to_highest_prio(pool, plist);
319   if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
320     prune_to_recommended(solv, plist);
321   /* FIXME: do this different! */
322   if (inst)
323     queue_push(plist, inst);
324
325   prune_best_version_arch (solv, pool, plist);
326 }
327
328
329 int
330 policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
331 {
332   Pool *pool = solv->pool;
333   Id a1 = s1->arch, a2 = s2->arch;
334
335   if (solv && solv->archCheckCb)
336      {   /* The application is responsible for */
337          return solv->archCheckCb (s1, s2);
338      }
339
340   /* we allow changes to/from noarch */
341   if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
342     return 0;
343   if (!pool->id2arch)
344     return 0;
345   a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
346   a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
347   if (((a1 ^ a2) & 0xffff0000) != 0)
348     return 1;
349   return 0;
350 }
351
352 int
353 policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
354 {
355   Pool *pool = solv->pool;
356   Id vendormask1, vendormask2;
357
358   if (solv && solv->vendorCheckCb)
359      {   /* The application is responsible for */
360          return solv->vendorCheckCb (s1, s2);
361      }
362
363   if (s1->vendor == s2->vendor)
364     return 0;
365   vendormask1 = pool_vendor2mask(pool, s1->vendor);
366   if (!vendormask1)
367     return 0;
368   vendormask2 = pool_vendor2mask(pool, s2->vendor);
369   if ((vendormask1 & vendormask2) == 0)
370     return 0;
371   return 1;
372 }
373
374 void
375 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allowall)
376 {
377   /* installed packages get a special upgrade allowed rule */
378   Pool *pool = solv->pool;
379   Id p, *pp, n, p2, *pp2;
380   Id obs, *obsp;
381   Solvable *ps;
382   Id vendormask;
383
384   queue_empty(qs);
385
386   if (solv && solv->updateCandidateCb)
387      {   /* The application is responsible for */
388          return solv->updateCandidateCb (s, qs);
389      }
390
391   /*
392    * s = solvable ptr
393    * n = solvable Id
394    */
395   n = s - pool->solvables;
396   vendormask = pool_vendor2mask(pool, s->vendor);
397
398   /*
399    * look for updates for s
400    */
401   FOR_PROVIDES(p, pp, s->name)  /* every provider of s' name */
402     {
403       if (p == n)               /* skip itself */
404         continue;
405
406       ps = pool->solvables + p;
407       if (s->name == ps->name)  /* name match */
408         {
409           if (!allowall)
410             {
411               if (!solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_MATCH_RELEASE) > 0)
412                 continue;
413               if (!solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
414                 continue;
415               if (!solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
416                 continue;
417             }
418         }
419       else if (!solv->noupdateprovide && ps->obsoletes)   /* provides/obsoletes combination ? */
420         {
421           obsp = ps->repo->idarraydata + ps->obsoletes;
422           while ((obs = *obsp++) != 0)  /* for all obsoletes */
423             {
424               FOR_PROVIDES(p2, pp2, obs)   /* and all matching providers of the obsoletes */
425                 {
426                   if (p2 == n)          /* match ! */
427                     break;
428                 }
429               if (p2)                   /* match! */
430                 break;
431             }
432           if (!obs)                     /* continue if no match */
433             continue;
434           /* here we have 'p' with a matching provides/obsoletes combination
435            * thus flagging p as a valid update candidate for s
436            */
437         }
438       else
439         continue;
440       queue_push(qs, p);
441     }
442   if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->installed->start])
443     {
444       for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *pp++) != 0;)
445         queue_push(qs, p);
446     }
447 }
448