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