57895c01619284f840b29fa4cbc8ed72a8d06ba4
[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
1377   if (pool->considered && pool_disabled_solvable(pool, s))
1378     {
1379       /* disabled installed solvables must stay installed */
1380       solver_addrule(solv, p, 0, 0);
1381       return;
1382     }
1383
1384   /* Orphan detection. We cheat by looking at the feature rule, which
1385    * we already calculated */
1386   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1387   if (!r->p)
1388     {
1389 #ifdef ENABLE_LINKED_PKGS
1390       if (is_linked_pseudo_package(solv, s))
1391         {
1392           solver_addrule(solv, 0, 0, 0);
1393           return;
1394         }
1395 #endif
1396       p = 0;
1397       queue_push(&solv->orphaned, s - pool->solvables);         /* an orphaned package */
1398       if (solv->keep_orphans && !(solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, s - pool->solvables - solv->installed->start))))
1399         p = s - pool->solvables;        /* keep this orphaned package installed */
1400       solver_addrule(solv, p, 0, 0);
1401       return;
1402     }
1403
1404   /* find update candidates for 's' */
1405   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1406   dupinvolved = solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p));
1407   policy_findupdatepackages(solv, s, &qs, dupinvolved ? 2 : 0);
1408
1409   if (qs.count && solv->multiversion.size)
1410     {
1411       int i, j;
1412
1413       for (i = 0; i < qs.count; i++)
1414         if (MAPTST(&solv->multiversion, qs.elements[i]))
1415           break;
1416       if (i < qs.count)
1417         {
1418           /* filter out all multiversion packages as they don't update */
1419           d = pool_queuetowhatprovides(pool, &qs);      /* save qs away */
1420           for (j = i; i < qs.count; i++)
1421              {
1422               if (MAPTST(&solv->multiversion, qs.elements[i]))
1423                 {
1424                   Solvable *ps = pool->solvables + qs.elements[i];
1425                   /* check if there is an explicit obsoletes */
1426                   if (solv->keepexplicitobsoletes && ps->obsoletes && is_multiversion_obsoleteed(pool, s, ps))
1427                     {
1428                       qs.elements[j++] = qs.elements[i];
1429                       continue;
1430                     }
1431                   /* it's ok if they have same nevra */
1432                   if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
1433                     continue;
1434                 }
1435               qs.elements[j++] = qs.elements[i];
1436             }
1437
1438           if (j == 0 && dupinvolved && !dup_maykeepinstalled(solv, s))
1439             {
1440               /* this is a multiversion orphan */
1441               queue_push(&solv->orphaned, p);
1442               set_specialupdaters(solv, s, d);
1443               if (solv->keep_orphans && !(solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
1444                 {
1445                   /* we need to keep the orphan */
1446                   queue_free(&qs);
1447                   solver_addrule(solv, p, 0, 0);
1448                   return;
1449                 }
1450               /* we can drop it as long as we update */
1451               j = qs.count;
1452             }
1453
1454           if (j < qs.count)             /* filtered at least one package? */
1455             {
1456               if (d && (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p - solv->installed->start))))
1457                 {
1458                   /* non-orphan multiversion package, set special updaters if we want an update */
1459                   set_specialupdaters(solv, s, d);
1460                 }
1461               qs.count = j;
1462             }
1463           else
1464             {
1465               /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
1466               queue_free(&qs);
1467               solver_addrule(solv, p, 0, d);    /* allow update of s */
1468               return;
1469             }
1470         }
1471     }
1472   if (qs.count > 1)
1473     {
1474       d = pool_queuetowhatprovides(pool, &qs);
1475       queue_free(&qs);
1476       solver_addrule(solv, p, 0, d);    /* allow update of s */
1477     }
1478   else
1479     {
1480       d = qs.count ? qs.elements[0] : 0;
1481       queue_free(&qs);
1482       solver_addrule(solv, p, d, 0);    /* allow update of s */
1483     }
1484 }
1485
1486 static inline void
1487 disableupdaterule(Solver *solv, Id p)
1488 {
1489   Rule *r;
1490
1491   MAPSET(&solv->noupdate, p - solv->installed->start);
1492   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1493   if (r->p && r->d >= 0)
1494     solver_disablerule(solv, r);
1495   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1496   if (r->p && r->d >= 0)
1497     solver_disablerule(solv, r);
1498   if (solv->bestrules_info)
1499     {
1500       int i, ni;
1501       ni = solv->bestrules_end - solv->bestrules;
1502       for (i = solv->bestrules_up - solv->bestrules; i < ni; i++)
1503         if (solv->bestrules_info[i] == p)
1504           solver_disablerule(solv, solv->rules + solv->bestrules + i);
1505     }
1506 }
1507
1508 static inline void
1509 reenableupdaterule(Solver *solv, Id p)
1510 {
1511   Pool *pool = solv->pool;
1512   Rule *r;
1513
1514   MAPCLR(&solv->noupdate, p - solv->installed->start);
1515   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1516   if (r->p)
1517     {
1518       if (r->d < 0)
1519         {
1520           solver_enablerule(solv, r);
1521           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1522             {
1523               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1524               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1525             }
1526         }
1527     }
1528   else
1529     {
1530       r = solv->rules + solv->featurerules + (p - solv->installed->start);
1531       if (r->p && r->d < 0)
1532         {
1533           solver_enablerule(solv, r);
1534           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1535             {
1536               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1537               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1538             }
1539         }
1540     }
1541   if (solv->bestrules_info)
1542     {
1543       int i, ni;
1544       ni = solv->bestrules_end - solv->bestrules;
1545       for (i = solv->bestrules_up - solv->bestrules; i < ni; i++)
1546         if (solv->bestrules_info[i] == p)
1547           solver_enablerule(solv, solv->rules + solv->bestrules + i);
1548     }
1549 }
1550
1551
1552 /***********************************************************************
1553  ***
1554  ***  Infarch rule part
1555  ***
1556  ***  Infarch rules make sure the solver uses the best architecture of
1557  ***  a package if multiple archetectures are available
1558  ***
1559  ***/
1560
1561 void
1562 solver_addinfarchrules(Solver *solv, Map *addedmap)
1563 {
1564   Pool *pool = solv->pool;
1565   Repo *installed = pool->installed;
1566   int first, i, j;
1567   Id p, pp, aa;
1568   unsigned int a, bestscore;
1569   Solvable *s, *ps, *bests;
1570   Queue badq, allowedarchs;
1571   Queue lsq;
1572
1573   queue_init(&badq);
1574   queue_init(&allowedarchs);
1575   queue_init(&lsq);
1576   solv->infarchrules = solv->nrules;
1577   for (i = 1; i < pool->nsolvables; i++)
1578     {
1579       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1580         continue;
1581       s = pool->solvables + i;
1582       first = i;
1583       bestscore = 0;
1584       bests = 0;
1585       queue_empty(&allowedarchs);
1586       FOR_PROVIDES(p, pp, s->name)
1587         {
1588           ps = pool->solvables + p;
1589           if (ps->name != s->name || !MAPTST(addedmap, p))
1590             continue;
1591           if (p == i)
1592             first = 0;
1593           if (first)
1594             break;
1595           a = pool_arch2score(pool, ps->arch);
1596           if (a != 1 && installed && ps->repo == installed)
1597             {
1598               if (solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1599                 continue;
1600               queue_pushunique(&allowedarchs, ps->arch);        /* also ok to keep this architecture */
1601               continue;         /* but ignore installed solvables when calculating the best arch */
1602             }
1603           if (a && a != 1 && (!bestscore || a < bestscore))
1604             {
1605               bestscore = a;
1606               bests = ps;
1607             }
1608         }
1609       if (first)
1610         continue;
1611
1612       /* speed up common case where installed package already has best arch */
1613       if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1614         allowedarchs.count--;   /* installed arch is best */
1615
1616       if (allowedarchs.count && pool->implicitobsoleteusescolors && installed && bestscore)
1617         {
1618           /* need an extra pass for lockstep checking: we only allow to keep an inferior arch
1619            * if the corresponding installed package is not lock-stepped */
1620           queue_empty(&allowedarchs);
1621           FOR_PROVIDES(p, pp, s->name)
1622             {
1623               Id p2, pp2;
1624               ps = pool->solvables + p;
1625               if (ps->name != s->name || ps->repo != installed || !MAPTST(addedmap, p))
1626                 continue;
1627               if (solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1628                 continue;
1629               a = pool_arch2score(pool, ps->arch);
1630               if (!a)
1631                 {
1632                   queue_pushunique(&allowedarchs, ps->arch);    /* strange arch, allow */
1633                   continue;
1634                 }
1635               if (a == 1 || ((a ^ bestscore) & 0xffff0000) == 0)
1636                 continue;
1637               /* have installed package with inferior arch, check if lock-stepped */
1638               FOR_PROVIDES(p2, pp2, s->name)
1639                 {
1640                   Solvable *s2 = pool->solvables + p2;
1641                   unsigned int a2;
1642                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1643                     continue;
1644                   a2 = pool_arch2score(pool, s2->arch);
1645                   if (a2 && (a2 == 1 || ((a2 ^ bestscore) & 0xffff0000) == 0))
1646                     break;
1647                 }
1648               if (!p2)
1649                 queue_pushunique(&allowedarchs, ps->arch);
1650             }
1651         }
1652
1653       /* find all bad packages */
1654       queue_empty(&badq);
1655       FOR_PROVIDES(p, pp, s->name)
1656         {
1657           ps = pool->solvables + p;
1658           if (ps->name != s->name || !MAPTST(addedmap, p))
1659             continue;
1660           a = pool_arch2score(pool, ps->arch);
1661           if (a != 1 && bestscore && ((a ^ bestscore) & 0xffff0000) != 0)
1662             {
1663               if (installed && ps->repo == installed)
1664                 {
1665                   if (pool->implicitobsoleteusescolors)
1666                     queue_push(&badq, p);               /* special lock-step handling, see below */
1667                   continue;     /* always ok to keep an installed package */
1668                 }
1669               for (j = 0; j < allowedarchs.count; j++)
1670                 {
1671                   unsigned int aas;
1672                   aa = allowedarchs.elements[j];
1673                   if (ps->arch == aa)
1674                     break;
1675                   aas = pool_arch2score(pool, aa);
1676                   if (aas && ((a ^ aas) & 0xffff0000) == 0)
1677                     break;      /* compatible */
1678                 }
1679               if (j == allowedarchs.count)
1680                 queue_push(&badq, p);
1681             }
1682         }
1683
1684       /* block all solvables in the badq! */
1685       for (j = 0; j < badq.count; j++)
1686         {
1687           p = badq.elements[j];
1688           /* special lock-step handling */
1689           if (pool->implicitobsoleteusescolors)
1690             {
1691               Id p2;
1692               int haveinstalled = 0;
1693               queue_empty(&lsq);
1694               FOR_PROVIDES(p2, pp, s->name)
1695                 {
1696                   Solvable *s2 = pool->solvables + p2;
1697                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1698                     continue;
1699                   a = pool_arch2score(pool, s2->arch);
1700                   if (a && (a == 1 || ((a ^ bestscore) & 0xffff000) == 0))
1701                     {
1702                       queue_push(&lsq, p2);
1703                       if (installed && s2->repo == installed)
1704                         haveinstalled = 1;
1705                     }
1706                 }
1707               if (installed && pool->solvables[p].repo == installed && !haveinstalled)
1708                 continue;       /* installed package not in lock-step */
1709               if (lsq.count < 2)
1710                 solver_addrule(solv, -p, lsq.count ? lsq.elements[0] : 0, 0);
1711               else
1712                 solver_addrule(solv, -p, 0, pool_queuetowhatprovides(pool, &lsq));
1713             }
1714           else
1715             {
1716               solver_addrule(solv, -p, 0, 0);
1717             }
1718         }
1719     }
1720   queue_free(&lsq);
1721   queue_free(&badq);
1722   queue_free(&allowedarchs);
1723   solv->infarchrules_end = solv->nrules;
1724 }
1725
1726 static inline void
1727 disableinfarchrule(Solver *solv, Id name)
1728 {
1729   Pool *pool = solv->pool;
1730   Rule *r;
1731   int i;
1732   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1733     {
1734       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1735         solver_disablerule(solv, r);
1736     }
1737 }
1738
1739 static inline void
1740 reenableinfarchrule(Solver *solv, Id name)
1741 {
1742   Pool *pool = solv->pool;
1743   Rule *r;
1744   int i;
1745   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1746     {
1747       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1748         {
1749           solver_enablerule(solv, r);
1750           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1751             {
1752               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1753               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1754             }
1755         }
1756     }
1757 }
1758
1759
1760 /***********************************************************************
1761  ***
1762  ***  Dup rule part
1763  ***
1764  ***  Dup rules make sure a package is selected from the specified dup
1765  ***  repositories if an update candidate is included in one of them.
1766  ***
1767  ***/
1768
1769 static inline void
1770 add_cleandeps_updatepkg(Solver *solv, Id p)
1771 {
1772   if (!solv->cleandeps_updatepkgs)
1773     {
1774       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
1775       queue_init(solv->cleandeps_updatepkgs);
1776     }
1777   queue_pushunique(solv->cleandeps_updatepkgs, p);
1778 }
1779
1780 static void
1781 solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
1782 {
1783   Pool *pool = solv->pool;
1784   Solvable *ps, *s = pool->solvables + p;
1785   Repo *installed = solv->installed;
1786   Id pi, pip, obs, *obsp;
1787
1788   if (!solv->dupinvolvedmap.size)
1789     map_grow(&solv->dupinvolvedmap, pool->nsolvables);
1790
1791   MAPSET(&solv->dupinvolvedmap, p);
1792   if (targeted)
1793     MAPSET(&solv->dupmap, p);
1794   FOR_PROVIDES(pi, pip, s->name)
1795     {
1796       ps = pool->solvables + pi;
1797       if (ps->name != s->name)
1798         continue;
1799       MAPSET(&solv->dupinvolvedmap, pi);
1800       if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1801         {
1802           Id *opp, pi2;
1803           for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1804             if (pool->solvables[pi2].repo != installed)
1805               MAPSET(&solv->dupinvolvedmap, pi2);
1806         }
1807       if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0 && !solv->bestupdatemap_all)
1808         {
1809           if (!solv->bestupdatemap.size)
1810             map_grow(&solv->bestupdatemap, installed->end - installed->start);
1811           MAPSET(&solv->bestupdatemap, pi - installed->start);
1812         }
1813       if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1814         add_cleandeps_updatepkg(solv, pi);
1815       if (!targeted && ps->repo != installed)
1816         MAPSET(&solv->dupmap, pi);
1817     }
1818   if (s->repo == installed && solv->obsoletes && solv->obsoletes[p - installed->start])
1819     {
1820       Id *opp;
1821       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1822         {
1823           ps = pool->solvables + pi;
1824           if (ps->repo == installed)
1825             continue;
1826           MAPSET(&solv->dupinvolvedmap, pi);
1827           if (!targeted)
1828             MAPSET(&solv->dupmap, pi);
1829         }
1830     }
1831   if (targeted && s->repo != installed && s->obsoletes)
1832     {
1833       /* XXX: check obsoletes/provides combination */
1834       obsp = s->repo->idarraydata + s->obsoletes;
1835       while ((obs = *obsp++) != 0)
1836         {
1837           FOR_PROVIDES(pi, pip, obs)
1838             {
1839               Solvable *ps = pool->solvables + pi;
1840               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1841                 continue;
1842               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1843                 continue;
1844               MAPSET(&solv->dupinvolvedmap, pi);
1845               if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1846                 {
1847                   Id *opp, pi2;
1848                   for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1849                     if (pool->solvables[pi2].repo != installed)
1850                       MAPSET(&solv->dupinvolvedmap, pi2);
1851                 }
1852               if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0 && !solv->bestupdatemap_all)
1853                 {
1854                   if (!solv->bestupdatemap.size)
1855                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
1856                   MAPSET(&solv->bestupdatemap, pi - installed->start);
1857                 }
1858               if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1859                 add_cleandeps_updatepkg(solv, pi);
1860             }
1861         }
1862     }
1863 }
1864
1865 /* create the two dupmaps:
1866  * - dupmap: packages in that map are good to install/keep
1867  * - dupinvolvedmap: packages are subject to dup mode
1868  */
1869 void
1870 solver_createdupmaps(Solver *solv)
1871 {
1872   Queue *job = &solv->job;
1873   Pool *pool = solv->pool;
1874   Repo *installed = solv->installed;
1875   Id select, how, what, p, pp;
1876   Solvable *s;
1877   int i, targeted;
1878
1879   map_init(&solv->dupmap, pool->nsolvables);
1880   solv->dupinvolvedmap_all = 0;
1881   map_init(&solv->dupinvolvedmap, 0);
1882   for (i = 0; i < job->count; i += 2)
1883     {
1884       how = job->elements[i];
1885       select = job->elements[i] & SOLVER_SELECTMASK;
1886       what = job->elements[i + 1];
1887       switch (how & SOLVER_JOBMASK)
1888         {
1889         case SOLVER_DISTUPGRADE:
1890           if (select == SOLVER_SOLVABLE_REPO)
1891             {
1892               Repo *repo;
1893               if (what <= 0 || what > pool->nrepos)
1894                 break;
1895               repo = pool_id2repo(pool, what);
1896               if (!repo)
1897                 break;
1898               if (repo != installed && !(how & SOLVER_TARGETED) && solv->noautotarget)
1899                 break;
1900               targeted = repo != installed || (how & SOLVER_TARGETED) != 0;
1901               FOR_REPO_SOLVABLES(repo, p, s)
1902                 {
1903                   if (repo != installed && !pool_installable(pool, s))
1904                     continue;
1905                   solver_addtodupmaps(solv, p, how, targeted);
1906                 }
1907             }
1908           else if (select == SOLVER_SOLVABLE_ALL)
1909             {
1910               solv->dupinvolvedmap_all = 1;
1911               FOR_POOL_SOLVABLES(p)
1912                 {
1913                   Solvable *s = pool->solvables + p;
1914                   if (!s->repo || s->repo == installed)
1915                     continue;
1916                   if (!pool_installable(pool, s))
1917                     continue;
1918                   MAPSET(&solv->dupmap, p);
1919                 }
1920               if ((how & SOLVER_FORCEBEST) != 0)
1921                 solv->bestupdatemap_all = 1;
1922               if ((how & SOLVER_CLEANDEPS) != 0 && installed)
1923                 {
1924                   FOR_REPO_SOLVABLES(installed, p, s)
1925                     add_cleandeps_updatepkg(solv, p);
1926                 }
1927             }
1928           else
1929             {
1930               targeted = how & SOLVER_TARGETED ? 1 : 0;
1931               if (installed && !targeted && !solv->noautotarget)
1932                 {
1933                   FOR_JOB_SELECT(p, pp, select, what)
1934                     if (pool->solvables[p].repo == installed)
1935                       break;
1936                   targeted = p == 0;
1937                 }
1938               else if (!installed && !solv->noautotarget)
1939                 targeted = 1;
1940               FOR_JOB_SELECT(p, pp, select, what)
1941                 {
1942                   Solvable *s = pool->solvables + p;
1943                   if (!s->repo)
1944                     continue;
1945                   if (s->repo != installed && !targeted)
1946                     continue;
1947                   if (s->repo != installed && !pool_installable(pool, s))
1948                     continue;
1949                   solver_addtodupmaps(solv, p, how, targeted);
1950                 }
1951             }
1952           break;
1953         default:
1954           break;
1955         }
1956     }
1957   if (solv->dupinvolvedmap.size)
1958     MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1959   /* set update for all involved installed packages. We need to do
1960    * this before creating the update rules */
1961   if (solv->dupinvolvedmap_all)
1962     solv->updatemap_all = 1;
1963   else if (installed && !solv->updatemap_all && solv->dupinvolvedmap.size)
1964     {
1965       FOR_REPO_SOLVABLES(installed, p, s)
1966         {
1967           if (!MAPTST(&solv->dupinvolvedmap, p))
1968             continue;
1969           if (!solv->updatemap.size)
1970             map_grow(&solv->updatemap, installed->end - installed->start);
1971           MAPSET(&solv->updatemap, p - installed->start);
1972         }
1973     }
1974 }
1975
1976 void
1977 solver_freedupmaps(Solver *solv)
1978 {
1979   map_free(&solv->dupmap);
1980   /* we no longer free solv->dupinvolvedmap as we need it in
1981    * policy's priority pruning code. sigh. */
1982 }
1983
1984 void
1985 solver_addduprules(Solver *solv, Map *addedmap)
1986 {
1987   Pool *pool = solv->pool;
1988   Repo *installed = solv->installed;
1989   Id p, pp;
1990   Solvable *s, *ps;
1991   int first, i;
1992   Rule *r;
1993
1994   solv->duprules = solv->nrules;
1995   for (i = 1; i < pool->nsolvables; i++)
1996     {
1997       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1998         continue;
1999       s = pool->solvables + i;
2000       first = i;
2001       FOR_PROVIDES(p, pp, s->name)
2002         {
2003           ps = pool->solvables + p;
2004           if (ps->name != s->name || !MAPTST(addedmap, p))
2005             continue;
2006           if (p == i)
2007             first = 0;
2008           if (first)
2009             break;
2010           if (!solv->dupinvolvedmap_all && !MAPTST(&solv->dupinvolvedmap, p))
2011             continue;
2012           if (installed && ps->repo == installed)
2013             {
2014               if (pool->considered && pool_disabled_solvable(pool, ps))
2015                 continue;               /* always keep disabled installed packages */
2016               if (!MAPTST(&solv->dupmap, p))
2017                 {
2018                   Id ip, ipp;
2019                   /* is installed identical to a good one? */
2020                   FOR_PROVIDES(ip, ipp, ps->name)
2021                     {
2022                       Solvable *is = pool->solvables + ip;
2023                       if (!MAPTST(&solv->dupmap, ip))
2024                         continue;
2025                       if (is->evr == ps->evr && solvable_identical(ps, is))
2026                         break;
2027                     }
2028                   if (ip)
2029                     {
2030                       /* ok, identical to a good one. we may keep this package. */
2031                       MAPSET(&solv->dupmap, p);         /* for best rules processing */
2032                       continue;
2033                     }
2034                   /* check if it's orphaned. If yes, we may keep it */
2035                   r = solv->rules + solv->updaterules + (p - installed->start);
2036                   if (!r->p)
2037                     r = solv->rules + solv->featurerules + (p - installed->start);
2038                   if (!r->p)
2039                     {
2040                       /* no update/feature rule, this is an orphan */
2041                       MAPSET(&solv->dupmap, p);         /* for best rules processing */
2042                       continue;
2043                     }
2044                   if (solv->specialupdaters && solv->specialupdaters[p - installed->start])
2045                     {
2046                       /* this is a multiversion orphan, we're good if an update is installed */
2047                       solver_addrule(solv, -p, 0, solv->specialupdaters[p - installed->start]);
2048                       continue;
2049                     }
2050                   if (r->p == p && !r->d && !r->w2)
2051                     {
2052                       r = solv->rules + solv->featurerules + (p - installed->start);
2053                       if (!r->p || (!r->d && !r->w2))
2054                         {
2055                           /* this is an orphan */
2056                           MAPSET(&solv->dupmap, p);             /* for best rules processing */
2057                           continue;
2058                         }
2059                     }
2060                   solver_addrule(solv, -p, 0, 0);       /* no match, sorry */
2061                 }
2062             }
2063           else if (!MAPTST(&solv->dupmap, p))
2064             solver_addrule(solv, -p, 0, 0);
2065         }
2066     }
2067   solv->duprules_end = solv->nrules;
2068 }
2069
2070
2071 static inline void
2072 disableduprule(Solver *solv, Id name)
2073 {
2074   Pool *pool = solv->pool;
2075   Rule *r;
2076   int i;
2077   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
2078     {
2079       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
2080         solver_disablerule(solv, r);
2081     }
2082 }
2083
2084 static inline void
2085 reenableduprule(Solver *solv, Id name)
2086 {
2087   Pool *pool = solv->pool;
2088   Rule *r;
2089   int i;
2090   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
2091     {
2092       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
2093         {
2094           solver_enablerule(solv, r);
2095           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
2096             {
2097               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
2098               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
2099             }
2100         }
2101     }
2102 }
2103
2104
2105 /***********************************************************************
2106  ***
2107  ***  Policy rule disabling/reenabling
2108  ***
2109  ***  Disable all policy rules that conflict with our jobs. If a job
2110  ***  gets disabled later on, reenable the involved policy rules again.
2111  ***
2112  ***/
2113
2114 #define DISABLE_UPDATE  1
2115 #define DISABLE_INFARCH 2
2116 #define DISABLE_DUP     3
2117
2118 static void
2119 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
2120 {
2121   Pool *pool = solv->pool;
2122   Id select, p, pp;
2123   Repo *installed;
2124   Solvable *s;
2125   int i, j, set, qstart;
2126   Map omap;
2127
2128   installed = solv->installed;
2129   select = how & SOLVER_SELECTMASK;
2130   switch (how & SOLVER_JOBMASK)
2131     {
2132     case SOLVER_INSTALL:
2133       set = how & SOLVER_SETMASK;
2134       if (!(set & SOLVER_NOAUTOSET))
2135         {
2136           /* automatically add set bits by analysing the job */
2137           if (select == SOLVER_SOLVABLE_NAME)
2138             set |= SOLVER_SETNAME;
2139           if (select == SOLVER_SOLVABLE)
2140             set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
2141           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
2142             {
2143               Reldep *rd = GETRELDEP(pool, what);
2144               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
2145                 {
2146                   if (pool->disttype != DISTTYPE_DEB)
2147                     {
2148                       const char *rel = strrchr(pool_id2str(pool, rd->evr), '-');
2149                       set |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
2150                     }
2151                   else
2152                     set |= SOLVER_SETEVR;
2153                 }
2154               if (rd->flags <= 7 && ISRELDEP(rd->name))
2155                 rd = GETRELDEP(pool, rd->name);
2156               if (rd->flags == REL_ARCH)
2157                 set |= SOLVER_SETARCH;
2158             }
2159         }
2160       else
2161         set &= ~SOLVER_NOAUTOSET;
2162       if (!set)
2163         return;
2164       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
2165         {
2166           if (select == SOLVER_SOLVABLE)
2167             {
2168               for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2169                 if (solv->rules[i].p == -what)
2170                   break;
2171               if (i < solv->infarchrules_end)
2172                 queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
2173             }
2174           else
2175             {
2176               int qcnt = q->count;
2177               /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
2178                  they are not useful for SOLVER_INSTALL jobs anyway */
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;           /* already have that DISABLE_INFARCH element */
2188                   for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2189                     if (solv->rules[i].p == -p)
2190                       break;
2191                   if (i < solv->infarchrules_end)
2192                     queue_push2(q, DISABLE_INFARCH, s->name);
2193                 }
2194             }
2195         }
2196       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
2197         {
2198           if (select == SOLVER_SOLVABLE)
2199             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
2200           else
2201             {
2202               int qcnt = q->count;
2203               FOR_JOB_SELECT(p, pp, select, what)
2204                 {
2205                   s = pool->solvables + p;
2206                   /* unify names */
2207                   for (i = qcnt; i < q->count; i += 2)
2208                     if (q->elements[i + 1] == s->name)
2209                       break;
2210                   if (i < q->count)
2211                     continue;
2212                   queue_push2(q, DISABLE_DUP, s->name);
2213                 }
2214             }
2215         }
2216       if (!installed || installed->end == installed->start)
2217         return;
2218       /* now the hard part: disable some update rules */
2219
2220       /* first check if we have installed or multiversion packages in the job */
2221       FOR_JOB_SELECT(p, pp, select, what)
2222         {
2223           if (pool->solvables[p].repo == installed)
2224             return;
2225           if (solv->multiversion.size && MAPTST(&solv->multiversion, p) && !solv->keepexplicitobsoletes)
2226             return;             /* will not obsolete anything, so just return */
2227         }
2228       omap.size = 0;
2229       qstart = q->count;
2230       FOR_JOB_SELECT(p, pp, select, what)
2231         {
2232           solver_intersect_obsoleted(solv, p, q, qstart, &omap);
2233           if (q->count == qstart)
2234             break;
2235         }
2236       if (omap.size)
2237         map_free(&omap);
2238
2239       if (qstart == q->count)
2240         return;         /* nothing to prune */
2241
2242       /* convert result to (DISABLE_UPDATE, p) pairs */
2243       i = q->count;
2244       for (j = qstart; j < i; j++)
2245         queue_push(q, q->elements[j]);
2246       for (j = qstart; j < q->count; j += 2)
2247         {
2248           q->elements[j] = DISABLE_UPDATE;
2249           q->elements[j + 1] = q->elements[i++];
2250         }
2251
2252       /* now that we know which installed packages are obsoleted check each of them */
2253       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
2254         return;         /* all is set, nothing to do */
2255
2256       for (i = j = qstart; i < q->count; i += 2)
2257         {
2258           Solvable *is = pool->solvables + q->elements[i + 1];
2259           FOR_JOB_SELECT(p, pp, select, what)
2260             {
2261               int illegal = 0;
2262               s = pool->solvables + p;
2263               if ((set & SOLVER_SETEVR) != 0)
2264                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
2265               if ((set & SOLVER_SETNAME) != 0)
2266                 illegal |= POLICY_ILLEGAL_NAMECHANGE;   /* ignore */
2267               if ((set & SOLVER_SETARCH) != 0)
2268                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
2269               if ((set & SOLVER_SETVENDOR) != 0)
2270                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
2271               illegal = policy_is_illegal(solv, is, s, illegal);
2272               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
2273                 {
2274                   /* it's ok if the EV is different */
2275                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
2276                     illegal = 0;
2277                 }
2278               if (illegal)
2279                 break;
2280             }
2281           if (!p)
2282             {   
2283               /* no package conflicts with the update rule */
2284               /* thus keep the DISABLE_UPDATE */
2285               q->elements[j + 1] = q->elements[i + 1];
2286               j += 2;
2287             }
2288         }
2289       queue_truncate(q, j);
2290       return;
2291
2292     case SOLVER_ERASE:
2293       if (!installed)
2294         break;
2295       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2296         {
2297           FOR_REPO_SOLVABLES(installed, p, s)
2298             queue_push2(q, DISABLE_UPDATE, p);
2299         }
2300       FOR_JOB_SELECT(p, pp, select, what)
2301         if (pool->solvables[p].repo == installed)
2302           {
2303             queue_push2(q, DISABLE_UPDATE, p);
2304 #ifdef ENABLE_LINKED_PKGS
2305             if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
2306               queue_push2(q, DISABLE_UPDATE, solv->instbuddy[p - installed->start]);
2307 #endif
2308           }
2309       return;
2310
2311     case SOLVER_LOCK:
2312       if (!installed)
2313         break;
2314       qstart = q->count;
2315       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2316         {
2317           FOR_REPO_SOLVABLES(installed, p, s)
2318             {
2319               for (i = qstart; i < q->count; i += 2)
2320                 if (q->elements[i] == DISABLE_DUP && q->elements[i + 1] == pool->solvables[p].name)
2321                   break;
2322               if (i == q->count)
2323                 queue_push2(q, DISABLE_DUP, pool->solvables[p].name);
2324             }
2325         }
2326       FOR_JOB_SELECT(p, pp, select, what)
2327         {
2328           if (pool->solvables[p].repo != installed)
2329             continue;
2330           for (i = qstart; i < q->count; i += 2)
2331             if (q->elements[i] == DISABLE_DUP && q->elements[i + 1] == pool->solvables[p].name)
2332               break;
2333           if (i == q->count)
2334             queue_push2(q, DISABLE_DUP, pool->solvables[p].name);
2335         }
2336       break;
2337
2338     default:
2339       return;
2340     }
2341 }
2342
2343 /* disable all policy rules that are in conflict with our job list */
2344 void
2345 solver_disablepolicyrules(Solver *solv)
2346 {
2347   Queue *job = &solv->job;
2348   int i, j;
2349   Queue allq;
2350   Rule *r;
2351   Id lastjob = -1;
2352   Id allqbuf[128];
2353
2354   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2355
2356   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2357     {
2358       r = solv->rules + i;
2359       if (r->d < 0)     /* disabled? */
2360         continue;
2361       j = solv->ruletojob.elements[i - solv->jobrules];
2362       if (j == lastjob)
2363         continue;
2364       lastjob = j;
2365       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2366     }
2367   if (solv->cleandepsmap.size)
2368     {
2369       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2370       for (i = solv->installed->start; i < solv->installed->end; i++)
2371         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
2372           queue_push2(&allq, DISABLE_UPDATE, i);
2373     }
2374   MAPZERO(&solv->noupdate);
2375   for (i = 0; i < allq.count; i += 2)
2376     {
2377       Id type = allq.elements[i], arg = allq.elements[i + 1];
2378       switch(type)
2379         {
2380         case DISABLE_UPDATE:
2381           disableupdaterule(solv, arg);
2382           break;
2383         case DISABLE_INFARCH:
2384           disableinfarchrule(solv, arg);
2385           break;
2386         case DISABLE_DUP:
2387           disableduprule(solv, arg);
2388           break;
2389         default:
2390           break;
2391         }
2392     }
2393   queue_free(&allq);
2394 }
2395
2396 /* we just disabled job #jobidx, now reenable all policy rules that were
2397  * disabled because of this job */
2398 void
2399 solver_reenablepolicyrules(Solver *solv, int jobidx)
2400 {
2401   Queue *job = &solv->job;
2402   int i, j, k, ai;
2403   Queue q, allq;
2404   Rule *r;
2405   Id lastjob = -1;
2406   Id qbuf[32], allqbuf[32];
2407
2408   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2409   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
2410   if (!q.count)
2411     {
2412       queue_free(&q);
2413       return;
2414     }
2415   /* now remove everything from q that is disabled by other jobs */
2416
2417   /* first remove cleandeps packages, they count as DISABLE_UPDATE */
2418   if (solv->cleandepsmap.size)
2419     {
2420       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2421       for (j = k = 0; j < q.count; j += 2)
2422         {
2423           if (q.elements[j] == DISABLE_UPDATE)
2424             {
2425               Id p = q.elements[j + 1];
2426               if (p >= solv->installed->start && p < solv->installed->end && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2427                 continue;       /* remove element from q */
2428             }
2429           q.elements[k++] = q.elements[j];
2430           q.elements[k++] = q.elements[j + 1];
2431         }
2432       q.count = k;
2433       if (!q.count)
2434         {
2435           queue_free(&q);
2436           return;
2437         }
2438     }
2439
2440   /* now go through the disable list of all other jobs */
2441   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2442   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2443     {
2444       r = solv->rules + i;
2445       if (r->d < 0)     /* disabled? */
2446         continue;
2447       j = solv->ruletojob.elements[i - solv->jobrules];
2448       if (j == lastjob)
2449         continue;
2450       lastjob = j;
2451       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2452       if (!allq.count)
2453         continue;
2454       /* remove all elements in allq from q */
2455       for (j = k = 0; j < q.count; j += 2)
2456         {
2457           Id type = q.elements[j], arg = q.elements[j + 1];
2458           for (ai = 0; ai < allq.count; ai += 2)
2459             if (allq.elements[ai] == type && allq.elements[ai + 1] == arg)
2460               break;
2461           if (ai < allq.count)
2462             continue;   /* found it in allq, remove element from q */
2463           q.elements[k++] = q.elements[j];
2464           q.elements[k++] = q.elements[j + 1];
2465         }
2466       q.count = k;
2467       if (!q.count)
2468         {
2469           queue_free(&q);
2470           queue_free(&allq);
2471           return;
2472         }
2473       queue_empty(&allq);
2474     }
2475   queue_free(&allq);
2476
2477   /* now re-enable anything that's left in q */
2478   for (j = 0; j < q.count; j += 2)
2479     {
2480       Id type = q.elements[j], arg = q.elements[j + 1];
2481       switch(type)
2482         {
2483         case DISABLE_UPDATE:
2484           reenableupdaterule(solv, arg);
2485           break;
2486         case DISABLE_INFARCH:
2487           reenableinfarchrule(solv, arg);
2488           break;
2489         case DISABLE_DUP:
2490           reenableduprule(solv, arg);
2491           break;
2492         }
2493     }
2494   queue_free(&q);
2495 }
2496
2497 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
2498  * disabled because of this */
2499 void
2500 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
2501 {
2502   Queue *job = &solv->job;
2503   int i, j;
2504   Queue allq;
2505   Rule *r;
2506   Id lastjob = -1;
2507   Id allqbuf[128];
2508
2509   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2510   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2511     {
2512       r = solv->rules + i;
2513       if (r->d < 0)     /* disabled? */
2514         continue;
2515       j = solv->ruletojob.elements[i - solv->jobrules];
2516       if (j == lastjob)
2517         continue;
2518       lastjob = j;
2519       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2520     }
2521   for (i = 0; i < allq.count; i += 2)
2522     if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
2523       break;
2524   if (i == allq.count)
2525     reenableupdaterule(solv, pkg);
2526   queue_free(&allq);
2527 }
2528
2529
2530 /***********************************************************************
2531  ***
2532  ***  Rule info part, tell the user what the rule is about.
2533  ***
2534  ***/
2535
2536 static void
2537 addpkgruleinfo(Solver *solv, Id p, Id p2, Id d, int type, Id dep)
2538 {
2539   Pool *pool = solv->pool;
2540   Rule *r;
2541
2542   if (d)
2543     {
2544       assert(!p2 && d > 0);
2545       if (!pool->whatprovidesdata[d])
2546         d = 0;
2547       else if (!pool->whatprovidesdata[d + 1])
2548         {
2549           p2 = pool->whatprovidesdata[d];
2550           d = 0;
2551         }
2552     }
2553
2554   /* check if this creates the rule we're searching for */
2555   r = solv->rules + solv->ruleinfoq->elements[0];
2556   if (d)
2557     {
2558       /* three or more literals */
2559       Id od = r->d < 0 ? -r->d - 1 : r->d;
2560       if (p != r->p && !od)
2561         return;
2562       if (d != od)
2563         {
2564           Id *dp = pool->whatprovidesdata + d;
2565           Id *odp = pool->whatprovidesdata + od;
2566           while (*dp)
2567             if (*dp++ != *odp++)
2568               return;
2569           if (*odp)
2570             return;
2571         }
2572       if (p < 0 && pool->whatprovidesdata[d] < 0 && type == SOLVER_RULE_PKG_CONFLICTS)
2573         p2 = pool->whatprovidesdata[d];
2574     }
2575   else
2576     {
2577       /* one or two literals */
2578       Id op = p, op2 = p2;
2579       if (op2 && op > op2)      /* normalize */
2580         {
2581           Id o = op;
2582           op = op2;
2583           op2 = o;
2584         }
2585       if (r->p != op || r->w2 != op2 || (r->d && r->d != -1))
2586         return;
2587       if (type == SOLVER_RULE_PKG_CONFLICTS && !p2)
2588         p2 = -SYSTEMSOLVABLE;
2589       if (type == SOLVER_RULE_PKG_SAME_NAME)
2590         {
2591           p = op;       /* we normalize same name order */
2592           p2 = op2;
2593         }
2594     }
2595   /* yep, rule matches. record info */
2596   queue_push(solv->ruleinfoq, type);
2597   queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2598   queue_push(solv->ruleinfoq, p2 < 0 ? -p2 : 0);
2599   queue_push(solv->ruleinfoq, dep);
2600 }
2601
2602 static int
2603 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2604 {
2605   const Id *a = ap, *b = bp;
2606   int r;
2607
2608   r = a[0] - b[0];
2609   if (r)
2610     return r;
2611   r = a[1] - b[1];
2612   if (r)
2613     return r;
2614   r = a[2] - b[2];
2615   if (r)
2616     return r;
2617   r = a[3] - b[3];
2618   if (r)
2619     return r;
2620   return 0;
2621 }
2622
2623 static void
2624 getpkgruleinfos(Solver *solv, Rule *r, Queue *rq)
2625 {
2626   Pool *pool = solv->pool;
2627   Id l, pp;
2628   if (r->p >= 0)
2629     return;
2630   queue_push(rq, r - solv->rules);      /* push the rule we're interested in */
2631   solv->ruleinfoq = rq;
2632   FOR_RULELITERALS(l, pp, r)
2633     {
2634       if (l >= 0)
2635         break;
2636       solver_addpkgrulesforsolvable(solv, pool->solvables - l, 0);
2637     }
2638 #ifdef ENABLE_LINKED_PKGS
2639   FOR_RULELITERALS(l, pp, r)
2640     {
2641       if (l < 0)
2642         {
2643           if (l == r->p)
2644             continue;
2645           break;
2646         }
2647       if (!strchr(pool_id2str(pool, pool->solvables[l].name), ':') || !has_package_link(pool, pool->solvables + l))
2648         break;
2649       add_package_link(solv, pool->solvables + l, 0, 0);
2650     }
2651 #endif
2652   solv->ruleinfoq = 0;
2653   queue_shift(rq);
2654 }
2655
2656 int
2657 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2658 {
2659   Rule *r = solv->rules + rid;
2660   int i, j;
2661
2662   queue_empty(rq);
2663   if (rid <= 0 || rid >= solv->pkgrules_end)
2664     {
2665       Id type, from, to, dep;
2666       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2667       queue_push(rq, type);
2668       queue_push(rq, from);
2669       queue_push(rq, to);
2670       queue_push(rq, dep);
2671       return 1;
2672     }
2673   getpkgruleinfos(solv, r, rq);
2674   /* now sort & unify em */
2675   if (!rq->count)
2676     return 0;
2677   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2678   /* throw out identical entries */
2679   for (i = j = 0; i < rq->count; i += 4)
2680     {
2681       if (j)
2682         {
2683           if (rq->elements[i] == rq->elements[j - 4] &&
2684               rq->elements[i + 1] == rq->elements[j - 3] &&
2685               rq->elements[i + 2] == rq->elements[j - 2] &&
2686               rq->elements[i + 3] == rq->elements[j - 1])
2687             continue;
2688         }
2689       rq->elements[j++] = rq->elements[i];
2690       rq->elements[j++] = rq->elements[i + 1];
2691       rq->elements[j++] = rq->elements[i + 2];
2692       rq->elements[j++] = rq->elements[i + 3];
2693     }
2694   rq->count = j;
2695   return j / 4;
2696 }
2697
2698 SolverRuleinfo
2699 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2700 {
2701   Pool *pool = solv->pool;
2702   Rule *r = solv->rules + rid;
2703   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2704
2705   if (fromp)
2706     *fromp = 0;
2707   if (top)
2708     *top = 0;
2709   if (depp)
2710     *depp = 0;
2711   if (rid > 0 && rid < solv->pkgrules_end)
2712     {
2713       Queue rq;
2714       int i;
2715
2716       if (r->p >= 0)
2717         return SOLVER_RULE_PKG;
2718       if (fromp)
2719         *fromp = -r->p;
2720       queue_init(&rq);
2721       getpkgruleinfos(solv, r, &rq);
2722       type = SOLVER_RULE_PKG;
2723       for (i = 0; i < rq.count; i += 4)
2724         {
2725           Id qt, qo, qp, qd;
2726           qt = rq.elements[i];
2727           qp = rq.elements[i + 1];
2728           qo = rq.elements[i + 2];
2729           qd = rq.elements[i + 3];
2730           if (type == SOLVER_RULE_PKG || type > qt)
2731             {
2732               type = qt;
2733               if (fromp)
2734                 *fromp = qp;
2735               if (top)
2736                 *top = qo;
2737               if (depp)
2738                 *depp = qd;
2739             }
2740         }
2741       queue_free(&rq);
2742       return type;
2743     }
2744   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2745     {
2746       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2747       if (fromp)
2748         *fromp = jidx;
2749       if (top)
2750         *top = solv->job.elements[jidx];
2751       if (depp)
2752         *depp = solv->job.elements[jidx + 1];
2753       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2754         {
2755           Id how = solv->job.elements[jidx];
2756           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2757             return SOLVER_RULE_JOB_UNKNOWN_PACKAGE;
2758           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2759             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2760           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2761             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2762           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2763             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2764           return SOLVER_RULE_JOB_UNSUPPORTED;
2765         }
2766       return SOLVER_RULE_JOB;
2767     }
2768   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2769     {
2770       if (fromp)
2771         *fromp = solv->installed->start + (rid - solv->updaterules);
2772       return SOLVER_RULE_UPDATE;
2773     }
2774   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2775     {
2776       if (fromp)
2777         *fromp = solv->installed->start + (rid - solv->featurerules);
2778       return SOLVER_RULE_FEATURE;
2779     }
2780   if (rid >= solv->duprules && rid < solv->duprules_end)
2781     {
2782       if (fromp)
2783         *fromp = -r->p;
2784       if (depp)
2785         *depp = pool->solvables[-r->p].name;
2786       return SOLVER_RULE_DISTUPGRADE;
2787     }
2788   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2789     {
2790       if (fromp)
2791         *fromp = -r->p;
2792       if (depp)
2793         *depp = pool->solvables[-r->p].name;
2794       return SOLVER_RULE_INFARCH;
2795     }
2796   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2797     {
2798       if (fromp && solv->bestrules_info[rid - solv->bestrules] > 0)
2799         *fromp = solv->bestrules_info[rid - solv->bestrules];
2800       return SOLVER_RULE_BEST;
2801     }
2802   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
2803     {
2804       if (fromp)
2805         *fromp = -r->p;
2806       if (top)
2807         {
2808           /* first solvable is enough, we just need it for the name */
2809           if (!r->d || r->d == -1)
2810             *top = r->w2;
2811           else
2812             *top = pool->whatprovidesdata[r->d < 0 ? -r->d : r->d];
2813         }
2814       if (depp)
2815         *depp = solv->yumobsrules_info[rid - solv->yumobsrules];
2816       return SOLVER_RULE_YUMOBS;
2817     }
2818   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2819     return SOLVER_RULE_CHOICE;
2820   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
2821     return SOLVER_RULE_RECOMMENDS;
2822   if (rid >= solv->learntrules)
2823     return SOLVER_RULE_LEARNT;
2824   return SOLVER_RULE_UNKNOWN;
2825 }
2826
2827 SolverRuleinfo
2828 solver_ruleclass(Solver *solv, Id rid)
2829 {
2830   if (rid <= 0)
2831     return SOLVER_RULE_UNKNOWN;
2832   if (rid > 0 && rid < solv->pkgrules_end)
2833     return SOLVER_RULE_PKG;
2834   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2835     return SOLVER_RULE_JOB;
2836   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2837     return SOLVER_RULE_UPDATE;
2838   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2839     return SOLVER_RULE_FEATURE;
2840   if (rid >= solv->duprules && rid < solv->duprules_end)
2841     return SOLVER_RULE_DISTUPGRADE;
2842   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2843     return SOLVER_RULE_INFARCH;
2844   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2845     return SOLVER_RULE_BEST;
2846   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
2847     return SOLVER_RULE_YUMOBS;
2848   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2849     return SOLVER_RULE_CHOICE;
2850   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
2851     return SOLVER_RULE_RECOMMENDS;
2852   if (rid >= solv->learntrules && rid < solv->nrules)
2853     return SOLVER_RULE_LEARNT;
2854   return SOLVER_RULE_UNKNOWN;
2855 }
2856
2857 void
2858 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2859 {
2860   Pool *pool = solv->pool;
2861   Id p, pp;
2862   Rule *r;
2863
2864   queue_empty(q);
2865   r = solv->rules + rid;
2866   FOR_RULELITERALS(p, pp, r)
2867     if (p != -SYSTEMSOLVABLE)
2868       queue_push(q, p);
2869   if (!q->count)
2870     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
2871 }
2872
2873 int
2874 solver_rule2jobidx(Solver *solv, Id rid)
2875 {
2876   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2877     return 0;
2878   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2879 }
2880
2881 /* job rule introspection */
2882 Id
2883 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2884 {
2885   int idx;
2886   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2887     {
2888       if (whatp)
2889         *whatp = 0;
2890       return 0;
2891     }
2892   idx = solv->ruletojob.elements[rid - solv->jobrules];
2893   if (whatp)
2894     *whatp = solv->job.elements[idx + 1];
2895   return solv->job.elements[idx];
2896 }
2897
2898 /* update/feature rule introspection */
2899 Id
2900 solver_rule2solvable(Solver *solv, Id rid)
2901 {
2902   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2903     return rid - solv->updaterules;
2904   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2905     return rid - solv->featurerules;
2906   return 0;
2907 }
2908
2909 Id
2910 solver_rule2pkgrule(Solver *solv, Id rid)
2911 {
2912   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2913     return solv->choicerules_info[rid - solv->choicerules];
2914   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
2915     return solv->recommendsrules_info[rid - solv->recommendsrules];
2916   return 0;
2917 }
2918
2919 static void
2920 solver_rule2rules_rec(Solver *solv, Id rid, Queue *q, Map *seen)
2921 {
2922   int i;
2923   Id rid2;
2924
2925   if (seen)
2926     MAPSET(seen, rid);
2927   for (i = solv->learnt_why.elements[rid - solv->learntrules]; (rid2 = solv->learnt_pool.elements[i]) != 0; i++)
2928     {
2929       if (seen)
2930         {
2931           if (MAPTST(seen, rid2))
2932             continue;
2933           if (rid2 >= solv->learntrules)
2934             solver_rule2rules_rec(solv, rid2, q, seen);
2935           continue;
2936         }
2937       queue_push(q, rid2);
2938     }
2939 }
2940
2941 /* learnt rule introspection */
2942 void
2943 solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
2944 {
2945   queue_empty(q);
2946   if (rid < solv->learntrules || rid >= solv->nrules)
2947     return;
2948   if (recursive)
2949     {
2950       Map seen;
2951       map_init(&seen, solv->nrules);
2952       solver_rule2rules_rec(solv, rid, q, &seen);
2953       map_free(&seen);
2954     }
2955   else
2956     solver_rule2rules_rec(solv, rid, q, 0);
2957 }
2958
2959
2960 /* check if the newest versions of pi still provides the dependency we're looking for */
2961 static int
2962 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
2963 {
2964   Pool *pool = solv->pool;
2965   Rule *ur;
2966   Id p, pp;
2967   int i;
2968
2969   if (!q->count || q->elements[0] != pi)
2970     {
2971       if (q->count)
2972         queue_empty(q);
2973       ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
2974       if (!ur->p)
2975         ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
2976       if (!ur->p)
2977         return 0;
2978       queue_push2(q, pi, 0);
2979       FOR_RULELITERALS(p, pp, ur)
2980         if (p > 0)
2981           queue_push(q, p);
2982     }
2983   if (q->count == 2)
2984     return 1;
2985   if (q->count == 3)
2986     {
2987       p = q->elements[2];
2988       return MAPTST(m, p) ? 0 : 1;
2989     }
2990   if (!q->elements[1])
2991     {
2992       for (i = 2; i < q->count; i++)
2993         if (!MAPTST(m, q->elements[i]))
2994           break;
2995       if (i == q->count)
2996         return 0;       /* all provide it, no need to filter */
2997       /* some don't provide it, have to filter */
2998       queue_deleten(q, 0, 2);
2999       policy_filter_unwanted(solv, q, POLICY_MODE_CHOOSE);
3000       queue_unshift(q, 1);      /* filter mark */
3001       queue_unshift(q, pi);
3002     }
3003   for (i = 2; i < q->count; i++)
3004     if (MAPTST(m, q->elements[i]))
3005       return 0;         /* at least one provides it */
3006   return 1;     /* none of the new packages provided it */
3007 }
3008
3009 static inline void
3010 queue_removeelement(Queue *q, Id el)
3011 {
3012   int i, j;
3013   for (i = 0; i < q->count; i++)
3014     if (q->elements[i] == el)
3015       break;
3016   if (i < q->count)
3017     {
3018       for (j = i++; i < q->count; i++)
3019         if (q->elements[i] != el)
3020           q->elements[j++] = q->elements[i];
3021       queue_truncate(q, j);
3022     }
3023 }
3024
3025 void
3026 solver_addchoicerules(Solver *solv)
3027 {
3028   Pool *pool = solv->pool;
3029   Map m, mneg;
3030   Rule *r;
3031   Queue q, qi, qcheck, infoq;
3032   int i, j, rid, havechoice;
3033   Id p, d, pp;
3034   Id p2, pp2;
3035   Solvable *s, *s2;
3036   Id lastaddedp, lastaddedd;
3037   int lastaddedcnt;
3038   unsigned int now;
3039
3040   solv->choicerules = solv->nrules;
3041   if (!pool->installed)
3042     {
3043       solv->choicerules_end = solv->nrules;
3044       return;
3045     }
3046   now = solv_timems(0);
3047   solv->choicerules_info = solv_calloc(solv->pkgrules_end, sizeof(Id));
3048   queue_init(&q);
3049   queue_init(&qi);
3050   queue_init(&qcheck);
3051   queue_init(&infoq);
3052   map_init(&m, pool->nsolvables);
3053   map_init(&mneg, pool->nsolvables);
3054   /* set up negative assertion map from infarch and dup rules */
3055   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
3056     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3057       MAPSET(&mneg, -r->p);
3058   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
3059     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3060       MAPSET(&mneg, -r->p);
3061   lastaddedp = 0;
3062   lastaddedd = 0;
3063   lastaddedcnt = 0;
3064   for (rid = 1; rid < solv->pkgrules_end ; rid++)
3065     {
3066       r = solv->rules + rid;
3067       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
3068         continue;       /* only look at requires rules */
3069       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
3070       queue_empty(&q);
3071       queue_empty(&qi);
3072       havechoice = 0;
3073       FOR_RULELITERALS(p, pp, r)
3074         {
3075           if (p < 0)
3076             continue;
3077           s = pool->solvables + p;
3078           if (!s->repo)
3079             continue;
3080           if (s->repo == pool->installed)
3081             {
3082               queue_push(&q, p);
3083               continue;
3084             }
3085           /* check if this package is "blocked" by a installed package */
3086           s2 = 0;
3087           FOR_PROVIDES(p2, pp2, s->name)
3088             {
3089               s2 = pool->solvables + p2;
3090               if (s2->repo != pool->installed)
3091                 continue;
3092               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
3093                 continue;
3094               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
3095                 continue;
3096               break;
3097             }
3098           if (p2)
3099             {
3100               /* found installed package p2 that we can update to p */
3101               if (MAPTST(&mneg, p))
3102                 continue;
3103               if (policy_is_illegal(solv, s2, s, 0))
3104                 continue;
3105 #if 0
3106               if (solver_choicerulecheck(solv, p2, r, &m))
3107                 continue;
3108               queue_push(&qi, p2);
3109 #else
3110               queue_push2(&qi, p2, p);
3111 #endif
3112               queue_push(&q, p);
3113               continue;
3114             }
3115           if (s->obsoletes)
3116             {
3117               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3118               s2 = 0;
3119               while ((obs = *obsp++) != 0)
3120                 {
3121                   FOR_PROVIDES(p2, pp2, obs)
3122                     {
3123                       s2 = pool->solvables + p2;
3124                       if (s2->repo != pool->installed)
3125                         continue;
3126                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3127                         continue;
3128                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
3129                         continue;
3130                       break;
3131                     }
3132                   if (p2)
3133                     break;
3134                 }
3135               if (obs)
3136                 {
3137                   /* found installed package p2 that we can update to p */
3138                   if (MAPTST(&mneg, p))
3139                     continue;
3140                   if (policy_is_illegal(solv, s2, s, 0))
3141                     continue;
3142 #if 0
3143                   if (solver_choicerulecheck(solv, p2, r, &m))
3144                     continue;
3145                   queue_push(&qi, p2);
3146 #else
3147                   queue_push2(&qi, p2, p);
3148 #endif
3149                   queue_push(&q, p);
3150                   continue;
3151                 }
3152             }
3153           /* package p is independent of the installed ones */
3154           havechoice = 1;
3155         }
3156       if (!havechoice || !q.count || !qi.count)
3157         continue;       /* no choice */
3158
3159       FOR_RULELITERALS(p, pp, r)
3160         if (p > 0)
3161           MAPSET(&m, p);
3162
3163       /* do extra checking */
3164       for (i = j = 0; i < qi.count; i += 2)
3165         {
3166           p2 = qi.elements[i];
3167           if (!p2)
3168             continue;
3169           if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
3170             {
3171               /* oops, remove element p from q */
3172               queue_removeelement(&q, qi.elements[i + 1]);
3173               continue;
3174             }
3175           qi.elements[j++] = p2;
3176         }
3177       queue_truncate(&qi, j);
3178
3179       if (!q.count || !qi.count)
3180         {
3181           FOR_RULELITERALS(p, pp, r)
3182             if (p > 0)
3183               MAPCLR(&m, p);
3184           continue;
3185         }
3186
3187
3188       /* now check the update rules of the installed package.
3189        * if all packages of the update rules are contained in
3190        * the dependency rules, there's no need to set up the choice rule */
3191       for (i = 0; i < qi.count; i++)
3192         {
3193           Rule *ur;
3194           if (!qi.elements[i])
3195             continue;
3196           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
3197           if (!ur->p)
3198             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
3199           if (!ur->p)
3200             continue;
3201           FOR_RULELITERALS(p, pp, ur)
3202             if (!MAPTST(&m, p))
3203               break;
3204           if (p)
3205             break;
3206           for (j = i + 1; j < qi.count; j++)
3207             if (qi.elements[i] == qi.elements[j])
3208               qi.elements[j] = 0;
3209         }
3210       /* empty map again */
3211       FOR_RULELITERALS(p, pp, r)
3212         if (p > 0)
3213           MAPCLR(&m, p);
3214       if (i == qi.count)
3215         {
3216 #if 0
3217           printf("skipping choice ");
3218           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3219 #endif
3220           continue;
3221         }
3222
3223       /* don't add identical rules */
3224       if (lastaddedp == r->p && lastaddedcnt == q.count)
3225         {
3226           for (i = 0; i < q.count; i++)
3227             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
3228               break;
3229           if (i == q.count)
3230             continue;   /* already added that one */
3231         }
3232       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
3233
3234       lastaddedp = r->p;
3235       lastaddedd = d;
3236       lastaddedcnt = q.count;
3237
3238       solver_addrule(solv, r->p, 0, d);
3239       queue_push(&solv->weakruleq, solv->nrules - 1);
3240       queue_push(&infoq, rid);
3241 #if 0
3242       printf("OLD ");
3243       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3244       printf("WEAK CHOICE ");
3245       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
3246 #endif
3247     }
3248   if (infoq.count)
3249     solv->choicerules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3250   queue_free(&q);
3251   queue_free(&qi);
3252   queue_free(&qcheck);
3253   queue_free(&infoq);
3254   map_free(&m);
3255   map_free(&mneg);
3256   solv->choicerules_end = solv->nrules;
3257   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
3258 }
3259
3260 /* called when a choice rule needs to be disabled by analyze_unsolvable.
3261  * We also have to disable all other choice rules so that the best packages
3262  * get picked */
3263 void
3264 solver_disablechoicerules(Solver *solv, Rule *r)
3265 {
3266   Id rid, p, pp;
3267   Pool *pool = solv->pool;
3268   Map m;
3269   Rule *or;
3270
3271   solver_disablerule(solv, r);
3272   or = solv->rules + solv->choicerules_info[(r - solv->rules) - solv->choicerules];
3273   map_init(&m, pool->nsolvables);
3274   FOR_RULELITERALS(p, pp, or)
3275     if (p > 0)
3276       MAPSET(&m, p);
3277   FOR_RULELITERALS(p, pp, r)
3278     if (p > 0)
3279       MAPCLR(&m, p);
3280   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
3281     {
3282       r = solv->rules + rid;
3283       if (r->d < 0)
3284         continue;
3285       or = solv->rules + solv->choicerules_info[rid - solv->choicerules];
3286       FOR_RULELITERALS(p, pp, or)
3287         if (p > 0 && MAPTST(&m, p))
3288           break;
3289       if (p)
3290         solver_disablerule(solv, r);
3291     }
3292 }
3293
3294 static void
3295 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
3296 {
3297   int i, j;
3298   Id p, *cp2;
3299   for (i = j = 0; i < q->count; i++)
3300     {
3301       p = q->elements[i];
3302       for (cp2 = cp; *cp2; cp2++)
3303         if (*cp2 == p)
3304           {
3305             q->elements[j++] = p;
3306             break;
3307           }
3308     }
3309   queue_truncate(q, j);
3310 }
3311
3312 static void
3313 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
3314 {
3315   int i, j;
3316   for (i = j = 0; i < q->count; i++)
3317     {
3318       Id p = q->elements[i];
3319       if (MAPTST(&solv->dupmap, p))
3320         q->elements[j++] = p;
3321     }
3322   queue_truncate(q, j);
3323 }
3324
3325 static void
3326 prune_best_update(Solver *solv, Id p, Queue *q)
3327 {
3328   if (solv->update_targets && solv->update_targets->elements[p - solv->installed->start])
3329     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - solv->installed->start], q);
3330   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3331     prune_to_dup_packages(solv, p, q);
3332   /* select best packages, just look at prio and version */
3333   policy_filter_unwanted(solv, q, POLICY_MODE_RECOMMEND);
3334 }
3335
3336 static void
3337 prune_disabled(Pool *pool, Queue *q)
3338 {
3339   int i, j;
3340   for (i = j = 0; i < q->count; i++)
3341     {
3342       Id p = q->elements[i];
3343       Solvable *s = pool->solvables + p;
3344       if (s->repo && s->repo != pool->installed && !MAPTST(pool->considered, p))
3345         continue;
3346       q->elements[j++] = p;
3347     }
3348   queue_truncate(q, j);
3349 }
3350
3351 void
3352 solver_addbestrules(Solver *solv, int havebestinstalljobs, int haslockjob)
3353 {
3354   Pool *pool = solv->pool;
3355   Id p;
3356   Solvable *s;
3357   Repo *installed = solv->installed;
3358   Queue q, q2;
3359   Rule *r;
3360   Queue infoq;
3361   int i, oldcnt;
3362   Map *lockedmap = 0;
3363
3364   solv->bestrules = solv->nrules;
3365   queue_init(&q);
3366   queue_init(&q2);
3367   queue_init(&infoq);
3368
3369   if (haslockjob)
3370     {
3371       int i;
3372       lockedmap = solv_calloc(1, sizeof(Map));
3373       map_init(lockedmap, pool->nsolvables);
3374       for (i = 0, r = solv->rules + solv->jobrules; i < solv->ruletojob.count; i++, r++)
3375         {
3376           if (r->w2 || (solv->job.elements[solv->ruletojob.elements[i]] & SOLVER_JOBMASK) != SOLVER_LOCK)
3377             continue;
3378           p = r->p > 0 ? r->p : -r->p;
3379           MAPSET(lockedmap, p);
3380         }
3381     }
3382   if (havebestinstalljobs)
3383     {
3384       for (i = 0; i < solv->job.count; i += 2)
3385         {
3386           Id how = solv->job.elements[i];
3387           if ((how & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
3388             {
3389               int j, k;
3390               Id p2, pp2;
3391               for (j = 0; j < solv->ruletojob.count; j++)
3392                 {
3393                   if (solv->ruletojob.elements[j] != i)
3394                     continue;
3395                   r = solv->rules + solv->jobrules + j;
3396                   queue_empty(&q);
3397                   queue_empty(&q2);
3398                   FOR_RULELITERALS(p2, pp2, r)
3399                     {
3400                       if (p2 > 0)
3401                         queue_push(&q, p2);
3402                       else if (p2 < 0)
3403                         queue_push(&q2, p2);
3404                     }
3405                   if (pool->considered && pool->whatprovideswithdisabled)
3406                     prune_disabled(pool, &q);
3407                   if (!q.count)
3408                     continue;   /* orphaned */
3409                   /* select best packages, just look at prio and version */
3410                   oldcnt = q.count;
3411                   policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3412                   if (q.count == oldcnt)
3413                     continue;   /* nothing filtered */
3414                   if (lockedmap)
3415                     {
3416                       queue_insertn(&q, 0, q2.count, q2.elements);
3417                       queue_empty(&q2);
3418                       FOR_RULELITERALS(p2, pp2, r)
3419                         {
3420                           if (p2 <= 0)
3421                             continue;
3422                           if (installed && pool->solvables[p2].repo == installed)
3423                             {
3424                               if (MAPTST(lockedmap, p2))
3425                                 queue_pushunique(&q, p2);               /* we always want that package */
3426                             }
3427                           else if (MAPTST(lockedmap, p2))
3428                             continue;
3429                           queue_push(&q2, p2);
3430                         }
3431                       if (pool->considered && pool->whatprovideswithdisabled)
3432                         prune_disabled(pool, &q2);
3433                       policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
3434                       for (k = 0; k < q2.count; k++)
3435                         queue_pushunique(&q, q2.elements[k]);
3436                       queue_empty(&q2);
3437                     }
3438                   if (q2.count)
3439                     queue_insertn(&q, 0, q2.count, q2.elements);
3440                   p2 = queue_shift(&q);
3441                   if (q.count < 2)
3442                     solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3443                   else
3444                     solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3445                   if ((how & SOLVER_WEAK) != 0)
3446                     queue_push(&solv->weakruleq, solv->nrules - 1);
3447                   queue_push(&infoq, -(solv->jobrules + j));
3448                 }
3449             }
3450         }
3451     }
3452   solv->bestrules_up = solv->nrules;
3453
3454   if (installed && (solv->bestupdatemap_all || solv->bestupdatemap.size))
3455     {
3456       Map m;
3457
3458       if (solv->allowuninstall || solv->allowuninstall_all || solv->allowuninstallmap.size)
3459         map_init(&m, pool->nsolvables);
3460       else
3461         map_init(&m, 0);
3462       FOR_REPO_SOLVABLES(installed, p, s)
3463         {
3464           Id d, p2, pp2;
3465           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
3466             continue;
3467           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
3468             continue;
3469           queue_empty(&q);
3470           if (solv->bestobeypolicy)
3471             r = solv->rules + solv->updaterules + (p - installed->start);
3472           else
3473             {
3474               r = solv->rules + solv->featurerules + (p - installed->start);
3475               if (!r->p)        /* identical to update rule? */
3476                 r = solv->rules + solv->updaterules + (p - installed->start);
3477             }
3478           if (solv->specialupdaters && (d = solv->specialupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
3479             {
3480               /* need to check specialupdaters */
3481               if (r->p == p)    /* be careful with the dup case */
3482                 queue_push(&q, p);
3483               while ((p2 = pool->whatprovidesdata[d++]) != 0)
3484                 queue_push(&q, p2);
3485             }
3486           else
3487             {
3488               FOR_RULELITERALS(p2, pp2, r)
3489                 if (p2 > 0)
3490                   queue_push(&q, p2);
3491             }
3492           if (lockedmap)
3493             {
3494               queue_empty(&q2);
3495               queue_insertn(&q2, 0, q.count, q.elements);
3496             }
3497           prune_best_update(solv, p, &q);
3498           if (!q.count)
3499             continue;   /* orphaned */
3500           if (lockedmap)
3501             {
3502               int j;
3503               /* always ok to keep installed locked packages */
3504               if (MAPTST(lockedmap, p))
3505                 queue_pushunique(&q2, p);
3506               for (j = 0; j < q2.count; j++)
3507                 {
3508                   Id p2 = q2.elements[j];
3509                   if (pool->solvables[p2].repo == installed && MAPTST(lockedmap, p2))
3510                     queue_pushunique(&q, p2);
3511                 }
3512               /* filter out locked packages */
3513               for (i = j = 0; j < q2.count; j++)
3514                 {
3515                   Id p2 = q2.elements[j];
3516                   if (pool->solvables[p2].repo == installed || !MAPTST(lockedmap, p2))
3517                     q2.elements[i++] = p2;
3518                 }
3519               queue_truncate(&q2, i);
3520               prune_best_update(solv, p, &q2);
3521               for (j = 0; j < q2.count; j++)
3522                 queue_pushunique(&q, q2.elements[j]);
3523             }
3524           if (solv->bestobeypolicy)
3525             {
3526               /* also filter the best of the feature rule packages and add them */
3527               r = solv->rules + solv->featurerules + (p - installed->start);
3528               if (r->p)
3529                 {
3530                   int j;
3531                   queue_empty(&q2);
3532                   FOR_RULELITERALS(p2, pp2, r)
3533                     if (p2 > 0)
3534                       queue_push(&q2, p2);
3535                   prune_best_update(solv, p, &q2);
3536                   for (j = 0; j < q2.count; j++)
3537                     queue_pushunique(&q, q2.elements[j]);
3538                   if (lockedmap)
3539                     {
3540                       queue_empty(&q2);
3541                       FOR_RULELITERALS(p2, pp2, r)
3542                         if (p2 > 0)
3543                           if (pool->solvables[p2].repo == installed || !MAPTST(lockedmap, p2))
3544                             queue_push(&q2, p2);
3545                       prune_best_update(solv, p, &q2);
3546                       for (j = 0; j < q2.count; j++)
3547                         queue_pushunique(&q, q2.elements[j]);
3548                     }
3549                 }
3550             }
3551           if (solv->allowuninstall || solv->allowuninstall_all || (solv->allowuninstallmap.size && MAPTST(&solv->allowuninstallmap, p - installed->start)))
3552             {
3553               /* package is flagged both for allowuninstall and best, add negative rules */
3554               d = q.count == 1 ? q.elements[0] : -pool_queuetowhatprovides(pool, &q);
3555               for (i = 0; i < q.count; i++)
3556                 MAPSET(&m, q.elements[i]);
3557               r = solv->rules + solv->featurerules + (p - installed->start);
3558               if (!r->p)        /* identical to update rule? */
3559                 r = solv->rules + solv->updaterules + (p - installed->start);
3560               FOR_RULELITERALS(p2, pp2, r)
3561                 {
3562                   if (MAPTST(&m, p2))
3563                     continue;
3564                   if (d >= 0)
3565                     solver_addrule(solv, -p2, d, 0);
3566                   else
3567                     solver_addrule(solv, -p2, 0, -d);
3568                   queue_push(&infoq, p);
3569                 }
3570               for (i = 0; i < q.count; i++)
3571                 MAPCLR(&m, q.elements[i]);
3572               continue;
3573             }
3574           p2 = queue_shift(&q);
3575           if (q.count < 2)
3576             solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3577           else
3578             solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3579           queue_push(&infoq, p);
3580         }
3581       map_free(&m);
3582     }
3583   if (infoq.count)
3584     solv->bestrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3585   solv->bestrules_end = solv->nrules;
3586   queue_free(&q);
3587   queue_free(&q2);
3588   queue_free(&infoq);
3589   if (lockedmap)
3590     {
3591       map_free(lockedmap);
3592       solv_free(lockedmap);
3593     }
3594 }
3595
3596
3597
3598
3599 /* yumobs rule handling */
3600 /* note that we use pool->obsoleteusescolors || pool->implicitobsoleteusescolors
3601  * like in policy_findupdatepackages */
3602
3603 static void
3604 find_obsolete_group(Solver *solv, Id obs, Queue *q)
3605 {
3606   Pool *pool = solv->pool;
3607   Queue qn;
3608   Id p2, pp2, op, *opp, opp2;
3609   int i, j, qnc, ncnt;
3610
3611   queue_empty(q);
3612   FOR_PROVIDES(p2, pp2, obs)
3613     {
3614       Solvable *s2 = pool->solvables + p2;
3615       if (s2->repo != pool->installed)
3616         continue;
3617       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3618         continue;
3619       /* we obsolete installed package s2 with obs. now find all other packages that have the same dep  */
3620       for (opp = solv->obsoletes_data + solv->obsoletes[p2 - solv->installed->start]; (op = *opp++) != 0;)
3621         {
3622           Solvable *os = pool->solvables + op;
3623           Id obs2, *obsp2;
3624           if (!os->obsoletes)
3625             continue;
3626           if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s2, os))
3627             continue;
3628           obsp2 = os->repo->idarraydata + os->obsoletes;
3629           while ((obs2 = *obsp2++) != 0)
3630             if (obs2 == obs)
3631               break;
3632           if (obs2)
3633             queue_pushunique(q, op);
3634         }
3635       /* also search packages with the same name */
3636       FOR_PROVIDES(op, opp2, s2->name)
3637         {
3638           Solvable *os = pool->solvables + op;
3639           Id obs2, *obsp2;
3640           if (os->name != s2->name)
3641             continue;
3642           if (!os->obsoletes)
3643             continue;
3644           if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s2, os))
3645             continue;
3646           obsp2 = os->repo->idarraydata + os->obsoletes;
3647           while ((obs2 = *obsp2++) != 0)
3648             if (obs2 == obs)
3649               break;
3650           if (obs2)
3651             queue_pushunique(q, op);
3652         }
3653     }
3654   /* find names so that we can build groups */
3655   queue_init_clone(&qn, q);
3656   prune_to_best_version(solv->pool, &qn);
3657 #if 0
3658 {
3659   for (i = 0; i < qn.count; i++)
3660     printf(" + %s\n", pool_solvid2str(pool, qn.elements[i]));
3661 }
3662 #endif
3663   /* filter into name groups */
3664   qnc = qn.count;
3665   if (qnc == 1)
3666     {
3667       queue_free(&qn);
3668       queue_empty(q);
3669       return;
3670     }
3671   ncnt = 0;
3672   for (i = 0; i < qnc; i++)
3673     {
3674       Id n = pool->solvables[qn.elements[i]].name;
3675       int got = 0;
3676       for (j = 0; j < q->count; j++)
3677         {
3678           Id p = q->elements[j];
3679           if (pool->solvables[p].name == n)
3680             {
3681               queue_push(&qn, p);
3682               got = 1;
3683             }
3684         }
3685       if (got)
3686         {
3687           queue_push(&qn, 0);
3688           ncnt++;
3689         }
3690     }
3691   if (ncnt <= 1)
3692     {
3693       queue_empty(q);
3694     }
3695   else
3696     {
3697       queue_empty(q);
3698       queue_insertn(q, 0, qn.count - qnc, qn.elements + qnc);
3699     }
3700   queue_free(&qn);
3701 }
3702
3703 void
3704 solver_addyumobsrules(Solver *solv)
3705 {
3706   Pool *pool = solv->pool;
3707   Repo *installed = solv->installed;
3708   Id p, op, *opp;
3709   Solvable *s;
3710   Queue qo, qq, infoq;
3711   int i, j, k;
3712   unsigned int now;
3713
3714   solv->yumobsrules = solv->nrules;
3715   if (!installed || !solv->obsoletes)
3716     {
3717       solv->yumobsrules_end = solv->nrules;
3718       return;
3719     }
3720   now = solv_timems(0);
3721   queue_init(&qo);
3722   FOR_REPO_SOLVABLES(installed, p, s)
3723     {
3724       if (!solv->obsoletes[p - installed->start])
3725         continue;
3726 #if 0
3727 printf("checking yumobs for %s\n", pool_solvable2str(pool, s));
3728 #endif
3729       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (op = *opp++) != 0;)
3730         {
3731           Solvable *os = pool->solvables + op;
3732           Id obs, *obsp = os->repo->idarraydata + os->obsoletes;
3733           Id p2, pp2;
3734           while ((obs = *obsp++) != 0)
3735             {
3736               FOR_PROVIDES(p2, pp2, obs)
3737                 {
3738                   Solvable *s2 = pool->solvables + p2;
3739                   if (s2->repo != installed)
3740                     continue;
3741                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3742                     continue;
3743                   if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s, s2))
3744                     continue;
3745                   queue_pushunique(&qo, obs);
3746                   break;
3747                 }
3748             }
3749         }
3750     }
3751   if (!qo.count)
3752     {
3753       queue_free(&qo);
3754       return;
3755     }
3756   queue_init(&infoq);
3757   queue_init(&qq);
3758   for (i = 0; i < qo.count; i++)
3759     {
3760       int group, groupk, groupstart;
3761       queue_empty(&qq);
3762 #if 0
3763 printf("investigating %s\n", pool_dep2str(pool, qo.elements[i]));
3764 #endif
3765       find_obsolete_group(solv, qo.elements[i], &qq);
3766 #if 0
3767 printf("result:\n");
3768 for (j = 0; j < qq.count; j++)
3769   if (qq.elements[j] == 0)
3770     printf("---\n");
3771   else
3772     printf("%s\n", pool_solvid2str(pool, qq.elements[j]));
3773 #endif
3774
3775       if (!qq.count)
3776         continue;
3777       /* at least two goups, build rules */
3778       group = 0;
3779       for (j = 0; j < qq.count; j++)
3780         {
3781           p = qq.elements[j];
3782           if (!p)
3783             {
3784               group++;
3785               continue;
3786             }
3787           if (pool->solvables[p].repo == installed)
3788             continue;
3789           groupk = 0;
3790           groupstart = 0;
3791           for (k = 0; k < qq.count; k++)
3792             {
3793               Id pk = qq.elements[k];
3794               if (pk)
3795                 continue;
3796               if (group != groupk && k > groupstart)
3797                 {
3798                   /* add the rule */
3799                   if (k - groupstart == 1)
3800                     solver_addrule(solv, -p, qq.elements[groupstart], 0);
3801                   else
3802                     solver_addrule(solv, -p, 0, pool_ids2whatprovides(pool, qq.elements + groupstart, k - groupstart));
3803                   queue_push(&infoq, qo.elements[i]);
3804                 }
3805               groupstart = k + 1;
3806               groupk++;
3807             }
3808         }
3809     }
3810   if (infoq.count)
3811     solv->yumobsrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3812   queue_free(&infoq);
3813   queue_free(&qq);
3814   queue_free(&qo);
3815   solv->yumobsrules_end = solv->nrules;
3816   POOL_DEBUG(SOLV_DEBUG_STATS, "yumobs rule creation took %d ms\n", solv_timems(now));
3817 }
3818
3819 /* recommendsrules are a copy of some recommends package rule but
3820  * with some disfavored literals removed */
3821 void
3822 solver_addrecommendsrules(Solver *solv)
3823 {
3824   Pool *pool = solv->pool;
3825   int i, havedis, havepos;
3826   Id p, pp;
3827   Queue q, infoq;
3828
3829   solv->recommendsrules = solv->nrules;
3830   queue_init(&q);
3831   queue_init(&infoq);
3832   for (i = 0; i < solv->recommendsruleq->count; i++)
3833     {
3834       int rid = solv->recommendsruleq->elements[i];
3835       Rule *r = solv->rules + rid;
3836       queue_empty(&q);
3837       havedis = havepos = 0;
3838       FOR_RULELITERALS(p, pp, r)
3839         {
3840           if (p > 0 && solv->favormap[p] < 0)
3841             havedis = 1;
3842           else
3843             {
3844               if (p > 0)
3845                 havepos = 1;
3846               queue_push(&q, p);
3847             }
3848         }
3849       if (!havedis)
3850         continue;
3851       solver_disablerule(solv, r);
3852       if (!havepos || q.count < 2)
3853         continue;
3854       if (q.count == 2)
3855         solver_addrule(solv, q.elements[0], q.elements[1], 0);
3856       else
3857         solver_addrule(solv, q.elements[0], 0, pool_ids2whatprovides(pool, q.elements + 1, q.count - 1));
3858       queue_push(&infoq, rid);
3859     }
3860   if (infoq.count)
3861     solv->recommendsrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3862   queue_free(&infoq);
3863   queue_free(&q);
3864   solv->recommendsrules_end = solv->nrules;
3865 }
3866
3867 void
3868 solver_breakorphans(Solver *solv)
3869 {
3870   Pool *pool = solv->pool;
3871   Repo *installed = solv->installed;
3872   int i, rid;
3873   Map m;
3874
3875   if (!installed || solv->droporphanedmap_all)
3876     return;
3877   solv->brokenorphanrules = solv_calloc(1, sizeof(Queue));
3878   queue_init(solv->brokenorphanrules);
3879   map_init(&m, installed->end - installed->start);
3880   for (i = 0; i < solv->orphaned.count; i++)
3881     {
3882       Id p = solv->orphaned.elements[i];
3883       if (pool->solvables[p].repo != installed)
3884         continue;
3885       if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - installed->start))
3886         continue;
3887       MAPSET(&m, p - installed->start);
3888     }
3889   for (rid = 1; rid < solv->pkgrules_end ; rid++)
3890     {
3891       Id p, *dp;
3892       Rule *r = solv->rules + rid;
3893       /* ignore non-deps and simple conflicts */
3894       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
3895         continue;
3896       p = -r->p;
3897       if (p < installed->start || p >= installed->end || !MAPTST(&m, p - installed->start))
3898         {
3899           /* need to check other literals */
3900           if (r->d == 0 || r->d == -1)
3901             continue;
3902           for (dp = pool->whatprovidesdata + (r->d < 0 ? -r->d - 1 : r->d); *dp < 0; dp++)
3903             {
3904               p = -*dp;
3905               if (p >= installed->start && p < installed->end && MAPTST(&m, p - installed->start))
3906                 break;
3907             }
3908           if (*dp >= 0)
3909             continue;
3910         }
3911       /* ok, disable this rule */
3912       queue_push(solv->brokenorphanrules, rid);
3913       if (r->d >= 0)
3914         solver_disablerule(solv, r);
3915     }
3916   map_free(&m);
3917   if (!solv->brokenorphanrules->count)
3918     {
3919       queue_free(solv->brokenorphanrules);
3920       solv->brokenorphanrules = solv_free(solv->brokenorphanrules);
3921     }
3922 }
3923
3924 void
3925 solver_check_brokenorphanrules(Solver *solv, Queue *dq)
3926 {
3927   Pool *pool = solv->pool;
3928   int i;
3929   Id l, pp;
3930
3931   queue_empty(dq);
3932   if (!solv->brokenorphanrules)
3933     return;
3934   for (i = 0; i < solv->brokenorphanrules->count; i++)
3935     {
3936       int rid = solv->brokenorphanrules->elements[i];
3937       Rule *r = solv->rules + rid;
3938       FOR_RULELITERALS(l, pp, r)
3939         {
3940           if (l < 0)
3941             {
3942               if (solv->decisionmap[-l] <= 0)
3943                 break;
3944             }
3945           else
3946             {
3947               if (solv->decisionmap[l] > 0 && pool->solvables[l].repo != solv->installed)
3948                 break;
3949             }
3950         }
3951       if (l)
3952         continue;
3953       FOR_RULELITERALS(l, pp, r)
3954         if (l > 0 && solv->decisionmap[l] == 0 && pool->solvables[l].repo != solv->installed)
3955           queue_pushunique(dq, l);
3956     }
3957 }
3958