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