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