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