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