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