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