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