Imported Upstream version 0.7.3
[platform/upstream/libsolv.git] / src / solver.c
1 /*
2  * Copyright (c) 2007-2008, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solver.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 "util.h"
25 #include "policy.h"
26 #include "poolarch.h"
27 #include "solverdebug.h"
28 #include "cplxdeps.h"
29 #include "linkedpkg.h"
30
31 #define RULES_BLOCK 63
32
33
34 /************************************************************************/
35
36 /*
37  * enable/disable learnt rules
38  *
39  * we have enabled or disabled some of our rules. We now reenable all
40  * of our learnt rules except the ones that were learnt from rules that
41  * are now disabled.
42  */
43 static void
44 enabledisablelearntrules(Solver *solv)
45 {
46   Pool *pool = solv->pool;
47   Rule *r;
48   Id why, *whyp;
49   int i;
50
51   POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
52   for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
53     {
54       whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
55       while ((why = *whyp++) != 0)
56         {
57           assert(why > 0 && why < i);
58           if (solv->rules[why].d < 0)
59             break;
60         }
61       /* why != 0: we found a disabled rule, disable the learnt rule */
62       if (why && r->d >= 0)
63         {
64           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
65             {
66               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
67               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
68             }
69           solver_disablerule(solv, r);
70         }
71       else if (!why && r->d < 0)
72         {
73           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
74             {
75               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
76               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
77             }
78           solver_enablerule(solv, r);
79         }
80     }
81 }
82
83
84 /*
85  * make assertion rules into decisions
86  *
87  * Go through rules and add direct assertions to the decisionqueue.
88  * If we find a conflict, disable rules and add them to problem queue.
89  */
90
91 static int
92 makeruledecisions(Solver *solv, int disablerules)
93 {
94   Pool *pool = solv->pool;
95   int i, ri, ii, ori;
96   Rule *r, *rr;
97   Id v, vv;
98   int decisionstart;
99   int record_proof = 1;
100   int oldproblemcount;
101   int havedisabled = 0;
102   int doautouninstall;
103
104   /* The system solvable is always installed first */
105   assert(solv->decisionq.count == 0);
106   queue_push(&solv->decisionq, SYSTEMSOLVABLE);
107   queue_push(&solv->decisionq_why, 0);
108   queue_push2(&solv->decisionq_reason, 0, 0);
109   solv->decisionmap[SYSTEMSOLVABLE] = 1;        /* installed at level '1' */
110
111   decisionstart = solv->decisionq.count;
112   for (;;)
113     {
114       /* if we needed to re-run, back up decisions to decisionstart */
115       while (solv->decisionq.count > decisionstart)
116         {
117           v = solv->decisionq.elements[--solv->decisionq.count];
118           --solv->decisionq_why.count;
119           vv = v > 0 ? v : -v;
120           solv->decisionmap[vv] = 0;
121         }
122
123       /* note that the ruleassertions queue is ordered */
124       for (ii = 0; ii < solv->ruleassertions.count; ii++)
125         {
126           ri = solv->ruleassertions.elements[ii];
127           r = solv->rules + ri;
128
129           if (havedisabled && ri >= solv->learntrules)
130             {
131               /* just started with learnt rule assertions. If we have disabled
132                * some rules, adapt the learnt rule status */
133               enabledisablelearntrules(solv);
134               havedisabled = 0;
135             }
136
137           if (r->d < 0 || !r->p || r->w2)       /* disabled, dummy or no assertion */
138             continue;
139
140           /* do weak rules in phase 2 */
141           if (ri < solv->learntrules && solv->weakrulemap.size && MAPTST(&solv->weakrulemap, ri))
142             continue;
143
144           v = r->p;
145           vv = v > 0 ? v : -v;
146
147           if (!solv->decisionmap[vv])          /* if not yet decided */
148             {
149               queue_push(&solv->decisionq, v);
150               queue_push(&solv->decisionq_why, ri);
151               solv->decisionmap[vv] = v > 0 ? 1 : -1;
152               IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
153                 {
154                   Solvable *s = solv->pool->solvables + vv;
155                   if (v < 0)
156                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
157                   else
158                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing  %s (assertion)\n", pool_solvable2str(solv->pool, s));
159                 }
160               continue;
161             }
162
163           /* check against previous decision: is there a conflict? */
164           if (v > 0 && solv->decisionmap[vv] > 0)    /* ok to install */
165             continue;
166           if (v < 0 && solv->decisionmap[vv] < 0)    /* ok to remove */
167             continue;
168
169           /*
170            * found a conflict!
171            *
172            * The rule (r) we're currently processing says something
173            * different (v = r->p) than a previous decision (decisionmap[abs(v)])
174            * on this literal
175            */
176
177           if (ri >= solv->learntrules)
178             {
179               /* conflict with a learnt rule */
180               /* can happen when packages cannot be installed for multiple reasons. */
181               /* we disable the learnt rule in this case */
182               /* (XXX: we should really do something like analyze_unsolvable_rule here!) */
183               solver_disablerule(solv, r);
184               continue;
185             }
186
187           POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ASSERTION ----------------------\n");
188           assert(ri >= solv->pkgrules_end);     /* must not have a conflict in the pkg rules! */
189
190           /*
191            * find the decision which is the "opposite" of the rule
192            */
193           for (i = 0; i < solv->decisionq.count; i++)
194             if (solv->decisionq.elements[i] == -v)
195               break;
196           assert(i < solv->decisionq.count);         /* assert that we found it */
197           if (v == -SYSTEMSOLVABLE)
198             ori = 0;
199           else
200             {
201               ori = solv->decisionq_why.elements[i];            /* the conflicting rule */
202               assert(ori > 0);
203             }
204
205           /*
206            * record the problem
207            */
208           doautouninstall = 0;
209           oldproblemcount = solv->problems.count;
210           queue_push(&solv->problems, 0);                       /* start problem */
211           if (ori < solv->pkgrules_end)
212             {
213               /* easy: conflict with system solvable or pkg rule */
214               assert(v > 0 || v == -SYSTEMSOLVABLE);
215               IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
216                 {
217                   if (ori)
218                     POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with pkg rule, disabling rule #%d\n", ri);
219                   else
220                     POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
221                   solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + ri);
222                   if (ori)
223                     solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + ori);
224                 }
225               solver_recordproblem(solv, ri);
226               if (ri >= solv->featurerules && ri < solv->updaterules_end)
227                 doautouninstall = 1;
228             }
229           else
230             {
231               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
232               /*
233                * push all of our rules (can only be feature or job rules)
234                * asserting this literal on the problem stack
235                */
236               for (i = solv->pkgrules_end, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
237                 {
238                   if (rr->d < 0                          /* disabled */
239                       || rr->w2)                         /*  or no assertion */
240                     continue;
241                   if (rr->p != vv                        /* not affecting the literal */
242                       && rr->p != -vv)
243                     continue;
244                   if (solv->weakrulemap.size && MAPTST(&solv->weakrulemap, i))     /* weak: silently ignore */
245                     continue;
246
247                   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
248                   solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
249                   solver_recordproblem(solv, i);
250                   if (i >= solv->featurerules && i < solv->updaterules_end)
251                     doautouninstall = 1;
252                 }
253             }
254           queue_push(&solv->problems, 0);                       /* finish problem */
255
256           /* try autouninstall if requested */
257           if (doautouninstall)
258             {
259               if (solv->allowuninstall || solv->allowuninstall_all || solv->allowuninstallmap.size)
260                 if (solver_autouninstall(solv, oldproblemcount) != 0)
261                   {
262                     solv->problems.count = oldproblemcount;
263                     havedisabled = 1;
264                     break;      /* start over */
265                   }
266             }
267
268           /* record the proof if requested */
269           if (record_proof)
270             {
271               solv->problems.elements[oldproblemcount] = solv->learnt_pool.count;
272               queue_push(&solv->learnt_pool, ri);
273               if (ori)
274                 queue_push(&solv->learnt_pool, ori);
275               queue_push(&solv->learnt_pool, 0);
276             }
277
278           if (!disablerules)
279             {
280               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
281               return -1;
282             }
283           /* disable all problem rules */
284           solver_disableproblemset(solv, oldproblemcount);
285           havedisabled = 1;
286           break;        /* start over */
287         }
288       if (ii < solv->ruleassertions.count)
289         continue;
290
291       /*
292        * phase 2: now do the weak assertions
293        */
294       if (!solv->weakrulemap.size)
295         break;                          /* no weak rules, no phase 2 */
296       for (ii = 0; ii < solv->ruleassertions.count; ii++)
297         {
298           ri = solv->ruleassertions.elements[ii];
299           r = solv->rules + ri;
300           if (r->d < 0 || r->w2)                         /* disabled or no assertion */
301             continue;
302           if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri))       /* skip non-weak */
303             continue;
304           v = r->p;
305           vv = v > 0 ? v : -v;
306
307           if (!solv->decisionmap[vv])          /* if not yet decided */
308             {
309               queue_push(&solv->decisionq, v);
310               queue_push(&solv->decisionq_why, r - solv->rules);
311               solv->decisionmap[vv] = v > 0 ? 1 : -1;
312               IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
313                 {
314                   Solvable *s = solv->pool->solvables + vv;
315                   if (v < 0)
316                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
317                   else
318                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing  %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
319                 }
320               continue;
321             }
322           /* check against previous decision: is there a conflict? */
323           if (v > 0 && solv->decisionmap[vv] > 0)
324             continue;
325           if (v < 0 && solv->decisionmap[vv] < 0)
326             continue;
327
328           POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
329           solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
330           solver_fixproblem(solv, ri);
331           havedisabled = 1;
332           break;        /* start over */
333         }
334       if (ii == solv->ruleassertions.count)
335         break;  /* finished! */
336     }
337   return 1;             /* the new level */
338 }
339
340
341 /********************************************************************/
342 /* watches */
343
344
345 /*-------------------------------------------------------------------
346  * makewatches
347  *
348  * initial setup for all watches
349  */
350
351 static void
352 makewatches(Solver *solv)
353 {
354   Rule *r;
355   int i;
356   int nsolvables = solv->pool->nsolvables;
357
358   solv_free(solv->watches);
359   /* lower half for removals, upper half for installs */
360   solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
361   for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
362     {
363       if (!r->w2)               /* assertions do not need watches */
364         continue;
365
366       /* see addwatches_rule(solv, r) */
367       r->n1 = solv->watches[nsolvables + r->w1];
368       solv->watches[nsolvables + r->w1] = r - solv->rules;
369
370       r->n2 = solv->watches[nsolvables + r->w2];
371       solv->watches[nsolvables + r->w2] = r - solv->rules;
372     }
373 }
374
375
376 /*-------------------------------------------------------------------
377  *
378  * add watches (for a new learned rule)
379  * sets up watches for a single rule
380  *
381  * see also makewatches() above.
382  */
383
384 static inline void
385 addwatches_rule(Solver *solv, Rule *r)
386 {
387   int nsolvables = solv->pool->nsolvables;
388
389   r->n1 = solv->watches[nsolvables + r->w1];
390   solv->watches[nsolvables + r->w1] = r - solv->rules;
391
392   r->n2 = solv->watches[nsolvables + r->w2];
393   solv->watches[nsolvables + r->w2] = r - solv->rules;
394 }
395
396
397 /********************************************************************/
398 /*
399  * rule propagation
400  */
401
402
403 /* shortcuts to check if a literal (positive or negative) assignment
404  * evaluates to 'true' or 'false'
405  */
406 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
407 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
408 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
409
410 /*-------------------------------------------------------------------
411  *
412  * propagate
413  *
414  * make decision and propagate to all rules
415  *
416  * Evaluate each term affected by the decision (linked through watches).
417  * If we find unit rules we make new decisions based on them.
418  *
419  * return : 0 = everything is OK
420  *          rule = conflict found in this rule
421  */
422
423 static Rule *
424 propagate(Solver *solv, int level)
425 {
426   Pool *pool = solv->pool;
427   Id *rp, *next_rp;           /* rule pointer, next rule pointer in linked list */
428   Rule *r;                    /* rule */
429   Id p, pkg, other_watch;
430   Id *dp;
431   Id *decisionmap = solv->decisionmap;
432   Id *watches = solv->watches + pool->nsolvables;   /* place ptr in middle */
433
434   POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
435
436   /* foreach non-propagated decision */
437   while (solv->propagate_index < solv->decisionq.count)
438     {
439       /*
440        * 'pkg' was just decided
441        * negate because our watches trigger if literal goes FALSE
442        */
443       pkg = -solv->decisionq.elements[solv->propagate_index++];
444         
445       IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
446         {
447           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
448           solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
449         }
450
451       /* foreach rule where 'pkg' is now FALSE */
452       for (rp = watches + pkg; *rp; rp = next_rp)
453         {
454           r = solv->rules + *rp;
455           if (r->d < 0)
456             {
457               /* rule is disabled, goto next */
458               if (pkg == r->w1)
459                 next_rp = &r->n1;
460               else
461                 next_rp = &r->n2;
462               continue;
463             }
464
465           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
466             {
467               POOL_DEBUG(SOLV_DEBUG_PROPAGATE,"  watch triggered ");
468               solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
469             }
470
471           /*
472            * 'pkg' was just decided (was set to FALSE), so this rule
473            * may now be unit.
474            */
475           /* find the other watch */
476           if (pkg == r->w1)
477             {
478               other_watch = r->w2;
479               next_rp = &r->n1;
480             }
481           else
482             {
483               other_watch = r->w1;
484               next_rp = &r->n2;
485             }
486
487           /*
488            * if the other watch is true we have nothing to do
489            */
490           if (DECISIONMAP_TRUE(other_watch))
491             continue;
492
493           /*
494            * The other literal is FALSE or UNDEF
495            *
496            */
497
498           if (r->d)
499             {
500               /* Not a binary clause, try to move our watch.
501                *
502                * Go over all literals and find one that is
503                *   not other_watch
504                *   and not FALSE
505                *
506                * (TRUE is also ok, in that case the rule is fulfilled)
507                * As speed matters here we do not use the FOR_RULELITERALS
508                * macro.
509                */
510               if (r->p                                /* we have a 'p' */
511                   && r->p != other_watch              /* which is not watched */
512                   && !DECISIONMAP_FALSE(r->p))        /* and not FALSE */
513                 {
514                   p = r->p;
515                 }
516               else                                    /* go find a 'd' to make 'true' */
517                 {
518                   /* foreach p in 'd'
519                      we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
520                    */
521                   for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
522                     {
523                       if (p != other_watch              /* which is not watched */
524                           && !DECISIONMAP_FALSE(p))     /* and not FALSE */
525                         break;
526                     }
527                 }
528
529               if (p)
530                 {
531                   /*
532                    * if we found some p that is UNDEF or TRUE, move
533                    * watch to it
534                    */
535                   IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
536                     {
537                       if (p > 0)
538                         POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
539                       else
540                         POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
541                     }
542
543                   *rp = *next_rp;
544                   next_rp = rp;
545
546                   if (pkg == r->w1)
547                     {
548                       r->w1 = p;
549                       r->n1 = watches[p];
550                     }
551                   else
552                     {
553                       r->w2 = p;
554                       r->n2 = watches[p];
555                     }
556                   watches[p] = r - solv->rules;
557                   continue;
558                 }
559               /* search failed, thus all unwatched literals are FALSE */
560                 
561             } /* not binary */
562
563           /*
564            * unit clause found, set literal other_watch to TRUE
565            */
566
567           if (DECISIONMAP_FALSE(other_watch))      /* check if literal is FALSE */
568             return r;                              /* eek, a conflict! */
569
570           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
571             {
572               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "  unit ");
573               solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
574             }
575
576           if (other_watch > 0)
577             decisionmap[other_watch] = level;    /* install! */
578           else
579             decisionmap[-other_watch] = -level;  /* remove! */
580
581           queue_push(&solv->decisionq, other_watch);
582           queue_push(&solv->decisionq_why, r - solv->rules);
583
584           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
585             {
586               if (other_watch > 0)
587                 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> decided to install %s\n", pool_solvid2str(pool, other_watch));
588               else
589                 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
590             }
591
592         } /* foreach rule involving 'pkg' */
593         
594     } /* while we have non-decided decisions */
595
596   POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
597
598   return 0;     /* all is well */
599 }
600
601
602 /********************************************************************/
603 /* Analysis */
604
605 /*-------------------------------------------------------------------
606  *
607  * revert
608  * revert decisionq to a level
609  */
610
611 static void
612 revert(Solver *solv, int level)
613 {
614   Pool *pool = solv->pool;
615   Id v, vv;
616   while (solv->decisionq.count)
617     {
618       v = solv->decisionq.elements[solv->decisionq.count - 1];
619       vv = v > 0 ? v : -v;
620       if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
621         break;
622       POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
623       solv->decisionmap[vv] = 0;
624       solv->decisionq.count--;
625       solv->decisionq_why.count--;
626       solv->propagate_index = solv->decisionq.count;
627     }
628   while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= level)
629     solv->branches.count -= solv->branches.elements[solv->branches.count - 2];
630   if (solv->recommends_index > solv->decisionq.count)
631     solv->recommends_index = -1;        /* rebuild recommends/suggests maps */
632   solv->decisionq_reason.count = level + 1;
633 }
634
635 /*-------------------------------------------------------------------
636  *
637  * watch2onhighest - put watch2 on literal with highest level
638  */
639
640 static inline void
641 watch2onhighest(Solver *solv, Rule *r)
642 {
643   int l, wl = 0;
644   Id d, v, *dp;
645
646   d = r->d < 0 ? -r->d - 1 : r->d;
647   if (!d)
648     return;     /* binary rule, both watches are set */
649   dp = solv->pool->whatprovidesdata + d;
650   while ((v = *dp++) != 0)
651     {
652       l = solv->decisionmap[v < 0 ? -v : v];
653       if (l < 0)
654         l = -l;
655       if (l > wl)
656         {
657           r->w2 = dp[-1];
658           wl = l;
659         }
660     }
661 }
662
663
664 /*-------------------------------------------------------------------
665  *
666  * analyze
667  *   and learn
668  */
669
670 static int
671 analyze(Solver *solv, int level, Rule *c, Rule **lrp)
672 {
673   Pool *pool = solv->pool;
674   Queue q;
675   Rule *r;
676   Id q_buf[8];
677   int rlevel = 1;
678   Map seen;             /* global? */
679   Id p = 0, pp, v, vv, why;
680   int l, i, idx;
681   int num = 0, l1num = 0;
682   int learnt_why = solv->learnt_pool.count;
683   Id *decisionmap = solv->decisionmap;
684
685   queue_init_buffer(&q, q_buf, sizeof(q_buf)/sizeof(*q_buf));
686
687   POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
688   map_init(&seen, pool->nsolvables);
689   idx = solv->decisionq.count;
690   for (;;)
691     {
692       IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
693         solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
694       queue_push(&solv->learnt_pool, c - solv->rules);
695       FOR_RULELITERALS(v, pp, c)
696         {
697           if (DECISIONMAP_TRUE(v))      /* the one true literal */
698             continue;
699           vv = v > 0 ? v : -v;
700           if (MAPTST(&seen, vv))
701             continue;
702           MAPSET(&seen, vv);            /* mark that we also need to look at this literal */
703           l = solv->decisionmap[vv];
704           if (l < 0)
705             l = -l;
706           if (l == 1)
707             l1num++;                    /* need to do this one in level1 pass */
708           else if (l == level)
709             num++;                      /* need to do this one as well */
710           else
711             {
712               queue_push(&q, v);        /* not level1 or conflict level, add to new rule */
713               if (l > rlevel)
714                 rlevel = l;
715             }
716         }
717 l1retry:
718       if (!num && !--l1num)
719         break;  /* all literals done */
720
721       /* find the next literal to investigate */
722       /* (as num + l1num > 0, we know that we'll always find one) */
723       for (;;)
724         {
725           assert(idx > 0);
726           v = solv->decisionq.elements[--idx];
727           vv = v > 0 ? v : -v;
728           if (MAPTST(&seen, vv))
729             break;
730         }
731       MAPCLR(&seen, vv);
732
733       if (num && --num == 0)
734         {
735           /* done with normal literals, now start level 1 literal processing */
736           p = -v;       /* so that v doesn't get lost */
737           if (!l1num)
738             break;
739           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
740           /* clear non-l1 bits from seen map */
741           for (i = 0; i < q.count; i++)
742             {
743               v = q.elements[i];
744               MAPCLR(&seen, v > 0 ? v : -v);
745             }
746           /* only level 1 marks left in seen map */
747           l1num++;      /* as l1retry decrements it */
748           goto l1retry;
749         }
750
751       why = solv->decisionq_why.elements[idx];
752       if (why <= 0)     /* just in case, maybe for SYSTEMSOLVABLE */
753         goto l1retry;
754       c = solv->rules + why;
755     }
756   map_free(&seen);
757   assert(p != 0);
758   assert(rlevel > 0 && rlevel < level);
759   IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
760     {
761       POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
762       solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
763       for (i = 0; i < q.count; i++)
764         solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, q.elements[i]);
765     }
766   /* push end marker on learnt reasons stack */
767   queue_push(&solv->learnt_pool, 0);
768   solv->stats_learned++;
769
770   POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, rlevel);
771   level = rlevel;
772   revert(solv, level);
773   if (q.count < 2)
774     {
775       Id d = q.count ? q.elements[0] : 0;
776       queue_free(&q);
777       r = solver_addrule(solv, p, d, 0);
778     }
779   else
780     {
781       Id d = pool_queuetowhatprovides(pool, &q);
782       queue_free(&q);
783       r = solver_addrule(solv, p, 0, d);
784     }
785   assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
786   queue_push(&solv->learnt_why, learnt_why);
787   if (r->w2)
788     {
789       /* needs watches */
790       watch2onhighest(solv, r);
791       addwatches_rule(solv, r);
792     }
793   else
794     {
795       /* rule is an assertion */
796       queue_push(&solv->ruleassertions, r - solv->rules);
797     }
798   *lrp = r;
799   return level;
800 }
801
802
803 /*-------------------------------------------------------------------
804  *
805  * solver_reset
806  *
807  * reset all solver decisions
808  * called after rules have been enabled/disabled
809  */
810
811 void
812 solver_reset(Solver *solv)
813 {
814   int i;
815   Id v;
816
817   /* rewind all decisions */
818   for (i = solv->decisionq.count - 1; i >= 0; i--)
819     {
820       v = solv->decisionq.elements[i];
821       solv->decisionmap[v > 0 ? v : -v] = 0;
822     }
823   queue_empty(&solv->decisionq_why);
824   queue_empty(&solv->decisionq);
825   queue_empty(&solv->decisionq_reason);
826   solv->recommends_index = -1;
827   solv->propagate_index = 0;
828   queue_empty(&solv->branches);
829
830   /* adapt learnt rule status to new set of enabled/disabled rules */
831   enabledisablelearntrules(solv);
832 }
833
834 static inline int
835 queue_contains(Queue *q, Id id)
836 {
837   int i;
838   for (i = 0; i < q->count; i++)
839     if (q->elements[i] == id)
840       return 1;
841   return 0;
842 }
843
844 static void
845 disable_recommendsrules(Solver *solv, Queue *weakq)
846 {
847   Pool *pool = solv->pool;
848   int i, rid;
849   for (i = 0; i < weakq->count; i++)
850     {
851       rid = weakq->elements[i];
852       if ((rid >= solv->recommendsrules && rid < solv->recommendsrules_end) || queue_contains(solv->recommendsruleq, rid))
853         {
854           Rule *r = solv->rules + rid;
855           if (r->d >= 0)
856             {
857               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
858               solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
859               solver_disablerule(solv, r);
860             }
861         }
862     }
863 }
864
865 /*-------------------------------------------------------------------
866  *
867  * analyze_unsolvable_rule
868  *
869  * recursion helper used by analyze_unsolvable
870  */
871
872 static void
873 analyze_unsolvable_rule(Solver *solv, Rule *r, Queue *weakq, Map *rseen)
874 {
875   Pool *pool = solv->pool;
876   int i;
877   Id why = r - solv->rules;
878
879   IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
880     solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
881   if (solv->learntrules && why >= solv->learntrules)
882     {
883       if (MAPTST(rseen, why - solv->learntrules))
884         return;
885       MAPSET(rseen, why - solv->learntrules);
886       for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
887         if (solv->learnt_pool.elements[i] > 0)
888           analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], weakq, rseen);
889       return;
890     }
891   if (solv->weakrulemap.size && MAPTST(&solv->weakrulemap, why) && weakq)
892     queue_push(weakq, why);
893   /* add non-pkg rules to problem and disable */
894   if (why >= solv->pkgrules_end)
895     solver_recordproblem(solv, why);
896 }
897
898 /* fix a problem by disabling one or more weak rules */
899 static void
900 disable_weakrules(Solver *solv, Queue *weakq)
901 {
902   Pool *pool = solv->pool;
903   int i;
904   Id lastweak = 0;
905   for (i = 0; i < weakq->count; i++)
906     if (weakq->elements[i] > lastweak)
907       lastweak = weakq->elements[i];
908   if (lastweak >= solv->recommendsrules && lastweak < solv->recommendsrules_end)
909     {
910       lastweak = 0;
911       for (i = 0; i < weakq->count; i++)
912         if (weakq->elements[i] < solv->recommendsrules && weakq->elements[i] > lastweak)
913           lastweak = weakq->elements[i];
914       if (lastweak < solv->pkgrules_end)
915         {
916           disable_recommendsrules(solv, weakq);
917           return;
918         }
919     }
920   if (lastweak < solv->pkgrules_end && solv->strongrecommends && solv->recommendsruleq && queue_contains(solv->recommendsruleq, lastweak))
921     {
922       disable_recommendsrules(solv, weakq);
923       return;
924     }
925   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
926   solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
927   /* choice rules need special handling */
928   if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
929     solver_disablechoicerules(solv, solv->rules + lastweak);
930   else
931     solver_fixproblem(solv, lastweak);
932 }
933
934 /*-------------------------------------------------------------------
935  *
936  * analyze_unsolvable (called from setpropagatelearn)
937  *
938  * We know that the problem is not solvable. Record all involved
939  * rules (i.e. the "proof") into solv->learnt_pool.
940  * Record the learnt pool index and all non-pkg rules into
941  * solv->problems. (Our solutions to fix the problems are to
942  * disable those rules.)
943  *
944  * If the proof contains at least one weak rule, we disable the
945  * last of them.
946  *
947  * Otherwise we return -1 if disablerules is not set or disable
948  * _all_ of the problem rules and return 0.
949  *
950  * return:  0 - disabled some rules, try again
951  *         -1 - hopeless
952  */
953
954 static int
955 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
956 {
957   Pool *pool = solv->pool;
958   Rule *r;
959   Map involved;         /* global to speed things up? */
960   Map rseen;
961   Queue weakq;
962   Id pp, v, vv, why;
963   int idx;
964   Id *decisionmap = solv->decisionmap;
965   int oldproblemcount;
966   int oldlearntpoolcount;
967   int record_proof = 1;
968
969   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
970   solv->stats_unsolvable++;
971   oldproblemcount = solv->problems.count;
972   oldlearntpoolcount = solv->learnt_pool.count;
973
974   /* make room for proof index */
975   /* must update it later, as analyze_unsolvable_rule would confuse
976    * it with a rule index if we put the real value in already */
977   queue_push(&solv->problems, 0);
978
979   r = cr;
980   map_init(&involved, pool->nsolvables);
981   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
982   queue_init(&weakq);
983   if (record_proof)
984     queue_push(&solv->learnt_pool, r - solv->rules);
985   analyze_unsolvable_rule(solv, r, &weakq, &rseen);
986   FOR_RULELITERALS(v, pp, r)
987     {
988       if (DECISIONMAP_TRUE(v))  /* the one true literal */
989           continue;
990       vv = v > 0 ? v : -v;
991       MAPSET(&involved, vv);
992     }
993   idx = solv->decisionq.count;
994   while (idx > 0)
995     {
996       v = solv->decisionq.elements[--idx];
997       vv = v > 0 ? v : -v;
998       if (!MAPTST(&involved, vv) || vv == SYSTEMSOLVABLE)
999         continue;
1000       why = solv->decisionq_why.elements[idx];
1001       assert(why > 0);
1002       if (record_proof)
1003         queue_push(&solv->learnt_pool, why);
1004       r = solv->rules + why;
1005       analyze_unsolvable_rule(solv, r, &weakq, &rseen);
1006       FOR_RULELITERALS(v, pp, r)
1007         {
1008           if (DECISIONMAP_TRUE(v))      /* the one true literal */
1009               continue;
1010           vv = v > 0 ? v : -v;
1011           MAPSET(&involved, vv);
1012         }
1013     }
1014   map_free(&involved);
1015   map_free(&rseen);
1016   queue_push(&solv->problems, 0);       /* mark end of this problem */
1017
1018   if (weakq.count)
1019     {
1020       /* revert problems */
1021       solv->problems.count = oldproblemcount;
1022       solv->learnt_pool.count = oldlearntpoolcount;
1023       /* disable some weak rules */
1024       disable_weakrules(solv, &weakq);
1025       queue_free(&weakq);
1026       solver_reset(solv);
1027       return 0;
1028     }
1029   queue_free(&weakq);
1030
1031   if (solv->allowuninstall || solv->allowuninstall_all || solv->allowuninstallmap.size)
1032     if (solver_autouninstall(solv, oldproblemcount) != 0)
1033       {
1034         solv->problems.count = oldproblemcount;
1035         solv->learnt_pool.count = oldlearntpoolcount;
1036         solver_reset(solv);
1037         return 0;
1038       }
1039
1040   /* finish proof */
1041   if (record_proof)
1042     {
1043       queue_push(&solv->learnt_pool, 0);
1044       solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1045     }
1046
1047   /* + 2: index + trailing zero */
1048   if (disablerules && oldproblemcount + 2 < solv->problems.count)
1049     {
1050       solver_disableproblemset(solv, oldproblemcount);
1051       /* XXX: might want to enable all weak rules again */
1052       solver_reset(solv);
1053       return 0;
1054     }
1055   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1056   return -1;
1057 }
1058
1059
1060 /*-------------------------------------------------------------------
1061  *
1062  * setpropagatelearn
1063  *
1064  * add free decision (solvable to install) to decisionq
1065  * increase level and propagate decision
1066  * return if no conflict.
1067  *
1068  * in conflict case, analyze conflict rule, add resulting
1069  * rule to learnt rule set, make decision from learnt
1070  * rule (always unit) and re-propagate.
1071  *
1072  * returns the new solver level or -1 if unsolvable
1073  *
1074  */
1075
1076 static int
1077 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid, Id reason)
1078 {
1079   Pool *pool = solv->pool;
1080   Rule *r, *lr;
1081
1082   if (decision)
1083     {
1084       level++;
1085       if (decision > 0)
1086         solv->decisionmap[decision] = level;
1087       else
1088         solv->decisionmap[-decision] = -level;
1089       queue_push(&solv->decisionq, decision);
1090       queue_push(&solv->decisionq_why, -ruleid);        /* <= 0 -> free decision */
1091       queue_push(&solv->decisionq_reason, reason);
1092     }
1093   assert(ruleid >= 0 && level > 0);
1094   for (;;)
1095     {
1096       r = propagate(solv, level);
1097       if (!r)
1098         break;
1099       if (level == 1)
1100         return analyze_unsolvable(solv, r, disablerules);
1101       POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1102       level = analyze(solv, level, r, &lr);
1103       /* the new rule is unit by design */
1104       decision = lr->p;
1105       solv->decisionmap[decision > 0 ? decision : -decision] = decision > 0 ? level : -level;
1106       queue_push(&solv->decisionq, decision);
1107       queue_push(&solv->decisionq_why, lr - solv->rules);
1108       IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1109         {
1110           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1111           solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, decision);
1112           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1113           solver_printrule(solv, SOLV_DEBUG_ANALYZE, lr);
1114         }
1115     }
1116   return level;
1117 }
1118
1119 static void
1120 reorder_dq_for_future_installed(Solver *solv, int level, Queue *dq)
1121 {
1122   Pool *pool = solv->pool;
1123   int i, j, haveone = 0, dqcount = dq->count;
1124   int decisionqcount = solv->decisionq.count;
1125   Id p;
1126   Solvable *s;
1127
1128   /* at the time we process jobrules the installed packages are not kept yet */
1129   /* reorder so that "future-supplemented" packages come first */
1130   FOR_REPO_SOLVABLES(solv->installed, p, s)
1131     {
1132       if (MAPTST(&solv->noupdate, p - solv->installed->start))
1133         continue;
1134       if (solv->decisionmap[p] == 0)
1135         {
1136           if (s->recommends || s->suggests)
1137             queue_push(&solv->decisionq, p);
1138           solv->decisionmap[p] = level + 1;
1139           haveone = 1;
1140         }
1141     }
1142   if (!haveone)
1143     return;
1144   policy_update_recommendsmap(solv);
1145   for (i = 0; i < dqcount; i++)
1146     {
1147       p = dq->elements[i];
1148       if (!(pool->solvables[p].repo == solv->installed || MAPTST(&solv->suggestsmap, p) || solver_is_enhancing(solv, pool->solvables + p)))
1149         {
1150           queue_push(dq, p);
1151           dq->elements[i] = 0;
1152         }
1153     }
1154   dqcount = dq->count;
1155   for (i = 0; i < dqcount; i++)
1156     {
1157       p = dq->elements[i];
1158       if (p && !(pool->solvables[p].repo == solv->installed || MAPTST(&solv->recommendsmap, p) || solver_is_supplementing(solv, pool->solvables + p)))
1159         {
1160           queue_push(dq, p);
1161           dq->elements[i] = 0;
1162         }
1163     }
1164   for (i = j = 0; i < dq->count; i++)
1165     if (dq->elements[i])
1166       dq->elements[j++] = dq->elements[i];
1167   queue_truncate(dq, j);
1168   FOR_REPO_SOLVABLES(solv->installed, p, s)
1169     if (solv->decisionmap[p] == level + 1)
1170       solv->decisionmap[p] = 0;
1171   if (solv->decisionq.count != decisionqcount)
1172     {
1173       solv->recommends_index = -1;
1174       queue_truncate(&solv->decisionq, decisionqcount);
1175     }
1176   /* but obey favored maps */
1177   policy_prefer_favored(solv, dq);
1178 }
1179
1180 /*-------------------------------------------------------------------
1181  *
1182  * branch handling
1183  *
1184  * format is:
1185  *   [ -p1 p2 p3 .. pn  opt_pkg opt_data size level ]
1186  *
1187  * pkgs are negative if we tried them (to prevent inifinite recursion)
1188  * opt_pkg:  recommends: package with the recommends
1189  *           rule: 0
1190  * opt_data: recommends: depid
1191  *           rule: ruleid
1192  */
1193
1194 static void
1195 createbranch(Solver *solv, int level, Queue *dq, Id p, Id data)
1196 {
1197   Pool *pool = solv->pool;
1198   int i;
1199   IF_POOLDEBUG (SOLV_DEBUG_POLICY)
1200     {
1201       POOL_DEBUG (SOLV_DEBUG_POLICY, "creating a branch:\n");
1202       for (i = 0; i < dq->count; i++)
1203         POOL_DEBUG (SOLV_DEBUG_POLICY, "  - %s\n", pool_solvid2str(pool, dq->elements[i]));
1204     }
1205   queue_push(&solv->branches, -dq->elements[0]);
1206   for (i = 1; i < dq->count; i++)
1207     queue_push(&solv->branches, dq->elements[i]);
1208   queue_push2(&solv->branches, p, data);
1209   queue_push2(&solv->branches, dq->count + 4, level);
1210 }
1211
1212 static int
1213 takebranch(Solver *solv, int pos, int end, const char *msg, int disablerules)
1214 {
1215   Pool *pool = solv->pool;
1216   int level;
1217   Id p, why, reason;
1218 #if 0
1219   {
1220     int i;
1221     printf("branch group level %d [%d-%d] %d %d:\n", solv->branches.elements[end - 1], start, end, solv->branches.elements[end - 4], solv->branches.elements[end - 3]);
1222     for (i = end - solv->branches.elements[end - 2]; i < end - 4; i++)
1223       printf("%c %c%s\n", i == pos ? 'x' : ' ', solv->branches.elements[i] >= 0 ? ' ' : '-', pool_solvid2str(pool, solv->branches.elements[i] >= 0 ? solv->branches.elements[i] : -solv->branches.elements[i]));
1224   }
1225 #endif
1226   level = solv->branches.elements[end - 1];
1227   p = solv->branches.elements[pos];
1228   solv->branches.elements[pos] = -p;
1229   POOL_DEBUG(SOLV_DEBUG_SOLVER, "%s %d -> %d with %s\n", msg, solv->decisionmap[p], level, pool_solvid2str(pool, p));
1230   /* hack: set level to zero so that revert does not remove the branch */
1231   solv->branches.elements[end - 1] = 0;
1232   revert(solv, level);
1233   solv->branches.elements[end - 1] = level;
1234   /* hack: revert simply sets the count, so we can still access the reverted elements */
1235   why = -solv->decisionq_why.elements[solv->decisionq_why.count];
1236   assert(why >= 0);
1237   reason = solv->decisionq_reason.elements[level + 1];
1238   return setpropagatelearn(solv, level, p, disablerules, why, reason);
1239 }
1240
1241 /*-------------------------------------------------------------------
1242  *
1243  * select and install
1244  *
1245  * install best package from the queue. We add an extra package, inst, if
1246  * provided. See comment in weak install section.
1247  *
1248  * returns the new solver level or -1 if unsolvable
1249  *
1250  */
1251
1252 static int
1253 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid, Id reason)
1254 {
1255   Pool *pool = solv->pool;
1256   Id p;
1257
1258   if (dq->count > 1)
1259     policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1260   /* if we're resolving rules and didn't resolve the installed packages yet,
1261    * do some special supplements ordering */
1262   if (dq->count > 1 && solv->do_extra_reordering)
1263     reorder_dq_for_future_installed(solv, level, dq);
1264   /* if we have multiple candidates we open a branch */
1265   if (dq->count > 1)
1266     createbranch(solv, level, dq, 0, ruleid);
1267   p = dq->elements[0];
1268   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1269   return setpropagatelearn(solv, level, p, disablerules, ruleid, reason);
1270 }
1271
1272
1273 /********************************************************************/
1274 /* Main solver interface */
1275
1276
1277 /*-------------------------------------------------------------------
1278  *
1279  * solver_create
1280  * create solver structure
1281  *
1282  * pool: all available solvables
1283  * installed: installed Solvables
1284  *
1285  *
1286  * Upon solving, rules are created to flag the Solvables
1287  * of the 'installed' Repo as installed.
1288  */
1289
1290 Solver *
1291 solver_create(Pool *pool)
1292 {
1293   Solver *solv;
1294   solv = (Solver *)solv_calloc(1, sizeof(Solver));
1295   solv->pool = pool;
1296   solv->installed = pool->installed;
1297
1298   solv->allownamechange = 1;
1299
1300   solv->dup_allowdowngrade = 1;
1301   solv->dup_allownamechange = 1;
1302   solv->dup_allowarchchange = 1;
1303   solv->dup_allowvendorchange = 1;
1304
1305   solv->keepexplicitobsoletes = pool->noobsoletesmultiversion ? 0 : 1;
1306
1307   queue_init(&solv->ruletojob);
1308   queue_init(&solv->decisionq);
1309   queue_init(&solv->decisionq_why);
1310   queue_init(&solv->decisionq_reason);
1311   queue_init(&solv->problems);
1312   queue_init(&solv->orphaned);
1313   queue_init(&solv->learnt_why);
1314   queue_init(&solv->learnt_pool);
1315   queue_init(&solv->branches);
1316   queue_init(&solv->weakruleq);
1317   queue_init(&solv->ruleassertions);
1318   queue_init(&solv->addedmap_deduceq);
1319
1320   queue_push(&solv->learnt_pool, 0);    /* so that 0 does not describe a proof */
1321
1322   map_init(&solv->recommendsmap, pool->nsolvables);
1323   map_init(&solv->suggestsmap, pool->nsolvables);
1324   map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1325   solv->recommends_index = 0;
1326
1327   solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1328   solv->nrules = 1;
1329   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1330   memset(solv->rules, 0, sizeof(Rule));
1331
1332   return solv;
1333 }
1334
1335
1336
1337 /*-------------------------------------------------------------------
1338  *
1339  * solver_free
1340  */
1341
1342 static inline void
1343 queuep_free(Queue **qp)
1344 {
1345   if (!*qp)
1346     return;
1347   queue_free(*qp);
1348   *qp = solv_free(*qp);
1349 }
1350
1351 static inline void
1352 map_zerosize(Map *m)
1353 {
1354   if (m->size)
1355     {
1356       map_free(m);
1357       map_init(m, 0);
1358     }
1359 }
1360
1361 void
1362 solver_free(Solver *solv)
1363 {
1364   queue_free(&solv->job);
1365   queue_free(&solv->ruletojob);
1366   queue_free(&solv->decisionq);
1367   queue_free(&solv->decisionq_why);
1368   queue_free(&solv->decisionq_reason);
1369   queue_free(&solv->learnt_why);
1370   queue_free(&solv->learnt_pool);
1371   queue_free(&solv->problems);
1372   queue_free(&solv->solutions);
1373   queue_free(&solv->orphaned);
1374   queue_free(&solv->branches);
1375   queue_free(&solv->weakruleq);
1376   queue_free(&solv->ruleassertions);
1377   queue_free(&solv->addedmap_deduceq);
1378   queuep_free(&solv->cleandeps_updatepkgs);
1379   queuep_free(&solv->cleandeps_mistakes);
1380   queuep_free(&solv->update_targets);
1381   queuep_free(&solv->installsuppdepq);
1382   queuep_free(&solv->recommendscplxq);
1383   queuep_free(&solv->suggestscplxq);
1384   queuep_free(&solv->brokenorphanrules);
1385   queuep_free(&solv->favorq);
1386   queuep_free(&solv->recommendsruleq);
1387
1388   map_free(&solv->recommendsmap);
1389   map_free(&solv->suggestsmap);
1390   map_free(&solv->noupdate);
1391   map_free(&solv->weakrulemap);
1392   map_free(&solv->multiversion);
1393
1394   map_free(&solv->updatemap);
1395   map_free(&solv->bestupdatemap);
1396   map_free(&solv->fixmap);
1397   map_free(&solv->dupmap);
1398   map_free(&solv->dupinvolvedmap);
1399   map_free(&solv->droporphanedmap);
1400   map_free(&solv->cleandepsmap);
1401   map_free(&solv->allowuninstallmap);
1402
1403   solv_free(solv->favormap);
1404   solv_free(solv->decisionmap);
1405   solv_free(solv->rules);
1406   solv_free(solv->watches);
1407   solv_free(solv->obsoletes);
1408   solv_free(solv->obsoletes_data);
1409   solv_free(solv->specialupdaters);
1410   solv_free(solv->choicerules_info);
1411   solv_free(solv->bestrules_info);
1412   solv_free(solv->yumobsrules_info);
1413   solv_free(solv->recommendsrules_info);
1414   solv_free(solv->instbuddy);
1415   solv_free(solv);
1416 }
1417
1418 int
1419 solver_get_flag(Solver *solv, int flag)
1420 {
1421   switch (flag)
1422   {
1423   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1424     return solv->allowdowngrade;
1425   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1426     return solv->allownamechange;
1427   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1428     return solv->allowarchchange;
1429   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1430     return solv->allowvendorchange;
1431   case SOLVER_FLAG_ALLOW_UNINSTALL:
1432     return solv->allowuninstall;
1433   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1434     return solv->noupdateprovide;
1435   case SOLVER_FLAG_SPLITPROVIDES:
1436     return solv->dosplitprovides;
1437   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1438     return solv->dontinstallrecommended;
1439   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1440     return solv->addalreadyrecommended;
1441   case SOLVER_FLAG_NO_INFARCHCHECK:
1442     return solv->noinfarchcheck;
1443   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1444     return solv->keepexplicitobsoletes;
1445   case SOLVER_FLAG_BEST_OBEY_POLICY:
1446     return solv->bestobeypolicy;
1447   case SOLVER_FLAG_NO_AUTOTARGET:
1448     return solv->noautotarget;
1449   case SOLVER_FLAG_DUP_ALLOW_DOWNGRADE:
1450     return solv->dup_allowdowngrade;
1451   case SOLVER_FLAG_DUP_ALLOW_NAMECHANGE:
1452     return solv->dup_allownamechange;
1453   case SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE:
1454     return solv->dup_allowarchchange;
1455   case SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE:
1456     return solv->dup_allowvendorchange;
1457   case SOLVER_FLAG_KEEP_ORPHANS:
1458     return solv->keep_orphans;
1459   case SOLVER_FLAG_BREAK_ORPHANS:
1460     return solv->break_orphans;
1461   case SOLVER_FLAG_FOCUS_INSTALLED:
1462     return solv->focus_installed;
1463   case SOLVER_FLAG_FOCUS_BEST:
1464     return solv->focus_best;
1465   case SOLVER_FLAG_YUM_OBSOLETES:
1466     return solv->do_yum_obsoletes;
1467   case SOLVER_FLAG_NEED_UPDATEPROVIDE:
1468     return solv->needupdateprovide;
1469   case SOLVER_FLAG_URPM_REORDER:
1470     return solv->urpmreorder;
1471   case SOLVER_FLAG_STRONG_RECOMMENDS:
1472     return solv->strongrecommends;
1473   case SOLVER_FLAG_INSTALL_ALSO_UPDATES:
1474     return solv->install_also_updates;
1475   case SOLVER_FLAG_ONLY_NAMESPACE_RECOMMENDED:
1476     return solv->only_namespace_recommended;
1477   default:
1478     break;
1479   }
1480   return -1;
1481 }
1482
1483 int
1484 solver_set_flag(Solver *solv, int flag, int value)
1485 {
1486   int old = solver_get_flag(solv, flag);
1487   switch (flag)
1488   {
1489   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1490     solv->allowdowngrade = value;
1491     break;
1492   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1493     solv->allownamechange = value;
1494     break;
1495   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1496     solv->allowarchchange = value;
1497     break;
1498   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1499     solv->allowvendorchange = value;
1500     break;
1501   case SOLVER_FLAG_ALLOW_UNINSTALL:
1502     solv->allowuninstall = value;
1503     break;
1504   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1505     solv->noupdateprovide = value;
1506     break;
1507   case SOLVER_FLAG_SPLITPROVIDES:
1508     solv->dosplitprovides = value;
1509     break;
1510   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1511     solv->dontinstallrecommended = value;
1512     break;
1513   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1514     solv->addalreadyrecommended = value;
1515     break;
1516   case SOLVER_FLAG_NO_INFARCHCHECK:
1517     solv->noinfarchcheck = value;
1518     break;
1519   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1520     solv->keepexplicitobsoletes = value;
1521     break;
1522   case SOLVER_FLAG_BEST_OBEY_POLICY:
1523     solv->bestobeypolicy = value;
1524     break;
1525   case SOLVER_FLAG_NO_AUTOTARGET:
1526     solv->noautotarget = value;
1527     break;
1528   case SOLVER_FLAG_DUP_ALLOW_DOWNGRADE:
1529     solv->dup_allowdowngrade = value;
1530     break;
1531   case SOLVER_FLAG_DUP_ALLOW_NAMECHANGE:
1532     solv->dup_allownamechange = value;
1533     break;
1534   case SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE:
1535     solv->dup_allowarchchange = value;
1536     break;
1537   case SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE:
1538     solv->dup_allowvendorchange = value;
1539     break;
1540   case SOLVER_FLAG_KEEP_ORPHANS:
1541     solv->keep_orphans = value;
1542     break;
1543   case SOLVER_FLAG_BREAK_ORPHANS:
1544     solv->break_orphans = value;
1545     break;
1546   case SOLVER_FLAG_FOCUS_INSTALLED:
1547     solv->focus_installed = value;
1548     break;
1549   case SOLVER_FLAG_FOCUS_BEST:
1550     solv->focus_best = value;
1551     break;
1552   case SOLVER_FLAG_YUM_OBSOLETES:
1553     solv->do_yum_obsoletes = value;
1554     break;
1555   case SOLVER_FLAG_NEED_UPDATEPROVIDE:
1556     solv->needupdateprovide = value;
1557     break;
1558   case SOLVER_FLAG_URPM_REORDER:
1559     solv->urpmreorder = value;
1560     break;
1561   case SOLVER_FLAG_STRONG_RECOMMENDS:
1562     solv->strongrecommends = value;
1563     break;
1564   case SOLVER_FLAG_INSTALL_ALSO_UPDATES:
1565     solv->install_also_updates = value;
1566     break;
1567   case SOLVER_FLAG_ONLY_NAMESPACE_RECOMMENDED:
1568     solv->only_namespace_recommended = value;
1569     break;
1570   default:
1571     break;
1572   }
1573   return old;
1574 }
1575
1576 static int
1577 resolve_jobrules(Solver *solv, int level, int disablerules, Queue *dq)
1578 {
1579   Pool *pool = solv->pool;
1580   int oldlevel = level;
1581   int i, olevel;
1582   Rule *r;
1583
1584   POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1585   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1586     {
1587       Id l, pp;
1588       if (r->d < 0)             /* ignore disabled rules */
1589         continue;
1590       queue_empty(dq);
1591       FOR_RULELITERALS(l, pp, r)
1592         {
1593           if (l < 0)
1594             {
1595               if (solv->decisionmap[-l] <= 0)
1596                 break;
1597             }
1598           else
1599             {
1600               if (solv->decisionmap[l] > 0)
1601                 break;
1602               if (solv->decisionmap[l] == 0)
1603                 queue_push(dq, l);
1604             }
1605         }
1606       if (l || !dq->count)
1607         continue;
1608       /* prune to installed if not updating */
1609       if (dq->count > 1 && solv->installed && !solv->updatemap_all &&
1610           !solv->install_also_updates &&
1611           !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1612         {
1613           int j = dq->count, k;
1614           if (solv->updatemap.size)
1615             {
1616               /* do not prune if an installed package wants to be updated */
1617               for (j = 0; j < dq->count; j++)
1618                 if (pool->solvables[dq->elements[j]].repo == solv->installed
1619                     && MAPTST(&solv->updatemap, dq->elements[j] - solv->installed->start))
1620                   break;
1621             }
1622           if (j == dq->count)
1623             {
1624               for (j = k = 0; j < dq->count; j++)
1625                 if (pool->solvables[dq->elements[j]].repo == solv->installed)
1626                   dq->elements[k++] = dq->elements[j];
1627               if (k)
1628                 dq->count = k;
1629             }
1630         }
1631       olevel = level;
1632       level = selectandinstall(solv, level, dq, disablerules, i, SOLVER_REASON_RESOLVE_JOB);
1633       if (level <= olevel)
1634         {
1635           if (level == olevel)
1636             {
1637               i--;
1638               r--;
1639               continue; /* try something else */
1640             }
1641           if (level < oldlevel)
1642             return level;
1643           /* redo from start of jobrules */
1644           i = solv->jobrules - 1;
1645           r = solv->rules + i;
1646         }
1647     }
1648   return level;
1649 }
1650
1651 static void
1652 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1653 {
1654   int i, j;
1655   Id p, *cp2;
1656   for (i = j = 0; i < q->count; i++)
1657     {
1658       p = q->elements[i];
1659       for (cp2 = cp; *cp2; cp2++)
1660         if (*cp2 == p)
1661           {
1662             q->elements[j++] = p;
1663             break;
1664           }
1665     }
1666   queue_truncate(q, j);
1667 }
1668
1669 static int
1670 resolve_installed(Solver *solv, int level, int disablerules, Queue *dq)
1671 {
1672   Pool *pool = solv->pool;
1673   Repo *installed = solv->installed;
1674   int i, n, pass;
1675   int installedpos = solv->installedpos;
1676   Solvable *s;
1677   Id p, pp;
1678   int olevel, origlevel = level;
1679
1680   POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1681   if (!installedpos)
1682     installedpos = installed->start;
1683   /* we use two passes if we need to update packages
1684    * to create a better user experience */
1685   for (pass = solv->updatemap.size ? 0 : 1; pass < 2; )
1686     {
1687       int passlevel = level;
1688       Id *specialupdaters = solv->specialupdaters;
1689       /* start with installedpos, the position that gave us problems the last time */
1690       for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1691         {
1692           Rule *r, *rr;
1693           Id d;
1694
1695           if (i == installed->end)
1696             i = installed->start;
1697           s = pool->solvables + i;
1698           if (s->repo != installed)
1699             continue;
1700
1701           if (solv->decisionmap[i] > 0 && (!specialupdaters || !specialupdaters[i - installed->start]))
1702             continue;           /* already decided */
1703           if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1704             continue;           /* updates first */
1705           r = solv->rules + solv->updaterules + (i - installed->start);
1706           rr = r;
1707           if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1708             rr -= solv->installed->end - solv->installed->start;
1709           if (!rr->p)           /* identical to update rule? */
1710             rr = r;
1711           if (!rr->p)
1712             continue;           /* orpaned package or pseudo package */
1713
1714           /* check if we should update this package to the latest version
1715            * noupdate is set for erase jobs, in that case we want to deinstall
1716            * the installed package and not replace it with a newer version
1717            * rr->p != i is for dup jobs where the installed package cannot be kept */
1718           if (dq->count)
1719             queue_empty(dq);
1720           if (!MAPTST(&solv->noupdate, i - installed->start) && (solv->decisionmap[i] < 0 || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || (rr->p && rr->p != i)))
1721             {
1722               if (specialupdaters && (d = specialupdaters[i - installed->start]) != 0)
1723                 {
1724                   /* special multiversion handling, make sure best version is chosen */
1725                   if (rr->p == i && solv->decisionmap[i] >= 0)
1726                     queue_push(dq, i);
1727                   while ((p = pool->whatprovidesdata[d++]) != 0)
1728                     if (solv->decisionmap[p] >= 0)
1729                       queue_push(dq, p);
1730                   if (dq->count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1731                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], dq);
1732                   if (dq->count)
1733                     {
1734                       policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1735                       p = dq->elements[0];
1736                       if (p != i && solv->decisionmap[p] == 0)
1737                         {
1738                           rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1739                           if (!rr->p)           /* update rule == feature rule? */
1740                             rr = rr - solv->featurerules + solv->updaterules;
1741                           dq->count = 1;
1742                         }
1743                       else
1744                         dq->count = 0;
1745                     }
1746                 }
1747               else
1748                 {
1749                   /* update to best package of the update rule */
1750                   FOR_RULELITERALS(p, pp, rr)
1751                     {
1752                       if (solv->decisionmap[p] > 0)
1753                         {
1754                           dq->count = 0;                /* already fulfilled */
1755                           break;
1756                         }
1757                       if (!solv->decisionmap[p])
1758                         queue_push(dq, p);
1759                     }
1760                 }
1761             }
1762           if (dq->count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1763             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], dq);
1764           /* install best version */
1765           if (dq->count)
1766             {
1767               olevel = level;
1768               level = selectandinstall(solv, level, dq, disablerules, rr - solv->rules, SOLVER_REASON_UPDATE_INSTALLED);
1769               if (level <= olevel)
1770                 {
1771                   if (level < passlevel)
1772                     break;      /* trouble */
1773                   if (level < olevel)
1774                     n = installed->start;       /* redo all */
1775                   i--;
1776                   n--;
1777                   continue;
1778                 }
1779             }
1780           /* if still undecided keep package */
1781           if (solv->decisionmap[i] == 0)
1782             {
1783               olevel = level;
1784               if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1785                 {
1786 #if 0
1787                   POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1788                   level = setpropagatelearn(solv, level, -i, disablerules, 0, SOLVER_REASON_CLEANDEPS_ERASE);
1789 #else
1790                   continue;
1791 #endif
1792                 }
1793               else
1794                 {
1795                   POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1796                   level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules, SOLVER_REASON_KEEP_INSTALLED);
1797                 }
1798               if (level <= olevel)
1799                 {
1800                   if (level < passlevel)
1801                     break;      /* trouble */
1802                   if (level < olevel)
1803                     n = installed->start;       /* redo all */
1804                   i--;
1805                   n--;
1806                   continue;     /* retry with learnt rule */
1807                 }
1808             }
1809         }
1810       if (n < installed->end)
1811         {
1812           installedpos = i;     /* retry problem solvable next time */
1813           if (level < origlevel)
1814             break;              /* ran into trouble */
1815           /* re-run all passes */
1816           pass = solv->updatemap.size ? 0 : 1;
1817           continue;
1818         }
1819       /* reset installedpos, advance to next pass */
1820       installedpos = installed->start;
1821       pass++;
1822     }
1823   solv->installedpos = installedpos;
1824   return level;
1825 }
1826
1827 static int
1828 resolve_dependencies(Solver *solv, int level, int disablerules, Queue *dq)
1829 {
1830   Pool *pool = solv->pool;
1831   int i, j, n;
1832   int postponed;
1833   Rule *r;
1834   int origlevel = level;
1835   Id p, *dp;
1836
1837   /*
1838    * decide
1839    */
1840   POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1841   postponed = 0;
1842   for (i = 1, n = 1; ; i++, n++)
1843     {
1844       if (n >= solv->nrules)
1845         {
1846           if (postponed <= 0)
1847             break;
1848           i = postponed;
1849           postponed = -1;
1850           n = 1;
1851         }
1852       if (i == solv->nrules)
1853         i = 1;
1854       r = solv->rules + i;
1855       if (r->d < 0)             /* ignore disabled rules */
1856         continue;
1857       if (r->p < 0)             /* most common cases first */
1858         {
1859           if (r->d == 0 || solv->decisionmap[-r->p] <= 0)
1860             continue;
1861         }
1862       if (dq->count)
1863         queue_empty(dq);
1864       if (r->d == 0)
1865         {
1866           /* binary or unary rule */
1867           /* need two positive undecided literals, r->p already checked above */
1868           if (r->w2 <= 0)
1869             continue;
1870           if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1871             continue;
1872           queue_push(dq, r->p);
1873           queue_push(dq, r->w2);
1874         }
1875       else
1876         {
1877           /* make sure that
1878            * all negative literals are installed
1879            * no positive literal is installed
1880            * i.e. the rule is not fulfilled and we
1881            * just need to decide on the positive literals
1882            * (decisionmap[-r->p] for the r->p < 0 case is already checked above)
1883            */
1884           if (r->p >= 0)
1885             {
1886               if (solv->decisionmap[r->p] > 0)
1887                 continue;
1888               if (solv->decisionmap[r->p] == 0)
1889                 queue_push(dq, r->p);
1890             }
1891           dp = pool->whatprovidesdata + r->d;
1892           while ((p = *dp++) != 0)
1893             {
1894               if (p < 0)
1895                 {
1896                   if (solv->decisionmap[-p] <= 0)
1897                     break;
1898                 }
1899               else
1900                 {
1901                   if (solv->decisionmap[p] > 0)
1902                     break;
1903                   if (solv->decisionmap[p] == 0)
1904                     queue_push(dq, p);
1905                 }
1906             }
1907           if (p)
1908             continue;
1909         }
1910       IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1911         {
1912           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1913           solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1914         }
1915       /* dq->count < 2 cannot happen as this means that
1916        * the rule is unit */
1917       assert(dq->count > 1);
1918
1919       /* prune to cleandeps packages */
1920       if (solv->cleandepsmap.size && solv->installed)
1921         {
1922           Repo *installed = solv->installed;
1923           for (j = 0; j < dq->count; j++)
1924             if (pool->solvables[dq->elements[j]].repo == installed && MAPTST(&solv->cleandepsmap, dq->elements[j] - installed->start))
1925               break;
1926           if (j < dq->count)
1927             {
1928               dq->elements[0] = dq->elements[j];
1929               queue_truncate(dq, 1);
1930             }
1931         }
1932
1933       if (dq->count > 1 && postponed >= 0)
1934         {
1935           policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE_NOREORDER);
1936           if (dq->count > 1)
1937             {
1938               if (!postponed)
1939                 postponed = i;
1940               continue;
1941             }
1942         }
1943
1944       level = selectandinstall(solv, level, dq, disablerules, r - solv->rules, SOLVER_REASON_RESOLVE);
1945       if (level < origlevel)
1946         break;          /* trouble */
1947       /* something changed, so look at all rules again */
1948       n = 0;
1949     }
1950   return level;
1951 }
1952
1953
1954 #ifdef ENABLE_COMPLEX_DEPS
1955
1956 static void
1957 add_complex_recommends(Solver *solv, Id rec, Queue *dq, Map *dqmap)
1958 {
1959   Pool *pool = solv->pool;
1960   int oldcnt = dq->count;
1961   int cutcnt, blkcnt;
1962   Id p;
1963   int i, j;
1964
1965 #if 0
1966   printf("ADD_COMPLEX_RECOMMENDS %s\n", pool_dep2str(pool, rec));
1967 #endif
1968   i = pool_normalize_complex_dep(pool, rec, dq, CPLXDEPS_EXPAND);
1969   if (i == 0 || i == 1)
1970     return;
1971   cutcnt = dq->count;
1972   for (i = oldcnt; i < cutcnt; i++)
1973     {
1974       blkcnt = dq->count;
1975       for (; (p = dq->elements[i]) != 0; i++)
1976         {
1977           if (p < 0)
1978             {
1979               if (solv->decisionmap[-p] <= 0)
1980                 break;
1981               continue;
1982             }
1983           if (solv->decisionmap[p] > 0)
1984             {
1985               queue_truncate(dq, blkcnt);
1986               break;
1987             }
1988           if (dqmap)
1989             {
1990               if (!MAPTST(dqmap, p))
1991                 continue;
1992             }
1993           else
1994             {
1995               if (solv->decisionmap[p] < 0)
1996                 continue;
1997               if (solv->process_orphans && solv->installed && pool->solvables[p].repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
1998                 continue;
1999             }
2000           queue_push(dq, p);
2001         }
2002       while (dq->elements[i])
2003         i++;
2004     }
2005   queue_deleten(dq, oldcnt, cutcnt - oldcnt);
2006   /* unify */
2007   if (dq->count != oldcnt)
2008     {
2009       for (j = oldcnt; j < dq->count; j++)
2010         {
2011           p = dq->elements[j];
2012           for (i = 0; i < j; i++)
2013             if (dq->elements[i] == p)
2014               {
2015                 dq->elements[j] = 0;
2016                 break;
2017               }
2018         }
2019       for (i = j = oldcnt; j < dq->count; j++)
2020         if (dq->elements[j])
2021           dq->elements[i++] = dq->elements[j];
2022       queue_truncate(dq, i);
2023     }
2024 #if 0
2025   printf("RETURN:\n");
2026   for (i = oldcnt; i < dq->count; i++)
2027     printf("  - %s\n", pool_solvid2str(pool, dq->elements[i]));
2028 #endif
2029 }
2030
2031 static void
2032 do_complex_recommendations(Solver *solv, Id rec, Map *m, int noselected)
2033 {
2034   Pool *pool = solv->pool;
2035   Queue dq;
2036   Id p;
2037   int i, blk;
2038
2039 #if 0
2040   printf("DO_COMPLEX_RECOMMENDATIONS %s\n", pool_dep2str(pool, rec));
2041 #endif
2042   queue_init(&dq);
2043   i = pool_normalize_complex_dep(pool, rec, &dq, CPLXDEPS_EXPAND);
2044   if (i == 0 || i == 1)
2045     {
2046       queue_free(&dq);
2047       return;
2048     }
2049   for (i = 0; i < dq.count; i++)
2050     {
2051       blk = i;
2052       for (; (p = dq.elements[i]) != 0; i++)
2053         {
2054           if (p < 0)
2055             {
2056               if (solv->decisionmap[-p] <= 0)
2057                 break;
2058               continue;
2059             }
2060           if (solv->decisionmap[p] > 0)
2061             {
2062               if (noselected)
2063                 break;
2064               MAPSET(m, p);
2065               for (i++; (p = dq.elements[i]) != 0; i++)
2066                 if (p > 0 && solv->decisionmap[p] > 0)
2067                   MAPSET(m, p);
2068               p = 1;
2069               break;
2070             }
2071         }
2072       if (!p)
2073         {
2074           for (i = blk; (p = dq.elements[i]) != 0; i++)
2075             if (p > 0)
2076               MAPSET(m, p);
2077         }
2078       while (dq.elements[i])
2079         i++;
2080     }
2081   queue_free(&dq);
2082 }
2083
2084 #endif
2085
2086 static void
2087 prune_disfavored(Solver *solv, Queue *plist)
2088 {
2089   int i, j;
2090   for (i = j = 0; i < plist->count; i++)
2091     {
2092       Id p = plist->elements[i];
2093       if (solv->favormap[p] >= 0)
2094         plist->elements[j++] = p;
2095     }
2096   if (i != j)
2097     queue_truncate(plist, j);
2098 }
2099
2100 static int
2101 resolve_weak(Solver *solv, int level, int disablerules, Queue *dq, Queue *dqs, int *rerunp)
2102 {
2103   Pool *pool = solv->pool;
2104   int i, j, qcount;
2105   int olevel;
2106   Solvable *s;
2107   Map dqmap;
2108   int decisioncount;
2109   Id p;
2110
2111   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2112   if (dq->count)
2113     queue_empty(dq);    /* recommended packages */
2114   if (dqs->count)
2115     queue_empty(dqs);   /* supplemented packages */
2116   for (i = 1; i < pool->nsolvables; i++)
2117     {
2118       if (solv->decisionmap[i] < 0)
2119         continue;
2120       s = pool->solvables + i;
2121       if (solv->decisionmap[i] > 0)
2122         {
2123           /* installed, check for recommends */
2124           Id *recp, rec, pp, p;
2125           if (!solv->addalreadyrecommended && s->repo == solv->installed)
2126             continue;
2127           if (s->recommends)
2128             {
2129               recp = s->repo->idarraydata + s->recommends;
2130               while ((rec = *recp++) != 0)
2131                 {
2132                   /* cheat: we just look if there is REL_NAMESPACE in the dep */
2133                   if (solv->only_namespace_recommended && !solver_is_namespace_dep(solv, rec))
2134                     continue;
2135 #ifdef ENABLE_COMPLEX_DEPS
2136                   if (pool_is_complex_dep(pool, rec))
2137                     {
2138                       add_complex_recommends(solv, rec, dq, 0);
2139                       continue;
2140                     }
2141 #endif
2142                   qcount = dq->count;
2143                   FOR_PROVIDES(p, pp, rec)
2144                     {
2145                       if (solv->decisionmap[p] > 0)
2146                         {
2147                           dq->count = qcount;
2148                           break;
2149                         }
2150                       else if (solv->decisionmap[p] == 0)
2151                         {
2152                           if (solv->process_orphans && solv->installed && pool->solvables[p].repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
2153                             continue;
2154                           queue_pushunique(dq, p);
2155                         }
2156                     }
2157                 }
2158             }
2159         }
2160       else
2161         {
2162           /* not yet installed, check if supplemented */
2163           if (!s->supplements)
2164             continue;
2165           if (!pool_installable(pool, s))
2166             continue;
2167           if (!solver_is_supplementing(solv, s))
2168             continue;
2169           if (solv->process_orphans && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2170             continue;
2171           if (solv->havedisfavored && solv->favormap[i] < 0)
2172             continue;   /* disfavored supplements, do not install */
2173           queue_push(dqs, i);
2174         }
2175     }
2176
2177   /* filter out disfavored recommended packages */
2178   if (dq->count && solv->havedisfavored)
2179     prune_disfavored(solv, dq);
2180
2181   /* filter out all packages obsoleted by installed packages */
2182   /* this is no longer needed if we have (and trust) reverse obsoletes */
2183   if ((dqs->count || dq->count) && solv->installed)
2184     {
2185       Map obsmap;
2186       Id obs, *obsp, po, ppo;
2187
2188       map_init(&obsmap, pool->nsolvables);
2189       for (p = solv->installed->start; p < solv->installed->end; p++)
2190         {
2191           s = pool->solvables + p;
2192           if (s->repo != solv->installed || !s->obsoletes)
2193             continue;
2194           if (solv->decisionmap[p] <= 0)
2195             continue;
2196           if (!solv->keepexplicitobsoletes && solv->multiversion.size && MAPTST(&solv->multiversion, p))
2197             continue;
2198           obsp = s->repo->idarraydata + s->obsoletes;
2199           /* foreach obsoletes */
2200           while ((obs = *obsp++) != 0)
2201             FOR_PROVIDES(po, ppo, obs)
2202               {
2203                 Solvable *pos = pool->solvables + po;
2204                 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pos, obs))
2205                   continue;
2206                 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, pos))
2207                   continue;
2208                 MAPSET(&obsmap, po);
2209               }
2210         }
2211       for (i = j = 0; i < dqs->count; i++)
2212         if (!MAPTST(&obsmap, dqs->elements[i]))
2213           dqs->elements[j++] = dqs->elements[i];
2214       dqs->count = j;
2215       for (i = j = 0; i < dq->count; i++)
2216         if (!MAPTST(&obsmap, dq->elements[i]))
2217           dq->elements[j++] = dq->elements[i];
2218       dq->count = j;
2219       map_free(&obsmap);
2220     }
2221
2222   /* filter out all already supplemented packages if requested */
2223   if ((!solv->addalreadyrecommended || solv->only_namespace_recommended) && dqs->count)
2224     {
2225       /* filter out old supplements */
2226       for (i = j = 0; i < dqs->count; i++)
2227         {
2228           p = dqs->elements[i];
2229           s = pool->solvables + p;
2230           if (s->supplements && solver_is_supplementing_alreadyinstalled(solv, s))
2231             dqs->elements[j++] = p;
2232         }
2233       dqs->count = j;
2234     }
2235
2236   /* multiversion doesn't mix well with supplements.
2237    * filter supplemented packages where we already decided
2238    * to install a different version (see bnc#501088) */
2239   if (dqs->count && solv->multiversion.size)
2240     {
2241       for (i = j = 0; i < dqs->count; i++)
2242         {
2243           p = dqs->elements[i];
2244           if (MAPTST(&solv->multiversion, p))
2245             {
2246               Id p2, pp2;
2247               s = pool->solvables + p;
2248               FOR_PROVIDES(p2, pp2, s->name)
2249                 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2250                   break;
2251               if (p2)
2252                 continue;       /* ignore this package */
2253             }
2254           dqs->elements[j++] = p;
2255         }
2256       dqs->count = j;
2257     }
2258
2259   /* implicitobsoleteusescolors doesn't mix well with supplements.
2260    * filter supplemented packages where we already decided
2261    * to install a different architecture */
2262   if (dqs->count && pool->implicitobsoleteusescolors)
2263     {
2264       for (i = j = 0; i < dqs->count; i++)
2265         {
2266           Id p2, pp2;
2267           p = dqs->elements[i];
2268           s = pool->solvables + p;
2269           FOR_PROVIDES(p2, pp2, s->name)
2270             if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name && pool->solvables[p2].arch != s->arch)
2271               break;
2272           if (p2)
2273             continue;   /* ignore this package */
2274           dqs->elements[j++] = p;
2275         }
2276       dqs->count = j;
2277     }
2278
2279   /* make dq contain both recommended and supplemented pkgs */
2280   if (dqs->count)
2281     {
2282       for (i = 0; i < dqs->count; i++)
2283         queue_pushunique(dq, dqs->elements[i]);
2284     }
2285
2286   if (!dq->count)
2287     return level;
2288   *rerunp = 1;
2289
2290   if (dq->count == 1)
2291     {
2292       /* simple case, just one package. no need to choose to best version */
2293       p = dq->elements[0];
2294       if (dqs->count)
2295         POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2296       else
2297         POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2298       return setpropagatelearn(solv, level, p, 0, 0, SOLVER_REASON_WEAKDEP);
2299     }
2300
2301   /* filter packages, this gives us the best versions */
2302   policy_filter_unwanted(solv, dq, POLICY_MODE_RECOMMEND);
2303
2304   /* create map of result */
2305   map_init(&dqmap, pool->nsolvables);
2306   for (i = 0; i < dq->count; i++)
2307     MAPSET(&dqmap, dq->elements[i]);
2308
2309   /* prune dqs so that it only contains the best versions */
2310   for (i = j = 0; i < dqs->count; i++)
2311     {
2312       p = dqs->elements[i];
2313       if (MAPTST(&dqmap, p))
2314         dqs->elements[j++] = p;
2315     }
2316   dqs->count = j;
2317
2318   /* install all supplemented packages, but order first */
2319   if (dqs->count > 1)
2320     policy_filter_unwanted(solv, dqs, POLICY_MODE_SUPPLEMENT);
2321   decisioncount = solv->decisionq.count;
2322   for (i = 0; i < dqs->count; i++)
2323     {
2324       p = dqs->elements[i];
2325       if (solv->decisionmap[p])
2326         continue;
2327       POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2328       olevel = level;
2329       level = setpropagatelearn(solv, level, p, 0, 0, SOLVER_REASON_WEAKDEP);
2330       if (level <= olevel)
2331         break;
2332     }
2333   if (i < dqs->count || solv->decisionq.count < decisioncount)
2334     {
2335       map_free(&dqmap);
2336       return level;
2337     }
2338
2339   /* install all recommended packages */
2340   /* more work as we want to created branches if multiple
2341    * choices are valid */
2342   for (i = 0; i < decisioncount; i++)
2343     {
2344       Id rec, *recp, pp;
2345       p = solv->decisionq.elements[i];
2346       if (p < 0)
2347         continue;
2348       s = pool->solvables + p;
2349       if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2350         continue;
2351       if (!s->recommends)
2352         continue;
2353       recp = s->repo->idarraydata + s->recommends;
2354       while ((rec = *recp++) != 0)
2355         {
2356           queue_empty(dq);
2357 #ifdef ENABLE_COMPLEX_DEPS
2358           if (pool_is_complex_dep(pool, rec))
2359               add_complex_recommends(solv, rec, dq, &dqmap);
2360           else
2361 #endif
2362           FOR_PROVIDES(p, pp, rec)
2363             {
2364               if (solv->decisionmap[p] > 0)
2365                 {
2366                   dq->count = 0;
2367                   break;
2368                 }
2369               else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2370                 queue_push(dq, p);
2371             }
2372           if (!dq->count)
2373             continue;
2374           if (dq->count > 1)
2375             policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
2376           /* if we have multiple candidates we open a branch */
2377           if (dq->count > 1)
2378             createbranch(solv, level, dq, s - pool->solvables, rec);
2379           p = dq->elements[0];
2380           POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2381           olevel = level;
2382           level = setpropagatelearn(solv, level, p, 0, 0, SOLVER_REASON_WEAKDEP);
2383           if (level <= olevel || solv->decisionq.count < decisioncount)
2384             break;      /* we had to revert some decisions */
2385         }
2386       if (rec)
2387         break;  /* had a problem above, quit loop */
2388     }
2389   map_free(&dqmap);
2390   return level;
2391 }
2392
2393 static int
2394 resolve_cleandeps(Solver *solv, int level, int disablerules, int *rerunp)
2395 {
2396   Pool *pool = solv->pool;
2397   Repo *installed = solv->installed;
2398   int olevel;
2399   Id p;
2400   Solvable *s;
2401
2402   if (!installed || !solv->cleandepsmap.size)
2403     return level;
2404   POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding cleandeps packages\n");
2405   for (p = installed->start; p < installed->end; p++)
2406     {
2407       s = pool->solvables + p;
2408       if (s->repo != installed)
2409         continue;
2410       if (solv->decisionmap[p] != 0 || !MAPTST(&solv->cleandepsmap, p - installed->start))
2411         continue;
2412       POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, p));
2413       olevel = level;
2414       level = setpropagatelearn(solv, level, -p, 0, 0, SOLVER_REASON_CLEANDEPS_ERASE);
2415       if (level < olevel)
2416         break;
2417     }
2418   if (p < installed->end)
2419     *rerunp = 1;
2420   return level;
2421 }
2422
2423 static int
2424 resolve_orphaned(Solver *solv, int level, int disablerules, Queue *dq, int *rerunp)
2425 {
2426   Pool *pool = solv->pool;
2427   int i;
2428   Id p;
2429   int installedone = 0;
2430   int olevel;
2431
2432   /* let's see if we can install some unsupported package */
2433   POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2434   for (i = 0; i < solv->orphaned.count; i++)
2435     {
2436       p = solv->orphaned.elements[i];
2437       if (solv->decisionmap[p])
2438         continue;       /* already decided */
2439       if (solv->droporphanedmap_all)
2440         continue;
2441       if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2442         continue;
2443       POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2444       olevel = level;
2445       level = setpropagatelearn(solv, level, p, 0, 0, SOLVER_REASON_RESOLVE_ORPHAN);
2446       installedone = 1;
2447       if (level < olevel)
2448         break;
2449     }
2450   if (installedone || i < solv->orphaned.count)
2451     {
2452       *rerunp = 1;
2453       return level;
2454     }
2455   for (i = 0; i < solv->orphaned.count; i++)
2456     {
2457       p = solv->orphaned.elements[i];
2458       if (solv->decisionmap[p])
2459         continue;       /* already decided */
2460       POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2461       olevel = level;
2462       level = setpropagatelearn(solv, level, -p, 0, 0, SOLVER_REASON_RESOLVE_ORPHAN);
2463       if (level < olevel)
2464         {
2465           *rerunp = 1;
2466           return level;
2467         }
2468     }
2469   if (solv->brokenorphanrules)
2470     {
2471       solver_check_brokenorphanrules(solv, dq);
2472       if (dq->count)
2473         {
2474           policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
2475           for (i = 0; i < dq->count; i++)
2476             {
2477               p = dq->elements[i];
2478               POOL_DEBUG(SOLV_DEBUG_POLICY, "installing orphaned dep %s\n", pool_solvid2str(pool, p));
2479               olevel = level;
2480               level = setpropagatelearn(solv, level, p, 0, 0, SOLVER_REASON_RESOLVE_ORPHAN);
2481               if (level < olevel)
2482                 break;
2483             }
2484           *rerunp = 1;
2485           return level;
2486         }
2487     }
2488   return level;
2489 }
2490
2491 /*-------------------------------------------------------------------
2492  *
2493  * solver_run_sat
2494  *
2495  * all rules have been set up, now actually run the solver
2496  *
2497  */
2498
2499 void
2500 solver_run_sat(Solver *solv, int disablerules, int doweak)
2501 {
2502   Queue dq;             /* local decisionqueue */
2503   Queue dqs;            /* local decisionqueue for supplements */
2504   int systemlevel;
2505   int level, olevel;
2506   Rule *r;
2507   int i;
2508   Solvable *s;
2509   Pool *pool = solv->pool;
2510   Id p;
2511   int minimizationsteps;
2512
2513   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
2514     {
2515       POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2516       for (i = 1; i < solv->nrules; i++)
2517         solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
2518     }
2519
2520   /* start SAT algorithm */
2521   level = 0;
2522   systemlevel = level + 1;
2523   POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
2524
2525   queue_init(&dq);
2526   queue_init(&dqs);
2527   solv->installedpos = 0;
2528   solv->do_extra_reordering = 0;
2529
2530   /*
2531    * here's the main loop:
2532    * 1) decide assertion rules and propagate
2533    * 2) fulfill jobs
2534    * 3) try to keep installed packages
2535    * 4) fulfill all unresolved rules
2536    * 5) install recommended packages
2537    * 6) minimalize solution if we had choices
2538    * if we encounter a problem, we rewind to a safe level and restart
2539    * with step 1
2540    */
2541
2542   minimizationsteps = 0;
2543   for (;;)
2544     {
2545       /*
2546        * initial propagation of the assertions
2547        */
2548       if (level <= 0)
2549         {
2550           if (level < 0)
2551             break;
2552           level = makeruledecisions(solv, disablerules);
2553           if (level < 0)
2554             break;
2555           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "initial propagate (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2556           if ((r = propagate(solv, level)) != 0)
2557             {
2558               level = analyze_unsolvable(solv, r, disablerules);
2559               continue;
2560             }
2561           systemlevel = level + 1;
2562         }
2563
2564       /*
2565        * resolve jobs first (unless focus_installed is set)
2566        */
2567      if (level < systemlevel && !solv->focus_installed)
2568         {
2569           if (solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
2570             solv->do_extra_reordering = 1;
2571           olevel = level;
2572           level = resolve_jobrules(solv, level, disablerules, &dq);
2573           solv->do_extra_reordering = 0;
2574           if (level < olevel)
2575             continue;
2576           systemlevel = level + 1;
2577         }
2578
2579       /* resolve job dependencies in the focus_best case */
2580       if (level < systemlevel && solv->focus_best && !solv->focus_installed && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
2581         {
2582           solv->do_extra_reordering = 1;
2583           olevel = level;
2584           level = resolve_dependencies(solv, level, disablerules, &dq);
2585           solv->do_extra_reordering = 0;
2586           if (level < olevel)
2587             continue;           /* start over */
2588           systemlevel = level + 1;
2589         }
2590
2591       /*
2592        * installed packages
2593        */
2594       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
2595         {
2596           olevel = level;
2597           level = resolve_installed(solv, level, disablerules, &dq);
2598           if (level < olevel)
2599             continue;
2600           systemlevel = level + 1;
2601         }
2602
2603      /* resolve jobs in focus_installed case */
2604      if (level < systemlevel && solv->focus_installed)
2605         {
2606           olevel = level;
2607           level = resolve_jobrules(solv, level, disablerules, &dq);
2608           if (level < olevel)
2609             continue;
2610           systemlevel = level + 1;
2611         }
2612
2613       if (level < systemlevel)
2614         systemlevel = level;
2615
2616       /* resolve all dependencies */
2617       olevel = level;
2618       level = resolve_dependencies(solv, level, disablerules, &dq);
2619       if (level < olevel)
2620         continue;               /* start over */
2621
2622       /* decide leftover cleandeps packages */
2623       if (solv->cleandepsmap.size && solv->installed)
2624         {
2625           int rerun = 0;
2626           level = resolve_cleandeps(solv, level, disablerules, &rerun);
2627           if (rerun)
2628             continue;
2629         }
2630
2631       /* at this point we have a consistent system. now do the extras... */
2632
2633       if (doweak)
2634         {
2635           int rerun = 0;
2636           level = resolve_weak(solv, level, disablerules, &dq, &dqs, &rerun);
2637           if (rerun)
2638             continue;
2639         }
2640
2641       if (solv->installed && (solv->orphaned.count || solv->brokenorphanrules))
2642         {
2643           int rerun = 0;
2644           level = resolve_orphaned(solv, level, disablerules, &dq, &rerun);
2645           if (rerun)
2646             continue;
2647         }
2648
2649      /* one final pass to make sure we decided all installed packages */
2650       if (solv->installed)
2651         {
2652           for (p = solv->installed->start; p < solv->installed->end; p++)
2653             {
2654               if (solv->decisionmap[p])
2655                 continue;       /* already decided */
2656               s = pool->solvables + p;
2657               if (s->repo != solv->installed)
2658                 continue;
2659               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing unwanted %s\n", pool_solvid2str(pool, p));
2660               olevel = level;
2661               level = setpropagatelearn(solv, level, -p, 0, 0, SOLVER_REASON_CLEANDEPS_ERASE);
2662               if (level < olevel)
2663                 break;
2664             }
2665           if (p < solv->installed->end)
2666             continue;           /* back to main loop */
2667         }
2668
2669       if (solv->installed && solv->cleandepsmap.size && solver_check_cleandeps_mistakes(solv))
2670         {
2671           solver_reset(solv);
2672           level = 0;    /* restart from scratch */
2673           continue;
2674         }
2675
2676       if (solv->solution_callback)
2677         {
2678           solv->solution_callback(solv, solv->solution_callback_data);
2679           if (solv->branches.count)
2680             {
2681               int l, endi = 0;
2682               p = l = 0;
2683               for (i = solv->branches.count - 1; i >= 0; i--)
2684                 {
2685                   p = solv->branches.elements[i];
2686                   if (p > 0 && !l)
2687                     {
2688                       endi = i + 1;
2689                       l = p;
2690                       i -= 3;   /* skip: p data count */
2691                     }
2692                   else if (p > 0)
2693                     break;
2694                   else if (p < 0)
2695                     l = 0;
2696                 }
2697               if (i >= 0)
2698                 {
2699                   while (i > 0 && solv->branches.elements[i - 1] > 0)
2700                     i--;
2701                   level = takebranch(solv, i, endi, "branching", disablerules);
2702                   continue;
2703                 }
2704             }
2705           /* all branches done, we're finally finished */
2706           break;
2707         }
2708
2709       /* auto-minimization step */
2710       if (solv->branches.count)
2711         {
2712           int endi, lasti = -1, lastiend = -1;
2713           if (solv->recommends_index < solv->decisionq.count)
2714             policy_update_recommendsmap(solv);
2715           for (endi = solv->branches.count; endi > 0;)
2716             {
2717               int l, lastsi = -1, starti = endi - solv->branches.elements[endi - 2];
2718               l = solv->branches.elements[endi - 1];
2719               for (i = starti; i < endi - 4; i++)
2720                 {
2721                   p = solv->branches.elements[i];
2722                   if (p <= 0)
2723                     continue;
2724                   if (solv->decisionmap[p] > l)
2725                     {
2726                       lasti = i;
2727                       lastiend = endi;
2728                       lastsi = -1;
2729                       break;
2730                     }
2731                   if (solv->havedisfavored && solv->favormap[p] < 0)
2732                     continue;
2733                   if (lastsi < 0 && (MAPTST(&solv->recommendsmap, p) || solver_is_supplementing(solv, pool->solvables + p)))
2734                     lastsi = i;
2735                 }
2736               if (lastsi >= 0)
2737                 {
2738                   /* we have a recommended package that could not be installed */
2739                   /* find current selection and take new one if it is not recommended */
2740                   for (i = starti; i < endi - 4; i++)
2741                     {
2742                       p = -solv->branches.elements[i];
2743                       if (p <= 0 || solv->decisionmap[p] != l + 1)
2744                         continue;
2745                       if (solv->favormap && solv->favormap[p] > solv->favormap[solv->branches.elements[lastsi]])
2746                         continue;       /* current selection is more favored */
2747                       if (!(MAPTST(&solv->recommendsmap, p) || solver_is_supplementing(solv, pool->solvables + p)))
2748                         {
2749                           lasti = lastsi;
2750                           lastiend = endi;
2751                           break;
2752                         }
2753                     }
2754                 }
2755               endi = starti;
2756             }
2757           if (lasti >= 0)
2758             {
2759               minimizationsteps++;
2760               level = takebranch(solv, lasti, lastiend, "minimizing", disablerules);
2761               continue;         /* back to main loop */
2762             }
2763         }
2764       /* no minimization found, we're finally finished! */
2765       break;
2766     }
2767   assert(level == -1 || level + 1 == solv->decisionq_reason.count);
2768
2769   POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2770
2771   POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2772   queue_free(&dq);
2773   queue_free(&dqs);
2774 #if 0
2775   solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2776 #endif
2777 }
2778
2779
2780 /*-------------------------------------------------------------------
2781  *
2782  * remove disabled conflicts
2783  *
2784  * purpose: update the decisionmap after some rules were disabled.
2785  * this is used to calculate the suggested/recommended package list.
2786  * Also returns a "removed" list to undo the discisionmap changes.
2787  */
2788
2789 static void
2790 removedisabledconflicts(Solver *solv, Queue *removed)
2791 {
2792   Pool *pool = solv->pool;
2793   int i, n;
2794   Id p, why, *dp;
2795   Id new;
2796   Rule *r;
2797   Id *decisionmap = solv->decisionmap;
2798
2799   queue_empty(removed);
2800   for (i = 0; i < solv->decisionq.count; i++)
2801     {
2802       p = solv->decisionq.elements[i];
2803       if (p > 0)
2804         continue;       /* conflicts only, please */
2805       why = solv->decisionq_why.elements[i];
2806       if (why == 0)
2807         {
2808           /* no rule involved, must be a orphan package drop */
2809           continue;
2810         }
2811       /* we never do conflicts on free decisions, so there
2812        * must have been an unit rule */
2813       assert(why > 0);
2814       r = solv->rules + why;
2815       if (r->d < 0 && decisionmap[-p])
2816         {
2817           /* rule is now disabled, remove from decisionmap */
2818           POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2819           queue_push(removed, -p);
2820           queue_push(removed, decisionmap[-p]);
2821           decisionmap[-p] = 0;
2822         }
2823     }
2824   if (!removed->count)
2825     return;
2826   /* we removed some confliced packages. some of them might still
2827    * be in conflict, so search for unit rules and re-conflict */
2828   new = 0;
2829   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2830     {
2831       if (i == solv->nrules)
2832         {
2833           i = 1;
2834           r = solv->rules + i;
2835         }
2836       if (r->d < 0)
2837         continue;
2838       if (!r->w2)
2839         {
2840           if (r->p < 0 && !decisionmap[-r->p])
2841             new = r->p;
2842         }
2843       else if (!r->d)
2844         {
2845           /* binary rule */
2846           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2847             new = r->p;
2848           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2849             new = r->w2;
2850         }
2851       else
2852         {
2853           if (r->p < 0 && decisionmap[-r->p] == 0)
2854             new = r->p;
2855           if (new || DECISIONMAP_FALSE(r->p))
2856             {
2857               dp = pool->whatprovidesdata + r->d;
2858               while ((p = *dp++) != 0)
2859                 {
2860                   if (new && p == new)
2861                     continue;
2862                   if (p < 0 && decisionmap[-p] == 0)
2863                     {
2864                       if (new)
2865                         {
2866                           new = 0;
2867                           break;
2868                         }
2869                       new = p;
2870                     }
2871                   else if (!DECISIONMAP_FALSE(p))
2872                     {
2873                       new = 0;
2874                       break;
2875                     }
2876                 }
2877             }
2878         }
2879       if (new)
2880         {
2881           POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2882           decisionmap[-new] = -1;
2883           new = 0;
2884           n = 0;        /* redo all rules */
2885         }
2886     }
2887 }
2888
2889 static inline void
2890 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2891 {
2892   int i;
2893   for (i = 0; i < removed->count; i += 2)
2894     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2895 }
2896
2897
2898 /*-------------------------------------------------------------------
2899  *
2900  * weaken solvable dependencies
2901  */
2902
2903 static void
2904 weaken_solvable_deps(Solver *solv, Id p)
2905 {
2906   int i;
2907   Rule *r;
2908
2909   for (i = 1, r = solv->rules + i; i < solv->pkgrules_end; i++, r++)
2910     {
2911       if (r->p != -p)
2912         continue;
2913       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2914         continue;       /* conflict */
2915       queue_push(&solv->weakruleq, i);
2916     }
2917 }
2918
2919
2920 /********************************************************************/
2921 /* main() */
2922
2923
2924 void
2925 solver_calculate_multiversionmap(Pool *pool, Queue *job, Map *multiversionmap)
2926 {
2927   int i;
2928   Id how, what, select;
2929   Id p, pp;
2930   for (i = 0; i < job->count; i += 2)
2931     {
2932       how = job->elements[i];
2933       if ((how & SOLVER_JOBMASK) != SOLVER_MULTIVERSION)
2934         continue;
2935       what = job->elements[i + 1];
2936       select = how & SOLVER_SELECTMASK;
2937       if (!multiversionmap->size)
2938         map_grow(multiversionmap, pool->nsolvables);
2939       if (select == SOLVER_SOLVABLE_ALL)
2940         {
2941           FOR_POOL_SOLVABLES(p)
2942             MAPSET(multiversionmap, p);
2943         }
2944       else if (select == SOLVER_SOLVABLE_REPO)
2945         {
2946           Solvable *s;
2947           Repo *repo = pool_id2repo(pool, what);
2948           if (repo)
2949             {
2950               FOR_REPO_SOLVABLES(repo, p, s)
2951                 MAPSET(multiversionmap, p);
2952             }
2953         }
2954       FOR_JOB_SELECT(p, pp, select, what)
2955         MAPSET(multiversionmap, p);
2956     }
2957 }
2958
2959 void
2960 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *multiversionmap)
2961 {
2962   solver_calculate_multiversionmap(pool, job, multiversionmap);
2963 }
2964
2965 /*
2966  * add a rule created by a job, record job number and weak flag
2967  */
2968 static inline void
2969 solver_addjobrule(Solver *solv, Id p, Id p2, Id d, Id job, int weak)
2970 {
2971   solver_addrule(solv, p, p2, d);
2972   queue_push(&solv->ruletojob, job);
2973   if (weak)
2974     queue_push(&solv->weakruleq, solv->nrules - 1);
2975 }
2976
2977 static inline void
2978 add_cleandeps_updatepkg(Solver *solv, Id p)
2979 {
2980   if (!solv->cleandeps_updatepkgs)
2981     {
2982       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
2983       queue_init(solv->cleandeps_updatepkgs);
2984     }
2985   queue_pushunique(solv->cleandeps_updatepkgs, p);
2986 }
2987
2988 static void
2989 add_update_target(Solver *solv, Id p, Id how)
2990 {
2991   Pool *pool = solv->pool;
2992   Solvable *s = pool->solvables + p;
2993   Repo *installed = solv->installed;
2994   Id pi, pip, identicalp;
2995   int startcnt, endcnt;
2996
2997   if (!solv->update_targets)
2998     {
2999       solv->update_targets = solv_calloc(1, sizeof(Queue));
3000       queue_init(solv->update_targets);
3001     }
3002   if (s->repo == installed)
3003     {
3004       queue_push2(solv->update_targets, p, p);
3005       return;
3006     }
3007   identicalp = 0;
3008   startcnt = solv->update_targets->count;
3009   FOR_PROVIDES(pi, pip, s->name)
3010     {
3011       Solvable *si = pool->solvables + pi;
3012       if (si->repo != installed || si->name != s->name)
3013         continue;
3014       if (how & SOLVER_FORCEBEST)
3015         {
3016           if (!solv->bestupdatemap.size)
3017             map_grow(&solv->bestupdatemap, installed->end - installed->start);
3018           MAPSET(&solv->bestupdatemap, pi - installed->start);
3019         }
3020       if (how & SOLVER_CLEANDEPS)
3021         add_cleandeps_updatepkg(solv, pi);
3022       queue_push2(solv->update_targets, pi, p);
3023       /* remember an installed package that is identical to p */
3024       if (s->evr == si->evr && solvable_identical(s, si))
3025         identicalp = pi;
3026     }
3027   if (s->obsoletes)
3028     {
3029       Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3030       while ((obs = *obsp++) != 0)
3031         {
3032           FOR_PROVIDES(pi, pip, obs)
3033             {
3034               Solvable *si = pool->solvables + pi;
3035               if (si->repo != installed)
3036                 continue;
3037               if (si->name == s->name)
3038                 continue;       /* already handled above */
3039               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
3040                 continue;
3041               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
3042                 continue;
3043               if (how & SOLVER_FORCEBEST)
3044                 {
3045                   if (!solv->bestupdatemap.size)
3046                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
3047                   MAPSET(&solv->bestupdatemap, pi - installed->start);
3048                 }
3049               if (how & SOLVER_CLEANDEPS)
3050                 add_cleandeps_updatepkg(solv, pi);
3051               queue_push2(solv->update_targets, pi, p);
3052             }
3053         }
3054     }
3055   /* also allow upgrading to an identical installed package */
3056   if (identicalp)
3057     {
3058       for (endcnt = solv->update_targets->count; startcnt < endcnt; startcnt += 2)
3059         queue_push2(solv->update_targets, solv->update_targets->elements[startcnt], identicalp);
3060     }
3061 }
3062
3063 static int
3064 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
3065 {
3066   const Id *a = ap;
3067   const Id *b = bp;
3068   if (a[0] - b[0])
3069     return a[0] - b[0];
3070   return a[1] - b[1];
3071 }
3072
3073 static void
3074 transform_update_targets(Solver *solv)
3075 {
3076   Repo *installed = solv->installed;
3077   Queue *update_targets = solv->update_targets;
3078   int i, j;
3079   Id p, q, lastp, lastq;
3080
3081   if (!update_targets->count)
3082     {
3083       queue_free(update_targets);
3084       solv->update_targets = solv_free(update_targets);
3085       return;
3086     }
3087   if (update_targets->count > 2)
3088     solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
3089   queue_insertn(update_targets, 0, installed->end - installed->start, 0);
3090   lastp = lastq = 0;
3091   for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
3092     {
3093       if ((p = update_targets->elements[i]) != lastp)
3094         {
3095           if (!solv->updatemap.size)
3096             map_grow(&solv->updatemap, installed->end - installed->start);
3097           MAPSET(&solv->updatemap, p - installed->start);
3098           update_targets->elements[j++] = 0;                    /* finish old set */
3099           update_targets->elements[p - installed->start] = j;   /* start new set */
3100           lastp = p;
3101           lastq = 0;
3102         }
3103       if ((q = update_targets->elements[i + 1]) != lastq)
3104         {
3105           update_targets->elements[j++] = q;
3106           lastq = q;
3107         }
3108     }
3109   queue_truncate(update_targets, j);
3110   queue_push(update_targets, 0);        /* finish last set */
3111 }
3112
3113
3114 static void
3115 addedmap2deduceq(Solver *solv, Map *addedmap)
3116 {
3117   Pool *pool = solv->pool;
3118   int i, j;
3119   Id p;
3120   Rule *r;
3121
3122   queue_empty(&solv->addedmap_deduceq);
3123   for (i = 2, j = solv->pkgrules_end - 1; i < pool->nsolvables && j > 0; j--)
3124     {
3125       r = solv->rules + j;
3126       if (r->p >= 0)
3127         continue;
3128       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3129         continue;
3130       p = -r->p;
3131       if (!MAPTST(addedmap, p))
3132         {
3133           /* should never happen, but... */
3134           if (!solv->addedmap_deduceq.count || solv->addedmap_deduceq.elements[solv->addedmap_deduceq.count - 1] != -p)
3135             queue_push(&solv->addedmap_deduceq, -p);
3136           continue;
3137         }
3138       for (; i < p; i++)
3139         if (MAPTST(addedmap, i))
3140           queue_push(&solv->addedmap_deduceq, i);
3141       if (i == p)
3142         i++;
3143     }
3144   for (; i < pool->nsolvables; i++)
3145     if (MAPTST(addedmap, i))
3146       queue_push(&solv->addedmap_deduceq, i);
3147 }
3148
3149 static void
3150 deduceq2addedmap(Solver *solv, Map *addedmap)
3151 {
3152   int j;
3153   Id p;
3154   Rule *r;
3155   for (j = solv->pkgrules_end - 1; j > 0; j--)
3156     {
3157       r = solv->rules + j;
3158       if (r->d < 0 && r->p)
3159         solver_enablerule(solv, r);
3160       if (r->p >= 0)
3161         continue;
3162       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3163         continue;
3164       p = -r->p;
3165       MAPSET(addedmap, p);
3166     }
3167   for (j = 0; j < solv->addedmap_deduceq.count; j++)
3168     {
3169       p = solv->addedmap_deduceq.elements[j];
3170       if (p > 0)
3171         MAPSET(addedmap, p);
3172       else
3173         MAPCLR(addedmap, p);
3174     }
3175 }
3176
3177 #ifdef ENABLE_COMPLEX_DEPS
3178 static int
3179 add_complex_jobrules(Solver *solv, Id dep, int flags, int jobidx, int weak)
3180 {
3181   Pool *pool = solv->pool;
3182   Queue bq;
3183   int i, j;
3184
3185   queue_init(&bq);
3186   i = pool_normalize_complex_dep(pool, dep, &bq, flags | CPLXDEPS_EXPAND);
3187   if (i == 0 || i == 1)
3188     {
3189       queue_free(&bq);
3190       if (i == 0)
3191         solver_addjobrule(solv, -SYSTEMSOLVABLE, 0, 0, jobidx, weak);
3192       return 0;
3193     }
3194   for (i = 0; i < bq.count; i++)
3195     {
3196       if (!bq.elements[i])
3197         continue;
3198       for (j = 0; bq.elements[i + j + 1]; j++)
3199         ;
3200       if (j > 1)
3201         solver_addjobrule(solv, bq.elements[i], 0, pool_ids2whatprovides(pool, bq.elements + i + 1, j), jobidx, weak);
3202       else
3203         solver_addjobrule(solv, bq.elements[i], bq.elements[i + 1], 0, jobidx, weak);
3204       i += j + 1;
3205     }
3206   queue_free(&bq);
3207   return 1;
3208 }
3209 #endif
3210
3211 /* sort by package id, last entry wins */
3212 static int
3213 setup_favormap_cmp(const void *ap, const void *bp, void *dp)
3214 {
3215   const Id *a = ap, *b = bp;
3216   if ((*a - *b) != 0)
3217     return *a - *b;
3218   return (b[1] < 0 ? -b[1] : b[1]) - (a[1] < 0 ? -a[1] : a[1]);
3219 }
3220
3221 static void
3222 setup_favormap(Solver *solv)
3223 {
3224   Queue *q = solv->favorq;
3225   Pool *pool = solv->pool;
3226   int i;
3227   Id oldp = 0;
3228   if (q->count > 2)
3229     solv_sort(q->elements, q->count / 2, 2 * sizeof(Id), setup_favormap_cmp, solv);
3230   solv->favormap = solv_calloc(pool->nsolvables, sizeof(Id));
3231   solv->havedisfavored = 0;
3232   for (i = 0; i < q->count; i += 2)
3233     {
3234       Id p = q->elements[i];
3235       if (p == oldp)
3236         continue;
3237       oldp = p;
3238       solv->favormap[p] = q->elements[i + 1];
3239       if (q->elements[i + 1] < 0)
3240         solv->havedisfavored = 1;
3241     }
3242 }
3243
3244 /*
3245  *
3246  * solve job queue
3247  *
3248  */
3249
3250 int
3251 solver_solve(Solver *solv, Queue *job)
3252 {
3253   Pool *pool = solv->pool;
3254   Repo *installed = solv->installed;
3255   int i;
3256   int oldnrules, initialnrules;
3257   Map addedmap;                /* '1' == have pkg-rules for solvable */
3258   Map installcandidatemap;
3259   Id how, what, select, name, weak, p, pp, d;
3260   Queue q;
3261   Solvable *s, *name_s;
3262   Rule *r;
3263   int now, solve_start;
3264   int needduprules = 0;
3265   int hasbestinstalljob = 0;
3266
3267   solve_start = solv_timems(0);
3268
3269   /* log solver options */
3270   POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
3271   POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
3272   POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allownamechange=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allownamechange, solv->allowarchchange, solv->allowvendorchange);
3273   POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
3274   POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d, implicitobsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors, pool->implicitobsoleteusescolors);
3275   POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d onlynamespacerecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended, solv->only_namespace_recommended);
3276
3277   /* create whatprovides if not already there */
3278   if (!pool->whatprovides)
3279     pool_createwhatprovides(pool);
3280
3281   /* create obsolete index */
3282   policy_create_obsolete_index(solv);
3283
3284   /* remember job */
3285   queue_free(&solv->job);
3286   queue_init_clone(&solv->job, job);
3287   solv->pooljobcnt = pool->pooljobs.count;
3288   if (pool->pooljobs.count)
3289     queue_insertn(&solv->job, 0, pool->pooljobs.count, pool->pooljobs.elements);
3290   job = &solv->job;
3291
3292   /* free old stuff in case we re-run a solver */
3293   queuep_free(&solv->update_targets);
3294   queuep_free(&solv->cleandeps_updatepkgs);
3295   queue_empty(&solv->ruleassertions);
3296   solv->bestrules_info = solv_free(solv->bestrules_info);
3297   solv->yumobsrules_info = solv_free(solv->yumobsrules_info);
3298   solv->recommendsrules_info = solv_free(solv->recommendsrules_info);
3299   solv->choicerules_info = solv_free(solv->choicerules_info);
3300   if (solv->noupdate.size)
3301     map_empty(&solv->noupdate);
3302   map_zerosize(&solv->multiversion);
3303   solv->updatemap_all = 0;
3304   map_zerosize(&solv->updatemap);
3305   solv->bestupdatemap_all = 0;
3306   map_zerosize(&solv->bestupdatemap);
3307   solv->fixmap_all = 0;
3308   map_zerosize(&solv->fixmap);
3309   map_zerosize(&solv->dupmap);
3310   map_zerosize(&solv->dupinvolvedmap);
3311   solv->process_orphans = 0;
3312   solv->droporphanedmap_all = 0;
3313   map_zerosize(&solv->droporphanedmap);
3314   solv->allowuninstall_all = 0;
3315   map_zerosize(&solv->allowuninstallmap);
3316   map_zerosize(&solv->cleandepsmap);
3317   map_zerosize(&solv->weakrulemap);
3318   solv->favormap = solv_free(solv->favormap);
3319   queue_empty(&solv->weakruleq);
3320   solv->watches = solv_free(solv->watches);
3321   queue_empty(&solv->ruletojob);
3322   if (solv->decisionq.count)
3323     memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
3324   queue_empty(&solv->decisionq);
3325   queue_empty(&solv->decisionq_why);
3326   queue_empty(&solv->decisionq_reason);
3327   queue_empty(&solv->learnt_why);
3328   queue_empty(&solv->learnt_pool);
3329   queue_empty(&solv->branches);
3330   solv->propagate_index = 0;
3331   queue_empty(&solv->problems);
3332   queue_empty(&solv->solutions);
3333   queue_empty(&solv->orphaned);
3334   solv->stats_learned = solv->stats_unsolvable = 0;
3335   if (solv->recommends_index)
3336     {
3337       map_empty(&solv->recommendsmap);
3338       map_empty(&solv->suggestsmap);
3339       queuep_free(&solv->recommendscplxq);
3340       queuep_free(&solv->suggestscplxq);
3341       solv->recommends_index = 0;
3342     }
3343   queuep_free(&solv->brokenorphanrules);
3344   solv->specialupdaters = solv_free(solv->specialupdaters);
3345
3346
3347   /*
3348    * create basic rule set of all involved packages
3349    * use addedmap bitmap to make sure we don't create rules twice
3350    */
3351
3352   /* create multiversion map if needed */
3353   solver_calculate_multiversionmap(pool, job, &solv->multiversion);
3354
3355   map_init(&addedmap, pool->nsolvables);
3356   MAPSET(&addedmap, SYSTEMSOLVABLE);
3357
3358   map_init(&installcandidatemap, pool->nsolvables);
3359   queue_init(&q);
3360
3361   now = solv_timems(0);
3362   /*
3363    * create rules for all package that could be involved with the solving
3364    * so called: pkg rules
3365    *
3366    */
3367   initialnrules = solv->pkgrules_end ? solv->pkgrules_end : 1;
3368   if (initialnrules > 1)
3369     deduceq2addedmap(solv, &addedmap);          /* also enables all pkg rules */
3370   if (solv->nrules != initialnrules)
3371     solver_shrinkrules(solv, initialnrules);    /* shrink to just pkg rules */
3372   solv->lastpkgrule = 0;
3373   solv->pkgrules_end = 0;
3374
3375   if (installed)
3376     {
3377       /* check for update/verify jobs as they need to be known early */
3378       /* also setup the droporphaned map, we need it when creating update rules */
3379       for (i = 0; i < job->count; i += 2)
3380         {
3381           how = job->elements[i];
3382           what = job->elements[i + 1];
3383           select = how & SOLVER_SELECTMASK;
3384           switch (how & SOLVER_JOBMASK)
3385             {
3386             case SOLVER_VERIFY:
3387               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3388                 solv->fixmap_all = 1;
3389               FOR_JOB_SELECT(p, pp, select, what)
3390                 {
3391                   s = pool->solvables + p;
3392                   if (s->repo != installed)
3393                     continue;
3394                   if (!solv->fixmap.size)
3395                     map_grow(&solv->fixmap, installed->end - installed->start);
3396                   MAPSET(&solv->fixmap, p - installed->start);
3397                 }
3398               break;
3399             case SOLVER_UPDATE:
3400               if (select == SOLVER_SOLVABLE_ALL)
3401                 {
3402                   solv->updatemap_all = 1;
3403                   if (how & SOLVER_FORCEBEST)
3404                     solv->bestupdatemap_all = 1;
3405                   if (how & SOLVER_CLEANDEPS)
3406                     {
3407                       FOR_REPO_SOLVABLES(installed, p, s)
3408                         add_cleandeps_updatepkg(solv, p);
3409                     }
3410                 }
3411               else if (select == SOLVER_SOLVABLE_REPO)
3412                 {
3413                   Repo *repo = pool_id2repo(pool, what);
3414                   if (!repo)
3415                     break;
3416                   if (repo == installed && !(how & SOLVER_TARGETED))
3417                     {
3418                       solv->updatemap_all = 1;
3419                       if (how & SOLVER_FORCEBEST)
3420                         solv->bestupdatemap_all = 1;
3421                       if (how & SOLVER_CLEANDEPS)
3422                         {
3423                           FOR_REPO_SOLVABLES(installed, p, s)
3424                             add_cleandeps_updatepkg(solv, p);
3425                         }
3426                       break;
3427                     }
3428                   if (solv->noautotarget && !(how & SOLVER_TARGETED))
3429                     break;
3430                   /* targeted update */
3431                   FOR_REPO_SOLVABLES(repo, p, s)
3432                     add_update_target(solv, p, how);
3433                 }
3434               else
3435                 {
3436                   if (!(how & SOLVER_TARGETED))
3437                     {
3438                       int targeted = 1;
3439                       FOR_JOB_SELECT(p, pp, select, what)
3440                         {
3441                           s = pool->solvables + p;
3442                           if (s->repo != installed)
3443                             continue;
3444                           if (!solv->updatemap.size)
3445                             map_grow(&solv->updatemap, installed->end - installed->start);
3446                           MAPSET(&solv->updatemap, p - installed->start);
3447                           if (how & SOLVER_FORCEBEST)
3448                             {
3449                               if (!solv->bestupdatemap.size)
3450                                 map_grow(&solv->bestupdatemap, installed->end - installed->start);
3451                               MAPSET(&solv->bestupdatemap, p - installed->start);
3452                             }
3453                           if (how & SOLVER_CLEANDEPS)
3454                             add_cleandeps_updatepkg(solv, p);
3455                           targeted = 0;
3456                         }
3457                       if (!targeted || solv->noautotarget)
3458                         break;
3459                     }
3460                   FOR_JOB_SELECT(p, pp, select, what)
3461                     add_update_target(solv, p, how);
3462                 }
3463               break;
3464             case SOLVER_DROP_ORPHANED:
3465               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3466                 solv->droporphanedmap_all = 1;
3467               FOR_JOB_SELECT(p, pp, select, what)
3468                 {
3469                   s = pool->solvables + p;
3470                   if (s->repo != installed)
3471                     continue;
3472                   if (!solv->droporphanedmap.size)
3473                     map_grow(&solv->droporphanedmap, installed->end - installed->start);
3474                   MAPSET(&solv->droporphanedmap, p - installed->start);
3475                 }
3476               break;
3477             case SOLVER_ALLOWUNINSTALL:
3478               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3479                 solv->allowuninstall_all = 1;
3480               FOR_JOB_SELECT(p, pp, select, what)
3481                 {
3482                   s = pool->solvables + p;
3483                   if (s->repo != installed)
3484                     continue;
3485                   if (!solv->allowuninstallmap.size)
3486                     map_grow(&solv->allowuninstallmap, installed->end - installed->start);
3487                   MAPSET(&solv->allowuninstallmap, p - installed->start);
3488                 }
3489               break;
3490             default:
3491               break;
3492             }
3493         }
3494
3495       if (solv->update_targets)
3496         transform_update_targets(solv);
3497
3498       oldnrules = solv->nrules;
3499       FOR_REPO_SOLVABLES(installed, p, s)
3500         solver_addpkgrulesforsolvable(solv, s, &addedmap);
3501       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d pkg rules for installed solvables\n", solv->nrules - oldnrules);
3502       oldnrules = solv->nrules;
3503       FOR_REPO_SOLVABLES(installed, p, s)
3504         solver_addpkgrulesforupdaters(solv, s, &addedmap, 1);
3505       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d pkg rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3506     }
3507
3508   /*
3509    * create rules for all packages involved in the job
3510    * (to be installed or removed)
3511    */
3512
3513   oldnrules = solv->nrules;
3514   for (i = 0; i < job->count; i += 2)
3515     {
3516       how = job->elements[i];
3517       what = job->elements[i + 1];
3518       select = how & SOLVER_SELECTMASK;
3519
3520       switch (how & SOLVER_JOBMASK)
3521         {
3522         case SOLVER_INSTALL:
3523           FOR_JOB_SELECT(p, pp, select, what)
3524             {
3525               MAPSET(&installcandidatemap, p);
3526               solver_addpkgrulesforsolvable(solv, pool->solvables + p, &addedmap);
3527             }
3528           break;
3529         case SOLVER_DISTUPGRADE:
3530           needduprules = 1;
3531           if (select == SOLVER_SOLVABLE_ALL)
3532             solv->process_orphans = 1;
3533           break;
3534         default:
3535           break;
3536         }
3537     }
3538   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d pkg rules for packages involved in a job\n", solv->nrules - oldnrules);
3539
3540
3541   /*
3542    * add rules for suggests, enhances
3543    */
3544   oldnrules = solv->nrules;
3545   solver_addpkgrulesforweak(solv, &addedmap);
3546   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d pkg rules because of weak dependencies\n", solv->nrules - oldnrules);
3547
3548 #ifdef ENABLE_LINKED_PKGS
3549   oldnrules = solv->nrules;
3550   solver_addpkgrulesforlinked(solv, &addedmap);
3551   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d pkg rules because of linked packages\n", solv->nrules - oldnrules);
3552 #endif
3553
3554   /*
3555    * first pass done, we now have all the pkg rules we need.
3556    * unify existing rules before going over all job rules and
3557    * policy rules.
3558    * at this point the system is always solvable,
3559    * as an empty system (remove all packages) is a valid solution
3560    */
3561
3562   IF_POOLDEBUG (SOLV_DEBUG_STATS)
3563     {
3564       int possible = 0, installable = 0;
3565       for (i = 1; i < pool->nsolvables; i++)
3566         {
3567           if (pool_installable(pool, pool->solvables + i))
3568             installable++;
3569           if (MAPTST(&addedmap, i))
3570             possible++;
3571         }
3572       POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3573     }
3574
3575   if (solv->nrules > initialnrules)
3576     solver_unifyrules(solv);                    /* remove duplicate pkg rules */
3577   solv->pkgrules_end = solv->nrules;            /* mark end of pkg rules */
3578   solv->lastpkgrule = 0;
3579
3580   if (solv->nrules > initialnrules)
3581     addedmap2deduceq(solv, &addedmap);          /* so that we can recreate the addedmap */
3582
3583   POOL_DEBUG(SOLV_DEBUG_STATS, "pkg rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3584   POOL_DEBUG(SOLV_DEBUG_STATS, "pkg rule creation took %d ms\n", solv_timems(now));
3585
3586   /* create dup maps if needed. We need the maps early to create our
3587    * update rules */
3588   if (needduprules)
3589     solver_createdupmaps(solv);
3590
3591   /*
3592    * create feature rules
3593    *
3594    * foreach installed:
3595    *   create assertion (keep installed, if no update available)
3596    *   or
3597    *   create update rule (A|update1(A)|update2(A)|...)
3598    *
3599    * those are used later on to keep a version of the installed packages in
3600    * best effort mode
3601    */
3602
3603   solv->featurerules = solv->nrules;              /* mark start of feature rules */
3604   if (installed)
3605     {
3606       /* foreach possibly installed solvable */
3607       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3608         {
3609           if (s->repo != installed)
3610             {
3611               solver_addrule(solv, 0, 0, 0);    /* create dummy rule */
3612               continue;
3613             }
3614           solver_addfeaturerule(solv, s);
3615         }
3616       /* make sure we accounted for all rules */
3617       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3618     }
3619   solv->featurerules_end = solv->nrules;
3620
3621     /*
3622      * Add update rules for installed solvables
3623      *
3624      * almost identical to feature rules
3625      * except that downgrades/archchanges/vendorchanges are not allowed
3626      */
3627
3628   solv->updaterules = solv->nrules;
3629
3630   if (installed)
3631     { /* foreach installed solvables */
3632       /* we create all update rules, but disable some later on depending on the job */
3633       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3634         {
3635           Rule *sr;
3636
3637           if (s->repo != installed)
3638             {
3639               solver_addrule(solv, 0, 0, 0);    /* create dummy rule */
3640               continue;
3641             }
3642           solver_addupdaterule(solv, s);
3643           /*
3644            * check for and remove duplicate
3645            */
3646           r = solv->rules + solv->nrules - 1;           /* r: update rule */
3647           sr = r - (installed->end - installed->start); /* sr: feature rule */
3648           if (!r->p)
3649             {
3650               if (sr->p)
3651                 memset(sr, 0, sizeof(*sr));             /* no feature rules without update rules */
3652               continue;
3653             }
3654           /* it's also orphaned if the feature rule consists just of the installed package */
3655           if (!solv->process_orphans && sr->p == i && !sr->d && !sr->w2)
3656             queue_push(&solv->orphaned, i);
3657
3658           if (!solver_rulecmp(solv, r, sr))
3659             memset(sr, 0, sizeof(*sr));         /* delete unneeded feature rule */
3660           else if (sr->p)
3661             solver_disablerule(solv, sr);       /* disable feature rule for now */
3662         }
3663       /* consistency check: we added a rule for _every_ installed solvable */
3664       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3665     }
3666   solv->updaterules_end = solv->nrules;
3667
3668
3669   /*
3670    * now add all job rules
3671    */
3672
3673   solv->jobrules = solv->nrules;
3674   for (i = 0; i < job->count; i += 2)
3675     {
3676       oldnrules = solv->nrules;
3677
3678       if (i && i == solv->pooljobcnt)
3679         POOL_DEBUG(SOLV_DEBUG_JOB, "end of pool jobs\n");
3680       how = job->elements[i];
3681       what = job->elements[i + 1];
3682       weak = how & SOLVER_WEAK;
3683       select = how & SOLVER_SELECTMASK;
3684       switch (how & SOLVER_JOBMASK)
3685         {
3686         case SOLVER_INSTALL:
3687           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3688           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3689             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3690           if (select == SOLVER_SOLVABLE)
3691             {
3692               p = what;
3693               d = 0;
3694             }
3695 #ifdef ENABLE_COMPLEX_DEPS
3696           else if ((select == SOLVER_SOLVABLE_PROVIDES || select == SOLVER_SOLVABLE_NAME) && pool_is_complex_dep(pool, what))
3697             {
3698               if (add_complex_jobrules(solv, what, select == SOLVER_SOLVABLE_NAME ? CPLXDEPS_NAME : 0, i, weak))
3699                 if (how & SOLVER_FORCEBEST)
3700                   hasbestinstalljob = 1;
3701               break;
3702             }
3703 #endif
3704           else
3705             {
3706               queue_empty(&q);
3707               FOR_JOB_SELECT(p, pp, select, what)
3708                 queue_push(&q, p);
3709               if (!q.count)
3710                 {
3711                   if (select == SOLVER_SOLVABLE_ONE_OF)
3712                     break;      /* ignore empty installs */
3713                   /* no candidate found or unsupported, make this an impossible rule */
3714                   queue_push(&q, -SYSTEMSOLVABLE);
3715                 }
3716               p = queue_shift(&q);      /* get first candidate */
3717               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
3718             }
3719           /* force install of namespace supplements hack */
3720           if (select == SOLVER_SOLVABLE_PROVIDES && !d && (p == SYSTEMSOLVABLE || p == -SYSTEMSOLVABLE) && ISRELDEP(what))
3721             {
3722               Reldep *rd = GETRELDEP(pool, what);
3723               if (rd->flags == REL_NAMESPACE)
3724                 {
3725                   p = SYSTEMSOLVABLE;
3726                   if (!solv->installsuppdepq)
3727                     {
3728                       solv->installsuppdepq = solv_calloc(1, sizeof(Queue));
3729                       queue_init(solv->installsuppdepq);
3730                     }
3731                   queue_pushunique(solv->installsuppdepq, rd->evr == 0 ? rd->name : what);
3732                 }
3733             }
3734           solver_addjobrule(solv, p, 0, d, i, weak);
3735           if (how & SOLVER_FORCEBEST)
3736             hasbestinstalljob = 1;
3737           break;
3738         case SOLVER_ERASE:
3739           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3740           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3741             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3742           /* specific solvable: by id or by nevra */
3743           name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3744           name_s = 0;
3745           if (select == SOLVER_SOLVABLE_ALL)    /* hmmm ;) */
3746             {
3747               FOR_POOL_SOLVABLES(p)
3748                 solver_addjobrule(solv, -p, 0, 0, i, weak);
3749             }
3750           else if (select == SOLVER_SOLVABLE_REPO)
3751             {
3752               Repo *repo = pool_id2repo(pool, what);
3753               if (repo)
3754                 {
3755                   FOR_REPO_SOLVABLES(repo, p, s)
3756                     solver_addjobrule(solv, -p, 0, 0, i, weak);
3757                 }
3758             }
3759 #ifdef ENABLE_COMPLEX_DEPS
3760           else if ((select == SOLVER_SOLVABLE_PROVIDES || select == SOLVER_SOLVABLE_NAME) && pool_is_complex_dep(pool, what))
3761             {
3762               /* no special "erase a specific solvable" handling? */
3763               add_complex_jobrules(solv, what, select == SOLVER_SOLVABLE_NAME ? (CPLXDEPS_NAME | CPLXDEPS_TODNF | CPLXDEPS_INVERT) : (CPLXDEPS_TODNF | CPLXDEPS_INVERT), i, weak);
3764               break;
3765             }
3766 #endif
3767           FOR_JOB_SELECT(p, pp, select, what)
3768             {
3769               s = pool->solvables + p;
3770               if (installed && s->repo == installed)
3771                 {
3772                   name = !name ? s->name : -1;
3773                   name_s = s;
3774                 }
3775               solver_addjobrule(solv, -p, 0, 0, i, weak);
3776             }
3777           /* special case for "erase a specific solvable": we also
3778            * erase all other solvables with that name, so that they
3779            * don't get picked up as replacement.
3780            * name is > 0 if exactly one installed solvable matched.
3781            */
3782           /* XXX: look also at packages that obsolete this package? */
3783           if (name > 0)
3784             {
3785               int j, k;
3786               k = solv->nrules;
3787               FOR_PROVIDES(p, pp, name)
3788                 {
3789                   s = pool->solvables + p;
3790                   if (s->name != name)
3791                     continue;
3792                   /* keep other versions installed */
3793                   if (s->repo == installed)
3794                     continue;
3795                   /* keep installcandidates of other jobs */
3796                   if (MAPTST(&installcandidatemap, p))
3797                     continue;
3798                   if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, name_s, s))
3799                     continue;
3800                   /* don't add the same rule twice */
3801                   for (j = oldnrules; j < k; j++)
3802                     if (solv->rules[j].p == -p)
3803                       break;
3804                   if (j == k)
3805                     solver_addjobrule(solv, -p, 0, 0, i, weak); /* remove by id */
3806                 }
3807             }
3808           break;
3809
3810         case SOLVER_UPDATE:
3811           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3812           break;
3813         case SOLVER_VERIFY:
3814           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3815           break;
3816         case SOLVER_WEAKENDEPS:
3817           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3818           if (select != SOLVER_SOLVABLE)
3819             break;
3820           s = pool->solvables + what;
3821           weaken_solvable_deps(solv, what);
3822           break;
3823         case SOLVER_MULTIVERSION:
3824           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %smultiversion %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3825           break;
3826         case SOLVER_LOCK:
3827           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3828           if (select == SOLVER_SOLVABLE_ALL)
3829             {
3830               FOR_POOL_SOLVABLES(p)
3831                 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, 0, i, weak);
3832             }
3833           else if (select == SOLVER_SOLVABLE_REPO)
3834             {
3835               Repo *repo = pool_id2repo(pool, what);
3836               if (repo)
3837                 {
3838                   FOR_REPO_SOLVABLES(repo, p, s)
3839                     solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, 0, i, weak);
3840                 }
3841             }
3842           FOR_JOB_SELECT(p, pp, select, what)
3843             solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, 0, i, weak);
3844           break;
3845         case SOLVER_DISTUPGRADE:
3846           POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3847           break;
3848         case SOLVER_DROP_ORPHANED:
3849           POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3850           break;
3851         case SOLVER_USERINSTALLED:
3852           POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3853           break;
3854         case SOLVER_ALLOWUNINSTALL:
3855           POOL_DEBUG(SOLV_DEBUG_JOB, "job: allowuninstall %s\n", solver_select2str(pool, select, what));
3856           break;
3857         case SOLVER_FAVOR:
3858         case SOLVER_DISFAVOR:
3859           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s %s\n", (how & SOLVER_JOBMASK) == SOLVER_FAVOR ? "favor" : "disfavor", solver_select2str(pool, select, what));
3860           FOR_JOB_SELECT(p, pp, select, what)
3861             {
3862               if (!solv->favorq)
3863                 {
3864                   solv->favorq = solv_calloc(1, sizeof(Queue));
3865                   queue_init(solv->favorq);
3866                 }
3867               queue_push2(solv->favorq, p, (how & SOLVER_JOBMASK) == SOLVER_FAVOR ? i + 1 : -(i + 1));
3868             }
3869           break;
3870         default:
3871           POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3872           break;
3873         }
3874         
3875       IF_POOLDEBUG (SOLV_DEBUG_JOB)
3876         {
3877           int j;
3878           if (solv->nrules == oldnrules)
3879             POOL_DEBUG(SOLV_DEBUG_JOB, "  - no rule created\n");
3880           for (j = oldnrules; j < solv->nrules; j++)
3881             {
3882               POOL_DEBUG(SOLV_DEBUG_JOB, "  - job ");
3883               solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3884             }
3885         }
3886     }
3887   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3888   solv->jobrules_end = solv->nrules;
3889
3890   /* sort favorq and transform it into two maps */
3891   if (solv->favorq)
3892     setup_favormap(solv);
3893
3894   /* now create infarch and dup rules */
3895   if (!solv->noinfarchcheck)
3896     solver_addinfarchrules(solv, &addedmap);
3897   else
3898     solv->infarchrules = solv->infarchrules_end = solv->nrules;
3899
3900   if (solv->dupinvolvedmap_all || solv->dupinvolvedmap.size)
3901     solver_addduprules(solv, &addedmap);
3902   else
3903     solv->duprules = solv->duprules_end = solv->nrules;
3904
3905 #ifdef ENABLE_LINKED_PKGS
3906   if (solv->instbuddy && solv->updatemap.size)
3907     extend_updatemap_to_buddies(solv);
3908 #endif
3909
3910   if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3911     solver_addbestrules(solv, hasbestinstalljob);
3912   else
3913     solv->bestrules = solv->bestrules_end = solv->bestrules_up = solv->nrules;
3914
3915   if (needduprules)
3916     solver_freedupmaps(solv);   /* no longer needed */
3917
3918   if (solv->do_yum_obsoletes)
3919     solver_addyumobsrules(solv);
3920   else
3921     solv->yumobsrules = solv->yumobsrules_end = solv->nrules;
3922
3923   if (solv->havedisfavored && solv->strongrecommends)
3924     solver_addrecommendsrules(solv);
3925   else
3926     solv->recommendsrules = solv->recommendsrules_end = solv->nrules;
3927
3928   if (1)
3929     solver_addchoicerules(solv);
3930   else
3931     solv->choicerules = solv->choicerules_end = solv->nrules;
3932
3933   /* all rules created
3934    * --------------------------------------------------------------
3935    * prepare for solving
3936    */
3937
3938   /* free unneeded memory */
3939   map_free(&addedmap);
3940   map_free(&installcandidatemap);
3941   queue_free(&q);
3942
3943   POOL_DEBUG(SOLV_DEBUG_STATS, "%d pkg rules, 2 * %d update rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules, %d best rules, %d yumobs rules\n", solv->pkgrules_end - 1, solv->updaterules_end - solv->updaterules, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules, solv->bestrules_end - solv->bestrules, solv->yumobsrules_end - solv->yumobsrules);
3944   POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3945
3946   /* create weak map */
3947   if (solv->weakruleq.count || solv->recommendsruleq)
3948     {
3949       map_grow(&solv->weakrulemap, solv->nrules);
3950       for (i = 0; i < solv->weakruleq.count; i++)
3951         {
3952           p = solv->weakruleq.elements[i];
3953           MAPSET(&solv->weakrulemap, p);
3954         }
3955       if (solv->recommendsruleq)
3956         {
3957           for (i = 0; i < solv->recommendsruleq->count; i++)
3958             {
3959               p = solv->recommendsruleq->elements[i];
3960               MAPSET(&solv->weakrulemap, p);
3961             }
3962         }
3963     }
3964
3965   /* enable cleandepsmap creation if we have updatepkgs */
3966   if (solv->cleandeps_updatepkgs && !solv->cleandepsmap.size)
3967     map_grow(&solv->cleandepsmap, installed->end - installed->start);
3968   /* no mistakes */
3969   if (solv->cleandeps_mistakes)
3970     {
3971       queue_free(solv->cleandeps_mistakes);
3972       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3973     }
3974
3975   /* all new rules are learnt after this point */
3976   solv->learntrules = solv->nrules;
3977
3978   /* create watches chains */
3979   makewatches(solv);
3980
3981   /* create assertion index. it is only used to speed up
3982    * makeruledecsions() a bit */
3983   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3984     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3985       queue_push(&solv->ruleassertions, i);
3986
3987   /* disable update rules that conflict with our job */
3988   solver_disablepolicyrules(solv);
3989
3990   /* break orphans if requested */
3991   if (solv->process_orphans && solv->orphaned.count && solv->break_orphans)
3992     solver_breakorphans(solv);
3993
3994   /*
3995    * ********************************************
3996    * solve!
3997    * ********************************************
3998    */
3999
4000   now = solv_timems(0);
4001   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4002   POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
4003
4004   /*
4005    * prepare solution queue if there were problems
4006    */
4007   solver_prepare_solutions(solv);
4008
4009   POOL_DEBUG(SOLV_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
4010   POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
4011
4012   /* return number of problems */
4013   return solv->problems.count ? solv->problems.count / 2 : 0;
4014 }
4015
4016 Transaction *
4017 solver_create_transaction(Solver *solv)
4018 {
4019   return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->multiversion);
4020 }
4021
4022 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
4023 {
4024   queue_free(orphanedq);
4025   queue_init_clone(orphanedq, &solv->orphaned);
4026 }
4027
4028 void solver_get_cleandeps(Solver *solv, Queue *cleandepsq)
4029 {
4030   Pool *pool = solv->pool;
4031   Repo *installed = solv->installed;
4032   Solvable *s;
4033   Rule *r;
4034   Id p, pp, pr;
4035
4036   queue_empty(cleandepsq);
4037   if (!installed || !solv->cleandepsmap.size)
4038     return;
4039   FOR_REPO_SOLVABLES(installed, p, s)
4040     {
4041       if (!MAPTST(&solv->cleandepsmap, p - installed->start) || solv->decisionmap[p] >= 0)
4042         continue;
4043       /* now check the update rule */
4044       r = solv->rules + solv->updaterules + (p - solv->installed->start);
4045       if (r->p)
4046         {
4047           FOR_RULELITERALS(pr, pp, r)
4048             if (solv->decisionmap[pr] > 0)
4049               break;
4050           if (pr)
4051             continue;
4052         }
4053       queue_push(cleandepsq, p);
4054     }
4055 }
4056
4057 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
4058 {
4059   Pool *pool = solv->pool;
4060   Queue redoq, disabledq;
4061   int goterase, i;
4062   Solvable *s;
4063   Rule *r;
4064   Map obsmap;
4065
4066   if (!recommendationsq && !suggestionsq)
4067     return;
4068
4069   map_init(&obsmap, pool->nsolvables);
4070   if (solv->installed)
4071     {
4072       Id obs, *obsp, p, po, ppo;
4073       for (p = solv->installed->start; p < solv->installed->end; p++)
4074         {
4075           s = pool->solvables + p;
4076           if (s->repo != solv->installed || !s->obsoletes)
4077             continue;
4078           if (solv->decisionmap[p] <= 0)
4079             continue;
4080           if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
4081             continue;
4082           obsp = s->repo->idarraydata + s->obsoletes;
4083           /* foreach obsoletes */
4084           while ((obs = *obsp++) != 0)
4085             FOR_PROVIDES(po, ppo, obs)
4086               MAPSET(&obsmap, po);
4087         }
4088     }
4089
4090   queue_init(&redoq);
4091   queue_init(&disabledq);
4092   goterase = 0;
4093   /* disable all erase jobs (including weak "keep uninstalled" rules) */
4094   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
4095     {
4096       if (r->d < 0)     /* disabled ? */
4097         continue;
4098       if (r->p >= 0)    /* install job? */
4099         continue;
4100       queue_push(&disabledq, i);
4101       solver_disablerule(solv, r);
4102       goterase++;
4103     }
4104
4105   if (goterase)
4106     {
4107       enabledisablelearntrules(solv);
4108       removedisabledconflicts(solv, &redoq);
4109     }
4110
4111   /*
4112    * find recommended packages
4113    */
4114   if (recommendationsq)
4115     {
4116       Id rec, *recp, p, pp;
4117
4118       queue_empty(recommendationsq);
4119       /* create map of all recommened packages */
4120       solv->recommends_index = -1;
4121       MAPZERO(&solv->recommendsmap);
4122
4123       /* put all packages the solver already chose in the map */
4124       for (i = 1; i < solv->decisionq.count; i++)
4125         if ((p = solv->decisionq.elements[i]) > 0 && solv->decisionq_why.elements[i] == 0)
4126           {
4127             if (solv->decisionq_reason.elements[solv->decisionmap[p]] == SOLVER_REASON_WEAKDEP)
4128               MAPSET(&solv->recommendsmap, p);
4129           }
4130
4131       for (i = 0; i < solv->decisionq.count; i++)
4132         {
4133           p = solv->decisionq.elements[i];
4134           if (p < 0)
4135             continue;
4136           s = pool->solvables + p;
4137           if (s->recommends)
4138             {
4139               recp = s->repo->idarraydata + s->recommends;
4140               while ((rec = *recp++) != 0)
4141                 {
4142 #ifdef ENABLE_COMPLEX_DEPS
4143                   if (pool_is_complex_dep(pool, rec))
4144                     {
4145                       do_complex_recommendations(solv, rec, &solv->recommendsmap, noselected);
4146                       continue;
4147                     }
4148 #endif
4149                   FOR_PROVIDES(p, pp, rec)
4150                     if (solv->decisionmap[p] > 0)
4151                       break;
4152                   if (p)
4153                     {
4154                       if (!noselected)
4155                         {
4156                           FOR_PROVIDES(p, pp, rec)
4157                             if (solv->decisionmap[p] > 0)
4158                               MAPSET(&solv->recommendsmap, p);
4159                         }
4160                       continue; /* p != 0: already fulfilled */
4161                     }
4162                   FOR_PROVIDES(p, pp, rec)
4163                     MAPSET(&solv->recommendsmap, p);
4164                 }
4165             }
4166         }
4167       for (i = 1; i < pool->nsolvables; i++)
4168         {
4169           if (solv->decisionmap[i] < 0)
4170             continue;
4171           if (solv->decisionmap[i] > 0 && noselected)
4172             continue;
4173           if (MAPTST(&obsmap, i))
4174             continue;
4175           s = pool->solvables + i;
4176           if (!MAPTST(&solv->recommendsmap, i))
4177             {
4178               if (!s->supplements)
4179                 continue;
4180               if (!pool_installable(pool, s))
4181                 continue;
4182               if (!solver_is_supplementing(solv, s))
4183                 continue;
4184             }
4185           queue_push(recommendationsq, i);
4186         }
4187       /* we use MODE_SUGGEST here so that repo prio is ignored */
4188       policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
4189     }
4190
4191   /*
4192    * find suggested packages
4193    */
4194
4195   if (suggestionsq)
4196     {
4197       Id sug, *sugp, p, pp;
4198
4199       queue_empty(suggestionsq);
4200       /* create map of all suggests that are still open */
4201       solv->recommends_index = -1;
4202       MAPZERO(&solv->suggestsmap);
4203       for (i = 0; i < solv->decisionq.count; i++)
4204         {
4205           p = solv->decisionq.elements[i];
4206           if (p < 0)
4207             continue;
4208           s = pool->solvables + p;
4209           if (s->suggests)
4210             {
4211               sugp = s->repo->idarraydata + s->suggests;
4212               while ((sug = *sugp++) != 0)
4213                 {
4214 #ifdef ENABLE_COMPLEX_DEPS
4215                   if (pool_is_complex_dep(pool, sug))
4216                     {
4217                       do_complex_recommendations(solv, sug, &solv->suggestsmap, noselected);
4218                       continue;
4219                     }
4220 #endif
4221                   FOR_PROVIDES(p, pp, sug)
4222                     if (solv->decisionmap[p] > 0)
4223                       break;
4224                   if (p)
4225                     {
4226                       if (!noselected)
4227                         {
4228                           FOR_PROVIDES(p, pp, sug)
4229                             if (solv->decisionmap[p] > 0)
4230                               MAPSET(&solv->suggestsmap, p);
4231                         }
4232                       continue; /* already fulfilled */
4233                     }
4234                   FOR_PROVIDES(p, pp, sug)
4235                     MAPSET(&solv->suggestsmap, p);
4236                 }
4237             }
4238         }
4239       for (i = 1; i < pool->nsolvables; i++)
4240         {
4241           if (solv->decisionmap[i] < 0)
4242             continue;
4243           if (solv->decisionmap[i] > 0 && noselected)
4244             continue;
4245           if (MAPTST(&obsmap, i))
4246             continue;
4247           s = pool->solvables + i;
4248           if (!MAPTST(&solv->suggestsmap, i))
4249             {
4250               if (!s->enhances)
4251                 continue;
4252               if (!pool_installable(pool, s))
4253                 continue;
4254               if (!solver_is_enhancing(solv, s))
4255                 continue;
4256             }
4257           queue_push(suggestionsq, i);
4258         }
4259       policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
4260     }
4261
4262   /* undo removedisabledconflicts */
4263   if (redoq.count)
4264     undo_removedisabledconflicts(solv, &redoq);
4265   queue_free(&redoq);
4266
4267   /* undo job rule disabling */
4268   for (i = 0; i < disabledq.count; i++)
4269     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
4270   queue_free(&disabledq);
4271   map_free(&obsmap);
4272 }
4273
4274
4275 /***********************************************************************/
4276 /* disk usage computations */
4277
4278 /*-------------------------------------------------------------------
4279  *
4280  * calculate DU changes
4281  */
4282
4283 void
4284 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4285 {
4286   Map installedmap;
4287
4288   solver_create_state_maps(solv, &installedmap, 0);
4289   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
4290   map_free(&installedmap);
4291 }
4292
4293
4294 /*-------------------------------------------------------------------
4295  *
4296  * calculate changes in install size
4297  */
4298
4299 int
4300 solver_calc_installsizechange(Solver *solv)
4301 {
4302   Map installedmap;
4303   int change;
4304
4305   solver_create_state_maps(solv, &installedmap, 0);
4306   change = pool_calc_installsizechange(solv->pool, &installedmap);
4307   map_free(&installedmap);
4308   return change;
4309 }
4310
4311 void
4312 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
4313 {
4314   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
4315 }
4316
4317 /*-------------------------------------------------------------------
4318  *
4319  * decision introspection
4320  */
4321
4322 int
4323 solver_get_decisionlevel(Solver *solv, Id p)
4324 {
4325   return solv->decisionmap[p];
4326 }
4327
4328 void
4329 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
4330 {
4331   queue_free(decisionq);
4332   queue_init_clone(decisionq, &solv->decisionq);
4333 }
4334
4335 int
4336 solver_get_lastdecisionblocklevel(Solver *solv)
4337 {
4338   Id p;
4339   if (solv->decisionq.count == 0)
4340     return 0;
4341   p = solv->decisionq.elements[solv->decisionq.count - 1];
4342   if (p < 0)
4343     p = -p;
4344   return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
4345 }
4346
4347 void
4348 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
4349 {
4350   Id p;
4351   int i;
4352
4353   queue_empty(decisionq);
4354   for (i = 0; i < solv->decisionq.count; i++)
4355     {
4356       p = solv->decisionq.elements[i];
4357       if (p < 0)
4358         p = -p;
4359       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4360         break;
4361     }
4362   if (i == solv->decisionq.count)
4363     return;
4364   for (i = 0; i < solv->decisionq.count; i++)
4365     {
4366       p = solv->decisionq.elements[i];
4367       if (p < 0)
4368         p = -p;
4369       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4370         queue_push(decisionq, p);
4371       else
4372         break;
4373     }
4374 }
4375
4376 int
4377 solver_describe_decision(Solver *solv, Id p, Id *infop)
4378 {
4379   int i;
4380   Id pp, why;
4381
4382   if (infop)
4383     *infop = 0;
4384   if (!solv->decisionmap[p])
4385     return SOLVER_REASON_UNRELATED;
4386   pp = solv->decisionmap[p] < 0 ? -p : p;
4387   for (i = 0; i < solv->decisionq.count; i++)
4388     if (solv->decisionq.elements[i] == pp)
4389       break;
4390   if (i == solv->decisionq.count)       /* just in case... */
4391     return SOLVER_REASON_UNRELATED;
4392   why = solv->decisionq_why.elements[i];
4393   if (infop)
4394     *infop = why > 0 ? why : -why;
4395   if (why > 0)
4396     return SOLVER_REASON_UNIT_RULE;
4397   i = solv->decisionmap[p] >= 0 ? solv->decisionmap[p] : -solv->decisionmap[p];
4398   return solv->decisionq_reason.elements[i];
4399 }
4400
4401
4402 void
4403 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
4404 {
4405   Pool *pool = solv->pool;
4406   int i;
4407   int level = solv->decisionmap[p];
4408   int decisionno;
4409   Solvable *s;
4410
4411   queue_empty(whyq);
4412   if (level < 0)
4413     return;     /* huh? */
4414   for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
4415     if (solv->decisionq.elements[decisionno] == p)
4416       break;
4417   if (decisionno == solv->decisionq.count)
4418     return;     /* huh? */
4419   i = solv->decisionmap[p] >= 0 ? solv->decisionmap[p] : -solv->decisionmap[p];
4420   if (solv->decisionq_reason.elements[i] != SOLVER_REASON_WEAKDEP)
4421     return;     /* huh? */
4422
4423   /* 1) list all packages that recommend us */
4424   for (i = 1; i < pool->nsolvables; i++)
4425     {
4426       Id *recp, rec, pp2, p2;
4427       if (solv->decisionmap[i] <= 0 || solv->decisionmap[i] >= level)
4428         continue;
4429       s = pool->solvables + i;
4430       if (!s->recommends)
4431         continue;
4432       if (!solv->addalreadyrecommended && s->repo == solv->installed)
4433         continue;
4434       recp = s->repo->idarraydata + s->recommends;
4435       while ((rec = *recp++) != 0)
4436         {
4437           int found = 0;
4438           FOR_PROVIDES(p2, pp2, rec)
4439             {
4440               if (p2 == p)
4441                 found = 1;
4442               else
4443                 {
4444                   /* if p2 is already installed, this recommends is ignored */
4445                   if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4446                     break;
4447                 }
4448             }
4449           if (!p2 && found)
4450             {
4451               queue_push(whyq, SOLVER_REASON_RECOMMENDED);
4452               queue_push2(whyq, i, rec);
4453             }
4454         }
4455     }
4456   /* 2) list all supplements */
4457   s = pool->solvables + p;
4458   if (s->supplements && level > 0)
4459     {
4460       Id *supp, sup, pp2, p2;
4461       /* this is a hack. to use solver_dep_fulfilled we temporarily clear
4462        * everything above our level in the decisionmap */
4463       for (i = decisionno; i < solv->decisionq.count; i++ )
4464         {
4465           p2 = solv->decisionq.elements[i];
4466           if (p2 > 0)
4467             solv->decisionmap[p2] = -solv->decisionmap[p2];
4468         }
4469       supp = s->repo->idarraydata + s->supplements;
4470       while ((sup = *supp++) != 0)
4471         if (solver_dep_fulfilled(solv, sup))
4472           {
4473             int found = 0;
4474             /* let's see if this is an easy supp */
4475             FOR_PROVIDES(p2, pp2, sup)
4476               {
4477                 if (!solv->addalreadyrecommended && solv->installed)
4478                   {
4479                     if (pool->solvables[p2].repo == solv->installed)
4480                       continue;
4481                   }
4482                 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4483                   {
4484                     queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4485                     queue_push2(whyq, p2, sup);
4486                     found = 1;
4487                   }
4488               }
4489             if (!found)
4490               {
4491                 /* hard case, just note dependency with no package */
4492                 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4493                 queue_push2(whyq, 0, sup);
4494               }
4495           }
4496       for (i = decisionno; i < solv->decisionq.count; i++)
4497         {
4498           p2 = solv->decisionq.elements[i];
4499           if (p2 > 0)
4500             solv->decisionmap[p2] = -solv->decisionmap[p2];
4501         }
4502     }
4503 }
4504
4505 void
4506 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
4507 {
4508   Id p, pp;
4509   how &= SOLVER_SELECTMASK;
4510   queue_empty(pkgs);
4511   if (how == SOLVER_SOLVABLE_ALL)
4512     {
4513       FOR_POOL_SOLVABLES(p)
4514         queue_push(pkgs, p);
4515     }
4516   else if (how == SOLVER_SOLVABLE_REPO)
4517     {
4518       Repo *repo = pool_id2repo(pool, what);
4519       Solvable *s;
4520       if (repo)
4521         {
4522           FOR_REPO_SOLVABLES(repo, p, s)
4523             queue_push(pkgs, p);
4524         }
4525     }
4526   else
4527     {
4528       FOR_JOB_SELECT(p, pp, how, what)
4529         queue_push(pkgs, p);
4530     }
4531 }
4532
4533 int
4534 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
4535 {
4536   Id p, pp, pi, pip;
4537   Id select = how & SOLVER_SELECTMASK;
4538   if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
4539     return 0;
4540   if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
4541     return 0;
4542   if (!pool->installed)
4543     return 1;
4544   FOR_JOB_SELECT(p, pp, select, what)
4545     if (pool->solvables[p].repo == pool->installed)
4546       return 0;
4547   /* hard work */
4548   FOR_JOB_SELECT(p, pp, select, what)
4549     {
4550       Solvable *s = pool->solvables + p;
4551       FOR_PROVIDES(pi, pip, s->name)
4552         {
4553           Solvable *si = pool->solvables + pi;
4554           if (si->repo != pool->installed || si->name != s->name)
4555             continue;
4556           return 0;
4557         }
4558       if (s->obsoletes)
4559         {
4560           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4561           while ((obs = *obsp++) != 0)
4562             {
4563               FOR_PROVIDES(pi, pip, obs)
4564                 {
4565                   Solvable *si = pool->solvables + pi;
4566                   if (si->repo != pool->installed)
4567                     continue;
4568                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
4569                     continue;
4570                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
4571                     continue;
4572                   return 0;
4573                 }
4574             }
4575         }
4576     }
4577   return 1;
4578 }
4579
4580 int
4581 solver_alternatives_count(Solver *solv)
4582 {
4583   Id *elements = solv->branches.elements;
4584   int res, count;
4585   for (res = 0, count = solv->branches.count; count; res++)
4586     count -= elements[count - 2];
4587   return res;
4588 }
4589
4590 int
4591 solver_get_alternative(Solver *solv, Id alternative, Id *idp, Id *fromp, Id *chosenp, Queue *choices, int *levelp)
4592 {
4593   int cnt = solver_alternatives_count(solv);
4594   int count = solv->branches.count;
4595   Id *elements = solv->branches.elements;
4596   if (choices)
4597     queue_empty(choices);
4598   if (alternative <= 0 || alternative > cnt)
4599     return 0;
4600   elements += count;
4601   for (; cnt > alternative; cnt--)
4602     elements -= elements[-2];
4603   if (levelp)
4604     *levelp = elements[-1];
4605   if (fromp)
4606     *fromp = elements[-4];
4607   if (idp)
4608     *idp = elements[-3];
4609   if (chosenp)
4610     {
4611       int i;
4612       *chosenp = 0;
4613       for (i = elements[-2]; i > 4; i--)
4614         {
4615           Id p = -elements[-i];
4616           if (p > 0 && solv->decisionmap[p] == elements[-1] + 1)
4617             {
4618               *chosenp = p;
4619               break;
4620             }
4621         }
4622     }
4623   if (choices)
4624     queue_insertn(choices, 0, elements[-2] - 4, elements - elements[-2]);
4625   return elements[-4] ? SOLVER_ALTERNATIVE_TYPE_RECOMMENDS : SOLVER_ALTERNATIVE_TYPE_RULE;
4626 }
4627
4628 const char *
4629 solver_select2str(Pool *pool, Id select, Id what)
4630 {
4631   const char *s;
4632   char *b;
4633   select &= SOLVER_SELECTMASK;
4634   if (select == SOLVER_SOLVABLE)
4635     return pool_solvid2str(pool, what);
4636   if (select == SOLVER_SOLVABLE_NAME)
4637     return pool_dep2str(pool, what);
4638   if (select == SOLVER_SOLVABLE_PROVIDES)
4639     {
4640       s = pool_dep2str(pool, what);
4641       b = pool_alloctmpspace(pool, 11 + strlen(s));
4642       sprintf(b, "providing %s", s);
4643       return b;
4644     }
4645   if (select == SOLVER_SOLVABLE_ONE_OF)
4646     {
4647       Id p;
4648       b = 0;
4649       while ((p = pool->whatprovidesdata[what++]) != 0)
4650         {
4651           s = pool_solvid2str(pool, p);
4652           if (b)
4653             b = pool_tmpappend(pool, b, ", ", s);
4654           else
4655             b = pool_tmpjoin(pool, s, 0, 0);
4656           pool_freetmpspace(pool, s);
4657         }
4658       return b ? b : "nothing";
4659     }
4660   if (select == SOLVER_SOLVABLE_REPO)
4661     {
4662       b = pool_alloctmpspace(pool, 20);
4663       sprintf(b, "repo #%d", what);
4664       return b;
4665     }
4666   if (select == SOLVER_SOLVABLE_ALL)
4667     return "all packages";
4668   return "unknown job select";
4669 }
4670
4671 const char *
4672 pool_job2str(Pool *pool, Id how, Id what, Id flagmask)
4673 {
4674   Id select = how & SOLVER_SELECTMASK;
4675   const char *strstart = 0, *strend = 0;
4676   char *s;
4677   int o;
4678
4679   switch (how & SOLVER_JOBMASK)
4680     {
4681     case SOLVER_NOOP:
4682       return "do nothing";
4683     case SOLVER_INSTALL:
4684       if (select == SOLVER_SOLVABLE && pool->installed && pool->solvables[what].repo == pool->installed)
4685         strstart = "keep ", strend = " installed";
4686       else if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_NAME)
4687         strstart = "install ";
4688       else if (select == SOLVER_SOLVABLE_PROVIDES)
4689         strstart = "install a package ";
4690       else
4691         strstart = "install one of ";
4692       break;
4693     case SOLVER_ERASE:
4694       if (select == SOLVER_SOLVABLE && !(pool->installed && pool->solvables[what].repo == pool->installed))
4695         strstart = "keep ", strend = " uninstalled";
4696       else if (select == SOLVER_SOLVABLE_PROVIDES)
4697         strstart = "deinstall all packages ";
4698       else
4699         strstart = "deinstall ";
4700       break;
4701     case SOLVER_UPDATE:
4702       strstart = "update ";
4703       break;
4704     case SOLVER_WEAKENDEPS:
4705       strstart = "weaken deps of ";
4706       break;
4707     case SOLVER_MULTIVERSION:
4708       strstart = "multi version ";
4709       break;
4710     case SOLVER_LOCK:
4711       strstart = "lock ";
4712       break;
4713     case SOLVER_DISTUPGRADE:
4714       strstart = "dist upgrade ";
4715       break;
4716     case SOLVER_VERIFY:
4717       strstart = "verify ";
4718       break;
4719     case SOLVER_DROP_ORPHANED:
4720       strstart = "deinstall ", strend = " if orphaned";
4721       break;
4722     case SOLVER_USERINSTALLED:
4723       strstart = "regard ", strend = " as userinstalled";
4724       break;
4725     case SOLVER_ALLOWUNINSTALL:
4726       strstart = "allow deinstallation of ";
4727       break;
4728     case SOLVER_FAVOR:
4729       strstart = "favor ";
4730       break;
4731     case SOLVER_DISFAVOR:
4732       strstart = "disfavor ";
4733       break;
4734     default:
4735       strstart = "unknown job ";
4736       break;
4737     }
4738   s = pool_tmpjoin(pool, strstart, solver_select2str(pool, select, what), strend);
4739   how &= flagmask;
4740   if ((how & ~(SOLVER_SELECTMASK|SOLVER_JOBMASK)) == 0)
4741     return s;
4742   o = strlen(s);
4743   s = pool_tmpappend(pool, s, " ", 0);
4744   if (how & SOLVER_WEAK)
4745     s = pool_tmpappend(pool, s, ",weak", 0);
4746   if (how & SOLVER_ESSENTIAL)
4747     s = pool_tmpappend(pool, s, ",essential", 0);
4748   if (how & SOLVER_CLEANDEPS)
4749     s = pool_tmpappend(pool, s, ",cleandeps", 0);
4750   if (how & SOLVER_ORUPDATE)
4751     s = pool_tmpappend(pool, s, ",orupdate", 0);
4752   if (how & SOLVER_FORCEBEST)
4753     s = pool_tmpappend(pool, s, ",forcebest", 0);
4754   if (how & SOLVER_TARGETED)
4755     s = pool_tmpappend(pool, s, ",targeted", 0);
4756   if (how & SOLVER_SETEV)
4757     s = pool_tmpappend(pool, s, ",setev", 0);
4758   if (how & SOLVER_SETEVR)
4759     s = pool_tmpappend(pool, s, ",setevr", 0);
4760   if (how & SOLVER_SETARCH)
4761     s = pool_tmpappend(pool, s, ",setarch", 0);
4762   if (how & SOLVER_SETVENDOR)
4763     s = pool_tmpappend(pool, s, ",setvendor", 0);
4764   if (how & SOLVER_SETREPO)
4765     s = pool_tmpappend(pool, s, ",setrepo", 0);
4766   if (how & SOLVER_SETNAME)
4767     s = pool_tmpappend(pool, s, ",setname", 0);
4768   if (how & SOLVER_NOAUTOSET)
4769     s = pool_tmpappend(pool, s, ",noautoset", 0);
4770   if (s[o + 1] != ',')
4771     s = pool_tmpappend(pool, s, ",?", 0);
4772   s[o + 1] = '[';
4773   return pool_tmpappend(pool, s, "]", 0);
4774 }
4775
4776 const char *
4777 solver_alternative2str(Solver *solv, int type, Id id, Id from)
4778 {
4779   Pool *pool = solv->pool;
4780   if (type == SOLVER_ALTERNATIVE_TYPE_RECOMMENDS)
4781     {
4782       const char *s = pool_dep2str(pool, id);
4783       return pool_tmpappend(pool, s, ", recommended by ", pool_solvid2str(pool, from));
4784     }
4785   if (type == SOLVER_ALTERNATIVE_TYPE_RULE)
4786     {
4787       int rtype;
4788       Id depfrom, depto, dep;
4789       char buf[64];
4790       if (solver_ruleclass(solv, id) == SOLVER_RULE_CHOICE)
4791         id = solver_rule2pkgrule(solv, id);
4792       if (solver_ruleclass(solv, id) == SOLVER_RULE_RECOMMENDS)
4793         id = solver_rule2pkgrule(solv, id);
4794       rtype = solver_ruleinfo(solv, id, &depfrom, &depto, &dep);
4795       if ((rtype & SOLVER_RULE_TYPEMASK) == SOLVER_RULE_JOB)
4796         {
4797           if ((depto & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_PROVIDES)
4798             return pool_dep2str(pool, dep);
4799           return solver_select2str(pool, depto & SOLVER_SELECTMASK, dep);
4800         }
4801       if (rtype == SOLVER_RULE_PKG_REQUIRES)
4802         {
4803           const char *s = pool_dep2str(pool, dep);
4804           return pool_tmpappend(pool, s, ", required by ", pool_solvid2str(pool, depfrom));
4805         }
4806       sprintf(buf, "Rule #%d", id);
4807       return pool_tmpjoin(pool, buf, 0, 0);
4808     }
4809   return "unknown alternative type";
4810 }
4811