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