satsolver  0.17.2
policy.c
Go to the documentation of this file.
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 /*-----------------------------------------------------------------*/
26 
27 /*
28  * prep for prune_best_version
29  * sort by name
30  */
31 
32 static int
33 prune_to_best_version_sortcmp(const void *ap, const void *bp, void *dp)
34 {
35  Pool *pool = dp;
36  int r;
37  Id a = *(Id *)ap;
38  Id b = *(Id *)bp;
39  Solvable *sa, *sb;
40 
41  sa = pool->solvables + a;
42  sb = pool->solvables + b;
43  r = sa->name - sb->name;
44  if (r)
45  {
46  const char *na, *nb;
47  /* different names. We use real strcmp here so that the result
48  * is not depending on some random solvable order */
49  na = id2str(pool, sa->name);
50  nb = id2str(pool, sb->name);
51  return strcmp(na, nb);
52  }
53  /* the same name, bring installed solvables to the front */
54  if (pool->installed)
55  {
56  if (sa->repo == pool->installed)
57  {
58  if (sb->repo != pool->installed)
59  return -1;
60  }
61  else if (sb->repo == pool->installed)
62  return 1;
63  }
64  /* sort by repository sub-prio (installed repo handled above) */
65  r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
66  if (r)
67  return r;
68  /* no idea about the order, sort by id */
69  return a - b;
70 }
71 
72 
73 /*
74  * prune to repository with highest priority.
75  * does not prune installed solvables.
76  */
77 
78 static void
80 {
81  int i, j;
82  Solvable *s;
83  int bestprio = 0, bestprioset = 0;
84 
85  /* prune to highest priority */
86  for (i = 0; i < plist->count; i++) /* find highest prio in queue */
87  {
88  s = pool->solvables + plist->elements[i];
89  if (pool->installed && s->repo == pool->installed)
90  continue;
91  if (!bestprioset || s->repo->priority > bestprio)
92  {
93  bestprio = s->repo->priority;
94  bestprioset = 1;
95  }
96  }
97  if (!bestprioset)
98  return;
99  for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
100  {
101  s = pool->solvables + plist->elements[i];
102  if (s->repo->priority == bestprio || (pool->installed && s->repo == pool->installed))
103  plist->elements[j++] = plist->elements[i];
104  }
105  plist->count = j;
106 }
107 
108 static void
110 {
111  Queue pq;
112  int i, j, k;
113  Id name;
114 
115  queue_init(&pq);
116  sat_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
117  queue_push(&pq, plist->elements[0]);
118  name = pool->solvables[pq.elements[0]].name;
119  for (i = 1, j = 0; i < plist->count; i++)
120  {
121  if (pool->solvables[plist->elements[i]].name != name)
122  {
123  if (pq.count > 2)
124  prune_to_highest_prio(pool, &pq);
125  for (k = 0; k < pq.count; k++)
126  plist->elements[j++] = pq.elements[k];
127  queue_empty(&pq);
128  queue_push(&pq, plist->elements[i]);
129  name = pool->solvables[pq.elements[0]].name;
130  }
131  }
132  if (pq.count > 2)
133  prune_to_highest_prio(pool, &pq);
134  for (k = 0; k < pq.count; k++)
135  plist->elements[j++] = pq.elements[k];
136  queue_free(&pq);
137  plist->count = j;
138 }
139 
140 
141 /*
142  * prune to recommended/suggested packages.
143  * does not prune installed packages (they are also somewhat recommended).
144  */
145 
146 static void
148 {
149  Pool *pool = solv->pool;
150  int i, j, k, ninst;
151  Solvable *s;
152  Id p, pp, rec, *recp, sug, *sugp;
153 
154  ninst = 0;
155  if (pool->installed)
156  {
157  for (i = 0; i < plist->count; i++)
158  {
159  p = plist->elements[i];
160  s = pool->solvables + p;
161  if (pool->installed && s->repo == pool->installed)
162  ninst++;
163  }
164  }
165  if (plist->count - ninst < 2)
166  return;
167 
168  /* update our recommendsmap/suggestsmap */
169  if (solv->recommends_index < 0)
170  {
171  MAPZERO(&solv->recommendsmap);
172  MAPZERO(&solv->suggestsmap);
173  solv->recommends_index = 0;
174  }
175  while (solv->recommends_index < solv->decisionq.count)
176  {
177  p = solv->decisionq.elements[solv->recommends_index++];
178  if (p < 0)
179  continue;
180  s = pool->solvables + p;
181  if (s->recommends)
182  {
183  recp = s->repo->idarraydata + s->recommends;
184  while ((rec = *recp++) != 0)
185  FOR_PROVIDES(p, pp, rec)
186  MAPSET(&solv->recommendsmap, p);
187  }
188  if (s->suggests)
189  {
190  sugp = s->repo->idarraydata + s->suggests;
191  while ((sug = *sugp++) != 0)
192  FOR_PROVIDES(p, pp, sug)
193  MAPSET(&solv->suggestsmap, p);
194  }
195  }
196 
197  /* prune to recommended/supplemented */
198  ninst = 0;
199  for (i = j = 0; i < plist->count; i++)
200  {
201  p = plist->elements[i];
202  s = pool->solvables + p;
203  if (pool->installed && s->repo == pool->installed)
204  {
205  ninst++;
206  if (j)
207  plist->elements[j++] = p;
208  continue;
209  }
210  if (!MAPTST(&solv->recommendsmap, p))
211  if (!solver_is_supplementing(solv, s))
212  continue;
213  if (!j && ninst)
214  {
215  for (k = 0; j < ninst; k++)
216  {
217  s = pool->solvables + plist->elements[k];
218  if (pool->installed && s->repo == pool->installed)
219  plist->elements[j++] = plist->elements[k];
220  }
221  }
222  plist->elements[j++] = p;
223  }
224  if (j)
225  plist->count = j;
226 
227  /* anything left to prune? */
228  if (plist->count - ninst < 2)
229  return;
230 
231  /* prune to suggested/enhanced*/
232  ninst = 0;
233  for (i = j = 0; i < plist->count; i++)
234  {
235  p = plist->elements[i];
236  s = pool->solvables + p;
237  if (pool->installed && s->repo == pool->installed)
238  {
239  ninst++;
240  if (j)
241  plist->elements[j++] = p;
242  continue;
243  }
244  if (!MAPTST(&solv->suggestsmap, p))
245  if (!solver_is_enhancing(solv, s))
246  continue;
247  if (!j && ninst)
248  {
249  for (k = 0; j < ninst; k++)
250  {
251  s = pool->solvables + plist->elements[k];
252  if (pool->installed && s->repo == pool->installed)
253  plist->elements[j++] = plist->elements[k];
254  }
255  }
256  plist->elements[j++] = p;
257  }
258  if (j)
259  plist->count = j;
260 }
261 
262 void
263 prune_to_best_arch(const Pool *pool, Queue *plist)
264 {
265  Id a, bestscore;
266  Solvable *s;
267  int i, j;
268 
269  if (!pool->id2arch || plist->count < 2)
270  return;
271  bestscore = 0;
272  for (i = 0; i < plist->count; i++)
273  {
274  s = pool->solvables + plist->elements[i];
275  a = s->arch;
276  a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
277  if (a && a != 1 && (!bestscore || a < bestscore))
278  bestscore = a;
279  }
280  if (!bestscore)
281  return;
282  for (i = j = 0; i < plist->count; i++)
283  {
284  s = pool->solvables + plist->elements[i];
285  a = s->arch;
286  if (a > pool->lastarch)
287  continue;
288  a = pool->id2arch[a];
289  /* a == 1 -> noarch */
290  if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
291  continue;
292  plist->elements[j++] = plist->elements[i];
293  }
294  if (j)
295  plist->count = j;
296 }
297 
298 /*
299  * remove entries from plist that are obsoleted by other entries
300  * with different name.
301  * plist should be sorted in some way.
302  */
303 static void
304 prune_obsoleted(Pool *pool, Queue *plist)
305 {
306  int i, j;
307  Solvable *s;
308 
309  /* FIXME maybe also check provides depending on noupdateprovide? */
310  /* FIXME do not prune cycles */
311  for (i = 0; i < plist->count; i++)
312  {
313  Id p, pp, obs, *obsp;
314  s = pool->solvables + plist->elements[i];
315  if (!s->obsoletes)
316  continue;
317  obsp = s->repo->idarraydata + s->obsoletes;
318  while ((obs = *obsp++) != 0)
319  {
320  FOR_PROVIDES(p, pp, obs)
321  {
322  Solvable *ps = pool->solvables + p;
323  if (ps->name == s->name)
324  continue;
325  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
326  continue;
327  if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
328  continue;
329  /* hmm, expensive. should use hash if plist is big */
330  for (j = 0; j < plist->count; j++)
331  {
332  if (i == j)
333  continue;
334  if (plist->elements[j] == p)
335  plist->elements[j] = 0;
336  }
337  }
338  }
339  }
340  /* delete zeroed out queue entries */
341  for (i = j = 0; i < plist->count; i++)
342  if (plist->elements[i])
343  plist->elements[j++] = plist->elements[i];
344  plist->count = j;
345 }
346 
347 /*
348  * prune_to_best_version
349  *
350  * sort list of packages (given through plist) by name and evr
351  * return result through plist
352  */
353 void
355 {
356  int i, j;
357  Solvable *s, *best;
358 
359  if (plist->count < 2) /* no need to prune for a single entry */
360  return;
361  POOL_DEBUG(SAT_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);
362 
363  /* sort by name first, prefer installed */
364  sat_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
365 
366  /* now find best 'per name' */
367  best = 0;
368  for (i = j = 0; i < plist->count; i++)
369  {
370  s = pool->solvables + plist->elements[i];
371 
372  POOL_DEBUG(SAT_DEBUG_POLICY, "- %s[%s]\n",
373  solvable2str(pool, s),
374  (pool->installed && s->repo == pool->installed) ? "installed" : "not installed");
375 
376  if (!best) /* if no best yet, the current is best */
377  {
378  best = s;
379  continue;
380  }
381 
382  /* name switch: finish group, re-init */
383  if (best->name != s->name) /* new name */
384  {
385  plist->elements[j++] = best - pool->solvables; /* move old best to front */
386  best = s; /* take current as new best */
387  continue;
388  }
389 
390  if (best->evr != s->evr) /* compare evr */
391  {
392  if (evrcmp(pool, best->evr, s->evr, EVRCMP_COMPARE) < 0)
393  best = s;
394  }
395  }
396  plist->elements[j++] = best - pool->solvables; /* finish last group */
397  plist->count = j;
398 
399  /* we reduced the list to one package per name, now look at
400  * package obsoletes */
401  if (plist->count > 1)
402  prune_obsoleted(pool, plist);
403 }
404 
405 
406 /* legacy, do not use anymore!
407  * (rates arch higher than version, but thats a policy)
408  */
409 
410 void
411 prune_best_arch_name_version(const Solver *solv, Pool *pool, Queue *plist)
412 {
413  if (solv && solv->bestSolvableCb)
414  { /* The application is responsible for */
415  return solv->bestSolvableCb(solv->pool, plist);
416  }
417 
418  if (plist->count > 1)
419  prune_to_best_arch(pool, plist);
420  if (plist->count > 1)
421  prune_to_best_version(pool, plist);
422 }
423 
424 /* installed packages involed in a dup operation can only be kept
425 * if they are identical to a non-installed one */
426 static void
428 {
429  Pool *pool = solv->pool;
430  int i, j, k;
431 
432  for (i = j = 0; i < plist->count; i++)
433  {
434  Id p = plist->elements[i];
435  Solvable *s = pool->solvables + p;
436  if (s->repo == pool->installed && (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
437  {
438  for (k = 0; k < plist->count; k++)
439  {
440  Solvable *s2 = pool->solvables + plist->elements[k];
441  if (s2->repo != pool->installed && solvable_identical(s, s2))
442  break;
443  }
444  if (k == plist->count)
445  continue; /* no identical package found, ignore installed package */
446  }
447  plist->elements[j++] = p;
448  }
449  if (j)
450  plist->count = j;
451 }
452 
453 /*
454  * POLICY_MODE_CHOOSE: default, do all pruning steps
455  * POLICY_MODE_RECOMMEND: leave out prune_to_recommended
456  * POLICY_MODE_SUGGEST: leave out prune_to_recommended, do prio pruning just per name
457  */
458 void
459 policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
460 {
461  Pool *pool = solv->pool;
462  if (plist->count > 1)
463  {
464  if (mode != POLICY_MODE_SUGGEST)
465  prune_to_highest_prio(pool, plist);
466  else
467  prune_to_highest_prio_per_name(pool, plist);
468  /* installed dup packages need special treatment as prio pruning
469  * does not prune installed packages */
470  if (plist->count > 1 && pool->installed && (solv->dupmap_all || solv->dupinvolvedmap.size))
471  prune_installed_dup_packages(solv, plist);
472  }
473  if (plist->count > 1 && mode == POLICY_MODE_CHOOSE)
474  prune_to_recommended(solv, plist);
475  prune_best_arch_name_version(solv, pool, plist);
476 }
477 
478 
479 /* check if there is an illegal architecture change if
480  * installed solvable s1 is replaced by s2 */
481 int
483 {
484  Pool *pool = solv->pool;
485  Id a1 = s1->arch, a2 = s2->arch;
486 
487  if (solv && solv->archCheckCb)
488  { /* The application is responsible for */
489  return solv->archCheckCb(solv->pool, s1, s2);
490  }
491 
492  /* we allow changes to/from noarch */
493 #ifndef DEBIAN_SEMANTICS
494  if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
495  return 0;
496 #else
497  if (a1 == a2 || a1 == ARCH_ALL || a2 == ARCH_ALL)
498  return 0;
499 #endif
500  if (!pool->id2arch)
501  return 0;
502  a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
503  a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
504  if (((a1 ^ a2) & 0xffff0000) != 0)
505  return 1;
506  return 0;
507 }
508 
509 /* check if there is an illegal vendor change if
510  * installed solvable s1 is replaced by s2 */
511 int
513 {
514  Pool *pool = solv->pool;
515  Id v1, v2;
516  Id vendormask1, vendormask2;
517 
518  if (solv->vendorCheckCb)
519  { /* The application is responsible for */
520  return solv->vendorCheckCb(pool, s1, s2);
521  }
522  /* treat a missing vendor as empty string */
523  v1 = s1->vendor ? s1->vendor : ID_EMPTY;
524  v2 = s2->vendor ? s2->vendor : ID_EMPTY;
525  if (v1 == v2)
526  return 0;
527  vendormask1 = pool_vendor2mask(pool, v1);
528  if (!vendormask1)
529  return 1; /* can't match */
530  vendormask2 = pool_vendor2mask(pool, v2);
531  if ((vendormask1 & vendormask2) != 0)
532  return 0;
533  return 1; /* no class matches */
534 }
535 
536 /* check if it is illegal to replace installed
537  * package "is" with package "s" (which must obsolete "is")
538  */
539 int
540 policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
541 {
542  Pool *pool = solv->pool;
543  int ret = 0;
544  if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !solv->allowdowngrade)
545  {
546  if (is->name == s->name && evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
548  }
549  if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !solv->allowarchchange)
550  {
551  if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
553  }
554  if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !solv->allowvendorchange)
555  {
556  if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
558  }
559  return ret;
560 }
561 
562 /*-------------------------------------------------------------------
563  *
564  * create reverse obsoletes map for installed solvables
565  *
566  * For each installed solvable find which packages with *different* names
567  * obsolete the solvable.
568  * This index is used in policy_findupdatepackages() below.
569  */
570 void
572 {
573  Pool *pool = solv->pool;
574  Solvable *s;
575  Repo *installed = solv->installed;
576  Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
577  int i, n, cnt;
578 
579  if (!installed || installed->start == installed->end)
580  return;
581  cnt = installed->end - installed->start;
582  solv->obsoletes = obsoletes = sat_calloc(cnt, sizeof(Id));
583  for (i = 1; i < pool->nsolvables; i++)
584  {
585  s = pool->solvables + i;
586  if (!s->obsoletes)
587  continue;
588  if (!pool_installable(pool, s))
589  continue;
590  obsp = s->repo->idarraydata + s->obsoletes;
591  while ((obs = *obsp++) != 0)
592  {
593  FOR_PROVIDES(p, pp, obs)
594  {
595  Solvable *ps = pool->solvables + p;;
596  if (ps->repo != installed)
597  continue;
598  if (ps->name == s->name)
599  continue;
600  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
601  continue;
602  if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
603  continue;
604  obsoletes[p - installed->start]++;
605  }
606  }
607  }
608  n = 0;
609  for (i = 0; i < cnt; i++)
610  if (obsoletes[i])
611  {
612  n += obsoletes[i] + 1;
613  obsoletes[i] = n;
614  }
615  solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
616  POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
617  for (i = pool->nsolvables - 1; i > 0; i--)
618  {
619  s = pool->solvables + i;
620  if (!s->obsoletes)
621  continue;
622  if (!pool_installable(pool, s))
623  continue;
624  obsp = s->repo->idarraydata + s->obsoletes;
625  while ((obs = *obsp++) != 0)
626  {
627  FOR_PROVIDES(p, pp, obs)
628  {
629  Solvable *ps = pool->solvables + p;;
630  if (ps->repo != installed)
631  continue;
632  if (ps->name == s->name)
633  continue;
634  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
635  continue;
636  if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
637  continue;
638  if (obsoletes_data[obsoletes[p - installed->start]] != i)
639  obsoletes_data[--obsoletes[p - installed->start]] = i;
640  }
641  }
642  }
643 }
644 
645 
646 /*
647  * find update candidates
648  *
649  * s: installed solvable to be updated
650  * qs: [out] queue to hold Ids of candidates
651  * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
652  *
653  */
654 void
655 policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
656 {
657  /* installed packages get a special upgrade allowed rule */
658  Pool *pool = solv->pool;
659  Id p, pp, n, p2, pp2;
660  Id obs, *obsp;
661  Solvable *ps;
662  int haveprovobs = 0;
663 
664  queue_empty(qs);
665 
666  if (solv && solv->updateCandidateCb)
667  { /* The application is responsible for */
668  return solv->updateCandidateCb(solv->pool, s, qs);
669  }
670 
671  n = s - pool->solvables;
672 
673  /*
674  * look for updates for s
675  */
676  FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
677  {
678  if (p == n) /* skip itself */
679  continue;
680 
681  ps = pool->solvables + p;
682  if (s->name == ps->name) /* name match */
683  {
684  if (!allow_all && !solv->allowdowngrade && evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
685  continue;
686  }
687  else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
688  {
689  obsp = ps->repo->idarraydata + ps->obsoletes;
690  while ((obs = *obsp++) != 0) /* for all obsoletes */
691  {
692  FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
693  {
694  Solvable *ps2 = pool->solvables + p2;
695  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
696  continue;
697  if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps2))
698  continue;
699  if (p2 == n) /* match ! */
700  break;
701  }
702  if (p2) /* match! */
703  break;
704  }
705  if (!obs) /* continue if no match */
706  continue;
707  /* here we have 'p' with a matching provides/obsoletes combination
708  * thus flagging p as a valid update candidate for s
709  */
710  haveprovobs = 1;
711  }
712  else
713  continue;
714  if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
715  continue;
716  if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
717  continue;
718  queue_push(qs, p);
719  }
720  /* if we have found some valid candidates and noupdateprovide is not set, we're
721  done. otherwise we fallback to all obsoletes */
722  if (!solv->noupdateprovide && haveprovobs)
723  return;
724  if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
725  {
726  Id *opp;
727  for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
728  {
729  ps = pool->solvables + p;
730  if (!allow_all && !solv->allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
731  continue;
732  if (!allow_all && !solv->allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
733  continue;
734  queue_push(qs, p);
735  }
736  }
737 }
738