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