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