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