small optimization for solver_addupdaterule
[platform/upstream/libsolv.git] / src / rules.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * rules.c
10  *
11  * SAT based dependency solver
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "solver.h"
21 #include "solver_private.h"
22 #include "bitmap.h"
23 #include "pool.h"
24 #include "poolarch.h"
25 #include "util.h"
26 #include "evr.h"
27 #include "policy.h"
28 #include "solverdebug.h"
29 #include "linkedpkg.h"
30 #include "cplxdeps.h"
31
32 #define RULES_BLOCK 63
33
34 static void addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep);
35 static void solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded);
36
37 /*-------------------------------------------------------------------
38  * Check if dependency is possible
39  *
40  * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap.
41  * used in solver_addrpmrulesforweak and solver_createcleandepsmap.
42  */
43
44 static inline int
45 dep_possible(Solver *solv, Id dep, Map *m)
46 {
47   Pool *pool = solv->pool;
48   Id p, pp;
49
50   if (ISRELDEP(dep))
51     {
52       Reldep *rd = GETRELDEP(pool, dep);
53       if (rd->flags >= 8)
54          {
55           if (rd->flags == REL_AND || rd->flags == REL_COND)
56             {
57               if (!dep_possible(solv, rd->name, m))
58                 return 0;
59               return dep_possible(solv, rd->evr, m);
60             }
61           if (rd->flags == REL_OR)
62             {
63               if (dep_possible(solv, rd->name, m))
64                 return 1;
65               return dep_possible(solv, rd->evr, m);
66             }
67           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
68             return solver_splitprovides(solv, rd->evr);
69           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
70             return solver_dep_installed(solv, rd->evr);
71         }
72     }
73   FOR_PROVIDES(p, pp, dep)
74     {
75       if (MAPTST(m, p))
76         return 1;
77     }
78   return 0;
79 }
80
81 static inline int
82 is_otherproviders_dep(Pool *pool, Id dep)
83 {
84   if (ISRELDEP(dep))
85     {
86       Reldep *rd = GETRELDEP(pool, dep);
87       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
88         return 1;
89     }
90   return 0;
91 }
92
93 /********************************************************************
94  *
95  * Rule handling
96  *
97  * - unify rules, remove duplicates
98  */
99
100 /*-------------------------------------------------------------------
101  *
102  * compare rules for unification sort
103  *
104  */
105
106 static int
107 unifyrules_sortcmp(const void *ap, const void *bp, void *dp)
108 {
109   Pool *pool = dp;
110   Rule *a = (Rule *)ap;
111   Rule *b = (Rule *)bp;
112   Id *ad, *bd;
113   int x;
114
115   x = a->p - b->p;
116   if (x)
117     return x;                          /* p differs */
118
119   /* identical p */
120   if (a->d == 0 && b->d == 0)
121     return a->w2 - b->w2;              /* assertion: return w2 diff */
122
123   if (a->d == 0)                       /* a is assertion, b not */
124     {
125       x = a->w2 - pool->whatprovidesdata[b->d];
126       return x ? x : -1;
127     }
128
129   if (b->d == 0)                       /* b is assertion, a not */
130     {
131       x = pool->whatprovidesdata[a->d] - b->w2;
132       return x ? x : 1;
133     }
134
135   /* compare whatprovidesdata */
136   ad = pool->whatprovidesdata + a->d;
137   bd = pool->whatprovidesdata + b->d;
138   while (*bd)
139     if ((x = *ad++ - *bd++) != 0)
140       return x;
141   return *ad;
142 }
143
144 int
145 solver_rulecmp(Solver *solv, Rule *r1, Rule *r2)
146 {
147   return unifyrules_sortcmp(r1, r2, solv->pool);
148 }
149
150
151 /*-------------------------------------------------------------------
152  *
153  * unify rules
154  * go over all rules and remove duplicates
155  */
156
157 void
158 solver_unifyrules(Solver *solv)
159 {
160   Pool *pool = solv->pool;
161   int i, j;
162   Rule *ir, *jr;
163
164   if (solv->nrules <= 2)               /* nothing to unify */
165     return;
166
167   /* sort rules first */
168   solv_sort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp, solv->pool);
169
170   /* prune rules
171    * i = unpruned
172    * j = pruned
173    */
174   jr = 0;
175   for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
176     {
177       if (jr && !unifyrules_sortcmp(ir, jr, pool))
178         continue;                      /* prune! */
179       jr = solv->rules + j++;          /* keep! */
180       if (ir != jr)
181         *jr = *ir;
182     }
183
184   /* reduced count from nrules to j rules */
185   POOL_DEBUG(SOLV_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
186
187   /* adapt rule buffer */
188   solv->nrules = j;
189   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
190
191   /*
192    * debug: log rule statistics
193    */
194   IF_POOLDEBUG (SOLV_DEBUG_STATS)
195     {
196       int binr = 0;
197       int lits = 0;
198       Id *dp;
199       Rule *r;
200
201       for (i = 1; i < solv->nrules; i++)
202         {
203           r = solv->rules + i;
204           if (r->d == 0)
205             binr++;
206           else
207             {
208               dp = solv->pool->whatprovidesdata + r->d;
209               while (*dp++)
210                 lits++;
211             }
212         }
213       POOL_DEBUG(SOLV_DEBUG_STATS, "  binary: %d\n", binr);
214       POOL_DEBUG(SOLV_DEBUG_STATS, "  normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
215     }
216 }
217
218 #if 0
219
220 /*
221  * hash rule
222  */
223
224 static Hashval
225 hashrule(Solver *solv, Id p, Id d, int n)
226 {
227   unsigned int x = (unsigned int)p;
228   int *dp;
229
230   if (n <= 1)
231     return (x * 37) ^ (unsigned int)d;
232   dp = solv->pool->whatprovidesdata + d;
233   while (*dp)
234     x = (x * 37) ^ (unsigned int)*dp++;
235   return x;
236 }
237 #endif
238
239
240 /*-------------------------------------------------------------------
241  *
242  */
243
244 /*
245  * add rule
246  *  p = direct literal; always < 0 for installed rpm rules
247  *  d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
248  *
249  *
250  * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
251  *
252  * p < 0 : pkg id of A
253  * d > 0 : Offset in whatprovidesdata (list of providers of b)
254  *
255  * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
256  * p < 0 : pkg id of A
257  * d < 0 : Id of solvable (e.g. B1)
258  *
259  * d == 0: unary rule, assertion => (A) or (-A)
260  *
261  *   Install:    p > 0, d = 0   (A)             user requested install
262  *   Remove:     p < 0, d = 0   (-A)            user requested remove (also: uninstallable)
263  *   Requires:   p < 0, d > 0   (-A|B1|B2|...)  d: <list of providers for requirement of p>
264  *   Updates:    p > 0, d > 0   (A|B1|B2|...)   d: <list of updates for solvable p>
265  *   Conflicts:  p < 0, d < 0   (-A|-B)         either p (conflict issuer) or d (conflict provider) (binary rule)
266  *                                              also used for obsoletes
267  *   ?:          p > 0, d < 0   (A|-B)
268  *   No-op ?:    p = 0, d = 0   (null)          (used as policy rule placeholder)
269  *
270  *   resulting watches:
271  *   ------------------
272  *   Direct assertion (no watch needed) --> d = 0, w1 = p, w2 = 0
273  *   Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
274  *   every other : w1 = p, w2 = whatprovidesdata[d];
275  *   Disabled rule: w1 = 0
276  *
277  *   always returns a rule for non-rpm rules
278  */
279
280 Rule *
281 solver_addrule(Solver *solv, Id p, Id d)
282 {
283   Pool *pool = solv->pool;
284   Rule *r = 0;
285   Id *dp = 0;
286
287   int n = 0;                           /* number of literals in rule - 1
288                                           0 = direct assertion (single literal)
289                                           1 = binary rule
290                                           >1 =
291                                         */
292
293   /* it often happenes that requires lead to adding the same rpm rule
294    * multiple times, so we prune those duplicates right away to make
295    * the work for unifyrules a bit easier */
296
297   if (!solv->rpmrules_end)              /* we add rpm rules */
298     {
299       r = solv->rules + solv->nrules - 1;       /* get the last added rule */
300       if (r->p == p && r->d == d && (d != 0 || !r->w2))
301         return r;
302     }
303
304     /*
305      * compute number of literals (n) in rule
306      */
307
308   if (d < 0)
309     {
310       /* always a binary rule */
311       if (p == d)
312         return 0;                      /* ignore self conflict */
313       n = 1;
314     }
315   else if (d > 0)
316     {
317       for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
318         if (*dp == -p)
319           return 0;                     /* rule is self-fulfilling */
320         
321       if (n == 1)                       /* convert to binary rule */
322         d = dp[-1];
323     }
324
325   if (n == 1 && p > d && !solv->rpmrules_end)
326     {
327       /* smallest literal first so we can find dups */
328       n = p; p = d; d = n;             /* p <-> d */
329       n = 1;                           /* re-set n, was used as temp var */
330     }
331
332   /*
333    * check for duplicate
334    */
335
336   /* check if the last added rule (r) is exactly the same as what we're looking for. */
337   if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
338     return r;  /* binary rule */
339
340     /* have n-ary rule with same first literal, check other literals */
341   if (r && n > 1 && r->d && r->p == p)
342     {
343       /* Rule where d is an offset in whatprovidesdata */
344       Id *dp2;
345       if (d == r->d)
346         return r;
347       dp2 = pool->whatprovidesdata + r->d;
348       for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
349         if (*dp != *dp2)
350           break;
351       if (*dp == *dp2)
352         return r;
353    }
354
355   /*
356    * allocate new rule
357    */
358
359   /* extend rule buffer */
360   solv->rules = solv_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
361   r = solv->rules + solv->nrules++;    /* point to rule space */
362
363     /*
364      * r = new rule
365      */
366
367   r->p = p;
368   if (n == 0)
369     {
370       /* direct assertion, no watch needed */
371       r->d = 0;
372       r->w1 = p;
373       r->w2 = 0;
374     }
375   else if (n == 1)
376     {
377       /* binary rule */
378       r->d = 0;
379       r->w1 = p;
380       r->w2 = d;
381     }
382   else
383     {
384       r->d = d;
385       r->w1 = p;
386       r->w2 = pool->whatprovidesdata[d];
387     }
388   r->n1 = 0;
389   r->n2 = 0;
390
391   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
392     {
393       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "  Add rule: ");
394       solver_printrule(solv, SOLV_DEBUG_RULE_CREATION, r);
395     }
396
397   return r;
398 }
399
400 void
401 solver_shrinkrules(Solver *solv, int nrules)
402 {
403   solv->nrules = nrules;
404   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
405 }
406
407 /******************************************************************************
408  ***
409  *** rpm rule part: create rules representing the package dependencies
410  ***
411  ***/
412
413 /*
414  *  special multiversion patch conflict handling:
415  *  a patch conflict is also satisfied if some other
416  *  version with the same name/arch that doesn't conflict
417  *  gets installed. The generated rule is thus:
418  *  -patch|-cpack|opack1|opack2|...
419  */
420 static Id
421 makemultiversionconflict(Solver *solv, Id n, Id con)
422 {
423   Pool *pool = solv->pool;
424   Solvable *s, *sn;
425   Queue q;
426   Id p, pp, qbuf[64];
427
428   sn = pool->solvables + n;
429   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
430   queue_push(&q, -n);
431   FOR_PROVIDES(p, pp, sn->name)
432     {
433       s = pool->solvables + p;
434       if (s->name != sn->name || s->arch != sn->arch)
435         continue;
436       if (!MAPTST(&solv->multiversion, p))
437         continue;
438       if (pool_match_nevr(pool, pool->solvables + p, con))
439         continue;
440       /* here we have a multiversion solvable that doesn't conflict */
441       /* thus we're not in conflict if it is installed */
442       queue_push(&q, p);
443     }
444   if (q.count == 1)
445     return -n;  /* no other package found, generate normal conflict */
446   return pool_queuetowhatprovides(pool, &q);
447 }
448
449 static inline void
450 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
451 {
452   if (!solv->ruleinfoq)
453     solver_addrule(solv, p, d);
454   else
455     addrpmruleinfo(solv, p, d, type, dep);
456 }
457
458 #ifdef ENABLE_LINKED_PKGS
459
460 static void
461 addlinks(Solver *solv, Solvable *s, Id req, Queue *qr, Id prv, Queue *qp, Map *m, Queue *workq)
462 {
463   Pool *pool = solv->pool;
464   int i;
465   if (!qr->count)
466     return;
467 #if 0
468   printf("ADDLINKS %s\n -> %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
469   for (i = 0; i < qr->count; i++)
470     printf("    - %s\n", pool_solvid2str(pool, qr->elements[i]));
471   printf(" <- %s\n", pool_dep2str(pool, prv));
472   for (i = 0; i < qp->count; i++)
473     printf("    - %s\n", pool_solvid2str(pool, qp->elements[i]));
474 #endif
475
476   if (qr->count == 1)
477     addrpmrule(solv, qr->elements[0], -(s - pool->solvables), SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
478   else
479     addrpmrule(solv, -(s - pool->solvables), pool_queuetowhatprovides(pool, qr), SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
480   if (qp->count > 1)
481     {
482       Id d = pool_queuetowhatprovides(pool, qp);
483       for (i = 0; i < qr->count; i++)
484         addrpmrule(solv, -qr->elements[i], d, SOLVER_RULE_RPM_PACKAGE_REQUIRES, prv);
485     }
486   else if (qp->count)
487     {
488       for (i = 0; i < qr->count; i++)
489         addrpmrule(solv, qp->elements[0], -qr->elements[i], SOLVER_RULE_RPM_PACKAGE_REQUIRES, prv);
490     }
491   if (!m)
492     return;     /* nothing more to do if called from getrpmruleinfos() */
493   for (i = 0; i < qr->count; i++)
494     if (!MAPTST(m, qr->elements[i]))
495       queue_push(workq, qr->elements[i]);
496   for (i = 0; i < qp->count; i++)
497     if (!MAPTST(m, qp->elements[i]))
498       queue_push(workq, qp->elements[i]);
499   if (solv->installed && s->repo == solv->installed)
500     {
501       Repo *installed = solv->installed;
502       /* record installed buddies */
503       if (!solv->instbuddy)
504         solv->instbuddy = solv_calloc(installed->end - installed->start, sizeof(Id));
505       if (qr->count == 1)
506         solv->instbuddy[s - pool->solvables - installed->start] = qr->elements[0];
507       for (i = 0; i < qp->count; i++)
508         {
509           Id p = qp->elements[i];
510           if (pool->solvables[p].repo != installed)
511             continue;   /* huh? */
512           if (qp->count > 1 || (solv->instbuddy[p - installed->start] != 0 && solv->instbuddy[p - installed->start] != s - pool->solvables))
513             solv->instbuddy[p - installed->start] = 1;  /* 1: ambiguous buddy */
514           else
515             solv->instbuddy[p - installed->start] = s - pool->solvables;
516         }
517     }
518 }
519
520 static void
521 add_package_link(Solver *solv, Solvable *s, Map *m, Queue *workq)
522 {
523   Queue qr, qp;
524   Id req = 0, prv = 0;
525   queue_init(&qr);
526   queue_init(&qp);
527   find_package_link(solv->pool, s, &req, &qr, &prv, &qp);
528   if (qr.count)
529     addlinks(solv, s, req, &qr, prv, &qp, m, workq);
530   queue_free(&qr);
531   queue_free(&qp);
532 }
533
534 #endif
535
536 #ifdef ENABLE_COMPLEX_DEPS
537
538 static void
539 add_complex_deprules(Solver *solv, Id p, Id dep, int type, int dontfix, Queue *workq, Map *m)
540 {
541   Pool *pool = solv->pool;
542   Repo *installed = solv->installed;
543   int i, j;
544   Queue bq;
545
546   queue_init(&bq);
547
548   /* CNF expansion for requires, DNF + INVERT expansion for conflicts */
549   i = pool_normalize_complex_dep(pool, dep, &bq, type == SOLVER_RULE_RPM_PACKAGE_REQUIRES ? 0 : (CPLXDEPS_TODNF | CPLXDEPS_EXPAND | CPLXDEPS_INVERT));
550   /* handle special cases */
551   if (i == 0)
552     {
553       if (dontfix)
554         {
555           POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken dependency %s of installed package %s\n", pool_dep2str(pool, dep), pool_solvid2str(pool, p));
556         }
557       else
558         {
559           POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvid2str(pool, p), p, pool_dep2str(pool, dep));
560           addrpmrule(solv, -p, 0, type == SOLVER_RULE_RPM_PACKAGE_REQUIRES ? SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP : type, dep);
561         }
562       queue_free(&bq);
563       return;
564     }
565   if (i == 1)
566     {
567       queue_free(&bq);
568       return;
569     }
570
571   /* go through all blocks and add a rule for each block */
572   for (i = 0; i < bq.count; i++)
573     {
574       if (!bq.elements[i])
575         continue;       /* huh? */
576       if (bq.elements[i] == pool->nsolvables)
577         {
578           /* conventional requires (cannot be a conflicts as they have been expanded) */
579           Id *dp = pool->whatprovidesdata + bq.elements[i + 1];
580           i += 2;
581           if (dontfix)
582             {
583               for (j = 0; dp[j] != 0; j++)
584                 if (pool->solvables[dp[j]].repo == installed)
585                   break;                /* provider was installed */
586               if (!dp[j])
587                 {
588                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, dep), pool_solvid2str(pool, p));
589                   continue;
590                 }
591             }
592           if (!*dp)
593             {
594               /* nothing provides req! */
595               POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvid2str(pool, p), p, pool_dep2str(pool, dep));
596               addrpmrule(solv, -p, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, dep);
597               continue;
598             }
599           addrpmrule(solv, -p, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, dep);
600           /* push all non-visited providers on the work queue */
601           if (m)
602             for (; *dp; dp++)
603               if (!MAPTST(m, *dp))
604                 queue_push(workq, *dp);
605           continue;
606         }
607       if (!bq.elements[i + 1])
608         {
609           Id p2 = bq.elements[i];
610           /* simple rule with just two literals */
611           if (dontfix && p2 < 0 && pool->solvables[-p2].repo == installed)
612             continue;
613           if (dontfix && p2 > 0 && pool->solvables[p2].repo != installed)
614             continue;
615           if (p == p2)
616             continue;
617           if (-p == p2)
618             {
619               if (type == SOLVER_RULE_RPM_PACKAGE_CONFLICT)
620                 {
621                   if (pool->forbidselfconflicts && !is_otherproviders_dep(pool, dep))
622                     addrpmrule(solv, -p, 0, SOLVER_RULE_RPM_SELF_CONFLICT, dep);
623                   continue;
624                 }
625               addrpmrule(solv, -p, 0, type, dep);
626               continue;
627             }
628           if (p2 > 0)
629             addrpmrule(solv, p2, -p, type, dep);        /* hack so that we don't need pool_queuetowhatprovides */
630           else
631             addrpmrule(solv, -p, p2, type, dep);
632           if (m && p2 > 0 && !MAPTST(m, p2))
633             queue_push(workq, p2);
634         }
635       else
636         {
637           Id *qele;
638           int qcnt;
639
640           qele = bq.elements + i;
641           qcnt = i;
642           while (bq.elements[i])
643              i++;
644           qcnt = i - qcnt;
645           if (dontfix)
646             {
647               for (j = 0; j < qcnt; j++)
648                 {
649                   if (qele[j] > 0 && pool->solvables[qele[j]].repo == installed)
650                     break;
651                   if (qele[j] < 0 && pool->solvables[-qele[j]].repo != installed)
652                     break;
653                 }
654               if (j == qcnt)
655                 continue;
656             }
657           /* add -p to (ordered) rule (overwriting the trailing zero) */
658           for (j = 0; ; j++)
659             {
660               if (j == qcnt || qele[j] > -p)
661                 {
662                   if (j < qcnt)
663                     memmove(qele + j + 1, qele + j, (qcnt - j) * sizeof(Id));
664                   qele[j] = -p;
665                   qcnt++;
666                   break;
667                 }
668               if (qele[j] == -p)
669                 break;
670             }
671           /* check if the rule contains both p and -p */
672           for (j = 0; j < qcnt; j++)
673             if (qele[j] == p)
674               break;
675           if (j == qcnt)
676             {
677               /* hack: create fake queue 'q' so that we can call pool_queuetowhatprovides */
678               Queue q;
679               memset(&q, 0, sizeof(q));
680               q.count = qcnt - 1;
681               q.elements = qele + 1;
682               addrpmrule(solv, qele[0], pool_queuetowhatprovides(pool, &q), type, dep);
683               if (m)
684                 for (j = 0; j < qcnt; j++)
685                   if (qele[j] > 0 && !MAPTST(m, qele[j]))
686                     queue_push(workq, qele[j]);
687             }
688         }
689     }
690   queue_free(&bq);
691 }
692
693 #endif
694
695 /*-------------------------------------------------------------------
696  *
697  * add (install) rules for solvable
698  *
699  * s: Solvable for which to add rules
700  * m: m[s] = 1 for solvables which have rules, prevent rule duplication
701  *
702  * Algorithm: 'visit all nodes of a graph'. The graph nodes are
703  *  solvables, the edges their dependencies.
704  *  Starting from an installed solvable, this will create all rules
705  *  representing the graph created by the solvables dependencies.
706  *
707  * for unfulfilled requirements, conflicts, obsoletes,....
708  * add a negative assertion for solvables that are not installable
709  *
710  * It will also create rules for all solvables referenced by 's'
711  *  i.e. descend to all providers of requirements of 's'
712  *
713  */
714
715 void
716 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
717 {
718   Pool *pool = solv->pool;
719   Repo *installed = solv->installed;
720
721   Queue workq;  /* list of solvables we still have to work on */
722   Id workqbuf[64];
723
724   int i;
725   int dontfix;          /* ignore dependency errors for installed solvables */
726   Id req, *reqp;
727   Id con, *conp;
728   Id obs, *obsp;
729   Id rec, *recp;
730   Id sug, *sugp;
731   Id p, pp;             /* whatprovides loops */
732   Id *dp;               /* ptr to 'whatprovides' */
733   Id n;                 /* Id for current solvable 's' */
734
735   queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
736   queue_push(&workq, s - pool->solvables);      /* push solvable Id to work queue */
737
738   /* loop until there's no more work left */
739   while (workq.count)
740     {
741       /*
742        * n: Id of solvable
743        * s: Pointer to solvable
744        */
745
746       n = queue_shift(&workq);          /* 'pop' next solvable to work on from queue */
747       if (m)
748         {
749           if (MAPTST(m, n))             /* continue if already visited */
750             continue;
751           MAPSET(m, n);                 /* mark as visited */
752         }
753
754       s = pool->solvables + n;
755
756       dontfix = 0;
757       if (installed                     /* Installed system available */
758           && s->repo == installed       /* solvable is installed */
759           && !solv->fixmap_all          /* NOT repair errors in rpm dependency graph */
760           && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
761         {
762           dontfix = 1;                  /* dont care about broken rpm deps */
763         }
764
765       if (!dontfix)
766         {
767           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC
768                 ? pool_disabled_solvable(pool, s)
769                 : !pool_installable(pool, s))
770             {
771               POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvid2str(pool, n), n);
772               addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
773             }
774         }
775
776 #ifdef ENABLE_LINKED_PKGS
777       /* add pseudo-package <-> real-package links */
778       if (has_package_link(pool, s))
779         add_package_link(solv, s, m, &workq);
780 #endif
781
782       /*-----------------------------------------
783        * check requires of s
784        */
785
786       if (s->requires)
787         {
788           reqp = s->repo->idarraydata + s->requires;
789           while ((req = *reqp++) != 0)            /* go through all requires */
790             {
791               if (req == SOLVABLE_PREREQMARKER)   /* skip the marker */
792                 continue;
793
794 #ifdef ENABLE_COMPLEX_DEPS
795               if (pool_is_complex_dep(pool, req))
796                 {
797                   /* we have AND/COND deps, normalize */
798                   add_complex_deprules(solv, n, req, SOLVER_RULE_RPM_PACKAGE_REQUIRES, dontfix, &workq, m);
799                   continue;
800                 }
801 #endif
802
803               /* find list of solvables providing 'req' */
804               dp = pool_whatprovides_ptr(pool, req);
805
806               if (*dp == SYSTEMSOLVABLE)          /* always installed */
807                 continue;
808
809               if (dontfix)
810                 {
811                   /* the strategy here is to not insist on dependencies
812                    * that are already broken. so if we find one provider
813                    * that was already installed, we know that the
814                    * dependency was not broken before so we enforce it */
815                   for (i = 0; (p = dp[i]) != 0; i++)
816                     if (pool->solvables[p].repo == installed)
817                       break;            /* found installed provider */
818                   if (!p)
819                     {
820                       /* didn't find an installed provider: previously broken dependency */
821                       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
822                       continue;
823                     }
824                 }
825
826               if (!*dp)
827                 {
828                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvid2str(pool, n), n, pool_dep2str(pool, req));
829                   addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
830                   continue;
831                 }
832
833               IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
834                 {
835                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION,"  %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
836                   for (i = 0; dp[i]; i++)
837                     POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "   provided by %s\n", pool_solvid2str(pool, dp[i]));
838                 }
839
840               /* add 'requires' dependency */
841               /* rule: (-requestor|provider1|provider2|...|providerN) */
842               addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
843
844               /* push all non-visited providers on the work queue */
845               if (m)
846                 for (; *dp; dp++)
847                   if (!MAPTST(m, *dp))
848                     queue_push(&workq, *dp);
849             }
850         }
851
852       /* that's all we check for src packages */
853       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
854         continue;
855
856       /*-----------------------------------------
857        * check conflicts of s
858        */
859
860       if (s->conflicts)
861         {
862           int ispatch = 0;
863
864           /* we treat conflicts in patches a bit differen:
865            * - nevr matching
866            * - multiversion handling
867            * XXX: we should really handle this different, looking
868            * at the name is a bad hack
869            */
870           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
871             ispatch = 1;
872           conp = s->repo->idarraydata + s->conflicts;
873           /* foreach conflicts of 's' */
874           while ((con = *conp++) != 0)
875             {
876 #ifdef ENABLE_COMPLEX_DEPS
877               if (!ispatch && pool_is_complex_dep(pool, con))
878                 {
879                   /* we have AND/COND deps, normalize */
880                   add_complex_deprules(solv, n, con, SOLVER_RULE_RPM_PACKAGE_CONFLICT, dontfix, &workq, m);
881                   continue;
882                 }
883 #endif
884               /* foreach providers of a conflict of 's' */
885               FOR_PROVIDES(p, pp, con)
886                 {
887                   if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
888                     continue;
889                   /* dontfix: dont care about conflicts with already installed packs */
890                   if (dontfix && pool->solvables[p].repo == installed)
891                     continue;
892                   if (p == n)           /* p == n: self conflict */
893                     {
894                       if (!pool->forbidselfconflicts || is_otherproviders_dep(pool, con))
895                         continue;
896                       addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_SELF_CONFLICT, con);
897                       continue;
898                     }
899                   if (ispatch && solv->multiversion.size && MAPTST(&solv->multiversion, p) && ISRELDEP(con))
900                     {
901                       /* our patch conflicts with a multiversion package */
902                       p = -makemultiversionconflict(solv, p, con);
903                     }
904                   /* rule: -n|-p: either solvable _or_ provider of conflict */
905                   addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_CONFLICT, con);
906                 }
907             }
908         }
909
910       /*-----------------------------------------
911        * check obsoletes and implicit obsoletes of a package
912        * if ignoreinstalledsobsoletes is not set, we're also checking
913        * obsoletes of installed packages (like newer rpm versions)
914        */
915       if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
916         {
917           int multi = solv->multiversion.size && MAPTST(&solv->multiversion, n);
918           int isinstalled = (installed && s->repo == installed);
919           if (s->obsoletes && (!multi || solv->keepexplicitobsoletes))
920             {
921               obsp = s->repo->idarraydata + s->obsoletes;
922               /* foreach obsoletes */
923               while ((obs = *obsp++) != 0)
924                 {
925                   /* foreach provider of an obsoletes of 's' */
926                   FOR_PROVIDES(p, pp, obs)
927                     {
928                       Solvable *ps = pool->solvables + p;
929                       if (p == n)
930                         continue;
931                       if (isinstalled && dontfix && ps->repo == installed)
932                         continue;       /* don't repair installed/installed problems */
933                       if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
934                           && !pool_match_nevr(pool, ps, obs))
935                         continue;
936                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
937                         continue;
938                       if (!isinstalled)
939                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
940                       else
941                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
942                     }
943                 }
944             }
945           /* check implicit obsoletes
946            * for installed packages we only need to check installed/installed problems (and
947            * only when dontfix is not set), as the others are picked up when looking at the
948            * uninstalled package.
949            */
950           if (!isinstalled || !dontfix)
951             {
952               FOR_PROVIDES(p, pp, s->name)
953                 {
954                   Solvable *ps = pool->solvables + p;
955                   if (p == n)
956                     continue;
957                   if (isinstalled && ps->repo != installed)
958                     continue;
959                   /* we still obsolete packages with same nevra, like rpm does */
960                   /* (actually, rpm mixes those packages. yuck...) */
961                   if (multi && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
962                     continue;
963                   if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
964                     continue;
965                   if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
966                     continue;
967                   if (s->name == ps->name)
968                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
969                   else
970                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
971                 }
972             }
973         }
974
975       if (m && pool->implicitobsoleteusescolors && (s->arch > pool->lastarch || pool->id2arch[s->arch] != 1))
976         {
977           int a = pool->id2arch[s->arch];
978           /* check lock-step candidates */
979           FOR_PROVIDES(p, pp, s->name)
980             {
981               Solvable *ps = pool->solvables + p;
982               if (s->name != ps->name || s->evr != ps->evr || MAPTST(m, p))
983                 continue;
984               if (ps->arch > pool->lastarch || pool->id2arch[ps->arch] == 1 || pool->id2arch[ps->arch] >= a)
985                 continue;
986               queue_push(&workq, p);
987             }
988         }
989
990       /*-----------------------------------------
991        * add recommends to the work queue
992        */
993       if (s->recommends && m)
994         {
995           recp = s->repo->idarraydata + s->recommends;
996           while ((rec = *recp++) != 0)
997             {
998               FOR_PROVIDES(p, pp, rec)
999                 if (!MAPTST(m, p))
1000                   queue_push(&workq, p);
1001             }
1002         }
1003       if (s->suggests && m)
1004         {
1005           sugp = s->repo->idarraydata + s->suggests;
1006           while ((sug = *sugp++) != 0)
1007             {
1008               FOR_PROVIDES(p, pp, sug)
1009                 if (!MAPTST(m, p))
1010                   queue_push(&workq, p);
1011             }
1012         }
1013     }
1014   queue_free(&workq);
1015 }
1016
1017 #ifdef ENABLE_LINKED_PKGS
1018 void
1019 solver_addrpmrulesforlinked(Solver *solv, Map *m)
1020 {
1021   Pool *pool = solv->pool;
1022   Solvable *s;
1023   int i, j;
1024   Queue qr;
1025
1026   queue_init(&qr);
1027   for (i = 1; i < pool->nsolvables; i++)
1028     {
1029       if (MAPTST(m, i))
1030         continue;
1031       s = pool->solvables + i;
1032       if (!s->repo || s->repo == solv->installed)
1033         continue;
1034       if (!strchr(pool_id2str(pool, s->name), ':'))
1035         continue;
1036       if (!pool_installable(pool, s))
1037         continue;
1038       find_package_link(pool, s, 0, &qr, 0, 0);
1039       if (qr.count)
1040         {
1041           for (j = 0; j < qr.count; j++)
1042             if (MAPTST(m, qr.elements[j]))
1043               {
1044                 solver_addrpmrulesforsolvable(solv, s, m);
1045                 break;
1046               }
1047           queue_empty(&qr);
1048         }
1049     }
1050   queue_free(&qr);
1051 }
1052 #endif
1053
1054 /*-------------------------------------------------------------------
1055  *
1056  * Add rules for packages possibly selected in by weak dependencies
1057  *
1058  * m: already added solvables
1059  */
1060
1061 void
1062 solver_addrpmrulesforweak(Solver *solv, Map *m)
1063 {
1064   Pool *pool = solv->pool;
1065   Solvable *s;
1066   Id sup, *supp;
1067   int i, n;
1068
1069   /* foreach solvable in pool */
1070   for (i = n = 1; n < pool->nsolvables; i++, n++)
1071     {
1072       if (i == pool->nsolvables)                /* wrap i */
1073         i = 1;
1074       if (MAPTST(m, i))                         /* already added that one */
1075         continue;
1076
1077       s = pool->solvables + i;
1078       if (!s->repo)
1079         continue;
1080       if (s->repo != pool->installed && !pool_installable(pool, s))
1081         continue;       /* only look at installable ones */
1082
1083       sup = 0;
1084       if (s->supplements)
1085         {
1086           /* find possible supplements */
1087           supp = s->repo->idarraydata + s->supplements;
1088           while ((sup = *supp++) != 0)
1089             if (dep_possible(solv, sup, m))
1090               break;
1091         }
1092
1093       /* if nothing found, check for enhances */
1094       if (!sup && s->enhances)
1095         {
1096           supp = s->repo->idarraydata + s->enhances;
1097           while ((sup = *supp++) != 0)
1098             if (dep_possible(solv, sup, m))
1099               break;
1100         }
1101       /* if nothing found, goto next solvables */
1102       if (!sup)
1103         continue;
1104       solver_addrpmrulesforsolvable(solv, s, m);
1105       n = 0;                    /* check all solvables again because we added solvables to m */
1106     }
1107 }
1108
1109
1110 /*-------------------------------------------------------------------
1111  *
1112  * add package rules for possible updates
1113  *
1114  * s: solvable
1115  * m: map of already visited solvables
1116  * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1117  */
1118
1119 void
1120 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1121 {
1122   Pool *pool = solv->pool;
1123   int i;
1124     /* queue and buffer for it */
1125   Queue qs;
1126   Id qsbuf[64];
1127
1128   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1129     /* find update candidates for 's' */
1130   policy_findupdatepackages(solv, s, &qs, allow_all);
1131     /* add rule for 's' if not already done */
1132   if (!MAPTST(m, s - pool->solvables))
1133     solver_addrpmrulesforsolvable(solv, s, m);
1134     /* foreach update candidate, add rule if not already done */
1135   for (i = 0; i < qs.count; i++)
1136     if (!MAPTST(m, qs.elements[i]))
1137       solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1138   queue_free(&qs);
1139 }
1140
1141
1142 /***********************************************************************
1143  ***
1144  ***  Update/Feature rule part
1145  ***
1146  ***  Those rules make sure an installed package isn't silently deleted
1147  ***
1148  ***/
1149
1150 static Id
1151 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
1152 {
1153   Pool *pool = solv->pool;
1154   int i;
1155
1156   policy_findupdatepackages(solv, s, qs, allow_all ? allow_all : 2);
1157   if (!qs->count)
1158     {
1159       if (allow_all)
1160         return 0;       /* orphaned, don't create feature rule */
1161       /* check if this is an orphaned package */
1162       policy_findupdatepackages(solv, s, qs, 1);
1163       if (!qs->count)
1164         return 0;       /* orphaned, don't create update rule */
1165       qs->count = 0;
1166       return -SYSTEMSOLVABLE;   /* supported but not installable */
1167     }
1168   if (allow_all)
1169     return s - pool->solvables;
1170   /* check if it is ok to keep the installed package */
1171   for (i = 0; i < qs->count; i++)
1172     {
1173       Solvable *ns = pool->solvables + qs->elements[i];
1174       if (s->evr == ns->evr && solvable_identical(s, ns))
1175         return s - pool->solvables;
1176     }
1177   /* nope, it must be some other package */
1178   return -SYSTEMSOLVABLE;
1179 }
1180
1181 /* add packages from the dup repositories to the update candidates
1182  * this isn't needed for the global dup mode as all packages are
1183  * from dup repos in that case */
1184 static void
1185 addduppackages(Solver *solv, Solvable *s, Queue *qs)
1186 {
1187   Queue dupqs;
1188   Id p, dupqsbuf[64];
1189   int i;
1190   int oldnoupdateprovide = solv->noupdateprovide;
1191
1192   queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
1193   solv->noupdateprovide = 1;
1194   policy_findupdatepackages(solv, s, &dupqs, 2);
1195   solv->noupdateprovide = oldnoupdateprovide;
1196   for (i = 0; i < dupqs.count; i++)
1197     {
1198       p = dupqs.elements[i];
1199       if (MAPTST(&solv->dupmap, p))
1200         queue_pushunique(qs, p);
1201     }
1202   queue_free(&dupqs);
1203 }
1204
1205 /*-------------------------------------------------------------------
1206  *
1207  * add rule for update
1208  *   (A|A1|A2|A3...)  An = update candidates for A
1209  *
1210  * s = (installed) solvable
1211  */
1212
1213 void
1214 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
1215 {
1216   /* installed packages get a special upgrade allowed rule */
1217   Pool *pool = solv->pool;
1218   Id p, d;
1219   Queue qs;
1220   Id qsbuf[64];
1221
1222   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1223   p = s - pool->solvables;
1224   /* find update candidates for 's' */
1225   if (solv->dupmap_all)
1226     p = finddistupgradepackages(solv, s, &qs, allow_all);
1227   else
1228     {
1229       policy_findupdatepackages(solv, s, &qs, allow_all);
1230       if (!allow_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
1231         addduppackages(solv, s, &qs);
1232     }
1233
1234 #ifdef ENABLE_LINKED_PKGS
1235   if (solv->instbuddy && solv->instbuddy[s - pool->solvables - solv->installed->start])
1236     {
1237       const char *name = pool_id2str(pool, s->name);
1238       if (strncmp(name, "pattern:", 8) == 0 || strncmp(name, "application:", 12) == 0)
1239         {
1240           /* a linked pseudo package. As it is linked, we do not need an update rule */
1241           /* nevertheless we set specialupdaters so we can update */
1242           solver_addrule(solv, 0, 0);
1243           if (!allow_all && qs.count)
1244             {
1245               if (p != -SYSTEMSOLVABLE)
1246                 queue_unshift(&qs, p);
1247               if (!solv->specialupdaters)
1248                 solv->specialupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
1249               solv->specialupdaters[s - pool->solvables - solv->installed->start] = pool_queuetowhatprovides(pool, &qs);
1250             }
1251           queue_free(&qs);
1252           return;
1253         }
1254     }
1255 #endif
1256
1257   if (!allow_all && qs.count && solv->multiversion.size)
1258     {
1259       int i, j;
1260
1261       for (i = 0; i < qs.count; i++)
1262         if (MAPTST(&solv->multiversion, qs.elements[i]))
1263           break;
1264       if (i < qs.count)
1265         {
1266           /* filter out all multiversion packages as they don't update */
1267           d = pool_queuetowhatprovides(pool, &qs);
1268           for (j = i; i < qs.count; i++)
1269              {
1270               if (MAPTST(&solv->multiversion, qs.elements[i]))
1271                 {
1272                   Solvable *ps = pool->solvables + qs.elements[i];
1273                   /* if keepexplicitobsoletes is set and the name is different,
1274                    * we assume that there is an obsoletes. XXX: not 100% correct */
1275                   if (solv->keepexplicitobsoletes && ps->name != s->name)
1276                     {
1277                       qs.elements[j++] = qs.elements[i];
1278                       continue;
1279                     }
1280                   /* it's ok if they have same nevra */
1281                   if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
1282                     continue;
1283                 }
1284               qs.elements[j++] = qs.elements[i];
1285             }
1286           if (j < qs.count)
1287             {
1288               if (d && solv->installed && s->repo == solv->installed &&
1289                   (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
1290                 {
1291                   if (!solv->specialupdaters)
1292                     solv->specialupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
1293                   solv->specialupdaters[s - pool->solvables - solv->installed->start] = d;
1294                 }
1295               if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
1296                 {
1297                   queue_push(&solv->orphaned, s - pool->solvables);     /* treat as orphaned */
1298                   j = qs.count;
1299                 }
1300               qs.count = j;
1301             }
1302           else if (p != -SYSTEMSOLVABLE)
1303             {
1304               /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
1305               queue_free(&qs);
1306               solver_addrule(solv, p, d);       /* allow update of s */
1307               return;
1308             }
1309         }
1310     }
1311   if (qs.count && p == -SYSTEMSOLVABLE)
1312     p = queue_shift(&qs);
1313   d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
1314   queue_free(&qs);
1315   solver_addrule(solv, p, d);   /* allow update of s */
1316 }
1317
1318 static inline void
1319 disableupdaterule(Solver *solv, Id p)
1320 {
1321   Rule *r;
1322
1323   MAPSET(&solv->noupdate, p - solv->installed->start);
1324   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1325   if (r->p && r->d >= 0)
1326     solver_disablerule(solv, r);
1327   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1328   if (r->p && r->d >= 0)
1329     solver_disablerule(solv, r);
1330   if (solv->bestrules_pkg)
1331     {
1332       int i, ni;
1333       ni = solv->bestrules_end - solv->bestrules;
1334       for (i = 0; i < ni; i++)
1335         if (solv->bestrules_pkg[i] == p)
1336           solver_disablerule(solv, solv->rules + solv->bestrules + i);
1337     }
1338 }
1339
1340 static inline void
1341 reenableupdaterule(Solver *solv, Id p)
1342 {
1343   Pool *pool = solv->pool;
1344   Rule *r;
1345
1346   MAPCLR(&solv->noupdate, p - solv->installed->start);
1347   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1348   if (r->p)
1349     {
1350       if (r->d < 0)
1351         {
1352           solver_enablerule(solv, r);
1353           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1354             {
1355               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1356               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1357             }
1358         }
1359     }
1360   else
1361     {
1362       r = solv->rules + solv->featurerules + (p - solv->installed->start);
1363       if (r->p && r->d < 0)
1364         {
1365           solver_enablerule(solv, r);
1366           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1367             {
1368               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1369               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1370             }
1371         }
1372     }
1373   if (solv->bestrules_pkg)
1374     {
1375       int i, ni;
1376       ni = solv->bestrules_end - solv->bestrules;
1377       for (i = 0; i < ni; i++)
1378         if (solv->bestrules_pkg[i] == p)
1379           solver_enablerule(solv, solv->rules + solv->bestrules + i);
1380     }
1381 }
1382
1383
1384 /***********************************************************************
1385  ***
1386  ***  Infarch rule part
1387  ***
1388  ***  Infarch rules make sure the solver uses the best architecture of
1389  ***  a package if multiple archetectures are available
1390  ***
1391  ***/
1392
1393 void
1394 solver_addinfarchrules(Solver *solv, Map *addedmap)
1395 {
1396   Pool *pool = solv->pool;
1397   Repo *installed = pool->installed;
1398   int first, i, j;
1399   Id p, pp, a, aa, bestarch;
1400   Solvable *s, *ps, *bests;
1401   Queue badq, allowedarchs;
1402   Queue lsq;
1403
1404   queue_init(&badq);
1405   queue_init(&allowedarchs);
1406   queue_init(&lsq);
1407   solv->infarchrules = solv->nrules;
1408   for (i = 1; i < pool->nsolvables; i++)
1409     {
1410       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1411         continue;
1412       s = pool->solvables + i;
1413       first = i;
1414       bestarch = 0;
1415       bests = 0;
1416       queue_empty(&allowedarchs);
1417       FOR_PROVIDES(p, pp, s->name)
1418         {
1419           ps = pool->solvables + p;
1420           if (ps->name != s->name || !MAPTST(addedmap, p))
1421             continue;
1422           if (p == i)
1423             first = 0;
1424           if (first)
1425             break;
1426           a = ps->arch;
1427           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1428           if (a != 1 && installed && ps->repo == installed)
1429             {
1430               if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1431                 queue_pushunique(&allowedarchs, ps->arch);      /* also ok to keep this architecture */
1432               continue;         /* ignore installed solvables when calculating the best arch */
1433             }
1434           if (a && a != 1 && (!bestarch || a < bestarch))
1435             {
1436               bestarch = a;
1437               bests = ps;
1438             }
1439         }
1440       if (first)
1441         continue;
1442
1443       /* speed up common case where installed package already has best arch */
1444       if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1445         allowedarchs.count--;   /* installed arch is best */
1446
1447       if (allowedarchs.count && pool->implicitobsoleteusescolors && installed && bestarch)
1448         {
1449           /* need an extra pass for lockstep checking: we only allow to keep an inferior arch
1450            * if the corresponding installed package is not lock-stepped */
1451           queue_empty(&allowedarchs);
1452           FOR_PROVIDES(p, pp, s->name)
1453             {
1454               Id p2, pp2;
1455               ps = pool->solvables + p;
1456               if (ps->name != s->name || ps->repo != installed || !MAPTST(addedmap, p))
1457                 continue;
1458               if (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1459                 continue;
1460               a = ps->arch;
1461               a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1462               if (!a)
1463                 {
1464                   queue_pushunique(&allowedarchs, ps->arch);    /* strange arch, allow */
1465                   continue;
1466                 }
1467               if (a == 1 || ((a ^ bestarch) & 0xffff0000) == 0)
1468                 continue;
1469               /* have installed package with inferior arch, check if lock-stepped */
1470               FOR_PROVIDES(p2, pp2, s->name)
1471                 {
1472                   Solvable *s2 = pool->solvables + p2;
1473                   Id a2;
1474                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1475                     continue;
1476                   a2 = s2->arch;
1477                   a2 = (a2 <= pool->lastarch) ? pool->id2arch[a2] : 0;
1478                   if (a2 && (a2 == 1 || ((a2 ^ bestarch) & 0xffff0000) == 0))
1479                     break;
1480                 }
1481               if (!p2)
1482                 queue_pushunique(&allowedarchs, ps->arch);
1483             }
1484         }
1485
1486       /* find all bad packages */
1487       queue_empty(&badq);
1488       FOR_PROVIDES(p, pp, s->name)
1489         {
1490           ps = pool->solvables + p;
1491           if (ps->name != s->name || !MAPTST(addedmap, p))
1492             continue;
1493           a = ps->arch;
1494           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1495           if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1496             {
1497               if (installed && ps->repo == installed)
1498                 {
1499                   if (pool->implicitobsoleteusescolors)
1500                     queue_push(&badq, p);               /* special lock-step handling, see below */
1501                   continue;     /* always ok to keep an installed package */
1502                 }
1503               for (j = 0; j < allowedarchs.count; j++)
1504                 {
1505                   aa = allowedarchs.elements[j];
1506                   if (ps->arch == aa)
1507                     break;
1508                   aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1509                   if (aa && ((a ^ aa) & 0xffff0000) == 0)
1510                     break;      /* compatible */
1511                 }
1512               if (j == allowedarchs.count)
1513                 queue_push(&badq, p);
1514             }
1515         }
1516
1517       /* block all solvables in the badq! */
1518       for (j = 0; j < badq.count; j++)
1519         {
1520           p = badq.elements[j];
1521           /* lock-step */
1522           if (pool->implicitobsoleteusescolors)
1523             {
1524               Id p2;
1525               int haveinstalled = 0;
1526               queue_empty(&lsq);
1527               FOR_PROVIDES(p2, pp, s->name)
1528                 {
1529                   Solvable *s2 = pool->solvables + p2;
1530                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1531                     continue;
1532                   a = s2->arch;
1533                   a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1534                   if (a && (a == 1 || ((a ^ bestarch) & 0xffff000) == 0))
1535                     {
1536                       queue_push(&lsq, p2);
1537                       if (installed && s2->repo == installed)
1538                         haveinstalled = 1;
1539                     }
1540                 }
1541               if (installed && pool->solvables[p].repo == installed && !haveinstalled)
1542                 continue;       /* installed package not in lock-step */
1543             }
1544           solver_addrule(solv, -p, lsq.count ? pool_queuetowhatprovides(pool, &lsq) : 0);
1545         }
1546     }
1547   queue_free(&lsq);
1548   queue_free(&badq);
1549   queue_free(&allowedarchs);
1550   solv->infarchrules_end = solv->nrules;
1551 }
1552
1553 static inline void
1554 disableinfarchrule(Solver *solv, Id name)
1555 {
1556   Pool *pool = solv->pool;
1557   Rule *r;
1558   int i;
1559   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1560     {
1561       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1562         solver_disablerule(solv, r);
1563     }
1564 }
1565
1566 static inline void
1567 reenableinfarchrule(Solver *solv, Id name)
1568 {
1569   Pool *pool = solv->pool;
1570   Rule *r;
1571   int i;
1572   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1573     {
1574       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1575         {
1576           solver_enablerule(solv, r);
1577           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1578             {
1579               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1580               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1581             }
1582         }
1583     }
1584 }
1585
1586
1587 /***********************************************************************
1588  ***
1589  ***  Dup rule part
1590  ***
1591  ***  Dup rules make sure a package is selected from the specified dup
1592  ***  repositories if an update candidate is included in one of them.
1593  ***
1594  ***/
1595
1596 static inline void
1597 add_cleandeps_package(Solver *solv, Id p)
1598 {
1599   if (!solv->cleandeps_updatepkgs)
1600     {
1601       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
1602       queue_init(solv->cleandeps_updatepkgs);
1603     }
1604   queue_pushunique(solv->cleandeps_updatepkgs, p);
1605 }
1606
1607 static inline void
1608 solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
1609 {
1610   Pool *pool = solv->pool;
1611   Solvable *ps, *s = pool->solvables + p;
1612   Repo *installed = solv->installed;
1613   Id pi, pip, obs, *obsp;
1614
1615   MAPSET(&solv->dupinvolvedmap, p);
1616   if (targeted)
1617     MAPSET(&solv->dupmap, p);
1618   FOR_PROVIDES(pi, pip, s->name)
1619     {
1620       ps = pool->solvables + pi;
1621       if (ps->name != s->name)
1622         continue;
1623       MAPSET(&solv->dupinvolvedmap, pi);
1624       if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1625         {
1626           Id *opp, pi2;
1627           for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1628             if (pool->solvables[pi2].repo != installed)
1629               MAPSET(&solv->dupinvolvedmap, pi2);
1630         }
1631       if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1632         {
1633           if (!solv->bestupdatemap.size)
1634             map_grow(&solv->bestupdatemap, installed->end - installed->start);
1635           MAPSET(&solv->bestupdatemap, pi - installed->start);
1636         }
1637       if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1638         add_cleandeps_package(solv, pi);
1639       if (!targeted && ps->repo != installed)
1640         MAPSET(&solv->dupmap, pi);
1641     }
1642   if (s->repo == installed && solv->obsoletes && solv->obsoletes[p - installed->start])
1643     {
1644       Id *opp;
1645       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1646         {
1647           ps = pool->solvables + pi;
1648           if (ps->repo == installed)
1649             continue;
1650           MAPSET(&solv->dupinvolvedmap, pi);
1651           if (!targeted)
1652             MAPSET(&solv->dupmap, pi);
1653         }
1654     }
1655   if (targeted && s->repo != installed && s->obsoletes)
1656     {
1657       /* XXX: check obsoletes/provides combination */
1658       obsp = s->repo->idarraydata + s->obsoletes;
1659       while ((obs = *obsp++) != 0)
1660         {
1661           FOR_PROVIDES(pi, pip, obs)
1662             {
1663               Solvable *ps = pool->solvables + pi;
1664               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1665                 continue;
1666               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1667                 continue;
1668               MAPSET(&solv->dupinvolvedmap, pi);
1669               if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1670                 {
1671                   Id *opp, pi2;
1672                   for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1673                     if (pool->solvables[pi2].repo != installed)
1674                       MAPSET(&solv->dupinvolvedmap, pi2);
1675                 }
1676               if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1677                 {
1678                   if (!solv->bestupdatemap.size)
1679                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
1680                   MAPSET(&solv->bestupdatemap, pi - installed->start);
1681                 }
1682               if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1683                 add_cleandeps_package(solv, pi);
1684             }
1685         }
1686     }
1687 }
1688
1689 void
1690 solver_createdupmaps(Solver *solv)
1691 {
1692   Queue *job = &solv->job;
1693   Pool *pool = solv->pool;
1694   Repo *installed = solv->installed;
1695   Id select, how, what, p, pp;
1696   Solvable *s;
1697   int i, targeted;
1698
1699   map_init(&solv->dupmap, pool->nsolvables);
1700   map_init(&solv->dupinvolvedmap, pool->nsolvables);
1701   for (i = 0; i < job->count; i += 2)
1702     {
1703       how = job->elements[i];
1704       select = job->elements[i] & SOLVER_SELECTMASK;
1705       what = job->elements[i + 1];
1706       switch (how & SOLVER_JOBMASK)
1707         {
1708         case SOLVER_DISTUPGRADE:
1709           if (select == SOLVER_SOLVABLE_REPO)
1710             {
1711               Repo *repo;
1712               if (what <= 0 || what > pool->nrepos)
1713                 break;
1714               repo = pool_id2repo(pool, what);
1715               if (!repo)
1716                 break;
1717               if (repo != installed && !(how & SOLVER_TARGETED) && solv->noautotarget)
1718                 break;
1719               targeted = repo != installed || (how & SOLVER_TARGETED) != 0;
1720               FOR_REPO_SOLVABLES(repo, p, s)
1721                 {
1722                   if (repo != installed && !pool_installable(pool, s))
1723                     continue;
1724                   solver_addtodupmaps(solv, p, how, targeted);
1725                 }
1726             }
1727           else
1728             {
1729               targeted = how & SOLVER_TARGETED ? 1 : 0;
1730               if (installed && !targeted && !solv->noautotarget)
1731                 {
1732                   FOR_JOB_SELECT(p, pp, select, what)
1733                     if (pool->solvables[p].repo == installed)
1734                       break;
1735                   targeted = p == 0;
1736                 }
1737               else if (!installed && !solv->noautotarget)
1738                 targeted = 1;
1739               FOR_JOB_SELECT(p, pp, select, what)
1740                 {
1741                   Solvable *s = pool->solvables + p;
1742                   if (!s->repo)
1743                     continue;
1744                   if (s->repo != installed && !targeted)
1745                     continue;
1746                   if (s->repo != installed && !pool_installable(pool, s))
1747                     continue;
1748                   solver_addtodupmaps(solv, p, how, targeted);
1749                 }
1750             }
1751           break;
1752         default:
1753           break;
1754         }
1755     }
1756   MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1757 }
1758
1759 void
1760 solver_freedupmaps(Solver *solv)
1761 {
1762   map_free(&solv->dupmap);
1763   /* we no longer free solv->dupinvolvedmap as we need it in
1764    * policy's priority pruning code. sigh. */
1765 }
1766
1767 void
1768 solver_addduprules(Solver *solv, Map *addedmap)
1769 {
1770   Pool *pool = solv->pool;
1771   Id p, pp;
1772   Solvable *s, *ps;
1773   int first, i;
1774
1775   solv->duprules = solv->nrules;
1776   for (i = 1; i < pool->nsolvables; i++)
1777     {
1778       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1779         continue;
1780       s = pool->solvables + i;
1781       first = i;
1782       FOR_PROVIDES(p, pp, s->name)
1783         {
1784           ps = pool->solvables + p;
1785           if (ps->name != s->name || !MAPTST(addedmap, p))
1786             continue;
1787           if (p == i)
1788             first = 0;
1789           if (first)
1790             break;
1791           if (!MAPTST(&solv->dupinvolvedmap, p))
1792             continue;
1793           if (solv->installed && ps->repo == solv->installed)
1794             {
1795               if (!solv->updatemap.size)
1796                 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1797               MAPSET(&solv->updatemap, p - solv->installed->start);
1798               if (!MAPTST(&solv->dupmap, p))
1799                 {
1800                   Id ip, ipp;
1801                   /* is installed identical to a good one? */
1802                   FOR_PROVIDES(ip, ipp, ps->name)
1803                     {
1804                       Solvable *is = pool->solvables + ip;
1805                       if (!MAPTST(&solv->dupmap, ip))
1806                         continue;
1807                       if (is->evr == ps->evr && solvable_identical(ps, is))
1808                         break;
1809                     }
1810                   if (!ip)
1811                     solver_addrule(solv, -p, 0);        /* no match, sorry */
1812                   else
1813                     MAPSET(&solv->dupmap, p);           /* for best rules processing */
1814                 }
1815             }
1816           else if (!MAPTST(&solv->dupmap, p))
1817             solver_addrule(solv, -p, 0);
1818         }
1819     }
1820   solv->duprules_end = solv->nrules;
1821 }
1822
1823
1824 static inline void
1825 disableduprule(Solver *solv, Id name)
1826 {
1827   Pool *pool = solv->pool;
1828   Rule *r;
1829   int i;
1830   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1831     {
1832       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1833         solver_disablerule(solv, r);
1834     }
1835 }
1836
1837 static inline void
1838 reenableduprule(Solver *solv, Id name)
1839 {
1840   Pool *pool = solv->pool;
1841   Rule *r;
1842   int i;
1843   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
1844     {
1845       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1846         {
1847           solver_enablerule(solv, r);
1848           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1849             {
1850               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1851               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1852             }
1853         }
1854     }
1855 }
1856
1857
1858 /***********************************************************************
1859  ***
1860  ***  Policy rule disabling/reenabling
1861  ***
1862  ***  Disable all policy rules that conflict with our jobs. If a job
1863  ***  gets disabled later on, reenable the involved policy rules again.
1864  ***
1865  ***/
1866
1867 #define DISABLE_UPDATE  1
1868 #define DISABLE_INFARCH 2
1869 #define DISABLE_DUP     3
1870
1871 /*
1872  * add all installed packages that package p obsoletes to Queue q.
1873  * Package p is not installed. Also, we know that if
1874  * solv->keepexplicitobsoletes is not set, p is not in the multiversion map.
1875  * Entries may get added multiple times.
1876  */
1877 static void
1878 add_obsoletes(Solver *solv, Id p, Queue *q)
1879 {
1880   Pool *pool = solv->pool;
1881   Repo *installed = solv->installed;
1882   Id p2, pp2;
1883   Solvable *s = pool->solvables + p;
1884   Id obs, *obsp;
1885   Id lastp2 = 0;
1886
1887   if (!solv->keepexplicitobsoletes || !(solv->multiversion.size && MAPTST(&solv->multiversion, p)))
1888     {
1889       FOR_PROVIDES(p2, pp2, s->name)
1890         {
1891           Solvable *ps = pool->solvables + p2;
1892           if (ps->repo != installed)
1893             continue;
1894           if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1895             continue;
1896           if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
1897             continue;
1898           queue_push(q, p2);
1899           lastp2 = p2;
1900         }
1901     }
1902   if (!s->obsoletes)
1903     return;
1904   obsp = s->repo->idarraydata + s->obsoletes;
1905   while ((obs = *obsp++) != 0)
1906     FOR_PROVIDES(p2, pp2, obs)
1907       {
1908         Solvable *ps = pool->solvables + p2;
1909         if (ps->repo != installed)
1910           continue;
1911         if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1912           continue;
1913         if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1914           continue;
1915         if (p2 == lastp2)
1916           continue;
1917         queue_push(q, p2);
1918         lastp2 = p2;
1919       }
1920 }
1921
1922 /*
1923  * Call add_obsoletes and intersect the result with the
1924  * elements in Queue q starting at qstart.
1925  * Assumes that it's the first call if qstart == q->count.
1926  * May use auxillary map m for the intersection process, all
1927  * elements of q starting at qstart must have their bit cleared.
1928  * (This is also true after the function returns.)
1929  */
1930 static void
1931 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
1932 {
1933   int i, j;
1934   int qcount = q->count;
1935
1936   add_obsoletes(solv, p, q);
1937   if (qcount == qstart)
1938     return;     /* first call */
1939   if (qcount == q->count)
1940     j = qstart; 
1941   else if (qcount == qstart + 1)
1942     {
1943       /* easy if there's just one element */
1944       j = qstart;
1945       for (i = qcount; i < q->count; i++)
1946         if (q->elements[i] == q->elements[qstart])
1947           {
1948             j++;        /* keep the element */
1949             break;
1950           }
1951     }
1952   else if (!m->size && q->count - qstart <= 8)
1953     {
1954       /* faster than a map most of the time */
1955       int k;
1956       for (i = j = qstart; i < qcount; i++)
1957         {
1958           Id ip = q->elements[i];
1959           for (k = qcount; k < q->count; k++)
1960             if (q->elements[k] == ip)
1961               {
1962                 q->elements[j++] = ip;
1963                 break;
1964               }
1965         }
1966     }
1967   else
1968     {
1969       /* for the really pathologic cases we use the map */
1970       Repo *installed = solv->installed;
1971       if (!m->size)
1972         map_init(m, installed->end - installed->start);
1973       for (i = qcount; i < q->count; i++)
1974         MAPSET(m, q->elements[i] - installed->start);
1975       for (i = j = qstart; i < qcount; i++)
1976         if (MAPTST(m, q->elements[i] - installed->start))
1977           {
1978             MAPCLR(m, q->elements[i] - installed->start);
1979             q->elements[j++] = q->elements[i];
1980           }
1981     }
1982   queue_truncate(q, j);
1983 }
1984
1985 static void
1986 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1987 {
1988   Pool *pool = solv->pool;
1989   Id select, p, pp;
1990   Repo *installed;
1991   Solvable *s;
1992   int i, j, set, qstart;
1993   Map omap;
1994
1995   installed = solv->installed;
1996   select = how & SOLVER_SELECTMASK;
1997   switch (how & SOLVER_JOBMASK)
1998     {
1999     case SOLVER_INSTALL:
2000       set = how & SOLVER_SETMASK;
2001       if (!(set & SOLVER_NOAUTOSET))
2002         {
2003           /* automatically add set bits by analysing the job */
2004           if (select == SOLVER_SOLVABLE_NAME)
2005             set |= SOLVER_SETNAME;
2006           if (select == SOLVER_SOLVABLE)
2007             set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
2008           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
2009             {
2010               Reldep *rd = GETRELDEP(pool, what);
2011               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
2012                 {
2013                   if (pool->disttype != DISTTYPE_DEB)
2014                     {
2015                       const char *rel = strrchr(pool_id2str(pool, rd->evr), '-');
2016                       set |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
2017                     }
2018                   else
2019                     set |= SOLVER_SETEVR;
2020                 }
2021               if (rd->flags <= 7 && ISRELDEP(rd->name))
2022                 rd = GETRELDEP(pool, rd->name);
2023               if (rd->flags == REL_ARCH)
2024                 set |= SOLVER_SETARCH;
2025             }
2026         }
2027       else
2028         set &= ~SOLVER_NOAUTOSET;
2029       if (!set)
2030         return;
2031       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
2032         {
2033           if (select == SOLVER_SOLVABLE)
2034             queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
2035           else
2036             {
2037               int qcnt = q->count;
2038               /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
2039                  they are not useful for SOLVER_INSTALL jobs anyway */
2040               FOR_JOB_SELECT(p, pp, select, what)
2041                 {
2042                   s = pool->solvables + p;
2043                   /* unify names */
2044                   for (i = qcnt; i < q->count; i += 2)
2045                     if (q->elements[i + 1] == s->name)
2046                       break;
2047                   if (i < q->count)
2048                     continue;
2049                   queue_push2(q, DISABLE_INFARCH, s->name);
2050                 }
2051             }
2052         }
2053       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
2054         {
2055           if (select == SOLVER_SOLVABLE)
2056             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
2057           else
2058             {
2059               int qcnt = q->count;
2060               FOR_JOB_SELECT(p, pp, select, what)
2061                 {
2062                   s = pool->solvables + p;
2063                   /* unify names */
2064                   for (i = qcnt; i < q->count; i += 2)
2065                     if (q->elements[i + 1] == s->name)
2066                       break;
2067                   if (i < q->count)
2068                     continue;
2069                   queue_push2(q, DISABLE_DUP, s->name);
2070                 }
2071             }
2072         }
2073       if (!installed || installed->end == installed->start)
2074         return;
2075       /* now the hard part: disable some update rules */
2076
2077       /* first check if we have multiversion or installed packages in the job */
2078       i = j = 0;
2079       FOR_JOB_SELECT(p, pp, select, what)
2080         {
2081           if (pool->solvables[p].repo == installed)
2082             j = p;
2083           else if (solv->multiversion.size && MAPTST(&solv->multiversion, p) && !solv->keepexplicitobsoletes)
2084             return;
2085           i++;
2086         }
2087       if (j)    /* have installed packages */
2088         {
2089           /* this is for dupmap_all jobs, it can go away if we create
2090            * duprules for them */
2091           if (i == 1 && (set & SOLVER_SETREPO) != 0)
2092             queue_push2(q, DISABLE_UPDATE, j);
2093           return;
2094         }
2095
2096       omap.size = 0;
2097       qstart = q->count;
2098       FOR_JOB_SELECT(p, pp, select, what)
2099         {
2100           intersect_obsoletes(solv, p, q, qstart, &omap);
2101           if (q->count == qstart)
2102             break;
2103         }
2104       if (omap.size)
2105         map_free(&omap);
2106
2107       if (qstart == q->count)
2108         return;         /* nothing to prune */
2109
2110       /* convert result to (DISABLE_UPDATE, p) pairs */
2111       i = q->count;
2112       for (j = qstart; j < i; j++)
2113         queue_push(q, q->elements[j]);
2114       for (j = qstart; j < q->count; j += 2)
2115         {
2116           q->elements[j] = DISABLE_UPDATE;
2117           q->elements[j + 1] = q->elements[i++];
2118         }
2119
2120       /* now that we know which installed packages are obsoleted check each of them */
2121       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
2122         return;         /* all is set, nothing to do */
2123
2124       for (i = j = qstart; i < q->count; i += 2)
2125         {
2126           Solvable *is = pool->solvables + q->elements[i + 1];
2127           FOR_JOB_SELECT(p, pp, select, what)
2128             {
2129               int illegal = 0;
2130               s = pool->solvables + p;
2131               if ((set & SOLVER_SETEVR) != 0)
2132                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
2133               if ((set & SOLVER_SETNAME) != 0)
2134                 illegal |= POLICY_ILLEGAL_NAMECHANGE;   /* ignore */
2135               if ((set & SOLVER_SETARCH) != 0)
2136                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
2137               if ((set & SOLVER_SETVENDOR) != 0)
2138                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
2139               illegal = policy_is_illegal(solv, is, s, illegal);
2140               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
2141                 {
2142                   /* it's ok if the EV is different */
2143                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
2144                     illegal = 0;
2145                 }
2146               if (illegal)
2147                 break;
2148             }
2149           if (!p)
2150             {   
2151               /* no package conflicts with the update rule */
2152               /* thus keep the DISABLE_UPDATE */
2153               q->elements[j + 1] = q->elements[i + 1];
2154               j += 2;
2155             }
2156         }
2157       queue_truncate(q, j);
2158       return;
2159
2160     case SOLVER_ERASE:
2161       if (!installed)
2162         break;
2163       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2164         FOR_REPO_SOLVABLES(installed, p, s)
2165           queue_push2(q, DISABLE_UPDATE, p);
2166       FOR_JOB_SELECT(p, pp, select, what)
2167         if (pool->solvables[p].repo == installed)
2168           {
2169             queue_push2(q, DISABLE_UPDATE, p);
2170 #ifdef ENABLE_LINKED_PKGS
2171             if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
2172               queue_push2(q, DISABLE_UPDATE, solv->instbuddy[p - installed->start]);
2173 #endif
2174           }
2175       return;
2176     default:
2177       return;
2178     }
2179 }
2180
2181 /* disable all policy rules that are in conflict with our job list */
2182 void
2183 solver_disablepolicyrules(Solver *solv)
2184 {
2185   Queue *job = &solv->job;
2186   int i, j;
2187   Queue allq;
2188   Rule *r;
2189   Id lastjob = -1;
2190   Id allqbuf[128];
2191
2192   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2193
2194   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2195     {
2196       r = solv->rules + i;
2197       if (r->d < 0)     /* disabled? */
2198         continue;
2199       j = solv->ruletojob.elements[i - solv->jobrules];
2200       if (j == lastjob)
2201         continue;
2202       lastjob = j;
2203       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2204     }
2205   if (solv->cleandepsmap.size)
2206     {
2207       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2208       for (i = solv->installed->start; i < solv->installed->end; i++)
2209         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
2210           queue_push2(&allq, DISABLE_UPDATE, i);
2211     }
2212   MAPZERO(&solv->noupdate);
2213   for (i = 0; i < allq.count; i += 2)
2214     {
2215       Id type = allq.elements[i], arg = allq.elements[i + 1];
2216       switch(type)
2217         {
2218         case DISABLE_UPDATE:
2219           disableupdaterule(solv, arg);
2220           break;
2221         case DISABLE_INFARCH:
2222           disableinfarchrule(solv, arg);
2223           break;
2224         case DISABLE_DUP:
2225           disableduprule(solv, arg);
2226           break;
2227         default:
2228           break;
2229         }
2230     }
2231   queue_free(&allq);
2232 }
2233
2234 /* we just disabled job #jobidx, now reenable all policy rules that were
2235  * disabled because of this job */
2236 void
2237 solver_reenablepolicyrules(Solver *solv, int jobidx)
2238 {
2239   Queue *job = &solv->job;
2240   int i, j, k, ai;
2241   Queue q, allq;
2242   Rule *r;
2243   Id lastjob = -1;
2244   Id qbuf[32], allqbuf[32];
2245
2246   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2247   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
2248   if (!q.count)
2249     {
2250       queue_free(&q);
2251       return;
2252     }
2253   /* now remove everything from q that is disabled by other jobs */
2254
2255   /* first remove cleandeps packages, they count as DISABLE_UPDATE */
2256   if (solv->cleandepsmap.size)
2257     {
2258       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2259       for (j = k = 0; j < q.count; j += 2)
2260         {
2261           if (q.elements[j] == DISABLE_UPDATE)
2262             {
2263               Id p = q.elements[j + 1];
2264               if (p >= solv->installed->start && p < solv->installed->end && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2265                 continue;       /* remove element from q */
2266             }
2267           q.elements[k++] = q.elements[j];
2268           q.elements[k++] = q.elements[j + 1];
2269         }
2270       q.count = k;
2271       if (!q.count)
2272         {
2273           queue_free(&q);
2274           return;
2275         }
2276     }
2277
2278   /* now go through the disable list of all other jobs */
2279   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2280   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2281     {
2282       r = solv->rules + i;
2283       if (r->d < 0)     /* disabled? */
2284         continue;
2285       j = solv->ruletojob.elements[i - solv->jobrules];
2286       if (j == lastjob)
2287         continue;
2288       lastjob = j;
2289       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2290       if (!allq.count)
2291         continue;
2292       /* remove all elements in allq from q */
2293       for (j = k = 0; j < q.count; j += 2)
2294         {
2295           Id type = q.elements[j], arg = q.elements[j + 1];
2296           for (ai = 0; ai < allq.count; ai += 2)
2297             if (allq.elements[ai] == type && allq.elements[ai + 1] == arg)
2298               break;
2299           if (ai < allq.count)
2300             continue;   /* found it in allq, remove element from q */
2301           q.elements[k++] = q.elements[j];
2302           q.elements[k++] = q.elements[j + 1];
2303         }
2304       q.count = k;
2305       if (!q.count)
2306         {
2307           queue_free(&q);
2308           queue_free(&allq);
2309           return;
2310         }
2311       queue_empty(&allq);
2312     }
2313   queue_free(&allq);
2314
2315   /* now re-enable anything that's left in q */
2316   for (j = 0; j < q.count; j += 2)
2317     {
2318       Id type = q.elements[j], arg = q.elements[j + 1];
2319       switch(type)
2320         {
2321         case DISABLE_UPDATE:
2322           reenableupdaterule(solv, arg);
2323           break;
2324         case DISABLE_INFARCH:
2325           reenableinfarchrule(solv, arg);
2326           break;
2327         case DISABLE_DUP:
2328           reenableduprule(solv, arg);
2329           break;
2330         }
2331     }
2332   queue_free(&q);
2333 }
2334
2335 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
2336  * disabled because of this */
2337 void
2338 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
2339 {
2340   Queue *job = &solv->job;
2341   int i, j;
2342   Queue allq;
2343   Rule *r;
2344   Id lastjob = -1;
2345   Id allqbuf[128];
2346
2347   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2348   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2349     {
2350       r = solv->rules + i;
2351       if (r->d < 0)     /* disabled? */
2352         continue;
2353       j = solv->ruletojob.elements[i - solv->jobrules];
2354       if (j == lastjob)
2355         continue;
2356       lastjob = j;
2357       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2358     }
2359   for (i = 0; i < allq.count; i += 2)
2360     if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
2361       break;
2362   if (i == allq.count)
2363     reenableupdaterule(solv, pkg);
2364   queue_free(&allq);
2365 }
2366
2367
2368 /***********************************************************************
2369  ***
2370  ***  Rule info part, tell the user what the rule is about.
2371  ***
2372  ***/
2373
2374 static void
2375 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
2376 {
2377   Pool *pool = solv->pool;
2378   Rule *r;
2379   Id w2, op, od, ow2;
2380
2381   /* check if this creates the rule we're searching for */
2382   r = solv->rules + solv->ruleinfoq->elements[0];
2383   op = r->p;
2384   od = r->d < 0 ? -r->d - 1 : r->d;
2385   ow2 = 0;
2386
2387   /* normalize */
2388   w2 = d > 0 ? 0 : d;
2389   if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
2390     {
2391       w2 = pool->whatprovidesdata[d];
2392       d = 0;
2393     }
2394   if (p > 0 && d < 0)           /* this hack is used for package links and complex deps */
2395     {
2396       w2 = p;
2397       p = d;
2398     }
2399
2400   if (d > 0)
2401     {
2402       if (p != op && !od)
2403         return;
2404       if (d != od)
2405         {
2406           Id *dp = pool->whatprovidesdata + d;
2407           Id *odp = pool->whatprovidesdata + od;
2408           while (*dp)
2409             if (*dp++ != *odp++)
2410               return;
2411           if (*odp)
2412             return;
2413         }
2414       w2 = 0;
2415       /* handle multiversion conflict rules */
2416       if (p < 0 && pool->whatprovidesdata[d] < 0)
2417         {
2418           w2 = pool->whatprovidesdata[d];
2419           /* XXX: free memory */
2420         }
2421     }
2422   else
2423     {
2424       if (od)
2425         return;
2426       ow2 = r->w2;
2427       if (p > w2)
2428         {
2429           if (w2 != op || p != ow2)
2430             return;
2431         }
2432       else
2433         {
2434           if (p != op || w2 != ow2)
2435             return;
2436         }
2437     }
2438   /* yep, rule matches. record info */
2439   queue_push(solv->ruleinfoq, type);
2440   if (type == SOLVER_RULE_RPM_SAME_NAME)
2441     {
2442       /* we normalize same name order */
2443       queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
2444       queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
2445     }
2446   else
2447     {
2448       queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2449       queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
2450     }
2451   queue_push(solv->ruleinfoq, dep);
2452 }
2453
2454 static int
2455 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2456 {
2457   const Id *a = ap, *b = bp;
2458   int r;
2459
2460   r = a[0] - b[0];
2461   if (r)
2462     return r;
2463   r = a[1] - b[1];
2464   if (r)
2465     return r;
2466   r = a[2] - b[2];
2467   if (r)
2468     return r;
2469   r = a[3] - b[3];
2470   if (r)
2471     return r;
2472   return 0;
2473 }
2474
2475 static void
2476 getrpmruleinfos(Solver *solv, Rule *r, Queue *rq)
2477 {
2478   Pool *pool = solv->pool;
2479   Id l, pp;
2480   if (r->p >= 0)
2481     return;
2482   queue_push(rq, r - solv->rules);      /* push the rule we're interested in */
2483   solv->ruleinfoq = rq;
2484   FOR_RULELITERALS(l, pp, r)
2485     {
2486       if (l >= 0)
2487         break;
2488       solver_addrpmrulesforsolvable(solv, pool->solvables - l, 0);
2489     }
2490 #ifdef ENABLE_LINKED_PKGS
2491   FOR_RULELITERALS(l, pp, r)
2492     {
2493       if (l < 0 || l != r->p)
2494         break;
2495       if (!strchr(pool_id2str(pool, pool->solvables[l].name), ':') || !has_package_link(pool, pool->solvables + l))
2496         break;
2497       add_package_link(solv, pool->solvables + l, 0, 0);
2498     }
2499 #endif
2500   solv->ruleinfoq = 0;
2501   queue_shift(rq);
2502 }
2503
2504 int
2505 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2506 {
2507   Rule *r = solv->rules + rid;
2508   int i, j;
2509
2510   queue_empty(rq);
2511   if (rid <= 0 || rid >= solv->rpmrules_end)
2512     {
2513       Id type, from, to, dep;
2514       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2515       queue_push(rq, type);
2516       queue_push(rq, from);
2517       queue_push(rq, to);
2518       queue_push(rq, dep);
2519       return 1;
2520     }
2521   getrpmruleinfos(solv, r, rq);
2522   /* now sort & unify em */
2523   if (!rq->count)
2524     return 0;
2525   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2526   /* throw out identical entries */
2527   for (i = j = 0; i < rq->count; i += 4)
2528     {
2529       if (j)
2530         {
2531           if (rq->elements[i] == rq->elements[j - 4] &&
2532               rq->elements[i + 1] == rq->elements[j - 3] &&
2533               rq->elements[i + 2] == rq->elements[j - 2] &&
2534               rq->elements[i + 3] == rq->elements[j - 1])
2535             continue;
2536         }
2537       rq->elements[j++] = rq->elements[i];
2538       rq->elements[j++] = rq->elements[i + 1];
2539       rq->elements[j++] = rq->elements[i + 2];
2540       rq->elements[j++] = rq->elements[i + 3];
2541     }
2542   rq->count = j;
2543   return j / 4;
2544 }
2545
2546 SolverRuleinfo
2547 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2548 {
2549   Pool *pool = solv->pool;
2550   Rule *r = solv->rules + rid;
2551   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2552
2553   if (fromp)
2554     *fromp = 0;
2555   if (top)
2556     *top = 0;
2557   if (depp)
2558     *depp = 0;
2559   if (rid > 0 && rid < solv->rpmrules_end)
2560     {
2561       Queue rq;
2562       int i;
2563
2564       if (r->p >= 0)
2565         return SOLVER_RULE_RPM;
2566       if (fromp)
2567         *fromp = -r->p;
2568       queue_init(&rq);
2569       getrpmruleinfos(solv, r, &rq);
2570       type = SOLVER_RULE_RPM;
2571       for (i = 0; i < rq.count; i += 4)
2572         {
2573           Id qt, qo, qp, qd;
2574           qt = rq.elements[i];
2575           qp = rq.elements[i + 1];
2576           qo = rq.elements[i + 2];
2577           qd = rq.elements[i + 3];
2578           if (type == SOLVER_RULE_RPM || type > qt)
2579             {
2580               type = qt;
2581               if (fromp)
2582                 *fromp = qp;
2583               if (top)
2584                 *top = qo;
2585               if (depp)
2586                 *depp = qd;
2587             }
2588         }
2589       queue_free(&rq);
2590       return type;
2591     }
2592   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2593     {
2594       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2595       if (fromp)
2596         *fromp = jidx;
2597       if (top)
2598         *top = solv->job.elements[jidx];
2599       if (depp)
2600         *depp = solv->job.elements[jidx + 1];
2601       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2602         {
2603           Id how = solv->job.elements[jidx];
2604           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2605             return SOLVER_RULE_JOB_UNKNOWN_PACKAGE;
2606           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2607             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2608           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2609             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2610           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2611             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2612           return SOLVER_RULE_JOB_UNSUPPORTED;
2613         }
2614       return SOLVER_RULE_JOB;
2615     }
2616   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2617     {
2618       if (fromp)
2619         *fromp = solv->installed->start + (rid - solv->updaterules);
2620       return SOLVER_RULE_UPDATE;
2621     }
2622   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2623     {
2624       if (fromp)
2625         *fromp = solv->installed->start + (rid - solv->featurerules);
2626       return SOLVER_RULE_FEATURE;
2627     }
2628   if (rid >= solv->duprules && rid < solv->duprules_end)
2629     {
2630       if (fromp)
2631         *fromp = -r->p;
2632       if (depp)
2633         *depp = pool->solvables[-r->p].name;
2634       return SOLVER_RULE_DISTUPGRADE;
2635     }
2636   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2637     {
2638       if (fromp)
2639         *fromp = -r->p;
2640       if (depp)
2641         *depp = pool->solvables[-r->p].name;
2642       return SOLVER_RULE_INFARCH;
2643     }
2644   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2645     {
2646       return SOLVER_RULE_BEST;
2647     }
2648   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2649     {
2650       return SOLVER_RULE_CHOICE;
2651     }
2652   if (rid >= solv->learntrules)
2653     {
2654       return SOLVER_RULE_LEARNT;
2655     }
2656   return SOLVER_RULE_UNKNOWN;
2657 }
2658
2659 SolverRuleinfo
2660 solver_ruleclass(Solver *solv, Id rid)
2661 {
2662   if (rid <= 0)
2663     return SOLVER_RULE_UNKNOWN;
2664   if (rid > 0 && rid < solv->rpmrules_end)
2665     return SOLVER_RULE_RPM;
2666   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2667     return SOLVER_RULE_JOB;
2668   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2669     return SOLVER_RULE_UPDATE;
2670   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2671     return SOLVER_RULE_FEATURE;
2672   if (rid >= solv->duprules && rid < solv->duprules_end)
2673     return SOLVER_RULE_DISTUPGRADE;
2674   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2675     return SOLVER_RULE_INFARCH;
2676   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2677     return SOLVER_RULE_BEST;
2678   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2679     return SOLVER_RULE_CHOICE;
2680   if (rid >= solv->learntrules)
2681     return SOLVER_RULE_LEARNT;
2682   return SOLVER_RULE_UNKNOWN;
2683 }
2684
2685 void
2686 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2687 {
2688   Pool *pool = solv->pool;
2689   Id p, pp;
2690   Rule *r;
2691
2692   queue_empty(q);
2693   r = solv->rules + rid;
2694   FOR_RULELITERALS(p, pp, r)
2695     if (p != -SYSTEMSOLVABLE)
2696       queue_push(q, p);
2697   if (!q->count)
2698     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
2699 }
2700
2701 int
2702 solver_rule2jobidx(Solver *solv, Id rid)
2703 {
2704   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2705     return 0;
2706   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2707 }
2708
2709 /* job rule introspection */
2710 Id
2711 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2712 {
2713   int idx;
2714   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2715     {
2716       if (whatp)
2717         *whatp = 0;
2718       return 0;
2719     }
2720   idx = solv->ruletojob.elements[rid - solv->jobrules];
2721   if (whatp)
2722     *whatp = solv->job.elements[idx + 1];
2723   return solv->job.elements[idx];
2724 }
2725
2726 /* update/feature rule introspection */
2727 Id
2728 solver_rule2solvable(Solver *solv, Id rid)
2729 {
2730   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2731     return rid - solv->updaterules;
2732   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2733     return rid - solv->featurerules;
2734   return 0;
2735 }
2736
2737 static void
2738 solver_rule2rules_rec(Solver *solv, Id rid, Queue *q, Map *seen)
2739 {
2740   int i;
2741   Id rid2;
2742
2743   if (seen)
2744     MAPSET(seen, rid);
2745   for (i = solv->learnt_why.elements[rid - solv->learntrules]; (rid2 = solv->learnt_pool.elements[i]) != 0; i++)
2746     {
2747       if (seen)
2748         {
2749           if (MAPTST(seen, rid2))
2750             continue;
2751           if (rid2 >= solv->learntrules)
2752             solver_rule2rules_rec(solv, rid2, q, seen);
2753           continue;
2754         }
2755       queue_push(q, rid2);
2756     }
2757 }
2758
2759 /* learnt rule introspection */
2760 void
2761 solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
2762 {
2763   queue_empty(q);
2764   if (rid < solv->learntrules || rid >= solv->nrules)
2765     return;
2766   if (recursive)
2767     {
2768       Map seen;
2769       map_init(&seen, solv->nrules);
2770       solver_rule2rules_rec(solv, rid, q, &seen);
2771       map_free(&seen);
2772     }
2773   else
2774     solver_rule2rules_rec(solv, rid, q, 0);
2775 }
2776
2777
2778 /* check if the newest versions of pi still provides the dependency we're looking for */
2779 static int
2780 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m)
2781 {
2782   Pool *pool = solv->pool;
2783   Rule *ur;
2784   Queue q;
2785   Id p, pp, qbuf[32];
2786   int i;
2787
2788   ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
2789   if (!ur->p)
2790     ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
2791   if (!ur->p)
2792     return 0;
2793   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2794   FOR_RULELITERALS(p, pp, ur)
2795     if (p > 0)
2796       queue_push(&q, p);
2797   if (q.count > 1)
2798     policy_filter_unwanted(solv, &q, POLICY_MODE_CHOOSE);
2799   for (i = 0; i < q.count; i++)
2800     if (MAPTST(m, q.elements[i]))
2801       break;
2802   /* 1: none of the newest versions provide it */
2803   i = i == q.count ? 1 : 0;
2804   queue_free(&q);
2805   return i;
2806 }
2807
2808 static inline void
2809 queue_removeelement(Queue *q, Id el)
2810 {
2811   int i, j;
2812   for (i = 0; i < q->count; i++)
2813     if (q->elements[i] == el)
2814       break;
2815   if (i < q->count)
2816     {
2817       for (j = i++; i < q->count; i++)
2818         if (q->elements[i] != el)
2819           q->elements[j++] = q->elements[i];
2820       queue_truncate(q, j);
2821     }
2822 }
2823
2824 void
2825 solver_addchoicerules(Solver *solv)
2826 {
2827   Pool *pool = solv->pool;
2828   Map m, mneg;
2829   Rule *r;
2830   Queue q, qi;
2831   int i, j, rid, havechoice;
2832   Id p, d, pp;
2833   Id p2, pp2;
2834   Solvable *s, *s2;
2835   Id lastaddedp, lastaddedd;
2836   int lastaddedcnt;
2837   unsigned int now;
2838
2839   solv->choicerules = solv->nrules;
2840   if (!pool->installed)
2841     {
2842       solv->choicerules_end = solv->nrules;
2843       return;
2844     }
2845   now = solv_timems(0);
2846   solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2847   queue_init(&q);
2848   queue_init(&qi);
2849   map_init(&m, pool->nsolvables);
2850   map_init(&mneg, pool->nsolvables);
2851   /* set up negative assertion map from infarch and dup rules */
2852   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2853     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2854       MAPSET(&mneg, -r->p);
2855   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2856     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2857       MAPSET(&mneg, -r->p);
2858   lastaddedp = 0;
2859   lastaddedd = 0;
2860   lastaddedcnt = 0;
2861   for (rid = 1; rid < solv->rpmrules_end ; rid++)
2862     {
2863       r = solv->rules + rid;
2864       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2865         continue;       /* only look at requires rules */
2866       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2867       queue_empty(&q);
2868       queue_empty(&qi);
2869       havechoice = 0;
2870       FOR_RULELITERALS(p, pp, r)
2871         {
2872           if (p < 0)
2873             continue;
2874           s = pool->solvables + p;
2875           if (!s->repo)
2876             continue;
2877           if (s->repo == pool->installed)
2878             {
2879               queue_push(&q, p);
2880               continue;
2881             }
2882           /* check if this package is "blocked" by a installed package */
2883           s2 = 0;
2884           FOR_PROVIDES(p2, pp2, s->name)
2885             {
2886               s2 = pool->solvables + p2;
2887               if (s2->repo != pool->installed)
2888                 continue;
2889               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2890                 continue;
2891               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
2892                 continue;
2893               break;
2894             }
2895           if (p2)
2896             {
2897               /* found installed package p2 that we can update to p */
2898               if (MAPTST(&mneg, p))
2899                 continue;
2900               if (policy_is_illegal(solv, s2, s, 0))
2901                 continue;
2902 #if 0
2903               if (solver_choicerulecheck(solv, p2, r, &m))
2904                 continue;
2905               queue_push(&qi, p2);
2906 #else
2907               queue_push2(&qi, p2, p);
2908 #endif
2909               queue_push(&q, p);
2910               continue;
2911             }
2912           if (s->obsoletes)
2913             {
2914               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2915               s2 = 0;
2916               while ((obs = *obsp++) != 0)
2917                 {
2918                   FOR_PROVIDES(p2, pp2, obs)
2919                     {
2920                       s2 = pool->solvables + p2;
2921                       if (s2->repo != pool->installed)
2922                         continue;
2923                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2924                         continue;
2925                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2926                         continue;
2927                       break;
2928                     }
2929                   if (p2)
2930                     break;
2931                 }
2932               if (obs)
2933                 {
2934                   /* found installed package p2 that we can update to p */
2935                   if (MAPTST(&mneg, p))
2936                     continue;
2937                   if (policy_is_illegal(solv, s2, s, 0))
2938                     continue;
2939 #if 0
2940                   if (solver_choicerulecheck(solv, p2, r, &m))
2941                     continue;
2942                   queue_push(&qi, p2);
2943 #else
2944                   queue_push2(&qi, p2, p);
2945 #endif
2946                   queue_push(&q, p);
2947                   continue;
2948                 }
2949             }
2950           /* package p is independent of the installed ones */
2951           havechoice = 1;
2952         }
2953       if (!havechoice || !q.count || !qi.count)
2954         continue;       /* no choice */
2955
2956       FOR_RULELITERALS(p, pp, r)
2957         if (p > 0)
2958           MAPSET(&m, p);
2959
2960       /* do extra checking */
2961       for (i = j = 0; i < qi.count; i += 2)
2962         {
2963           p2 = qi.elements[i];
2964           if (!p2)
2965             continue;
2966           if (solver_choicerulecheck(solv, p2, r, &m))
2967             {
2968               /* oops, remove element p from q */
2969               queue_removeelement(&q, qi.elements[i + 1]);
2970               continue;
2971             }
2972           qi.elements[j++] = p2;
2973         }
2974       queue_truncate(&qi, j);
2975       if (!q.count || !qi.count)
2976         {
2977           FOR_RULELITERALS(p, pp, r)
2978             if (p > 0)
2979               MAPCLR(&m, p);
2980           continue;
2981         }
2982
2983
2984       /* now check the update rules of the installed package.
2985        * if all packages of the update rules are contained in
2986        * the dependency rules, there's no need to set up the choice rule */
2987       for (i = 0; i < qi.count; i++)
2988         {
2989           Rule *ur;
2990           if (!qi.elements[i])
2991             continue;
2992           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2993           if (!ur->p)
2994             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2995           if (!ur->p)
2996             continue;
2997           FOR_RULELITERALS(p, pp, ur)
2998             if (!MAPTST(&m, p))
2999               break;
3000           if (p)
3001             break;
3002           for (j = i + 1; j < qi.count; j++)
3003             if (qi.elements[i] == qi.elements[j])
3004               qi.elements[j] = 0;
3005         }
3006       /* empty map again */
3007       FOR_RULELITERALS(p, pp, r)
3008         if (p > 0)
3009           MAPCLR(&m, p);
3010       if (i == qi.count)
3011         {
3012 #if 0
3013           printf("skipping choice ");
3014           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3015 #endif
3016           continue;
3017         }
3018
3019       /* don't add identical rules */
3020       if (lastaddedp == r->p && lastaddedcnt == q.count)
3021         {
3022           for (i = 0; i < q.count; i++)
3023             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
3024               break;
3025           if (i == q.count)
3026             continue;   /* already added that one */
3027         }
3028       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
3029
3030       lastaddedp = r->p;
3031       lastaddedd = d;
3032       lastaddedcnt = q.count;
3033
3034       solver_addrule(solv, r->p, d);
3035       queue_push(&solv->weakruleq, solv->nrules - 1);
3036       solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
3037 #if 0
3038       printf("OLD ");
3039       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3040       printf("WEAK CHOICE ");
3041       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
3042 #endif
3043     }
3044   queue_free(&q);
3045   queue_free(&qi);
3046   map_free(&m);
3047   map_free(&mneg);
3048   solv->choicerules_end = solv->nrules;
3049   /* shrink choicerules_ref */
3050   solv->choicerules_ref = solv_realloc2(solv->choicerules_ref, solv->choicerules_end - solv->choicerules, sizeof(Id));
3051   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
3052 }
3053
3054 /* called when a choice rule is disabled by analyze_unsolvable. We also
3055  * have to disable all other choice rules so that the best packages get
3056  * picked */
3057 void
3058 solver_disablechoicerules(Solver *solv, Rule *r)
3059 {
3060   Id rid, p, pp;
3061   Pool *pool = solv->pool;
3062   Map m;
3063   Rule *or;
3064
3065   or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3066   map_init(&m, pool->nsolvables);
3067   FOR_RULELITERALS(p, pp, or)
3068     if (p > 0)
3069       MAPSET(&m, p);
3070   FOR_RULELITERALS(p, pp, r)
3071     if (p > 0)
3072       MAPCLR(&m, p);
3073   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
3074     {
3075       r = solv->rules + rid;
3076       if (r->d < 0)
3077         continue;
3078       or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3079       FOR_RULELITERALS(p, pp, or)
3080         if (p > 0 && MAPTST(&m, p))
3081           break;
3082       if (p)
3083         solver_disablerule(solv, r);
3084     }
3085 }
3086
3087 static void
3088 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
3089 {
3090   int i, j;
3091   Id p, *cp2;
3092   for (i = j = 0; i < q->count; i++)
3093     {
3094       p = q->elements[i];
3095       for (cp2 = cp; *cp2; cp2++)
3096         if (*cp2 == p)
3097           {
3098             q->elements[j++] = p;
3099             break;
3100           }
3101     }
3102   queue_truncate(q, j);
3103 }
3104
3105 static void
3106 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
3107 {
3108   int i, j;
3109   for (i = j = 0; i < q->count; i++)
3110     {
3111       Id p = q->elements[i];
3112       if (MAPTST(&solv->dupmap, p))
3113         q->elements[j++] = p;
3114     }
3115   queue_truncate(q, j);
3116 }
3117
3118 void
3119 solver_addbestrules(Solver *solv, int havebestinstalljobs)
3120 {
3121   Pool *pool = solv->pool;
3122   Id p;
3123   Solvable *s;
3124   Repo *installed = solv->installed;
3125   Queue q, q2;
3126   Rule *r;
3127   Queue r2pkg;
3128   int i, oldcnt;
3129
3130   solv->bestrules = solv->nrules;
3131   if (!installed)
3132     {
3133       solv->bestrules_end = solv->nrules;
3134       return;
3135     }
3136   queue_init(&q);
3137   queue_init(&q2);
3138   queue_init(&r2pkg);
3139
3140   if (havebestinstalljobs)
3141     {
3142       for (i = 0; i < solv->job.count; i += 2)
3143         {
3144           if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
3145             {
3146               int j;
3147               Id p2, pp2;
3148               for (j = 0; j < solv->ruletojob.count; j++)
3149                 if (solv->ruletojob.elements[j] == i)
3150                   break;
3151               if (j == solv->ruletojob.count)
3152                 continue;
3153               r = solv->rules + solv->jobrules + j;
3154               queue_empty(&q);
3155               FOR_RULELITERALS(p2, pp2, r)
3156                 if (p2 > 0)
3157                   queue_push(&q, p2);
3158               if (!q.count)
3159                 continue;       /* orphaned */
3160               /* select best packages, just look at prio and version */
3161               oldcnt = q.count;
3162               policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3163               if (q.count == oldcnt)
3164                 continue;       /* nothing filtered */
3165               p2 = queue_shift(&q);
3166               solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
3167               queue_push(&r2pkg, -(solv->jobrules + j));
3168             }
3169         }
3170     }
3171
3172   if (solv->bestupdatemap_all || solv->bestupdatemap.size)
3173     {
3174       FOR_REPO_SOLVABLES(installed, p, s)
3175         {
3176           Id d, p2, pp2;
3177           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
3178             continue;
3179           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
3180             continue;
3181           queue_empty(&q);
3182           if (solv->bestobeypolicy)
3183             r = solv->rules + solv->updaterules + (p - installed->start);
3184           else
3185             {
3186               r = solv->rules + solv->featurerules + (p - installed->start);
3187               if (!r->p)        /* identical to update rule? */
3188                 r = solv->rules + solv->updaterules + (p - installed->start);
3189             }
3190           if (solv->specialupdaters && (d = solv->specialupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
3191             {
3192               /* need to check specialupdaters */
3193               if (r->p == p)    /* be careful with the dup case */
3194                 queue_push(&q, p);
3195               while ((p2 = pool->whatprovidesdata[d++]) != 0)
3196                 queue_push(&q, p2);
3197             }
3198           else
3199             {
3200               FOR_RULELITERALS(p2, pp2, r)
3201                 if (p2 > 0)
3202                   queue_push(&q, p2);
3203             }
3204           if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3205             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
3206           if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3207             prune_to_dup_packages(solv, p, &q);
3208           /* select best packages, just look at prio and version */
3209           policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3210           if (!q.count)
3211             continue;   /* orphaned */
3212           if (solv->bestobeypolicy)
3213             {
3214               /* also filter the best of the feature rule packages and add them */
3215               r = solv->rules + solv->featurerules + (p - installed->start);
3216               if (r->p)
3217                 {
3218                   int j;
3219                   queue_empty(&q2);
3220                   FOR_RULELITERALS(p2, pp2, r)
3221                     if (p2 > 0)
3222                       queue_push(&q2, p2);
3223                   if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3224                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
3225                   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3226                     prune_to_dup_packages(solv, p, &q2);
3227                   policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
3228                   for (j = 0; j < q2.count; j++)
3229                     queue_pushunique(&q, q2.elements[j]);
3230                 }
3231             }
3232           p2 = queue_shift(&q);
3233           solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
3234           queue_push(&r2pkg, p);
3235         }
3236     }
3237   if (r2pkg.count)
3238     solv->bestrules_pkg = solv_memdup2(r2pkg.elements, r2pkg.count, sizeof(Id));
3239   solv->bestrules_end = solv->nrules;
3240   queue_free(&q);
3241   queue_free(&q2);
3242   queue_free(&r2pkg);
3243 }
3244
3245 #undef CLEANDEPSDEBUG
3246
3247 /*
3248  * This functions collects all packages that are looked at
3249  * when a dependency is checked. We need it to "pin" installed
3250  * packages when removing a supplemented package in createcleandepsmap.
3251  * Here's an not uncommon example:
3252  *   A contains "Supplements: packageand(B, C)"
3253  *   B contains "Requires: A"
3254  * Now if we remove C, the supplements is no longer true,
3255  * thus we also remove A. Without the dep_pkgcheck function, we
3256  * would now also remove B, but this is wrong, as adding back
3257  * C doesn't make the supplements true again. Thus we "pin" B
3258  * when we remove A.
3259  * There's probably a better way to do this, but I haven't come
3260  * up with it yet ;)
3261  */
3262 static inline void
3263 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
3264 {
3265   Pool *pool = solv->pool;
3266   Id p, pp;
3267
3268   if (ISRELDEP(dep))
3269     {
3270       Reldep *rd = GETRELDEP(pool, dep);
3271       if (rd->flags >= 8)
3272         {
3273           if (rd->flags == REL_AND)
3274             {
3275               dep_pkgcheck(solv, rd->name, m, q);
3276               dep_pkgcheck(solv, rd->evr, m, q);
3277               return;
3278             }
3279           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3280             return;
3281           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
3282             return;
3283         }
3284     }
3285   FOR_PROVIDES(p, pp, dep)
3286     if (!m || MAPTST(m, p))
3287       queue_push(q, p);
3288 }
3289
3290 static int
3291 check_xsupp(Solver *solv, Queue *depq, Id dep)
3292 {
3293   Pool *pool = solv->pool;
3294   Id p, pp;
3295
3296   if (ISRELDEP(dep))
3297     {
3298       Reldep *rd = GETRELDEP(pool, dep);
3299       if (rd->flags >= 8)
3300         {
3301           if (rd->flags == REL_AND)
3302             {
3303               if (!check_xsupp(solv, depq, rd->name))
3304                 return 0;
3305               return check_xsupp(solv, depq, rd->evr);
3306             }
3307           if (rd->flags == REL_OR)
3308             {
3309               if (check_xsupp(solv, depq, rd->name))
3310                 return 1;
3311               return check_xsupp(solv, depq, rd->evr);
3312             }
3313           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3314             return solver_splitprovides(solv, rd->evr);
3315           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
3316             return solver_dep_installed(solv, rd->evr);
3317         }
3318       if (depq && rd->flags == REL_NAMESPACE)
3319         {
3320           int i;
3321           for (i = 0; i < depq->count; i++)
3322             if (depq->elements[i] == dep || depq->elements[i] == rd->name)
3323              return 1;
3324         }
3325     }
3326   FOR_PROVIDES(p, pp, dep)
3327     if (p == SYSTEMSOLVABLE || pool->solvables[p].repo == solv->installed)
3328       return 1;
3329   return 0;
3330 }
3331
3332 static inline int
3333 queue_contains(Queue *q, Id id)
3334 {
3335   int i;
3336   for (i = 0; i < q->count; i++)
3337     if (q->elements[i] == id)
3338       return 1;
3339   return 0;
3340 }
3341
3342 #ifdef ENABLE_COMPLEX_DEPS
3343 static void
3344 complex_cleandeps_remove(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq)
3345 {
3346   int i;
3347   Queue dq;
3348   Id p;
3349
3350   queue_init(&dq);
3351   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3352   if (i == 0 || i == 1)
3353     {
3354       queue_free(&dq);
3355       return;
3356     }
3357   for (i = 0; i < dq.count; i++)
3358     {
3359       for (; (p = dq.elements[i]) != 0; i++)
3360         {
3361           if (p < 0)
3362             {
3363               if (!MAPTST(installedm, -p))
3364                 break;
3365               continue;
3366             }
3367           if (p != SYSTEMSOLVABLE && MAPTST(im, p))
3368             {
3369 #ifdef CLEANDEPSDEBUG
3370               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3371 #endif
3372               queue_push(iq, p);
3373             }
3374         }
3375       while (dq.elements[i])
3376         i++;
3377     }
3378   queue_free(&dq);
3379 }
3380
3381 static void
3382 complex_cleandeps_addback(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq, Map *userinstalled)
3383 {
3384   int i, blk;
3385   Queue dq;
3386   Id p;
3387
3388   queue_init(&dq);
3389   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3390   if (i == 0 || i == 1)
3391     {
3392       queue_free(&dq);
3393       return;
3394     }
3395   for (i = 0; i < dq.count; i++)
3396     {
3397       blk = i;
3398       for (; (p = dq.elements[i]) != 0; i++)
3399         {
3400           if (p < 0)
3401             {
3402               if (!MAPTST(installedm, -p))
3403                 break;
3404               continue;
3405             }
3406           if (MAPTST(im, p))
3407             break;
3408         }
3409       if (!p)
3410         {
3411           for (i = blk; (p = dq.elements[i]) != 0; i++)
3412             {
3413               if (p < 0)
3414                 continue;
3415               if (!MAPTST(installedm, p))
3416                 continue;
3417               if (p == ip || MAPTST(userinstalled, p - pool->installed->start))
3418                 continue;
3419 #ifdef CLEANDEPSDEBUG
3420               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3421 #endif
3422               MAPSET(im, p);
3423               queue_push(iq, p);
3424             }
3425         }
3426       while (dq.elements[i])
3427         i++;
3428     }
3429   queue_free(&dq);
3430 }
3431
3432 #endif
3433
3434 /*
3435  * Find all installed packages that are no longer
3436  * needed regarding the current solver job.
3437  *
3438  * The algorithm is:
3439  * - remove pass: remove all packages that could have
3440  *   been dragged in by the obsoleted packages.
3441  *   i.e. if package A is obsolete and contains "Requires: B",
3442  *   also remove B, as installing A will have pulled in B.
3443  *   after this pass, we have a set of still installed packages
3444  *   with broken dependencies.
3445  * - add back pass:
3446  *   now add back all packages that the still installed packages
3447  *   require.
3448  *
3449  * The cleandeps packages are the packages removed in the first
3450  * pass and not added back in the second pass.
3451  *
3452  * If we search for unneeded packages (unneeded is true), we
3453  * simply remove all packages except the userinstalled ones in
3454  * the first pass.
3455  */
3456 static void
3457 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
3458 {
3459   Pool *pool = solv->pool;
3460   Repo *installed = solv->installed;
3461   Queue *job = &solv->job;
3462   Map userinstalled;
3463   Map im;
3464   Map installedm;
3465   Rule *r;
3466   Id rid, how, what, select;
3467   Id p, pp, ip, jp;
3468   Id req, *reqp, sup, *supp;
3469   Solvable *s;
3470   Queue iq, iqcopy, xsuppq;
3471   int i;
3472
3473   map_empty(cleandepsmap);
3474   if (!installed || installed->end == installed->start)
3475     return;
3476   map_init(&userinstalled, installed->end - installed->start);
3477   map_init(&im, pool->nsolvables);
3478   map_init(&installedm, pool->nsolvables);
3479   queue_init(&iq);
3480   queue_init(&xsuppq);
3481
3482   for (i = 0; i < job->count; i += 2)
3483     {
3484       how = job->elements[i];
3485       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3486         {
3487           what = job->elements[i + 1];
3488           select = how & SOLVER_SELECTMASK;
3489           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
3490             FOR_REPO_SOLVABLES(installed, p, s)
3491               MAPSET(&userinstalled, p - installed->start);
3492           FOR_JOB_SELECT(p, pp, select, what)
3493             if (pool->solvables[p].repo == installed)
3494               MAPSET(&userinstalled, p - installed->start);
3495         }
3496       if ((how & (SOLVER_JOBMASK | SOLVER_SELECTMASK)) == (SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES))
3497         {
3498           what = job->elements[i + 1];
3499           if (ISRELDEP(what))
3500             {
3501               Reldep *rd = GETRELDEP(pool, what);
3502               if (rd->flags != REL_NAMESPACE)
3503                 continue;
3504               if (rd->evr == 0)
3505                 {
3506                   queue_pushunique(&iq, rd->name);
3507                   continue;
3508                 }
3509               FOR_PROVIDES(p, pp, what)
3510                 if (p)
3511                   break;
3512               if (p)
3513                 continue;
3514               queue_pushunique(&iq, what);
3515             }
3516         }
3517     }
3518
3519   /* have special namespace cleandeps erases */
3520   if (iq.count)
3521     {
3522       for (ip = solv->installed->start; ip < solv->installed->end; ip++)
3523         {
3524           s = pool->solvables + ip;
3525           if (s->repo != installed)
3526             continue;
3527           if (!s->supplements)
3528             continue;
3529           supp = s->repo->idarraydata + s->supplements;
3530           while ((sup = *supp++) != 0)
3531             if (check_xsupp(solv, &iq, sup) && !check_xsupp(solv, 0, sup))
3532               {
3533 #ifdef CLEANDEPSDEBUG
3534                 printf("xsupp %s from %s\n", pool_dep2str(pool, sup), pool_solvid2str(pool, ip));
3535 #endif
3536                 queue_pushunique(&xsuppq, sup);
3537               }
3538         }
3539       queue_empty(&iq);
3540     }
3541
3542   /* also add visible patterns to userinstalled for openSUSE */
3543   if (1)
3544     {
3545       Dataiterator di;
3546       dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
3547       while (dataiterator_step(&di))
3548         {
3549           Id *dp;
3550           if (di.solvid <= 0)
3551             continue;
3552           s = pool->solvables + di.solvid;
3553           if (!s->repo || !s->requires)
3554             continue;
3555           if (s->repo != installed && !pool_installable(pool, s))
3556             continue;
3557           if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
3558             continue;
3559           dp = s->repo->idarraydata + s->requires;
3560           for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
3561             FOR_PROVIDES(p, pp, *dp)
3562               if (pool->solvables[p].repo == installed)
3563                 {
3564                   if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
3565                     continue;
3566                   MAPSET(&userinstalled, p - installed->start);
3567                 }
3568         }
3569       dataiterator_free(&di);
3570     }
3571   if (1)
3572     {
3573       /* all products and their buddies are userinstalled */
3574       for (p = installed->start; p < installed->end; p++)
3575         {
3576           Solvable *s = pool->solvables + p;
3577           if (s->repo != installed)
3578             continue;
3579           if (!strncmp("product:", pool_id2str(pool, s->name), 8))
3580             {
3581               MAPSET(&userinstalled, p - installed->start);
3582               if (pool->nscallback)
3583                 {
3584                   Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, p);
3585                   if (buddy >= installed->start && buddy < installed->end && pool->solvables[buddy].repo == installed)
3586                     MAPSET(&userinstalled, buddy - installed->start);
3587                 }
3588             }
3589         }
3590     }
3591
3592   /* add all positive elements (e.g. locks) to "userinstalled" */
3593   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3594     {
3595       r = solv->rules + rid;
3596       if (r->d < 0)
3597         continue;
3598       i = solv->ruletojob.elements[rid - solv->jobrules];
3599       if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3600         continue;
3601       FOR_RULELITERALS(p, jp, r)
3602         if (p > 0 && pool->solvables[p].repo == installed)
3603           MAPSET(&userinstalled, p - installed->start);
3604     }
3605
3606   /* add all cleandeps candidates to iq */
3607   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3608     {
3609       r = solv->rules + rid;
3610       if (r->d < 0)                             /* disabled? */
3611         continue;
3612       if (r->d == 0 && r->p < 0 && r->w2 == 0)  /* negative assertion (erase job)? */
3613         {
3614           p = -r->p;
3615           if (pool->solvables[p].repo != installed)
3616             continue;
3617           MAPCLR(&userinstalled, p - installed->start);
3618           if (unneeded)
3619             continue;
3620           i = solv->ruletojob.elements[rid - solv->jobrules];
3621           how = job->elements[i];
3622           if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
3623             queue_push(&iq, p);
3624         }
3625       else if (r->p > 0)                        /* install job */
3626         {
3627           if (unneeded)
3628             continue;
3629           i = solv->ruletojob.elements[rid - solv->jobrules];
3630           if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3631             {
3632               /* check if the literals all obsolete some installed package */
3633               Map om;
3634               int iqstart;
3635
3636               /* just one installed literal */
3637               if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
3638                 continue;
3639               /* multiversion is bad */
3640               if (solv->multiversion.size && !solv->keepexplicitobsoletes)
3641                 {
3642                   FOR_RULELITERALS(p, jp, r)
3643                     if (MAPTST(&solv->multiversion, p))
3644                       break;
3645                   if (p)
3646                     continue;
3647                 }
3648
3649               om.size = 0;
3650               iqstart = iq.count;
3651               FOR_RULELITERALS(p, jp, r)
3652                 {
3653                   if (p < 0)
3654                     {
3655                       queue_truncate(&iq, iqstart);     /* abort */
3656                       break;
3657                     }
3658                   if (pool->solvables[p].repo == installed)
3659                     {
3660                       if (iq.count == iqstart)
3661                         queue_push(&iq, p);
3662                       else
3663                         {
3664                           for (i = iqstart; i < iq.count; i++)
3665                             if (iq.elements[i] == p)
3666                               break;
3667                           queue_truncate(&iq, iqstart);
3668                           if (i < iq.count)
3669                             queue_push(&iq, p);
3670                         }
3671                     }
3672                   else
3673                     intersect_obsoletes(solv, p, &iq, iqstart, &om);
3674                   if (iq.count == iqstart)
3675                     break;
3676                 }
3677               if (om.size)
3678                 map_free(&om);
3679             }
3680         }
3681     }
3682   queue_init_clone(&iqcopy, &iq);
3683
3684   if (!unneeded)
3685     {
3686       if (solv->cleandeps_updatepkgs)
3687         for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3688           queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
3689     }
3690
3691   if (unneeded)
3692     queue_empty(&iq);   /* just in case... */
3693
3694   /* clear userinstalled bit for the packages we really want to delete/update */
3695   for (i = 0; i < iq.count; i++)
3696     {
3697       p = iq.elements[i];
3698       if (pool->solvables[p].repo != installed)
3699         continue;
3700       MAPCLR(&userinstalled, p - installed->start);
3701     }
3702
3703   for (p = installed->start; p < installed->end; p++)
3704     {
3705       if (pool->solvables[p].repo != installed)
3706         continue;
3707       MAPSET(&installedm, p);
3708       if (unneeded && !MAPTST(&userinstalled, p - installed->start))
3709         continue;
3710       MAPSET(&im, p);
3711     }
3712   MAPSET(&installedm, SYSTEMSOLVABLE);
3713   MAPSET(&im, SYSTEMSOLVABLE);
3714
3715 #ifdef CLEANDEPSDEBUG
3716   printf("REMOVE PASS\n");
3717 #endif
3718
3719   for (;;)
3720     {
3721       if (!iq.count)
3722         {
3723           if (unneeded)
3724             break;
3725           /* supplements pass */
3726           for (ip = installed->start; ip < installed->end; ip++)
3727             {
3728               if (!MAPTST(&installedm, ip))
3729                 continue;
3730               s = pool->solvables + ip;
3731               if (!s->supplements)
3732                 continue;
3733               if (!MAPTST(&im, ip))
3734                 continue;
3735               if (MAPTST(&userinstalled, ip - installed->start))
3736                 continue;
3737               supp = s->repo->idarraydata + s->supplements;
3738               while ((sup = *supp++) != 0)
3739                 if (dep_possible(solv, sup, &im))
3740                   break;
3741               if (!sup)
3742                 {
3743                   supp = s->repo->idarraydata + s->supplements;
3744                   while ((sup = *supp++) != 0)
3745                     if (dep_possible(solv, sup, &installedm) || (xsuppq.count && queue_contains(&xsuppq, sup)))
3746                       {
3747                         /* no longer supplemented, also erase */
3748                         int iqcount = iq.count;
3749                         /* pin packages, see comment above dep_pkgcheck */
3750                         dep_pkgcheck(solv, sup, &im, &iq);
3751                         for (i = iqcount; i < iq.count; i++)
3752                           {
3753                             Id pqp = iq.elements[i];
3754                             if (pool->solvables[pqp].repo == installed)
3755                               MAPSET(&userinstalled, pqp - installed->start);
3756                           }
3757                         queue_truncate(&iq, iqcount);
3758 #ifdef CLEANDEPSDEBUG
3759                         printf("%s supplemented [%s]\n", pool_solvid2str(pool, ip), pool_dep2str(pool, sup));
3760 #endif
3761                         queue_push(&iq, ip);
3762                       }
3763                 }
3764             }
3765           if (!iq.count)
3766             break;      /* no supplementing package found, we're done */
3767         }
3768       ip = queue_shift(&iq);
3769       s = pool->solvables + ip;
3770       if (!MAPTST(&im, ip))
3771         continue;
3772       if (!MAPTST(&installedm, ip))
3773         continue;
3774       if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
3775         continue;
3776       MAPCLR(&im, ip);
3777 #ifdef CLEANDEPSDEBUG
3778       printf("removing %s\n", pool_solvable2str(pool, s));
3779 #endif
3780       if (s->requires)
3781         {
3782           reqp = s->repo->idarraydata + s->requires;
3783           while ((req = *reqp++) != 0)
3784             {
3785               if (req == SOLVABLE_PREREQMARKER)
3786                 continue;
3787 #ifdef ENABLE_COMPLEX_DEPS
3788               if (pool_is_complex_dep(pool, req))
3789                 {
3790                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
3791                   continue;
3792                 }
3793 #endif
3794               FOR_PROVIDES(p, pp, req)
3795                 {
3796                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3797                     {
3798 #ifdef CLEANDEPSDEBUG
3799                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3800 #endif
3801                       queue_push(&iq, p);
3802                     }
3803                 }
3804             }
3805         }
3806       if (s->recommends)
3807         {
3808           reqp = s->repo->idarraydata + s->recommends;
3809           while ((req = *reqp++) != 0)
3810             {
3811 #ifdef ENABLE_COMPLEX_DEPS
3812               if (pool_is_complex_dep(pool, req))
3813                 {
3814                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
3815                   continue;
3816                 }
3817 #endif
3818               FOR_PROVIDES(p, pp, req)
3819                 {
3820                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3821                     {
3822 #ifdef CLEANDEPSDEBUG
3823                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3824 #endif
3825                       queue_push(&iq, p);
3826                     }
3827                 }
3828             }
3829         }
3830     }
3831
3832   /* turn userinstalled into remove set for pruning */
3833   map_empty(&userinstalled);
3834   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3835     {
3836       r = solv->rules + rid;
3837       if (r->p >= 0 || r->d != 0 || r->w2 != 0)
3838         continue;       /* disabled or not erase */
3839       p = -r->p;
3840       MAPCLR(&im, p);
3841       if (pool->solvables[p].repo == installed)
3842         MAPSET(&userinstalled, p - installed->start);
3843     }
3844   if (!unneeded && solv->cleandeps_updatepkgs)
3845     {
3846       for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3847         {
3848           p = solv->cleandeps_updatepkgs->elements[i];
3849           if (pool->solvables[p].repo == installed)
3850             MAPSET(&userinstalled, p - installed->start);
3851         }
3852     }
3853   MAPSET(&im, SYSTEMSOLVABLE);  /* in case we cleared it above */
3854   for (p = installed->start; p < installed->end; p++)
3855     if (MAPTST(&im, p))
3856       queue_push(&iq, p);
3857   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3858     {
3859       r = solv->rules + rid;
3860       if (r->d < 0)
3861         continue;
3862       FOR_RULELITERALS(p, jp, r)
3863         if (p > 0)
3864           queue_push(&iq, p);
3865     }
3866   /* also put directly addressed packages on the install queue
3867    * so we can mark patterns as installed */
3868   for (i = 0; i < job->count; i += 2)
3869     {
3870       how = job->elements[i];
3871       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3872         {
3873           what = job->elements[i + 1];
3874           select = how & SOLVER_SELECTMASK;
3875           if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
3876             queue_push(&iq, what);
3877         }
3878     }
3879
3880 #ifdef CLEANDEPSDEBUG
3881   printf("ADDBACK PASS\n");
3882 #endif
3883   for (;;)
3884     {
3885       if (!iq.count)
3886         {
3887           /* supplements pass */
3888           for (ip = installed->start; ip < installed->end; ip++)
3889             {
3890               if (!MAPTST(&installedm, ip))
3891                 continue;
3892               if (MAPTST(&userinstalled, ip - installed->start))
3893                 continue;
3894               s = pool->solvables + ip;
3895               if (!s->supplements)
3896                 continue;
3897               if (MAPTST(&im, ip))
3898                 continue;
3899               supp = s->repo->idarraydata + s->supplements;
3900               while ((sup = *supp++) != 0)
3901                 if (dep_possible(solv, sup, &im))
3902                   break;
3903               if (sup)
3904                 {
3905 #ifdef CLEANDEPSDEBUG
3906                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
3907 #endif
3908                   MAPSET(&im, ip);
3909                   queue_push(&iq, ip);
3910                 }
3911             }
3912           if (!iq.count)
3913             break;
3914         }
3915       ip = queue_shift(&iq);
3916       s = pool->solvables + ip;
3917 #ifdef CLEANDEPSDEBUG
3918       printf("adding back %s\n", pool_solvable2str(pool, s));
3919 #endif
3920       if (s->requires)
3921         {
3922           reqp = s->repo->idarraydata + s->requires;
3923           while ((req = *reqp++) != 0)
3924             {
3925 #ifdef ENABLE_COMPLEX_DEPS
3926               if (pool_is_complex_dep(pool, req))
3927                 {
3928                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
3929                   continue;
3930                 }
3931 #endif
3932               FOR_PROVIDES(p, pp, req)
3933                 if (MAPTST(&im, p))
3934                   break;
3935               if (p)
3936                 continue;
3937               FOR_PROVIDES(p, pp, req)
3938                 {
3939                   if (MAPTST(&installedm, p))
3940                     {
3941                       if (p == ip)
3942                         continue;
3943                       if (MAPTST(&userinstalled, p - installed->start))
3944                         continue;
3945 #ifdef CLEANDEPSDEBUG
3946                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3947 #endif
3948                       MAPSET(&im, p);
3949                       queue_push(&iq, p);
3950                     }
3951                 }
3952             }
3953         }
3954       if (s->recommends)
3955         {
3956           reqp = s->repo->idarraydata + s->recommends;
3957           while ((req = *reqp++) != 0)
3958             {
3959 #ifdef ENABLE_COMPLEX_DEPS
3960               if (pool_is_complex_dep(pool, req))
3961                 {
3962                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
3963                   continue;
3964                 }
3965 #endif
3966               FOR_PROVIDES(p, pp, req)
3967                 if (MAPTST(&im, p))
3968                   break;
3969               if (p)
3970                 continue;
3971               FOR_PROVIDES(p, pp, req)
3972                 {
3973                   if (MAPTST(&installedm, p))
3974                     {
3975                       if (p == ip)
3976                         continue;
3977                       if (MAPTST(&userinstalled, p - installed->start))
3978                         continue;
3979 #ifdef CLEANDEPSDEBUG
3980                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3981 #endif
3982                       MAPSET(&im, p);
3983                       queue_push(&iq, p);
3984                     }
3985                 }
3986             }
3987         }
3988     }
3989
3990   queue_free(&iq);
3991   /* make sure the updatepkgs and mistakes are not in the cleandeps map */
3992   if (solv->cleandeps_updatepkgs)
3993     for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3994       MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
3995   if (solv->cleandeps_mistakes)
3996     for (i = 0; i < solv->cleandeps_mistakes->count; i++)
3997       MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
3998   /* also remove original iq packages */
3999   for (i = 0; i < iqcopy.count; i++)
4000     MAPSET(&im, iqcopy.elements[i]);
4001   queue_free(&iqcopy);
4002   for (p = installed->start; p < installed->end; p++)
4003     {
4004       if (pool->solvables[p].repo != installed)
4005         continue;
4006       if (!MAPTST(&im, p))
4007         MAPSET(cleandepsmap, p - installed->start);
4008     }
4009   map_free(&im);
4010   map_free(&installedm);
4011   map_free(&userinstalled);
4012   queue_free(&xsuppq);
4013 #ifdef CLEANDEPSDEBUG
4014   printf("=== final cleandeps map:\n");
4015   for (p = installed->start; p < installed->end; p++)
4016     if (MAPTST(cleandepsmap, p - installed->start))
4017       printf("  - %s\n", pool_solvid2str(pool, p));
4018 #endif
4019 }
4020
4021
4022 struct trj_data {
4023   Queue *edges;
4024   Id *low;
4025   Id idx;
4026   Id nstack;
4027   Id firstidx;
4028 };
4029
4030 /* Tarjan's SCC algorithm, slightly modifed */
4031 static void
4032 trj_visit(struct trj_data *trj, Id node)
4033 {
4034   Id *low = trj->low;
4035   Queue *edges = trj->edges;
4036   Id nnode, myidx, stackstart;
4037   int i;
4038
4039   low[node] = myidx = trj->idx++;
4040   low[(stackstart = trj->nstack++)] = node;
4041   for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
4042     {
4043       Id l = low[nnode];
4044       if (!l)
4045         {
4046           if (!edges->elements[edges->elements[nnode]])
4047             {
4048               trj->idx++;
4049               low[nnode] = -1;
4050               continue;
4051             }
4052           trj_visit(trj, nnode);
4053           l = low[nnode];
4054         }
4055       if (l < 0)
4056         continue;
4057       if (l < trj->firstidx)
4058         {
4059           int k;
4060           for (k = l; low[low[k]] == l; k++)
4061             low[low[k]] = -1;
4062         }
4063       else if (l < low[node])
4064         low[node] = l;
4065     }
4066   if (low[node] == myidx)
4067     {
4068       if (myidx != trj->firstidx)
4069         myidx = -1;
4070       for (i = stackstart; i < trj->nstack; i++)
4071         low[low[i]] = myidx;
4072       trj->nstack = stackstart;
4073     }
4074 }
4075
4076 #ifdef ENABLE_COMPLEX_DEPS
4077 static void
4078 complex_unneeded(Pool *pool, Id ip, Id req, Queue *edges, Map *cleandepsmap, Queue *unneededq)
4079 {
4080   int i, j;
4081   Queue dq;
4082   Id p;
4083
4084   queue_init(&dq);
4085   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
4086   if (i == 0 || i == 1)
4087     {
4088       queue_free(&dq);
4089       return;
4090     }
4091   for (i = 0; i < dq.count; i++)
4092     {
4093       for (; (p = dq.elements[i]) != 0; i++)
4094         {
4095           if (p < 0)
4096             {
4097               if (pool->solvables[-p].repo != pool->installed)
4098                 break;
4099               continue;
4100             }
4101           if (p == ip || pool->solvables[p].repo != pool->installed || !MAPTST(cleandepsmap, p - pool->installed->start))
4102             continue;
4103           for (j = 0; j < unneededq->count; j++)
4104             if (p == unneededq->elements[j])
4105               {
4106                 if (edges->elements[edges->count - 1] != j + 1)
4107                   queue_push(edges, j + 1);
4108                 break;
4109               }
4110         }
4111       while (dq.elements[i])
4112         i++;
4113     }
4114   queue_free(&dq);
4115 }
4116 #endif
4117
4118 void
4119 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
4120 {
4121   Repo *installed = solv->installed;
4122   int i;
4123   Map cleandepsmap;
4124
4125   queue_empty(unneededq);
4126   if (!installed || installed->end == installed->start)
4127     return;
4128
4129   map_init(&cleandepsmap, installed->end - installed->start);
4130   solver_createcleandepsmap(solv, &cleandepsmap, 1);
4131   for (i = installed->start; i < installed->end; i++)
4132     if (MAPTST(&cleandepsmap, i - installed->start))
4133       queue_push(unneededq, i);
4134
4135   if (filtered && unneededq->count > 1)
4136     {
4137       Pool *pool = solv->pool;
4138       Queue edges;
4139       Id *nrequires;
4140       Map installedm;
4141       int j, pass, count = unneededq->count;
4142       Id *low;
4143
4144       map_init(&installedm, pool->nsolvables);
4145       for (i = installed->start; i < installed->end; i++)
4146         if (pool->solvables[i].repo == installed)
4147           MAPSET(&installedm, i);
4148
4149       nrequires = solv_calloc(count, sizeof(Id));
4150       queue_init(&edges);
4151       queue_prealloc(&edges, count * 4 + 10);   /* pre-size */
4152
4153       /*
4154        * Go through the solvables in the nodes queue and create edges for
4155        * all requires/recommends/supplements between the nodes.
4156        * The edges are stored in the edges queue, we add 1 to the node
4157        * index so that nodes in the edges queue are != 0 and we can
4158        * terminate the edge list with 0.
4159        * Thus for node element 5, the edges are stored starting at
4160        * edges.elements[6] and are 0-terminated.
4161        */
4162       /* leave first element zero to make things easier */
4163       /* also add trailing zero */
4164       queue_insertn(&edges, 0, 1 + count + 1, 0);
4165
4166       /* first requires and recommends */
4167       for (i = 0; i < count; i++)
4168         {
4169           Solvable *s = pool->solvables + unneededq->elements[i];
4170           int oldcount = edges.count;
4171           edges.elements[i + 1] = oldcount;
4172           for (pass = 0; pass < 2; pass++)
4173             {
4174               unsigned int off = pass == 0 ? s->requires : s->recommends;
4175               Id p, pp, dep, *dp;
4176               if (off)
4177                 for (dp = s->repo->idarraydata + off; (dep = *dp) != 0; dp++)
4178                   {
4179 #ifdef ENABLE_COMPLEX_DEPS
4180                     if (pool_is_complex_dep(pool, dep))
4181                       {
4182                         complex_unneeded(pool, s - pool->solvables, dep, &edges, &cleandepsmap, unneededq);
4183                         continue;
4184                       }
4185 #endif
4186                     FOR_PROVIDES(p, pp, dep)
4187                       {
4188                         Solvable *sp = pool->solvables + p;
4189                         if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4190                           continue;
4191                         for (j = 0; j < count; j++)
4192                           if (p == unneededq->elements[j])
4193                             {
4194                               if (edges.elements[edges.count - 1] != j + 1)
4195                                 queue_push(&edges, j + 1);
4196                             }
4197                       }
4198                   }
4199               if (pass == 0)
4200                 nrequires[i] = edges.count - oldcount;
4201             }
4202           queue_push(&edges, 0);
4203         }
4204 #if 0
4205       printf("requires + recommends\n");
4206       for (i = 0; i < count; i++)
4207         {
4208           int j;
4209           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4210           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4211             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4212         }
4213 #endif
4214
4215       /* then add supplements */
4216       for (i = 0; i < count; i++)
4217         {
4218           Solvable *s = pool->solvables + unneededq->elements[i];
4219           if (s->supplements)
4220             {
4221               Id *dp;
4222               int k;
4223               for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
4224                 if (dep_possible(solv, *dp, &installedm))
4225                   {
4226                     Queue iq;
4227                     Id iqbuf[16];
4228                     queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
4229                     dep_pkgcheck(solv, *dp, 0, &iq);
4230                     for (k = 0; k < iq.count; k++)
4231                       {
4232                         Id p = iq.elements[k];
4233                         Solvable *sp = pool->solvables + p;
4234                         if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4235                           continue;
4236                         for (j = 0; j < count; j++)
4237                           if (p == unneededq->elements[j])
4238                             break;
4239                         /* now add edge from j + 1 to i + 1 */
4240                         queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
4241                         /* addapt following edge pointers */
4242                         for (j = j + 2; j < count + 1; j++)
4243                           edges.elements[j]++;
4244                       }
4245                     queue_free(&iq);
4246                   }
4247             }
4248         }
4249 #if 0
4250       /* print result */
4251       printf("+ supplements\n");
4252       for (i = 0; i < count; i++)
4253         {
4254           int j;
4255           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4256           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4257             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4258         }
4259 #endif
4260       map_free(&installedm);
4261
4262       /* now run SCC algo two times, first with requires+recommends+supplements,
4263        * then again without the requires. We run it the second time to get rid
4264        * of packages that got dragged in via recommends/supplements */
4265       /*
4266        * low will contain the result of the SCC search.
4267        * it must be of at least size 2 * (count + 1) and
4268        * must be zero initialized.
4269        * The layout is:
4270        *    0  low low ... low stack stack ...stack 0
4271        *            count              count
4272        */
4273       low = solv_calloc(count + 1, 2 * sizeof(Id));
4274       for (pass = 0; pass < 2; pass++)
4275         {
4276           struct trj_data trj;
4277           if (pass)
4278             {
4279               memset(low, 0, (count + 1) * (2 * sizeof(Id)));
4280               for (i = 0; i < count; i++)
4281                 {
4282                   edges.elements[i + 1] += nrequires[i];
4283                   if (!unneededq->elements[i])
4284                     low[i + 1] = -1;    /* ignore this node */
4285                 }
4286             }
4287           trj.edges = &edges;
4288           trj.low = low;
4289           trj.idx = count + 1;  /* stack starts here */
4290           for (i = 1; i <= count; i++)
4291             {
4292               if (low[i])
4293                 continue;
4294               if (edges.elements[edges.elements[i]])
4295                 {
4296                   trj.firstidx = trj.nstack = trj.idx;
4297                   trj_visit(&trj, i);
4298                 }
4299               else
4300                 {
4301                   Id myidx = trj.idx++;
4302                   low[i] = myidx;
4303                   low[myidx] = i;
4304                 }
4305             }
4306           /* prune packages */
4307           for (i = 0; i < count; i++)
4308             if (low[i + 1] <= 0)
4309               unneededq->elements[i] = 0;
4310         }
4311       solv_free(low);
4312       solv_free(nrequires);
4313       queue_free(&edges);
4314
4315       /* finally remove all pruned entries from unneededq */
4316       for (i = j = 0; i < count; i++)
4317         if (unneededq->elements[i])
4318           unneededq->elements[j++] = unneededq->elements[i];
4319       queue_truncate(unneededq, j);
4320     }
4321   map_free(&cleandepsmap);
4322 }
4323
4324 /* EOF */