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