6b1432f4fa2eedc34e5f45fc9a5b11fd2cfed3dc
[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  ***  Policy rule disabling/reenabling
2261  ***
2262  ***  Disable all policy rules that conflict with our jobs. If a job
2263  ***  gets disabled later on, reenable the involved policy rules again.
2264  ***
2265  ***/
2266
2267 #define DISABLE_UPDATE  1
2268 #define DISABLE_INFARCH 2
2269 #define DISABLE_DUP     3
2270 #define DISABLE_BLACK   4
2271
2272 static void
2273 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
2274 {
2275   Pool *pool = solv->pool;
2276   Id select, p, pp;
2277   Repo *installed;
2278   Solvable *s;
2279   int i, j, set, qstart;
2280   Map omap;
2281
2282   installed = solv->installed;
2283   select = how & SOLVER_SELECTMASK;
2284   switch (how & SOLVER_JOBMASK)
2285     {
2286     case SOLVER_INSTALL:
2287       set = how & SOLVER_SETMASK;
2288       if (!(set & SOLVER_NOAUTOSET))
2289         {
2290           /* automatically add set bits by analysing the job */
2291           if (select == SOLVER_SOLVABLE_NAME)
2292             set |= SOLVER_SETNAME;
2293           if (select == SOLVER_SOLVABLE)
2294             set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
2295           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
2296             {
2297               Reldep *rd = GETRELDEP(pool, what);
2298               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
2299                 {
2300                   if (pool->disttype != DISTTYPE_DEB)
2301                     {
2302                       const char *rel = strrchr(pool_id2str(pool, rd->evr), '-');
2303                       set |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
2304                     }
2305                   else
2306                     set |= SOLVER_SETEVR;
2307                 }
2308               if (rd->flags <= 7 && ISRELDEP(rd->name))
2309                 rd = GETRELDEP(pool, rd->name);
2310               if (rd->flags == REL_ARCH)
2311                 set |= SOLVER_SETARCH;
2312             }
2313         }
2314       else
2315         set &= ~SOLVER_NOAUTOSET;
2316       if (!set)
2317         return;
2318       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
2319         {
2320           if (select == SOLVER_SOLVABLE)
2321             {
2322               for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2323                 if (solv->rules[i].p == -what)
2324                   break;
2325               if (i < solv->infarchrules_end)
2326                 queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
2327             }
2328           else
2329             {
2330               int qcnt = q->count;
2331               /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
2332                  they are not useful for SOLVER_INSTALL jobs anyway */
2333               FOR_JOB_SELECT(p, pp, select, what)
2334                 {
2335                   s = pool->solvables + p;
2336                   /* unify names */
2337                   for (i = qcnt; i < q->count; i += 2)
2338                     if (q->elements[i + 1] == s->name)
2339                       break;
2340                   if (i < q->count)
2341                     continue;           /* already have that DISABLE_INFARCH element */
2342                   for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2343                     if (solv->rules[i].p == -p)
2344                       break;
2345                   if (i < solv->infarchrules_end)
2346                     queue_push2(q, DISABLE_INFARCH, s->name);
2347                 }
2348             }
2349         }
2350       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
2351         {
2352           if (select == SOLVER_SOLVABLE)
2353             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
2354           else
2355             {
2356               int qcnt = q->count;
2357               FOR_JOB_SELECT(p, pp, select, what)
2358                 {
2359                   s = pool->solvables + p;
2360                   /* unify names */
2361                   for (i = qcnt; i < q->count; i += 2)
2362                     if (q->elements[i + 1] == s->name)
2363                       break;
2364                   if (i < q->count)
2365                     continue;
2366                   queue_push2(q, DISABLE_DUP, s->name);
2367                 }
2368             }
2369         }
2370       if ((set & SOLVER_SETEVR) != 0 && solv->blackrules != solv->blackrules_end)
2371         {
2372           if (select == SOLVER_SOLVABLE)
2373             queue_push2(q, DISABLE_BLACK, what);
2374           else
2375             {
2376               FOR_JOB_SELECT(p, pp, select, what)
2377                 queue_push2(q, DISABLE_BLACK, p);
2378             }
2379         }
2380       if (!installed || installed->end == installed->start)
2381         return;
2382       /* now the hard part: disable some update rules */
2383
2384       /* first check if we have installed or multiversion packages in the job */
2385       FOR_JOB_SELECT(p, pp, select, what)
2386         {
2387           if (pool->solvables[p].repo == installed)
2388             return;
2389           if (solv->multiversion.size && MAPTST(&solv->multiversion, p) && !solv->keepexplicitobsoletes)
2390             return;             /* will not obsolete anything, so just return */
2391         }
2392       omap.size = 0;
2393       qstart = q->count;
2394       FOR_JOB_SELECT(p, pp, select, what)
2395         {
2396           solver_intersect_obsoleted(solv, p, q, qstart, &omap);
2397           if (q->count == qstart)
2398             break;
2399         }
2400       if (omap.size)
2401         map_free(&omap);
2402
2403       if (qstart == q->count)
2404         return;         /* nothing to prune */
2405
2406       /* convert result to (DISABLE_UPDATE, p) pairs */
2407       i = q->count;
2408       for (j = qstart; j < i; j++)
2409         queue_push(q, q->elements[j]);
2410       for (j = qstart; j < q->count; j += 2)
2411         {
2412           q->elements[j] = DISABLE_UPDATE;
2413           q->elements[j + 1] = q->elements[i++];
2414         }
2415
2416       /* now that we know which installed packages are obsoleted check each of them */
2417       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
2418         return;         /* all is set, nothing to do */
2419
2420       for (i = j = qstart; i < q->count; i += 2)
2421         {
2422           Solvable *is = pool->solvables + q->elements[i + 1];
2423           FOR_JOB_SELECT(p, pp, select, what)
2424             {
2425               int illegal = 0;
2426               s = pool->solvables + p;
2427               if ((set & SOLVER_SETEVR) != 0)
2428                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
2429               if ((set & SOLVER_SETNAME) != 0)
2430                 illegal |= POLICY_ILLEGAL_NAMECHANGE;   /* ignore */
2431               if ((set & SOLVER_SETARCH) != 0)
2432                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
2433               if ((set & SOLVER_SETVENDOR) != 0)
2434                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
2435               illegal = policy_is_illegal(solv, is, s, illegal);
2436               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
2437                 {
2438                   /* it's ok if the EV is different */
2439                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
2440                     illegal = 0;
2441                 }
2442               if (illegal)
2443                 break;
2444             }
2445           if (!p)
2446             {   
2447               /* no package conflicts with the update rule */
2448               /* thus keep the DISABLE_UPDATE */
2449               q->elements[j + 1] = q->elements[i + 1];
2450               j += 2;
2451             }
2452         }
2453       queue_truncate(q, j);
2454       return;
2455
2456     case SOLVER_ERASE:
2457       if (!installed)
2458         break;
2459       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2460         {
2461           FOR_REPO_SOLVABLES(installed, p, s)
2462             queue_push2(q, DISABLE_UPDATE, p);
2463         }
2464       FOR_JOB_SELECT(p, pp, select, what)
2465         if (pool->solvables[p].repo == installed)
2466           {
2467             queue_push2(q, DISABLE_UPDATE, p);
2468 #ifdef ENABLE_LINKED_PKGS
2469             if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
2470               queue_push2(q, DISABLE_UPDATE, solv->instbuddy[p - installed->start]);
2471 #endif
2472           }
2473       return;
2474
2475     case SOLVER_LOCK:
2476       if (!installed)
2477         break;
2478       qstart = q->count;
2479       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2480         {
2481           FOR_REPO_SOLVABLES(installed, p, s)
2482             {
2483               for (i = qstart; i < q->count; i += 2)
2484                 if (q->elements[i] == DISABLE_DUP && q->elements[i + 1] == pool->solvables[p].name)
2485                   break;
2486               if (i == q->count)
2487                 queue_push2(q, DISABLE_DUP, pool->solvables[p].name);
2488             }
2489         }
2490       FOR_JOB_SELECT(p, pp, select, what)
2491         {
2492           if (pool->solvables[p].repo != installed)
2493             continue;
2494           for (i = qstart; i < q->count; i += 2)
2495             if (q->elements[i] == DISABLE_DUP && q->elements[i + 1] == pool->solvables[p].name)
2496               break;
2497           if (i == q->count)
2498             queue_push2(q, DISABLE_DUP, pool->solvables[p].name);
2499         }
2500       break;
2501
2502     default:
2503       return;
2504     }
2505 }
2506
2507 /* disable all policy rules that are in conflict with our job list */
2508 void
2509 solver_disablepolicyrules(Solver *solv)
2510 {
2511   Queue *job = &solv->job;
2512   int i, j;
2513   Queue allq;
2514   Rule *r;
2515   Id lastjob = -1;
2516   Id allqbuf[128];
2517
2518   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2519
2520   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2521     {
2522       r = solv->rules + i;
2523       if (r->d < 0)     /* disabled? */
2524         continue;
2525       j = solv->ruletojob.elements[i - solv->jobrules];
2526       if (j == lastjob)
2527         continue;
2528       lastjob = j;
2529       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2530     }
2531   if (solv->cleandepsmap.size)
2532     {
2533       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2534       for (i = solv->installed->start; i < solv->installed->end; i++)
2535         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
2536           queue_push2(&allq, DISABLE_UPDATE, i);
2537     }
2538   MAPZERO(&solv->noupdate);
2539   for (i = 0; i < allq.count; i += 2)
2540     {
2541       Id type = allq.elements[i], arg = allq.elements[i + 1];
2542       switch(type)
2543         {
2544         case DISABLE_UPDATE:
2545           disableupdaterule(solv, arg);
2546           break;
2547         case DISABLE_INFARCH:
2548           disableinfarchrule(solv, arg);
2549           break;
2550         case DISABLE_DUP:
2551           disableduprule(solv, arg);
2552           break;
2553         case DISABLE_BLACK:
2554           disableblackrule(solv, arg);
2555           break;
2556         default:
2557           break;
2558         }
2559     }
2560   queue_free(&allq);
2561 }
2562
2563 /* we just disabled job #jobidx, now reenable all policy rules that were
2564  * disabled because of this job */
2565 void
2566 solver_reenablepolicyrules(Solver *solv, int jobidx)
2567 {
2568   Queue *job = &solv->job;
2569   int i, j, k, ai;
2570   Queue q, allq;
2571   Rule *r;
2572   Id lastjob = -1;
2573   Id qbuf[32], allqbuf[32];
2574
2575   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2576   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
2577   if (!q.count)
2578     {
2579       queue_free(&q);
2580       return;
2581     }
2582   /* now remove everything from q that is disabled by other jobs */
2583
2584   /* first remove cleandeps packages, they count as DISABLE_UPDATE */
2585   if (solv->cleandepsmap.size)
2586     {
2587       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2588       for (j = k = 0; j < q.count; j += 2)
2589         {
2590           if (q.elements[j] == DISABLE_UPDATE)
2591             {
2592               Id p = q.elements[j + 1];
2593               if (p >= solv->installed->start && p < solv->installed->end && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2594                 continue;       /* remove element from q */
2595             }
2596           q.elements[k++] = q.elements[j];
2597           q.elements[k++] = q.elements[j + 1];
2598         }
2599       q.count = k;
2600       if (!q.count)
2601         {
2602           queue_free(&q);
2603           return;
2604         }
2605     }
2606
2607   /* now go through the disable list of all other jobs */
2608   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2609   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2610     {
2611       r = solv->rules + i;
2612       if (r->d < 0)     /* disabled? */
2613         continue;
2614       j = solv->ruletojob.elements[i - solv->jobrules];
2615       if (j == lastjob)
2616         continue;
2617       lastjob = j;
2618       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2619       if (!allq.count)
2620         continue;
2621       /* remove all elements in allq from q */
2622       for (j = k = 0; j < q.count; j += 2)
2623         {
2624           Id type = q.elements[j], arg = q.elements[j + 1];
2625           for (ai = 0; ai < allq.count; ai += 2)
2626             if (allq.elements[ai] == type && allq.elements[ai + 1] == arg)
2627               break;
2628           if (ai < allq.count)
2629             continue;   /* found it in allq, remove element from q */
2630           q.elements[k++] = q.elements[j];
2631           q.elements[k++] = q.elements[j + 1];
2632         }
2633       q.count = k;
2634       if (!q.count)
2635         {
2636           queue_free(&q);
2637           queue_free(&allq);
2638           return;
2639         }
2640       queue_empty(&allq);
2641     }
2642   queue_free(&allq);
2643
2644   /* now re-enable anything that's left in q */
2645   for (j = 0; j < q.count; j += 2)
2646     {
2647       Id type = q.elements[j], arg = q.elements[j + 1];
2648       switch(type)
2649         {
2650         case DISABLE_UPDATE:
2651           reenableupdaterule(solv, arg);
2652           break;
2653         case DISABLE_INFARCH:
2654           reenableinfarchrule(solv, arg);
2655           break;
2656         case DISABLE_DUP:
2657           reenableduprule(solv, arg);
2658           break;
2659         case DISABLE_BLACK:
2660           reenableblackrule(solv, arg);
2661           break;
2662         }
2663     }
2664   queue_free(&q);
2665 }
2666
2667 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
2668  * disabled because of this */
2669 void
2670 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
2671 {
2672   Queue *job = &solv->job;
2673   int i, j;
2674   Queue allq;
2675   Rule *r;
2676   Id lastjob = -1;
2677   Id allqbuf[128];
2678
2679   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2680   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2681     {
2682       r = solv->rules + i;
2683       if (r->d < 0)     /* disabled? */
2684         continue;
2685       j = solv->ruletojob.elements[i - solv->jobrules];
2686       if (j == lastjob)
2687         continue;
2688       lastjob = j;
2689       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2690     }
2691   for (i = 0; i < allq.count; i += 2)
2692     if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
2693       break;
2694   if (i == allq.count)
2695     reenableupdaterule(solv, pkg);
2696   queue_free(&allq);
2697 }
2698
2699
2700 /***********************************************************************
2701  ***
2702  ***  Rule info part, tell the user what the rule is about.
2703  ***
2704  ***/
2705
2706 static void
2707 addpkgruleinfo(Solver *solv, Id p, Id p2, Id d, int type, Id dep)
2708 {
2709   Pool *pool = solv->pool;
2710   Rule *r;
2711
2712   if (d)
2713     {
2714       assert(!p2 && d > 0);
2715       if (!pool->whatprovidesdata[d])
2716         d = 0;
2717       else if (!pool->whatprovidesdata[d + 1])
2718         {
2719           p2 = pool->whatprovidesdata[d];
2720           d = 0;
2721         }
2722     }
2723
2724   /* check if this creates the rule we're searching for */
2725   r = solv->rules + solv->ruleinfoq->elements[0];
2726   if (d)
2727     {
2728       /* three or more literals */
2729       Id od = r->d < 0 ? -r->d - 1 : r->d;
2730       if (p != r->p && !od)
2731         return;
2732       if (d != od)
2733         {
2734           Id *dp = pool->whatprovidesdata + d;
2735           Id *odp = pool->whatprovidesdata + od;
2736           while (*dp)
2737             if (*dp++ != *odp++)
2738               return;
2739           if (*odp)
2740             return;
2741         }
2742       /* set p2 for multiversion conflicts */
2743       if (p < 0 && pool->whatprovidesdata[d] < 0 && pool->whatprovidesdata[d + 1] >= 0 && type == SOLVER_RULE_PKG_CONFLICTS)
2744         p2 = pool->whatprovidesdata[d];
2745     }
2746   else
2747     {
2748       /* one or two literals */
2749       Id op = p, op2 = p2;
2750       if (op2 && op > op2)      /* normalize */
2751         {
2752           Id o = op;
2753           op = op2;
2754           op2 = o;
2755         }
2756       if (r->p != op || r->w2 != op2 || (r->d && r->d != -1))
2757         return;
2758       if (type == SOLVER_RULE_PKG_CONFLICTS && !p2)
2759         p2 = -SYSTEMSOLVABLE;
2760       if (type == SOLVER_RULE_PKG_SAME_NAME)
2761         {
2762           p = op;       /* we normalize same name order */
2763           p2 = op2;
2764         }
2765     }
2766   /* yep, rule matches. record info */
2767   queue_push(solv->ruleinfoq, type);
2768   queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2769   queue_push(solv->ruleinfoq, p2 < 0 ? -p2 : 0);
2770   queue_push(solv->ruleinfoq, dep);
2771 }
2772
2773 static int
2774 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2775 {
2776   const Id *a = ap, *b = bp;
2777   int r;
2778
2779   r = a[0] - b[0];
2780   if (r)
2781     return r;
2782   r = a[1] - b[1];
2783   if (r)
2784     return r;
2785   r = a[2] - b[2];
2786   if (r)
2787     return r;
2788   r = a[3] - b[3];
2789   if (r)
2790     return r;
2791   return 0;
2792 }
2793
2794 static void
2795 getpkgruleinfos(Solver *solv, Rule *r, Queue *rq)
2796 {
2797   Pool *pool = solv->pool;
2798   Id l, pp;
2799   if (r->p >= 0)
2800     return;
2801   queue_push(rq, r - solv->rules);      /* push the rule we're interested in */
2802   solv->ruleinfoq = rq;
2803   FOR_RULELITERALS(l, pp, r)
2804     {
2805       if (l >= 0)
2806         break;
2807       solver_addpkgrulesforsolvable(solv, pool->solvables - l, 0);
2808     }
2809 #ifdef ENABLE_LINKED_PKGS
2810   FOR_RULELITERALS(l, pp, r)
2811     {
2812       if (l < 0)
2813         {
2814           if (l == r->p)
2815             continue;
2816           break;
2817         }
2818       if (!strchr(pool_id2str(pool, pool->solvables[l].name), ':') || !has_package_link(pool, pool->solvables + l))
2819         break;
2820       add_package_link(solv, pool->solvables + l, 0, 0);
2821     }
2822 #endif
2823   solv->ruleinfoq = 0;
2824   queue_shift(rq);
2825 }
2826
2827 int
2828 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2829 {
2830   Rule *r = solv->rules + rid;
2831   int i, j;
2832
2833   queue_empty(rq);
2834   if (rid <= 0 || rid >= solv->pkgrules_end)
2835     {
2836       Id type, from, to, dep;
2837       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2838       queue_push(rq, type);
2839       queue_push(rq, from);
2840       queue_push(rq, to);
2841       queue_push(rq, dep);
2842       return 1;
2843     }
2844   getpkgruleinfos(solv, r, rq);
2845   /* now sort & unify em */
2846   if (!rq->count)
2847     return 0;
2848   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2849   /* throw out identical entries */
2850   for (i = j = 0; i < rq->count; i += 4)
2851     {
2852       if (j)
2853         {
2854           if (rq->elements[i] == rq->elements[j - 4] &&
2855               rq->elements[i + 1] == rq->elements[j - 3] &&
2856               rq->elements[i + 2] == rq->elements[j - 2] &&
2857               rq->elements[i + 3] == rq->elements[j - 1])
2858             continue;
2859         }
2860       rq->elements[j++] = rq->elements[i];
2861       rq->elements[j++] = rq->elements[i + 1];
2862       rq->elements[j++] = rq->elements[i + 2];
2863       rq->elements[j++] = rq->elements[i + 3];
2864     }
2865   rq->count = j;
2866   return j / 4;
2867 }
2868
2869 SolverRuleinfo
2870 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2871 {
2872   Pool *pool = solv->pool;
2873   Rule *r = solv->rules + rid;
2874   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2875
2876   if (fromp)
2877     *fromp = 0;
2878   if (top)
2879     *top = 0;
2880   if (depp)
2881     *depp = 0;
2882   if (rid > 0 && rid < solv->pkgrules_end)
2883     {
2884       Queue rq;
2885       int i;
2886
2887       if (r->p >= 0)
2888         return SOLVER_RULE_PKG;
2889       if (fromp)
2890         *fromp = -r->p;
2891       queue_init(&rq);
2892       getpkgruleinfos(solv, r, &rq);
2893       type = SOLVER_RULE_PKG;
2894       for (i = 0; i < rq.count; i += 4)
2895         {
2896           Id qt, qo, qp, qd;
2897           qt = rq.elements[i];
2898           qp = rq.elements[i + 1];
2899           qo = rq.elements[i + 2];
2900           qd = rq.elements[i + 3];
2901           if (type == SOLVER_RULE_PKG || type > qt)
2902             {
2903               type = qt;
2904               if (fromp)
2905                 *fromp = qp;
2906               if (top)
2907                 *top = qo;
2908               if (depp)
2909                 *depp = qd;
2910             }
2911         }
2912       queue_free(&rq);
2913       return type;
2914     }
2915   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2916     {
2917       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2918       if (fromp)
2919         *fromp = jidx;
2920       if (top)
2921         *top = solv->job.elements[jidx];
2922       if (depp)
2923         *depp = solv->job.elements[jidx + 1];
2924       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2925         {
2926           Id how = solv->job.elements[jidx];
2927           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2928             return SOLVER_RULE_JOB_UNKNOWN_PACKAGE;
2929           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2930             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2931           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2932             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2933           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2934             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2935           return SOLVER_RULE_JOB_UNSUPPORTED;
2936         }
2937       return SOLVER_RULE_JOB;
2938     }
2939   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2940     {
2941       if (fromp)
2942         *fromp = solv->installed->start + (rid - solv->updaterules);
2943       return SOLVER_RULE_UPDATE;
2944     }
2945   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2946     {
2947       if (fromp)
2948         *fromp = solv->installed->start + (rid - solv->featurerules);
2949       return SOLVER_RULE_FEATURE;
2950     }
2951   if (rid >= solv->duprules && rid < solv->duprules_end)
2952     {
2953       if (fromp)
2954         *fromp = -r->p;
2955       if (depp)
2956         *depp = pool->solvables[-r->p].name;
2957       return SOLVER_RULE_DISTUPGRADE;
2958     }
2959   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2960     {
2961       if (fromp)
2962         *fromp = -r->p;
2963       if (depp)
2964         *depp = pool->solvables[-r->p].name;
2965       return SOLVER_RULE_INFARCH;
2966     }
2967   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2968     {
2969       if (fromp && solv->bestrules_info[rid - solv->bestrules] > 0)
2970         *fromp = solv->bestrules_info[rid - solv->bestrules];
2971       return SOLVER_RULE_BEST;
2972     }
2973   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
2974     {
2975       if (fromp)
2976         *fromp = -r->p;
2977       if (top)
2978         {
2979           /* first solvable is enough, we just need it for the name */
2980           if (!r->d || r->d == -1)
2981             *top = r->w2;
2982           else
2983             *top = pool->whatprovidesdata[r->d < 0 ? -r->d : r->d];
2984         }
2985       if (depp)
2986         *depp = solv->yumobsrules_info[rid - solv->yumobsrules];
2987       return SOLVER_RULE_YUMOBS;
2988     }
2989   if (rid >= solv->blackrules && rid < solv->blackrules_end)
2990     {
2991       if (fromp)
2992         *fromp = -r->p;
2993       return SOLVER_RULE_BLACK;
2994     }
2995   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2996     return SOLVER_RULE_CHOICE;
2997   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
2998     return SOLVER_RULE_RECOMMENDS;
2999   if (rid >= solv->learntrules)
3000     return SOLVER_RULE_LEARNT;
3001   return SOLVER_RULE_UNKNOWN;
3002 }
3003
3004 SolverRuleinfo
3005 solver_ruleclass(Solver *solv, Id rid)
3006 {
3007   if (rid <= 0)
3008     return SOLVER_RULE_UNKNOWN;
3009   if (rid > 0 && rid < solv->pkgrules_end)
3010     return SOLVER_RULE_PKG;
3011   if (rid >= solv->jobrules && rid < solv->jobrules_end)
3012     return SOLVER_RULE_JOB;
3013   if (rid >= solv->updaterules && rid < solv->updaterules_end)
3014     return SOLVER_RULE_UPDATE;
3015   if (rid >= solv->featurerules && rid < solv->featurerules_end)
3016     return SOLVER_RULE_FEATURE;
3017   if (rid >= solv->duprules && rid < solv->duprules_end)
3018     return SOLVER_RULE_DISTUPGRADE;
3019   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
3020     return SOLVER_RULE_INFARCH;
3021   if (rid >= solv->bestrules && rid < solv->bestrules_end)
3022     return SOLVER_RULE_BEST;
3023   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
3024     return SOLVER_RULE_YUMOBS;
3025   if (rid >= solv->blackrules && rid < solv->blackrules_end)
3026     return SOLVER_RULE_BLACK;
3027   if (rid >= solv->choicerules && rid < solv->choicerules_end)
3028     return SOLVER_RULE_CHOICE;
3029   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
3030     return SOLVER_RULE_RECOMMENDS;
3031   if (rid >= solv->blackrules && rid < solv->blackrules_end)
3032     return SOLVER_RULE_BLACK;
3033   if (rid >= solv->learntrules && rid < solv->nrules)
3034     return SOLVER_RULE_LEARNT;
3035   return SOLVER_RULE_UNKNOWN;
3036 }
3037
3038 void
3039 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
3040 {
3041   Pool *pool = solv->pool;
3042   Id p, pp;
3043   Rule *r;
3044
3045   queue_empty(q);
3046   r = solv->rules + rid;
3047   FOR_RULELITERALS(p, pp, r)
3048     if (p != -SYSTEMSOLVABLE)
3049       queue_push(q, p);
3050   if (!q->count)
3051     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
3052 }
3053
3054 int
3055 solver_rule2jobidx(Solver *solv, Id rid)
3056 {
3057   if (rid < solv->jobrules || rid >= solv->jobrules_end)
3058     return 0;
3059   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
3060 }
3061
3062 /* job rule introspection */
3063 Id
3064 solver_rule2job(Solver *solv, Id rid, Id *whatp)
3065 {
3066   int idx;
3067   if (rid < solv->jobrules || rid >= solv->jobrules_end)
3068     {
3069       if (whatp)
3070         *whatp = 0;
3071       return 0;
3072     }
3073   idx = solv->ruletojob.elements[rid - solv->jobrules];
3074   if (whatp)
3075     *whatp = solv->job.elements[idx + 1];
3076   return solv->job.elements[idx];
3077 }
3078
3079 /* update/feature rule introspection */
3080 Id
3081 solver_rule2solvable(Solver *solv, Id rid)
3082 {
3083   if (rid >= solv->updaterules && rid < solv->updaterules_end)
3084     return rid - solv->updaterules;
3085   if (rid >= solv->featurerules && rid < solv->featurerules_end)
3086     return rid - solv->featurerules;
3087   return 0;
3088 }
3089
3090 Id
3091 solver_rule2pkgrule(Solver *solv, Id rid)
3092 {
3093   if (rid >= solv->choicerules && rid < solv->choicerules_end)
3094     return solv->choicerules_info[rid - solv->choicerules];
3095   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
3096     return solv->recommendsrules_info[rid - solv->recommendsrules];
3097   return 0;
3098 }
3099
3100 static void
3101 solver_rule2rules_rec(Solver *solv, Id rid, Queue *q, Map *seen)
3102 {
3103   int i;
3104   Id rid2;
3105
3106   if (seen)
3107     MAPSET(seen, rid);
3108   for (i = solv->learnt_why.elements[rid - solv->learntrules]; (rid2 = solv->learnt_pool.elements[i]) != 0; i++)
3109     {
3110       if (seen)
3111         {
3112           if (MAPTST(seen, rid2))
3113             continue;
3114           if (rid2 >= solv->learntrules)
3115             solver_rule2rules_rec(solv, rid2, q, seen);
3116           continue;
3117         }
3118       queue_push(q, rid2);
3119     }
3120 }
3121
3122 /* learnt rule introspection */
3123 void
3124 solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
3125 {
3126   queue_empty(q);
3127   if (rid < solv->learntrules || rid >= solv->nrules)
3128     return;
3129   if (recursive)
3130     {
3131       Map seen;
3132       map_init(&seen, solv->nrules);
3133       solver_rule2rules_rec(solv, rid, q, &seen);
3134       map_free(&seen);
3135     }
3136   else
3137     solver_rule2rules_rec(solv, rid, q, 0);
3138 }
3139
3140
3141 /* check if the newest versions of pi still provides the dependency we're looking for */
3142 static int
3143 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
3144 {
3145   Pool *pool = solv->pool;
3146   Rule *ur;
3147   Id p, pp;
3148   int i;
3149
3150   if (!q->count || q->elements[0] != pi)
3151     {
3152       if (q->count)
3153         queue_empty(q);
3154       ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
3155       if (!ur->p)
3156         ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
3157       if (!ur->p)
3158         return 0;
3159       queue_push2(q, pi, 0);
3160       FOR_RULELITERALS(p, pp, ur)
3161         if (p > 0)
3162           queue_push(q, p);
3163     }
3164   if (q->count == 2)
3165     return 1;
3166   if (q->count == 3)
3167     {
3168       p = q->elements[2];
3169       return MAPTST(m, p) ? 0 : 1;
3170     }
3171   if (!q->elements[1])
3172     {
3173       for (i = 2; i < q->count; i++)
3174         if (!MAPTST(m, q->elements[i]))
3175           break;
3176       if (i == q->count)
3177         return 0;       /* all provide it, no need to filter */
3178       /* some don't provide it, have to filter */
3179       queue_deleten(q, 0, 2);
3180       policy_filter_unwanted(solv, q, POLICY_MODE_CHOOSE);
3181       queue_unshift(q, 1);      /* filter mark */
3182       queue_unshift(q, pi);
3183     }
3184   for (i = 2; i < q->count; i++)
3185     if (MAPTST(m, q->elements[i]))
3186       return 0;         /* at least one provides it */
3187   return 1;     /* none of the new packages provided it */
3188 }
3189
3190 static inline void
3191 queue_removeelement(Queue *q, Id el)
3192 {
3193   int i, j;
3194   for (i = 0; i < q->count; i++)
3195     if (q->elements[i] == el)
3196       break;
3197   if (i < q->count)
3198     {
3199       for (j = i++; i < q->count; i++)
3200         if (q->elements[i] != el)
3201           q->elements[j++] = q->elements[i];
3202       queue_truncate(q, j);
3203     }
3204 }
3205
3206 void
3207 solver_addchoicerules(Solver *solv)
3208 {
3209   Pool *pool = solv->pool;
3210   Map m, mneg;
3211   Rule *r;
3212   Queue q, qi, qcheck, infoq;
3213   int i, j, rid, havechoice;
3214   Id p, d, pp;
3215   Id p2, pp2;
3216   Solvable *s, *s2;
3217   Id lastaddedp, lastaddedd;
3218   int lastaddedcnt;
3219   unsigned int now;
3220
3221   solv->choicerules = solv->nrules;
3222   if (!pool->installed)
3223     {
3224       solv->choicerules_end = solv->nrules;
3225       return;
3226     }
3227   now = solv_timems(0);
3228   solv->choicerules_info = solv_calloc(solv->pkgrules_end, sizeof(Id));
3229   queue_init(&q);
3230   queue_init(&qi);
3231   queue_init(&qcheck);
3232   queue_init(&infoq);
3233   map_init(&m, pool->nsolvables);
3234   map_init(&mneg, pool->nsolvables);
3235   /* set up negative assertion map from infarch and dup rules */
3236   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
3237     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3238       MAPSET(&mneg, -r->p);
3239   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
3240     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3241       MAPSET(&mneg, -r->p);
3242   lastaddedp = 0;
3243   lastaddedd = 0;
3244   lastaddedcnt = 0;
3245   for (rid = 1; rid < solv->pkgrules_end ; rid++)
3246     {
3247       r = solv->rules + rid;
3248       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
3249         continue;       /* only look at requires rules */
3250       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
3251       queue_empty(&q);
3252       queue_empty(&qi);
3253       havechoice = 0;
3254       FOR_RULELITERALS(p, pp, r)
3255         {
3256           if (p < 0)
3257             continue;
3258           s = pool->solvables + p;
3259           if (!s->repo)
3260             continue;
3261           if (s->repo == pool->installed)
3262             {
3263               queue_push(&q, p);
3264               continue;
3265             }
3266           /* check if this package is "blocked" by a installed package */
3267           s2 = 0;
3268           FOR_PROVIDES(p2, pp2, s->name)
3269             {
3270               s2 = pool->solvables + p2;
3271               if (s2->repo != pool->installed)
3272                 continue;
3273               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
3274                 continue;
3275               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
3276                 continue;
3277               break;
3278             }
3279           if (p2)
3280             {
3281               /* found installed package p2 that we can update to p */
3282               if (MAPTST(&mneg, p))
3283                 continue;
3284               if (policy_is_illegal(solv, s2, s, 0))
3285                 continue;
3286 #if 0
3287               if (solver_choicerulecheck(solv, p2, r, &m))
3288                 continue;
3289               queue_push(&qi, p2);
3290 #else
3291               queue_push2(&qi, p2, p);
3292 #endif
3293               queue_push(&q, p);
3294               continue;
3295             }
3296           if (s->obsoletes)
3297             {
3298               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3299               s2 = 0;
3300               while ((obs = *obsp++) != 0)
3301                 {
3302                   FOR_PROVIDES(p2, pp2, obs)
3303                     {
3304                       s2 = pool->solvables + p2;
3305                       if (s2->repo != pool->installed)
3306                         continue;
3307                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3308                         continue;
3309                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
3310                         continue;
3311                       break;
3312                     }
3313                   if (p2)
3314                     break;
3315                 }
3316               if (obs)
3317                 {
3318                   /* found installed package p2 that we can update to p */
3319                   if (MAPTST(&mneg, p))
3320                     continue;
3321                   if (policy_is_illegal(solv, s2, s, 0))
3322                     continue;
3323 #if 0
3324                   if (solver_choicerulecheck(solv, p2, r, &m))
3325                     continue;
3326                   queue_push(&qi, p2);
3327 #else
3328                   queue_push2(&qi, p2, p);
3329 #endif
3330                   queue_push(&q, p);
3331                   continue;
3332                 }
3333             }
3334           /* package p is independent of the installed ones */
3335           havechoice = 1;
3336         }
3337       if (!havechoice || !q.count || !qi.count)
3338         continue;       /* no choice */
3339
3340       FOR_RULELITERALS(p, pp, r)
3341         if (p > 0)
3342           MAPSET(&m, p);
3343
3344       /* do extra checking */
3345       for (i = j = 0; i < qi.count; i += 2)
3346         {
3347           p2 = qi.elements[i];
3348           if (!p2)
3349             continue;
3350           if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
3351             {
3352               /* oops, remove element p from q */
3353               queue_removeelement(&q, qi.elements[i + 1]);
3354               continue;
3355             }
3356           qi.elements[j++] = p2;
3357         }
3358       queue_truncate(&qi, j);
3359
3360       if (!q.count || !qi.count)
3361         {
3362           FOR_RULELITERALS(p, pp, r)
3363             if (p > 0)
3364               MAPCLR(&m, p);
3365           continue;
3366         }
3367
3368
3369       /* now check the update rules of the installed package.
3370        * if all packages of the update rules are contained in
3371        * the dependency rules, there's no need to set up the choice rule */
3372       for (i = 0; i < qi.count; i++)
3373         {
3374           Rule *ur;
3375           if (!qi.elements[i])
3376             continue;
3377           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
3378           if (!ur->p)
3379             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
3380           if (!ur->p)
3381             continue;
3382           FOR_RULELITERALS(p, pp, ur)
3383             if (!MAPTST(&m, p))
3384               break;
3385           if (p)
3386             break;
3387           for (j = i + 1; j < qi.count; j++)
3388             if (qi.elements[i] == qi.elements[j])
3389               qi.elements[j] = 0;
3390         }
3391       /* empty map again */
3392       FOR_RULELITERALS(p, pp, r)
3393         if (p > 0)
3394           MAPCLR(&m, p);
3395       if (i == qi.count)
3396         {
3397 #if 0
3398           printf("skipping choice ");
3399           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3400 #endif
3401           continue;
3402         }
3403
3404       /* don't add identical rules */
3405       if (lastaddedp == r->p && lastaddedcnt == q.count)
3406         {
3407           for (i = 0; i < q.count; i++)
3408             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
3409               break;
3410           if (i == q.count)
3411             continue;   /* already added that one */
3412         }
3413       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
3414
3415       lastaddedp = r->p;
3416       lastaddedd = d;
3417       lastaddedcnt = q.count;
3418
3419       solver_addrule(solv, r->p, 0, d);
3420       queue_push(&solv->weakruleq, solv->nrules - 1);
3421       queue_push(&infoq, rid);
3422 #if 0
3423       printf("OLD ");
3424       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3425       printf("WEAK CHOICE ");
3426       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
3427 #endif
3428     }
3429   if (infoq.count)
3430     solv->choicerules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3431   queue_free(&q);
3432   queue_free(&qi);
3433   queue_free(&qcheck);
3434   queue_free(&infoq);
3435   map_free(&m);
3436   map_free(&mneg);
3437   solv->choicerules_end = solv->nrules;
3438   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
3439 }
3440
3441 /* called when a choice rule needs to be disabled by analyze_unsolvable.
3442  * We also have to disable all other choice rules so that the best packages
3443  * get picked */
3444 void
3445 solver_disablechoicerules(Solver *solv, Rule *r)
3446 {
3447   Id rid, p, pp;
3448   Pool *pool = solv->pool;
3449   Map m;
3450   Rule *or;
3451
3452   solver_disablerule(solv, r);
3453   or = solv->rules + solv->choicerules_info[(r - solv->rules) - solv->choicerules];
3454   map_init(&m, pool->nsolvables);
3455   FOR_RULELITERALS(p, pp, or)
3456     if (p > 0)
3457       MAPSET(&m, p);
3458   FOR_RULELITERALS(p, pp, r)
3459     if (p > 0)
3460       MAPCLR(&m, p);
3461   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
3462     {
3463       r = solv->rules + rid;
3464       if (r->d < 0)
3465         continue;
3466       or = solv->rules + solv->choicerules_info[rid - solv->choicerules];
3467       FOR_RULELITERALS(p, pp, or)
3468         if (p > 0 && MAPTST(&m, p))
3469           break;
3470       if (p)
3471         solver_disablerule(solv, r);
3472     }
3473 }
3474
3475 static void
3476 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
3477 {
3478   int i, j;
3479   Id p, *cp2;
3480   for (i = j = 0; i < q->count; i++)
3481     {
3482       p = q->elements[i];
3483       for (cp2 = cp; *cp2; cp2++)
3484         if (*cp2 == p)
3485           {
3486             q->elements[j++] = p;
3487             break;
3488           }
3489     }
3490   queue_truncate(q, j);
3491 }
3492
3493 static void
3494 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
3495 {
3496   int i, j;
3497   for (i = j = 0; i < q->count; i++)
3498     {
3499       Id p = q->elements[i];
3500       if (MAPTST(&solv->dupmap, p))
3501         q->elements[j++] = p;
3502     }
3503   queue_truncate(q, j);
3504 }
3505
3506 static void
3507 prune_best_update(Solver *solv, Id p, Queue *q)
3508 {
3509   if (solv->update_targets && solv->update_targets->elements[p - solv->installed->start])
3510     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - solv->installed->start], q);
3511   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3512     prune_to_dup_packages(solv, p, q);
3513   /* select best packages, just look at prio and version */
3514   policy_filter_unwanted(solv, q, POLICY_MODE_RECOMMEND);
3515 }
3516
3517 static void
3518 prune_disabled(Pool *pool, Queue *q)
3519 {
3520   int i, j;
3521   for (i = j = 0; i < q->count; i++)
3522     {
3523       Id p = q->elements[i];
3524       Solvable *s = pool->solvables + p;
3525       if (s->repo && s->repo != pool->installed && !MAPTST(pool->considered, p))
3526         continue;
3527       q->elements[j++] = p;
3528     }
3529   queue_truncate(q, j);
3530 }
3531
3532 void
3533 solver_addbestrules(Solver *solv, int havebestinstalljobs, int haslockjob)
3534 {
3535   Pool *pool = solv->pool;
3536   Id p;
3537   Solvable *s;
3538   Repo *installed = solv->installed;
3539   Queue q, q2;
3540   Rule *r;
3541   Queue infoq;
3542   int i, oldcnt;
3543   Map *lockedmap = 0;
3544
3545   solv->bestrules = solv->nrules;
3546   queue_init(&q);
3547   queue_init(&q2);
3548   queue_init(&infoq);
3549
3550   if (haslockjob)
3551     {
3552       int i;
3553       lockedmap = solv_calloc(1, sizeof(Map));
3554       map_init(lockedmap, pool->nsolvables);
3555       for (i = 0, r = solv->rules + solv->jobrules; i < solv->ruletojob.count; i++, r++)
3556         {
3557           if (r->w2 || (solv->job.elements[solv->ruletojob.elements[i]] & SOLVER_JOBMASK) != SOLVER_LOCK)
3558             continue;
3559           p = r->p > 0 ? r->p : -r->p;
3560           MAPSET(lockedmap, p);
3561         }
3562     }
3563   if (havebestinstalljobs)
3564     {
3565       for (i = 0; i < solv->job.count; i += 2)
3566         {
3567           Id how = solv->job.elements[i];
3568           if ((how & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
3569             {
3570               int j, k;
3571               Id p2, pp2;
3572               for (j = 0; j < solv->ruletojob.count; j++)
3573                 {
3574                   if (solv->ruletojob.elements[j] != i)
3575                     continue;
3576                   r = solv->rules + solv->jobrules + j;
3577                   queue_empty(&q);
3578                   queue_empty(&q2);
3579                   FOR_RULELITERALS(p2, pp2, r)
3580                     {
3581                       if (p2 > 0)
3582                         queue_push(&q, p2);
3583                       else if (p2 < 0)
3584                         queue_push(&q2, p2);
3585                     }
3586                   if (pool->considered && pool->whatprovideswithdisabled)
3587                     prune_disabled(pool, &q);
3588                   if (!q.count)
3589                     continue;   /* orphaned */
3590                   /* select best packages, just look at prio and version */
3591                   oldcnt = q.count;
3592                   policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3593                   if (q.count == oldcnt)
3594                     continue;   /* nothing filtered */
3595                   if (lockedmap)
3596                     {
3597                       queue_insertn(&q, 0, q2.count, q2.elements);
3598                       queue_empty(&q2);
3599                       FOR_RULELITERALS(p2, pp2, r)
3600                         {
3601                           if (p2 <= 0)
3602                             continue;
3603                           if (installed && pool->solvables[p2].repo == installed)
3604                             {
3605                               if (MAPTST(lockedmap, p2))
3606                                 queue_pushunique(&q, p2);               /* we always want that package */
3607                             }
3608                           else if (MAPTST(lockedmap, p2))
3609                             continue;
3610                           queue_push(&q2, p2);
3611                         }
3612                       if (pool->considered && pool->whatprovideswithdisabled)
3613                         prune_disabled(pool, &q2);
3614                       policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
3615                       for (k = 0; k < q2.count; k++)
3616                         queue_pushunique(&q, q2.elements[k]);
3617                       queue_empty(&q2);
3618                     }
3619                   if (q2.count)
3620                     queue_insertn(&q, 0, q2.count, q2.elements);
3621                   p2 = queue_shift(&q);
3622                   if (q.count < 2)
3623                     solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3624                   else
3625                     solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3626                   if ((how & SOLVER_WEAK) != 0)
3627                     queue_push(&solv->weakruleq, solv->nrules - 1);
3628                   queue_push(&infoq, -(solv->jobrules + j));
3629                 }
3630             }
3631         }
3632     }
3633   solv->bestrules_up = solv->nrules;
3634
3635   if (installed && (solv->bestupdatemap_all || solv->bestupdatemap.size))
3636     {
3637       Map m;
3638
3639       if (solv->allowuninstall || solv->allowuninstall_all || solv->allowuninstallmap.size)
3640         map_init(&m, pool->nsolvables);
3641       else
3642         map_init(&m, 0);
3643       FOR_REPO_SOLVABLES(installed, p, s)
3644         {
3645           Id d, p2, pp2;
3646           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
3647             continue;
3648           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
3649             continue;
3650           queue_empty(&q);
3651           if (solv->bestobeypolicy)
3652             r = solv->rules + solv->updaterules + (p - installed->start);
3653           else
3654             {
3655               r = solv->rules + solv->featurerules + (p - installed->start);
3656               if (!r->p)        /* identical to update rule? */
3657                 r = solv->rules + solv->updaterules + (p - installed->start);
3658             }
3659           if (solv->specialupdaters && (d = solv->specialupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
3660             {
3661               /* need to check specialupdaters */
3662               if (r->p == p)    /* be careful with the dup case */
3663                 queue_push(&q, p);
3664               while ((p2 = pool->whatprovidesdata[d++]) != 0)
3665                 queue_push(&q, p2);
3666             }
3667           else
3668             {
3669               FOR_RULELITERALS(p2, pp2, r)
3670                 if (p2 > 0)
3671                   queue_push(&q, p2);
3672             }
3673           if (lockedmap)
3674             {
3675               queue_empty(&q2);
3676               queue_insertn(&q2, 0, q.count, q.elements);
3677             }
3678           prune_best_update(solv, p, &q);
3679           if (!q.count)
3680             continue;   /* orphaned */
3681           if (lockedmap)
3682             {
3683               int j;
3684               /* always ok to keep installed locked packages */
3685               if (MAPTST(lockedmap, p))
3686                 queue_pushunique(&q2, p);
3687               for (j = 0; j < q2.count; j++)
3688                 {
3689                   Id p2 = q2.elements[j];
3690                   if (pool->solvables[p2].repo == installed && MAPTST(lockedmap, p2))
3691                     queue_pushunique(&q, p2);
3692                 }
3693               /* filter out locked packages */
3694               for (i = j = 0; j < q2.count; j++)
3695                 {
3696                   Id p2 = q2.elements[j];
3697                   if (pool->solvables[p2].repo == installed || !MAPTST(lockedmap, p2))
3698                     q2.elements[i++] = p2;
3699                 }
3700               queue_truncate(&q2, i);
3701               prune_best_update(solv, p, &q2);
3702               for (j = 0; j < q2.count; j++)
3703                 queue_pushunique(&q, q2.elements[j]);
3704             }
3705           if (solv->bestobeypolicy)
3706             {
3707               /* also filter the best of the feature rule packages and add them */
3708               r = solv->rules + solv->featurerules + (p - installed->start);
3709               if (r->p)
3710                 {
3711                   int j;
3712                   queue_empty(&q2);
3713                   FOR_RULELITERALS(p2, pp2, r)
3714                     if (p2 > 0)
3715                       queue_push(&q2, p2);
3716                   prune_best_update(solv, p, &q2);
3717                   for (j = 0; j < q2.count; j++)
3718                     queue_pushunique(&q, q2.elements[j]);
3719                   if (lockedmap)
3720                     {
3721                       queue_empty(&q2);
3722                       FOR_RULELITERALS(p2, pp2, r)
3723                         if (p2 > 0)
3724                           if (pool->solvables[p2].repo == installed || !MAPTST(lockedmap, p2))
3725                             queue_push(&q2, p2);
3726                       prune_best_update(solv, p, &q2);
3727                       for (j = 0; j < q2.count; j++)
3728                         queue_pushunique(&q, q2.elements[j]);
3729                     }
3730                 }
3731             }
3732           if (solv->allowuninstall || solv->allowuninstall_all || (solv->allowuninstallmap.size && MAPTST(&solv->allowuninstallmap, p - installed->start)))
3733             {
3734               /* package is flagged both for allowuninstall and best, add negative rules */
3735               d = q.count == 1 ? q.elements[0] : -pool_queuetowhatprovides(pool, &q);
3736               for (i = 0; i < q.count; i++)
3737                 MAPSET(&m, q.elements[i]);
3738               r = solv->rules + solv->featurerules + (p - installed->start);
3739               if (!r->p)        /* identical to update rule? */
3740                 r = solv->rules + solv->updaterules + (p - installed->start);
3741               FOR_RULELITERALS(p2, pp2, r)
3742                 {
3743                   if (MAPTST(&m, p2))
3744                     continue;
3745                   if (d >= 0)
3746                     solver_addrule(solv, -p2, d, 0);
3747                   else
3748                     solver_addrule(solv, -p2, 0, -d);
3749                   queue_push(&infoq, p);
3750                 }
3751               for (i = 0; i < q.count; i++)
3752                 MAPCLR(&m, q.elements[i]);
3753               continue;
3754             }
3755           p2 = queue_shift(&q);
3756           if (q.count < 2)
3757             solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3758           else
3759             solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3760           queue_push(&infoq, p);
3761         }
3762       map_free(&m);
3763     }
3764   if (infoq.count)
3765     solv->bestrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3766   solv->bestrules_end = solv->nrules;
3767   queue_free(&q);
3768   queue_free(&q2);
3769   queue_free(&infoq);
3770   if (lockedmap)
3771     {
3772       map_free(lockedmap);
3773       solv_free(lockedmap);
3774     }
3775 }
3776
3777
3778
3779
3780 /* yumobs rule handling */
3781 /* note that we use pool->obsoleteusescolors || pool->implicitobsoleteusescolors
3782  * like in policy_findupdatepackages */
3783
3784 static void
3785 find_obsolete_group(Solver *solv, Id obs, Queue *q)
3786 {
3787   Pool *pool = solv->pool;
3788   Queue qn;
3789   Id p2, pp2, op, *opp, opp2;
3790   int i, j, qnc, ncnt;
3791
3792   queue_empty(q);
3793   FOR_PROVIDES(p2, pp2, obs)
3794     {
3795       Solvable *s2 = pool->solvables + p2;
3796       if (s2->repo != pool->installed)
3797         continue;
3798       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3799         continue;
3800       /* we obsolete installed package s2 with obs. now find all other packages that have the same dep  */
3801       for (opp = solv->obsoletes_data + solv->obsoletes[p2 - solv->installed->start]; (op = *opp++) != 0;)
3802         {
3803           Solvable *os = pool->solvables + op;
3804           Id obs2, *obsp2;
3805           if (!os->obsoletes)
3806             continue;
3807           if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s2, os))
3808             continue;
3809           obsp2 = os->repo->idarraydata + os->obsoletes;
3810           while ((obs2 = *obsp2++) != 0)
3811             if (obs2 == obs)
3812               break;
3813           if (obs2)
3814             queue_pushunique(q, op);
3815         }
3816       /* also search packages with the same name */
3817       FOR_PROVIDES(op, opp2, s2->name)
3818         {
3819           Solvable *os = pool->solvables + op;
3820           Id obs2, *obsp2;
3821           if (os->name != s2->name)
3822             continue;
3823           if (!os->obsoletes)
3824             continue;
3825           if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s2, os))
3826             continue;
3827           obsp2 = os->repo->idarraydata + os->obsoletes;
3828           while ((obs2 = *obsp2++) != 0)
3829             if (obs2 == obs)
3830               break;
3831           if (obs2)
3832             queue_pushunique(q, op);
3833         }
3834     }
3835   /* find names so that we can build groups */
3836   queue_init_clone(&qn, q);
3837   prune_to_best_version(solv->pool, &qn);
3838 #if 0
3839 {
3840   for (i = 0; i < qn.count; i++)
3841     printf(" + %s\n", pool_solvid2str(pool, qn.elements[i]));
3842 }
3843 #endif
3844   /* filter into name groups */
3845   qnc = qn.count;
3846   if (qnc == 1)
3847     {
3848       queue_free(&qn);
3849       queue_empty(q);
3850       return;
3851     }
3852   ncnt = 0;
3853   for (i = 0; i < qnc; i++)
3854     {
3855       Id n = pool->solvables[qn.elements[i]].name;
3856       int got = 0;
3857       for (j = 0; j < q->count; j++)
3858         {
3859           Id p = q->elements[j];
3860           if (pool->solvables[p].name == n)
3861             {
3862               queue_push(&qn, p);
3863               got = 1;
3864             }
3865         }
3866       if (got)
3867         {
3868           queue_push(&qn, 0);
3869           ncnt++;
3870         }
3871     }
3872   if (ncnt <= 1)
3873     {
3874       queue_empty(q);
3875     }
3876   else
3877     {
3878       queue_empty(q);
3879       queue_insertn(q, 0, qn.count - qnc, qn.elements + qnc);
3880     }
3881   queue_free(&qn);
3882 }
3883
3884 void
3885 solver_addyumobsrules(Solver *solv)
3886 {
3887   Pool *pool = solv->pool;
3888   Repo *installed = solv->installed;
3889   Id p, op, *opp;
3890   Solvable *s;
3891   Queue qo, qq, infoq;
3892   int i, j, k;
3893   unsigned int now;
3894
3895   solv->yumobsrules = solv->nrules;
3896   if (!installed || !solv->obsoletes)
3897     {
3898       solv->yumobsrules_end = solv->nrules;
3899       return;
3900     }
3901   now = solv_timems(0);
3902   queue_init(&qo);
3903   FOR_REPO_SOLVABLES(installed, p, s)
3904     {
3905       if (!solv->obsoletes[p - installed->start])
3906         continue;
3907 #if 0
3908 printf("checking yumobs for %s\n", pool_solvable2str(pool, s));
3909 #endif
3910       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (op = *opp++) != 0;)
3911         {
3912           Solvable *os = pool->solvables + op;
3913           Id obs, *obsp = os->repo->idarraydata + os->obsoletes;
3914           Id p2, pp2;
3915           while ((obs = *obsp++) != 0)
3916             {
3917               FOR_PROVIDES(p2, pp2, obs)
3918                 {
3919                   Solvable *s2 = pool->solvables + p2;
3920                   if (s2->repo != installed)
3921                     continue;
3922                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3923                     continue;
3924                   if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s, s2))
3925                     continue;
3926                   queue_pushunique(&qo, obs);
3927                   break;
3928                 }
3929             }
3930         }
3931     }
3932   if (!qo.count)
3933     {
3934       queue_free(&qo);
3935       return;
3936     }
3937   queue_init(&infoq);
3938   queue_init(&qq);
3939   for (i = 0; i < qo.count; i++)
3940     {
3941       int group, groupk, groupstart;
3942       queue_empty(&qq);
3943 #if 0
3944 printf("investigating %s\n", pool_dep2str(pool, qo.elements[i]));
3945 #endif
3946       find_obsolete_group(solv, qo.elements[i], &qq);
3947 #if 0
3948 printf("result:\n");
3949 for (j = 0; j < qq.count; j++)
3950   if (qq.elements[j] == 0)
3951     printf("---\n");
3952   else
3953     printf("%s\n", pool_solvid2str(pool, qq.elements[j]));
3954 #endif
3955
3956       if (!qq.count)
3957         continue;
3958       /* at least two goups, build rules */
3959       group = 0;
3960       for (j = 0; j < qq.count; j++)
3961         {
3962           p = qq.elements[j];
3963           if (!p)
3964             {
3965               group++;
3966               continue;
3967             }
3968           if (pool->solvables[p].repo == installed)
3969             continue;
3970           groupk = 0;
3971           groupstart = 0;
3972           for (k = 0; k < qq.count; k++)
3973             {
3974               Id pk = qq.elements[k];
3975               if (pk)
3976                 continue;
3977               if (group != groupk && k > groupstart)
3978                 {
3979                   /* add the rule */
3980                   if (k - groupstart == 1)
3981                     solver_addrule(solv, -p, qq.elements[groupstart], 0);
3982                   else
3983                     solver_addrule(solv, -p, 0, pool_ids2whatprovides(pool, qq.elements + groupstart, k - groupstart));
3984                   queue_push(&infoq, qo.elements[i]);
3985                 }
3986               groupstart = k + 1;
3987               groupk++;
3988             }
3989         }
3990     }
3991   if (infoq.count)
3992     solv->yumobsrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
3993   queue_free(&infoq);
3994   queue_free(&qq);
3995   queue_free(&qo);
3996   solv->yumobsrules_end = solv->nrules;
3997   POOL_DEBUG(SOLV_DEBUG_STATS, "yumobs rule creation took %d ms\n", solv_timems(now));
3998 }
3999
4000 /* recommendsrules are a copy of some recommends package rule but
4001  * with some disfavored literals removed */
4002 void
4003 solver_addrecommendsrules(Solver *solv)
4004 {
4005   Pool *pool = solv->pool;
4006   int i, havedis, havepos;
4007   Id p, pp;
4008   Queue q, infoq;
4009
4010   solv->recommendsrules = solv->nrules;
4011   queue_init(&q);
4012   queue_init(&infoq);
4013   for (i = 0; i < solv->recommendsruleq->count; i++)
4014     {
4015       int rid = solv->recommendsruleq->elements[i];
4016       Rule *r = solv->rules + rid;
4017       queue_empty(&q);
4018       havedis = havepos = 0;
4019       FOR_RULELITERALS(p, pp, r)
4020         {
4021           if (p > 0 && solv->favormap[p] < 0)
4022             havedis = 1;
4023           else
4024             {
4025               if (p > 0)
4026                 havepos = 1;
4027               queue_push(&q, p);
4028             }
4029         }
4030       if (!havedis)
4031         continue;
4032       solver_disablerule(solv, r);
4033       if (!havepos || q.count < 2)
4034         continue;
4035       if (q.count == 2)
4036         solver_addrule(solv, q.elements[0], q.elements[1], 0);
4037       else
4038         solver_addrule(solv, q.elements[0], 0, pool_ids2whatprovides(pool, q.elements + 1, q.count - 1));
4039       queue_push(&infoq, rid);
4040     }
4041   if (infoq.count)
4042     solv->recommendsrules_info = solv_memdup2(infoq.elements, infoq.count, sizeof(Id));
4043   queue_free(&infoq);
4044   queue_free(&q);
4045   solv->recommendsrules_end = solv->nrules;
4046 }
4047
4048 void
4049 solver_breakorphans(Solver *solv)
4050 {
4051   Pool *pool = solv->pool;
4052   Repo *installed = solv->installed;
4053   int i, rid;
4054   Map m;
4055
4056   if (!installed || solv->droporphanedmap_all)
4057     return;
4058   solv->brokenorphanrules = solv_calloc(1, sizeof(Queue));
4059   queue_init(solv->brokenorphanrules);
4060   map_init(&m, installed->end - installed->start);
4061   for (i = 0; i < solv->orphaned.count; i++)
4062     {
4063       Id p = solv->orphaned.elements[i];
4064       if (pool->solvables[p].repo != installed)
4065         continue;
4066       if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - installed->start))
4067         continue;
4068       MAPSET(&m, p - installed->start);
4069     }
4070   for (rid = 1; rid < solv->pkgrules_end ; rid++)
4071     {
4072       Id p, *dp;
4073       Rule *r = solv->rules + rid;
4074       /* ignore non-deps and simple conflicts */
4075       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
4076         continue;
4077       p = -r->p;
4078       if (p < installed->start || p >= installed->end || !MAPTST(&m, p - installed->start))
4079         {
4080           /* need to check other literals */
4081           if (r->d == 0 || r->d == -1)
4082             continue;
4083           for (dp = pool->whatprovidesdata + (r->d < 0 ? -r->d - 1 : r->d); *dp < 0; dp++)
4084             {
4085               p = -*dp;
4086               if (p >= installed->start && p < installed->end && MAPTST(&m, p - installed->start))
4087                 break;
4088             }
4089           if (*dp >= 0)
4090             continue;
4091         }
4092       /* ok, disable this rule */
4093       queue_push(solv->brokenorphanrules, rid);
4094       if (r->d >= 0)
4095         solver_disablerule(solv, r);
4096     }
4097   map_free(&m);
4098   if (!solv->brokenorphanrules->count)
4099     {
4100       queue_free(solv->brokenorphanrules);
4101       solv->brokenorphanrules = solv_free(solv->brokenorphanrules);
4102     }
4103 }
4104
4105 void
4106 solver_check_brokenorphanrules(Solver *solv, Queue *dq)
4107 {
4108   Pool *pool = solv->pool;
4109   int i;
4110   Id l, pp;
4111
4112   queue_empty(dq);
4113   if (!solv->brokenorphanrules)
4114     return;
4115   for (i = 0; i < solv->brokenorphanrules->count; i++)
4116     {
4117       int rid = solv->brokenorphanrules->elements[i];
4118       Rule *r = solv->rules + rid;
4119       FOR_RULELITERALS(l, pp, r)
4120         {
4121           if (l < 0)
4122             {
4123               if (solv->decisionmap[-l] <= 0)
4124                 break;
4125             }
4126           else
4127             {
4128               if (solv->decisionmap[l] > 0 && pool->solvables[l].repo != solv->installed)
4129                 break;
4130             }
4131         }
4132       if (l)
4133         continue;
4134       FOR_RULELITERALS(l, pp, r)
4135         if (l > 0 && solv->decisionmap[l] == 0 && pool->solvables[l].repo != solv->installed)
4136           queue_pushunique(dq, l);
4137     }
4138 }
4139