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