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