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