rules/selection: use strrchr in EVR checks as well
[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, Map *cleandepsmap, int unneeded);
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_rulecmp(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 void
381 solver_shrinkrules(Solver *solv, int nrules)
382 {
383   solv->nrules = nrules;
384   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
385 }
386
387 /******************************************************************************
388  ***
389  *** rpm rule part: create rules representing the package dependencies
390  ***
391  ***/
392
393 /*
394  *  special multiversion patch conflict handling:
395  *  a patch conflict is also satisfied if some other
396  *  version with the same name/arch that doesn't conflict
397  *  gets installed. The generated rule is thus:
398  *  -patch|-cpack|opack1|opack2|...
399  */
400 static Id
401 makemultiversionconflict(Solver *solv, Id n, Id con)
402 {
403   Pool *pool = solv->pool;
404   Solvable *s, *sn;
405   Queue q;
406   Id p, pp, qbuf[64];
407
408   sn = pool->solvables + n;
409   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
410   queue_push(&q, -n);
411   FOR_PROVIDES(p, pp, sn->name)
412     {
413       s = pool->solvables + p;
414       if (s->name != sn->name || s->arch != sn->arch)
415         continue;
416       if (!MAPTST(&solv->multiversion, p))
417         continue;
418       if (pool_match_nevr(pool, pool->solvables + p, con))
419         continue;
420       /* here we have a multiversion solvable that doesn't conflict */
421       /* thus we're not in conflict if it is installed */
422       queue_push(&q, p);
423     }
424   if (q.count == 1)
425     return -n;  /* no other package found, generate normal conflict */
426   return pool_queuetowhatprovides(pool, &q);
427 }
428
429 static inline void
430 addrpmrule(Solver *solv, Id p, Id d, int type, Id dep)
431 {
432   if (!solv->ruleinfoq)
433     solver_addrule(solv, p, d);
434   else
435     addrpmruleinfo(solv, p, d, type, dep);
436 }
437
438 /*-------------------------------------------------------------------
439  * 
440  * add (install) rules for solvable
441  * 
442  * s: Solvable for which to add rules
443  * m: m[s] = 1 for solvables which have rules, prevent rule duplication
444  * 
445  * Algorithm: 'visit all nodes of a graph'. The graph nodes are
446  *  solvables, the edges their dependencies.
447  *  Starting from an installed solvable, this will create all rules
448  *  representing the graph created by the solvables dependencies.
449  * 
450  * for unfulfilled requirements, conflicts, obsoletes,....
451  * add a negative assertion for solvables that are not installable
452  * 
453  * It will also create rules for all solvables referenced by 's'
454  *  i.e. descend to all providers of requirements of 's'
455  *
456  */
457
458 void
459 solver_addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
460 {
461   Pool *pool = solv->pool;
462   Repo *installed = solv->installed;
463
464   /* 'work' queue. keeps Ids of solvables we still have to work on.
465      And buffer for it. */
466   Queue workq;
467   Id workqbuf[64];
468     
469   int i;
470     /* if to add rules for broken deps ('rpm -V' functionality)
471      * 0 = yes, 1 = no
472      */
473   int dontfix;
474     /* Id var and pointer for each dependency
475      * (not used in parallel)
476      */
477   Id req, *reqp;
478   Id con, *conp;
479   Id obs, *obsp;
480   Id rec, *recp;
481   Id sug, *sugp;
482   Id p, pp;             /* whatprovides loops */
483   Id *dp;               /* ptr to 'whatprovides' */
484   Id n;                 /* Id for current solvable 's' */
485
486   queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
487   queue_push(&workq, s - pool->solvables);      /* push solvable Id to work queue */
488
489   /* loop until there's no more work left */
490   while (workq.count)
491     {
492       /*
493        * n: Id of solvable
494        * s: Pointer to solvable
495        */
496
497       n = queue_shift(&workq);          /* 'pop' next solvable to work on from queue */
498       if (m)
499         {
500           if (MAPTST(m, n))             /* continue if already visited */
501             continue;
502           MAPSET(m, n);                 /* mark as visited */
503         }
504
505       s = pool->solvables + n;          /* s = Solvable in question */
506
507       dontfix = 0;
508       if (installed                     /* Installed system available */
509           && s->repo == installed       /* solvable is installed */
510           && !solv->fixmap_all          /* NOT repair errors in rpm dependency graph */
511           && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
512         {
513           dontfix = 1;                  /* dont care about broken rpm deps */
514         }
515
516       if (!dontfix)
517         {
518           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC
519                 ? pool_disabled_solvable(pool, s) 
520                 : !pool_installable(pool, s))
521             {
522               POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvable2str(pool, s), (Id)(s - pool->solvables));
523               addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOT_INSTALLABLE, 0);
524             }
525         }
526
527       /* yet another SUSE hack, sigh */
528       if (pool->nscallback && !strncmp("product:", pool_id2str(pool, s->name), 8))
529         {
530           Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, n);
531           if (buddy > 0 && buddy != SYSTEMSOLVABLE && buddy != n && buddy < pool->nsolvables)
532             {
533               addrpmrule(solv, n, -buddy, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + n));
534               addrpmrule(solv, buddy, -n, SOLVER_RULE_RPM_PACKAGE_REQUIRES, solvable_selfprovidedep(pool->solvables + buddy)); 
535               if (m && !MAPTST(m, buddy))
536                 queue_push(&workq, buddy);
537             }
538         }
539
540       /*-----------------------------------------
541        * check requires of s
542        */
543
544       if (s->requires)
545         {
546           reqp = s->repo->idarraydata + s->requires;
547           while ((req = *reqp++) != 0)            /* go through all requires */
548             {
549               if (req == SOLVABLE_PREREQMARKER)   /* skip the marker */
550                 continue;
551
552               /* find list of solvables providing 'req' */
553               dp = pool_whatprovides_ptr(pool, req);
554
555               if (*dp == SYSTEMSOLVABLE)          /* always installed */
556                 continue;
557
558               if (dontfix)
559                 {
560                   /* the strategy here is to not insist on dependencies
561                    * that are already broken. so if we find one provider
562                    * that was already installed, we know that the
563                    * dependency was not broken before so we enforce it */
564                  
565                   /* check if any of the providers for 'req' is installed */
566                   for (i = 0; (p = dp[i]) != 0; i++)
567                     {
568                       if (pool->solvables[p].repo == installed)
569                         break;          /* provider was installed */
570                     }
571                   /* didn't find an installed provider: previously broken dependency */
572                   if (!p)
573                     {
574                       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
575                       continue;
576                     }
577                 }
578
579               if (!*dp)
580                 {
581                   /* nothing provides req! */
582                   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));
583                   addrpmrule(solv, -n, 0, SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP, req);
584                   continue;
585                 }
586
587               IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
588                 {
589                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION,"  %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
590                   for (i = 0; dp[i]; i++)
591                     POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "   provided by %s\n", pool_solvid2str(pool, dp[i]));
592                 }
593
594               /* add 'requires' dependency */
595               /* rule: (-requestor|provider1|provider2|...|providerN) */
596               addrpmrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_RPM_PACKAGE_REQUIRES, req);
597
598               /* descend the dependency tree
599                  push all non-visited providers on the work queue */
600               if (m)
601                 {
602                   for (; *dp; dp++)
603                     {
604                       if (!MAPTST(m, *dp))
605                         queue_push(&workq, *dp);
606                     }
607                 }
608
609             } /* while, requirements of n */
610
611         } /* if, requirements */
612
613       /* that's all we check for src packages */
614       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
615         continue;
616
617       /*-----------------------------------------
618        * check conflicts of s
619        */
620
621       if (s->conflicts)
622         {
623           int ispatch = 0;
624
625           /* we treat conflicts in patches a bit differen:
626            * - nevr matching
627            * - multiversion handling
628            * XXX: we should really handle this different, looking
629            * at the name is a bad hack
630            */
631           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
632             ispatch = 1;
633           conp = s->repo->idarraydata + s->conflicts;
634           /* foreach conflicts of 's' */
635           while ((con = *conp++) != 0)
636             {
637               /* foreach providers of a conflict of 's' */
638               FOR_PROVIDES(p, pp, con)
639                 {
640                   if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
641                     continue;
642                   /* dontfix: dont care about conflicts with already installed packs */
643                   if (dontfix && pool->solvables[p].repo == installed)
644                     continue;
645                   /* p == n: self conflict */
646                   if (p == n && pool->forbidselfconflicts)
647                     {
648                       if (ISRELDEP(con))
649                         {
650                           Reldep *rd = GETRELDEP(pool, con);
651                           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
652                             continue;
653                         }
654                       p = 0;    /* make it a negative assertion, aka 'uninstallable' */
655                     }
656                   if (p && ispatch && solv->multiversion.size && MAPTST(&solv->multiversion, p) && ISRELDEP(con))
657                     {
658                       /* our patch conflicts with a multiversion package */
659                       p = -makemultiversionconflict(solv, p, con);
660                     }
661                  /* rule: -n|-p: either solvable _or_ provider of conflict */
662                   addrpmrule(solv, -n, -p, p ? SOLVER_RULE_RPM_PACKAGE_CONFLICT : SOLVER_RULE_RPM_SELF_CONFLICT, con);
663                 }
664             }
665         }
666
667       /*-----------------------------------------
668        * check obsoletes and implicit obsoletes of a package
669        * if ignoreinstalledsobsoletes is not set, we're also checking
670        * obsoletes of installed packages (like newer rpm versions)
671        */
672       if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
673         {
674           int multi = solv->multiversion.size && MAPTST(&solv->multiversion, n);
675           int isinstalled = (installed && s->repo == installed);
676           if (s->obsoletes && (!multi || solv->keepexplicitobsoletes))
677             {
678               obsp = s->repo->idarraydata + s->obsoletes;
679               /* foreach obsoletes */
680               while ((obs = *obsp++) != 0)
681                 {
682                   /* foreach provider of an obsoletes of 's' */ 
683                   FOR_PROVIDES(p, pp, obs)
684                     {
685                       Solvable *ps = pool->solvables + p;
686                       if (p == n)
687                         continue;
688                       if (isinstalled && dontfix && ps->repo == installed)
689                         continue;       /* don't repair installed/installed problems */
690                       if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
691                           && !pool_match_nevr(pool, ps, obs))
692                         continue;
693                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
694                         continue;
695                       if (!isinstalled)
696                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_PACKAGE_OBSOLETES, obs);
697                       else
698                         addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES, obs);
699                     }
700                 }
701             }
702           /* check implicit obsoletes
703            * for installed packages we only need to check installed/installed problems (and
704            * only when dontfix is not set), as the others are picked up when looking at the
705            * uninstalled package.
706            */
707           if (!isinstalled || !dontfix)
708             {
709               FOR_PROVIDES(p, pp, s->name)
710                 {
711                   Solvable *ps = pool->solvables + p;
712                   if (p == n)
713                     continue;
714                   if (isinstalled && ps->repo != installed)
715                     continue;
716                   /* we still obsolete packages with same nevra, like rpm does */
717                   /* (actually, rpm mixes those packages. yuck...) */
718                   if (multi && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
719                     continue;
720                   if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
721                     continue;
722                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
723                     continue;
724                   if (s->name == ps->name)
725                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_SAME_NAME, 0);
726                   else
727                     addrpmrule(solv, -n, -p, SOLVER_RULE_RPM_IMPLICIT_OBSOLETES, s->name);
728                 }
729             }
730         }
731
732       /*-----------------------------------------
733        * add recommends to the work queue
734        */
735       if (s->recommends && m)
736         {
737           recp = s->repo->idarraydata + s->recommends;
738           while ((rec = *recp++) != 0)
739             {
740               FOR_PROVIDES(p, pp, rec)
741                 if (!MAPTST(m, p))
742                   queue_push(&workq, p);
743             }
744         }
745       if (s->suggests && m)
746         {
747           sugp = s->repo->idarraydata + s->suggests;
748           while ((sug = *sugp++) != 0)
749             {
750               FOR_PROVIDES(p, pp, sug)
751                 if (!MAPTST(m, p))
752                   queue_push(&workq, p);
753             }
754         }
755     }
756   queue_free(&workq);
757 }
758
759
760 /*-------------------------------------------------------------------
761  * 
762  * Add rules for packages possibly selected in by weak dependencies
763  *
764  * m: already added solvables
765  */
766
767 void
768 solver_addrpmrulesforweak(Solver *solv, Map *m)
769 {
770   Pool *pool = solv->pool;
771   Solvable *s;
772   Id sup, *supp;
773   int i, n;
774
775   /* foreach solvable in pool */
776   for (i = n = 1; n < pool->nsolvables; i++, n++)
777     {
778       if (i == pool->nsolvables)                /* wrap i */
779         i = 1;
780       if (MAPTST(m, i))                         /* already added that one */
781         continue;
782
783       s = pool->solvables + i;
784       if (!s->repo)
785         continue;
786       if (s->repo != pool->installed && !pool_installable(pool, s))
787         continue;       /* only look at installable ones */
788
789       sup = 0;
790       if (s->supplements)
791         {
792           /* find possible supplements */
793           supp = s->repo->idarraydata + s->supplements;
794           while ((sup = *supp++) != 0)
795             if (dep_possible(solv, sup, m))
796               break;
797         }
798
799       /* if nothing found, check for enhances */
800       if (!sup && s->enhances)
801         {
802           supp = s->repo->idarraydata + s->enhances;
803           while ((sup = *supp++) != 0)
804             if (dep_possible(solv, sup, m))
805               break;
806         }
807       /* if nothing found, goto next solvables */
808       if (!sup)
809         continue;
810       solver_addrpmrulesforsolvable(solv, s, m);
811       n = 0;                    /* check all solvables again because we added solvables to m */
812     }
813 }
814
815
816 /*-------------------------------------------------------------------
817  * 
818  * add package rules for possible updates
819  * 
820  * s: solvable
821  * m: map of already visited solvables
822  * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
823  */
824
825 void
826 solver_addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
827 {
828   Pool *pool = solv->pool;
829   int i;
830     /* queue and buffer for it */
831   Queue qs;
832   Id qsbuf[64];
833
834   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
835     /* find update candidates for 's' */
836   policy_findupdatepackages(solv, s, &qs, allow_all);
837     /* add rule for 's' if not already done */
838   if (!MAPTST(m, s - pool->solvables))
839     solver_addrpmrulesforsolvable(solv, s, m);
840     /* foreach update candidate, add rule if not already done */
841   for (i = 0; i < qs.count; i++)
842     if (!MAPTST(m, qs.elements[i]))
843       solver_addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
844   queue_free(&qs);
845 }
846
847
848 /***********************************************************************
849  ***
850  ***  Update/Feature rule part
851  ***
852  ***  Those rules make sure an installed package isn't silently deleted
853  ***
854  ***/
855
856 static Id
857 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
858 {
859   Pool *pool = solv->pool;
860   int i;
861
862   policy_findupdatepackages(solv, s, qs, allow_all ? allow_all : 2);
863   if (!qs->count)
864     {
865       if (allow_all)
866         return 0;       /* orphaned, don't create feature rule */
867       /* check if this is an orphaned package */
868       policy_findupdatepackages(solv, s, qs, 1);
869       if (!qs->count)
870         return 0;       /* orphaned, don't create update rule */
871       qs->count = 0;
872       return -SYSTEMSOLVABLE;   /* supported but not installable */
873     }
874   if (allow_all)
875     return s - pool->solvables;
876   /* check if it is ok to keep the installed package */
877   for (i = 0; i < qs->count; i++)
878     {
879       Solvable *ns = pool->solvables + qs->elements[i];
880       if (s->evr == ns->evr && solvable_identical(s, ns))
881         return s - pool->solvables;
882     }
883   /* nope, it must be some other package */
884   return -SYSTEMSOLVABLE;
885 }
886
887 /* add packages from the dup repositories to the update candidates
888  * this isn't needed for the global dup mode as all packages are
889  * from dup repos in that case */
890 static void
891 addduppackages(Solver *solv, Solvable *s, Queue *qs)
892 {
893   Queue dupqs;
894   Id p, dupqsbuf[64];
895   int i;
896   int oldnoupdateprovide = solv->noupdateprovide;
897
898   queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
899   solv->noupdateprovide = 1;
900   policy_findupdatepackages(solv, s, &dupqs, 2);
901   solv->noupdateprovide = oldnoupdateprovide;
902   for (i = 0; i < dupqs.count; i++)
903     {
904       p = dupqs.elements[i];
905       if (MAPTST(&solv->dupmap, p))
906         queue_pushunique(qs, p);
907     }
908   queue_free(&dupqs);
909 }
910
911 /*-------------------------------------------------------------------
912  * 
913  * add rule for update
914  *   (A|A1|A2|A3...)  An = update candidates for A
915  *
916  * s = (installed) solvable
917  */
918
919 void
920 solver_addupdaterule(Solver *solv, Solvable *s, int allow_all)
921 {
922   /* installed packages get a special upgrade allowed rule */
923   Pool *pool = solv->pool;
924   Id p, d;
925   Queue qs;
926   Id qsbuf[64];
927
928   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
929   p = s - pool->solvables;
930   /* find update candidates for 's' */
931   if (solv->dupmap_all)
932     p = finddistupgradepackages(solv, s, &qs, allow_all);
933   else
934     policy_findupdatepackages(solv, s, &qs, allow_all);
935   if (!allow_all && !solv->dupmap_all && solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
936     addduppackages(solv, s, &qs);
937
938   if (!allow_all && qs.count && solv->multiversion.size)
939     {
940       int i, j;
941
942       d = pool_queuetowhatprovides(pool, &qs);
943       /* filter out all multiversion packages as they don't update */
944       for (i = j = 0; i < qs.count; i++)
945         {
946           if (MAPTST(&solv->multiversion, qs.elements[i]))
947             {
948               /* it's ok if they have same nevra */
949               Solvable *ps = pool->solvables + qs.elements[i];
950               if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
951                 continue;
952             }
953           qs.elements[j++] = qs.elements[i];
954         }
955       if (j < qs.count)
956         {
957           if (d && solv->installed && s->repo == solv->installed &&
958               (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
959             {
960               if (!solv->multiversionupdaters)
961                 solv->multiversionupdaters = solv_calloc(solv->installed->end - solv->installed->start, sizeof(Id));
962               solv->multiversionupdaters[s - pool->solvables - solv->installed->start] = d;
963             }
964           if (j == 0 && p == -SYSTEMSOLVABLE && solv->dupmap_all)
965             {
966               queue_push(&solv->orphaned, s - pool->solvables); /* treat as orphaned */
967               j = qs.count;
968             }
969           qs.count = j;
970         }
971       else if (p != -SYSTEMSOLVABLE)
972         {
973           /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
974           queue_free(&qs);
975           solver_addrule(solv, p, d);   /* allow update of s */
976           return;
977         }
978     }
979   if (qs.count && p == -SYSTEMSOLVABLE)
980     p = queue_shift(&qs);
981   d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
982   queue_free(&qs);
983   solver_addrule(solv, p, d);   /* allow update of s */
984 }
985
986 static inline void 
987 disableupdaterule(Solver *solv, Id p)
988 {
989   Rule *r;
990
991   MAPSET(&solv->noupdate, p - solv->installed->start);
992   r = solv->rules + solv->updaterules + (p - solv->installed->start);
993   if (r->p && r->d >= 0)
994     solver_disablerule(solv, r);
995   r = solv->rules + solv->featurerules + (p - solv->installed->start);
996   if (r->p && r->d >= 0)
997     solver_disablerule(solv, r);
998   if (solv->bestrules_pkg)
999     {
1000       int i, ni;
1001       ni = solv->bestrules_end - solv->bestrules;
1002       for (i = 0; i < ni; i++)
1003         if (solv->bestrules_pkg[i] == p)
1004           solver_disablerule(solv, solv->rules + solv->bestrules + i);
1005     }
1006 }
1007
1008 static inline void 
1009 reenableupdaterule(Solver *solv, Id p)
1010 {
1011   Pool *pool = solv->pool;
1012   Rule *r;
1013
1014   MAPCLR(&solv->noupdate, p - solv->installed->start);
1015   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1016   if (r->p)
1017     {    
1018       if (r->d < 0)
1019         {
1020           solver_enablerule(solv, r);
1021           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1022             {
1023               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1024               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1025             }
1026         }
1027     }
1028   else
1029     {
1030       r = solv->rules + solv->featurerules + (p - solv->installed->start);
1031       if (r->p && r->d < 0)
1032         {
1033           solver_enablerule(solv, r);
1034           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1035             {
1036               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1037               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1038             }
1039         }
1040     }
1041   if (solv->bestrules_pkg)
1042     {
1043       int i, ni;
1044       ni = solv->bestrules_end - solv->bestrules;
1045       for (i = 0; i < ni; i++)
1046         if (solv->bestrules_pkg[i] == p)
1047           solver_enablerule(solv, solv->rules + solv->bestrules + i);
1048     }
1049 }
1050
1051
1052 /***********************************************************************
1053  ***
1054  ***  Infarch rule part
1055  ***
1056  ***  Infarch rules make sure the solver uses the best architecture of
1057  ***  a package if multiple archetectures are available
1058  ***
1059  ***/
1060
1061 void
1062 solver_addinfarchrules(Solver *solv, Map *addedmap)
1063 {
1064   Pool *pool = solv->pool;
1065   int first, i, j;
1066   Id p, pp, a, aa, bestarch;
1067   Solvable *s, *ps, *bests;
1068   Queue badq, allowedarchs;
1069
1070   queue_init(&badq);
1071   queue_init(&allowedarchs);
1072   solv->infarchrules = solv->nrules;
1073   for (i = 1; i < pool->nsolvables; i++)
1074     {
1075       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1076         continue;
1077       s = pool->solvables + i;
1078       first = i;
1079       bestarch = 0;
1080       bests = 0;
1081       queue_empty(&allowedarchs);
1082       FOR_PROVIDES(p, pp, s->name)
1083         {
1084           ps = pool->solvables + p;
1085           if (ps->name != s->name || !MAPTST(addedmap, p))
1086             continue;
1087           if (p == i)
1088             first = 0;
1089           if (first)
1090             break;
1091           a = ps->arch;
1092           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1093           if (a != 1 && pool->installed && ps->repo == pool->installed)
1094             {
1095               if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1096                 queue_pushunique(&allowedarchs, ps->arch);      /* also ok to keep this architecture */
1097               continue;         /* ignore installed solvables when calculating the best arch */
1098             }
1099           if (a && a != 1 && (!bestarch || a < bestarch))
1100             {
1101               bestarch = a;
1102               bests = ps;
1103             }
1104         }
1105       if (first)
1106         continue;
1107       /* speed up common case where installed package already has best arch */
1108       if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1109         allowedarchs.count--;   /* installed arch is best */
1110       queue_empty(&badq);
1111       FOR_PROVIDES(p, pp, s->name)
1112         {
1113           ps = pool->solvables + p;
1114           if (ps->name != s->name || !MAPTST(addedmap, p))
1115             continue;
1116           a = ps->arch;
1117           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1118           if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1119             {
1120               if (pool->installed && ps->repo == pool->installed)
1121                 continue;       /* always ok to keep an installed package */
1122               for (j = 0; j < allowedarchs.count; j++)
1123                 {
1124                   aa = allowedarchs.elements[j];
1125                   if (ps->arch == aa)
1126                     break;
1127                   aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1128                   if (aa && ((a ^ aa) & 0xffff0000) == 0)
1129                     break;      /* compatible */
1130                 }
1131               if (j == allowedarchs.count)
1132                 queue_push(&badq, p);
1133             }
1134         }
1135       if (!badq.count)
1136         continue;
1137       /* block all solvables in the badq! */
1138       for (j = 0; j < badq.count; j++)
1139         {
1140           p = badq.elements[j];
1141           solver_addrule(solv, -p, 0);
1142         }
1143     }
1144   queue_free(&badq);
1145   queue_free(&allowedarchs);
1146   solv->infarchrules_end = solv->nrules;
1147 }
1148
1149 static inline void
1150 disableinfarchrule(Solver *solv, Id name)
1151 {
1152   Pool *pool = solv->pool;
1153   Rule *r;
1154   int i;
1155   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1156     {
1157       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1158         solver_disablerule(solv, r);
1159     }
1160 }
1161
1162 static inline void
1163 reenableinfarchrule(Solver *solv, Id name)
1164 {
1165   Pool *pool = solv->pool;
1166   Rule *r;
1167   int i;
1168   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1169     {
1170       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1171         {
1172           solver_enablerule(solv, r);
1173           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1174             {
1175               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1176               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1177             }
1178         }
1179     }
1180 }
1181
1182
1183 /***********************************************************************
1184  ***
1185  ***  Dup rule part
1186  ***
1187  ***  Dup rules make sure a package is selected from the specified dup
1188  ***  repositories if an update candidate is included in one of them.
1189  ***
1190  ***/
1191
1192 static inline void 
1193 add_cleandeps_package(Solver *solv, Id p)
1194 {
1195   if (!solv->cleandeps_updatepkgs)
1196     {    
1197       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
1198       queue_init(solv->cleandeps_updatepkgs);
1199     }    
1200   queue_pushunique(solv->cleandeps_updatepkgs, p); 
1201 }
1202
1203 static inline void
1204 solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
1205 {
1206   Pool *pool = solv->pool;
1207   Solvable *ps, *s = pool->solvables + p;
1208   Repo *installed = solv->installed;
1209   Id pi, pip, obs, *obsp;
1210
1211   MAPSET(&solv->dupinvolvedmap, p);
1212   if (targeted)
1213     MAPSET(&solv->dupmap, p);
1214   FOR_PROVIDES(pi, pip, s->name)
1215     {
1216       ps = pool->solvables + pi;
1217       if (ps->name != s->name)
1218         continue;
1219       MAPSET(&solv->dupinvolvedmap, pi);
1220       if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1221         {
1222           Id *opp, pi2;
1223           for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1224             if (pool->solvables[pi2].repo != installed)
1225               MAPSET(&solv->dupinvolvedmap, pi2);
1226         }
1227       if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1228         {
1229           if (!solv->bestupdatemap.size)
1230             map_grow(&solv->bestupdatemap, installed->end - installed->start);
1231           MAPSET(&solv->bestupdatemap, pi - installed->start);
1232         }
1233       if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1234         add_cleandeps_package(solv, pi);
1235       if (!targeted && ps->repo != installed)
1236         MAPSET(&solv->dupmap, pi);
1237     }
1238   if (s->repo == installed && solv->obsoletes && solv->obsoletes[p - installed->start])
1239     {
1240       Id *opp;
1241       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1242         {
1243           ps = pool->solvables + pi;
1244           if (ps->repo == installed)
1245             continue;
1246           MAPSET(&solv->dupinvolvedmap, pi);
1247           if (!targeted)
1248             MAPSET(&solv->dupmap, pi);
1249         }
1250     }
1251   if (targeted && s->repo != installed && s->obsoletes)
1252     {
1253       /* XXX: check obsoletes/provides combination */
1254       obsp = s->repo->idarraydata + s->obsoletes;
1255       while ((obs = *obsp++) != 0)
1256         {
1257           FOR_PROVIDES(pi, pip, obs)
1258             {
1259               Solvable *ps = pool->solvables + pi;
1260               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1261                 continue;
1262               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1263                 continue;
1264               MAPSET(&solv->dupinvolvedmap, pi);
1265               if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1266                 {
1267                   Id *opp, pi2;
1268                   for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1269                     if (pool->solvables[pi2].repo != installed)
1270                       MAPSET(&solv->dupinvolvedmap, pi2);
1271                 }
1272               if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1273                 {
1274                   if (!solv->bestupdatemap.size)
1275                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
1276                   MAPSET(&solv->bestupdatemap, pi - installed->start);
1277                 }
1278               if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1279                 add_cleandeps_package(solv, pi);
1280             }
1281         }
1282     }
1283 }
1284
1285 void
1286 solver_createdupmaps(Solver *solv)
1287 {
1288   Queue *job = &solv->job;
1289   Pool *pool = solv->pool;
1290   Repo *installed = solv->installed;
1291   Id select, how, what, p, pp;
1292   Solvable *s;
1293   int i, targeted;
1294
1295   map_init(&solv->dupmap, pool->nsolvables);
1296   map_init(&solv->dupinvolvedmap, pool->nsolvables);
1297   for (i = 0; i < job->count; i += 2)
1298     {
1299       how = job->elements[i];
1300       select = job->elements[i] & SOLVER_SELECTMASK;
1301       what = job->elements[i + 1];
1302       switch (how & SOLVER_JOBMASK)
1303         {
1304         case SOLVER_DISTUPGRADE:
1305           if (select == SOLVER_SOLVABLE_REPO)
1306             {
1307               Repo *repo;
1308               if (what <= 0 || what > pool->nrepos)
1309                 break;
1310               repo = pool_id2repo(pool, what);
1311               if (!repo)
1312                 break;
1313               if (repo != installed && !(how & SOLVER_TARGETED) && solv->noautotarget)
1314                 break;
1315               targeted = repo != installed || (how & SOLVER_TARGETED) != 0;
1316               FOR_REPO_SOLVABLES(repo, p, s)
1317                 {
1318                   if (repo != installed && !pool_installable(pool, s))
1319                     continue;
1320                   solver_addtodupmaps(solv, p, how, targeted);
1321                 }
1322             }
1323           else
1324             {
1325               targeted = how & SOLVER_TARGETED ? 1 : 0;
1326               if (installed && !targeted && !solv->noautotarget)
1327                 {
1328                   FOR_JOB_SELECT(p, pp, select, what)
1329                     if (pool->solvables[p].repo == installed)
1330                       break;
1331                   targeted = p == 0;
1332                 }
1333               else if (!installed && !solv->noautotarget)
1334                 targeted = 1;
1335               FOR_JOB_SELECT(p, pp, select, what)
1336                 {
1337                   Solvable *s = pool->solvables + p;
1338                   if (!s->repo)
1339                     continue;
1340                   if (s->repo != installed && !targeted)
1341                     continue;
1342                   if (s->repo != installed && !pool_installable(pool, s))
1343                     continue;
1344                   solver_addtodupmaps(solv, p, how, targeted);
1345                 }
1346             }
1347           break;
1348         default:
1349           break;
1350         }
1351     }
1352   MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
1353 }
1354
1355 void
1356 solver_freedupmaps(Solver *solv)
1357 {
1358   map_free(&solv->dupmap);
1359   /* we no longer free solv->dupinvolvedmap as we need it in
1360    * policy's priority pruning code. sigh. */
1361 }
1362
1363 void
1364 solver_addduprules(Solver *solv, Map *addedmap)
1365 {
1366   Pool *pool = solv->pool;
1367   Id p, pp;
1368   Solvable *s, *ps;
1369   int first, i;
1370
1371   solv->duprules = solv->nrules;
1372   for (i = 1; i < pool->nsolvables; i++)
1373     {
1374       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1375         continue;
1376       s = pool->solvables + i;
1377       first = i;
1378       FOR_PROVIDES(p, pp, s->name)
1379         {
1380           ps = pool->solvables + p;
1381           if (ps->name != s->name || !MAPTST(addedmap, p))
1382             continue;
1383           if (p == i)
1384             first = 0;
1385           if (first)
1386             break;
1387           if (!MAPTST(&solv->dupinvolvedmap, p))
1388             continue;
1389           if (solv->installed && ps->repo == solv->installed)
1390             {
1391               if (!solv->updatemap.size)
1392                 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
1393               MAPSET(&solv->updatemap, p - solv->installed->start);
1394               if (!MAPTST(&solv->dupmap, p))
1395                 {
1396                   Id ip, ipp;
1397                   /* is installed identical to a good one? */
1398                   FOR_PROVIDES(ip, ipp, ps->name)
1399                     {
1400                       Solvable *is = pool->solvables + ip;
1401                       if (!MAPTST(&solv->dupmap, ip))
1402                         continue;
1403                       if (is->evr == ps->evr && solvable_identical(ps, is))
1404                         break;
1405                     }
1406                   if (!ip)
1407                     solver_addrule(solv, -p, 0);        /* no match, sorry */
1408                   else
1409                     MAPSET(&solv->dupmap, p);           /* for best rules processing */
1410                 }
1411             }
1412           else if (!MAPTST(&solv->dupmap, p))
1413             solver_addrule(solv, -p, 0);
1414         }
1415     }
1416   solv->duprules_end = solv->nrules;
1417 }
1418
1419
1420 static inline void
1421 disableduprule(Solver *solv, Id name)
1422 {
1423   Pool *pool = solv->pool;
1424   Rule *r;
1425   int i;
1426   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++) 
1427     {    
1428       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1429         solver_disablerule(solv, r);
1430     }    
1431 }
1432
1433 static inline void 
1434 reenableduprule(Solver *solv, Id name)
1435 {
1436   Pool *pool = solv->pool;
1437   Rule *r;
1438   int i;
1439   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++) 
1440     {    
1441       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1442         {
1443           solver_enablerule(solv, r);
1444           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1445             {
1446               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1447               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1448             }
1449         }
1450     }
1451 }
1452
1453
1454 /***********************************************************************
1455  ***
1456  ***  Policy rule disabling/reenabling
1457  ***
1458  ***  Disable all policy rules that conflict with our jobs. If a job
1459  ***  gets disabled later on, reenable the involved policy rules again.
1460  ***
1461  ***/
1462
1463 #define DISABLE_UPDATE  1
1464 #define DISABLE_INFARCH 2
1465 #define DISABLE_DUP     3
1466
1467 /* 
1468  * add all installed packages that package p obsoletes to Queue q.
1469  * Package p is not installed. Also, we know that if
1470  * solv->keepexplicitobsoletes is not set, p is not in the multiversion map.
1471  * Entries may get added multiple times.
1472  */
1473 static void
1474 add_obsoletes(Solver *solv, Id p, Queue *q)
1475 {
1476   Pool *pool = solv->pool;
1477   Repo *installed = solv->installed;
1478   Id p2, pp2;
1479   Solvable *s = pool->solvables + p;
1480   Id obs, *obsp;
1481   Id lastp2 = 0;
1482
1483   if (!solv->keepexplicitobsoletes || !(solv->multiversion.size && MAPTST(&solv->multiversion, p)))
1484     {
1485       FOR_PROVIDES(p2, pp2, s->name)
1486         {
1487           Solvable *ps = pool->solvables + p2;
1488           if (ps->repo != installed)
1489             continue;
1490           if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
1491             continue;
1492           if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps)) 
1493             continue;
1494           queue_push(q, p2);
1495           lastp2 = p2;
1496         }
1497     }
1498   if (!s->obsoletes)
1499     return;
1500   obsp = s->repo->idarraydata + s->obsoletes;
1501   while ((obs = *obsp++) != 0)
1502     FOR_PROVIDES(p2, pp2, obs) 
1503       {
1504         Solvable *ps = pool->solvables + p2;
1505         if (ps->repo != installed)
1506           continue;
1507         if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1508           continue;
1509         if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps)) 
1510           continue;
1511         if (p2 == lastp2)
1512           continue;
1513         queue_push(q, p2);
1514         lastp2 = p2;
1515       }
1516 }
1517
1518 /*
1519  * Call add_obsoletes and intersect the result with the
1520  * elements in Queue q starting at qstart.
1521  * Assumes that it's the first call if qstart == q->count.
1522  * May use auxillary map m for the intersection process, all
1523  * elements of q starting at qstart must have their bit cleared.
1524  * (This is also true after the function returns.)
1525  */
1526 static void
1527 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
1528 {
1529   int i, j;
1530   int qcount = q->count;
1531
1532   add_obsoletes(solv, p, q);
1533   if (qcount == qstart)
1534     return;     /* first call */
1535   if (qcount == q->count)
1536     j = qstart; 
1537   else if (qcount == qstart + 1)
1538     {
1539       /* easy if there's just one element */
1540       j = qstart;
1541       for (i = qcount; i < q->count; i++)
1542         if (q->elements[i] == q->elements[qstart])
1543           {
1544             j++;        /* keep the element */
1545             break;
1546           }
1547     }
1548   else if (!m->size && q->count - qstart <= 8)
1549     {
1550       /* faster than a map most of the time */
1551       int k;
1552       for (i = j = qstart; i < qcount; i++)
1553         {
1554           Id ip = q->elements[i];
1555           for (k = qcount; k < q->count; k++)
1556             if (q->elements[k] == ip)
1557               {
1558                 q->elements[j++] = ip;
1559                 break;
1560               }
1561         }
1562     }
1563   else
1564     {
1565       /* for the really pathologic cases we use the map */
1566       Repo *installed = solv->installed;
1567       if (!m->size)
1568         map_init(m, installed->end - installed->start);
1569       for (i = qcount; i < q->count; i++)
1570         MAPSET(m, q->elements[i] - installed->start);
1571       for (i = j = qstart; i < qcount; i++)
1572         if (MAPTST(m, q->elements[i] - installed->start))
1573           {
1574             MAPCLR(m, q->elements[i] - installed->start);
1575             q->elements[j++] = q->elements[i];
1576           }
1577     }
1578   queue_truncate(q, j);
1579 }
1580
1581 static void
1582 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
1583 {
1584   Pool *pool = solv->pool;
1585   Id select, p, pp;
1586   Repo *installed;
1587   Solvable *s;
1588   int i, j, set, qstart;
1589   Map omap;
1590
1591   installed = solv->installed;
1592   select = how & SOLVER_SELECTMASK;
1593   switch (how & SOLVER_JOBMASK)
1594     {
1595     case SOLVER_INSTALL:
1596       set = how & SOLVER_SETMASK;
1597       if (!(set & SOLVER_NOAUTOSET))
1598         {
1599           /* automatically add set bits by analysing the job */
1600           if (select == SOLVER_SOLVABLE_NAME)
1601             set |= SOLVER_SETNAME;
1602           if (select == SOLVER_SOLVABLE)
1603             set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
1604           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
1605             {
1606               Reldep *rd = GETRELDEP(pool, what);
1607               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
1608                 {
1609                   if (pool->disttype != DISTTYPE_DEB)
1610                     {
1611                       const char *rel = strrchr(pool_id2str(pool, rd->evr), '-');
1612                       if (rel && pool->disttype == DISTTYPE_HAIKU && (rel[1] < '0' || rel[1] > '9'))
1613                         rel = 0;
1614                       set |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
1615                     }
1616                   else
1617                     set |= SOLVER_SETEVR;
1618                 }
1619               if (rd->flags <= 7 && ISRELDEP(rd->name))
1620                 rd = GETRELDEP(pool, rd->name);
1621               if (rd->flags == REL_ARCH)
1622                 set |= SOLVER_SETARCH;
1623             }
1624         }
1625       else
1626         set &= ~SOLVER_NOAUTOSET;
1627       if (!set)
1628         return;
1629       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
1630         {
1631           if (select == SOLVER_SOLVABLE)
1632             queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
1633           else
1634             {
1635               int qcnt = q->count;
1636               /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
1637                  they are not useful for SOLVER_INSTALL jobs anyway */
1638               FOR_JOB_SELECT(p, pp, select, what)
1639                 {
1640                   s = pool->solvables + p;
1641                   /* unify names */
1642                   for (i = qcnt; i < q->count; i += 2)
1643                     if (q->elements[i + 1] == s->name)
1644                       break;
1645                   if (i < q->count)
1646                     continue;
1647                   queue_push2(q, DISABLE_INFARCH, s->name);
1648                 }
1649             }
1650         }
1651       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
1652         {
1653           if (select == SOLVER_SOLVABLE)
1654             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
1655           else
1656             {
1657               int qcnt = q->count;
1658               FOR_JOB_SELECT(p, pp, select, what)
1659                 {
1660                   s = pool->solvables + p;
1661                   /* unify names */
1662                   for (i = qcnt; i < q->count; i += 2)
1663                     if (q->elements[i + 1] == s->name)
1664                       break;
1665                   if (i < q->count)
1666                     continue;
1667                   queue_push2(q, DISABLE_DUP, s->name);
1668                 }
1669             }
1670         }
1671       if (!installed || installed->end == installed->start)
1672         return;
1673       /* now the hard part: disable some update rules */
1674
1675       /* first check if we have multiversion or installed packages in the job */
1676       i = j = 0;
1677       FOR_JOB_SELECT(p, pp, select, what)
1678         {
1679           if (pool->solvables[p].repo == installed)
1680             j = p;
1681           else if (solv->multiversion.size && MAPTST(&solv->multiversion, p) && !solv->keepexplicitobsoletes)
1682             return;
1683           i++;
1684         }
1685       if (j)    /* have installed packages */
1686         {
1687           /* this is for dupmap_all jobs, it can go away if we create
1688            * duprules for them */
1689           if (i == 1 && (set & SOLVER_SETREPO) != 0)
1690             queue_push2(q, DISABLE_UPDATE, j);
1691           return;
1692         }
1693
1694       omap.size = 0;
1695       qstart = q->count;
1696       FOR_JOB_SELECT(p, pp, select, what)
1697         {
1698           intersect_obsoletes(solv, p, q, qstart, &omap);
1699           if (q->count == qstart)
1700             break;
1701         }
1702       if (omap.size)
1703         map_free(&omap);
1704
1705       if (qstart == q->count)
1706         return;         /* nothing to prune */
1707
1708       /* convert result to (DISABLE_UPDATE, p) pairs */
1709       i = q->count;
1710       for (j = qstart; j < i; j++)
1711         queue_push(q, q->elements[j]);
1712       for (j = qstart; j < q->count; j += 2)
1713         {
1714           q->elements[j] = DISABLE_UPDATE;
1715           q->elements[j + 1] = q->elements[i++];
1716         }
1717
1718       /* now that we know which installed packages are obsoleted check each of them */
1719       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
1720         return;         /* all is set, nothing to do */
1721
1722       for (i = j = qstart; i < q->count; i += 2)
1723         {
1724           Solvable *is = pool->solvables + q->elements[i + 1];
1725           FOR_JOB_SELECT(p, pp, select, what)
1726             {
1727               int illegal = 0;
1728               s = pool->solvables + p;
1729               if ((set & SOLVER_SETEVR) != 0)
1730                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
1731               if ((set & SOLVER_SETNAME) != 0)
1732                 illegal |= POLICY_ILLEGAL_NAMECHANGE;   /* ignore */
1733               if ((set & SOLVER_SETARCH) != 0)
1734                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
1735               if ((set & SOLVER_SETVENDOR) != 0)
1736                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
1737               illegal = policy_is_illegal(solv, is, s, illegal);
1738               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
1739                 {
1740                   /* it's ok if the EV is different */
1741                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
1742                     illegal = 0;
1743                 }
1744               if (illegal)
1745                 break;
1746             }
1747           if (!p)
1748             {   
1749               /* no package conflicts with the update rule */
1750               /* thus keep the DISABLE_UPDATE */
1751               q->elements[j + 1] = q->elements[i + 1];
1752               j += 2;
1753             }
1754         }
1755       queue_truncate(q, j);
1756       return;
1757
1758     case SOLVER_ERASE:
1759       if (!installed)
1760         break;
1761       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
1762         FOR_REPO_SOLVABLES(installed, p, s)
1763           queue_push2(q, DISABLE_UPDATE, p);
1764       FOR_JOB_SELECT(p, pp, select, what)
1765         if (pool->solvables[p].repo == installed)
1766           queue_push2(q, DISABLE_UPDATE, p);
1767       return;
1768     default:
1769       return;
1770     }
1771 }
1772
1773 /* disable all policy rules that are in conflict with our job list */
1774 void
1775 solver_disablepolicyrules(Solver *solv)
1776 {
1777   Queue *job = &solv->job;
1778   int i, j;
1779   Queue allq;
1780   Rule *r;
1781   Id lastjob = -1;
1782   Id allqbuf[128];
1783
1784   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1785
1786   for (i = solv->jobrules; i < solv->jobrules_end; i++)
1787     {
1788       r = solv->rules + i;
1789       if (r->d < 0)     /* disabled? */
1790         continue;
1791       j = solv->ruletojob.elements[i - solv->jobrules];
1792       if (j == lastjob)
1793         continue;
1794       lastjob = j;
1795       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1796     }
1797   if (solv->cleandepsmap.size)
1798     {
1799       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1800       for (i = solv->installed->start; i < solv->installed->end; i++)
1801         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1802           queue_push2(&allq, DISABLE_UPDATE, i);
1803     }
1804   MAPZERO(&solv->noupdate);
1805   for (i = 0; i < allq.count; i += 2)
1806     {
1807       Id type = allq.elements[i], arg = allq.elements[i + 1];
1808       switch(type)
1809         {
1810         case DISABLE_UPDATE:
1811           disableupdaterule(solv, arg);
1812           break;
1813         case DISABLE_INFARCH:
1814           disableinfarchrule(solv, arg);
1815           break;
1816         case DISABLE_DUP:
1817           disableduprule(solv, arg);
1818           break;
1819         default:
1820           break;
1821         }
1822     }
1823   queue_free(&allq);
1824 }
1825
1826 /* we just disabled job #jobidx, now reenable all policy rules that were
1827  * disabled because of this job */
1828 void
1829 solver_reenablepolicyrules(Solver *solv, int jobidx)
1830 {
1831   Queue *job = &solv->job;
1832   int i, j, k, ai;
1833   Queue q, allq;
1834   Rule *r;
1835   Id lastjob = -1;
1836   Id qbuf[32], allqbuf[32];
1837
1838   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1839   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
1840   if (!q.count)
1841     {
1842       queue_free(&q);
1843       return;
1844     }
1845   /* now remove everything from q that is disabled by other jobs */
1846
1847   /* first remove cleandeps packages, they count as DISABLE_UPDATE */
1848   if (solv->cleandepsmap.size)
1849     {
1850       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
1851       for (j = k = 0; j < q.count; j += 2)
1852         {
1853           if (q.elements[j] == DISABLE_UPDATE)
1854             {
1855               Id p = q.elements[j + 1];
1856               if (p >= solv->installed->start && p < solv->installed->end && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
1857                 continue;       /* remove element from q */
1858             }
1859           q.elements[k++] = q.elements[j];
1860           q.elements[k++] = q.elements[j + 1];
1861         }
1862       q.count = k;
1863       if (!q.count)
1864         {
1865           queue_free(&q);
1866           return;
1867         }
1868     }
1869
1870   /* now go through the disable list of all other jobs */
1871   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1872   for (i = solv->jobrules; i < solv->jobrules_end; i++)
1873     {
1874       r = solv->rules + i;
1875       if (r->d < 0)     /* disabled? */
1876         continue;
1877       j = solv->ruletojob.elements[i - solv->jobrules];
1878       if (j == lastjob)
1879         continue;
1880       lastjob = j;
1881       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1882       if (!allq.count)
1883         continue;
1884       /* remove all elements in allq from q */
1885       for (j = k = 0; j < q.count; j += 2)
1886         {
1887           Id type = q.elements[j], arg = q.elements[j + 1];
1888           for (ai = 0; ai < allq.count; ai += 2)
1889             if (allq.elements[ai] == type && allq.elements[ai + 1] == arg)
1890               break;
1891           if (ai < allq.count)
1892             continue;   /* found it in allq, remove element from q */
1893           q.elements[k++] = q.elements[j];
1894           q.elements[k++] = q.elements[j + 1];
1895         }
1896       q.count = k;
1897       if (!q.count)
1898         {
1899           queue_free(&q);
1900           queue_free(&allq);
1901           return;
1902         }
1903       queue_empty(&allq);
1904     }
1905   queue_free(&allq);
1906
1907   /* now re-enable anything that's left in q */
1908   for (j = 0; j < q.count; j += 2)
1909     {
1910       Id type = q.elements[j], arg = q.elements[j + 1];
1911       switch(type)
1912         {
1913         case DISABLE_UPDATE:
1914           reenableupdaterule(solv, arg);
1915           break;
1916         case DISABLE_INFARCH:
1917           reenableinfarchrule(solv, arg);
1918           break;
1919         case DISABLE_DUP:
1920           reenableduprule(solv, arg);
1921           break;
1922         }
1923     }
1924   queue_free(&q);
1925 }
1926
1927 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
1928  * disabled because of this */
1929 void
1930 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
1931 {
1932   Queue *job = &solv->job;
1933   int i, j;
1934   Queue allq;
1935   Rule *r;
1936   Id lastjob = -1;
1937   Id allqbuf[128];
1938
1939   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
1940   for (i = solv->jobrules; i < solv->jobrules_end; i++)
1941     {
1942       r = solv->rules + i;
1943       if (r->d < 0)     /* disabled? */
1944         continue;
1945       j = solv->ruletojob.elements[i - solv->jobrules];
1946       if (j == lastjob)
1947         continue;
1948       lastjob = j;
1949       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
1950     }
1951   for (i = 0; i < allq.count; i += 2)
1952     if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
1953       break;
1954   if (i == allq.count)
1955     reenableupdaterule(solv, pkg);
1956   queue_free(&allq);
1957 }
1958
1959
1960 /***********************************************************************
1961  ***
1962  ***  Rule info part, tell the user what the rule is about.
1963  ***
1964  ***/
1965
1966 static void
1967 addrpmruleinfo(Solver *solv, Id p, Id d, int type, Id dep)
1968 {
1969   Pool *pool = solv->pool;
1970   Rule *r;
1971   Id w2, op, od, ow2;
1972
1973   /* check if this creates the rule we're searching for */
1974   r = solv->rules + solv->ruleinfoq->elements[0];
1975   op = r->p;
1976   od = r->d < 0 ? -r->d - 1 : r->d;
1977   ow2 = 0;
1978
1979   /* normalize */
1980   w2 = d > 0 ? 0 : d;
1981   if (p < 0 && d > 0 && (!pool->whatprovidesdata[d] || !pool->whatprovidesdata[d + 1]))
1982     {
1983       w2 = pool->whatprovidesdata[d];
1984       d = 0;
1985
1986     }
1987   if (p > 0 && d < 0)           /* this hack is used for buddy deps */
1988     {
1989       w2 = p;
1990       p = d;
1991     }
1992
1993   if (d > 0)
1994     {
1995       if (p != op && !od)
1996         return;
1997       if (d != od)
1998         {
1999           Id *dp = pool->whatprovidesdata + d;
2000           Id *odp = pool->whatprovidesdata + od;
2001           while (*dp)
2002             if (*dp++ != *odp++)
2003               return;
2004           if (*odp)
2005             return;
2006         }
2007       w2 = 0;
2008       /* handle multiversion conflict rules */
2009       if (p < 0 && pool->whatprovidesdata[d] < 0)
2010         {
2011           w2 = pool->whatprovidesdata[d];
2012           /* XXX: free memory */
2013         }
2014     }
2015   else
2016     {
2017       if (od)
2018         return;
2019       ow2 = r->w2;
2020       if (p > w2)
2021         {
2022           if (w2 != op || p != ow2)
2023             return;
2024         }
2025       else
2026         {
2027           if (p != op || w2 != ow2)
2028             return;
2029         }
2030     }
2031   /* yep, rule matches. record info */
2032   queue_push(solv->ruleinfoq, type);
2033   if (type == SOLVER_RULE_RPM_SAME_NAME)
2034     {
2035       /* we normalize same name order */
2036       queue_push(solv->ruleinfoq, op < 0 ? -op : 0);
2037       queue_push(solv->ruleinfoq, ow2 < 0 ? -ow2 : 0);
2038     }
2039   else
2040     {
2041       queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2042       queue_push(solv->ruleinfoq, w2 < 0 ? -w2 : 0);
2043     }
2044   queue_push(solv->ruleinfoq, dep);
2045 }
2046
2047 static int
2048 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2049 {
2050   const Id *a = ap, *b = bp;
2051   int r;
2052
2053   r = a[0] - b[0];
2054   if (r)
2055     return r;
2056   r = a[1] - b[1];
2057   if (r)
2058     return r;
2059   r = a[2] - b[2];
2060   if (r)
2061     return r;
2062   r = a[3] - b[3];
2063   if (r)
2064     return r;
2065   return 0;
2066 }
2067
2068 int
2069 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2070 {
2071   Pool *pool = solv->pool;
2072   Rule *r = solv->rules + rid;
2073   int i, j;
2074
2075   queue_empty(rq);
2076   if (rid <= 0 || rid >= solv->rpmrules_end)
2077     {
2078       Id type, from, to, dep;
2079       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2080       queue_push(rq, type);
2081       queue_push(rq, from);
2082       queue_push(rq, to);
2083       queue_push(rq, dep);
2084       return 1;
2085     }
2086   if (r->p >= 0)
2087     return 0;
2088   queue_push(rq, rid);
2089   solv->ruleinfoq = rq;
2090   solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2091   /* also try reverse direction for conflicts */
2092   if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2093     solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2094   solv->ruleinfoq = 0;
2095   queue_shift(rq);
2096   /* now sort & unify em */
2097   if (!rq->count)
2098     return 0;
2099   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2100   /* throw out identical entries */
2101   for (i = j = 0; i < rq->count; i += 4)
2102     {
2103       if (j)
2104         {
2105           if (rq->elements[i] == rq->elements[j - 4] && 
2106               rq->elements[i + 1] == rq->elements[j - 3] &&
2107               rq->elements[i + 2] == rq->elements[j - 2] &&
2108               rq->elements[i + 3] == rq->elements[j - 1])
2109             continue;
2110         }
2111       rq->elements[j++] = rq->elements[i];
2112       rq->elements[j++] = rq->elements[i + 1];
2113       rq->elements[j++] = rq->elements[i + 2];
2114       rq->elements[j++] = rq->elements[i + 3];
2115     }
2116   rq->count = j;
2117   return j / 4;
2118 }
2119
2120 SolverRuleinfo
2121 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2122 {
2123   Pool *pool = solv->pool;
2124   Rule *r = solv->rules + rid;
2125   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2126
2127   if (fromp)
2128     *fromp = 0;
2129   if (top)
2130     *top = 0;
2131   if (depp)
2132     *depp = 0;
2133   if (rid > 0 && rid < solv->rpmrules_end)
2134     {
2135       Queue rq;
2136       int i;
2137
2138       if (r->p >= 0)
2139         return SOLVER_RULE_RPM;
2140       if (fromp)
2141         *fromp = -r->p;
2142       queue_init(&rq);
2143       queue_push(&rq, rid);
2144       solv->ruleinfoq = &rq;
2145       solver_addrpmrulesforsolvable(solv, pool->solvables - r->p, 0);
2146       /* also try reverse direction for conflicts */
2147       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2148         solver_addrpmrulesforsolvable(solv, pool->solvables - r->w2, 0);
2149       solv->ruleinfoq = 0;
2150       type = SOLVER_RULE_RPM;
2151       for (i = 1; i < rq.count; i += 4)
2152         {
2153           Id qt, qo, qp, qd;
2154           qt = rq.elements[i];
2155           qp = rq.elements[i + 1];
2156           qo = rq.elements[i + 2];
2157           qd = rq.elements[i + 3];
2158           if (type == SOLVER_RULE_RPM || type > qt)
2159             {
2160               type = qt;
2161               if (fromp)
2162                 *fromp = qp;
2163               if (top)
2164                 *top = qo;
2165               if (depp)
2166                 *depp = qd;
2167             }
2168         }
2169       queue_free(&rq);
2170       return type;
2171     }
2172   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2173     {
2174       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2175       if (fromp)
2176         *fromp = jidx;
2177       if (top)
2178         *top = solv->job.elements[jidx];
2179       if (depp)
2180         *depp = solv->job.elements[jidx + 1];
2181       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2182         {
2183           if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2184             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2185           if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2186             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2187           if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2188             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2189           if ((solv->job.elements[jidx] & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2190             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2191         }
2192       return SOLVER_RULE_JOB;
2193     }
2194   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2195     {
2196       if (fromp)
2197         *fromp = solv->installed->start + (rid - solv->updaterules);
2198       return SOLVER_RULE_UPDATE;
2199     }
2200   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2201     {
2202       if (fromp)
2203         *fromp = solv->installed->start + (rid - solv->featurerules);
2204       return SOLVER_RULE_FEATURE;
2205     }
2206   if (rid >= solv->duprules && rid < solv->duprules_end)
2207     {
2208       if (fromp)
2209         *fromp = -r->p;
2210       if (depp)
2211         *depp = pool->solvables[-r->p].name;
2212       return SOLVER_RULE_DISTUPGRADE;
2213     }
2214   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2215     {
2216       if (fromp)
2217         *fromp = -r->p;
2218       if (depp)
2219         *depp = pool->solvables[-r->p].name;
2220       return SOLVER_RULE_INFARCH;
2221     }
2222   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2223     {
2224       return SOLVER_RULE_BEST;
2225     }
2226   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2227     {
2228       return SOLVER_RULE_CHOICE;
2229     }
2230   if (rid >= solv->learntrules)
2231     {
2232       return SOLVER_RULE_LEARNT;
2233     }
2234   return SOLVER_RULE_UNKNOWN;
2235 }
2236
2237 SolverRuleinfo
2238 solver_ruleclass(Solver *solv, Id rid)
2239 {
2240   if (rid <= 0)
2241     return SOLVER_RULE_UNKNOWN;
2242   if (rid > 0 && rid < solv->rpmrules_end)
2243     return SOLVER_RULE_RPM;
2244   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2245     return SOLVER_RULE_JOB;
2246   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2247     return SOLVER_RULE_UPDATE;
2248   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2249     return SOLVER_RULE_FEATURE;
2250   if (rid >= solv->duprules && rid < solv->duprules_end)
2251     return SOLVER_RULE_DISTUPGRADE;
2252   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2253     return SOLVER_RULE_INFARCH;
2254   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2255     return SOLVER_RULE_BEST;
2256   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2257     return SOLVER_RULE_CHOICE;
2258   if (rid >= solv->learntrules)
2259     return SOLVER_RULE_LEARNT;
2260   return SOLVER_RULE_UNKNOWN;
2261 }
2262
2263 void
2264 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2265 {
2266   Pool *pool = solv->pool;
2267   Id p, pp;
2268   Rule *r;
2269
2270   queue_empty(q);
2271   r = solv->rules + rid;
2272   FOR_RULELITERALS(p, pp, r)
2273     if (p != -SYSTEMSOLVABLE)
2274       queue_push(q, p);
2275   if (!q->count)
2276     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
2277 }
2278
2279 int
2280 solver_rule2jobidx(Solver *solv, Id rid)
2281 {
2282   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2283     return 0;
2284   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2285 }
2286
2287 Id
2288 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2289 {
2290   int idx;
2291   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2292     {
2293       if (whatp)
2294         *whatp = 0;
2295       return 0;
2296     }
2297   idx = solv->ruletojob.elements[rid - solv->jobrules];
2298   if (whatp)
2299     *whatp = solv->job.elements[idx + 1];
2300   return solv->job.elements[idx];
2301 }
2302
2303 /* check if the newest versions of pi still provides the dependency we're looking for */
2304 static int
2305 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m)
2306 {
2307   Pool *pool = solv->pool;
2308   Rule *ur;
2309   Queue q;
2310   Id p, pp, qbuf[32];
2311   int i;
2312
2313   ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
2314   if (!ur->p)
2315     ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
2316   if (!ur->p)
2317     return 0;
2318   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2319   FOR_RULELITERALS(p, pp, ur)
2320     if (p > 0)
2321       queue_push(&q, p);
2322   if (q.count > 1)
2323     policy_filter_unwanted(solv, &q, POLICY_MODE_CHOOSE);
2324   for (i = 0; i < q.count; i++)
2325     if (MAPTST(m, q.elements[i]))
2326       break;
2327   /* 1: none of the newest versions provide it */
2328   i = i == q.count ? 1 : 0;
2329   queue_free(&q);
2330   return i;
2331 }
2332
2333 static inline void
2334 queue_removeelement(Queue *q, Id el)
2335 {
2336   int i, j;
2337   for (i = 0; i < q->count; i++)
2338     if (q->elements[i] == el)
2339       break;
2340   if (i < q->count)
2341     {
2342       for (j = i++; i < q->count; i++)
2343         if (q->elements[i] != el)
2344           q->elements[j++] = q->elements[i];
2345       queue_truncate(q, j);
2346     }
2347 }
2348
2349 void
2350 solver_addchoicerules(Solver *solv)
2351 {
2352   Pool *pool = solv->pool;
2353   Map m, mneg;
2354   Rule *r;
2355   Queue q, qi;
2356   int i, j, rid, havechoice;
2357   Id p, d, pp;
2358   Id p2, pp2;
2359   Solvable *s, *s2;
2360   Id lastaddedp, lastaddedd;
2361   int lastaddedcnt;
2362   unsigned int now;
2363
2364   solv->choicerules = solv->nrules;
2365   if (!pool->installed)
2366     {
2367       solv->choicerules_end = solv->nrules;
2368       return;
2369     }
2370   now = solv_timems(0);
2371   solv->choicerules_ref = solv_calloc(solv->rpmrules_end, sizeof(Id));
2372   queue_init(&q);
2373   queue_init(&qi);
2374   map_init(&m, pool->nsolvables);
2375   map_init(&mneg, pool->nsolvables);
2376   /* set up negative assertion map from infarch and dup rules */
2377   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2378     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2379       MAPSET(&mneg, -r->p);
2380   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2381     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2382       MAPSET(&mneg, -r->p);
2383   lastaddedp = 0;
2384   lastaddedd = 0;
2385   lastaddedcnt = 0;
2386   for (rid = 1; rid < solv->rpmrules_end ; rid++)
2387     {
2388       r = solv->rules + rid;
2389       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
2390         continue;       /* only look at requires rules */
2391       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2392       queue_empty(&q);
2393       queue_empty(&qi);
2394       havechoice = 0;
2395       FOR_RULELITERALS(p, pp, r)
2396         {
2397           if (p < 0)
2398             continue;
2399           s = pool->solvables + p;
2400           if (!s->repo)
2401             continue;
2402           if (s->repo == pool->installed)
2403             {
2404               queue_push(&q, p);
2405               continue;
2406             }
2407           /* check if this package is "blocked" by a installed package */
2408           s2 = 0;
2409           FOR_PROVIDES(p2, pp2, s->name)
2410             {
2411               s2 = pool->solvables + p2;
2412               if (s2->repo != pool->installed)
2413                 continue;
2414               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2415                 continue;
2416               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2417                 continue;
2418               break;
2419             }
2420           if (p2)
2421             {
2422               /* found installed package p2 that we can update to p */
2423               if (MAPTST(&mneg, p))
2424                 continue;
2425               if (policy_is_illegal(solv, s2, s, 0))
2426                 continue;
2427 #if 0
2428               if (solver_choicerulecheck(solv, p2, r, &m))
2429                 continue;
2430               queue_push(&qi, p2);
2431 #else
2432               queue_push2(&qi, p2, p);
2433 #endif
2434               queue_push(&q, p);
2435               continue;
2436             }
2437           if (s->obsoletes)
2438             {
2439               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2440               s2 = 0;
2441               while ((obs = *obsp++) != 0)
2442                 {
2443                   FOR_PROVIDES(p2, pp2, obs)
2444                     {
2445                       s2 = pool->solvables + p2;
2446                       if (s2->repo != pool->installed)
2447                         continue;
2448                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2449                         continue;
2450                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2451                         continue;
2452                       break;
2453                     }
2454                   if (p2)
2455                     break;
2456                 }
2457               if (obs)
2458                 {
2459                   /* found installed package p2 that we can update to p */
2460                   if (MAPTST(&mneg, p))
2461                     continue;
2462                   if (policy_is_illegal(solv, s2, s, 0))
2463                     continue;
2464 #if 0
2465                   if (solver_choicerulecheck(solv, p2, r, &m))
2466                     continue;
2467                   queue_push(&qi, p2);
2468 #else
2469                   queue_push2(&qi, p2, p);
2470 #endif
2471                   queue_push(&q, p);
2472                   continue;
2473                 }
2474             }
2475           /* package p is independent of the installed ones */
2476           havechoice = 1;
2477         }
2478       if (!havechoice || !q.count || !qi.count)
2479         continue;       /* no choice */
2480
2481       FOR_RULELITERALS(p, pp, r)
2482         if (p > 0)
2483           MAPSET(&m, p);
2484
2485       /* do extra checking */
2486       for (i = j = 0; i < qi.count; i += 2)
2487         {
2488           p2 = qi.elements[i];
2489           if (!p2)
2490             continue;
2491           if (solver_choicerulecheck(solv, p2, r, &m))
2492             {
2493               /* oops, remove element p from q */
2494               queue_removeelement(&q, qi.elements[i + 1]);
2495               continue;
2496             }
2497           qi.elements[j++] = p2;
2498         }
2499       queue_truncate(&qi, j);
2500       if (!q.count || !qi.count)
2501         {
2502           FOR_RULELITERALS(p, pp, r)
2503             if (p > 0)
2504               MAPCLR(&m, p);
2505           continue;
2506         }
2507
2508
2509       /* now check the update rules of the installed package.
2510        * if all packages of the update rules are contained in
2511        * the dependency rules, there's no need to set up the choice rule */
2512       for (i = 0; i < qi.count; i++)
2513         {
2514           Rule *ur;
2515           if (!qi.elements[i])
2516             continue;
2517           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
2518           if (!ur->p)
2519             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
2520           if (!ur->p)
2521             continue;
2522           FOR_RULELITERALS(p, pp, ur)
2523             if (!MAPTST(&m, p))
2524               break;
2525           if (p)
2526             break;
2527           for (j = i + 1; j < qi.count; j++)
2528             if (qi.elements[i] == qi.elements[j])
2529               qi.elements[j] = 0;
2530         }
2531       /* empty map again */
2532       FOR_RULELITERALS(p, pp, r)
2533         if (p > 0)
2534           MAPCLR(&m, p);
2535       if (i == qi.count)
2536         {
2537 #if 0
2538           printf("skipping choice ");
2539           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2540 #endif
2541           continue;
2542         }
2543
2544       /* don't add identical rules */
2545       if (lastaddedp == r->p && lastaddedcnt == q.count)
2546         {
2547           for (i = 0; i < q.count; i++)
2548             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
2549               break;
2550           if (i == q.count)
2551             continue;   /* already added that one */
2552         }
2553       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
2554
2555       lastaddedp = r->p;
2556       lastaddedd = d;
2557       lastaddedcnt = q.count;
2558
2559       solver_addrule(solv, r->p, d);
2560       queue_push(&solv->weakruleq, solv->nrules - 1);
2561       solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
2562 #if 0
2563       printf("OLD ");
2564       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
2565       printf("WEAK CHOICE ");
2566       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
2567 #endif
2568     }
2569   queue_free(&q);
2570   queue_free(&qi);
2571   map_free(&m);
2572   map_free(&mneg);
2573   solv->choicerules_end = solv->nrules;
2574   /* shrink choicerules_ref */
2575   solv->choicerules_ref = solv_realloc2(solv->choicerules_ref, solv->choicerules_end - solv->choicerules, sizeof(Id));
2576   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
2577 }
2578
2579 /* called when a choice rule is disabled by analyze_unsolvable. We also
2580  * have to disable all other choice rules so that the best packages get
2581  * picked */
2582 void
2583 solver_disablechoicerules(Solver *solv, Rule *r)
2584 {
2585   Id rid, p, pp;
2586   Pool *pool = solv->pool;
2587   Map m;
2588   Rule *or;
2589
2590   or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2591   map_init(&m, pool->nsolvables);
2592   FOR_RULELITERALS(p, pp, or)
2593     if (p > 0)
2594       MAPSET(&m, p);
2595   FOR_RULELITERALS(p, pp, r)
2596     if (p > 0)
2597       MAPCLR(&m, p);
2598   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
2599     {
2600       r = solv->rules + rid;
2601       if (r->d < 0)
2602         continue;
2603       or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
2604       FOR_RULELITERALS(p, pp, or)
2605         if (p > 0 && MAPTST(&m, p))
2606           break;
2607       if (p)
2608         solver_disablerule(solv, r);
2609     }
2610 }
2611
2612 static void
2613 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
2614 {
2615   int i, j;
2616   Id p, *cp2; 
2617   for (i = j = 0; i < q->count; i++)
2618     {  
2619       p = q->elements[i];
2620       for (cp2 = cp; *cp2; cp2++)
2621         if (*cp2 == p)
2622           {
2623             q->elements[j++] = p;
2624             break;
2625           }
2626     }
2627   queue_truncate(q, j);
2628 }
2629
2630 static void
2631 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
2632 {
2633   int i, j;
2634   for (i = j = 0; i < q->count; i++)
2635     {
2636       Id p = q->elements[i];
2637       if (MAPTST(&solv->dupmap, p))
2638         q->elements[j++] = p;
2639     }
2640   queue_truncate(q, j);
2641 }
2642
2643 void
2644 solver_addbestrules(Solver *solv, int havebestinstalljobs)
2645 {
2646   Pool *pool = solv->pool;
2647   Id p;
2648   Solvable *s;
2649   Repo *installed = solv->installed;
2650   Queue q, q2;
2651   Rule *r;
2652   Queue r2pkg;
2653   int i, oldcnt;
2654
2655   solv->bestrules = solv->nrules;
2656   if (!installed)
2657     {
2658       solv->bestrules_end = solv->nrules;
2659       return;
2660     }
2661   queue_init(&q);
2662   queue_init(&q2);
2663   queue_init(&r2pkg);
2664
2665   if (havebestinstalljobs)
2666     {
2667       for (i = 0; i < solv->job.count; i += 2)
2668         {
2669           if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
2670             {
2671               int j;
2672               Id p2, pp2;
2673               for (j = 0; j < solv->ruletojob.count; j++)
2674                 if (solv->ruletojob.elements[j] == i)
2675                   break;
2676               if (j == solv->ruletojob.count)
2677                 continue;
2678               r = solv->rules + solv->jobrules + j;
2679               queue_empty(&q);
2680               FOR_RULELITERALS(p2, pp2, r)
2681                 if (p2 > 0)
2682                   queue_push(&q, p2);
2683               if (!q.count)
2684                 continue;       /* orphaned */
2685               /* select best packages, just look at prio and version */
2686               oldcnt = q.count;
2687               policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2688               if (q.count == oldcnt)
2689                 continue;       /* nothing filtered */
2690               p2 = queue_shift(&q);
2691               solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2692               queue_push(&r2pkg, -(solv->jobrules + j));
2693             }
2694         }
2695     }
2696
2697   if (solv->bestupdatemap_all || solv->bestupdatemap.size)
2698     {
2699       FOR_REPO_SOLVABLES(installed, p, s)
2700         {
2701           Id d, p2, pp2;
2702           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
2703             continue;
2704           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
2705             continue;
2706           queue_empty(&q);
2707           if (solv->bestobeypolicy)
2708             r = solv->rules + solv->updaterules + (p - installed->start);
2709           else
2710             {
2711               r = solv->rules + solv->featurerules + (p - installed->start);
2712               if (!r->p)        /* identical to update rule? */
2713                 r = solv->rules + solv->updaterules + (p - installed->start);
2714             }
2715           if (solv->multiversionupdaters && (d = solv->multiversionupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
2716             {
2717               /* need to check multiversionupdaters */
2718               if (r->p == p)    /* be careful with the dup case */
2719                 queue_push(&q, p);
2720               while ((p2 = pool->whatprovidesdata[d++]) != 0)
2721                 queue_push(&q, p2);
2722             }
2723           else
2724             {
2725               FOR_RULELITERALS(p2, pp2, r)
2726                 if (p2 > 0)
2727                   queue_push(&q, p2);
2728             }
2729           if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2730             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
2731           if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2732             prune_to_dup_packages(solv, p, &q);
2733           /* select best packages, just look at prio and version */
2734           policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
2735           if (!q.count)
2736             continue;   /* orphaned */
2737           if (solv->bestobeypolicy)
2738             {
2739               /* also filter the best of the feature rule packages and add them */
2740               r = solv->rules + solv->featurerules + (p - installed->start);
2741               if (r->p)
2742                 {
2743                   int j;
2744                   queue_empty(&q2);
2745                   FOR_RULELITERALS(p2, pp2, r)
2746                     if (p2 > 0)
2747                       queue_push(&q2, p2);
2748                   if (solv->update_targets && solv->update_targets->elements[p - installed->start])
2749                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
2750                   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
2751                     prune_to_dup_packages(solv, p, &q);
2752                   policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
2753                   for (j = 0; j < q2.count; j++)
2754                     queue_pushunique(&q, q2.elements[j]);
2755                 }
2756             }
2757           p2 = queue_shift(&q);
2758           solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
2759           queue_push(&r2pkg, p);
2760         }
2761     }
2762   if (r2pkg.count)
2763     {
2764       solv->bestrules_pkg = solv_calloc(r2pkg.count, sizeof(Id));
2765       memcpy(solv->bestrules_pkg, r2pkg.elements, r2pkg.count * sizeof(Id));
2766     }
2767   solv->bestrules_end = solv->nrules;
2768   queue_free(&q);
2769   queue_free(&q2);
2770   queue_free(&r2pkg);
2771 }
2772
2773 #undef CLEANDEPSDEBUG
2774
2775 /*
2776  * This functions collects all packages that are looked at
2777  * when a dependency is checked. We need it to "pin" installed
2778  * packages when removing a supplemented package in createcleandepsmap.
2779  * Here's an not uncommon example:
2780  *   A contains "Supplements: packageand(B, C)"
2781  *   B contains "Requires: A"
2782  * Now if we remove C, the supplements is no longer true,
2783  * thus we also remove A. Without the dep_pkgcheck function, we
2784  * would now also remove B, but this is wrong, as adding back
2785  * C doesn't make the supplements true again. Thus we "pin" B
2786  * when we remove A.
2787  * There's probably a better way to do this, but I haven't come
2788  * up with it yet ;)
2789  */
2790 static inline void
2791 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
2792 {
2793   Pool *pool = solv->pool;
2794   Id p, pp;
2795
2796   if (ISRELDEP(dep))
2797     {
2798       Reldep *rd = GETRELDEP(pool, dep);
2799       if (rd->flags >= 8)
2800         {
2801           if (rd->flags == REL_AND)
2802             {
2803               dep_pkgcheck(solv, rd->name, m, q);
2804               dep_pkgcheck(solv, rd->evr, m, q);
2805               return;
2806             }
2807           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2808             return;
2809           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2810             return;
2811         }
2812     }
2813   FOR_PROVIDES(p, pp, dep)
2814     if (!m || MAPTST(m, p))
2815       queue_push(q, p);
2816 }
2817
2818 static int
2819 check_xsupp(Solver *solv, Queue *depq, Id dep)
2820 {
2821   Pool *pool = solv->pool;
2822   Id p, pp;
2823
2824   if (ISRELDEP(dep))
2825     {
2826       Reldep *rd = GETRELDEP(pool, dep);
2827       if (rd->flags >= 8)
2828         {
2829           if (rd->flags == REL_AND)
2830             {
2831               if (!check_xsupp(solv, depq, rd->name))
2832                 return 0;
2833               return check_xsupp(solv, depq, rd->evr);
2834             }
2835           if (rd->flags == REL_OR)
2836             {
2837               if (check_xsupp(solv, depq, rd->name))
2838                 return 1;
2839               return check_xsupp(solv, depq, rd->evr);
2840             }
2841           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
2842             return solver_splitprovides(solv, rd->evr);
2843           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
2844             return solver_dep_installed(solv, rd->evr);
2845         }
2846       if (depq && rd->flags == REL_NAMESPACE)
2847         {
2848           int i;
2849           for (i = 0; i < depq->count; i++)
2850             if (depq->elements[i] == dep || depq->elements[i] == rd->name)
2851              return 1;
2852         }
2853     }
2854   FOR_PROVIDES(p, pp, dep)
2855     if (p == SYSTEMSOLVABLE || pool->solvables[p].repo == solv->installed)
2856       return 1;
2857   return 0;
2858 }
2859
2860 static inline int
2861 queue_contains(Queue *q, Id id)
2862 {
2863   int i;
2864   for (i = 0; i < q->count; i++)
2865     if (q->elements[i] == id)
2866       return 1;
2867   return 0;
2868 }
2869
2870
2871 /*
2872  * Find all installed packages that are no longer
2873  * needed regarding the current solver job.
2874  *
2875  * The algorithm is:
2876  * - remove pass: remove all packages that could have
2877  *   been dragged in by the obsoleted packages.
2878  *   i.e. if package A is obsolete and contains "Requires: B",
2879  *   also remove B, as installing A will have pulled in B.
2880  *   after this pass, we have a set of still installed packages
2881  *   with broken dependencies.
2882  * - add back pass:
2883  *   now add back all packages that the still installed packages
2884  *   require.
2885  *
2886  * The cleandeps packages are the packages removed in the first
2887  * pass and not added back in the second pass.
2888  *
2889  * If we search for unneeded packages (unneeded is true), we
2890  * simply remove all packages except the userinstalled ones in
2891  * the first pass.
2892  */
2893 static void
2894 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
2895 {
2896   Pool *pool = solv->pool;
2897   Repo *installed = solv->installed;
2898   Queue *job = &solv->job;
2899   Map userinstalled;
2900   Map im;
2901   Map installedm;
2902   Rule *r;
2903   Id rid, how, what, select;
2904   Id p, pp, ip, jp;
2905   Id req, *reqp, sup, *supp;
2906   Solvable *s;
2907   Queue iq, iqcopy, xsuppq;
2908   int i;
2909
2910   map_empty(cleandepsmap);
2911   if (!installed || installed->end == installed->start)
2912     return;
2913   map_init(&userinstalled, installed->end - installed->start);
2914   map_init(&im, pool->nsolvables);
2915   map_init(&installedm, pool->nsolvables);
2916   queue_init(&iq);
2917   queue_init(&xsuppq);
2918
2919   for (i = 0; i < job->count; i += 2)
2920     {
2921       how = job->elements[i];
2922       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
2923         {
2924           what = job->elements[i + 1];
2925           select = how & SOLVER_SELECTMASK;
2926           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2927             FOR_REPO_SOLVABLES(installed, p, s)
2928               MAPSET(&userinstalled, p - installed->start);
2929           FOR_JOB_SELECT(p, pp, select, what)
2930             if (pool->solvables[p].repo == installed)
2931               MAPSET(&userinstalled, p - installed->start);
2932         }
2933       if ((how & (SOLVER_JOBMASK | SOLVER_SELECTMASK)) == (SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES))
2934         {
2935           what = job->elements[i + 1];
2936           if (ISRELDEP(what))
2937             {
2938               Reldep *rd = GETRELDEP(pool, what);
2939               if (rd->flags != REL_NAMESPACE)
2940                 continue;
2941               if (rd->evr == 0)
2942                 {
2943                   queue_pushunique(&iq, rd->name);
2944                   continue;
2945                 }
2946               FOR_PROVIDES(p, pp, what)
2947                 if (p)
2948                   break;
2949               if (p)
2950                 continue;
2951               queue_pushunique(&iq, what);
2952             }
2953         }
2954     }
2955
2956   /* have special namespace cleandeps erases */
2957   if (iq.count)
2958     {
2959       for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2960         {
2961           s = pool->solvables + ip;
2962           if (s->repo != installed)
2963             continue;
2964           if (!s->supplements)
2965             continue;
2966           supp = s->repo->idarraydata + s->supplements;
2967           while ((sup = *supp++) != 0)
2968             if (check_xsupp(solv, &iq, sup) && !check_xsupp(solv, 0, sup))
2969               {
2970 #ifdef CLEANDEPSDEBUG
2971                 printf("xsupp %s from %s\n", pool_dep2str(pool, sup), pool_solvid2str(pool, ip));
2972 #endif
2973                 queue_pushunique(&xsuppq, sup);
2974               }
2975         }
2976       queue_empty(&iq);
2977     }
2978
2979   /* also add visible patterns to userinstalled for openSUSE */
2980   if (1)
2981     {
2982       Dataiterator di;
2983       dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
2984       while (dataiterator_step(&di))
2985         {
2986           Id *dp;
2987           if (di.solvid <= 0)
2988             continue;
2989           s = pool->solvables + di.solvid;
2990           if (!s->repo || !s->requires)
2991             continue;
2992           if (s->repo != installed && !pool_installable(pool, s))
2993             continue;
2994           if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
2995             continue;
2996           dp = s->repo->idarraydata + s->requires;
2997           for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
2998             FOR_PROVIDES(p, pp, *dp)
2999               if (pool->solvables[p].repo == installed)
3000                 {
3001                   if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
3002                     continue;
3003                   MAPSET(&userinstalled, p - installed->start);
3004                 }
3005         }
3006       dataiterator_free(&di);
3007     }
3008   if (1)
3009     {
3010       /* all products and their buddies are userinstalled */
3011       for (p = installed->start; p < installed->end; p++)
3012         {
3013           Solvable *s = pool->solvables + p;
3014           if (s->repo != installed)
3015             continue;
3016           if (!strncmp("product:", pool_id2str(pool, s->name), 8))
3017             {
3018               MAPSET(&userinstalled, p - installed->start);
3019               if (pool->nscallback)
3020                 {
3021                   Id buddy = pool->nscallback(pool, pool->nscallbackdata, NAMESPACE_PRODUCTBUDDY, p);
3022                   if (buddy >= installed->start && buddy < installed->end && pool->solvables[buddy].repo == installed)
3023                     MAPSET(&userinstalled, buddy - installed->start);
3024                 }
3025             }
3026         }
3027     }
3028   
3029   /* add all positive elements (e.g. locks) to "userinstalled" */
3030   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3031     {
3032       r = solv->rules + rid;
3033       if (r->d < 0)
3034         continue;
3035       i = solv->ruletojob.elements[rid - solv->jobrules];
3036       if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3037         continue;
3038       FOR_RULELITERALS(p, jp, r)
3039         if (p > 0 && pool->solvables[p].repo == installed)
3040           MAPSET(&userinstalled, p - installed->start);
3041     }
3042
3043   /* add all cleandeps candidates to iq */
3044   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3045     {
3046       r = solv->rules + rid;
3047       if (r->d < 0)                             /* disabled? */
3048         continue;
3049       if (r->d == 0 && r->p < 0 && r->w2 == 0)  /* negative assertion (erase job)? */
3050         {
3051           p = -r->p;
3052           if (pool->solvables[p].repo != installed)
3053             continue;
3054           MAPCLR(&userinstalled, p - installed->start);
3055           if (unneeded)
3056             continue;
3057           i = solv->ruletojob.elements[rid - solv->jobrules];
3058           how = job->elements[i];
3059           if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
3060             queue_push(&iq, p);
3061         }
3062       else if (r->p > 0)                        /* install job */
3063         {
3064           if (unneeded)
3065             continue;
3066           i = solv->ruletojob.elements[rid - solv->jobrules];
3067           if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3068             {
3069               /* check if the literals all obsolete some installed package */
3070               Map om;
3071               int iqstart;
3072
3073               /* just one installed literal */
3074               if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
3075                 continue;
3076               /* multiversion is bad */
3077               if (solv->multiversion.size && !solv->keepexplicitobsoletes)
3078                 {
3079                   FOR_RULELITERALS(p, jp, r)
3080                     if (MAPTST(&solv->multiversion, p))
3081                       break;
3082                   if (p)
3083                     continue;
3084                 }
3085
3086               om.size = 0;
3087               iqstart = iq.count;
3088               FOR_RULELITERALS(p, jp, r)
3089                 {
3090                   if (p < 0)
3091                     {
3092                       queue_truncate(&iq, iqstart);     /* abort */
3093                       break;
3094                     }
3095                   if (pool->solvables[p].repo == installed)
3096                     {
3097                       if (iq.count == iqstart)
3098                         queue_push(&iq, p);
3099                       else
3100                         {
3101                           for (i = iqstart; i < iq.count; i++)
3102                             if (iq.elements[i] == p)
3103                               break;
3104                           queue_truncate(&iq, iqstart);
3105                           if (i < iq.count)
3106                             queue_push(&iq, p);
3107                         }
3108                     }
3109                   else
3110                     intersect_obsoletes(solv, p, &iq, iqstart, &om);
3111                   if (iq.count == iqstart)
3112                     break;
3113                 }
3114               if (om.size)
3115                 map_free(&om);
3116             }
3117         }
3118     }
3119   queue_init_clone(&iqcopy, &iq);
3120
3121   if (!unneeded)
3122     {
3123       if (solv->cleandeps_updatepkgs)
3124         for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3125           queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
3126     }
3127
3128   if (unneeded)
3129     queue_empty(&iq);   /* just in case... */
3130
3131   /* clear userinstalled bit for the packages we really want to delete/update */
3132   for (i = 0; i < iq.count; i++)
3133     {
3134       p = iq.elements[i];
3135       if (pool->solvables[p].repo != installed)
3136         continue;
3137       MAPCLR(&userinstalled, p - installed->start);
3138     }
3139
3140   for (p = installed->start; p < installed->end; p++)
3141     {
3142       if (pool->solvables[p].repo != installed)
3143         continue;
3144       MAPSET(&installedm, p);
3145       if (unneeded && !MAPTST(&userinstalled, p - installed->start))
3146         continue;
3147       MAPSET(&im, p);
3148     }
3149   MAPSET(&installedm, SYSTEMSOLVABLE);
3150   MAPSET(&im, SYSTEMSOLVABLE);
3151
3152 #ifdef CLEANDEPSDEBUG
3153   printf("REMOVE PASS\n");
3154 #endif
3155
3156   for (;;)
3157     {
3158       if (!iq.count)
3159         {
3160           if (unneeded)
3161             break;
3162           /* supplements pass */
3163           for (ip = installed->start; ip < installed->end; ip++)
3164             {
3165               if (!MAPTST(&installedm, ip))
3166                 continue;
3167               s = pool->solvables + ip;
3168               if (!s->supplements)
3169                 continue;
3170               if (!MAPTST(&im, ip))
3171                 continue;
3172               if (MAPTST(&userinstalled, ip - installed->start))
3173                 continue;
3174               supp = s->repo->idarraydata + s->supplements;
3175               while ((sup = *supp++) != 0)
3176                 if (dep_possible(solv, sup, &im))
3177                   break;
3178               if (!sup)
3179                 {
3180                   supp = s->repo->idarraydata + s->supplements;
3181                   while ((sup = *supp++) != 0)
3182                     if (dep_possible(solv, sup, &installedm) || (xsuppq.count && queue_contains(&xsuppq, sup)))
3183                       {
3184                         /* no longer supplemented, also erase */
3185                         int iqcount = iq.count;
3186                         /* pin packages, see comment above dep_pkgcheck */
3187                         dep_pkgcheck(solv, sup, &im, &iq);
3188                         for (i = iqcount; i < iq.count; i++)
3189                           {
3190                             Id pqp = iq.elements[i];
3191                             if (pool->solvables[pqp].repo == installed)
3192                               MAPSET(&userinstalled, pqp - installed->start);
3193                           }
3194                         queue_truncate(&iq, iqcount);
3195 #ifdef CLEANDEPSDEBUG
3196                         printf("%s supplemented [%s]\n", pool_solvid2str(pool, ip), pool_dep2str(pool, sup));
3197 #endif
3198                         queue_push(&iq, ip);
3199                       }
3200                 }
3201             }
3202           if (!iq.count)
3203             break;      /* no supplementing package found, we're done */
3204         }
3205       ip = queue_shift(&iq);
3206       s = pool->solvables + ip;
3207       if (!MAPTST(&im, ip))
3208         continue;
3209       if (!MAPTST(&installedm, ip))
3210         continue;
3211       if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
3212         continue;
3213       MAPCLR(&im, ip);
3214 #ifdef CLEANDEPSDEBUG
3215       printf("removing %s\n", pool_solvable2str(pool, s));
3216 #endif
3217       if (s->requires)
3218         {
3219           reqp = s->repo->idarraydata + s->requires;
3220           while ((req = *reqp++) != 0)
3221             {
3222               if (req == SOLVABLE_PREREQMARKER)
3223                 continue;
3224 #if 0
3225               /* count number of installed packages that match */
3226               count = 0;
3227               FOR_PROVIDES(p, pp, req)
3228                 if (MAPTST(&installedm, p))
3229                   count++;
3230               if (count > 1)
3231                 continue;
3232 #endif
3233               FOR_PROVIDES(p, pp, req)
3234                 {
3235                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3236                     {
3237 #ifdef CLEANDEPSDEBUG
3238                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3239 #endif
3240                       queue_push(&iq, p);
3241                     }
3242                 }
3243             }
3244         }
3245       if (s->recommends)
3246         {
3247           reqp = s->repo->idarraydata + s->recommends;
3248           while ((req = *reqp++) != 0)
3249             {
3250 #if 0
3251               count = 0;
3252               FOR_PROVIDES(p, pp, req)
3253                 if (MAPTST(&installedm, p))
3254                   count++;
3255               if (count > 1)
3256                 continue;
3257 #endif
3258               FOR_PROVIDES(p, pp, req)
3259                 {
3260                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
3261                     {
3262 #ifdef CLEANDEPSDEBUG
3263                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3264 #endif
3265                       queue_push(&iq, p);
3266                     }
3267                 }
3268             }
3269         }
3270     }
3271
3272   /* turn userinstalled into remove set for pruning */
3273   map_empty(&userinstalled);
3274   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3275     {
3276       r = solv->rules + rid;
3277       if (r->p >= 0 || r->d != 0 || r->w2 != 0)
3278         continue;       /* disabled or not erase */
3279       p = -r->p;
3280       MAPCLR(&im, p);
3281       if (pool->solvables[p].repo == installed)
3282         MAPSET(&userinstalled, p - installed->start);
3283     }
3284   MAPSET(&im, SYSTEMSOLVABLE);  /* in case we cleared it above */
3285   for (p = installed->start; p < installed->end; p++)
3286     if (MAPTST(&im, p))
3287       queue_push(&iq, p);
3288   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3289     {
3290       r = solv->rules + rid;
3291       if (r->d < 0)
3292         continue;
3293       FOR_RULELITERALS(p, jp, r)
3294         if (p > 0)
3295           queue_push(&iq, p);
3296     }
3297   /* also put directly addressed packages on the install queue
3298    * so we can mark patterns as installed */
3299   for (i = 0; i < job->count; i += 2)
3300     {
3301       how = job->elements[i];
3302       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3303         {
3304           what = job->elements[i + 1];
3305           select = how & SOLVER_SELECTMASK;
3306           if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
3307             queue_push(&iq, what);
3308         }
3309     }
3310
3311 #ifdef CLEANDEPSDEBUG
3312   printf("ADDBACK PASS\n");
3313 #endif
3314   for (;;)
3315     {
3316       if (!iq.count)
3317         {
3318           /* supplements pass */
3319           for (ip = installed->start; ip < installed->end; ip++)
3320             {
3321               if (!MAPTST(&installedm, ip))
3322                 continue;
3323               if (MAPTST(&userinstalled, ip - installed->start))
3324                 continue;
3325               s = pool->solvables + ip;
3326               if (!s->supplements)
3327                 continue;
3328               if (MAPTST(&im, ip))
3329                 continue;
3330               supp = s->repo->idarraydata + s->supplements;
3331               while ((sup = *supp++) != 0)
3332                 if (dep_possible(solv, sup, &im))
3333                   break;
3334               if (sup)
3335                 {
3336 #ifdef CLEANDEPSDEBUG
3337                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
3338 #endif
3339                   MAPSET(&im, ip);
3340                   queue_push(&iq, ip);
3341                 }
3342             }
3343           if (!iq.count)
3344             break;
3345         }
3346       ip = queue_shift(&iq);
3347       s = pool->solvables + ip;
3348 #ifdef CLEANDEPSDEBUG
3349       printf("adding back %s\n", pool_solvable2str(pool, s));
3350 #endif
3351       if (s->requires)
3352         {
3353           reqp = s->repo->idarraydata + s->requires;
3354           while ((req = *reqp++) != 0)
3355             {
3356               FOR_PROVIDES(p, pp, req)
3357                 if (MAPTST(&im, p))
3358                   break;
3359               if (p)
3360                 continue;
3361               FOR_PROVIDES(p, pp, req)
3362                 {
3363                   if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3364                     {
3365                       if (p == ip)
3366                         continue;
3367                       if (MAPTST(&userinstalled, p - installed->start))
3368                         continue;
3369 #ifdef CLEANDEPSDEBUG
3370                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3371 #endif
3372                       MAPSET(&im, p);
3373                       queue_push(&iq, p);
3374                     }
3375                 }
3376             }
3377         }
3378       if (s->recommends)
3379         {
3380           reqp = s->repo->idarraydata + s->recommends;
3381           while ((req = *reqp++) != 0)
3382             {
3383               FOR_PROVIDES(p, pp, req)
3384                 if (MAPTST(&im, p))
3385                   break;
3386               if (p)
3387                 continue;
3388               FOR_PROVIDES(p, pp, req)
3389                 {
3390                   if (!MAPTST(&im, p) && MAPTST(&installedm, p))
3391                     {
3392                       if (p == ip)
3393                         continue;
3394                       if (MAPTST(&userinstalled, p - installed->start))
3395                         continue;
3396 #ifdef CLEANDEPSDEBUG
3397                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3398 #endif
3399                       MAPSET(&im, p);
3400                       queue_push(&iq, p);
3401                     }
3402                 }
3403             }
3404         }
3405     }
3406     
3407   queue_free(&iq);
3408   /* make sure the updatepkgs and mistakes are not in the cleandeps map */
3409   if (solv->cleandeps_updatepkgs)
3410     for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3411       MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
3412   if (solv->cleandeps_mistakes)
3413     for (i = 0; i < solv->cleandeps_mistakes->count; i++)
3414       MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
3415   /* also remove original iq packages */
3416   for (i = 0; i < iqcopy.count; i++)
3417     MAPSET(&im, iqcopy.elements[i]);
3418   queue_free(&iqcopy);
3419   for (p = installed->start; p < installed->end; p++)
3420     {
3421       if (pool->solvables[p].repo != installed)
3422         continue;
3423       if (!MAPTST(&im, p))
3424         MAPSET(cleandepsmap, p - installed->start);
3425     }
3426   map_free(&im);
3427   map_free(&installedm);
3428   map_free(&userinstalled);
3429   queue_free(&xsuppq);
3430 #ifdef CLEANDEPSDEBUG
3431   printf("=== final cleandeps map:\n");
3432   for (p = installed->start; p < installed->end; p++)
3433     if (MAPTST(cleandepsmap, p - installed->start))
3434       printf("  - %s\n", pool_solvid2str(pool, p));
3435 #endif
3436 }
3437
3438
3439 struct trj_data {
3440   Queue *edges;
3441   Id *low;
3442   Id idx;
3443   Id nstack;
3444   Id firstidx;
3445 };
3446
3447 /* Tarjan's SCC algorithm, slightly modifed */
3448 static void
3449 trj_visit(struct trj_data *trj, Id node)
3450 {
3451   Id *low = trj->low;
3452   Queue *edges = trj->edges;
3453   Id nnode, myidx, stackstart;
3454   int i;
3455
3456   low[node] = myidx = trj->idx++;
3457   low[(stackstart = trj->nstack++)] = node;
3458   for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
3459     {
3460       Id l = low[nnode];
3461       if (!l)
3462         {
3463           if (!edges->elements[edges->elements[nnode]])
3464             {
3465               trj->idx++;
3466               low[nnode] = -1;
3467               continue;
3468             }
3469           trj_visit(trj, nnode);
3470           l = low[nnode];
3471         }
3472       if (l < 0)
3473         continue;
3474       if (l < trj->firstidx)
3475         {
3476           int k;
3477           for (k = l; low[low[k]] == l; k++)
3478             low[low[k]] = -1;
3479         }
3480       else if (l < low[node])
3481         low[node] = l;
3482     }
3483   if (low[node] == myidx)
3484     {
3485       if (myidx != trj->firstidx)
3486         myidx = -1;
3487       for (i = stackstart; i < trj->nstack; i++)
3488         low[low[i]] = myidx;
3489       trj->nstack = stackstart;
3490     }
3491 }
3492
3493
3494 void
3495 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
3496 {
3497   Repo *installed = solv->installed;
3498   int i;
3499   Map cleandepsmap;
3500
3501   queue_empty(unneededq);
3502   if (!installed || installed->end == installed->start)
3503     return;
3504
3505   map_init(&cleandepsmap, installed->end - installed->start);
3506   solver_createcleandepsmap(solv, &cleandepsmap, 1);
3507   for (i = installed->start; i < installed->end; i++)
3508     if (MAPTST(&cleandepsmap, i - installed->start))
3509       queue_push(unneededq, i);
3510
3511   if (filtered && unneededq->count > 1)
3512     {
3513       Pool *pool = solv->pool;
3514       Queue edges;
3515       Id *nrequires;
3516       Map installedm;
3517       int j, pass, count = unneededq->count;
3518       Id *low;
3519
3520       map_init(&installedm, pool->nsolvables);
3521       for (i = installed->start; i < installed->end; i++)
3522         if (pool->solvables[i].repo == installed)
3523           MAPSET(&installedm, i);
3524
3525       nrequires = solv_calloc(count, sizeof(Id));
3526       queue_init(&edges);
3527       queue_prealloc(&edges, count * 4 + 10);   /* pre-size */
3528
3529       /*
3530        * Go through the solvables in the nodes queue and create edges for
3531        * all requires/recommends/supplements between the nodes.
3532        * The edges are stored in the edges queue, we add 1 to the node
3533        * index so that nodes in the edges queue are != 0 and we can
3534        * terminate the edge list with 0.
3535        * Thus for node element 5, the edges are stored starting at
3536        * edges.elements[6] and are 0-terminated.
3537        */
3538       /* leave first element zero to make things easier */
3539       /* also add trailing zero */
3540       queue_insertn(&edges, 0, 1 + count + 1, 0);
3541
3542       /* first requires and recommends */
3543       for (i = 0; i < count; i++)
3544         {
3545           Solvable *s = pool->solvables + unneededq->elements[i];
3546           edges.elements[i + 1] = edges.count;
3547           for (pass = 0; pass < 2; pass++)
3548             {
3549               int num = 0;
3550               unsigned int off = pass == 0 ? s->requires : s->recommends;
3551               Id p, pp, *dp;
3552               if (off)
3553                 for (dp = s->repo->idarraydata + off; *dp; dp++)
3554                   FOR_PROVIDES(p, pp, *dp)
3555                     {
3556                       Solvable *sp = pool->solvables + p;
3557                       if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3558                         continue;
3559                       for (j = 0; j < count; j++)
3560                         if (p == unneededq->elements[j])
3561                           break;
3562                       if (j == count)
3563                         continue;
3564                       if (num && edges.elements[edges.count - 1] == j + 1)
3565                         continue;
3566                       queue_push(&edges, j + 1);
3567                       num++;
3568                     }
3569                 if (pass == 0)
3570                   nrequires[i] = num;
3571             }
3572           queue_push(&edges, 0);
3573         }
3574 #if 0
3575       printf("requires + recommends\n");
3576       for (i = 0; i < count; i++)
3577         {
3578           int j;
3579           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3580           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3581             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3582         }
3583 #endif
3584
3585       /* then add supplements */
3586       for (i = 0; i < count; i++)
3587         {
3588           Solvable *s = pool->solvables + unneededq->elements[i];
3589           if (s->supplements)
3590             {
3591               Id *dp;
3592               int k;
3593               for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
3594                 if (dep_possible(solv, *dp, &installedm))
3595                   {
3596                     Queue iq;
3597                     Id iqbuf[16];
3598                     queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
3599                     dep_pkgcheck(solv, *dp, 0, &iq);
3600                     for (k = 0; k < iq.count; k++)
3601                       {
3602                         Id p = iq.elements[k];
3603                         Solvable *sp = pool->solvables + p;
3604                         if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
3605                           continue;
3606                         for (j = 0; j < count; j++)
3607                           if (p == unneededq->elements[j])
3608                             break;
3609                         /* now add edge from j + 1 to i + 1 */
3610                         queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
3611                         /* addapt following edge pointers */
3612                         for (k = j + 2; k < count + 2; k++)
3613                           edges.elements[k]++;
3614                       }
3615                     queue_free(&iq);
3616                   }
3617             }
3618         }
3619 #if 0
3620       /* print result */
3621       printf("+ supplements\n");
3622       for (i = 0; i < count; i++)
3623         {
3624           int j;
3625           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
3626           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
3627             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
3628     }
3629 #endif
3630       map_free(&installedm);
3631
3632       /* now run SCC algo two times, first with requires+recommends+supplements,
3633        * then again without the requires. We run it the second time to get rid
3634        * of packages that got dragged in via recommends/supplements */
3635       /*
3636        * low will contain the result of the SCC search.
3637        * it must be of at least size 2 * (count + 1) and
3638        * must be zero initialized.
3639        * The layout is:
3640        *    0  low low ... low stack stack ...stack 0
3641        *            count              count
3642        */
3643       low = solv_calloc(count + 1, 2 * sizeof(Id));
3644       for (pass = 0; pass < 2; pass++)
3645         {
3646           struct trj_data trj;
3647           if (pass)
3648             {
3649               memset(low, 0, (count + 1) * (2 * sizeof(Id)));
3650               for (i = 0; i < count; i++)
3651                 {
3652                   edges.elements[i + 1] += nrequires[i];
3653                   if (!unneededq->elements[i])
3654                     low[i + 1] = -1;    /* ignore this node */
3655                 }
3656             }
3657           trj.edges = &edges;
3658           trj.low = low;
3659           trj.idx = count + 1;  /* stack starts here */
3660           for (i = 1; i <= count; i++)
3661             {
3662               if (low[i])
3663                 continue;
3664               if (edges.elements[edges.elements[i]])
3665                 {
3666                   trj.firstidx = trj.nstack = trj.idx;
3667                   trj_visit(&trj, i);
3668                 }
3669               else
3670                 {
3671                   Id myidx = trj.idx++;
3672                   low[i] = myidx;
3673                   low[myidx] = i;
3674                 }
3675             }
3676           /* prune packages */
3677           for (i = 0; i < count; i++)
3678             if (low[i + 1] <= 0)
3679               unneededq->elements[i] = 0;
3680         }
3681       solv_free(low);
3682       solv_free(nrequires);
3683       queue_free(&edges);
3684
3685       /* finally remove all pruned entries from unneededq */
3686       for (i = j = 0; i < count; i++)
3687         if (unneededq->elements[i])
3688           unneededq->elements[j++] = unneededq->elements[i];
3689       queue_truncate(unneededq, j);
3690     }
3691   map_free(&cleandepsmap);
3692 }
3693
3694 /* EOF */