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