83426f8ebfd2de26966e36011e5aade9dbf6000a
[platform/upstream/libsolv.git] / src / rules.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * rules.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 "poolarch.h"
25 #include "util.h"
26 #include "evr.h"
27 #include "policy.h"
28 #include "solverdebug.h"
29
30 #define RULES_BLOCK 63
31
32 static void addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep);
33 static void solver_createcleandepsmap(Solver *solv);
34
35 /*-------------------------------------------------------------------
36  * Check if dependency is possible
37  * 
38  * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap
39  * used in solver_addrpmrulesforweak and solver_createcleandepsmap
40  */
41
42 static inline int
43 dep_possible(Solver *solv, Id dep, Map *m)
44 {
45   Pool *pool = solv->pool;
46   Id p, pp;
47
48   if (ISRELDEP(dep))
49     {
50       Reldep *rd = GETRELDEP(pool, dep);
51       if (rd->flags >= 8)
52         {
53           if (rd->flags == REL_AND)
54             {
55               if (!dep_possible(solv, rd->name, m))
56                 return 0;
57               return dep_possible(solv, rd->evr, m);
58             }
59           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
60             return solver_splitprovides(solv, rd->evr);
61           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
62             return solver_dep_installed(solv, rd->evr);
63         }
64     }
65   FOR_PROVIDES(p, pp, dep)
66     {
67       if (MAPTST(m, p))
68         return 1;
69     }
70   return 0;
71 }
72
73 /********************************************************************
74  *
75  * Rule handling
76  *
77  * - unify rules, remove duplicates
78  */
79
80 /*-------------------------------------------------------------------
81  *
82  * compare rules for unification sort
83  *
84  */
85
86 static int
87 unifyrules_sortcmp(const void *ap, const void *bp, void *dp)
88 {
89   Pool *pool = dp;
90   Rule *a = (Rule *)ap;
91   Rule *b = (Rule *)bp;
92   Id *ad, *bd;
93   int x;
94
95   x = a->p - b->p;
96   if (x)
97     return x;                          /* p differs */
98
99   /* identical p */
100   if (a->d == 0 && b->d == 0)
101     return a->w2 - b->w2;              /* assertion: return w2 diff */
102
103   if (a->d == 0)                       /* a is assertion, b not */
104     {
105       x = a->w2 - pool->whatprovidesdata[b->d];
106       return x ? x : -1;
107     }
108
109   if (b->d == 0)                       /* b is assertion, a not */
110     {
111       x = pool->whatprovidesdata[a->d] - b->w2;
112       return x ? x : 1;
113     }
114
115   /* compare whatprovidesdata */
116   ad = pool->whatprovidesdata + a->d;
117   bd = pool->whatprovidesdata + b->d;
118   while (*bd)
119     if ((x = *ad++ - *bd++) != 0)
120       return x;
121   return *ad;
122 }
123
124 int
125 solver_samerule(Solver *solv, Rule *r1, Rule *r2)
126 {
127   return unifyrules_sortcmp(r1, r2, solv->pool);
128 }
129
130
131 /*-------------------------------------------------------------------
132  *
133  * unify rules
134  * go over all rules and remove duplicates
135  */
136
137 void
138 solver_unifyrules(Solver *solv)
139 {
140   Pool *pool = solv->pool;
141   int i, j;
142   Rule *ir, *jr;
143
144   if (solv->nrules <= 2)               /* nothing to unify */
145     return;
146
147   /* sort rules first */
148   solv_sort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp, solv->pool);
149
150   /* prune rules
151    * i = unpruned
152    * j = pruned
153    */
154   jr = 0;
155   for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
156     {
157       if (jr && !unifyrules_sortcmp(ir, jr, pool))
158         continue;                      /* prune! */
159       jr = solv->rules + j++;          /* keep! */
160       if (ir != jr)
161         *jr = *ir;
162     }
163
164   /* reduced count from nrules to j rules */
165   POOL_DEBUG(SOLV_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
166
167   /* adapt rule buffer */
168   solv->nrules = j;
169   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
170
171   /*
172    * debug: log rule statistics
173    */
174   IF_POOLDEBUG (SOLV_DEBUG_STATS)
175     {
176       int binr = 0;
177       int lits = 0;
178       Id *dp;
179       Rule *r;
180
181       for (i = 1; i < solv->nrules; i++)
182         {
183           r = solv->rules + i;
184           if (r->d == 0)
185             binr++;
186           else
187             {
188               dp = solv->pool->whatprovidesdata + r->d;
189               while (*dp++)
190                 lits++;
191             }
192         }
193       POOL_DEBUG(SOLV_DEBUG_STATS, "  binary: %d\n", binr);
194       POOL_DEBUG(SOLV_DEBUG_STATS, "  normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
195     }
196 }
197
198 #if 0
199
200 /*
201  * hash rule
202  */
203
204 static Hashval
205 hashrule(Solver *solv, Id p, Id d, int n)
206 {
207   unsigned int x = (unsigned int)p;
208   int *dp;
209
210   if (n <= 1)
211     return (x * 37) ^ (unsigned int)d;
212   dp = solv->pool->whatprovidesdata + d;
213   while (*dp)
214     x = (x * 37) ^ (unsigned int)*dp++;
215   return x;
216 }
217 #endif
218
219
220 /*-------------------------------------------------------------------
221  * 
222  */
223
224 /*
225  * add rule
226  *  p = direct literal; always < 0 for installed rpm rules
227  *  d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
228  *
229  *
230  * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
231  *
232  * p < 0 : pkg id of A
233  * d > 0 : Offset in whatprovidesdata (list of providers of b)
234  *
235  * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
236  * p < 0 : pkg id of A
237  * d < 0 : Id of solvable (e.g. B1)
238  *
239  * d == 0: unary rule, assertion => (A) or (-A)
240  *
241  *   Install:    p > 0, d = 0   (A)             user requested install
242  *   Remove:     p < 0, d = 0   (-A)            user requested remove (also: uninstallable)
243  *   Requires:   p < 0, d > 0   (-A|B1|B2|...)  d: <list of providers for requirement of p>
244  *   Updates:    p > 0, d > 0   (A|B1|B2|...)   d: <list of updates for solvable p>
245  *   Conflicts:  p < 0, d < 0   (-A|-B)         either p (conflict issuer) or d (conflict provider) (binary rule)
246  *                                              also used for obsoletes
247  *   ?:          p > 0, d < 0   (A|-B)          
248  *   No-op ?:    p = 0, d = 0   (null)          (used as policy rule placeholder)
249  *
250  *   resulting watches:
251  *   ------------------
252  *   Direct assertion (no watch needed) --> d = 0, w1 = p, w2 = 0
253  *   Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
254  *   every other : w1 = p, w2 = whatprovidesdata[d];
255  *   Disabled rule: w1 = 0
256  *
257  *   always returns a rule for non-rpm rules
258  */
259
260 Rule *
261 solver_addrule(Solver *solv, Id p, Id d)
262 {
263   Pool *pool = solv->pool;
264   Rule *r = 0;
265   Id *dp = 0;
266
267   int n = 0;                           /* number of literals in rule - 1
268                                           0 = direct assertion (single literal)
269                                           1 = binary rule
270                                           >1 = 
271                                         */
272
273   /* it often happenes that requires lead to adding the same rpm rule
274    * multiple times, so we prune those duplicates right away to make
275    * the work for unifyrules a bit easier */
276
277   if (!solv->rpmrules_end)              /* we add rpm rules */
278     {
279       r = solv->rules + solv->nrules - 1;       /* get the last added rule */
280       if (r->p == p && r->d == d && (d != 0 || !r->w2))
281         return r;
282     }
283
284     /*
285      * compute number of literals (n) in rule
286      */
287     
288   if (d < 0)
289     {
290       /* always a binary rule */
291       if (p == d)
292         return 0;                      /* ignore self conflict */
293       n = 1;
294     }
295   else if (d > 0)
296     {
297       for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
298         if (*dp == -p)
299           return 0;                     /* rule is self-fulfilling */
300         
301       if (n == 1)                       /* convert to binary rule */
302         d = dp[-1];
303     }
304
305   if (n == 1 && p > d && !solv->rpmrules_end)
306     {
307       /* smallest literal first so we can find dups */
308       n = p; p = d; d = n;             /* p <-> d */
309       n = 1;                           /* re-set n, was used as temp var */
310     }
311
312   /*
313    * check for duplicate
314    */
315     
316   /* check if the last added rule (r) is exactly the same as what we're looking for. */
317   if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
318     return r;  /* binary rule */
319
320     /* have n-ary rule with same first literal, check other literals */
321   if (r && n > 1 && r->d && r->p == p)
322     {
323       /* Rule where d is an offset in whatprovidesdata */
324       Id *dp2;
325       if (d == r->d)
326         return r;
327       dp2 = pool->whatprovidesdata + r->d;
328       for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
329         if (*dp != *dp2)
330           break;
331       if (*dp == *dp2)
332         return r;
333    }
334
335   /*
336    * allocate new rule
337    */
338
339   /* extend rule buffer */
340   solv->rules = solv_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
341   r = solv->rules + solv->nrules++;    /* point to rule space */
342
343     /*
344      * r = new rule
345      */
346     
347   r->p = p;
348   if (n == 0)
349     {
350       /* direct assertion, no watch needed */
351       r->d = 0;
352       r->w1 = p;
353       r->w2 = 0;
354     }
355   else if (n == 1)
356     {
357       /* binary rule */
358       r->d = 0;
359       r->w1 = p;
360       r->w2 = d;
361     }
362   else
363     {
364       r->d = d;
365       r->w1 = p;
366       r->w2 = pool->whatprovidesdata[d];
367     }
368   r->n1 = 0;
369   r->n2 = 0;
370
371   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
372     {
373       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "  Add rule: ");
374       solver_printrule(solv, SOLV_DEBUG_RULE_CREATION, r);
375     }
376
377   return r;
378 }
379
380
381 /******************************************************************************
382  ***
383  *** rpm rule part: create rules representing the package dependencies
384  ***
385  ***/
386
387 /*
388  *  special multiversion patch conflict handling:
389  *  a patch conflict is also satisfied if some other
390  *  version with the same name/arch that doesn't conflict
391  *  gets installed. The generated rule is thus:
392  *  -patch|-cpack|opack1|opack2|...
393  */
394 static Id
395 makemultiversionconflict(Solver *solv, Id n, Id con)
396 {
397   Pool *pool = solv->pool;
398   Solvable *s, *sn;
399   Queue q;
400   Id p, pp, qbuf[64];
401
402   sn = pool->solvables + n;
403   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
404   queue_push(&q, -n);
405   FOR_PROVIDES(p, pp, sn->name)
406     {
407       s = pool->solvables + p;
408       if (s->name != sn->name || s->arch != sn->arch)
409         continue;
410       if (!MAPTST(&solv->noobsoletes, p))
411         continue;
412       if (pool_match_nevr(pool, pool->solvables + p, con))
413         continue;
414       /* here we have a multiversion solvable that doesn't conflict */
415       /* thus we're not in conflict if it is installed */
416       queue_push(&q, p);
417     }
418   if (q.count == 1)
419     return -n;  /* no other package found, generate normal conflict */
420   return pool_queuetowhatprovides(pool, &q);
421 }
422
423 static inline void
424 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
425 {
426   if (!solv->ruleinfoq)
427     solver_addrule(solv, p, d);
428   else
429     addrpmruleinfo(solv, p, d, type, dep);
430 }
431
432 /*-------------------------------------------------------------------
433  * 
434  * add (install) rules for solvable
435  * 
436  * s: Solvable for which to add rules
437  * m: m[s] = 1 for solvables which have rules, prevent rule duplication
438  * 
439  * Algorithm: 'visit all nodes of a graph'. The graph nodes are
440  *  solvables, the edges their dependencies.
441  *  Starting from an installed solvable, this will create all rules
442  *  representing the graph created by the solvables dependencies.
443  * 
444  * for unfulfilled requirements, conflicts, obsoletes,....
445  * add a negative assertion for solvables that are not installable
446  * 
447  * It will also create rules for all solvables referenced by 's'
448  *  i.e. descend to all providers of requirements of 's'
449  *
450  */
451
452 void
453 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
454 {
455   Pool *pool = solv->pool;
456   Repo *installed = solv->installed;
457
458   /* 'work' queue. keeps Ids of solvables we still have to work on.
459      And buffer for it. */
460   Queue workq;
461   Id workqbuf[64];
462     
463   int i;
464     /* if to add rules for broken deps ('rpm -V' functionality)
465      * 0 = yes, 1 = no
466      */
467   int dontfix;
468     /* Id var and pointer for each dependency
469      * (not used in parallel)
470      */
471   Id req, *reqp;
472   Id con, *conp;
473   Id obs, *obsp;
474   Id rec, *recp;
475   Id sug, *sugp;
476   Id p, pp;             /* whatprovides loops */
477   Id *dp;               /* ptr to 'whatprovides' */
478   Id n;                 /* Id for current solvable 's' */
479
480   queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
481   queue_push(&workq, s - pool->solvables);      /* push solvable Id to work queue */
482
483   /* loop until there's no more work left */
484   while (workq.count)
485     {
486       /*
487        * n: Id of solvable
488        * s: Pointer to solvable
489        */
490
491       n = queue_shift(&workq);          /* 'pop' next solvable to work on from queue */
492       if (m)
493         {
494           if (MAPTST(m, n))             /* continue if already visited */
495             continue;
496           MAPSET(m, n);                 /* mark as visited */
497         }
498
499       s = pool->solvables + n;          /* s = Solvable in question */
500
501       dontfix = 0;
502       if (installed                     /* Installed system available */
503           && s->repo == installed       /* solvable is installed */
504           && !solv->fixmap_all          /* NOT repair errors in rpm dependency graph */
505           && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
506         {
507           dontfix = 1;                  /* dont care about broken rpm deps */
508         }
509
510       if (!dontfix
511           && s->arch != ARCH_SRC
512           && s->arch != ARCH_NOSRC
513           && !pool_installable(pool, s))
514         {
515           POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables));
516           addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
517         }
518
519       /* yet another SUSE hack, sigh */
520       if (pool->nscallback && !strncmp("product:", pool_id2str(pool, s->name), 8))
521         {
522           Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
523           if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
524             {
525               addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
526               addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy)); 
527               if (m && !MAPTST(m, buddy))
528                 queue_push(&workq, buddy);
529             }
530         }
531
532       /*-----------------------------------------
533        * check requires of s
534        */
535
536       if (s->requires)
537         {
538           reqp = s->repo->idarraydata + s->requires;
539           while ((req = *reqp++) != 0)            /* go through all requires */
540             {
541               if (req == SOLVABLE_PREREQMARKER)   /* skip the marker */
542                 continue;
543
544               /* find list of solvables providing 'req' */
545               dp = pool_whatprovides_ptr(pool, req);
546
547               if (*dp == SYSTEMSOLVABLE)          /* always installed */
548                 continue;
549
550               if (dontfix)
551                 {
552                   /* the strategy here is to not insist on dependencies
553                    * that are already broken. so if we find one provider
554                    * that was already installed, we know that the
555                    * dependency was not broken before so we enforce it */
556                  
557                   /* check if any of the providers for 'req' is installed */
558                   for (i = 0; (p = dp[i]) != 0; i++)
559                     {
560                       if (pool->solvables[p].repo == installed)
561                         break;          /* provider was installed */
562                     }
563                   /* didn't find an installed provider: previously broken dependency */
564                   if (!p)
565                     {
566                       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
567                       continue;
568                     }
569                 }
570
571               if (!*dp)
572                 {
573                   /* nothing provides req! */
574                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables), pool_dep2str(pool, req));
575                   addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
576                   continue;
577                 }
578
579               IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
580                 {
581                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION,"  %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
582                   for (i = 0; dp[i]; i++)
583                     POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "   provided by %s\n", pool_solvid2str(pool, dp[i]));
584                 }
585
586               /* add 'requires' dependency */
587               /* rule: (-requestor|provider1|provider2|...|providerN) */
588               addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
589
590               /* descend the dependency tree
591                  push all non-visited providers on the work queue */
592               if (m)
593                 {
594                   for (; *dp; dp++)
595                     {
596                       if (!MAPTST(m, *dp))
597                         queue_push(&workq, *dp);
598                     }
599                 }
600
601             } /* while, requirements of n */
602
603         } /* if, requirements */
604
605       /* that's all we check for src packages */
606       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
607         continue;
608
609       /*-----------------------------------------
610        * check conflicts of s
611        */
612
613       if (s->conflicts)
614         {
615           int ispatch = 0;
616
617           /* we treat conflicts in patches a bit differen:
618            * - nevr matching
619            * - multiversion handling
620            * XXX: we should really handle this different, looking
621            * at the name is a bad hack
622            */
623           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
624             ispatch = 1;
625           conp = s->repo->idarraydata + s->conflicts;
626           /* foreach conflicts of 's' */
627           while ((con = *conp++) != 0)
628             {
629               /* foreach providers of a conflict of 's' */
630               FOR_PROVIDES(p, pp, con)
631                 {
632                   if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
633                     continue;
634                   /* dontfix: dont care about conflicts with already installed packs */
635                   if (dontfix && pool->solvables[p].repo == installed)
636                     continue;
637                   /* p == n: self conflict */
638                   if (p == n && !pool->allowselfconflicts)
639                     {
640                       if (ISRELDEP(con))
641                         {
642                           Reldep *rd = GETRELDEP(pool, con);
643                           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
644                             continue;
645                         }
646                       p = 0;    /* make it a negative assertion, aka 'uninstallable' */
647                     }
648                   if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
649                     {
650                       /* our patch conflicts with a noobsoletes (aka multiversion) package */
651                       p = -makemultiversionconflict(solv, p, con);
652                     }
653                  /* rule: -n|-p: either solvable _or_ provider of conflict */
654                   addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
655                 }
656             }
657         }
658
659       /*-----------------------------------------
660        * check obsoletes and implicit obsoletes of a package
661        * if ignoreinstalledsobsoletes is not set, we're also checking
662        * obsoletes of installed packages (like newer rpm versions)
663        */
664       if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
665         {
666           int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
667           int isinstalled = (installed && s->repo == installed);
668           if (s->obsoletes && !noobs)
669             {
670               obsp = s->repo->idarraydata + s->obsoletes;
671               /* foreach obsoletes */
672               while ((obs = *obsp++) != 0)
673                 {
674                   /* foreach provider of an obsoletes of 's' */ 
675                   FOR_PROVIDES(p, pp, obs)
676                     {
677                       Solvable *ps = pool->solvables + p;
678                       if (p == n)
679                         continue;
680                       if (isinstalled && dontfix && ps->repo == installed)
681                         continue;       /* don't repair installed/installed problems */
682                       if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
683                           && !pool_match_nevr(pool, ps, obs))
684                         continue;
685                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
686                         continue;
687                       if (!isinstalled)
688                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
689                       else
690                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
691                     }
692                 }
693             }
694           /* check implicit obsoletes
695            * for installed packages we only need to check installed/installed problems (and
696            * only when dontfix is not set), as the others are picked up when looking at the
697            * uninstalled package.
698            */
699           if (!isinstalled || !dontfix)
700             {
701               FOR_PROVIDES(p, pp, s->name)
702                 {
703                   Solvable *ps = pool->solvables + p;
704                   if (p == n)
705                     continue;
706                   if (isinstalled && ps->repo != installed)
707                     continue;
708                   /* we still obsolete packages with same nevra, like rpm does */
709                   /* (actually, rpm mixes those packages. yuck...) */
710                   if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
711                     continue;
712                   if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
713                     continue;
714                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
715                     continue;
716                   if (s->name == ps->name)
717                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
718                   else
719                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
720                 }
721             }
722         }
723
724       /*-----------------------------------------
725        * add recommends to the work queue
726        */
727       if (s->recommends && m)
728         {
729           recp = s->repo->idarraydata + s->recommends;
730           while ((rec = *recp++) != 0)
731             {
732               FOR_PROVIDES(p, pp, rec)
733                 if (!MAPTST(m, p))
734                   queue_push(&workq, p);
735             }
736         }
737       if (s->suggests && m)
738         {
739           sugp = s->repo->idarraydata + s->suggests;
740           while ((sug = *sugp++) != 0)
741             {
742               FOR_PROVIDES(p, pp, sug)
743                 if (!MAPTST(m, p))
744                   queue_push(&workq, p);
745             }
746         }
747     }
748   queue_free(&workq);
749 }
750
751
752 /*-------------------------------------------------------------------
753  * 
754  * Add rules for packages possibly selected in by weak dependencies
755  *
756  * m: already added solvables
757  */
758
759 void
760 solver_addrpmrulesforweak(Solver *solv, Map *m)
761 {
762   Pool *pool = solv->pool;
763   Solvable *s;
764   Id sup, *supp;
765   int i, n;
766
767   /* foreach solvable in pool */
768   for (i = n = 1; n < pool->nsolvables; i++, n++)
769     {
770       if (i == pool->nsolvables)                /* wrap i */
771         i = 1;
772       if (MAPTST(m, i))                         /* already added that one */
773         continue;
774
775       s = pool->solvables + i;
776       if (!pool_installable(pool, s))           /* only look at installable ones */
777         continue;
778
779       sup = 0;
780       if (s->supplements)
781         {
782           /* find possible supplements */
783           supp = s->repo->idarraydata + s->supplements;
784           while ((sup = *supp++) != 0)
785             if (dep_possible(solv, sup, m))
786               break;
787         }
788
789       /* if nothing found, check for enhances */
790       if (!sup && s->enhances)
791         {
792           supp = s->repo->idarraydata + s->enhances;
793           while ((sup = *supp++) != 0)
794             if (dep_possible(solv, sup, m))
795               break;
796         }
797       /* if nothing found, goto next solvables */
798       if (!sup)
799         continue;
800       solver_addrpmrulesforsolvable(solv, s, m);
801       n = 0;                    /* check all solvables again because we added solvables to m */
802     }
803 }
804
805
806 /*-------------------------------------------------------------------
807  * 
808  * add package rules for possible updates
809  * 
810  * s: solvable
811  * m: map of already visited solvables
812  * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
813  */
814
815 void
816 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
817 {
818   Pool *pool = solv->pool;
819   int i;
820     /* queue and buffer for it */
821   Queue qs;
822   Id qsbuf[64];
823
824   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
825     /* find update candidates for 's' */
826   policy_findupdatepackages(solv, s, &qs, allow_all);
827     /* add rule for 's' if not already done */
828   if (!MAPTST(m, s - pool->solvables))
829     solver_addrpmrulesforsolvable(solv, s, m);
830     /* foreach update candidate, add rule if not already done */
831   for (i = 0; i < qs.count; i++)
832     if (!MAPTST(m, qs.elements[i]))
833       solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
834   queue_free(&qs);
835 }
836
837
838 /***********************************************************************
839  ***
840  ***  Update/Feature rule part
841  ***
842  ***  Those rules make sure an installed package isn't silently deleted
843  ***
844  ***/
845
846 static Id
847 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
848 {
849   Pool *pool = solv->pool;
850   int i;
851
852   policy_findupdatepackages(solv, s, qs, allow_all);
853   if (!qs->count)
854     {
855       if (allow_all)
856         return 0;       /* orphaned, don't create feature rule */
857       /* check if this is an orphaned package */
858       policy_findupdatepackages(solv, s, qs, 1);
859       if (!qs->count)
860         return 0;       /* orphaned, don't create update rule */
861       qs->count = 0;
862       return -SYSTEMSOLVABLE;   /* supported but not installable */
863     }
864   if (allow_all)
865     return s - pool->solvables;
866   /* check if it is ok to keep the installed package */
867   for (i = 0; i < qs->count; i++)
868     {
869       Solvable *ns = pool->solvables + qs->elements[i];
870       if (s->evr == ns->evr && solvable_identical(s, ns))
871         return s - pool->solvables;
872     }
873   /* nope, it must be some other package */
874   return -SYSTEMSOLVABLE;
875 }
876
877 /* add packages from the dup repositories to the update candidates
878  * this isn't needed for the global dup mode as all packages are
879  * from dup repos in that case */
880 static void
881 addduppackages(Solver *solv, Solvable *s, Queue *qs)
882 {
883   Queue dupqs;
884   Id p, dupqsbuf[64];
885   int i;
886   int oldnoupdateprovide = solv->noupdateprovide;
887
888   queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
889   solv->noupdateprovide = 1;
890   policy_findupdatepackages(solv, s, &dupqs, 2);
891   solv->noupdateprovide = oldnoupdateprovide;
892   for (i = 0; i < dupqs.count; i++)
893     {
894       p = dupqs.elements[i];
895       if (MAPTST(&solv->dupmap, p))
896         queue_pushunique(qs, p);
897     }
898   queue_free(&dupqs);
899 }
900
901 /*-------------------------------------------------------------------
902  * 
903  * add rule for update
904  *   (A|A1|A2|A3...)  An = update candidates for A
905  *
906  * s = (installed) solvable
907  */
908
909 void
910 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
911 {
912   /* installed packages get a special upgrade allowed rule */
913   Pool *pool = solv->pool;
914   Id p, d;
915   Queue qs;
916   Id qsbuf[64];
917
918   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
919   p = s - pool->solvables;
920   /* find update candidates for 's' */
921   if (solv->dupmap_all)
922     p = finddistupgradepackages(solv, s, &qs, allow_all);
923   else
924     policy_findupdatepackages(solv, s, &qs, allow_all);
925   if (!allow_all && !solv->dupmap_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
926     addduppackages(solv, s, &qs);
927
928   if (!allow_all && qs.count && solv->noobsoletes.size)
929     {
930       int i, j;
931
932       d = pool_queuetowhatprovides(pool, &qs);
933       /* filter out all noobsoletes packages as they don't update */
934       for (i = j = 0; i < qs.count; i++)
935         {
936           if (MAPTST(&solv->noobsoletes, qs.elements[i]))
937             {
938               /* it's ok if they have same nevra */
939               Solvable *ps = pool->solvables + qs.elements[i];
940               if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
941                 continue;
942             }
943           qs.elements[j++] = qs.elements[i];
944         }
945       if (j < qs.count)
946         {
947           if (d && solv->installed && s->repo == solv->installed &&
948               (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
949             {
950               if (!solv->multiversionupdaters)
951                 solv->multiversionupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
952               solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
953             }
954           if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
955             {
956               queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
957               j = qs.count;
958             }
959           qs.count = j;
960         }
961     }
962   if (qs.count && p == -SYSTEMSOLVABLE)
963     p = queue_shift(&qs);
964   d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
965   queue_free(&qs);
966   solver_addrule(solv, p, d);   /* allow update of s */
967 }
968
969 static inline void 
970 disableupdaterule(Solver *solv, Id p)
971 {
972   Rule *r;
973
974   MAPSET(&solv->noupdate, p - solv->installed->start);
975   r = solv->rules + solv->updaterules + (p - solv->installed->start);
976   if (r->p && r->d >= 0)
977     solver_disablerule(solv, r);
978   r = solv->rules + solv->featurerules + (p - solv->installed->start);
979   if (r->p && r->d >= 0)
980     solver_disablerule(solv, r);
981 }
982
983 static inline void 
984 reenableupdaterule(Solver *solv, Id p)
985 {
986   Pool *pool = solv->pool;
987   Rule *r;
988
989   MAPCLR(&solv->noupdate, p - solv->installed->start);
990   r = solv->rules + solv->updaterules + (p - solv->installed->start);
991   if (r->p)
992     {    
993       if (r->d >= 0)
994         return;
995       solver_enablerule(solv, r);
996       IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
997         {
998           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
999           solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1000         }
1001       return;
1002     }
1003   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1004   if (r->p && r->d < 0)
1005     {
1006       solver_enablerule(solv, r);
1007       IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1008         {
1009           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1010           solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1011         }
1012     }
1013 }
1014
1015
1016 /***********************************************************************
1017  ***
1018  ***  Infarch rule part
1019  ***
1020  ***  Infarch rules make sure the solver uses the best architecture of
1021  ***  a package if multiple archetectures are available
1022  ***
1023  ***/
1024
1025 void
1026 solver_addinfarchrules(Solver *solv, Map *addedmap)
1027 {
1028   Pool *pool = solv->pool;
1029   int first, i, j;
1030   Id p, pp, a, aa, bestarch;
1031   Solvable *s, *ps, *bests;
1032   Queue badq, allowedarchs;
1033
1034   queue_init(&badq);
1035   queue_init(&allowedarchs);
1036   solv->infarchrules = solv->nrules;
1037   for (i = 1; i < pool->nsolvables; i++)
1038     {
1039       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1040         continue;
1041       s = pool->solvables + i;
1042       first = i;
1043       bestarch = 0;
1044       bests = 0;
1045       queue_empty(&allowedarchs);
1046       FOR_PROVIDES(p, pp, s->name)
1047         {
1048           ps = pool->solvables + p;
1049           if (ps->name != s->name || !MAPTST(addedmap, p))
1050             continue;
1051           if (p == i)
1052             first = 0;
1053           if (first)
1054             break;
1055           a = ps->arch;
1056           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1057           if (a != 1 && pool->installed && ps->repo == pool->installed)
1058             {
1059               if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1060                 queue_pushunique(&allowedarchs, ps->arch);      /* also ok to keep this architecture */
1061               continue;         /* ignore installed solvables when calculating the best arch */
1062             }
1063           if (a && a != 1 && (!bestarch || a < bestarch))
1064             {
1065               bestarch = a;
1066               bests = ps;
1067             }
1068         }
1069       if (first)
1070         continue;
1071       /* speed up common case where installed package already has best arch */
1072       if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1073         allowedarchs.count--;   /* installed arch is best */
1074       queue_empty(&badq);
1075       FOR_PROVIDES(p, pp, s->name)
1076         {
1077           ps = pool->solvables + p;
1078           if (ps->name != s->name || !MAPTST(addedmap, p))
1079             continue;
1080           a = ps->arch;
1081           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1082           if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1083             {
1084               if (pool->installed && ps->repo == pool->installed)
1085                 continue;       /* always ok to keep an installed package */
1086               for (j = 0; j < allowedarchs.count; j++)
1087                 {
1088                   aa = allowedarchs.elements[j];
1089                   if (ps->arch == aa)
1090                     break;
1091                   aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1092                   if (aa && ((a ^ aa) & 0xffff0000) == 0)
1093                     break;      /* compatible */
1094                 }
1095               if (j == allowedarchs.count)
1096                 queue_push(&badq, p);
1097             }
1098         }
1099       if (!badq.count)
1100         continue;
1101       /* block all solvables in the badq! */
1102       for (j = 0; j < badq.count; j++)
1103         {
1104           p = badq.elements[j];
1105           solver_addrule(solv, -p, 0);
1106         }
1107     }
1108   queue_free(&badq);
1109   queue_free(&allowedarchs);
1110   solv->infarchrules_end = solv->nrules;
1111 }
1112
1113 static inline void
1114 disableinfarchrule(Solver *solv, Id name)
1115 {
1116   Pool *pool = solv->pool;
1117   Rule *r;
1118   int i;
1119   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1120     {
1121       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1122         solver_disablerule(solv, r);
1123     }
1124 }
1125
1126 static inline void
1127 reenableinfarchrule(Solver *solv, Id name)
1128 {
1129   Pool *pool = solv->pool;
1130   Rule *r;
1131   int i;
1132   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1133     {
1134       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1135         {
1136           solver_enablerule(solv, r);
1137           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1138             {
1139               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1140               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1141             }
1142         }
1143     }
1144 }
1145
1146
1147 /***********************************************************************
1148  ***
1149  ***  Dup rule part
1150  ***
1151  ***  Dup rules make sure a package is selected from the specified dup
1152  ***  repositories if an update candidate is included in one of them.
1153  ***
1154  ***/
1155
1156 void
1157 solver_createdupmaps(Solver *solv)
1158 {
1159   Queue *job = &solv->job;
1160   Pool *pool = solv->pool;
1161   Repo *repo;
1162   Id how, what, p, pi, pp, obs, *obsp;
1163   Solvable *s, *ps;
1164   int i;
1165
1166   map_init(&solv->dupmap, pool->nsolvables);
1167   map_init(&solv->dupinvolvedmap, pool->nsolvables);
1168   for (i = 0; i < job->count; i += 2)
1169     {
1170       how = job->elements[i];
1171       what = job->elements[i + 1];
1172       switch (how & SOLVER_JOBMASK)
1173         {
1174         case SOLVER_DISTUPGRADE:
1175           if ((how & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_REPO)
1176             break;
1177           if (what <= 0 || what > pool->nrepos)
1178             break;
1179           repo = pool_id2repo(pool, what);
1180           FOR_REPO_SOLVABLES(repo, p, s)
1181             {
1182               MAPSET(&solv->dupmap, p);
1183               FOR_PROVIDES(pi, pp, s->name)
1184                 {
1185                   ps = pool->solvables + pi;
1186                   if (ps->name != s->name)
1187                     continue;
1188                   MAPSET(&solv->dupinvolvedmap, pi);
1189                 }
1190               if (s->obsoletes)
1191                 {
1192                   /* FIXME: check obsoletes/provides combination */
1193                   obsp = s->repo->idarraydata + s->obsoletes;
1194                   while ((obs = *obsp++) != 0)
1195                     {
1196                       FOR_PROVIDES(pi, pp, obs)
1197                         {
1198                           Solvable *pis = pool->solvables + pi;
1199                           if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pis, obs))
1200                             continue;
1201                           if (pool->obsoleteusescolors && !pool_colormatch(pool, s, pis))
1202                             continue;
1203                           MAPSET(&solv->dupinvolvedmap, pi);
1204                         }
1205                     }
1206                 }
1207             }
1208           break;
1209         default:
1210           break;
1211         }
1212     }
1213   MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1214 }
1215
1216 void
1217 solver_freedupmaps(Solver *solv)
1218 {
1219   map_free(&solv->dupmap);
1220   /* we no longer free solv->dupinvolvedmap as we need it in
1221    * policy's priority pruning code. sigh. */
1222 }
1223
1224 void
1225 solver_addduprules(Solver *solv, Map *addedmap)
1226 {
1227   Pool *pool = solv->pool;
1228   Id p, pp;
1229   Solvable *s, *ps;
1230   int first, i;
1231
1232   solv->duprules = solv->nrules;
1233   for (i = 1; i < pool->nsolvables; i++)
1234     {
1235       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1236         continue;
1237       s = pool->solvables + i;
1238       first = i;
1239       FOR_PROVIDES(p, pp, s->name)
1240         {
1241           ps = pool->solvables + p;
1242           if (ps->name != s->name || !MAPTST(addedmap, p))
1243             continue;
1244           if (p == i)
1245             first = 0;
1246           if (first)
1247             break;
1248           if (!MAPTST(&solv->dupinvolvedmap, p))
1249             continue;
1250           if (solv->installed && ps->repo == solv->installed)
1251             {
1252               if (!solv->updatemap.size)
1253                 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1254               MAPSET(&solv->updatemap, p - solv->installed->start);
1255               if (!MAPTST(&solv->dupmap, p))
1256                 {
1257                   Id ip, ipp;
1258                   /* is installed identical to a good one? */
1259                   FOR_PROVIDES(ip, ipp, ps->name)
1260                     {
1261                       Solvable *is = pool->solvables + ip;
1262                       if (!MAPTST(&solv->dupmap, ip))
1263                         continue;
1264                       if (is->evr == ps->evr && solvable_identical(ps, is))
1265                         break;
1266                     }
1267                   if (!ip)
1268                     solver_addrule(solv, -p, 0);        /* no match, sorry */
1269                 }
1270             }
1271           else if (!MAPTST(&solv->dupmap, p))
1272             solver_addrule(solv, -p, 0);
1273         }
1274     }
1275   solv->duprules_end = solv->nrules;
1276 }
1277
1278
1279 static inline void
1280 disableduprule(Solver *solv, Id name)
1281 {
1282   Pool *pool = solv->pool;
1283   Rule *r;
1284   int i;
1285   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++) 
1286     {    
1287       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1288         solver_disablerule(solv, r);
1289     }    
1290 }
1291
1292 static inline void 
1293 reenableduprule(Solver *solv, Id name)
1294 {
1295   Pool *pool = solv->pool;
1296   Rule *r;
1297   int i;
1298   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++) 
1299     {    
1300       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1301         {
1302           solver_enablerule(solv, r);
1303           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1304             {
1305               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1306               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1307             }
1308         }
1309     }
1310 }
1311
1312
1313 /***********************************************************************
1314  ***
1315  ***  Policy rule disabling/reenabling
1316  ***
1317  ***  Disable all policy rules that conflict with our jobs. If a job
1318  ***  gets disabled later on, reenable the involved policy rules again.
1319  ***
1320  ***/
1321
1322 #define DISABLE_UPDATE  1
1323 #define DISABLE_INFARCH 2
1324 #define DISABLE_DUP     3
1325
1326 static void
1327 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1328 {
1329   Pool *pool = solv->pool;
1330   Id select, p, pp;
1331   Repo *installed;
1332   Solvable *s;
1333   int i, j, set, qstart, pass;
1334   Map omap;
1335
1336   installed = solv->installed;
1337   select = how & SOLVER_SELECTMASK;
1338   switch (how & SOLVER_JOBMASK)
1339     {
1340     case SOLVER_INSTALL:
1341       set = how & SOLVER_SETMASK;
1342       if (!(set & SOLVER_NOAUTOSET))
1343         {
1344           /* automatically add set bits by analysing the job */
1345           if (select == SOLVER_SOLVABLE)
1346             set |= SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
1347           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
1348             {
1349               Reldep *rd = GETRELDEP(pool, what);
1350               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
1351                 {
1352 #if !defined(DEBIAN_SEMANTICS)
1353                   const char *evr = pool_id2str(pool, rd->evr);
1354                   if (strchr(evr, '-'))
1355                     set |= SOLVER_SETEVR;
1356                   else
1357                     set |= SOLVER_SETEV;
1358 #else
1359                   set |= SOLVER_SETEVR;
1360 #endif
1361                 }
1362               if (rd->flags <= 7 && ISRELDEP(rd->name))
1363                 rd = GETRELDEP(pool, rd->name);
1364               if (rd->flags == REL_ARCH)
1365                 set |= SOLVER_SETARCH;
1366             }
1367         }
1368       else
1369         set &= ~SOLVER_NOAUTOSET;
1370       if (!set)
1371         return;
1372       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
1373         {
1374           if (select == SOLVER_SOLVABLE)
1375             queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
1376           else
1377             {
1378               int qcnt = q->count;
1379               FOR_JOB_SELECT(p, pp, select, what)
1380                 {
1381                   s = pool->solvables + p;
1382                   /* unify names */
1383                   for (i = qcnt; i < q->count; i += 2)
1384                     if (q->elements[i + 1] == s->name)
1385                       break;
1386                   if (i < q->count)
1387                     continue;
1388                   queue_push2(q, DISABLE_INFARCH, s->name);
1389                 }
1390             }
1391         }
1392       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
1393         {
1394           if (select == SOLVER_SOLVABLE)
1395             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
1396           else
1397             {
1398               int qcnt = q->count;
1399               FOR_JOB_SELECT(p, pp, select, what)
1400                 {
1401                   s = pool->solvables + p;
1402                   /* unify names */
1403                   for (i = qcnt; i < q->count; i += 2)
1404                     if (q->elements[i + 1] == s->name)
1405                       break;
1406                   if (i < q->count)
1407                     continue;
1408                   queue_push2(q, DISABLE_DUP, s->name);
1409                 }
1410             }
1411         }
1412       if (!installed)
1413         return;
1414       /* now the hard part: disable some update rules */
1415
1416       /* first check if we have noobs or installed packages in the job */
1417       FOR_JOB_SELECT(p, pp, select, what)
1418         {
1419           if (pool->solvables[p].repo == installed)
1420             {
1421               if (select == SOLVER_SOLVABLE)
1422                 queue_push2(q, DISABLE_UPDATE, what);
1423               return;
1424             }
1425           if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1426             return;
1427         }
1428
1429       /* all job packages obsolete */
1430       qstart = q->count;
1431       pass = 0;
1432       memset(&omap, 0, sizeof(omap));
1433       FOR_JOB_SELECT(p, pp, select, what)
1434         {
1435           Id p2, pp2;
1436
1437           if (pass == 1)
1438             map_grow(&omap, installed->end - installed->start);
1439           s = pool->solvables + p;
1440           if (s->obsoletes)
1441             {
1442               Id obs, *obsp;
1443               obsp = s->repo->idarraydata + s->obsoletes;
1444               while ((obs = *obsp++) != 0)
1445                 FOR_PROVIDES(p2, pp2, obs)
1446                   {
1447                     Solvable *ps = pool->solvables + p2;
1448                     if (ps->repo != installed)
1449                       continue;
1450                     if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1451                       continue;
1452                     if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1453                       continue;
1454                     if (pass)
1455                       MAPSET(&omap, p2 - installed->start);
1456                     else
1457                       queue_push2(q, DISABLE_UPDATE, p2);
1458                   }
1459             }
1460           FOR_PROVIDES(p2, pp2, s->name)
1461             {
1462               Solvable *ps = pool->solvables + p2;
1463               if (ps->repo != installed)
1464                 continue;
1465               if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1466                 continue;
1467               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1468                 continue;
1469               if (pass)
1470                 MAPSET(&omap, p2 - installed->start);
1471               else
1472                 queue_push2(q, DISABLE_UPDATE, p2);
1473             }
1474           if (pass)
1475             {
1476               for (i = j = qstart; i < q->count; i += 2)
1477                 {
1478                   if (MAPTST(&omap, q->elements[i + 1] - installed->start))
1479                     {
1480                       MAPCLR(&omap, q->elements[i + 1] - installed->start);
1481                       q->elements[j + 1] = q->elements[i + 1];
1482                       j += 2;
1483                     }
1484                 }
1485               queue_truncate(q, j);
1486             }
1487           if (q->count == qstart)
1488             break;
1489           pass++;
1490         }
1491       if (omap.size)
1492         map_free(&omap);
1493
1494       if (qstart == q->count)
1495         return;         /* nothing to prune */
1496       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
1497         return;         /* all is set */
1498
1499       /* now that we know which installed packages are obsoleted check each of them */
1500       for (i = j = qstart; i < q->count; i += 2)
1501         {
1502           Solvable *is = pool->solvables + q->elements[i + 1];
1503           FOR_JOB_SELECT(p, pp, select, what)
1504             {
1505               int illegal = 0;
1506               s = pool->solvables + p;
1507               if ((set & SOLVER_SETEVR) != 0)
1508                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
1509               if ((set & SOLVER_SETARCH) != 0)
1510                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
1511               if ((set & SOLVER_SETVENDOR) != 0)
1512                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
1513               illegal = policy_is_illegal(solv, is, s, illegal);
1514               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
1515                 {
1516                   /* it's ok if the EV is different */
1517                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
1518                     illegal = 0;
1519                 }
1520               if (illegal)
1521                 break;
1522             }
1523           if (!p)
1524             {   
1525               /* no package conflicts with the update rule */
1526               /* thus keep the DISABLE_UPDATE */
1527               q->elements[j + 1] = q->elements[i + 1];
1528               j += 2;
1529             }
1530         }
1531       queue_truncate(q, j);
1532       return;
1533
1534     case SOLVER_ERASE:
1535       if (!installed)
1536         break;
1537       FOR_JOB_SELECT(p, pp, select, what)
1538         if (pool->solvables[p].repo == installed)
1539           queue_push2(q, DISABLE_UPDATE, p);
1540       return;
1541     default:
1542       return;
1543     }
1544 }
1545
1546 /* disable all policy rules that are in conflict with our job list */
1547 void
1548 solver_disablepolicyrules(Solver *solv)
1549 {
1550   Queue *job = &solv->job;
1551   int i, j;
1552   Queue allq;
1553   Rule *r;
1554   Id lastjob = -1;
1555   Id allqbuf[128];
1556
1557   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1558
1559   for (i = solv->jobrules; i < solv->jobrules_end; i++)
1560     {
1561       r = solv->rules + i;
1562       if (r->d < 0)     /* disabled? */
1563         continue;
1564       j = solv->ruletojob.elements[i - solv->jobrules];
1565       if (j == lastjob)
1566         continue;
1567       lastjob = j;
1568       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1569     }
1570   if (solv->cleandepsmap.size)
1571     {
1572       solver_createcleandepsmap(solv);
1573       for (i = solv->installed->start; i < solv->installed->end; i++)
1574         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1575           queue_push2(&allq, DISABLE_UPDATE, i);
1576     }
1577   MAPZERO(&solv->noupdate);
1578   for (i = 0; i < allq.count; i += 2)
1579     {
1580       Id type = allq.elements[i], arg = allq.elements[i + 1];
1581       switch(type)
1582         {
1583         case DISABLE_UPDATE:
1584           disableupdaterule(solv, arg);
1585           break;
1586         case DISABLE_INFARCH:
1587           disableinfarchrule(solv, arg);
1588           break;
1589         case DISABLE_DUP:
1590           disableduprule(solv, arg);
1591           break;
1592         default:
1593           break;
1594         }
1595     }
1596   queue_free(&allq);
1597 }
1598
1599 /* we just disabled job #jobidx, now reenable all policy rules that were
1600  * disabled because of this job */
1601 void
1602 solver_reenablepolicyrules(Solver *solv, int jobidx)
1603 {
1604   Queue *job = &solv->job;
1605   int i, j;
1606   Queue q, allq;
1607   Rule *r;
1608   Id lastjob = -1;
1609   Id qbuf[32], allqbuf[128];
1610
1611   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1612   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1613   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
1614   if (!q.count)
1615     return;
1616   for (i = solv->jobrules; i < solv->jobrules_end; i++)
1617     {
1618       r = solv->rules + i;
1619       if (r->d < 0)     /* disabled? */
1620         continue;
1621       j = solv->ruletojob.elements[i - solv->jobrules];
1622       if (j == lastjob)
1623         continue;
1624       lastjob = j;
1625       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1626     }
1627   if (solv->cleandepsmap.size)
1628     {
1629       solver_createcleandepsmap(solv);
1630       for (i = solv->installed->start; i < solv->installed->end; i++)
1631         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1632           queue_push2(&allq, DISABLE_UPDATE, i);
1633     }
1634   for (j = 0; j < q.count; j += 2)
1635     {
1636       Id type = q.elements[j], arg = q.elements[j + 1];
1637       for (i = 0; i < allq.count; i += 2)
1638         if (allq.elements[i] == type && allq.elements[i + 1] == arg)
1639           break;
1640       if (i < allq.count)
1641         continue;       /* still disabled */
1642       switch(type)
1643         {
1644         case DISABLE_UPDATE:
1645           reenableupdaterule(solv, arg);
1646           break;
1647         case DISABLE_INFARCH:
1648           reenableinfarchrule(solv, arg);
1649           break;
1650         case DISABLE_DUP:
1651           reenableduprule(solv, arg);
1652           break;
1653         }
1654     }
1655   queue_free(&allq);
1656   queue_free(&q);
1657 }
1658
1659
1660 /***********************************************************************
1661  ***
1662  ***  Rule info part, tell the user what the rule is about.
1663  ***
1664  ***/
1665
1666 static void
1667 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1668 {
1669   Pool *pool = solv->pool;
1670   Rule *r;
1671   Id w2, op, od, ow2;
1672
1673   /* check if this creates the rule we're searching for */
1674   r = solv->rules + solv->ruleinfoq->elements[0];
1675   op = r->p;
1676   od = r->d < 0 ? -r->d - 1 : r->d;
1677   ow2 = 0;
1678
1679   /* normalize */
1680   w2 = d > 0 ? 0 : d;
1681   if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1682     {
1683       w2 = pool->whatprovidesdata[d];
1684       d = 0;
1685
1686     }
1687   if (p > 0 && d < 0)           /* this hack is used for buddy deps */
1688     {
1689       w2 = p;
1690       p = d;
1691     }
1692
1693   if (d > 0)
1694     {
1695       if (p != op && !od)
1696         return;
1697       if (d != od)
1698         {
1699           Id *dp = pool->whatprovidesdata + d;
1700           Id *odp = pool->whatprovidesdata + od;
1701           while (*dp)
1702             if (*dp++ != *odp++)
1703               return;
1704           if (*odp)
1705             return;
1706         }
1707       w2 = 0;
1708       /* handle multiversion conflict rules */
1709       if (p < 0 && pool->whatprovidesdata[d] < 0)
1710         {
1711           w2 = pool->whatprovidesdata[d];
1712           /* XXX: free memory */
1713         }
1714     }
1715   else
1716     {
1717       if (od)
1718         return;
1719       ow2 = r->w2;
1720       if (p > w2)
1721         {
1722           if (w2 != op || p != ow2)
1723             return;
1724         }
1725       else
1726         {
1727           if (p != op || w2 != ow2)
1728             return;
1729         }
1730     }
1731   /* yep, rule matches. record info */
1732   queue_push(solv->ruleinfoq, type);
1733   if (type == SOLVER_RULE_RPM_SAME_NAME)
1734     {
1735       /* we normalize same name order */
1736       queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
1737       queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
1738     }
1739   else
1740     {
1741       queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
1742       queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
1743     }
1744   queue_push(solv->ruleinfoq, dep);
1745 }
1746
1747 static int
1748 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
1749 {
1750   const Id *a = ap, *b = bp;
1751   int r;
1752
1753   r = a[0] - b[0];
1754   if (r)
1755     return r;
1756   r = a[1] - b[1];
1757   if (r)
1758     return r;
1759   r = a[2] - b[2];
1760   if (r)
1761     return r;
1762   r = a[3] - b[3];
1763   if (r)
1764     return r;
1765   return 0;
1766 }
1767
1768 int
1769 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
1770 {
1771   Pool *pool = solv->pool;
1772   Rule *r = solv->rules + rid;
1773   int i, j;
1774
1775   queue_empty(rq);
1776   if (rid <= 0 || rid >= solv->rpmrules_end)
1777     {
1778       Id type, from, to, dep;
1779       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
1780       queue_push(rq, type);
1781       queue_push(rq, from);
1782       queue_push(rq, to);
1783       queue_push(rq, dep);
1784       return 1;
1785     }
1786   if (r->p >= 0)
1787     return 0;
1788   queue_push(rq, rid);
1789   solv->ruleinfoq = rq;
1790   solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1791   /* also try reverse direction for conflicts */
1792   if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1793     solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1794   solv->ruleinfoq = 0;
1795   queue_shift(rq);
1796   /* now sort & unify em */
1797   if (!rq->count)
1798     return 0;
1799   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
1800   /* throw out identical entries */
1801   for (i = j = 0; i < rq->count; i += 4)
1802     {
1803       if (j)
1804         {
1805           if (rq->elements[i] == rq->elements[j - 4] && 
1806               rq->elements[i + 1] == rq->elements[j - 3] &&
1807               rq->elements[i + 2] == rq->elements[j - 2] &&
1808               rq->elements[i + 3] == rq->elements[j - 1])
1809             continue;
1810         }
1811       rq->elements[j++] = rq->elements[i];
1812       rq->elements[j++] = rq->elements[i + 1];
1813       rq->elements[j++] = rq->elements[i + 2];
1814       rq->elements[j++] = rq->elements[i + 3];
1815     }
1816   rq->count = j;
1817   return j / 4;
1818 }
1819
1820 SolverRuleinfo
1821 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
1822 {
1823   Pool *pool = solv->pool;
1824   Rule *r = solv->rules + rid;
1825   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
1826
1827   if (fromp)
1828     *fromp = 0;
1829   if (top)
1830     *top = 0;
1831   if (depp)
1832     *depp = 0;
1833   if (rid > 0 && rid < solv->rpmrules_end)
1834     {
1835       Queue rq;
1836       int i;
1837
1838       if (r->p >= 0)
1839         return SOLVER_RULE_RPM;
1840       if (fromp)
1841         *fromp = -r->p;
1842       queue_init(&rq);
1843       queue_push(&rq, rid);
1844       solv->ruleinfoq = &rq;
1845       solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
1846       /* also try reverse direction for conflicts */
1847       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
1848         solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
1849       solv->ruleinfoq = 0;
1850       type = SOLVER_RULE_RPM;
1851       for (i = 1; i < rq.count; i += 4)
1852         {
1853           Id qt, qo, qp, qd;
1854           qt = rq.elements[i];
1855           qp = rq.elements[i + 1];
1856           qo = rq.elements[i + 2];
1857           qd = rq.elements[i + 3];
1858           if (type == SOLVER_RULE_RPM || type > qt)
1859             {
1860               type = qt;
1861               if (fromp)
1862                 *fromp = qp;
1863               if (top)
1864                 *top = qo;
1865               if (depp)
1866                 *depp = qd;
1867             }
1868         }
1869       queue_free(&rq);
1870       return type;
1871     }
1872   if (rid >= solv->jobrules && rid < solv->jobrules_end)
1873     {
1874       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
1875       if (fromp)
1876         *fromp = jidx;
1877       if (top)
1878         *top = solv->job.elements[jidx];
1879       if (depp)
1880         *depp = solv->job.elements[jidx + 1];
1881       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (solv->job.elements[jidx] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
1882         return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
1883       return SOLVER_RULE_JOB;
1884     }
1885   if (rid >= solv->updaterules && rid < solv->updaterules_end)
1886     {
1887       if (fromp)
1888         *fromp = solv->installed->start + (rid - solv->updaterules);
1889       return SOLVER_RULE_UPDATE;
1890     }
1891   if (rid >= solv->featurerules && rid < solv->featurerules_end)
1892     {
1893       if (fromp)
1894         *fromp = solv->installed->start + (rid - solv->featurerules);
1895       return SOLVER_RULE_FEATURE;
1896     }
1897   if (rid >= solv->duprules && rid < solv->duprules_end)
1898     {
1899       if (fromp)
1900         *fromp = -r->p;
1901       if (depp)
1902         *depp = pool->solvables[-r->p].name;
1903       return SOLVER_RULE_DISTUPGRADE;
1904     }
1905   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
1906     {
1907       if (fromp)
1908         *fromp = -r->p;
1909       if (depp)
1910         *depp = pool->solvables[-r->p].name;
1911       return SOLVER_RULE_INFARCH;
1912     }
1913   if (rid >= solv->choicerules && rid < solv->choicerules_end)
1914     {
1915       return SOLVER_RULE_CHOICE;
1916     }
1917   if (rid >= solv->learntrules)
1918     {
1919       return SOLVER_RULE_LEARNT;
1920     }
1921   return SOLVER_RULE_UNKNOWN;
1922 }
1923
1924 SolverRuleinfo
1925 solver_ruleclass(Solver *solv, Id rid)
1926 {
1927   if (rid <= 0)
1928     return SOLVER_RULE_UNKNOWN;
1929   if (rid > 0 && rid < solv->rpmrules_end)
1930     return SOLVER_RULE_RPM;
1931   if (rid >= solv->jobrules && rid < solv->jobrules_end)
1932     return SOLVER_RULE_JOB;
1933   if (rid >= solv->updaterules && rid < solv->updaterules_end)
1934     return SOLVER_RULE_UPDATE;
1935   if (rid >= solv->featurerules && rid < solv->featurerules_end)
1936     return SOLVER_RULE_FEATURE;
1937   if (rid >= solv->duprules && rid < solv->duprules_end)
1938     return SOLVER_RULE_DISTUPGRADE;
1939   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
1940     return SOLVER_RULE_INFARCH;
1941   if (rid >= solv->choicerules && rid < solv->choicerules_end)
1942     return SOLVER_RULE_CHOICE;
1943   if (rid >= solv->learntrules)
1944     return SOLVER_RULE_LEARNT;
1945   return SOLVER_RULE_UNKNOWN;
1946 }
1947
1948 void
1949 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
1950 {
1951   Pool *pool = solv->pool;
1952   Id p, *pp;
1953   Rule *r;
1954
1955   queue_empty(q);
1956   r = solv->rules + rid;
1957   FOR_RULELITERALS(p, pp, r)
1958     if (p != -SYSTEMSOLVABLE)
1959       queue_push(q, p);
1960   if (!q->count)
1961     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
1962 }
1963
1964 int
1965 solver_rule2jobidx(Solver *solv, Id rid)
1966 {
1967   if (rid < solv->jobrules || rid >= solv->jobrules_end)
1968     return 0;
1969   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
1970 }
1971
1972 Id
1973 solver_rule2job(Solver *solv, Id rid, Id *whatp)
1974 {
1975   int idx;
1976   if (rid < solv->jobrules || rid >= solv->jobrules_end)
1977     {
1978       if (whatp)
1979         *whatp = 0;
1980       return 0;
1981     }
1982   idx = solv->ruletojob.elements[rid - solv->jobrules];
1983   if (whatp)
1984     *whatp = solv->job.elements[idx + 1];
1985   return solv->job.elements[idx];
1986 }
1987
1988 void
1989 solver_addchoicerules(Solver *solv)
1990 {
1991   Pool *pool = solv->pool;
1992   Map m, mneg;
1993   Rule *r;
1994   Queue q, qi;
1995   int i, j, rid, havechoice;
1996   Id p, d, *pp;
1997   Id p2, pp2;
1998   Solvable *s, *s2;
1999
2000   solv->choicerules = solv->nrules;
2001   if (!pool->installed)
2002     {
2003       solv->choicerules_end = solv->nrules;
2004       return;
2005     }
2006   solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2007   queue_init(&q);
2008   queue_init(&qi);
2009   map_init(&m, pool->nsolvables);
2010   map_init(&mneg, pool->nsolvables);
2011   /* set up negative assertion map from infarch and dup rules */
2012   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2013     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2014       MAPSET(&mneg, -r->p);
2015   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2016     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2017       MAPSET(&mneg, -r->p);
2018   for (rid = 1; rid < solv->rpmrules_end ; rid++)
2019     {
2020       r = solv->rules + rid;
2021       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2022         continue;       /* only look at requires rules */
2023       // solver_printrule(solv, SOLV_DEBUG_RESULT, r);
2024       queue_empty(&q);
2025       queue_empty(&qi);
2026       havechoice = 0;
2027       FOR_RULELITERALS(p, pp, r)
2028         {
2029           if (p < 0)
2030             continue;
2031           s = pool->solvables + p;
2032           if (!s->repo)
2033             continue;
2034           if (s->repo == pool->installed)
2035             {
2036               queue_push(&q, p);
2037               continue;
2038             }
2039           /* check if this package is "blocked" by a installed package */
2040           s2 = 0;
2041           FOR_PROVIDES(p2, pp2, s->name)
2042             {
2043               s2 = pool->solvables + p2;
2044               if (s2->repo != pool->installed)
2045                 continue;
2046               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2047                 continue;
2048               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2049                 continue;
2050               break;
2051             }
2052           if (p2)
2053             {
2054               /* found installed package p2 that we can update to p */
2055               if (MAPTST(&mneg, p))
2056                 continue;
2057               if (policy_is_illegal(solv, s2, s, 0))
2058                 continue;
2059               queue_push(&qi, p2);
2060               queue_push(&q, p);
2061               continue;
2062             }
2063           if (s->obsoletes)
2064             {
2065               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2066               s2 = 0;
2067               while ((obs = *obsp++) != 0)
2068                 {
2069                   FOR_PROVIDES(p2, pp2, obs)
2070                     {
2071                       s2 = pool->solvables + p2;
2072                       if (s2->repo != pool->installed)
2073                         continue;
2074                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2075                         continue;
2076                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2077                         continue;
2078                       break;
2079                     }
2080                   if (p2)
2081                     break;
2082                 }
2083               if (obs)
2084                 {
2085                   /* found installed package p2 that we can update to p */
2086                   if (MAPTST(&mneg, p))
2087                     continue;
2088                   if (policy_is_illegal(solv, s2, s, 0))
2089                     continue;
2090                   queue_push(&qi, p2);
2091                   queue_push(&q, p);
2092                   continue;
2093                 }
2094             }
2095           /* package p is independent of the installed ones */
2096           havechoice = 1;
2097         }
2098       if (!havechoice || !q.count)
2099         continue;       /* no choice */
2100
2101       /* now check the update rules of the installed package.
2102        * if all packages of the update rules are contained in
2103        * the dependency rules, there's no need to set up the choice rule */
2104       map_empty(&m);
2105       FOR_RULELITERALS(p, pp, r)
2106         if (p > 0)
2107           MAPSET(&m, p);
2108       for (i = 0; i < qi.count; i++)
2109         {
2110           if (!qi.elements[i])
2111             continue;
2112           Rule *ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2113           if (!ur->p)
2114             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2115           if (!ur->p)
2116             continue;
2117           FOR_RULELITERALS(p, pp, ur)
2118             if (!MAPTST(&m, p))
2119               break;
2120           if (p)
2121             break;
2122           for (j = i + 1; j < qi.count; j++)
2123             if (qi.elements[i] == qi.elements[j])
2124               qi.elements[j] = 0;
2125         }
2126       if (i == qi.count)
2127         {
2128 #if 0
2129           printf("skipping choice ");
2130           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2131 #endif
2132           continue;
2133         }
2134       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
2135       solver_addrule(solv, r->p, d);
2136       queue_push(&solv->weakruleq, solv->nrules - 1);
2137       solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
2138 #if 0
2139       printf("OLD ");
2140       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2141       printf("WEAK CHOICE ");
2142       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
2143 #endif
2144     }
2145   queue_free(&q);
2146   queue_free(&qi);
2147   map_free(&m);
2148   map_free(&mneg);
2149   solv->choicerules_end = solv->nrules;
2150 }
2151
2152 /* called when a choice rule is disabled by analyze_unsolvable. We also
2153  * have to disable all other choice rules so that the best packages get
2154  * picked */
2155 void
2156 solver_disablechoicerules(Solver *solv, Rule *r)
2157 {
2158   Id rid, p, *pp;
2159   Pool *pool = solv->pool;
2160   Map m;
2161   Rule *or;
2162
2163   or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2164   map_init(&m, pool->nsolvables);
2165   FOR_RULELITERALS(p, pp, or)
2166     if (p > 0)
2167       MAPSET(&m, p);
2168   FOR_RULELITERALS(p, pp, r)
2169     if (p > 0)
2170       MAPCLR(&m, p);
2171   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
2172     {
2173       r = solv->rules + rid;
2174       if (r->d < 0)
2175         continue;
2176       or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2177       FOR_RULELITERALS(p, pp, or)
2178         if (p > 0 && MAPTST(&m, p))
2179           break;
2180       if (p)
2181         solver_disablerule(solv, r);
2182     }
2183 }
2184
2185 static void solver_createcleandepsmap(Solver *solv)
2186 {
2187   Pool *pool = solv->pool;
2188   Repo *installed = solv->installed;
2189   Queue *job = &solv->job;
2190   Map userinstalled;
2191   Map im;
2192   Map installedm;
2193   Rule *r;
2194   Id rid, how, what, select;
2195   Id p, pp, ip, *jp;
2196   Id req, *reqp, sup, *supp;
2197   Solvable *s;
2198   Queue iq;
2199   int i;
2200
2201   map_init(&userinstalled, installed->end - installed->start);
2202   map_init(&im, pool->nsolvables);
2203   map_init(&installedm, pool->nsolvables);
2204   map_empty(&solv->cleandepsmap);
2205   queue_init(&iq);
2206
2207   for (i = 0; i < job->count; i += 2)
2208     {
2209       how = job->elements[i];
2210       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2211         {
2212           what = job->elements[i + 1];
2213           select = how & SOLVER_SELECTMASK;
2214           FOR_JOB_SELECT(p, pp, select, what)
2215             if (pool->solvables[p].repo == installed)
2216               MAPSET(&userinstalled, p - installed->start);
2217         }
2218     }
2219   /* add all positive elements (e.g. locks) to "userinstalled" */
2220   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2221     {
2222       r = solv->rules + rid;
2223       if (r->d < 0)
2224         continue;
2225       FOR_RULELITERALS(p, jp, r)
2226         if (p > 0 && pool->solvables[p].repo == installed)
2227           MAPSET(&userinstalled, p - installed->start);
2228     }
2229   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2230     {
2231       r = solv->rules + rid;
2232       if (r->p >= 0 || r->d != 0)
2233         continue;       /* disabled or not erase */
2234       p = -r->p;
2235       if (pool->solvables[p].repo != installed)
2236         continue;
2237       MAPCLR(&userinstalled, p - installed->start);
2238       i = solv->ruletojob.elements[rid - solv->jobrules];
2239       how = job->elements[i];
2240       if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
2241         queue_push(&iq, p);
2242     }
2243   for (p = installed->start; p < installed->end; p++)
2244     {
2245       if (pool->solvables[p].repo != installed)
2246         continue;
2247       MAPSET(&installedm, p);
2248       MAPSET(&im, p);
2249     }
2250
2251   while (iq.count)
2252     {
2253       ip = queue_shift(&iq);
2254       s = pool->solvables + ip;
2255       if (!MAPTST(&im, ip))
2256         continue;
2257       if (!MAPTST(&installedm, ip))
2258         continue;
2259       if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
2260         continue;
2261       MAPCLR(&im, ip);
2262 #ifdef CLEANDEPSDEBUG
2263       printf("hello %s\n", pool_solvable2str(pool, s));
2264 #endif
2265       if (s->requires)
2266         {
2267           reqp = s->repo->idarraydata + s->requires;
2268           while ((req = *reqp++) != 0)
2269             {
2270               if (req == SOLVABLE_PREREQMARKER)
2271                 continue;
2272 #if 0
2273               /* count number of installed packages that match */
2274               count = 0;
2275               FOR_PROVIDES(p, pp, req)
2276                 if (MAPTST(&installedm, p))
2277                   count++;
2278               if (count > 1)
2279                 continue;
2280 #endif
2281               FOR_PROVIDES(p, pp, req)
2282                 {
2283                   if (MAPTST(&im, p))
2284                     {
2285 #ifdef CLEANDEPSDEBUG
2286                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2287 #endif
2288                       queue_push(&iq, p);
2289                     }
2290                 }
2291             }
2292         }
2293       if (s->recommends)
2294         {
2295           reqp = s->repo->idarraydata + s->recommends;
2296           while ((req = *reqp++) != 0)
2297             {
2298 #if 0
2299               count = 0;
2300               FOR_PROVIDES(p, pp, req)
2301                 if (MAPTST(&installedm, p))
2302                   count++;
2303               if (count > 1)
2304                 continue;
2305 #endif
2306               FOR_PROVIDES(p, pp, req)
2307                 {
2308                   if (MAPTST(&im, p))
2309                     {
2310 #ifdef CLEANDEPSDEBUG
2311                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2312 #endif
2313                       queue_push(&iq, p);
2314                     }
2315                 }
2316             }
2317         }
2318       if (!iq.count)
2319         {
2320           /* supplements pass */
2321           for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2322             {
2323               if (!MAPTST(&installedm, ip))
2324                 continue;
2325               s = pool->solvables + ip;
2326               if (!s->supplements)
2327                 continue;
2328               if (!MAPTST(&im, ip))
2329                 continue;
2330               supp = s->repo->idarraydata + s->supplements;
2331               while ((sup = *supp++) != 0)
2332                 if (!dep_possible(solv, sup, &im) && dep_possible(solv, sup, &installedm))
2333                   break;
2334               /* no longer supplemented, also erase */
2335               if (sup)
2336                 {
2337 #ifdef CLEANDEPSDEBUG
2338                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
2339 #endif
2340                   queue_push(&iq, ip);
2341                 }
2342             }
2343         }
2344     }
2345
2346   /* turn userinstalled into remove set for pruning */
2347   map_empty(&userinstalled);
2348   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2349     {
2350       r = solv->rules + rid;
2351       if (r->p >= 0 || r->d != 0)
2352         continue;       /* disabled or not erase */
2353       p = -r->p;
2354       MAPCLR(&im, p);
2355       if (pool->solvables[p].repo == installed)
2356         MAPSET(&userinstalled, p - installed->start);
2357     }
2358   for (p = installed->start; p < installed->end; p++)
2359     if (MAPTST(&im, p))
2360       queue_push(&iq, p);
2361   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
2362     {
2363       r = solv->rules + rid;
2364       if (r->d < 0)
2365         continue;
2366       FOR_RULELITERALS(p, jp, r)
2367         if (p > 0)
2368           queue_push(&iq, p);
2369     }
2370   /* also put directly addressed packages on the install queue
2371    * so we can mark patterns as installed */
2372   for (i = 0; i < job->count; i += 2)
2373     {
2374       how = job->elements[i];
2375       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2376         {
2377           what = job->elements[i + 1];
2378           select = how & SOLVER_SELECTMASK;
2379           if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
2380             queue_push(&iq, what);
2381         }
2382     }
2383   while (iq.count)
2384     {
2385       ip = queue_shift(&iq);
2386       s = pool->solvables + ip;
2387 #ifdef CLEANDEPSDEBUG
2388       printf("bye %s\n", pool_solvable2str(pool, s));
2389 #endif
2390       if (s->requires)
2391         {
2392           reqp = s->repo->idarraydata + s->requires;
2393           while ((req = *reqp++) != 0)
2394             {
2395               FOR_PROVIDES(p, pp, req)
2396                 {
2397                   if (!MAPTST(&im, p) && MAPTST(&installedm, p))
2398                     {
2399                       if (p == ip)
2400                         continue;
2401                       if (MAPTST(&userinstalled, p - installed->start))
2402                         continue;
2403 #ifdef CLEANDEPSDEBUG
2404                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2405 #endif
2406                       MAPSET(&im, p);
2407                       queue_push(&iq, p);
2408                     }
2409                 }
2410             }
2411         }
2412       if (s->recommends)
2413         {
2414           reqp = s->repo->idarraydata + s->recommends;
2415           while ((req = *reqp++) != 0)
2416             {
2417               FOR_PROVIDES(p, pp, req)
2418                 {
2419                   if (!MAPTST(&im, p) && MAPTST(&installedm, p))
2420                     {
2421                       if (p == ip)
2422                         continue;
2423                       if (MAPTST(&userinstalled, p - installed->start))
2424                         continue;
2425 #ifdef CLEANDEPSDEBUG
2426                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
2427 #endif
2428                       MAPSET(&im, p);
2429                       queue_push(&iq, p);
2430                     }
2431                 }
2432             }
2433         }
2434       if (!iq.count)
2435         {
2436           /* supplements pass */
2437           for (ip = installed->start; ip < installed->end; ip++)
2438             {
2439               if (!MAPTST(&installedm, ip))
2440                 continue;
2441               if (MAPTST(&userinstalled, ip - installed->start))
2442                 continue;
2443               s = pool->solvables + ip;
2444               if (!s->supplements)
2445                 continue;
2446               if (MAPTST(&im, ip) || !MAPTST(&installedm, ip))
2447                 continue;
2448               supp = s->repo->idarraydata + s->supplements;
2449               while ((sup = *supp++) != 0)
2450                 if (dep_possible(solv, sup, &im))
2451                   break;
2452               if (sup)
2453                 {
2454 #ifdef CLEANDEPSDEBUG
2455                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
2456 #endif
2457                   MAPSET(&im, ip);
2458                   queue_push(&iq, ip);
2459                 }
2460             }
2461         }
2462     }
2463     
2464   queue_free(&iq);
2465   for (p = installed->start; p < installed->end; p++)
2466     {
2467       if (pool->solvables[p].repo != installed)
2468         continue;
2469       if (!MAPTST(&im, p))
2470         MAPSET(&solv->cleandepsmap, p - installed->start);
2471     }
2472   map_free(&im);
2473   map_free(&installedm);
2474   map_free(&userinstalled);
2475 }
2476
2477
2478 /* EOF */