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