054567e0655e09438dce29dd5d68aae6f8132520
[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
1577   map_free(&solv->recommendsmap);
1578   map_free(&solv->suggestsmap);
1579   map_free(&solv->noupdate);
1580   map_free(&solv->weakrulemap);
1581   map_free(&solv->multiversion);
1582
1583   map_free(&solv->updatemap);
1584   map_free(&solv->bestupdatemap);
1585   map_free(&solv->fixmap);
1586   map_free(&solv->dupmap);
1587   map_free(&solv->dupinvolvedmap);
1588   map_free(&solv->droporphanedmap);
1589   map_free(&solv->cleandepsmap);
1590
1591   solv_free(solv->decisionmap);
1592   solv_free(solv->rules);
1593   solv_free(solv->watches);
1594   solv_free(solv->obsoletes);
1595   solv_free(solv->obsoletes_data);
1596   solv_free(solv->specialupdaters);
1597   solv_free(solv->choicerules_ref);
1598   solv_free(solv->bestrules_pkg);
1599   solv_free(solv->instbuddy);
1600   solv_free(solv);
1601 }
1602
1603 int
1604 solver_get_flag(Solver *solv, int flag)
1605 {
1606   switch (flag)
1607   {
1608   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1609     return solv->allowdowngrade;
1610   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1611     return solv->allownamechange;
1612   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1613     return solv->allowarchchange;
1614   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1615     return solv->allowvendorchange;
1616   case SOLVER_FLAG_ALLOW_UNINSTALL:
1617     return solv->allowuninstall;
1618   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1619     return solv->noupdateprovide;
1620   case SOLVER_FLAG_SPLITPROVIDES:
1621     return solv->dosplitprovides;
1622   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1623     return solv->dontinstallrecommended;
1624   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1625     return solv->addalreadyrecommended;
1626   case SOLVER_FLAG_NO_INFARCHCHECK:
1627     return solv->noinfarchcheck;
1628   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1629     return solv->keepexplicitobsoletes;
1630   case SOLVER_FLAG_BEST_OBEY_POLICY:
1631     return solv->bestobeypolicy;
1632   case SOLVER_FLAG_NO_AUTOTARGET:
1633     return solv->noautotarget;
1634   case SOLVER_FLAG_DUP_ALLOW_DOWNGRADE:
1635     return solv->dup_allowdowngrade;
1636   case SOLVER_FLAG_DUP_ALLOW_NAMECHANGE:
1637     return solv->dup_allownamechange;
1638   case SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE:
1639     return solv->dup_allowarchchange;
1640   case SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE:
1641     return solv->dup_allowvendorchange;
1642   case SOLVER_FLAG_KEEP_ORPHANS:
1643     return solv->keep_orphans;
1644   case SOLVER_FLAG_BREAK_ORPHANS:
1645     return solv->break_orphans;
1646   default:
1647     break;
1648   }
1649   return -1;
1650 }
1651
1652 int
1653 solver_set_flag(Solver *solv, int flag, int value)
1654 {
1655   int old = solver_get_flag(solv, flag);
1656   switch (flag)
1657   {
1658   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1659     solv->allowdowngrade = value;
1660     break;
1661   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1662     solv->allownamechange = value;
1663     break;
1664   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1665     solv->allowarchchange = value;
1666     break;
1667   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1668     solv->allowvendorchange = value;
1669     break;
1670   case SOLVER_FLAG_ALLOW_UNINSTALL:
1671     solv->allowuninstall = value;
1672     break;
1673   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1674     solv->noupdateprovide = value;
1675     break;
1676   case SOLVER_FLAG_SPLITPROVIDES:
1677     solv->dosplitprovides = value;
1678     break;
1679   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1680     solv->dontinstallrecommended = value;
1681     break;
1682   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1683     solv->addalreadyrecommended = value;
1684     break;
1685   case SOLVER_FLAG_NO_INFARCHCHECK:
1686     solv->noinfarchcheck = value;
1687     break;
1688   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1689     solv->keepexplicitobsoletes = value;
1690     break;
1691   case SOLVER_FLAG_BEST_OBEY_POLICY:
1692     solv->bestobeypolicy = value;
1693     break;
1694   case SOLVER_FLAG_NO_AUTOTARGET:
1695     solv->noautotarget = value;
1696     break;
1697   case SOLVER_FLAG_DUP_ALLOW_DOWNGRADE:
1698     solv->dup_allowdowngrade = value;
1699     break;
1700   case SOLVER_FLAG_DUP_ALLOW_NAMECHANGE:
1701     solv->dup_allownamechange = value;
1702     break;
1703   case SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE:
1704     solv->dup_allowarchchange = value;
1705     break;
1706   case SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE:
1707     solv->dup_allowvendorchange = value;
1708     break;
1709   case SOLVER_FLAG_KEEP_ORPHANS:
1710     solv->keep_orphans = value;
1711     break;
1712   case SOLVER_FLAG_BREAK_ORPHANS:
1713     solv->break_orphans = value;
1714     break;
1715   default:
1716     break;
1717   }
1718   return old;
1719 }
1720
1721 int
1722 cleandeps_check_mistakes(Solver *solv, int level)
1723 {
1724   Pool *pool = solv->pool;
1725   Rule *r;
1726   Id p, pp;
1727   int i;
1728   int mademistake = 0;
1729
1730   if (!solv->cleandepsmap.size)
1731     return 0;
1732   /* check for mistakes */
1733   for (i = solv->installed->start; i < solv->installed->end; i++)
1734     {
1735       if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1736         continue;
1737       r = solv->rules + solv->featurerules + (i - solv->installed->start);
1738       /* a mistake is when the featurerule is true but the updaterule is false */
1739       if (!r->p)
1740         continue;
1741       FOR_RULELITERALS(p, pp, r)
1742         if (p > 0 && solv->decisionmap[p] > 0)
1743           break;
1744       if (!p)
1745         continue;       /* feature rule is not true */
1746       r = solv->rules + solv->updaterules + (i - solv->installed->start);
1747       if (!r->p)
1748         continue;
1749       FOR_RULELITERALS(p, pp, r)
1750         if (p > 0 && solv->decisionmap[p] > 0)
1751           break;
1752       if (p)
1753         continue;       /* update rule is true */
1754       POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1755       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1756       POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1757       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1758       if (!solv->cleandeps_mistakes)
1759         {
1760           solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1761           queue_init(solv->cleandeps_mistakes);
1762         }
1763       queue_push(solv->cleandeps_mistakes, i);
1764       MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1765       solver_reenablepolicyrules_cleandeps(solv, i);
1766       mademistake = 1;
1767     }
1768   if (mademistake)
1769     solver_reset(solv);
1770   return mademistake;
1771 }
1772
1773 static void
1774 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1775 {
1776   int i, j;
1777   Id p, *cp2;
1778   for (i = j = 0; i < q->count; i++)
1779     {
1780       p = q->elements[i];
1781       for (cp2 = cp; *cp2; cp2++)
1782         if (*cp2 == p)
1783           {
1784             q->elements[j++] = p;
1785             break;
1786           }
1787     }
1788   queue_truncate(q, j);
1789 }
1790
1791 #ifdef ENABLE_COMPLEX_DEPS
1792
1793 static void
1794 add_complex_recommends(Solver *solv, Id rec, Queue *dq, Map *dqmap)
1795 {
1796   Pool *pool = solv->pool;
1797   int oldcnt = dq->count;
1798   int cutcnt, blkcnt;
1799   Id p;
1800   int i, j;
1801
1802 #if 0
1803   printf("ADD_COMPLEX_RECOMMENDS %s\n", pool_dep2str(pool, rec));
1804 #endif
1805   i = pool_normalize_complex_dep(pool, rec, dq, CPLXDEPS_EXPAND);
1806   if (i == 0 || i == 1)
1807     return;
1808   cutcnt = dq->count;
1809   for (i = oldcnt; i < cutcnt; i++)
1810     {
1811       blkcnt = dq->count;
1812       for (; (p = dq->elements[i]) != 0; i++)
1813         {
1814           if (p < 0)
1815             {
1816               if (solv->decisionmap[-p] <= 0)
1817                 break;
1818               continue;
1819             }
1820           if (solv->decisionmap[p] > 0)
1821             {
1822               queue_truncate(dq, blkcnt);
1823               break;
1824             }
1825           if (dqmap)
1826             {
1827               if (!MAPTST(dqmap, p))
1828                 continue;
1829             }
1830           else
1831             {
1832               if (solv->decisionmap[p] < 0)
1833                 continue;
1834               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))))
1835                 continue;
1836             }
1837           queue_push(dq, p);
1838         }
1839       while (dq->elements[i])
1840         i++;
1841     }
1842   queue_deleten(dq, oldcnt, cutcnt - oldcnt);
1843   /* unify */
1844   if (dq->count != oldcnt)
1845     {
1846       for (j = oldcnt; j < dq->count; j++)
1847         {
1848           p = dq->elements[j];
1849           for (i = 0; i < j; i++)
1850             if (dq->elements[i] == p)
1851               {
1852                 dq->elements[j] = 0;
1853                 break;
1854               }
1855         }
1856       for (i = j = oldcnt; j < dq->count; j++)
1857         if (dq->elements[j])
1858           dq->elements[i++] = dq->elements[j];
1859       queue_truncate(dq, i);
1860     }
1861 #if 0
1862   printf("RETURN:\n");
1863   for (i = oldcnt; i < dq->count; i++)
1864     printf("  - %s\n", pool_solvid2str(pool, dq->elements[i]));
1865 #endif
1866 }
1867
1868 static void
1869 do_complex_recommendations(Solver *solv, Id rec, Map *m, int noselected)
1870 {
1871   Pool *pool = solv->pool;
1872   Queue dq;
1873   Id p;
1874   int i, blk;
1875
1876 #if 0
1877   printf("DO_COMPLEX_RECOMMENDATIONS %s\n", pool_dep2str(pool, rec));
1878 #endif
1879   queue_init(&dq);
1880   i = pool_normalize_complex_dep(pool, rec, &dq, CPLXDEPS_EXPAND);
1881   if (i == 0 || i == 1)
1882     {
1883       queue_free(&dq);
1884       return;
1885     }
1886   for (i = 0; i < dq.count; i++)
1887     {
1888       blk = i;
1889       for (; (p = dq.elements[i]) != 0; i++)
1890         {
1891           if (p < 0)
1892             {
1893               if (solv->decisionmap[-p] <= 0)
1894                 break;
1895               continue;
1896             }
1897           if (solv->decisionmap[p] > 0)
1898             {
1899               if (noselected)
1900                 break;
1901               MAPSET(m, p);
1902               for (i++; (p = dq.elements[i]) != 0; i++)
1903                 if (p > 0 && solv->decisionmap[p] > 0)
1904                   MAPSET(m, p);
1905               p = 1;
1906               break;
1907             }
1908         }
1909       if (!p)
1910         {
1911           for (i = blk; (p = dq.elements[i]) != 0; i++)
1912             if (p > 0)
1913               MAPSET(m, p);
1914         }
1915       while (dq.elements[i])
1916         i++;
1917     }
1918   queue_free(&dq);
1919 }
1920
1921 #endif
1922
1923 /*-------------------------------------------------------------------
1924  *
1925  * solver_run_sat
1926  *
1927  * all rules have been set up, now actually run the solver
1928  *
1929  */
1930
1931 void
1932 solver_run_sat(Solver *solv, int disablerules, int doweak)
1933 {
1934   Queue dq;             /* local decisionqueue */
1935   Queue dqs;            /* local decisionqueue for supplements */
1936   int systemlevel;
1937   int level, olevel;
1938   Rule *r;
1939   int i, j, n;
1940   Solvable *s;
1941   Pool *pool = solv->pool;
1942   Id p, pp, *dp;
1943   int minimizationsteps;
1944   int installedpos = solv->installed ? solv->installed->start : 0;
1945
1946   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1947     {
1948       POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1949       for (i = 1; i < solv->nrules; i++)
1950         solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1951     }
1952
1953   POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1954
1955   /* start SAT algorithm */
1956   level = 1;
1957   systemlevel = level + 1;
1958   POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1959
1960   queue_init(&dq);
1961   queue_init(&dqs);
1962
1963   /*
1964    * here's the main loop:
1965    * 1) propagate new decisions (only needed once)
1966    * 2) fulfill jobs
1967    * 3) try to keep installed packages
1968    * 4) fulfill all unresolved rules
1969    * 5) install recommended packages
1970    * 6) minimalize solution if we had choices
1971    * if we encounter a problem, we rewind to a safe level and restart
1972    * with step 1
1973    */
1974
1975   minimizationsteps = 0;
1976   for (;;)
1977     {
1978       /*
1979        * initial propagation of the assertions
1980        */
1981       if (level == 1)
1982         {
1983           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1984           if ((r = propagate(solv, level)) != 0)
1985             {
1986               if (analyze_unsolvable(solv, r, disablerules))
1987                 continue;
1988               level = 0;
1989               break;    /* unsolvable */
1990             }
1991         }
1992
1993       /*
1994        * resolve jobs first
1995        */
1996      if (level < systemlevel)
1997         {
1998           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1999           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2000             {
2001               Id l;
2002               if (r->d < 0)             /* ignore disabled rules */
2003                 continue;
2004               queue_empty(&dq);
2005               FOR_RULELITERALS(l, pp, r)
2006                 {
2007                   if (l < 0)
2008                     {
2009                       if (solv->decisionmap[-l] <= 0)
2010                         break;
2011                     }
2012                   else
2013                     {
2014                       if (solv->decisionmap[l] > 0)
2015                         break;
2016                       if (solv->decisionmap[l] == 0)
2017                         queue_push(&dq, l);
2018                     }
2019                 }
2020               if (l || !dq.count)
2021                 continue;
2022               /* prune to installed if not updating */
2023               if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
2024                   !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
2025                 {
2026                   int j, k;
2027                   for (j = k = 0; j < dq.count; j++)
2028                     {
2029                       Solvable *s = pool->solvables + dq.elements[j];
2030                       if (s->repo == solv->installed)
2031                         {
2032                           dq.elements[k++] = dq.elements[j];
2033                           if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
2034                             {
2035                               k = 0;    /* package wants to be updated, do not prune */
2036                               break;
2037                             }
2038                         }
2039                     }
2040                   if (k)
2041                     dq.count = k;
2042                 }
2043               olevel = level;
2044               level = selectandinstall(solv, level, &dq, disablerules, i);
2045               if (level == 0)
2046                 break;
2047               if (level <= olevel)
2048                 break;
2049             }
2050           if (level == 0)
2051             break;      /* unsolvable */
2052           systemlevel = level + 1;
2053           if (i < solv->jobrules_end)
2054             continue;
2055           if (!solv->decisioncnt_update)
2056             solv->decisioncnt_update = solv->decisionq.count;
2057         }
2058
2059       /*
2060        * installed packages
2061        */
2062       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
2063         {
2064           Repo *installed = solv->installed;
2065           int pass;
2066
2067           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
2068           /* we use two passes if we need to update packages
2069            * to create a better user experience */
2070           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
2071             {
2072               int passlevel = level;
2073               Id *specialupdaters = solv->specialupdaters;
2074               if (pass == 1 && !solv->decisioncnt_keep)
2075                 solv->decisioncnt_keep = solv->decisionq.count;
2076               /* start with installedpos, the position that gave us problems the last time */
2077               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
2078                 {
2079                   Rule *rr;
2080                   Id d;
2081
2082                   if (i == installed->end)
2083                     i = installed->start;
2084                   s = pool->solvables + i;
2085                   if (s->repo != installed)
2086                     continue;
2087
2088                   if (solv->decisionmap[i] > 0 && (!specialupdaters || !specialupdaters[i - installed->start]))
2089                     continue;           /* already decided */
2090                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
2091                     continue;           /* updates first */
2092                   r = solv->rules + solv->updaterules + (i - installed->start);
2093                   rr = r;
2094                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
2095                     rr -= solv->installed->end - solv->installed->start;
2096                   if (!rr->p)           /* identical to update rule? */
2097                     rr = r;
2098                   if (!rr->p && !(specialupdaters && specialupdaters[i - installed->start]))
2099                     continue;           /* orpaned package */
2100
2101                   /* check if we should update this package to the latest version
2102                    * noupdate is set for erase jobs, in that case we want to deinstall
2103                    * the installed package and not replace it with a newer version
2104                    * rr->p != i is for dup jobs where the installed package cannot be kept */
2105                   queue_empty(&dq);
2106                   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)))
2107                     {
2108                       if (!rr->p)
2109                         {
2110                           /* specialupdater with no update/feature rule */
2111                           for (d = specialupdaters[i - installed->start]; (p = pool->whatprovidesdata[d++]) != 0; )
2112                             {
2113                               if (solv->decisionmap[p] > 0)
2114                                 {
2115                                   dq.count = 0;
2116                                   break;
2117                                 }
2118                               if (!solv->decisionmap[p])
2119                                 queue_push(&dq, p);
2120                             }
2121                         }
2122                       else if (specialupdaters && (d = specialupdaters[i - installed->start]) != 0)
2123                         {
2124                           /* special multiversion handling, make sure best version is chosen */
2125                           if (rr->p == i && solv->decisionmap[i] >= 0)
2126                             queue_push(&dq, i);
2127                           while ((p = pool->whatprovidesdata[d++]) != 0)
2128                             if (solv->decisionmap[p] >= 0)
2129                               queue_push(&dq, p);
2130                           if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
2131                             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
2132                           if (dq.count)
2133                             {
2134                               policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
2135                               p = dq.elements[0];
2136                               if (p != i && solv->decisionmap[p] == 0)
2137                                 {
2138                                   rr = solv->rules + solv->featurerules + (i - solv->installed->start);
2139                                   if (!rr->p)           /* update rule == feature rule? */
2140                                     rr = rr - solv->featurerules + solv->updaterules;
2141                                   dq.count = 1;
2142                                 }
2143                               else
2144                                 dq.count = 0;
2145                             }
2146                         }
2147                       else
2148                         {
2149                           /* update to best package of the update rule */
2150                           FOR_RULELITERALS(p, pp, rr)
2151                             {
2152                               if (solv->decisionmap[p] > 0)
2153                                 {
2154                                   dq.count = 0;         /* already fulfilled */
2155                                   break;
2156                                 }
2157                               if (!solv->decisionmap[p])
2158                                 queue_push(&dq, p);
2159                             }
2160                         }
2161                     }
2162                   if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
2163                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
2164                   /* install best version */
2165                   if (dq.count)
2166                     {
2167                       olevel = level;
2168                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
2169                       if (level == 0)
2170                         {
2171                           queue_free(&dq);
2172                           queue_free(&dqs);
2173                           return;
2174                         }
2175                       if (level <= olevel)
2176                         {
2177                           if (level == 1 || level < passlevel)
2178                             break;      /* trouble */
2179                           if (level < olevel)
2180                             n = installed->start;       /* redo all */
2181                           i--;
2182                           n--;
2183                           continue;
2184                         }
2185                     }
2186                   /* if still undecided keep package */
2187                   if (solv->decisionmap[i] == 0)
2188                     {
2189                       olevel = level;
2190                       if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
2191                         {
2192 #if 0
2193                           POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
2194                           level = setpropagatelearn(solv, level, -i, disablerules, 0);
2195 #else
2196                           continue;
2197 #endif
2198                         }
2199                       else
2200                         {
2201                           POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
2202                           level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
2203                         }
2204                       if (level == 0)
2205                         break;
2206                       if (level <= olevel)
2207                         {
2208                           if (level == 1 || level < passlevel)
2209                             break;      /* trouble */
2210                           if (level < olevel)
2211                             n = installed->start;       /* redo all */
2212                           i--;
2213                           n--;
2214                           continue;     /* retry with learnt rule */
2215                         }
2216                     }
2217                 }
2218               if (n < installed->end)
2219                 {
2220                   installedpos = i;     /* retry problem solvable next time */
2221                   break;                /* ran into trouble */
2222                 }
2223               installedpos = installed->start;  /* reset installedpos */
2224             }
2225           if (level == 0)
2226             break;              /* unsolvable */
2227           systemlevel = level + 1;
2228           if (pass < 2)
2229             continue;           /* had trouble, retry */
2230         }
2231       if (!solv->decisioncnt_keep)
2232         solv->decisioncnt_keep = solv->decisionq.count;
2233
2234       if (level < systemlevel)
2235         systemlevel = level;
2236
2237       /*
2238        * decide
2239        */
2240       if (!solv->decisioncnt_resolve)
2241         solv->decisioncnt_resolve = solv->decisionq.count;
2242       POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
2243       for (i = 1, n = 1; n < solv->nrules; i++, n++)
2244         {
2245           if (i == solv->nrules)
2246             i = 1;
2247           r = solv->rules + i;
2248           if (r->d < 0)         /* ignore disabled rules */
2249             continue;
2250           if (r->p < 0)         /* most common cases first */
2251             {
2252               if (r->d == 0 || solv->decisionmap[-r->p] <= 0)
2253                 continue;
2254             }
2255           if (dq.count)
2256             queue_empty(&dq);
2257           if (r->d == 0)
2258             {
2259               /* binary or unary rule */
2260               /* need two positive undecided literals, r->p already checked above */
2261               if (r->w2 <= 0)
2262                 continue;
2263               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2264                 continue;
2265               queue_push(&dq, r->p);
2266               queue_push(&dq, r->w2);
2267             }
2268           else
2269             {
2270               /* make sure that
2271                * all negative literals are installed
2272                * no positive literal is installed
2273                * i.e. the rule is not fulfilled and we
2274                * just need to decide on the positive literals
2275                * (decisionmap[-r->p] for the r->p < 0 case is already checked above)
2276                */
2277               if (r->p >= 0)
2278                 {
2279                   if (solv->decisionmap[r->p] > 0)
2280                     continue;
2281                   if (solv->decisionmap[r->p] == 0)
2282                     queue_push(&dq, r->p);
2283                 }
2284               dp = pool->whatprovidesdata + r->d;
2285               while ((p = *dp++) != 0)
2286                 {
2287                   if (p < 0)
2288                     {
2289                       if (solv->decisionmap[-p] <= 0)
2290                         break;
2291                     }
2292                   else
2293                     {
2294                       if (solv->decisionmap[p] > 0)
2295                         break;
2296                       if (solv->decisionmap[p] == 0)
2297                         queue_push(&dq, p);
2298                     }
2299                 }
2300               if (p)
2301                 continue;
2302             }
2303           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
2304             {
2305               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
2306               solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
2307             }
2308           /* dq.count < 2 cannot happen as this means that
2309            * the rule is unit */
2310           assert(dq.count > 1);
2311
2312           /* prune to cleandeps packages */
2313           if (solv->cleandepsmap.size && solv->installed)
2314             {
2315               Repo *installed = solv->installed;
2316               for (j = 0; j < dq.count; j++)
2317                 if (pool->solvables[dq.elements[j]].repo == installed && MAPTST(&solv->cleandepsmap, dq.elements[j] - installed->start))
2318                   break;
2319               if (j < dq.count)
2320                 {
2321                   dq.elements[0] = dq.elements[j];
2322                   queue_truncate(&dq, 1);
2323                 }
2324             }
2325
2326           olevel = level;
2327           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
2328           if (level == 0)
2329             break;              /* unsolvable */
2330           if (level < systemlevel || level == 1)
2331             break;              /* trouble */
2332           /* something changed, so look at all rules again */
2333           n = 0;
2334         }
2335
2336       if (n != solv->nrules)    /* ran into trouble? */
2337         {
2338           if (level == 0)
2339             break;              /* unsolvable */
2340           continue;             /* start over */
2341         }
2342
2343       /* decide leftover cleandeps packages */
2344       if (solv->cleandepsmap.size && solv->installed)
2345         {
2346           for (p = solv->installed->start; p < solv->installed->end; p++)
2347             {
2348               s = pool->solvables + p;
2349               if (s->repo != solv->installed)
2350                 continue;
2351               if (solv->decisionmap[p] == 0 && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2352                 {
2353                   POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, p));
2354                   olevel = level;
2355                   level = setpropagatelearn(solv, level, -p, 0, 0);
2356                   if (level < olevel)
2357                     break;
2358                 }
2359             }
2360           if (p < solv->installed->end)
2361             continue;
2362         }
2363
2364       /* at this point we have a consistent system. now do the extras... */
2365
2366       if (!solv->decisioncnt_weak)
2367         solv->decisioncnt_weak = solv->decisionq.count;
2368       if (doweak)
2369         {
2370           int qcount;
2371
2372           POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2373           queue_empty(&dq);     /* recommended packages */
2374           queue_empty(&dqs);    /* supplemented packages */
2375           for (i = 1; i < pool->nsolvables; i++)
2376             {
2377               if (solv->decisionmap[i] < 0)
2378                 continue;
2379               if (solv->decisionmap[i] > 0)
2380                 {
2381                   /* installed, check for recommends */
2382                   Id *recp, rec, pp, p;
2383                   s = pool->solvables + i;
2384                   if (!solv->addalreadyrecommended && s->repo == solv->installed)
2385                     continue;
2386                   /* XXX need to special case AND ? */
2387                   if (s->recommends)
2388                     {
2389                       recp = s->repo->idarraydata + s->recommends;
2390                       while ((rec = *recp++) != 0)
2391                         {
2392 #ifdef ENABLE_COMPLEX_DEPS
2393                           if (pool_is_complex_dep(pool, rec))
2394                             {
2395                               add_complex_recommends(solv, rec, &dq, 0);
2396                               continue;
2397                             }
2398 #endif
2399                           qcount = dq.count;
2400                           FOR_PROVIDES(p, pp, rec)
2401                             {
2402                               if (solv->decisionmap[p] > 0)
2403                                 {
2404                                   dq.count = qcount;
2405                                   break;
2406                                 }
2407                               else if (solv->decisionmap[p] == 0)
2408                                 {
2409                                   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))))
2410                                     continue;
2411                                   queue_pushunique(&dq, p);
2412                                 }
2413                             }
2414                         }
2415                     }
2416                 }
2417               else
2418                 {
2419                   s = pool->solvables + i;
2420                   if (!s->supplements)
2421                     continue;
2422                   if (!pool_installable(pool, s))
2423                     continue;
2424                   if (!solver_is_supplementing(solv, s))
2425                     continue;
2426                   if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2427                     continue;
2428                   queue_push(&dqs, i);
2429                 }
2430             }
2431
2432           /* filter out all packages obsoleted by installed packages */
2433           /* this is no longer needed if we have reverse obsoletes */
2434           if ((dqs.count || dq.count) && solv->installed)
2435             {
2436               Map obsmap;
2437               Id obs, *obsp, po, ppo;
2438
2439               map_init(&obsmap, pool->nsolvables);
2440               for (p = solv->installed->start; p < solv->installed->end; p++)
2441                 {
2442                   s = pool->solvables + p;
2443                   if (s->repo != solv->installed || !s->obsoletes)
2444                     continue;
2445                   if (solv->decisionmap[p] <= 0)
2446                     continue;
2447                   if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
2448                     continue;
2449                   obsp = s->repo->idarraydata + s->obsoletes;
2450                   /* foreach obsoletes */
2451                   while ((obs = *obsp++) != 0)
2452                     FOR_PROVIDES(po, ppo, obs)
2453                       MAPSET(&obsmap, po);
2454                 }
2455               for (i = j = 0; i < dqs.count; i++)
2456                 if (!MAPTST(&obsmap, dqs.elements[i]))
2457                   dqs.elements[j++] = dqs.elements[i];
2458               dqs.count = j;
2459               for (i = j = 0; i < dq.count; i++)
2460                 if (!MAPTST(&obsmap, dq.elements[i]))
2461                   dq.elements[j++] = dq.elements[i];
2462               dq.count = j;
2463               map_free(&obsmap);
2464             }
2465
2466           /* filter out all already supplemented packages if requested */
2467           if (!solv->addalreadyrecommended && dqs.count)
2468             {
2469               int dosplitprovides_old = solv->dosplitprovides;
2470               /* turn off all new packages */
2471               for (i = 0; i < solv->decisionq.count; i++)
2472                 {
2473                   p = solv->decisionq.elements[i];
2474                   if (p < 0)
2475                     continue;
2476                   s = pool->solvables + p;
2477                   if (s->repo && s->repo != solv->installed)
2478                     solv->decisionmap[p] = -solv->decisionmap[p];
2479                 }
2480               solv->dosplitprovides = 0;
2481               /* filter out old supplements */
2482               for (i = j = 0; i < dqs.count; i++)
2483                 {
2484                   p = dqs.elements[i];
2485                   s = pool->solvables + p;
2486                   if (!s->supplements)
2487                     continue;
2488                   if (!solver_is_supplementing(solv, s))
2489                     dqs.elements[j++] = p;
2490                   else if (solv->installsuppdepq && solver_check_installsuppdepq(solv, s))
2491                     dqs.elements[j++] = p;
2492                 }
2493               dqs.count = j;
2494               /* undo turning off */
2495               for (i = 0; i < solv->decisionq.count; i++)
2496                 {
2497                   p = solv->decisionq.elements[i];
2498                   if (p < 0)
2499                     continue;
2500                   s = pool->solvables + p;
2501                   if (s->repo && s->repo != solv->installed)
2502                     solv->decisionmap[p] = -solv->decisionmap[p];
2503                 }
2504               solv->dosplitprovides = dosplitprovides_old;
2505             }
2506
2507           /* multiversion doesn't mix well with supplements.
2508            * filter supplemented packages where we already decided
2509            * to install a different version (see bnc#501088) */
2510           if (dqs.count && solv->multiversion.size)
2511             {
2512               for (i = j = 0; i < dqs.count; i++)
2513                 {
2514                   p = dqs.elements[i];
2515                   if (MAPTST(&solv->multiversion, p))
2516                     {
2517                       Id p2, pp2;
2518                       s = pool->solvables + p;
2519                       FOR_PROVIDES(p2, pp2, s->name)
2520                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2521                           break;
2522                       if (p2)
2523                         continue;       /* ignore this package */
2524                     }
2525                   dqs.elements[j++] = p;
2526                 }
2527               dqs.count = j;
2528             }
2529
2530           /* make dq contain both recommended and supplemented pkgs */
2531           if (dqs.count)
2532             {
2533               for (i = 0; i < dqs.count; i++)
2534                 queue_pushunique(&dq, dqs.elements[i]);
2535             }
2536
2537           if (dq.count)
2538             {
2539               Map dqmap;
2540               int decisioncount = solv->decisionq.count;
2541
2542               if (dq.count == 1)
2543                 {
2544                   /* simple case, just one package. no need to choose to best version */
2545                   p = dq.elements[0];
2546                   if (dqs.count)
2547                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2548                   else
2549                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2550                   level = setpropagatelearn(solv, level, p, 0, 0);
2551                   if (level == 0)
2552                     break;
2553                   continue;     /* back to main loop */
2554                 }
2555
2556               /* filter packages, this gives us the best versions */
2557               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2558
2559               /* create map of result */
2560               map_init(&dqmap, pool->nsolvables);
2561               for (i = 0; i < dq.count; i++)
2562                 MAPSET(&dqmap, dq.elements[i]);
2563
2564               /* install all supplemented packages */
2565               for (i = 0; i < dqs.count; i++)
2566                 {
2567                   p = dqs.elements[i];
2568                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2569                     continue;
2570                   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2571                   olevel = level;
2572                   level = setpropagatelearn(solv, level, p, 0, 0);
2573                   if (level <= olevel)
2574                     break;
2575                 }
2576               if (i < dqs.count || solv->decisionq.count < decisioncount)
2577                 {
2578                   map_free(&dqmap);
2579                   if (level == 0)
2580                     break;
2581                   continue;
2582                 }
2583
2584               /* install all recommended packages */
2585               /* more work as we want to created branches if multiple
2586                * choices are valid */
2587               for (i = 0; i < decisioncount; i++)
2588                 {
2589                   Id rec, *recp, pp;
2590                   p = solv->decisionq.elements[i];
2591                   if (p < 0)
2592                     continue;
2593                   s = pool->solvables + p;
2594                   if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2595                     continue;
2596                   if (!s->recommends)
2597                     continue;
2598                   recp = s->repo->idarraydata + s->recommends;
2599                   while ((rec = *recp++) != 0)
2600                     {
2601                       queue_empty(&dq);
2602 #ifdef ENABLE_COMPLEX_DEPS
2603                       if (pool_is_complex_dep(pool, rec))
2604                           add_complex_recommends(solv, rec, &dq, &dqmap);
2605                       else
2606 #endif
2607                       FOR_PROVIDES(p, pp, rec)
2608                         {
2609                           if (solv->decisionmap[p] > 0)
2610                             {
2611                               dq.count = 0;
2612                               break;
2613                             }
2614                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2615                             queue_push(&dq, p);
2616                         }
2617                       if (!dq.count)
2618                         continue;
2619                       if (dq.count > 1)
2620                         {
2621                           /* multiple candidates, open a branch */
2622                           for (i = 1; i < dq.count; i++)
2623                             queue_push(&solv->branches, dq.elements[i]);
2624                           queue_push(&solv->branches, -level);
2625                         }
2626                       p = dq.elements[0];
2627                       POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2628                       olevel = level;
2629                       level = setpropagatelearn(solv, level, p, 0, 0);
2630                       if (level <= olevel || solv->decisionq.count < decisioncount)
2631                         break;  /* we had to revert some decisions */
2632                     }
2633                   if (rec)
2634                     break;      /* had a problem above, quit loop */
2635                 }
2636               map_free(&dqmap);
2637               if (level == 0)
2638                 break;
2639               continue;         /* back to main loop so that all deps are checked */
2640             }
2641         }
2642
2643       if (!solv->decisioncnt_orphan)
2644         solv->decisioncnt_orphan = solv->decisionq.count;
2645       if (solv->dupmap_all && solv->installed)
2646         {
2647           int installedone = 0;
2648
2649           /* let's see if we can install some unsupported package */
2650           POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2651           for (i = 0; i < solv->orphaned.count; i++)
2652             {
2653               p = solv->orphaned.elements[i];
2654               if (solv->decisionmap[p])
2655                 continue;       /* already decided */
2656               if (solv->droporphanedmap_all)
2657                 continue;
2658               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2659                 continue;
2660               POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2661               olevel = level;
2662               level = setpropagatelearn(solv, level, p, 0, 0);
2663               installedone = 1;
2664               if (level < olevel)
2665                 break;
2666             }
2667           if (installedone || i < solv->orphaned.count)
2668             {
2669               if (level == 0)
2670                 break;
2671               continue;         /* back to main loop */
2672             }
2673           for (i = 0; i < solv->orphaned.count; i++)
2674             {
2675               p = solv->orphaned.elements[i];
2676               if (solv->decisionmap[p])
2677                 continue;       /* already decided */
2678               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2679               olevel = level;
2680               level = setpropagatelearn(solv, level, -p, 0, 0);
2681               if (level < olevel)
2682                 break;
2683             }
2684           if (i < solv->orphaned.count)
2685             {
2686               if (level == 0)
2687                 break;
2688               continue;         /* back to main loop */
2689             }
2690         }
2691
2692      /* one final pass to make sure we decided all installed packages */
2693       if (solv->installed)
2694         {
2695           for (p = solv->installed->start; p < solv->installed->end; p++)
2696             {
2697               if (solv->decisionmap[p])
2698                 continue;       /* already decided */
2699               s = pool->solvables + p;
2700               if (s->repo != solv->installed)
2701                 continue;
2702               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing unwanted %s\n", pool_solvid2str(pool, p));
2703               olevel = level;
2704               level = setpropagatelearn(solv, level, -p, 0, 0);
2705               if (level < olevel)
2706                 break;
2707             }
2708           if (p < solv->installed->end)
2709             {
2710               if (level == 0)
2711                 break;
2712               continue;         /* back to main loop */
2713             }
2714         }
2715
2716       if (solv->installed && solv->cleandepsmap.size)
2717         {
2718           if (cleandeps_check_mistakes(solv, level))
2719             {
2720               level = 1;        /* restart from scratch */
2721               systemlevel = level + 1;
2722               continue;
2723             }
2724         }
2725
2726       if (solv->solution_callback)
2727         {
2728           solv->solution_callback(solv, solv->solution_callback_data);
2729           if (solv->branches.count)
2730             {
2731               int i = solv->branches.count - 1;
2732               int l = -solv->branches.elements[i];
2733               Id why;
2734
2735               for (; i > 0; i--)
2736                 if (solv->branches.elements[i - 1] < 0)
2737                   break;
2738               p = solv->branches.elements[i];
2739               POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2740               queue_empty(&dq);
2741               for (j = i + 1; j < solv->branches.count; j++)
2742                 queue_push(&dq, solv->branches.elements[j]);
2743               solv->branches.count = i;
2744               level = l;
2745               revert(solv, level);
2746               if (dq.count > 1)
2747                 for (j = 0; j < dq.count; j++)
2748                   queue_push(&solv->branches, dq.elements[j]);
2749               olevel = level;
2750               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2751               assert(why >= 0);
2752               level = setpropagatelearn(solv, level, p, disablerules, why);
2753               if (level == 0)
2754                 break;
2755               continue;
2756             }
2757           /* all branches done, we're finally finished */
2758           break;
2759         }
2760
2761       /* auto-minimization step */
2762       if (solv->branches.count)
2763         {
2764           int l = 0, lasti = -1, lastl = -1;
2765           Id why;
2766
2767           p = 0;
2768           for (i = solv->branches.count - 1; i >= 0; i--)
2769             {
2770               p = solv->branches.elements[i];
2771               if (p < 0)
2772                 l = -p;
2773               else if (p > 0 && solv->decisionmap[p] > l + 1)
2774                 {
2775                   lasti = i;
2776                   lastl = l;
2777                 }
2778             }
2779           if (lasti >= 0)
2780             {
2781               /* kill old solvable so that we do not loop */
2782               p = solv->branches.elements[lasti];
2783               solv->branches.elements[lasti] = 0;
2784               POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2785               minimizationsteps++;
2786
2787               level = lastl;
2788               revert(solv, level);
2789               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2790               assert(why >= 0);
2791               olevel = level;
2792               level = setpropagatelearn(solv, level, p, disablerules, why);
2793               if (level == 0)
2794                 break;
2795               continue;         /* back to main loop */
2796             }
2797         }
2798       /* no minimization found, we're finally finished! */
2799       break;
2800     }
2801
2802   POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2803
2804   POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2805   queue_free(&dq);
2806   queue_free(&dqs);
2807   if (level == 0)
2808     {
2809       /* unsolvable */
2810       solv->decisioncnt_update = solv->decisionq.count;
2811       solv->decisioncnt_keep = solv->decisionq.count;
2812       solv->decisioncnt_resolve = solv->decisionq.count;
2813       solv->decisioncnt_weak = solv->decisionq.count;
2814       solv->decisioncnt_orphan = solv->decisionq.count;
2815     }
2816 #if 0
2817   solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2818 #endif
2819 }
2820
2821
2822 /*-------------------------------------------------------------------
2823  *
2824  * remove disabled conflicts
2825  *
2826  * purpose: update the decisionmap after some rules were disabled.
2827  * this is used to calculate the suggested/recommended package list.
2828  * Also returns a "removed" list to undo the discisionmap changes.
2829  */
2830
2831 static void
2832 removedisabledconflicts(Solver *solv, Queue *removed)
2833 {
2834   Pool *pool = solv->pool;
2835   int i, n;
2836   Id p, why, *dp;
2837   Id new;
2838   Rule *r;
2839   Id *decisionmap = solv->decisionmap;
2840
2841   queue_empty(removed);
2842   for (i = 0; i < solv->decisionq.count; i++)
2843     {
2844       p = solv->decisionq.elements[i];
2845       if (p > 0)
2846         continue;       /* conflicts only, please */
2847       why = solv->decisionq_why.elements[i];
2848       if (why == 0)
2849         {
2850           /* no rule involved, must be a orphan package drop */
2851           continue;
2852         }
2853       /* we never do conflicts on free decisions, so there
2854        * must have been an unit rule */
2855       assert(why > 0);
2856       r = solv->rules + why;
2857       if (r->d < 0 && decisionmap[-p])
2858         {
2859           /* rule is now disabled, remove from decisionmap */
2860           POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2861           queue_push(removed, -p);
2862           queue_push(removed, decisionmap[-p]);
2863           decisionmap[-p] = 0;
2864         }
2865     }
2866   if (!removed->count)
2867     return;
2868   /* we removed some confliced packages. some of them might still
2869    * be in conflict, so search for unit rules and re-conflict */
2870   new = 0;
2871   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2872     {
2873       if (i == solv->nrules)
2874         {
2875           i = 1;
2876           r = solv->rules + i;
2877         }
2878       if (r->d < 0)
2879         continue;
2880       if (!r->w2)
2881         {
2882           if (r->p < 0 && !decisionmap[-r->p])
2883             new = r->p;
2884         }
2885       else if (!r->d)
2886         {
2887           /* binary rule */
2888           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2889             new = r->p;
2890           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2891             new = r->w2;
2892         }
2893       else
2894         {
2895           if (r->p < 0 && decisionmap[-r->p] == 0)
2896             new = r->p;
2897           if (new || DECISIONMAP_FALSE(r->p))
2898             {
2899               dp = pool->whatprovidesdata + r->d;
2900               while ((p = *dp++) != 0)
2901                 {
2902                   if (new && p == new)
2903                     continue;
2904                   if (p < 0 && decisionmap[-p] == 0)
2905                     {
2906                       if (new)
2907                         {
2908                           new = 0;
2909                           break;
2910                         }
2911                       new = p;
2912                     }
2913                   else if (!DECISIONMAP_FALSE(p))
2914                     {
2915                       new = 0;
2916                       break;
2917                     }
2918                 }
2919             }
2920         }
2921       if (new)
2922         {
2923           POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2924           decisionmap[-new] = -1;
2925           new = 0;
2926           n = 0;        /* redo all rules */
2927         }
2928     }
2929 }
2930
2931 static inline void
2932 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2933 {
2934   int i;
2935   for (i = 0; i < removed->count; i += 2)
2936     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2937 }
2938
2939
2940 /*-------------------------------------------------------------------
2941  *
2942  * weaken solvable dependencies
2943  */
2944
2945 static void
2946 weaken_solvable_deps(Solver *solv, Id p)
2947 {
2948   int i;
2949   Rule *r;
2950
2951   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2952     {
2953       if (r->p != -p)
2954         continue;
2955       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2956         continue;       /* conflict */
2957       queue_push(&solv->weakruleq, i);
2958     }
2959 }
2960
2961
2962 /********************************************************************/
2963 /* main() */
2964
2965
2966 void
2967 solver_calculate_multiversionmap(Pool *pool, Queue *job, Map *multiversionmap)
2968 {
2969   int i;
2970   Id how, what, select;
2971   Id p, pp;
2972   for (i = 0; i < job->count; i += 2)
2973     {
2974       how = job->elements[i];
2975       if ((how & SOLVER_JOBMASK) != SOLVER_MULTIVERSION)
2976         continue;
2977       what = job->elements[i + 1];
2978       select = how & SOLVER_SELECTMASK;
2979       if (!multiversionmap->size)
2980         map_grow(multiversionmap, pool->nsolvables);
2981       if (select == SOLVER_SOLVABLE_ALL)
2982         {
2983           FOR_POOL_SOLVABLES(p)
2984             MAPSET(multiversionmap, p);
2985         }
2986       else if (select == SOLVER_SOLVABLE_REPO)
2987         {
2988           Solvable *s;
2989           Repo *repo = pool_id2repo(pool, what);
2990           if (repo)
2991             FOR_REPO_SOLVABLES(repo, p, s)
2992               MAPSET(multiversionmap, p);
2993         }
2994       FOR_JOB_SELECT(p, pp, select, what)
2995         MAPSET(multiversionmap, p);
2996     }
2997 }
2998
2999 void
3000 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *multiversionmap)
3001 {
3002   solver_calculate_multiversionmap(pool, job, multiversionmap);
3003 }
3004
3005 /*
3006  * add a rule created by a job, record job number and weak flag
3007  */
3008 static inline void
3009 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
3010 {
3011   solver_addrule(solv, p, d);
3012   queue_push(&solv->ruletojob, job);
3013   if (weak)
3014     queue_push(&solv->weakruleq, solv->nrules - 1);
3015 }
3016
3017 static inline void
3018 add_cleandeps_package(Solver *solv, Id p)
3019 {
3020   if (!solv->cleandeps_updatepkgs)
3021     {
3022       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3023       queue_init(solv->cleandeps_updatepkgs);
3024     }
3025   queue_pushunique(solv->cleandeps_updatepkgs, p);
3026 }
3027
3028 static void
3029 add_update_target(Solver *solv, Id p, Id how)
3030 {
3031   Pool *pool = solv->pool;
3032   Solvable *s = pool->solvables + p;
3033   Repo *installed = solv->installed;
3034   Id pi, pip;
3035   if (!solv->update_targets)
3036     {
3037       solv->update_targets = solv_calloc(1, sizeof(Queue));
3038       queue_init(solv->update_targets);
3039     }
3040   if (s->repo == installed)
3041     {
3042       queue_push2(solv->update_targets, p, p);
3043       return;
3044     }
3045   FOR_PROVIDES(pi, pip, s->name)
3046     {
3047       Solvable *si = pool->solvables + pi;
3048       if (si->repo != installed || si->name != s->name)
3049         continue;
3050       if (how & SOLVER_FORCEBEST)
3051         {
3052           if (!solv->bestupdatemap.size)
3053             map_grow(&solv->bestupdatemap, installed->end - installed->start);
3054           MAPSET(&solv->bestupdatemap, pi - installed->start);
3055         }
3056       if (how & SOLVER_CLEANDEPS)
3057         add_cleandeps_package(solv, pi);
3058       queue_push2(solv->update_targets, pi, p);
3059       /* check if it's ok to keep the installed package */
3060       if (s->evr == si->evr && solvable_identical(s, si))
3061         queue_push2(solv->update_targets, pi, pi);
3062     }
3063   if (s->obsoletes)
3064     {
3065       Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3066       while ((obs = *obsp++) != 0)
3067         {
3068           FOR_PROVIDES(pi, pip, obs)
3069             {
3070               Solvable *si = pool->solvables + pi;
3071               if (si->repo != installed)
3072                 continue;
3073               if (si->name == s->name)
3074                 continue;       /* already handled above */
3075               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
3076                 continue;
3077               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
3078                 continue;
3079               if (how & SOLVER_FORCEBEST)
3080                 {
3081                   if (!solv->bestupdatemap.size)
3082                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
3083                   MAPSET(&solv->bestupdatemap, pi - installed->start);
3084                 }
3085               if (how & SOLVER_CLEANDEPS)
3086                 add_cleandeps_package(solv, pi);
3087               queue_push2(solv->update_targets, pi, p);
3088             }
3089         }
3090     }
3091 }
3092
3093 static int
3094 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
3095 {
3096   const Id *a = ap;
3097   const Id *b = bp;
3098   if (a[0] - b[0])
3099     return a[0] - b[0];
3100   return a[1] - b[1];
3101 }
3102
3103 static void
3104 transform_update_targets(Solver *solv)
3105 {
3106   Repo *installed = solv->installed;
3107   Queue *update_targets = solv->update_targets;
3108   int i, j;
3109   Id p, q, lastp, lastq;
3110
3111   if (!update_targets->count)
3112     {
3113       queue_free(update_targets);
3114       solv->update_targets = solv_free(update_targets);
3115       return;
3116     }
3117   if (update_targets->count > 2)
3118     solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
3119   queue_insertn(update_targets, 0, installed->end - installed->start, 0);
3120   lastp = lastq = 0;
3121   for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
3122     {
3123       if ((p = update_targets->elements[i]) != lastp)
3124         {
3125           if (!solv->updatemap.size)
3126             map_grow(&solv->updatemap, installed->end - installed->start);
3127           MAPSET(&solv->updatemap, p - installed->start);
3128           update_targets->elements[j++] = 0;                    /* finish old set */
3129           update_targets->elements[p - installed->start] = j;   /* start new set */
3130           lastp = p;
3131           lastq = 0;
3132         }
3133       if ((q = update_targets->elements[i + 1]) != lastq)
3134         {
3135           update_targets->elements[j++] = q;
3136           lastq = q;
3137         }
3138     }
3139   queue_truncate(update_targets, j);
3140   queue_push(update_targets, 0);        /* finish last set */
3141 }
3142
3143
3144 static void
3145 addedmap2deduceq(Solver *solv, Map *addedmap)
3146 {
3147   Pool *pool = solv->pool;
3148   int i, j;
3149   Id p;
3150   Rule *r;
3151
3152   queue_empty(&solv->addedmap_deduceq);
3153   for (i = 2, j = solv->rpmrules_end - 1; i < pool->nsolvables && j > 0; j--)
3154     {
3155       r = solv->rules + j;
3156       if (r->p >= 0)
3157         continue;
3158       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3159         continue;
3160       p = -r->p;
3161       if (!MAPTST(addedmap, p))
3162         {
3163           /* should never happen, but... */
3164           if (!solv->addedmap_deduceq.count || solv->addedmap_deduceq.elements[solv->addedmap_deduceq.count - 1] != -p)
3165             queue_push(&solv->addedmap_deduceq, -p);
3166           continue;
3167         }
3168       for (; i < p; i++)
3169         if (MAPTST(addedmap, i))
3170           queue_push(&solv->addedmap_deduceq, i);
3171       if (i == p)
3172         i++;
3173     }
3174   for (; i < pool->nsolvables; i++)
3175     if (MAPTST(addedmap, i))
3176       queue_push(&solv->addedmap_deduceq, i);
3177   j = 0;
3178   for (i = 2; i < pool->nsolvables; i++)
3179     if (MAPTST(addedmap, i))
3180       j++;
3181 }
3182
3183 static void
3184 deduceq2addedmap(Solver *solv, Map *addedmap)
3185 {
3186   int j;
3187   Id p;
3188   Rule *r;
3189   for (j = solv->rpmrules_end - 1; j > 0; j--)
3190     {
3191       r = solv->rules + j;
3192       if (r->d < 0 && r->p)
3193         solver_enablerule(solv, r);
3194       if (r->p >= 0)
3195         continue;
3196       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3197         continue;
3198       p = -r->p;
3199       MAPSET(addedmap, p);
3200     }
3201   for (j = 0; j < solv->addedmap_deduceq.count; j++)
3202     {
3203       p = solv->addedmap_deduceq.elements[j];
3204       if (p > 0)
3205         MAPSET(addedmap, p);
3206       else
3207         MAPCLR(addedmap, p);
3208     }
3209 }
3210
3211
3212 /*
3213  *
3214  * solve job queue
3215  *
3216  */
3217
3218 int
3219 solver_solve(Solver *solv, Queue *job)
3220 {
3221   Pool *pool = solv->pool;
3222   Repo *installed = solv->installed;
3223   int i;
3224   int oldnrules, initialnrules;
3225   Map addedmap;                /* '1' == have rpm-rules for solvable */
3226   Map installcandidatemap;
3227   Id how, what, select, name, weak, p, pp, d;
3228   Queue q;
3229   Solvable *s;
3230   Rule *r;
3231   int now, solve_start;
3232   int hasdupjob = 0;
3233   int hasbestinstalljob = 0;
3234
3235   solve_start = solv_timems(0);
3236
3237   /* log solver options */
3238   POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
3239   POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
3240   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);
3241   POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
3242   POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d, implicitobsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors, pool->implicitobsoleteusescolors);
3243   POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
3244
3245   /* create whatprovides if not already there */
3246   if (!pool->whatprovides)
3247     pool_createwhatprovides(pool);
3248
3249   /* create obsolete index */
3250   policy_create_obsolete_index(solv);
3251
3252   /* remember job */
3253   queue_free(&solv->job);
3254   queue_init_clone(&solv->job, job);
3255   solv->pooljobcnt = pool->pooljobs.count;
3256   if (pool->pooljobs.count)
3257     queue_insertn(&solv->job, 0, pool->pooljobs.count, pool->pooljobs.elements);
3258   job = &solv->job;
3259
3260   /* free old stuff in jase we re-run a solver */
3261   queuep_free(&solv->update_targets);
3262   queuep_free(&solv->cleandeps_updatepkgs);
3263   queue_empty(&solv->ruleassertions);
3264   solv->bestrules_pkg = solv_free(solv->bestrules_pkg);
3265   solv->choicerules_ref = solv_free(solv->choicerules_ref);
3266   if (solv->noupdate.size)
3267     map_empty(&solv->noupdate);
3268   map_zerosize(&solv->multiversion);
3269   solv->updatemap_all = 0;
3270   map_zerosize(&solv->updatemap);
3271   solv->bestupdatemap_all = 0;
3272   map_zerosize(&solv->bestupdatemap);
3273   solv->fixmap_all = 0;
3274   map_zerosize(&solv->fixmap);
3275   solv->dupmap_all = 0;
3276   map_zerosize(&solv->dupmap);
3277   map_zerosize(&solv->dupinvolvedmap);
3278   solv->droporphanedmap_all = 0;
3279   map_zerosize(&solv->droporphanedmap);
3280   map_zerosize(&solv->cleandepsmap);
3281   map_zerosize(&solv->weakrulemap);
3282   queue_empty(&solv->weakruleq);
3283   solv->watches = solv_free(solv->watches);
3284   queue_empty(&solv->ruletojob);
3285   if (solv->decisionq.count)
3286     memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
3287   queue_empty(&solv->decisionq);
3288   queue_empty(&solv->decisionq_why);
3289   solv->decisioncnt_update = solv->decisioncnt_keep = solv->decisioncnt_resolve = solv->decisioncnt_weak = solv->decisioncnt_orphan = 0;
3290   queue_empty(&solv->learnt_why);
3291   queue_empty(&solv->learnt_pool);
3292   queue_empty(&solv->branches);
3293   solv->propagate_index = 0;
3294   queue_empty(&solv->problems);
3295   queue_empty(&solv->solutions);
3296   queue_empty(&solv->orphaned);
3297   solv->stats_learned = solv->stats_unsolvable = 0;
3298   if (solv->recommends_index)
3299     {
3300       map_empty(&solv->recommendsmap);
3301       map_empty(&solv->suggestsmap);
3302       queuep_free(&solv->recommendscplxq);
3303       queuep_free(&solv->suggestscplxq);
3304       solv->recommends_index = 0;
3305     }
3306   solv->specialupdaters = solv_free(solv->specialupdaters);
3307
3308
3309   /*
3310    * create basic rule set of all involved packages
3311    * use addedmap bitmap to make sure we don't create rules twice
3312    */
3313
3314   /* create multiversion map if needed */
3315   solver_calculate_multiversionmap(pool, job, &solv->multiversion);
3316
3317   map_init(&addedmap, pool->nsolvables);
3318   MAPSET(&addedmap, SYSTEMSOLVABLE);
3319
3320   map_init(&installcandidatemap, pool->nsolvables);
3321   queue_init(&q);
3322
3323   now = solv_timems(0);
3324   /*
3325    * create rules for all package that could be involved with the solving
3326    * so called: rpm rules
3327    *
3328    */
3329   initialnrules = solv->rpmrules_end ? solv->rpmrules_end : 1;
3330   if (initialnrules > 1)
3331     deduceq2addedmap(solv, &addedmap);
3332   if (solv->nrules != initialnrules)
3333     solver_shrinkrules(solv, initialnrules);
3334   solv->nrules = initialnrules;
3335   solv->rpmrules_end = 0;
3336
3337   if (installed)
3338     {
3339       /* check for update/verify jobs as they need to be known early */
3340       for (i = 0; i < job->count; i += 2)
3341         {
3342           how = job->elements[i];
3343           what = job->elements[i + 1];
3344           select = how & SOLVER_SELECTMASK;
3345           switch (how & SOLVER_JOBMASK)
3346             {
3347             case SOLVER_VERIFY:
3348               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3349                 solv->fixmap_all = 1;
3350               FOR_JOB_SELECT(p, pp, select, what)
3351                 {
3352                   s = pool->solvables + p;
3353                   if (s->repo != installed)
3354                     continue;
3355                   if (!solv->fixmap.size)
3356                     map_grow(&solv->fixmap, installed->end - installed->start);
3357                   MAPSET(&solv->fixmap, p - installed->start);
3358                 }
3359               break;
3360             case SOLVER_UPDATE:
3361               if (select == SOLVER_SOLVABLE_ALL)
3362                 {
3363                   solv->updatemap_all = 1;
3364                   if (how & SOLVER_FORCEBEST)
3365                     solv->bestupdatemap_all = 1;
3366                   if (how & SOLVER_CLEANDEPS)
3367                     {
3368                       FOR_REPO_SOLVABLES(installed, p, s)
3369                         add_cleandeps_package(solv, p);
3370                     }
3371                 }
3372               else if (select == SOLVER_SOLVABLE_REPO)
3373                 {
3374                   Repo *repo = pool_id2repo(pool, what);
3375                   if (!repo)
3376                     break;
3377                   if (repo == installed && !(how & SOLVER_TARGETED))
3378                     {
3379                       solv->updatemap_all = 1;
3380                       if (how & SOLVER_FORCEBEST)
3381                         solv->bestupdatemap_all = 1;
3382                       if (how & SOLVER_CLEANDEPS)
3383                         {
3384                           FOR_REPO_SOLVABLES(installed, p, s)
3385                             add_cleandeps_package(solv, p);
3386                         }
3387                       break;
3388                     }
3389                   if (solv->noautotarget && !(how & SOLVER_TARGETED))
3390                     break;
3391                   /* targeted update */
3392                   FOR_REPO_SOLVABLES(repo, p, s)
3393                     add_update_target(solv, p, how);
3394                 }
3395               else
3396                 {
3397                   if (!(how & SOLVER_TARGETED))
3398                     {
3399                       int targeted = 1;
3400                       FOR_JOB_SELECT(p, pp, select, what)
3401                         {
3402                           s = pool->solvables + p;
3403                           if (s->repo != installed)
3404                             continue;
3405                           if (!solv->updatemap.size)
3406                             map_grow(&solv->updatemap, installed->end - installed->start);
3407                           MAPSET(&solv->updatemap, p - installed->start);
3408                           if (how & SOLVER_FORCEBEST)
3409                             {
3410                               if (!solv->bestupdatemap.size)
3411                                 map_grow(&solv->bestupdatemap, installed->end - installed->start);
3412                               MAPSET(&solv->bestupdatemap, p - installed->start);
3413                             }
3414                           if (how & SOLVER_CLEANDEPS)
3415                             add_cleandeps_package(solv, p);
3416                           targeted = 0;
3417                         }
3418                       if (!targeted || solv->noautotarget)
3419                         break;
3420                     }
3421                   FOR_JOB_SELECT(p, pp, select, what)
3422                     add_update_target(solv, p, how);
3423                 }
3424               break;
3425             default:
3426               break;
3427             }
3428         }
3429
3430       if (solv->update_targets)
3431         transform_update_targets(solv);
3432
3433       oldnrules = solv->nrules;
3434       FOR_REPO_SOLVABLES(installed, p, s)
3435         solver_addrpmrulesforsolvable(solv, s, &addedmap);
3436       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3437       oldnrules = solv->nrules;
3438       FOR_REPO_SOLVABLES(installed, p, s)
3439         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
3440       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3441     }
3442
3443   /*
3444    * create rules for all packages involved in the job
3445    * (to be installed or removed)
3446    */
3447
3448   oldnrules = solv->nrules;
3449   for (i = 0; i < job->count; i += 2)
3450     {
3451       how = job->elements[i];
3452       what = job->elements[i + 1];
3453       select = how & SOLVER_SELECTMASK;
3454
3455       switch (how & SOLVER_JOBMASK)
3456         {
3457         case SOLVER_INSTALL:
3458           FOR_JOB_SELECT(p, pp, select, what)
3459             {
3460               MAPSET(&installcandidatemap, p);
3461               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3462             }
3463           break;
3464         case SOLVER_DISTUPGRADE:
3465           if (select == SOLVER_SOLVABLE_ALL)
3466             {
3467               solv->dupmap_all = 1;
3468               solv->updatemap_all = 1;
3469               if (how & SOLVER_FORCEBEST)
3470                 solv->bestupdatemap_all = 1;
3471             }
3472           if (!solv->dupmap_all)
3473             hasdupjob = 1;
3474           break;
3475         default:
3476           break;
3477         }
3478     }
3479   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3480
3481
3482   /*
3483    * add rules for suggests, enhances
3484    */
3485   oldnrules = solv->nrules;
3486   solver_addrpmrulesforweak(solv, &addedmap);
3487   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3488
3489 #ifdef ENABLE_LINKED_PKGS
3490   oldnrules = solv->nrules;
3491   solver_addrpmrulesforlinked(solv, &addedmap);
3492   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of linked packages\n", solv->nrules - oldnrules);
3493 #endif
3494
3495   /*
3496    * first pass done, we now have all the rpm rules we need.
3497    * unify existing rules before going over all job rules and
3498    * policy rules.
3499    * at this point the system is always solvable,
3500    * as an empty system (remove all packages) is a valid solution
3501    */
3502
3503   IF_POOLDEBUG (SOLV_DEBUG_STATS)
3504     {
3505       int possible = 0, installable = 0;
3506       for (i = 1; i < pool->nsolvables; i++)
3507         {
3508           if (pool_installable(pool, pool->solvables + i))
3509             installable++;
3510           if (MAPTST(&addedmap, i))
3511             possible++;
3512         }
3513       POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3514     }
3515
3516   if (solv->nrules > initialnrules)
3517     solver_unifyrules(solv);                    /* remove duplicate rpm rules */
3518   solv->rpmrules_end = solv->nrules;            /* mark end of rpm rules */
3519
3520   if (solv->nrules > initialnrules)
3521     addedmap2deduceq(solv, &addedmap);          /* so that we can recreate the addedmap */
3522
3523   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3524   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
3525
3526   /* create dup maps if needed. We need the maps early to create our
3527    * update rules */
3528   if (hasdupjob)
3529     solver_createdupmaps(solv);
3530
3531   /*
3532    * create feature rules
3533    *
3534    * foreach installed:
3535    *   create assertion (keep installed, if no update available)
3536    *   or
3537    *   create update rule (A|update1(A)|update2(A)|...)
3538    *
3539    * those are used later on to keep a version of the installed packages in
3540    * best effort mode
3541    */
3542
3543   solv->featurerules = solv->nrules;              /* mark start of feature rules */
3544   if (installed)
3545     {
3546       /* foreach possibly installed solvable */
3547       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3548         {
3549           if (s->repo != installed)
3550             {
3551               solver_addrule(solv, 0, 0);       /* create dummy rule */
3552               continue;
3553             }
3554           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
3555         }
3556       /* make sure we accounted for all rules */
3557       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3558     }
3559   solv->featurerules_end = solv->nrules;
3560
3561     /*
3562      * Add update rules for installed solvables
3563      *
3564      * almost identical to feature rules
3565      * except that downgrades/archchanges/vendorchanges are not allowed
3566      */
3567
3568   solv->updaterules = solv->nrules;
3569
3570   if (installed)
3571     { /* foreach installed solvables */
3572       /* we create all update rules, but disable some later on depending on the job */
3573       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3574         {
3575           Rule *sr;
3576
3577           if (s->repo != installed)
3578             {
3579               solver_addrule(solv, 0, 0);       /* create dummy rule */
3580               continue;
3581             }
3582           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
3583           /*
3584            * check for and remove duplicate
3585            */
3586           r = solv->rules + solv->nrules - 1;           /* r: update rule */
3587           if (!r->p)
3588             continue;
3589           sr = r - (installed->end - installed->start); /* sr: feature rule */
3590           /* it's also orphaned if the feature rule consists just of the installed package */
3591           if (!solv->dupmap_all && sr->p == i && !sr->d && !sr->w2)
3592             queue_push(&solv->orphaned, i);
3593           if (!solver_rulecmp(solv, r, sr))
3594             memset(sr, 0, sizeof(*sr));         /* delete unneeded feature rule */
3595           else
3596             solver_disablerule(solv, sr);       /* disable feature rule for now */
3597         }
3598       /* consistency check: we added a rule for _every_ installed solvable */
3599       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3600     }
3601   solv->updaterules_end = solv->nrules;
3602
3603
3604   /*
3605    * now add all job rules
3606    */
3607
3608   solv->jobrules = solv->nrules;
3609   for (i = 0; i < job->count; i += 2)
3610     {
3611       oldnrules = solv->nrules;
3612
3613       if (i && i == solv->pooljobcnt)
3614         POOL_DEBUG(SOLV_DEBUG_JOB, "end of pool jobs\n");
3615       how = job->elements[i];
3616       what = job->elements[i + 1];
3617       weak = how & SOLVER_WEAK;
3618       select = how & SOLVER_SELECTMASK;
3619       switch (how & SOLVER_JOBMASK)
3620         {
3621         case SOLVER_INSTALL:
3622           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3623           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3624             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3625           if (select == SOLVER_SOLVABLE)
3626             {
3627               p = what;
3628               d = 0;
3629             }
3630           else
3631             {
3632               queue_empty(&q);
3633               FOR_JOB_SELECT(p, pp, select, what)
3634                 queue_push(&q, p);
3635               if (!q.count)
3636                 {
3637                   if (select == SOLVER_SOLVABLE_ONE_OF)
3638                     break;      /* ignore empty installs */
3639                   /* no candidate found or unsupported, make this an impossible rule */
3640                   queue_push(&q, -SYSTEMSOLVABLE);
3641                 }
3642               p = queue_shift(&q);      /* get first candidate */
3643               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
3644             }
3645           /* force install of namespace supplements hack */
3646           if (select == SOLVER_SOLVABLE_PROVIDES && !d && (p == SYSTEMSOLVABLE || p == -SYSTEMSOLVABLE) && ISRELDEP(what))
3647             {
3648               Reldep *rd = GETRELDEP(pool, what);
3649               if (rd->flags == REL_NAMESPACE)
3650                 {
3651                   p = SYSTEMSOLVABLE;
3652                   if (!solv->installsuppdepq)
3653                     {
3654                       solv->installsuppdepq = solv_calloc(1, sizeof(Queue));
3655                       queue_init(solv->installsuppdepq);
3656                     }
3657                   queue_pushunique(solv->installsuppdepq, rd->evr == 0 ? rd->name : what);
3658                 }
3659             }
3660           solver_addjobrule(solv, p, d, i, weak);
3661           if (how & SOLVER_FORCEBEST)
3662             hasbestinstalljob = 1;
3663           break;
3664         case SOLVER_ERASE:
3665           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3666           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3667             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3668           /* specific solvable: by id or by nevra */
3669           name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3670           if (select == SOLVER_SOLVABLE_ALL)    /* hmmm ;) */
3671             {
3672               FOR_POOL_SOLVABLES(p)
3673                 solver_addjobrule(solv, -p, 0, i, weak);
3674             }
3675           else if (select == SOLVER_SOLVABLE_REPO)
3676             {
3677               Repo *repo = pool_id2repo(pool, what);
3678               if (repo)
3679                 FOR_REPO_SOLVABLES(repo, p, s)
3680                   solver_addjobrule(solv, -p, 0, i, weak);
3681             }
3682           FOR_JOB_SELECT(p, pp, select, what)
3683             {
3684               s = pool->solvables + p;
3685               if (installed && s->repo == installed)
3686                 name = !name ? s->name : -1;
3687               solver_addjobrule(solv, -p, 0, i, weak);
3688             }
3689           /* special case for "erase a specific solvable": we also
3690            * erase all other solvables with that name, so that they
3691            * don't get picked up as replacement.
3692            * name is > 0 if exactly one installed solvable matched.
3693            */
3694           /* XXX: look also at packages that obsolete this package? */
3695           if (name > 0)
3696             {
3697               int j, k;
3698               k = solv->nrules;
3699               FOR_PROVIDES(p, pp, name)
3700                 {
3701                   s = pool->solvables + p;
3702                   if (s->name != name)
3703                     continue;
3704                   /* keep other versions installed */
3705                   if (s->repo == installed)
3706                     continue;
3707                   /* keep installcandidates of other jobs */
3708                   if (MAPTST(&installcandidatemap, p))
3709                     continue;
3710                   /* don't add the same rule twice */
3711                   for (j = oldnrules; j < k; j++)
3712                     if (solv->rules[j].p == -p)
3713                       break;
3714                   if (j == k)
3715                     solver_addjobrule(solv, -p, 0, i, weak);    /* remove by id */
3716                 }
3717             }
3718           break;
3719
3720         case SOLVER_UPDATE:
3721           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3722           break;
3723         case SOLVER_VERIFY:
3724           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3725           break;
3726         case SOLVER_WEAKENDEPS:
3727           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3728           if (select != SOLVER_SOLVABLE)
3729             break;
3730           s = pool->solvables + what;
3731           weaken_solvable_deps(solv, what);
3732           break;
3733         case SOLVER_MULTIVERSION:
3734           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %smultiversion %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3735           break;
3736         case SOLVER_LOCK:
3737           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3738           if (select == SOLVER_SOLVABLE_ALL)
3739             {
3740               FOR_POOL_SOLVABLES(p)
3741                 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3742             }
3743           else if (select == SOLVER_SOLVABLE_REPO)
3744             {
3745               Repo *repo = pool_id2repo(pool, what);
3746               if (repo)
3747                 FOR_REPO_SOLVABLES(repo, p, s)
3748                   solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3749             }
3750           FOR_JOB_SELECT(p, pp, select, what)
3751             solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3752           break;
3753         case SOLVER_DISTUPGRADE:
3754           POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3755           break;
3756         case SOLVER_DROP_ORPHANED:
3757           POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3758           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3759             solv->droporphanedmap_all = 1;
3760           FOR_JOB_SELECT(p, pp, select, what)
3761             {
3762               s = pool->solvables + p;
3763               if (!installed || s->repo != installed)
3764                 continue;
3765               if (!solv->droporphanedmap.size)
3766                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3767               MAPSET(&solv->droporphanedmap, p - installed->start);
3768             }
3769           break;
3770         case SOLVER_USERINSTALLED:
3771           POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3772           break;
3773         default:
3774           POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3775           break;
3776         }
3777         
3778         /*
3779          * debug
3780          */
3781         
3782       IF_POOLDEBUG (SOLV_DEBUG_JOB)
3783         {
3784           int j;
3785           if (solv->nrules == oldnrules)
3786             POOL_DEBUG(SOLV_DEBUG_JOB, "  - no rule created\n");
3787           for (j = oldnrules; j < solv->nrules; j++)
3788             {
3789               POOL_DEBUG(SOLV_DEBUG_JOB, "  - job ");
3790               solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3791             }
3792         }
3793     }
3794   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3795   solv->jobrules_end = solv->nrules;
3796
3797   /* now create infarch and dup rules */
3798   if (!solv->noinfarchcheck)
3799     {
3800       solver_addinfarchrules(solv, &addedmap);
3801 #if 0
3802       if (pool->implicitobsoleteusescolors)
3803         {
3804           /* currently doesn't work well with infarch rules, so make
3805            * them weak */
3806           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3807             queue_push(&solv->weakruleq, i);
3808         }
3809 #endif
3810     }
3811   else
3812     solv->infarchrules = solv->infarchrules_end = solv->nrules;
3813
3814   if (hasdupjob)
3815     solver_addduprules(solv, &addedmap);
3816   else
3817     solv->duprules = solv->duprules_end = solv->nrules;
3818
3819   if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3820     solver_addbestrules(solv, hasbestinstalljob);
3821   else
3822     solv->bestrules = solv->bestrules_end = solv->nrules;
3823
3824   if (hasdupjob)
3825     solver_freedupmaps(solv);   /* no longer needed */
3826
3827   if (1)
3828     solver_addchoicerules(solv);
3829   else
3830     solv->choicerules = solv->choicerules_end = solv->nrules;
3831
3832   if (0)
3833     {
3834       for (i = solv->featurerules; i < solv->nrules; i++)
3835         solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3836     }
3837   /* all rules created
3838    * --------------------------------------------------------------
3839    * prepare for solving
3840    */
3841
3842   /* free unneeded memory */
3843   map_free(&addedmap);
3844   map_free(&installcandidatemap);
3845   queue_free(&q);
3846
3847   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);
3848   POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3849
3850   /* create weak map */
3851   map_init(&solv->weakrulemap, solv->nrules);
3852   for (i = 0; i < solv->weakruleq.count; i++)
3853     {
3854       p = solv->weakruleq.elements[i];
3855       MAPSET(&solv->weakrulemap, p);
3856     }
3857
3858   /* enable cleandepsmap creation if we have updatepkgs */
3859   if (solv->cleandeps_updatepkgs && !solv->cleandepsmap.size)
3860     map_grow(&solv->cleandepsmap, installed->end - installed->start);
3861   /* no mistakes */
3862   if (solv->cleandeps_mistakes)
3863     {
3864       queue_free(solv->cleandeps_mistakes);
3865       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3866     }
3867
3868   /* all new rules are learnt after this point */
3869   solv->learntrules = solv->nrules;
3870
3871   /* create watches chains */
3872   makewatches(solv);
3873
3874   /* create assertion index. it is only used to speed up
3875    * makeruledecsions() a bit */
3876   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3877     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3878       queue_push(&solv->ruleassertions, i);
3879
3880   /* disable update rules that conflict with our job */
3881   solver_disablepolicyrules(solv);
3882
3883   /* make initial decisions based on assertion rules */
3884   makeruledecisions(solv);
3885   POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3886
3887   /*
3888    * ********************************************
3889    * solve!
3890    * ********************************************
3891    */
3892
3893   now = solv_timems(0);
3894   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3895   POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3896
3897   /*
3898    * prepare solution queue if there were problems
3899    */
3900   solver_prepare_solutions(solv);
3901
3902   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);
3903   POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3904
3905   /* return number of problems */
3906   return solv->problems.count ? solv->problems.count / 2 : 0;
3907 }
3908
3909 Transaction *
3910 solver_create_transaction(Solver *solv)
3911 {
3912   return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->multiversion);
3913 }
3914
3915 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3916 {
3917   queue_free(orphanedq);
3918   queue_init_clone(orphanedq, &solv->orphaned);
3919 }
3920
3921 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3922 {
3923   Pool *pool = solv->pool;
3924   Queue redoq, disabledq;
3925   int goterase, i;
3926   Solvable *s;
3927   Rule *r;
3928   Map obsmap;
3929
3930   if (!recommendationsq && !suggestionsq)
3931     return;
3932
3933   map_init(&obsmap, pool->nsolvables);
3934   if (solv->installed)
3935     {
3936       Id obs, *obsp, p, po, ppo;
3937       for (p = solv->installed->start; p < solv->installed->end; p++)
3938         {
3939           s = pool->solvables + p;
3940           if (s->repo != solv->installed || !s->obsoletes)
3941             continue;
3942           if (solv->decisionmap[p] <= 0)
3943             continue;
3944           if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
3945             continue;
3946           obsp = s->repo->idarraydata + s->obsoletes;
3947           /* foreach obsoletes */
3948           while ((obs = *obsp++) != 0)
3949             FOR_PROVIDES(po, ppo, obs)
3950               MAPSET(&obsmap, po);
3951         }
3952     }
3953
3954   queue_init(&redoq);
3955   queue_init(&disabledq);
3956   goterase = 0;
3957   /* disable all erase jobs (including weak "keep uninstalled" rules) */
3958   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3959     {
3960       if (r->d < 0)     /* disabled ? */
3961         continue;
3962       if (r->p >= 0)    /* install job? */
3963         continue;
3964       queue_push(&disabledq, i);
3965       solver_disablerule(solv, r);
3966       goterase++;
3967     }
3968
3969   if (goterase)
3970     {
3971       enabledisablelearntrules(solv);
3972       removedisabledconflicts(solv, &redoq);
3973     }
3974
3975   /*
3976    * find recommended packages
3977    */
3978   if (recommendationsq)
3979     {
3980       Id rec, *recp, p, pp;
3981
3982       queue_empty(recommendationsq);
3983       /* create map of all recommened packages */
3984       solv->recommends_index = -1;
3985       MAPZERO(&solv->recommendsmap);
3986
3987       /* put all packages the solver already chose in the map */
3988       if (solv->decisioncnt_weak)
3989         {
3990           for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3991             {
3992               Id why;
3993               why = solv->decisionq_why.elements[i];
3994               if (why)
3995                 continue;       /* forced by unit rule or dep resolving */
3996               p = solv->decisionq.elements[i];
3997               if (p < 0)
3998                 continue;
3999               MAPSET(&solv->recommendsmap, p);
4000             }
4001         }
4002
4003       for (i = 0; i < solv->decisionq.count; i++)
4004         {
4005           p = solv->decisionq.elements[i];
4006           if (p < 0)
4007             continue;
4008           s = pool->solvables + p;
4009           if (s->recommends)
4010             {
4011               recp = s->repo->idarraydata + s->recommends;
4012               while ((rec = *recp++) != 0)
4013                 {
4014 #ifdef ENABLE_COMPLEX_DEPS
4015                   if (pool_is_complex_dep(pool, rec))
4016                     {
4017                       do_complex_recommendations(solv, rec, &solv->recommendsmap, noselected);
4018                       continue;
4019                     }
4020 #endif
4021                   FOR_PROVIDES(p, pp, rec)
4022                     if (solv->decisionmap[p] > 0)
4023                       break;
4024                   if (p)
4025                     {
4026                       if (!noselected)
4027                         {
4028                           FOR_PROVIDES(p, pp, rec)
4029                             if (solv->decisionmap[p] > 0)
4030                               MAPSET(&solv->recommendsmap, p);
4031                         }
4032                       continue; /* p != 0: already fulfilled */
4033                     }
4034                   FOR_PROVIDES(p, pp, rec)
4035                     MAPSET(&solv->recommendsmap, p);
4036                 }
4037             }
4038         }
4039       for (i = 1; i < pool->nsolvables; i++)
4040         {
4041           if (solv->decisionmap[i] < 0)
4042             continue;
4043           if (solv->decisionmap[i] > 0 && noselected)
4044             continue;
4045           if (MAPTST(&obsmap, i))
4046             continue;
4047           s = pool->solvables + i;
4048           if (!MAPTST(&solv->recommendsmap, i))
4049             {
4050               if (!s->supplements)
4051                 continue;
4052               if (!pool_installable(pool, s))
4053                 continue;
4054               if (!solver_is_supplementing(solv, s))
4055                 continue;
4056             }
4057           queue_push(recommendationsq, i);
4058         }
4059       /* we use MODE_SUGGEST here so that repo prio is ignored */
4060       policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
4061     }
4062
4063   /*
4064    * find suggested packages
4065    */
4066
4067   if (suggestionsq)
4068     {
4069       Id sug, *sugp, p, pp;
4070
4071       queue_empty(suggestionsq);
4072       /* create map of all suggests that are still open */
4073       solv->recommends_index = -1;
4074       MAPZERO(&solv->suggestsmap);
4075       for (i = 0; i < solv->decisionq.count; i++)
4076         {
4077           p = solv->decisionq.elements[i];
4078           if (p < 0)
4079             continue;
4080           s = pool->solvables + p;
4081           if (s->suggests)
4082             {
4083               sugp = s->repo->idarraydata + s->suggests;
4084               while ((sug = *sugp++) != 0)
4085                 {
4086 #ifdef ENABLE_COMPLEX_DEPS
4087                   if (pool_is_complex_dep(pool, sug))
4088                     {
4089                       do_complex_recommendations(solv, sug, &solv->suggestsmap, noselected);
4090                       continue;
4091                     }
4092 #endif
4093                   FOR_PROVIDES(p, pp, sug)
4094                     if (solv->decisionmap[p] > 0)
4095                       break;
4096                   if (p)
4097                     {
4098                       if (!noselected)
4099                         {
4100                           FOR_PROVIDES(p, pp, sug)
4101                             if (solv->decisionmap[p] > 0)
4102                               MAPSET(&solv->suggestsmap, p);
4103                         }
4104                       continue; /* already fulfilled */
4105                     }
4106                   FOR_PROVIDES(p, pp, sug)
4107                     MAPSET(&solv->suggestsmap, p);
4108                 }
4109             }
4110         }
4111       for (i = 1; i < pool->nsolvables; i++)
4112         {
4113           if (solv->decisionmap[i] < 0)
4114             continue;
4115           if (solv->decisionmap[i] > 0 && noselected)
4116             continue;
4117           if (MAPTST(&obsmap, i))
4118             continue;
4119           s = pool->solvables + i;
4120           if (!MAPTST(&solv->suggestsmap, i))
4121             {
4122               if (!s->enhances)
4123                 continue;
4124               if (!pool_installable(pool, s))
4125                 continue;
4126               if (!solver_is_enhancing(solv, s))
4127                 continue;
4128             }
4129           queue_push(suggestionsq, i);
4130         }
4131       policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
4132     }
4133
4134   /* undo removedisabledconflicts */
4135   if (redoq.count)
4136     undo_removedisabledconflicts(solv, &redoq);
4137   queue_free(&redoq);
4138
4139   /* undo job rule disabling */
4140   for (i = 0; i < disabledq.count; i++)
4141     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
4142   queue_free(&disabledq);
4143   map_free(&obsmap);
4144 }
4145
4146
4147 /***********************************************************************/
4148 /* disk usage computations */
4149
4150 /*-------------------------------------------------------------------
4151  *
4152  * calculate DU changes
4153  */
4154
4155 void
4156 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4157 {
4158   Map installedmap;
4159
4160   solver_create_state_maps(solv, &installedmap, 0);
4161   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
4162   map_free(&installedmap);
4163 }
4164
4165
4166 /*-------------------------------------------------------------------
4167  *
4168  * calculate changes in install size
4169  */
4170
4171 int
4172 solver_calc_installsizechange(Solver *solv)
4173 {
4174   Map installedmap;
4175   int change;
4176
4177   solver_create_state_maps(solv, &installedmap, 0);
4178   change = pool_calc_installsizechange(solv->pool, &installedmap);
4179   map_free(&installedmap);
4180   return change;
4181 }
4182
4183 void
4184 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
4185 {
4186   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
4187 }
4188
4189 void
4190 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
4191 {
4192   Pool *pool = solv->pool;
4193   Map installedmap;
4194   int i;
4195   pool_create_state_maps(pool,  &solv->decisionq, &installedmap, 0);
4196   pool_trivial_installable_multiversionmap(pool, &installedmap, pkgs, res, solv->multiversion.size ? &solv->multiversion : 0);
4197   for (i = 0; i < res->count; i++)
4198     if (res->elements[i] != -1)
4199       {
4200         Solvable *s = pool->solvables + pkgs->elements[i];
4201         if (!strncmp("patch:", pool_id2str(pool, s->name), 6) && solvable_is_irrelevant_patch(s, &installedmap))
4202           res->elements[i] = -1;
4203       }
4204   map_free(&installedmap);
4205 }
4206
4207 /*-------------------------------------------------------------------
4208  *
4209  * decision introspection
4210  */
4211
4212 int
4213 solver_get_decisionlevel(Solver *solv, Id p)
4214 {
4215   return solv->decisionmap[p];
4216 }
4217
4218 void
4219 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
4220 {
4221   queue_free(decisionq);
4222   queue_init_clone(decisionq, &solv->decisionq);
4223 }
4224
4225 int
4226 solver_get_lastdecisionblocklevel(Solver *solv)
4227 {
4228   Id p;
4229   if (solv->decisionq.count == 0)
4230     return 0;
4231   p = solv->decisionq.elements[solv->decisionq.count - 1];
4232   if (p < 0)
4233     p = -p;
4234   return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
4235 }
4236
4237 void
4238 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
4239 {
4240   Id p;
4241   int i;
4242
4243   queue_empty(decisionq);
4244   for (i = 0; i < solv->decisionq.count; i++)
4245     {
4246       p = solv->decisionq.elements[i];
4247       if (p < 0)
4248         p = -p;
4249       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4250         break;
4251     }
4252   if (i == solv->decisionq.count)
4253     return;
4254   for (i = 0; i < solv->decisionq.count; i++)
4255     {
4256       p = solv->decisionq.elements[i];
4257       if (p < 0)
4258         p = -p;
4259       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4260         queue_push(decisionq, p);
4261       else
4262         break;
4263     }
4264 }
4265
4266 int
4267 solver_describe_decision(Solver *solv, Id p, Id *infop)
4268 {
4269   int i;
4270   Id pp, why;
4271
4272   if (infop)
4273     *infop = 0;
4274   if (!solv->decisionmap[p])
4275     return SOLVER_REASON_UNRELATED;
4276   pp = solv->decisionmap[p] < 0 ? -p : p;
4277   for (i = 0; i < solv->decisionq.count; i++)
4278     if (solv->decisionq.elements[i] == pp)
4279       break;
4280   if (i == solv->decisionq.count)       /* just in case... */
4281     return SOLVER_REASON_UNRELATED;
4282   why = solv->decisionq_why.elements[i];
4283   if (infop)
4284     *infop = why > 0 ? why : -why;
4285   if (why > 0)
4286     return SOLVER_REASON_UNIT_RULE;
4287   why = -why;
4288   if (i < solv->decisioncnt_update)
4289     {
4290       if (i == 0)
4291         return SOLVER_REASON_KEEP_INSTALLED;
4292       return SOLVER_REASON_RESOLVE_JOB;
4293     }
4294   if (i < solv->decisioncnt_keep)
4295     {
4296       if (why == 0 && pp < 0)
4297         return SOLVER_REASON_CLEANDEPS_ERASE;
4298       return SOLVER_REASON_UPDATE_INSTALLED;
4299     }
4300   if (i < solv->decisioncnt_resolve)
4301     {
4302       if (why == 0 && pp < 0)
4303         return SOLVER_REASON_CLEANDEPS_ERASE;
4304       return SOLVER_REASON_KEEP_INSTALLED;
4305     }
4306   if (why > 0)
4307     return SOLVER_REASON_RESOLVE;
4308   /* weak or orphaned */
4309   if (solv->decisionq.count < solv->decisioncnt_orphan)
4310     return SOLVER_REASON_WEAKDEP;
4311   return SOLVER_REASON_RESOLVE_ORPHAN;
4312 }
4313
4314
4315 void
4316 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
4317 {
4318   Pool *pool = solv->pool;
4319   int i;
4320   int level = solv->decisionmap[p];
4321   int decisionno;
4322   Solvable *s;
4323
4324   queue_empty(whyq);
4325   if (level < 0)
4326     return;     /* huh? */
4327   for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
4328     if (solv->decisionq.elements[decisionno] == p)
4329       break;
4330   if (decisionno == solv->decisionq.count)
4331     return;     /* huh? */
4332   if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
4333     return;     /* huh? */
4334
4335   /* 1) list all packages that recommend us */
4336   for (i = 1; i < pool->nsolvables; i++)
4337     {
4338       Id *recp, rec, pp2, p2;
4339       if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
4340         continue;
4341       s = pool->solvables + i;
4342       if (!s->recommends)
4343         continue;
4344       if (!solv->addalreadyrecommended && s->repo == solv->installed)
4345         continue;
4346       recp = s->repo->idarraydata + s->recommends;
4347       while ((rec = *recp++) != 0)
4348         {
4349           int found = 0;
4350           FOR_PROVIDES(p2, pp2, rec)
4351             {
4352               if (p2 == p)
4353                 found = 1;
4354               else
4355                 {
4356                   /* if p2 is already installed, this recommends is ignored */
4357                   if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4358                     break;
4359                 }
4360             }
4361           if (!p2 && found)
4362             {
4363               queue_push(whyq, SOLVER_REASON_RECOMMENDED);
4364               queue_push2(whyq, p2, rec);
4365             }
4366         }
4367     }
4368   /* 2) list all supplements */
4369   s = pool->solvables + p;
4370   if (s->supplements && level > 0)
4371     {
4372       Id *supp, sup, pp2, p2;
4373       /* this is a hack. to use solver_dep_fulfilled we temporarily clear
4374        * everything above our level in the decisionmap */
4375       for (i = decisionno; i < solv->decisionq.count; i++ )
4376         {
4377           p2 = solv->decisionq.elements[i];
4378           if (p2 > 0)
4379             solv->decisionmap[p2] = -solv->decisionmap[p2];
4380         }
4381       supp = s->repo->idarraydata + s->supplements;
4382       while ((sup = *supp++) != 0)
4383         if (solver_dep_fulfilled(solv, sup))
4384           {
4385             int found = 0;
4386             /* let's see if this is an easy supp */
4387             FOR_PROVIDES(p2, pp2, sup)
4388               {
4389                 if (!solv->addalreadyrecommended && solv->installed)
4390                   {
4391                     if (pool->solvables[p2].repo == solv->installed)
4392                       continue;
4393                   }
4394                 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4395                   {
4396                     queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4397                     queue_push2(whyq, p2, sup);
4398                     found = 1;
4399                   }
4400               }
4401             if (!found)
4402               {
4403                 /* hard case, just note dependency with no package */
4404                 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4405                 queue_push2(whyq, 0, sup);
4406               }
4407           }
4408       for (i = decisionno; i < solv->decisionq.count; i++)
4409         {
4410           p2 = solv->decisionq.elements[i];
4411           if (p2 > 0)
4412             solv->decisionmap[p2] = -solv->decisionmap[p2];
4413         }
4414     }
4415 }
4416
4417 void
4418 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
4419 {
4420   Id p, pp;
4421   how &= SOLVER_SELECTMASK;
4422   queue_empty(pkgs);
4423   if (how == SOLVER_SOLVABLE_ALL)
4424     {
4425       FOR_POOL_SOLVABLES(p)
4426         queue_push(pkgs, p);
4427     }
4428   else if (how == SOLVER_SOLVABLE_REPO)
4429     {
4430       Repo *repo = pool_id2repo(pool, what);
4431       Solvable *s;
4432       if (repo)
4433         FOR_REPO_SOLVABLES(repo, p, s)
4434           queue_push(pkgs, p);
4435     }
4436   else
4437     {
4438       FOR_JOB_SELECT(p, pp, how, what)
4439         queue_push(pkgs, p);
4440     }
4441 }
4442
4443 int
4444 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
4445 {
4446   Id p, pp, pi, pip;
4447   Id select = how & SOLVER_SELECTMASK;
4448   if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
4449     return 0;
4450   if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
4451     return 0;
4452   if (!pool->installed)
4453     return 1;
4454   FOR_JOB_SELECT(p, pp, select, what)
4455     if (pool->solvables[p].repo == pool->installed)
4456       return 0;
4457   /* hard work */
4458   FOR_JOB_SELECT(p, pp, select, what)
4459     {
4460       Solvable *s = pool->solvables + p;
4461       FOR_PROVIDES(pi, pip, s->name)
4462         {
4463           Solvable *si = pool->solvables + pi;
4464           if (si->repo != pool->installed || si->name != s->name)
4465             continue;
4466           return 0;
4467         }
4468       if (s->obsoletes)
4469         {
4470           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4471           while ((obs = *obsp++) != 0)
4472             {
4473               FOR_PROVIDES(pi, pip, obs)
4474                 {
4475                   Solvable *si = pool->solvables + pi;
4476                   if (si->repo != pool->installed)
4477                     continue;
4478                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
4479                     continue;
4480                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
4481                     continue;
4482                   return 0;
4483                 }
4484             }
4485         }
4486     }
4487   return 1;
4488 }
4489
4490 static int
4491 get_userinstalled_cmp(const void *ap, const void *bp, void *dp)
4492 {
4493   return *(Id *)ap - *(Id *)bp;
4494 }
4495
4496 static int
4497 get_userinstalled_cmp_names(const void *ap, const void *bp, void *dp)
4498 {
4499   Pool *pool = dp;
4500   return strcmp(pool_id2str(pool, *(Id *)ap), pool_id2str(pool, *(Id *)bp));
4501 }
4502
4503 static void
4504 get_userinstalled_sort_uniq(Pool *pool, Queue *q, int flags)
4505 {
4506   Id lastp = -1;
4507   int i, j;
4508   if ((flags & GET_USERINSTALLED_NAMES) != 0)
4509     solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp_names, pool);
4510   else
4511     solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp, 0);
4512   for (i = j = 0; i < q->count; i++)
4513     if (q->elements[i] != lastp)
4514       q->elements[j++] = lastp = q->elements[i];
4515   queue_truncate(q, j);
4516 }
4517
4518 void
4519 solver_get_userinstalled(Solver *solv, Queue *q, int flags)
4520 {
4521   Pool *pool = solv->pool;
4522   Id p, p2, pp;
4523   Solvable *s;
4524   Repo *installed = solv->installed;
4525   int i, j;
4526   Map userinstalled;
4527   
4528   map_init(&userinstalled, 0);
4529   queue_empty(q);
4530   /* first process jobs */
4531   for (i = 0; i < solv->job.count; i += 2)
4532     {
4533       Id how = solv->job.elements[i];
4534       Id what, select;
4535       if (installed && (how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
4536         {
4537           if (!userinstalled.size)
4538             map_grow(&userinstalled, installed->end - installed->start);
4539           what = solv->job.elements[i + 1];
4540           select = how & SOLVER_SELECTMASK;
4541           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
4542             FOR_REPO_SOLVABLES(installed, p, s)
4543               MAPSET(&userinstalled, p - installed->start);
4544           FOR_JOB_SELECT(p, pp, select, what)
4545             if (pool->solvables[p].repo == installed)
4546               MAPSET(&userinstalled, p - installed->start);
4547           continue;
4548         }
4549       if ((how & SOLVER_JOBMASK) != SOLVER_INSTALL)
4550         continue;
4551       if ((how & SOLVER_NOTBYUSER) != 0)
4552         continue;
4553       what = solv->job.elements[i + 1];
4554       select = how & SOLVER_SELECTMASK;
4555       FOR_JOB_SELECT(p, pp, select, what)
4556         if (solv->decisionmap[p] > 0)
4557           {
4558             queue_push(q, p);
4559 #ifdef ENABLE_LINKED_PKGS
4560             if (has_package_link(pool, pool->solvables + p))
4561               {
4562                 int j;
4563                 Queue lq;
4564                 queue_init(&lq);
4565                 find_package_link(pool, pool->solvables + p, 0, &lq, 0, 0);
4566                 for (j = 0; j < lq.count; j++)
4567                   if (solv->decisionmap[lq.elements[j]] > 0)
4568                     queue_push(q, lq.elements[j]);
4569               }
4570 #endif
4571           }
4572     }
4573   /* now process updates of userinstalled packages */
4574   if (installed && userinstalled.size)
4575     {
4576       for (i = 1; i < solv->decisionq.count; i++)
4577         {
4578           p = solv->decisionq.elements[i];
4579           if (p <= 0)
4580             continue;
4581           s = pool->solvables + p;
4582           if (!s->repo)
4583             continue;
4584           if (s->repo == installed)
4585             {
4586               if (MAPTST(&userinstalled, p - installed->start))
4587                 queue_push(q, p);
4588               continue;
4589             }
4590           /* new package, check if we replace a userinstalled one */
4591           FOR_PROVIDES(p2, pp, s->name)
4592             {
4593               Solvable *ps = pool->solvables + p2;
4594               if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
4595                 continue;
4596               if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
4597                 continue;
4598               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
4599                 continue;
4600               queue_push(q, p);
4601               break;
4602             }
4603           if (!p2 && s->repo != installed && s->obsoletes)
4604             {
4605               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4606               while ((obs = *obsp++) != 0)
4607                 {
4608                   FOR_PROVIDES(p2, pp, obs)
4609                     {
4610                       Solvable *ps = pool->solvables + p2;
4611                       if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
4612                         continue;
4613                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
4614                         continue;
4615                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps)) 
4616                         continue;
4617                       queue_push(q, p); 
4618                       break;
4619                     }
4620                   if (p2)
4621                     break;
4622                 }
4623             }
4624         }
4625     }
4626   map_free(&userinstalled);
4627   /* convert to names if asked */
4628   if ((flags & GET_USERINSTALLED_NAMES) != 0)
4629     {
4630       for (i = 0; i < q->count; i++)
4631         {
4632           s = pool->solvables + q->elements[i];
4633           q->elements[i] = s->name;
4634         }
4635     }
4636   /* sort and unify */
4637   if (q->count > 1)
4638     get_userinstalled_sort_uniq(pool, q, flags);
4639   /* invert if asked */
4640   if ((flags & GET_USERINSTALLED_INVERTED) != 0)
4641     {
4642       /* first generate queue with all installed packages */
4643       Queue invq;
4644       queue_init(&invq);
4645       for (i = 1; i < solv->decisionq.count; i++)
4646         {
4647           p = solv->decisionq.elements[i];
4648           if (p <= 0)
4649             continue;
4650           s = pool->solvables + p;
4651           if (!s->repo)
4652             continue;
4653           if ((flags & GET_USERINSTALLED_NAMES) != 0)
4654             queue_push(&invq, s->name);
4655           else
4656             queue_push(&invq, p);
4657         }
4658       /* push q on invq, just in case... */
4659       queue_insertn(&invq, invq.count, q->count, q->elements);
4660       if (invq.count > 1)
4661         get_userinstalled_sort_uniq(pool, &invq, flags);
4662       /* subtract queues (easy as they are sorted and invq is a superset of q) */
4663       if (q->count)
4664         {
4665           for (i = j = 0; i < invq.count; i++)
4666             if (invq.elements[i] == q->elements[j])
4667               {
4668                 invq.elements[i] = 0;
4669                 if (++j >= q->count)
4670                   break;
4671               }
4672           queue_empty(q);
4673         }
4674       for (i = j = 0; i < invq.count; i++)
4675         if (invq.elements[i])
4676           queue_push(q, invq.elements[i]);
4677       queue_free(&invq);
4678     }
4679 }
4680
4681 void
4682 pool_add_userinstalled_jobs(Pool *pool, Queue *q, Queue *job, int flags)
4683 {
4684   int i;
4685
4686   if (flags & GET_USERINSTALLED_INVERTED)
4687     {
4688       Queue invq;
4689       Id p, lastid;
4690       Solvable *s;
4691       int bad;
4692       if (!pool->installed)
4693         return;
4694       queue_init(&invq);
4695       FOR_REPO_SOLVABLES(pool->installed, p, s)
4696         queue_push(&invq, flags & GET_USERINSTALLED_NAMES ? s->name : p);
4697       queue_insertn(&invq, invq.count, q->count, q->elements);
4698       if (invq.count > 1)
4699         get_userinstalled_sort_uniq(pool, &invq, flags);
4700       /* now the fun part, add q again, sort, and remove all dups */
4701       queue_insertn(&invq, invq.count, q->count, q->elements);
4702       if (invq.count > 1)
4703         {
4704           if ((flags & GET_USERINSTALLED_NAMES) != 0)
4705             solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp_names, pool);
4706           else
4707             solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp, 0);
4708         }
4709       lastid = -1;
4710       bad = 1;
4711       for (i = 0; i < invq.count; i++)
4712         {
4713           if (invq.elements[i] == lastid)
4714             {
4715               bad = 1;
4716               continue;
4717             }
4718           if (!bad)
4719             queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
4720           bad = 0;
4721           lastid = invq.elements[i];
4722         }
4723       if (!bad)
4724         queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
4725       queue_free(&invq);
4726     }
4727   else
4728     {
4729       for (i = 0; i < q->count; i++)
4730         queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), q->elements[i]);
4731     }
4732 }
4733
4734 const char *
4735 solver_select2str(Pool *pool, Id select, Id what)
4736 {
4737   const char *s;
4738   char *b;
4739   select &= SOLVER_SELECTMASK;
4740   if (select == SOLVER_SOLVABLE)
4741     return pool_solvid2str(pool, what);
4742   if (select == SOLVER_SOLVABLE_NAME)
4743     return pool_dep2str(pool, what);
4744   if (select == SOLVER_SOLVABLE_PROVIDES)
4745     {
4746       s = pool_dep2str(pool, what);
4747       b = pool_alloctmpspace(pool, 11 + strlen(s));
4748       sprintf(b, "providing %s", s);
4749       return b;
4750     }
4751   if (select == SOLVER_SOLVABLE_ONE_OF)
4752     {
4753       Id p;
4754       b = 0;
4755       while ((p = pool->whatprovidesdata[what++]) != 0)
4756         {
4757           s = pool_solvid2str(pool, p);
4758           if (b)
4759             b = pool_tmpappend(pool, b, ", ", s);
4760           else
4761             b = pool_tmpjoin(pool, s, 0, 0);
4762           pool_freetmpspace(pool, s);
4763         }
4764       return b ? b : "nothing";
4765     }
4766   if (select == SOLVER_SOLVABLE_REPO)
4767     {
4768       b = pool_alloctmpspace(pool, 20);
4769       sprintf(b, "repo #%d", what);
4770       return b;
4771     }
4772   if (select == SOLVER_SOLVABLE_ALL)
4773     return "all packages";
4774   return "unknown job select";
4775 }
4776
4777 const char *
4778 pool_job2str(Pool *pool, Id how, Id what, Id flagmask)
4779 {
4780   Id select = how & SOLVER_SELECTMASK;
4781   const char *strstart = 0, *strend = 0;
4782   char *s;
4783   int o;
4784
4785   switch (how & SOLVER_JOBMASK)
4786     {
4787     case SOLVER_NOOP:
4788       return "do nothing";
4789     case SOLVER_INSTALL:
4790       if (select == SOLVER_SOLVABLE && pool->installed && pool->solvables[what].repo == pool->installed)
4791         strstart = "keep ", strend = " installed";
4792       else if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_NAME)
4793         strstart = "install ";
4794       else if (select == SOLVER_SOLVABLE_PROVIDES)
4795         strstart = "install a package ";
4796       else
4797         strstart = "install one of ";
4798       break;
4799     case SOLVER_ERASE:
4800       if (select == SOLVER_SOLVABLE && !(pool->installed && pool->solvables[what].repo == pool->installed))
4801         strstart = "keep ", strend = " uninstalled";
4802       else if (select == SOLVER_SOLVABLE_PROVIDES)
4803         strstart = "deinstall all packages ";
4804       else
4805         strstart = "deinstall ";
4806       break;
4807     case SOLVER_UPDATE:
4808       strstart = "update ";
4809       break;
4810     case SOLVER_WEAKENDEPS:
4811       strstart = "weaken deps of ";
4812       break;
4813     case SOLVER_MULTIVERSION:
4814       strstart = "multi version ";
4815       break;
4816     case SOLVER_LOCK:
4817       strstart = "lock ";
4818       break;
4819     case SOLVER_DISTUPGRADE:
4820       strstart = "dist upgrade ";
4821       break;
4822     case SOLVER_VERIFY:
4823       strstart = "verify ";
4824       break;
4825     case SOLVER_DROP_ORPHANED:
4826       strstart = "deinstall ", strend = " if orphaned";
4827       break;
4828     case SOLVER_USERINSTALLED:
4829       strstart = "regard ", strend = " as userinstalled";
4830       break;
4831     default:
4832       strstart = "unknown job ";
4833       break;
4834     }
4835   s = pool_tmpjoin(pool, strstart, solver_select2str(pool, select, what), strend);
4836   how &= flagmask;
4837   if ((how & ~(SOLVER_SELECTMASK|SOLVER_JOBMASK)) == 0)
4838     return s;
4839   o = strlen(s);
4840   s = pool_tmpappend(pool, s, " ", 0);
4841   if (how & SOLVER_WEAK)
4842     s = pool_tmpappend(pool, s, ",weak", 0);
4843   if (how & SOLVER_ESSENTIAL)
4844     s = pool_tmpappend(pool, s, ",essential", 0);
4845   if (how & SOLVER_CLEANDEPS)
4846     s = pool_tmpappend(pool, s, ",cleandeps", 0);
4847   if (how & SOLVER_ORUPDATE)
4848     s = pool_tmpappend(pool, s, ",orupdate", 0);
4849   if (how & SOLVER_FORCEBEST)
4850     s = pool_tmpappend(pool, s, ",forcebest", 0);
4851   if (how & SOLVER_TARGETED)
4852     s = pool_tmpappend(pool, s, ",targeted", 0);
4853   if (how & SOLVER_SETEV)
4854     s = pool_tmpappend(pool, s, ",setev", 0);
4855   if (how & SOLVER_SETEVR)
4856     s = pool_tmpappend(pool, s, ",setevr", 0);
4857   if (how & SOLVER_SETARCH)
4858     s = pool_tmpappend(pool, s, ",setarch", 0);
4859   if (how & SOLVER_SETVENDOR)
4860     s = pool_tmpappend(pool, s, ",setvendor", 0);
4861   if (how & SOLVER_SETREPO)
4862     s = pool_tmpappend(pool, s, ",setrepo", 0);
4863   if (how & SOLVER_SETNAME)
4864     s = pool_tmpappend(pool, s, ",setname", 0);
4865   if (how & SOLVER_NOAUTOSET)
4866     s = pool_tmpappend(pool, s, ",noautoset", 0);
4867   if (s[o + 1] != ',')
4868     s = pool_tmpappend(pool, s, ",?", 0);
4869   s[o + 1] = '[';
4870   return pool_tmpappend(pool, s, "]", 0);
4871 }
4872