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