33f5350a0e82644c821b2c9ac9410313c1381c05
[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 #include "linkedpkg.h"
30 #include "cplxdeps.h"
31
32 #define RULES_BLOCK 63
33
34 static void addpkgruleinfo(Solver *solv, Id p, Id p2, Id d, int type, Id dep);
35 static void solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded);
36
37 /*-------------------------------------------------------------------
38  * Check if dependency is possible
39  *
40  * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap.
41  * used in solver_addpkgrulesforweak and solver_createcleandepsmap.
42  */
43
44 static inline int
45 dep_possible(Solver *solv, Id dep, Map *m)
46 {
47   Pool *pool = solv->pool;
48   Id p, pp;
49
50   if (ISRELDEP(dep))
51     {
52       Reldep *rd = GETRELDEP(pool, dep);
53       if (rd->flags >= 8)
54          {
55           if (rd->flags == REL_COND)
56             return 1;
57           if (rd->flags == REL_AND)
58             {
59               if (!dep_possible(solv, rd->name, m))
60                 return 0;
61               return dep_possible(solv, rd->evr, m);
62             }
63           if (rd->flags == REL_OR)
64             {
65               if (dep_possible(solv, rd->name, m))
66                 return 1;
67               return dep_possible(solv, rd->evr, m);
68             }
69           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
70             return solver_splitprovides(solv, rd->evr, m);
71         }
72     }
73   FOR_PROVIDES(p, pp, dep)
74     {
75       if (MAPTST(m, p))
76         return 1;
77     }
78   return 0;
79 }
80
81 static inline int
82 is_otherproviders_dep(Pool *pool, Id dep)
83 {
84   if (ISRELDEP(dep))
85     {
86       Reldep *rd = GETRELDEP(pool, dep);
87       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
88         return 1;
89     }
90   return 0;
91 }
92
93 /********************************************************************
94  *
95  * Rule handling
96  *
97  * - unify rules, remove duplicates
98  */
99
100 /*-------------------------------------------------------------------
101  *
102  * compare rules for unification sort
103  *
104  */
105
106 static int
107 unifyrules_sortcmp(const void *ap, const void *bp, void *dp)
108 {
109   Pool *pool = dp;
110   Rule *a = (Rule *)ap;
111   Rule *b = (Rule *)bp;
112   Id *ad, *bd;
113   int x;
114
115   x = a->p - b->p;
116   if (x)
117     return x;                           /* p differs */
118
119   /* identical p */
120   if (a->d == 0 && b->d == 0)           /* both assertions or binary */
121     return a->w2 - b->w2;
122
123   if (a->d == 0)                        /* a is assertion or binary, b not */
124     {
125       x = a->w2 - pool->whatprovidesdata[b->d];
126       return x ? x : -1;
127     }
128
129   if (b->d == 0)                        /* b is assertion or binary, a not */
130     {
131       x = pool->whatprovidesdata[a->d] - b->w2;
132       return x ? x : 1;
133     }
134
135   if (a->d == b->d)
136     return 0;
137
138   /* compare whatprovidesdata */
139   ad = pool->whatprovidesdata + a->d;
140   bd = pool->whatprovidesdata + b->d;
141   while (*bd)
142     if ((x = *ad++ - *bd++) != 0)
143       return x;
144   return *ad;
145 }
146
147 int
148 solver_rulecmp(Solver *solv, Rule *r1, Rule *r2)
149 {
150   return unifyrules_sortcmp(r1, r2, solv->pool);
151 }
152
153
154 /*-------------------------------------------------------------------
155  *
156  * unify rules
157  * go over all rules and remove duplicates
158  */
159
160 void
161 solver_unifyrules(Solver *solv)
162 {
163   Pool *pool = solv->pool;
164   int i, j;
165   Rule *ir, *jr;
166
167   if (solv->nrules <= 2)                /* nothing to unify */
168     return;
169
170   if (solv->recommendsruleq)
171     {
172       /* mis-use n2 as recommends rule marker */
173       for (i = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
174         ir->n2 = 0;
175       for (i = 0; i < solv->recommendsruleq->count; i++)
176         solv->rules[solv->recommendsruleq->elements[i]].n2 = 1;
177     }
178
179   /* sort rules first */
180   solv_sort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp, solv->pool);
181
182   /* prune rules */
183   jr = 0;
184   for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
185     {
186       if (jr && !unifyrules_sortcmp(ir, jr, pool))
187         {
188           jr->n2 &= ir->n2;             /* bitwise-and recommends marker */
189           continue;                     /* prune! */
190         }
191       jr = solv->rules + j++;           /* keep! */
192       if (ir != jr)
193         *jr = *ir;
194     }
195
196   /* reduced count from nrules to j rules */
197   POOL_DEBUG(SOLV_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
198
199   /* adapt rule buffer */
200   solver_shrinkrules(solv, j);
201
202   if (solv->recommendsruleq)
203     {
204       /* rebuild recommendsruleq */
205       queue_empty(solv->recommendsruleq);
206       for (i = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
207         if (ir->n2)
208           {
209             ir->n2 = 0;
210             queue_push(solv->recommendsruleq, i);
211           }
212     }
213
214   /*
215    * debug: log rule statistics
216    */
217   IF_POOLDEBUG (SOLV_DEBUG_STATS)
218     {
219       int binr = 0;
220       int lits = 0;
221       Id *dp;
222       Rule *r;
223
224       for (i = 1; i < solv->nrules; i++)
225         {
226           r = solv->rules + i;
227           if (r->d == 0)
228             binr++;
229           else
230             {
231               dp = solv->pool->whatprovidesdata + r->d;
232               while (*dp++)
233                 lits++;
234             }
235         }
236       POOL_DEBUG(SOLV_DEBUG_STATS, "  binary: %d\n", binr);
237       POOL_DEBUG(SOLV_DEBUG_STATS, "  normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
238     }
239 }
240
241 #if 0
242
243 /*
244  * hash rule
245  */
246
247 static Hashval
248 hashrule(Solver *solv, Id p, Id d, int n)
249 {
250   unsigned int x = (unsigned int)p;
251   int *dp;
252
253   if (n <= 1)
254     return (x * 37) ^ (unsigned int)d;
255   dp = solv->pool->whatprovidesdata + d;
256   while (*dp)
257     x = (x * 37) ^ (unsigned int)*dp++;
258   return x;
259 }
260 #endif
261
262
263 /*-------------------------------------------------------------------
264  *
265  */
266
267 /*
268  * add rule
269  *
270  * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
271  *
272  * p < 0  : pkg id of A
273  * d > 0  : Offset in whatprovidesdata (list of providers of b)
274  *
275  * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
276  * p < 0  : pkg id of A
277  * p2 < 0 : Id of solvable (e.g. B1)
278  *
279  * d == 0, p2 == 0: unary rule, assertion => (A) or (-A)
280  *
281  *   Install:    p > 0, d = 0   (A)             user requested install
282  *   Remove:     p < 0, d = 0   (-A)            user requested remove (also: uninstallable)
283  *   Requires:   p < 0, d > 0   (-A|B1|B2|...)  d: <list of providers for requirement of p>
284  *   Updates:    p > 0, d > 0   (A|B1|B2|...)   d: <list of updates for solvable p>
285  *   Conflicts:  p < 0, p2 < 0  (-A|-B)         either p (conflict issuer) or d (conflict provider) (binary rule)
286  *                                              also used for obsoletes
287  *   No-op ?:    p = 0, d = 0   (null)          (used as placeholder in update/feature rules)
288  *
289  *   resulting watches:
290  *   ------------------
291  *   Direct assertion (no watch needed) --> d = 0, w1 = p, w2 = 0
292  *   Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
293  *   every other : w1 = p, w2 = whatprovidesdata[d];
294  *
295  *   always returns a rule for non-pkg rules
296  */
297
298 Rule *
299 solver_addrule(Solver *solv, Id p, Id p2, Id d)
300 {
301   Pool *pool = solv->pool;
302   Rule *r;
303
304   if (d)
305     {
306       assert(!p2 && d > 0);
307       if (!pool->whatprovidesdata[d])
308         d = 0;
309       else if (!pool->whatprovidesdata[d + 1])
310         {
311           p2 = pool->whatprovidesdata[d];
312           d = 0;
313         }
314     }
315
316   /* now we have two cases:
317    * 1 or 2 literals:    d = 0, p, p2 contain the literals
318    * 3 or more literals: d > 0, p2 == 0, d is offset into whatprovidesdata
319    */
320
321   /* it often happenes that requires lead to adding the same pkg rule
322    * multiple times, so we prune those duplicates right away to make
323    * the work for unifyrules a bit easier */
324   if (!solv->pkgrules_end)              /* we add pkg rules */
325     {
326       r = solv->rules + solv->lastpkgrule;
327       if (d)
328         {
329           Id *dp;
330           /* check if rule is identical */
331           if (r->p == p)
332             {
333               Id *dp2;
334               if (r->d == d)
335                 return r;
336               dp2 = pool->whatprovidesdata + r->d;
337               for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
338                 if (*dp != *dp2)
339                   break;
340               if (*dp == *dp2)
341                 return r;
342             }
343           /* check if rule is self-fulfilling */
344           for (dp = pool->whatprovidesdata + d; *dp; dp++)
345             if (*dp == -p)
346               return 0;                 /* rule is self-fulfilling */
347         }
348       else
349         {
350           if (p2 && p > p2)
351             {
352               Id o = p;                 /* switch p1 and p2 */
353               p = p2;
354               p2 = o;
355             }
356           if (r->p == p && !r->d && r->w2 == p2)
357             return r;
358           if (p == -p2)
359             return 0;                   /* rule is self-fulfilling */
360         }
361       solv->lastpkgrule = solv->nrules;
362     }
363
364   solv->rules = solv_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
365   r = solv->rules + solv->nrules++;    /* point to rule space */
366   r->p = p;
367   r->d = d;
368   r->w1 = p;
369   r->w2 = d ? pool->whatprovidesdata[d] : p2;
370   r->n1 = 0;
371   r->n2 = 0;
372   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
373     {
374       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "  Add rule: ");
375       solver_printrule(solv, SOLV_DEBUG_RULE_CREATION, r);
376     }
377   return r;
378 }
379
380
381 void
382 solver_shrinkrules(Solver *solv, int nrules)
383 {
384   solv->nrules = nrules;
385   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
386   solv->lastpkgrule = 0;
387 }
388
389 /******************************************************************************
390  ***
391  *** pkg rule part: create rules representing the package dependencies
392  ***
393  ***/
394
395 /*
396  *  special multiversion patch conflict handling:
397  *  a patch conflict is also satisfied if some other
398  *  version with the same name/arch that doesn't conflict
399  *  gets installed. The generated rule is thus:
400  *  -patch|-cpack|opack1|opack2|...
401  */
402 static Id
403 makemultiversionconflict(Solver *solv, Id n, Id con)
404 {
405   Pool *pool = solv->pool;
406   Solvable *s, *sn;
407   Queue q;
408   Id p, pp, qbuf[64];
409
410   sn = pool->solvables + n;
411   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
412   queue_push(&q, -n);
413   FOR_PROVIDES(p, pp, sn->name)
414     {
415       s = pool->solvables + p;
416       if (s->name != sn->name || s->arch != sn->arch)
417         continue;
418       if (!MAPTST(&solv->multiversion, p))
419         continue;
420       if (pool_match_nevr(pool, pool->solvables + p, con))
421         continue;
422       /* here we have a multiversion solvable that doesn't conflict */
423       /* thus we're not in conflict if it is installed */
424       queue_push(&q, p);
425     }
426   if (q.count == 1)
427     n = 0;      /* no other package found, normal conflict handling */
428   else
429     n = pool_queuetowhatprovides(pool, &q);
430   queue_free(&q);
431   return n;
432 }
433
434 static inline void
435 addpkgrule(Solver *solv, Id p, Id p2, Id d, int type, Id dep)
436 {
437   if (!solv->ruleinfoq)
438     solver_addrule(solv, p, p2, d);
439   else
440     addpkgruleinfo(solv, p, p2, d, type, dep);
441 }
442
443 #ifdef ENABLE_LINKED_PKGS
444
445 static int
446 addlinks_cmp(const void *ap, const void *bp, void *dp)
447 {
448   Pool *pool = dp;
449   Id a = *(Id *)ap;
450   Id b = *(Id *)bp;
451   Solvable *sa = pool->solvables + a;
452   Solvable *sb = pool->solvables + b;
453   if (sa->name != sb->name)
454     return sa->name - sb->name;
455   return sa - sb;
456 }
457
458 static void
459 addlinks(Solver *solv, Solvable *s, Id req, Queue *qr, Id prv, Queue *qp, Map *m, Queue *workq)
460 {
461   Pool *pool = solv->pool;
462   int i;
463   if (!qr->count)
464     return;
465   if (qp->count > 1)
466     solv_sort(qp->elements, qp->count, sizeof(Id), addlinks_cmp, pool);
467 #if 0
468   printf("ADDLINKS %s\n -> %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
469   for (i = 0; i < qr->count; i++)
470     printf("    - %s\n", pool_solvid2str(pool, qr->elements[i]));
471   printf(" <- %s\n", pool_dep2str(pool, prv));
472   for (i = 0; i < qp->count; i++)
473     printf("    - %s\n", pool_solvid2str(pool, qp->elements[i]));
474 #endif
475
476   if (qr->count == 1)
477     addpkgrule(solv, -(s - pool->solvables), qr->elements[0], 0, SOLVER_RULE_PKG_REQUIRES, req);
478   else
479     addpkgrule(solv, -(s - pool->solvables), 0, pool_queuetowhatprovides(pool, qr), SOLVER_RULE_PKG_REQUIRES, req);
480   if (qp->count > 1)
481     {
482       int j;
483       for (i = j = 0; i < qp->count; i = j)
484         {
485           Id d = pool->solvables[qp->elements[i]].name;
486           for (j = i + 1; j < qp->count; j++)
487             if (d != pool->solvables[qp->elements[j]].name)
488               break;
489           d = pool_ids2whatprovides(pool, qp->elements + i, j - i);
490           for (i = 0; i < qr->count; i++)
491             addpkgrule(solv, -qr->elements[i], 0, d, SOLVER_RULE_PKG_REQUIRES, prv);
492         }
493     }
494   else if (qp->count)
495     {
496       for (i = 0; i < qr->count; i++)
497         addpkgrule(solv, -qr->elements[i], qp->elements[0], 0, SOLVER_RULE_PKG_REQUIRES, prv);
498     }
499   if (!m)
500     return;     /* nothing more to do if called from getpkgruleinfos() */
501   for (i = 0; i < qr->count; i++)
502     if (!MAPTST(m, qr->elements[i]))
503       queue_push(workq, qr->elements[i]);
504   for (i = 0; i < qp->count; i++)
505     if (!MAPTST(m, qp->elements[i]))
506       queue_push(workq, qp->elements[i]);
507   if (solv->installed && s->repo == solv->installed)
508     {
509       Repo *installed = solv->installed;
510       /* record installed buddies */
511       if (!solv->instbuddy)
512         solv->instbuddy = solv_calloc(installed->end - installed->start, sizeof(Id));
513       if (qr->count == 1)
514         solv->instbuddy[s - pool->solvables - installed->start] = qr->elements[0];
515       for (i = 0; i < qr->count; i++)
516         {
517           Id p = qr->elements[i];
518           if (pool->solvables[p].repo != installed)
519             continue;   /* huh? */
520           if (qp->count > 1 || (solv->instbuddy[p - installed->start] != 0 && solv->instbuddy[p - installed->start] != s - pool->solvables))
521             solv->instbuddy[p - installed->start] = 1;  /* 1: ambiguous buddy */
522           else
523             solv->instbuddy[p - installed->start] = s - pool->solvables;
524         }
525     }
526 }
527
528 static void
529 add_package_link(Solver *solv, Solvable *s, Map *m, Queue *workq)
530 {
531   Queue qr, qp;
532   Id req = 0, prv = 0;
533   queue_init(&qr);
534   queue_init(&qp);
535   find_package_link(solv->pool, s, &req, &qr, &prv, &qp);
536   if (qr.count)
537     addlinks(solv, s, req, &qr, prv, &qp, m, workq);
538   queue_free(&qr);
539   queue_free(&qp);
540 }
541
542 #endif
543
544 #ifdef ENABLE_COMPLEX_DEPS
545
546 static void
547 add_complex_deprules(Solver *solv, Id p, Id dep, int type, int dontfix, Queue *workq, Map *m)
548 {
549   Pool *pool = solv->pool;
550   Repo *installed = solv->installed;
551   int i, j, flags;
552   Queue bq;
553
554   queue_init(&bq);
555   flags = dontfix ? CPLXDEPS_DONTFIX : 0;
556   /* CNF expansion for requires, DNF + INVERT expansion for conflicts */
557   if (type == SOLVER_RULE_PKG_CONFLICTS)
558     flags |= CPLXDEPS_TODNF | CPLXDEPS_EXPAND | CPLXDEPS_INVERT;
559
560   i = pool_normalize_complex_dep(pool, dep, &bq, flags);
561   /* handle special cases */
562   if (i == 0)
563     {
564       if (dontfix)
565         {
566           POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken dependency %s of installed package %s\n", pool_dep2str(pool, dep), pool_solvid2str(pool, p));
567         }
568       else
569         {
570           POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvid2str(pool, p), p, pool_dep2str(pool, dep));
571           addpkgrule(solv, -p, 0, 0, type == SOLVER_RULE_PKG_REQUIRES ? SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP : type, dep);
572         }
573       queue_free(&bq);
574       return;
575     }
576   if (i == 1)
577     {
578       queue_free(&bq);
579       return;
580     }
581
582   /* go through all blocks and add a rule for each block */
583   for (i = 0; i < bq.count; i++)
584     {
585       if (!bq.elements[i])
586         continue;       /* huh? */
587       if (bq.elements[i] == pool->nsolvables)
588         {
589           /* conventional requires (cannot be a conflicts as they have been expanded) */
590           Id *dp = pool->whatprovidesdata + bq.elements[i + 1];
591           i += 2;
592           if (dontfix)
593             {
594               for (j = 0; dp[j] != 0; j++)
595                 if (pool->solvables[dp[j]].repo == installed)
596                   break;                /* provider was installed */
597               if (!dp[j])
598                 continue;
599             }
600           if (type == SOLVER_RULE_PKG_RECOMMENDS && !*dp)
601             continue;
602           /* check if the rule contains both p and -p */
603           for (j = 0; dp[j] != 0; j++)
604             if (dp[j] == p)
605               break;
606           if (dp[j])
607             continue;
608           addpkgrule(solv, -p, 0, dp - pool->whatprovidesdata, type, dep);
609           /* push all non-visited providers on the work queue */
610           if (m)
611             for (; *dp; dp++)
612               if (!MAPTST(m, *dp))
613                 queue_push(workq, *dp);
614           continue;
615         }
616       if (!bq.elements[i + 1])
617         {
618           Id p2 = bq.elements[i++];
619           /* simple rule with just two literals, we'll add a (-p, p2) rule */
620           if (dontfix)
621             {
622               if (p2 < 0 && pool->solvables[-p2].repo == installed)
623                 continue;
624               if (p2 > 0 && pool->solvables[p2].repo != installed)
625                 continue;
626             }
627           if (-p == p2)
628             {
629               if (type == SOLVER_RULE_PKG_CONFLICTS)
630                 {
631                   if (pool->forbidselfconflicts && !is_otherproviders_dep(pool, dep))
632                     addpkgrule(solv, -p, 0, 0, SOLVER_RULE_PKG_SELF_CONFLICT, dep);
633                   continue;
634                 }
635               addpkgrule(solv, -p, 0, 0, type, dep);
636               continue;
637             }
638           /* check if the rule contains both p and -p */
639           if (p == p2)
640             continue;
641           addpkgrule(solv, -p, p2, 0, type, dep);
642           if (m && p2 > 0 && !MAPTST(m, p2))
643             queue_push(workq, p2);
644         }
645       else
646         {
647           Id *qele;
648           int qcnt;
649
650           qele = bq.elements + i;
651           qcnt = i;
652           while (bq.elements[i])
653              i++;
654           qcnt = i - qcnt;
655           if (dontfix)
656             {
657               for (j = 0; j < qcnt; j++)
658                 {
659                   if (qele[j] > 0 && pool->solvables[qele[j]].repo == installed)
660                     break;
661                   if (qele[j] < 0 && pool->solvables[-qele[j]].repo != installed)
662                     break;
663                 }
664               if (j == qcnt)
665                 continue;
666             }
667           /* add -p to (ordered) rule (overwriting the trailing zero) */
668           for (j = 0; ; j++)
669             {
670               if (j == qcnt || qele[j] > -p)
671                 {
672                   if (j < qcnt)
673                     memmove(qele + j + 1, qele + j, (qcnt - j) * sizeof(Id));
674                   qele[j] = -p;
675                   qcnt++;
676                   break;
677                 }
678               if (qele[j] == -p)
679                 break;
680             }
681           /* check if the rule contains both p and -p */
682           for (j = 0; j < qcnt; j++)
683             if (qele[j] == p)
684               break;
685           if (j < qcnt)
686             continue;
687           addpkgrule(solv, qele[0], 0, pool_ids2whatprovides(pool, qele + 1, qcnt - 1), type, dep);
688           if (m)
689             for (j = 0; j < qcnt; j++)
690               if (qele[j] > 0 && !MAPTST(m, qele[j]))
691                 queue_push(workq, qele[j]);
692         }
693     }
694   queue_free(&bq);
695 }
696
697 #endif
698
699 /*-------------------------------------------------------------------
700  *
701  * add dependency rules for solvable
702  *
703  * s: Solvable for which to add rules
704  * m: m[s] = 1 for solvables which have rules, prevent rule duplication
705  *
706  * Algorithm: 'visit all nodes of a graph'. The graph nodes are
707  *  solvables, the edges their dependencies.
708  *  Starting from an installed solvable, this will create all rules
709  *  representing the graph created by the solvables dependencies.
710  *
711  * for unfulfilled requirements, conflicts, obsoletes,....
712  * add a negative assertion for solvables that are not installable
713  *
714  * It will also create rules for all solvables referenced by 's'
715  *  i.e. descend to all providers of requirements of 's'
716  *
717  */
718
719 void
720 solver_addpkgrulesforsolvable(Solver *solv, Solvable *s, Map *m)
721 {
722   Pool *pool = solv->pool;
723   Repo *installed = solv->installed;
724
725   Queue workq;  /* list of solvables we still have to work on */
726   Id workqbuf[64];
727   Queue prereqq;        /* list of pre-req ids to ignore */
728   Id prereqbuf[16];
729
730   int i;
731   int dontfix;          /* ignore dependency errors for installed solvables */
732   Id req, *reqp;
733   Id con, *conp;
734   Id obs, *obsp;
735   Id rec, *recp;
736   Id sug, *sugp;
737   Id p, pp;             /* whatprovides loops */
738   Id *dp;               /* ptr to 'whatprovides' */
739   Id n;                 /* Id for current solvable 's' */
740
741   queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
742   queue_push(&workq, s - pool->solvables);      /* push solvable Id to work queue */
743
744   queue_init_buffer(&prereqq, prereqbuf, sizeof(prereqbuf)/sizeof(*prereqbuf));
745
746   /* loop until there's no more work left */
747   while (workq.count)
748     {
749       /*
750        * n: Id of solvable
751        * s: Pointer to solvable
752        */
753
754       n = queue_shift(&workq);          /* 'pop' next solvable to work on from queue */
755       if (m)
756         {
757           if (MAPTST(m, n))             /* continue if already visited */
758             continue;
759           MAPSET(m, n);                 /* mark as visited */
760         }
761
762       s = pool->solvables + n;
763
764       dontfix = 0;
765       if (installed                     /* Installed system available */
766           && s->repo == installed       /* solvable is installed */
767           && !solv->fixmap_all          /* NOT repair errors in dependency graph */
768           && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
769         {
770           dontfix = 1;                  /* dont care about broken deps */
771         }
772
773       if (!dontfix)
774         {
775           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC
776                 ? pool_disabled_solvable(pool, s)
777                 : !pool_installable(pool, s))
778             {
779               POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", pool_solvid2str(pool, n), n);
780               addpkgrule(solv, -n, 0, 0, SOLVER_RULE_PKG_NOT_INSTALLABLE, 0);
781             }
782         }
783
784 #ifdef ENABLE_LINKED_PKGS
785       /* add pseudo-package <-> real-package links */
786       if (has_package_link(pool, s))
787         add_package_link(solv, s, m, &workq);
788 #endif
789
790       /*-----------------------------------------
791        * check requires of s
792        */
793
794       if (s->requires)
795         {
796           int filterpre = 0;
797           reqp = s->repo->idarraydata + s->requires;
798           while ((req = *reqp++) != 0)            /* go through all requires */
799             {
800               if (req == SOLVABLE_PREREQMARKER)   /* skip the marker */
801                 {
802                   if (installed && s->repo == installed)
803                     {
804                       if (prereqq.count)
805                         queue_empty(&prereqq);
806                       solvable_lookup_idarray(s, SOLVABLE_PREREQ_IGNOREINST, &prereqq);
807                       filterpre = prereqq.count;
808                     }
809                   continue;
810                 }
811               if (filterpre)
812                 {
813                   /* check if this id is filtered. assumes that prereqq.count is small */
814                   for (i = 0; i < prereqq.count; i++)
815                     if (req == prereqq.elements[i])
816                       break;
817                   if (i < prereqq.count)
818                     {
819                       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s: ignoring filtered pre-req dependency %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
820                       continue;
821                     }
822                 }
823
824 #ifdef ENABLE_COMPLEX_DEPS
825               if (pool_is_complex_dep(pool, req))
826                 {
827                   /* we have AND/COND deps, normalize */
828                   add_complex_deprules(solv, n, req, SOLVER_RULE_PKG_REQUIRES, dontfix, &workq, m);
829                   continue;
830                 }
831 #endif
832
833               /* find list of solvables providing 'req' */
834               dp = pool_whatprovides_ptr(pool, req);
835
836               if (*dp == SYSTEMSOLVABLE)          /* always installed */
837                 continue;
838
839               if (dontfix)
840                 {
841                   /* the strategy here is to not insist on dependencies
842                    * that are already broken. so if we find one provider
843                    * that was already installed, we know that the
844                    * dependency was not broken before so we enforce it */
845                   for (i = 0; (p = dp[i]) != 0; i++)
846                     if (pool->solvables[p].repo == installed)
847                       break;            /* found installed provider */
848                   if (!p)
849                     {
850                       /* didn't find an installed provider: previously broken dependency */
851                       POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s));
852                       continue;
853                     }
854                 }
855
856               if (!*dp)
857                 {
858                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", pool_solvid2str(pool, n), n, pool_dep2str(pool, req));
859                   addpkgrule(solv, -n, 0, 0, SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP, req);
860                   continue;
861                 }
862
863               for (i = 0; dp[i] != 0; i++)
864                 if (n == dp[i])
865                   break;
866               if (dp[i])
867                 continue;               /* provided by itself, no need to add rule */
868
869               IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
870                 {
871                   POOL_DEBUG(SOLV_DEBUG_RULE_CREATION,"  %s requires %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, req));
872                   for (i = 0; dp[i]; i++)
873                     POOL_DEBUG(SOLV_DEBUG_RULE_CREATION, "   provided by %s\n", pool_solvid2str(pool, dp[i]));
874                 }
875
876               /* add 'requires' dependency */
877               /* rule: (-requestor|provider1|provider2|...|providerN) */
878               addpkgrule(solv, -n, 0, dp - pool->whatprovidesdata, SOLVER_RULE_PKG_REQUIRES, req);
879
880               /* push all non-visited providers on the work queue */
881               if (m)
882                 for (; *dp; dp++)
883                   if (!MAPTST(m, *dp))
884                     queue_push(&workq, *dp);
885             }
886         }
887
888       if (s->recommends && solv->strongrecommends)
889         {
890           int start = solv->nrules;
891           solv->lastpkgrule = 0;
892           reqp = s->repo->idarraydata + s->recommends;
893           while ((req = *reqp++) != 0)            /* go through all recommends */
894             {
895 #ifdef ENABLE_COMPLEX_DEPS
896               if (pool_is_complex_dep(pool, req))
897                 {
898                   /* we have AND/COND deps, normalize */
899                   add_complex_deprules(solv, n, req, SOLVER_RULE_PKG_RECOMMENDS, dontfix, &workq, m);
900                   continue;
901                 }
902 #endif
903               dp = pool_whatprovides_ptr(pool, req);
904               if (*dp == SYSTEMSOLVABLE || !*dp)          /* always installed or not installable */
905                 continue;
906               for (i = 0; dp[i] != 0; i++)
907                 if (n == dp[i])
908                   break;
909               if (dp[i])
910                 continue;               /* provided by itself, no need to add rule */
911               addpkgrule(solv, -n, 0, dp - pool->whatprovidesdata, SOLVER_RULE_PKG_RECOMMENDS, req);
912               if (m)
913                 for (; *dp; dp++)
914                   if (!MAPTST(m, *dp))
915                     queue_push(&workq, *dp);
916             }
917           if (!solv->ruleinfoq && start < solv->nrules)
918             {
919               if (!solv->recommendsruleq)
920                 {
921                   solv->recommendsruleq = solv_calloc(1, sizeof(Queue));
922                   queue_init(solv->recommendsruleq);
923                 }
924               for (i = start; i < solv->nrules; i++)
925                 queue_push(solv->recommendsruleq, i);
926               solv->lastpkgrule = 0;
927             }
928         }
929
930       /* that's all we check for src packages */
931       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
932         continue;
933
934       /*-----------------------------------------
935        * check conflicts of s
936        */
937
938       if (s->conflicts)
939         {
940           int ispatch = 0;
941
942           /* we treat conflicts in patches a bit differen:
943            * - nevr matching
944            * - multiversion handling
945            * XXX: we should really handle this different, looking
946            * at the name is a bad hack
947            */
948           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
949             ispatch = 1;
950           conp = s->repo->idarraydata + s->conflicts;
951           /* foreach conflicts of 's' */
952           while ((con = *conp++) != 0)
953             {
954 #ifdef ENABLE_COMPLEX_DEPS
955               if (!ispatch && pool_is_complex_dep(pool, con))
956                 {
957                   /* we have AND/COND deps, normalize */
958                   add_complex_deprules(solv, n, con, SOLVER_RULE_PKG_CONFLICTS, dontfix, &workq, m);
959                   continue;
960                 }
961 #endif
962               /* foreach providers of a conflict of 's' */
963               FOR_PROVIDES(p, pp, con)
964                 {
965                   if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
966                     continue;
967                   /* dontfix: dont care about conflicts with already installed packs */
968                   if (dontfix && pool->solvables[p].repo == installed)
969                     continue;
970                   if (p == n)           /* p == n: self conflict */
971                     {
972                       if (!pool->forbidselfconflicts || is_otherproviders_dep(pool, con))
973                         continue;
974                       addpkgrule(solv, -n, 0, 0, SOLVER_RULE_PKG_SELF_CONFLICT, con);
975                       continue;
976                     }
977                   if (ispatch && solv->multiversion.size && MAPTST(&solv->multiversion, p) && ISRELDEP(con))
978                     {
979                       /* our patch conflicts with a multiversion package */
980                       Id d = makemultiversionconflict(solv, p, con);
981                       if (d)
982                         {
983                           addpkgrule(solv, -n, 0, d, SOLVER_RULE_PKG_CONFLICTS, con);
984                           continue;
985                         }
986                     }
987                   if (p == SYSTEMSOLVABLE)
988                     p = 0;
989                   /* rule: -n|-p: either solvable _or_ provider of conflict */
990                   addpkgrule(solv, -n, -p, 0, SOLVER_RULE_PKG_CONFLICTS, con);
991                 }
992             }
993         }
994
995       /*-----------------------------------------
996        * check obsoletes and implicit obsoletes of a package
997        * if ignoreinstalledsobsoletes is not set, we're also checking
998        * obsoletes of installed packages (like newer rpm versions)
999        */
1000       if ((!installed || s->repo != installed) || !pool->noinstalledobsoletes)
1001         {
1002           int multi = solv->multiversion.size && MAPTST(&solv->multiversion, n);
1003           int isinstalled = (installed && s->repo == installed);
1004           if (s->obsoletes && (!multi || solv->keepexplicitobsoletes))
1005             {
1006               obsp = s->repo->idarraydata + s->obsoletes;
1007               /* foreach obsoletes */
1008               while ((obs = *obsp++) != 0)
1009                 {
1010                   /* foreach provider of an obsoletes of 's' */
1011                   FOR_PROVIDES(p, pp, obs)
1012                     {
1013                       Solvable *ps = pool->solvables + p;
1014                       if (p == n)
1015                         continue;
1016                       if (isinstalled && dontfix && ps->repo == installed)
1017                         continue;       /* don't repair installed/installed problems */
1018                       if (!pool->obsoleteusesprovides /* obsoletes are matched names, not provides */
1019                           && !pool_match_nevr(pool, ps, obs))
1020                         continue;
1021                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1022                         continue;
1023                       if (p == SYSTEMSOLVABLE)
1024                         p = 0;
1025                       if (!isinstalled)
1026                         addpkgrule(solv, -n, -p, 0, SOLVER_RULE_PKG_OBSOLETES, obs);
1027                       else
1028                         addpkgrule(solv, -n, -p, 0, SOLVER_RULE_PKG_INSTALLED_OBSOLETES, obs);
1029                     }
1030                 }
1031             }
1032           /* check implicit obsoletes
1033            * for installed packages we only need to check installed/installed problems (and
1034            * only when dontfix is not set), as the others are picked up when looking at the
1035            * uninstalled package.
1036            */
1037           if (!isinstalled || !dontfix)
1038             {
1039               FOR_PROVIDES(p, pp, s->name)
1040                 {
1041                   Solvable *ps = pool->solvables + p;
1042                   if (p == n)
1043                     continue;
1044                   if (isinstalled && ps->repo != installed)
1045                     continue;
1046                   /* we still obsolete packages with same nevra, like rpm does */
1047                   /* (actually, rpm mixes those packages. yuck...) */
1048                   if (multi && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
1049                     {
1050                       if (isinstalled || ps->repo != installed)
1051                         continue;
1052                       /* also check the installed package for multi-ness */
1053                       if (MAPTST(&solv->multiversion, p))
1054                         continue;
1055                     }
1056                   if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
1057                     continue;
1058                   if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
1059                     continue;
1060                   if (p == SYSTEMSOLVABLE)
1061                     p = 0;
1062                   if (s->name == ps->name)
1063                     {
1064                       /* optimization: do not add the same-name conflict rule if it was
1065                        * already added when we looked at the other package.
1066                        * (this assumes pool_colormatch is symmetric) */
1067                       if (p && m && ps->repo != installed && MAPTST(m, p) &&
1068                           (ps->arch != ARCH_SRC && ps->arch != ARCH_NOSRC) &&
1069                           !(solv->multiversion.size && MAPTST(&solv->multiversion, p)))
1070                         continue;
1071                       addpkgrule(solv, -n, -p, 0, SOLVER_RULE_PKG_SAME_NAME, 0);
1072                     }
1073                   else
1074                     addpkgrule(solv, -n, -p, 0, SOLVER_RULE_PKG_IMPLICIT_OBSOLETES, s->name);
1075                 }
1076             }
1077         }
1078
1079       if (m && pool->implicitobsoleteusescolors && (s->arch > pool->lastarch || pool->id2arch[s->arch] != 1))
1080         {
1081           int a = pool->id2arch[s->arch];
1082           /* check lock-step candidates */
1083           FOR_PROVIDES(p, pp, s->name)
1084             {
1085               Solvable *ps = pool->solvables + p;
1086               if (s->name != ps->name || s->evr != ps->evr || MAPTST(m, p))
1087                 continue;
1088               if (ps->arch > pool->lastarch || pool->id2arch[ps->arch] == 1 || pool->id2arch[ps->arch] >= a)
1089                 continue;
1090               queue_push(&workq, p);
1091             }
1092         }
1093
1094       /*-----------------------------------------
1095        * add recommends/suggests to the work queue
1096        */
1097       if (s->recommends && m)
1098         {
1099           recp = s->repo->idarraydata + s->recommends;
1100           while ((rec = *recp++) != 0)
1101             {
1102 #ifdef ENABLE_COMPLEX_DEPS
1103               if (pool_is_complex_dep(pool, rec))
1104                 {
1105                   pool_add_pos_literals_complex_dep(pool, rec, &workq, m, 0);
1106                     continue;
1107                 }
1108 #endif
1109               FOR_PROVIDES(p, pp, rec)
1110                 if (!MAPTST(m, p))
1111                   queue_push(&workq, p);
1112             }
1113         }
1114       if (s->suggests && m)
1115         {
1116           sugp = s->repo->idarraydata + s->suggests;
1117           while ((sug = *sugp++) != 0)
1118             {
1119 #ifdef ENABLE_COMPLEX_DEPS
1120               if (pool_is_complex_dep(pool, sug))
1121                 {
1122                   pool_add_pos_literals_complex_dep(pool, sug, &workq, m, 0);
1123                     continue;
1124                 }
1125 #endif
1126               FOR_PROVIDES(p, pp, sug)
1127                 if (!MAPTST(m, p))
1128                   queue_push(&workq, p);
1129             }
1130         }
1131     }
1132   queue_free(&prereqq);
1133   queue_free(&workq);
1134 }
1135
1136 #ifdef ENABLE_LINKED_PKGS
1137 void
1138 solver_addpkgrulesforlinked(Solver *solv, Map *m)
1139 {
1140   Pool *pool = solv->pool;
1141   Solvable *s;
1142   int i, j;
1143   Queue qr;
1144
1145   queue_init(&qr);
1146   for (i = 1; i < pool->nsolvables; i++)
1147     {
1148       if (MAPTST(m, i))
1149         continue;
1150       s = pool->solvables + i;
1151       if (!s->repo || s->repo == solv->installed)
1152         continue;
1153       if (!strchr(pool_id2str(pool, s->name), ':'))
1154         continue;
1155       if (!pool_installable(pool, s))
1156         continue;
1157       find_package_link(pool, s, 0, &qr, 0, 0);
1158       if (qr.count)
1159         {
1160           for (j = 0; j < qr.count; j++)
1161             if (MAPTST(m, qr.elements[j]))
1162               {
1163                 solver_addpkgrulesforsolvable(solv, s, m);
1164                 break;
1165               }
1166           queue_empty(&qr);
1167         }
1168     }
1169   queue_free(&qr);
1170 }
1171 #endif
1172
1173 /*-------------------------------------------------------------------
1174  *
1175  * Add rules for packages possibly selected in by weak dependencies
1176  *
1177  * m: already added solvables
1178  */
1179
1180 void
1181 solver_addpkgrulesforweak(Solver *solv, Map *m)
1182 {
1183   Pool *pool = solv->pool;
1184   Solvable *s;
1185   Id sup, *supp;
1186   int i, n;
1187
1188   /* foreach solvable in pool */
1189   for (i = n = 1; n < pool->nsolvables; i++, n++)
1190     {
1191       if (i == pool->nsolvables)                /* wrap i */
1192         i = 1;
1193       if (MAPTST(m, i))                         /* already added that one */
1194         continue;
1195
1196       s = pool->solvables + i;
1197       if (!s->repo)
1198         continue;
1199       if (s->repo != pool->installed && !pool_installable(pool, s))
1200         continue;       /* only look at installable ones */
1201
1202       sup = 0;
1203       if (s->supplements)
1204         {
1205           /* find possible supplements */
1206           supp = s->repo->idarraydata + s->supplements;
1207           while ((sup = *supp++) != 0)
1208             if (dep_possible(solv, sup, m))
1209               break;
1210         }
1211
1212       /* if nothing found, check for enhances */
1213       if (!sup && s->enhances)
1214         {
1215           supp = s->repo->idarraydata + s->enhances;
1216           while ((sup = *supp++) != 0)
1217             if (dep_possible(solv, sup, m))
1218               break;
1219         }
1220       /* if nothing found, goto next solvables */
1221       if (!sup)
1222         continue;
1223       solver_addpkgrulesforsolvable(solv, s, m);
1224       n = 0;                    /* check all solvables again because we added solvables to m */
1225     }
1226 }
1227
1228
1229 /*-------------------------------------------------------------------
1230  *
1231  * add package rules for possible updates
1232  *
1233  * s: solvable
1234  * m: map of already visited solvables
1235  * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1236  */
1237
1238 void
1239 solver_addpkgrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1240 {
1241   Pool *pool = solv->pool;
1242   int i;
1243     /* queue and buffer for it */
1244   Queue qs;
1245   Id qsbuf[64];
1246
1247   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1248     /* find update candidates for 's' */
1249   policy_findupdatepackages(solv, s, &qs, allow_all);
1250     /* add rule for 's' if not already done */
1251   if (!MAPTST(m, s - pool->solvables))
1252     solver_addpkgrulesforsolvable(solv, s, m);
1253     /* foreach update candidate, add rule if not already done */
1254   for (i = 0; i < qs.count; i++)
1255     if (!MAPTST(m, qs.elements[i]))
1256       solver_addpkgrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1257   queue_free(&qs);
1258 }
1259
1260
1261 /***********************************************************************
1262  ***
1263  ***  Update/Feature rule part
1264  ***
1265  ***  Those rules make sure an installed package isn't silently deleted
1266  ***
1267  ***/
1268
1269 static int
1270 dup_maykeepinstalled(Solver *solv, Solvable *s)
1271 {
1272   Pool *pool = solv->pool;
1273   Id ip, pp;
1274
1275   if (solv->dupmap.size && MAPTST(&solv->dupmap,  s - pool->solvables))
1276     return 1;
1277   /* is installed identical to a good one? */
1278   FOR_PROVIDES(ip, pp, s->name)
1279     {
1280       Solvable *is = pool->solvables + ip;
1281       if (is->evr != s->evr)
1282         continue;
1283       if (solv->dupmap.size)
1284         {
1285           if (!MAPTST(&solv->dupmap, ip))
1286             continue;
1287         }
1288       else if (is->repo == pool->installed)
1289         continue;
1290       if (solvable_identical(s, is))
1291         return 1;
1292     }
1293   return 0;
1294 }
1295
1296
1297 static Id
1298 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs)
1299 {
1300   Pool *pool = solv->pool;
1301   int i, j;
1302
1303   policy_findupdatepackages(solv, s, qs, 2);
1304   if (qs->count)
1305     {
1306       /* remove installed packages we can't keep */
1307       for (i = j = 0; i < qs->count; i++)
1308         {
1309           Solvable *ns = pool->solvables + qs->elements[i];
1310           if (ns->repo == pool->installed && !dup_maykeepinstalled(solv, ns))
1311             continue;
1312           qs->elements[j++] = qs->elements[i];
1313         }
1314       queue_truncate(qs, j);
1315     }
1316   /* check if it is ok to keep the installed package */
1317   if (dup_maykeepinstalled(solv, s))
1318     return s - pool->solvables;
1319   /* nope, it must be some other package */
1320   return -SYSTEMSOLVABLE;
1321 }
1322
1323 #if 0
1324 /* add packages from the dup repositories to the update candidates
1325  * this isn't needed for the global dup mode as all packages are
1326  * from dup repos in that case */
1327 static void
1328 addduppackages(Solver *solv, Solvable *s, Queue *qs)
1329 {
1330   Queue dupqs;
1331   Id p, dupqsbuf[64];
1332   int i;
1333   int oldnoupdateprovide = solv->noupdateprovide;
1334
1335   queue_init_buffer(&dupqs, dupqsbuf, sizeof(dupqsbuf)/sizeof(*dupqsbuf));
1336   solv->noupdateprovide = 1;
1337   policy_findupdatepackages(solv, s, &dupqs, 2);
1338   solv->noupdateprovide = oldnoupdateprovide;
1339   for (i = 0; i < dupqs.count; i++)
1340     {
1341       p = dupqs.elements[i];
1342       if (MAPTST(&solv->dupmap, p))
1343         queue_pushunique(qs, p);
1344     }
1345   queue_free(&dupqs);
1346 }
1347 #endif
1348
1349 /* stash away the original updaters for multiversion packages. We do this so that
1350  * we can update the package later */
1351 static inline void
1352 set_specialupdaters(Solver *solv, Solvable *s, Id d)
1353 {
1354   Repo *installed = solv->installed;
1355   if (!solv->specialupdaters)
1356     solv->specialupdaters = solv_calloc(installed->end - installed->start, sizeof(Id));
1357   solv->specialupdaters[s - solv->pool->solvables - installed->start] = d;
1358 }
1359
1360 #ifdef ENABLE_LINKED_PKGS
1361 /* Check if this is a linked pseudo package. As it is linked, we do not need an update/feature rule */
1362 static inline int
1363 is_linked_pseudo_package(Solver *solv, Solvable *s)
1364 {
1365   Pool *pool = solv->pool;
1366   if (solv->instbuddy && solv->instbuddy[s - pool->solvables - solv->installed->start])
1367     {
1368       const char *name = pool_id2str(pool, s->name);
1369       if (strncmp(name, "pattern:", 8) == 0 || strncmp(name, "application:", 12) == 0)
1370         return 1;
1371     }
1372   return 0;
1373 }
1374 #endif
1375
1376 void
1377 solver_addfeaturerule(Solver *solv, Solvable *s)
1378 {
1379   Pool *pool = solv->pool;
1380   int i;
1381   Id p;
1382   Queue qs;
1383   Id qsbuf[64];
1384
1385 #ifdef ENABLE_LINKED_PKGS
1386   if (is_linked_pseudo_package(solv, s))
1387     {
1388       solver_addrule(solv, 0, 0, 0);    /* no feature rules for those */
1389       return;
1390     }
1391 #endif
1392   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1393   p = s - pool->solvables;
1394   policy_findupdatepackages(solv, s, &qs, 1);
1395   if (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1396     {
1397       if (!dup_maykeepinstalled(solv, s))
1398         {
1399           for (i = 0; i < qs.count; i++)
1400             {
1401               Solvable *ns = pool->solvables + qs.elements[i];
1402               if (ns->repo != pool->installed || dup_maykeepinstalled(solv, ns))
1403                 break;
1404             }
1405           if (i == qs.count)
1406             {
1407               solver_addrule(solv, 0, 0, 0);    /* this is an orphan */
1408               queue_free(&qs);
1409               return;
1410             }
1411         }
1412     }
1413   if (qs.count > 1)
1414     {
1415       Id d = pool_queuetowhatprovides(pool, &qs);
1416       queue_free(&qs);
1417       solver_addrule(solv, p, 0, d);    /* allow update of s */
1418     }
1419   else
1420     {
1421       Id d = qs.count ? qs.elements[0] : 0;
1422       queue_free(&qs);
1423       solver_addrule(solv, p, d, 0);    /* allow update of s */
1424     }
1425 }
1426
1427 /*-------------------------------------------------------------------
1428  *
1429  * add rule for update
1430  *   (A|A1|A2|A3...)  An = update candidates for A
1431  *
1432  * s = (installed) solvable
1433  */
1434
1435 void
1436 solver_addupdaterule(Solver *solv, Solvable *s)
1437 {
1438   /* installed packages get a special upgrade allowed rule */
1439   Pool *pool = solv->pool;
1440   Id p, d;
1441   Queue qs;
1442   Id qsbuf[64];
1443   int isorphaned = 0;
1444   Rule *r;
1445
1446   p = s - pool->solvables;
1447   /* Orphan detection. We cheat by looking at the feature rule, which
1448    * we already calculated */
1449   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1450   if (!r->p)
1451     {
1452 #ifdef ENABLE_LINKED_PKGS
1453       if (is_linked_pseudo_package(solv, s))
1454         {
1455           solver_addrule(solv, 0, 0, 0);
1456           return;
1457         }
1458 #endif
1459       p = 0;
1460       queue_push(&solv->orphaned, s - pool->solvables);         /* an orphaned package */
1461       if (solv->keep_orphans && !(solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, s - pool->solvables - solv->installed->start))))
1462         p = s - pool->solvables;        /* keep this orphaned package installed */
1463       solver_addrule(solv, p, 0, 0);
1464       return;
1465     }
1466
1467   queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1468   /* find update candidates for 's' */
1469   if (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1470     p = finddistupgradepackages(solv, s, &qs);
1471   else
1472     policy_findupdatepackages(solv, s, &qs, 0);
1473
1474   if (qs.count && solv->multiversion.size)
1475     {
1476       int i, j;
1477
1478       for (i = 0; i < qs.count; i++)
1479         if (MAPTST(&solv->multiversion, qs.elements[i]))
1480           break;
1481       if (i < qs.count)
1482         {
1483           /* filter out all multiversion packages as they don't update */
1484           d = pool_queuetowhatprovides(pool, &qs);      /* save qs away */
1485           for (j = i; i < qs.count; i++)
1486              {
1487               if (MAPTST(&solv->multiversion, qs.elements[i]))
1488                 {
1489                   Solvable *ps = pool->solvables + qs.elements[i];
1490                   /* if keepexplicitobsoletes is set and the name is different,
1491                    * we assume that there is an obsoletes. XXX: not 100% correct */
1492                   if (solv->keepexplicitobsoletes && ps->name != s->name)
1493                     {
1494                       qs.elements[j++] = qs.elements[i];
1495                       continue;
1496                     }
1497                   /* it's ok if they have same nevra */
1498                   if (ps->name != s->name || ps->evr != s->evr || ps->arch != s->arch)
1499                     continue;
1500                 }
1501               qs.elements[j++] = qs.elements[i];
1502             }
1503           if (j < qs.count)             /* filtered at least one package? */
1504             {
1505               if (j == 0 && p == -SYSTEMSOLVABLE)
1506                 {
1507                   /* this is a multiversion orphan */
1508                   queue_push(&solv->orphaned, s - pool->solvables);
1509                   set_specialupdaters(solv, s, d);
1510                   if (solv->keep_orphans && !(solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, s - pool->solvables - solv->installed->start))))
1511                     {
1512                       /* we need to keep the orphan */
1513                       queue_free(&qs);
1514                       solver_addrule(solv, s - pool->solvables, 0, 0);
1515                       return;
1516                     }
1517                   /* we can drop it as long as we update */
1518                   isorphaned = 1;
1519                   j = qs.count;         /* force the update */
1520                 }
1521               else if (d && (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, s - pool->solvables - solv->installed->start))))
1522                 {
1523                   /* non-orphan multiversion package, set special updaters if we want an update */
1524                   set_specialupdaters(solv, s, d);
1525                 }
1526               qs.count = j;
1527             }
1528           else if (p != -SYSTEMSOLVABLE)
1529             {
1530               /* could fallthrough, but then we would do pool_queuetowhatprovides twice */
1531               queue_free(&qs);
1532               solver_addrule(solv, s - pool->solvables, 0, d);  /* allow update of s */
1533               return;
1534             }
1535         }
1536     }
1537   if (!isorphaned && p == -SYSTEMSOLVABLE && qs.count && solv->dupmap.size)
1538     p = s - pool->solvables;            /* let the dup rules sort it out */
1539   if (qs.count && p == -SYSTEMSOLVABLE)
1540     p = queue_shift(&qs);
1541   if (qs.count > 1)
1542     {
1543       d = pool_queuetowhatprovides(pool, &qs);
1544       queue_free(&qs);
1545       solver_addrule(solv, p, 0, d);    /* allow update of s */
1546     }
1547   else
1548     {
1549       d = qs.count ? qs.elements[0] : 0;
1550       queue_free(&qs);
1551       solver_addrule(solv, p, d, 0);    /* allow update of s */
1552     }
1553 }
1554
1555 static inline void
1556 disableupdaterule(Solver *solv, Id p)
1557 {
1558   Rule *r;
1559
1560   MAPSET(&solv->noupdate, p - solv->installed->start);
1561   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1562   if (r->p && r->d >= 0)
1563     solver_disablerule(solv, r);
1564   r = solv->rules + solv->featurerules + (p - solv->installed->start);
1565   if (r->p && r->d >= 0)
1566     solver_disablerule(solv, r);
1567   if (solv->bestrules_pkg)
1568     {
1569       int i, ni;
1570       ni = solv->bestrules_end - solv->bestrules;
1571       for (i = 0; i < ni; i++)
1572         if (solv->bestrules_pkg[i] == p)
1573           solver_disablerule(solv, solv->rules + solv->bestrules + i);
1574     }
1575 }
1576
1577 static inline void
1578 reenableupdaterule(Solver *solv, Id p)
1579 {
1580   Pool *pool = solv->pool;
1581   Rule *r;
1582
1583   MAPCLR(&solv->noupdate, p - solv->installed->start);
1584   r = solv->rules + solv->updaterules + (p - solv->installed->start);
1585   if (r->p)
1586     {
1587       if (r->d < 0)
1588         {
1589           solver_enablerule(solv, r);
1590           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1591             {
1592               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1593               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1594             }
1595         }
1596     }
1597   else
1598     {
1599       r = solv->rules + solv->featurerules + (p - solv->installed->start);
1600       if (r->p && r->d < 0)
1601         {
1602           solver_enablerule(solv, r);
1603           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1604             {
1605               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1606               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1607             }
1608         }
1609     }
1610   if (solv->bestrules_pkg)
1611     {
1612       int i, ni;
1613       ni = solv->bestrules_end - solv->bestrules;
1614       for (i = 0; i < ni; i++)
1615         if (solv->bestrules_pkg[i] == p)
1616           solver_enablerule(solv, solv->rules + solv->bestrules + i);
1617     }
1618 }
1619
1620
1621 /***********************************************************************
1622  ***
1623  ***  Infarch rule part
1624  ***
1625  ***  Infarch rules make sure the solver uses the best architecture of
1626  ***  a package if multiple archetectures are available
1627  ***
1628  ***/
1629
1630 void
1631 solver_addinfarchrules(Solver *solv, Map *addedmap)
1632 {
1633   Pool *pool = solv->pool;
1634   Repo *installed = pool->installed;
1635   int first, i, j;
1636   Id p, pp, a, aa, bestarch;
1637   Solvable *s, *ps, *bests;
1638   Queue badq, allowedarchs;
1639   Queue lsq;
1640
1641   queue_init(&badq);
1642   queue_init(&allowedarchs);
1643   queue_init(&lsq);
1644   solv->infarchrules = solv->nrules;
1645   for (i = 1; i < pool->nsolvables; i++)
1646     {
1647       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
1648         continue;
1649       s = pool->solvables + i;
1650       first = i;
1651       bestarch = 0;
1652       bests = 0;
1653       queue_empty(&allowedarchs);
1654       FOR_PROVIDES(p, pp, s->name)
1655         {
1656           ps = pool->solvables + p;
1657           if (ps->name != s->name || !MAPTST(addedmap, p))
1658             continue;
1659           if (p == i)
1660             first = 0;
1661           if (first)
1662             break;
1663           a = ps->arch;
1664           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1665           if (a != 1 && installed && ps->repo == installed)
1666             {
1667               if (!solv->dupmap_all && !(solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1668                 queue_pushunique(&allowedarchs, ps->arch);      /* also ok to keep this architecture */
1669               continue;         /* ignore installed solvables when calculating the best arch */
1670             }
1671           if (a && a != 1 && (!bestarch || a < bestarch))
1672             {
1673               bestarch = a;
1674               bests = ps;
1675             }
1676         }
1677       if (first)
1678         continue;
1679
1680       /* speed up common case where installed package already has best arch */
1681       if (allowedarchs.count == 1 && bests && allowedarchs.elements[0] == bests->arch)
1682         allowedarchs.count--;   /* installed arch is best */
1683
1684       if (allowedarchs.count && pool->implicitobsoleteusescolors && installed && bestarch)
1685         {
1686           /* need an extra pass for lockstep checking: we only allow to keep an inferior arch
1687            * if the corresponding installed package is not lock-stepped */
1688           queue_empty(&allowedarchs);
1689           FOR_PROVIDES(p, pp, s->name)
1690             {
1691               Id p2, pp2;
1692               ps = pool->solvables + p;
1693               if (ps->name != s->name || ps->repo != installed || !MAPTST(addedmap, p))
1694                 continue;
1695               if (solv->dupmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p)))
1696                 continue;
1697               a = ps->arch;
1698               a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1699               if (!a)
1700                 {
1701                   queue_pushunique(&allowedarchs, ps->arch);    /* strange arch, allow */
1702                   continue;
1703                 }
1704               if (a == 1 || ((a ^ bestarch) & 0xffff0000) == 0)
1705                 continue;
1706               /* have installed package with inferior arch, check if lock-stepped */
1707               FOR_PROVIDES(p2, pp2, s->name)
1708                 {
1709                   Solvable *s2 = pool->solvables + p2;
1710                   Id a2;
1711                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1712                     continue;
1713                   a2 = s2->arch;
1714                   a2 = (a2 <= pool->lastarch) ? pool->id2arch[a2] : 0;
1715                   if (a2 && (a2 == 1 || ((a2 ^ bestarch) & 0xffff0000) == 0))
1716                     break;
1717                 }
1718               if (!p2)
1719                 queue_pushunique(&allowedarchs, ps->arch);
1720             }
1721         }
1722
1723       /* find all bad packages */
1724       queue_empty(&badq);
1725       FOR_PROVIDES(p, pp, s->name)
1726         {
1727           ps = pool->solvables + p;
1728           if (ps->name != s->name || !MAPTST(addedmap, p))
1729             continue;
1730           a = ps->arch;
1731           a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1732           if (a != 1 && bestarch && ((a ^ bestarch) & 0xffff0000) != 0)
1733             {
1734               if (installed && ps->repo == installed)
1735                 {
1736                   if (pool->implicitobsoleteusescolors)
1737                     queue_push(&badq, p);               /* special lock-step handling, see below */
1738                   continue;     /* always ok to keep an installed package */
1739                 }
1740               for (j = 0; j < allowedarchs.count; j++)
1741                 {
1742                   aa = allowedarchs.elements[j];
1743                   if (ps->arch == aa)
1744                     break;
1745                   aa = (aa <= pool->lastarch) ? pool->id2arch[aa] : 0;
1746                   if (aa && ((a ^ aa) & 0xffff0000) == 0)
1747                     break;      /* compatible */
1748                 }
1749               if (j == allowedarchs.count)
1750                 queue_push(&badq, p);
1751             }
1752         }
1753
1754       /* block all solvables in the badq! */
1755       for (j = 0; j < badq.count; j++)
1756         {
1757           p = badq.elements[j];
1758           /* lock-step */
1759           if (pool->implicitobsoleteusescolors)
1760             {
1761               Id p2;
1762               int haveinstalled = 0;
1763               queue_empty(&lsq);
1764               FOR_PROVIDES(p2, pp, s->name)
1765                 {
1766                   Solvable *s2 = pool->solvables + p2;
1767                   if (p2 == p || s2->name != s->name || s2->evr != pool->solvables[p].evr || s2->arch == pool->solvables[p].arch)
1768                     continue;
1769                   a = s2->arch;
1770                   a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
1771                   if (a && (a == 1 || ((a ^ bestarch) & 0xffff000) == 0))
1772                     {
1773                       queue_push(&lsq, p2);
1774                       if (installed && s2->repo == installed)
1775                         haveinstalled = 1;
1776                     }
1777                 }
1778               if (installed && pool->solvables[p].repo == installed && !haveinstalled)
1779                 continue;       /* installed package not in lock-step */
1780             }
1781           if (lsq.count < 2)
1782             solver_addrule(solv, -p, lsq.count ? lsq.elements[0] : 0, 0);
1783           else
1784             solver_addrule(solv, -p, 0, pool_queuetowhatprovides(pool, &lsq));
1785         }
1786     }
1787   queue_free(&lsq);
1788   queue_free(&badq);
1789   queue_free(&allowedarchs);
1790   solv->infarchrules_end = solv->nrules;
1791 }
1792
1793 static inline void
1794 disableinfarchrule(Solver *solv, Id name)
1795 {
1796   Pool *pool = solv->pool;
1797   Rule *r;
1798   int i;
1799   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1800     {
1801       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
1802         solver_disablerule(solv, r);
1803     }
1804 }
1805
1806 static inline void
1807 reenableinfarchrule(Solver *solv, Id name)
1808 {
1809   Pool *pool = solv->pool;
1810   Rule *r;
1811   int i;
1812   for (i = solv->infarchrules, r = solv->rules + i; i < solv->infarchrules_end; i++, r++)
1813     {
1814       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
1815         {
1816           solver_enablerule(solv, r);
1817           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
1818             {
1819               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1820               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
1821             }
1822         }
1823     }
1824 }
1825
1826
1827 /***********************************************************************
1828  ***
1829  ***  Dup rule part
1830  ***
1831  ***  Dup rules make sure a package is selected from the specified dup
1832  ***  repositories if an update candidate is included in one of them.
1833  ***
1834  ***/
1835
1836 static inline void
1837 add_cleandeps_package(Solver *solv, Id p)
1838 {
1839   if (!solv->cleandeps_updatepkgs)
1840     {
1841       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
1842       queue_init(solv->cleandeps_updatepkgs);
1843     }
1844   queue_pushunique(solv->cleandeps_updatepkgs, p);
1845 }
1846
1847 static void
1848 solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
1849 {
1850   Pool *pool = solv->pool;
1851   Solvable *ps, *s = pool->solvables + p;
1852   Repo *installed = solv->installed;
1853   Id pi, pip, obs, *obsp;
1854
1855   MAPSET(&solv->dupinvolvedmap, p);
1856   if (targeted)
1857     MAPSET(&solv->dupmap, p);
1858   FOR_PROVIDES(pi, pip, s->name)
1859     {
1860       ps = pool->solvables + pi;
1861       if (ps->name != s->name)
1862         continue;
1863       MAPSET(&solv->dupinvolvedmap, pi);
1864       if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1865         {
1866           Id *opp, pi2;
1867           for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1868             if (pool->solvables[pi2].repo != installed)
1869               MAPSET(&solv->dupinvolvedmap, pi2);
1870         }
1871       if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1872         {
1873           if (!solv->bestupdatemap.size)
1874             map_grow(&solv->bestupdatemap, installed->end - installed->start);
1875           MAPSET(&solv->bestupdatemap, pi - installed->start);
1876         }
1877       if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1878         add_cleandeps_package(solv, pi);
1879       if (!targeted && ps->repo != installed)
1880         MAPSET(&solv->dupmap, pi);
1881     }
1882   if (s->repo == installed && solv->obsoletes && solv->obsoletes[p - installed->start])
1883     {
1884       Id *opp;
1885       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (pi = *opp++) != 0;)
1886         {
1887           ps = pool->solvables + pi;
1888           if (ps->repo == installed)
1889             continue;
1890           MAPSET(&solv->dupinvolvedmap, pi);
1891           if (!targeted)
1892             MAPSET(&solv->dupmap, pi);
1893         }
1894     }
1895   if (targeted && s->repo != installed && s->obsoletes)
1896     {
1897       /* XXX: check obsoletes/provides combination */
1898       obsp = s->repo->idarraydata + s->obsoletes;
1899       while ((obs = *obsp++) != 0)
1900         {
1901           FOR_PROVIDES(pi, pip, obs)
1902             {
1903               Solvable *ps = pool->solvables + pi;
1904               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
1905                 continue;
1906               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
1907                 continue;
1908               MAPSET(&solv->dupinvolvedmap, pi);
1909               if (targeted && ps->repo == installed && solv->obsoletes && solv->obsoletes[pi - installed->start])
1910                 {
1911                   Id *opp, pi2;
1912                   for (opp = solv->obsoletes_data + solv->obsoletes[pi - installed->start]; (pi2 = *opp++) != 0;)
1913                     if (pool->solvables[pi2].repo != installed)
1914                       MAPSET(&solv->dupinvolvedmap, pi2);
1915                 }
1916               if (ps->repo == installed && (how & SOLVER_FORCEBEST) != 0)
1917                 {
1918                   if (!solv->bestupdatemap.size)
1919                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
1920                   MAPSET(&solv->bestupdatemap, pi - installed->start);
1921                 }
1922               if (ps->repo == installed && (how & SOLVER_CLEANDEPS) != 0)
1923                 add_cleandeps_package(solv, pi);
1924             }
1925         }
1926     }
1927 }
1928
1929 void
1930 solver_createdupmaps(Solver *solv)
1931 {
1932   Queue *job = &solv->job;
1933   Pool *pool = solv->pool;
1934   Repo *installed = solv->installed;
1935   Id select, how, what, p, pp;
1936   Solvable *s;
1937   int i, targeted;
1938
1939   map_init(&solv->dupmap, pool->nsolvables);
1940   map_init(&solv->dupinvolvedmap, pool->nsolvables);
1941   for (i = 0; i < job->count; i += 2)
1942     {
1943       how = job->elements[i];
1944       select = job->elements[i] & SOLVER_SELECTMASK;
1945       what = job->elements[i + 1];
1946       switch (how & SOLVER_JOBMASK)
1947         {
1948         case SOLVER_DISTUPGRADE:
1949           if (select == SOLVER_SOLVABLE_REPO)
1950             {
1951               Repo *repo;
1952               if (what <= 0 || what > pool->nrepos)
1953                 break;
1954               repo = pool_id2repo(pool, what);
1955               if (!repo)
1956                 break;
1957               if (repo != installed && !(how & SOLVER_TARGETED) && solv->noautotarget)
1958                 break;
1959               targeted = repo != installed || (how & SOLVER_TARGETED) != 0;
1960               FOR_REPO_SOLVABLES(repo, p, s)
1961                 {
1962                   if (repo != installed && !pool_installable(pool, s))
1963                     continue;
1964                   solver_addtodupmaps(solv, p, how, targeted);
1965                 }
1966             }
1967           else if (select == SOLVER_SOLVABLE_ALL)
1968             {
1969               FOR_POOL_SOLVABLES(p)
1970                 {
1971                   MAPSET(&solv->dupinvolvedmap, p);
1972                   if (installed && pool->solvables[p].repo != installed)
1973                     MAPSET(&solv->dupmap, p);
1974                 }
1975             }
1976           else
1977             {
1978               targeted = how & SOLVER_TARGETED ? 1 : 0;
1979               if (installed && !targeted && !solv->noautotarget)
1980                 {
1981                   FOR_JOB_SELECT(p, pp, select, what)
1982                     if (pool->solvables[p].repo == installed)
1983                       break;
1984                   targeted = p == 0;
1985                 }
1986               else if (!installed && !solv->noautotarget)
1987                 targeted = 1;
1988               FOR_JOB_SELECT(p, pp, select, what)
1989                 {
1990                   Solvable *s = pool->solvables + p;
1991                   if (!s->repo)
1992                     continue;
1993                   if (s->repo != installed && !targeted)
1994                     continue;
1995                   if (s->repo != installed && !pool_installable(pool, s))
1996                     continue;
1997                   solver_addtodupmaps(solv, p, how, targeted);
1998                 }
1999             }
2000           break;
2001         default:
2002           break;
2003         }
2004     }
2005   MAPCLR(&solv->dupinvolvedmap, SYSTEMSOLVABLE);
2006 }
2007
2008 void
2009 solver_freedupmaps(Solver *solv)
2010 {
2011   map_free(&solv->dupmap);
2012   /* we no longer free solv->dupinvolvedmap as we need it in
2013    * policy's priority pruning code. sigh. */
2014 }
2015
2016 void
2017 solver_addduprules(Solver *solv, Map *addedmap)
2018 {
2019   Pool *pool = solv->pool;
2020   Repo *installed = solv->installed;
2021   Id p, pp;
2022   Solvable *s, *ps;
2023   int first, i;
2024   Rule *r;
2025
2026   solv->duprules = solv->nrules;
2027   for (i = 1; i < pool->nsolvables; i++)
2028     {
2029       if (i == SYSTEMSOLVABLE || !MAPTST(addedmap, i))
2030         continue;
2031       s = pool->solvables + i;
2032       first = i;
2033       FOR_PROVIDES(p, pp, s->name)
2034         {
2035           ps = pool->solvables + p;
2036           if (ps->name != s->name || !MAPTST(addedmap, p))
2037             continue;
2038           if (p == i)
2039             first = 0;
2040           if (first)
2041             break;
2042           if (!MAPTST(&solv->dupinvolvedmap, p))
2043             continue;
2044           if (installed && ps->repo == installed)
2045             {
2046               if (!solv->updatemap.size)
2047                 map_grow(&solv->updatemap, installed->end - installed->start);
2048               MAPSET(&solv->updatemap, p - installed->start);
2049               if (!MAPTST(&solv->dupmap, p))
2050                 {
2051                   Id ip, ipp;
2052                   /* is installed identical to a good one? */
2053                   FOR_PROVIDES(ip, ipp, ps->name)
2054                     {
2055                       Solvable *is = pool->solvables + ip;
2056                       if (!MAPTST(&solv->dupmap, ip))
2057                         continue;
2058                       if (is->evr == ps->evr && solvable_identical(ps, is))
2059                         break;
2060                     }
2061                   if (ip)
2062                     {
2063                       /* ok, found a good one. we may keep this package. */
2064                       MAPSET(&solv->dupmap, p);         /* for best rules processing */
2065                       continue;
2066                     }
2067                   r = solv->rules + solv->updaterules + (p - installed->start);
2068                   if (!r->p)
2069                       r = solv->rules + solv->featurerules + (p - installed->start);
2070                   if (r->p && solv->specialupdaters && solv->specialupdaters[p - installed->start])
2071                     {
2072                       /* this is a multiversion orphan, we're good if an update is installed */
2073                       solver_addrule(solv, -p, 0, solv->specialupdaters[p - installed->start]);
2074                       continue;
2075                     }
2076                   if (!r->p || (r->p == p && !r->d && !r->w2))
2077                     {
2078                       /* this is an orphan */
2079                       MAPSET(&solv->dupmap, p);         /* for best rules processing */
2080                       continue;
2081                     }
2082                   solver_addrule(solv, -p, 0, 0);       /* no match, sorry */
2083                 }
2084             }
2085           else if (!MAPTST(&solv->dupmap, p))
2086             solver_addrule(solv, -p, 0, 0);
2087         }
2088     }
2089   solv->duprules_end = solv->nrules;
2090 }
2091
2092
2093 static inline void
2094 disableduprule(Solver *solv, Id name)
2095 {
2096   Pool *pool = solv->pool;
2097   Rule *r;
2098   int i;
2099   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
2100     {
2101       if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
2102         solver_disablerule(solv, r);
2103     }
2104 }
2105
2106 static inline void
2107 reenableduprule(Solver *solv, Id name)
2108 {
2109   Pool *pool = solv->pool;
2110   Rule *r;
2111   int i;
2112   for (i = solv->duprules, r = solv->rules + i; i < solv->duprules_end; i++, r++)
2113     {
2114       if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
2115         {
2116           solver_enablerule(solv, r);
2117           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
2118             {
2119               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
2120               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
2121             }
2122         }
2123     }
2124 }
2125
2126
2127 /***********************************************************************
2128  ***
2129  ***  Policy rule disabling/reenabling
2130  ***
2131  ***  Disable all policy rules that conflict with our jobs. If a job
2132  ***  gets disabled later on, reenable the involved policy rules again.
2133  ***
2134  ***/
2135
2136 #define DISABLE_UPDATE  1
2137 #define DISABLE_INFARCH 2
2138 #define DISABLE_DUP     3
2139
2140 /*
2141  * add all installed packages that package p obsoletes to Queue q.
2142  * Package p is not installed. Also, we know that if
2143  * solv->keepexplicitobsoletes is not set, p is not in the multiversion map.
2144  * Entries may get added multiple times.
2145  */
2146 static void
2147 add_obsoletes(Solver *solv, Id p, Queue *q)
2148 {
2149   Pool *pool = solv->pool;
2150   Repo *installed = solv->installed;
2151   Id p2, pp2;
2152   Solvable *s = pool->solvables + p;
2153   Id obs, *obsp;
2154   Id lastp2 = 0;
2155
2156   if (!solv->keepexplicitobsoletes || !(solv->multiversion.size && MAPTST(&solv->multiversion, p)))
2157     {
2158       FOR_PROVIDES(p2, pp2, s->name)
2159         {
2160           Solvable *ps = pool->solvables + p2;
2161           if (ps->repo != installed)
2162             continue;
2163           if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
2164             continue;
2165           if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
2166             continue;
2167           queue_push(q, p2);
2168           lastp2 = p2;
2169         }
2170     }
2171   if (!s->obsoletes)
2172     return;
2173   obsp = s->repo->idarraydata + s->obsoletes;
2174   while ((obs = *obsp++) != 0)
2175     FOR_PROVIDES(p2, pp2, obs)
2176       {
2177         Solvable *ps = pool->solvables + p2;
2178         if (ps->repo != installed)
2179           continue;
2180         if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
2181           continue;
2182         if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
2183           continue;
2184         if (p2 == lastp2)
2185           continue;
2186         queue_push(q, p2);
2187         lastp2 = p2;
2188       }
2189 }
2190
2191 /*
2192  * Call add_obsoletes and intersect the result with the
2193  * elements in Queue q starting at qstart.
2194  * Assumes that it's the first call if qstart == q->count.
2195  * May use auxillary map m for the intersection process, all
2196  * elements of q starting at qstart must have their bit cleared.
2197  * (This is also true after the function returns.)
2198  */
2199 static void
2200 intersect_obsoletes(Solver *solv, Id p, Queue *q, int qstart, Map *m)
2201 {
2202   int i, j;
2203   int qcount = q->count;
2204
2205   add_obsoletes(solv, p, q);
2206   if (qcount == qstart)
2207     return;     /* first call */
2208   if (qcount == q->count)
2209     j = qstart; 
2210   else if (qcount == qstart + 1)
2211     {
2212       /* easy if there's just one element */
2213       j = qstart;
2214       for (i = qcount; i < q->count; i++)
2215         if (q->elements[i] == q->elements[qstart])
2216           {
2217             j++;        /* keep the element */
2218             break;
2219           }
2220     }
2221   else if (!m->size && q->count - qstart <= 8)
2222     {
2223       /* faster than a map most of the time */
2224       int k;
2225       for (i = j = qstart; i < qcount; i++)
2226         {
2227           Id ip = q->elements[i];
2228           for (k = qcount; k < q->count; k++)
2229             if (q->elements[k] == ip)
2230               {
2231                 q->elements[j++] = ip;
2232                 break;
2233               }
2234         }
2235     }
2236   else
2237     {
2238       /* for the really pathologic cases we use the map */
2239       Repo *installed = solv->installed;
2240       if (!m->size)
2241         map_init(m, installed->end - installed->start);
2242       for (i = qcount; i < q->count; i++)
2243         MAPSET(m, q->elements[i] - installed->start);
2244       for (i = j = qstart; i < qcount; i++)
2245         if (MAPTST(m, q->elements[i] - installed->start))
2246           {
2247             MAPCLR(m, q->elements[i] - installed->start);
2248             q->elements[j++] = q->elements[i];
2249           }
2250     }
2251   queue_truncate(q, j);
2252 }
2253
2254 static void
2255 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
2256 {
2257   Pool *pool = solv->pool;
2258   Id select, p, pp;
2259   Repo *installed;
2260   Solvable *s;
2261   int i, j, set, qstart;
2262   Map omap;
2263
2264   installed = solv->installed;
2265   select = how & SOLVER_SELECTMASK;
2266   switch (how & SOLVER_JOBMASK)
2267     {
2268     case SOLVER_INSTALL:
2269       set = how & SOLVER_SETMASK;
2270       if (!(set & SOLVER_NOAUTOSET))
2271         {
2272           /* automatically add set bits by analysing the job */
2273           if (select == SOLVER_SOLVABLE_NAME)
2274             set |= SOLVER_SETNAME;
2275           if (select == SOLVER_SOLVABLE)
2276             set |= SOLVER_SETNAME | SOLVER_SETARCH | SOLVER_SETVENDOR | SOLVER_SETREPO | SOLVER_SETEVR;
2277           else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
2278             {
2279               Reldep *rd = GETRELDEP(pool, what);
2280               if (rd->flags == REL_EQ && select == SOLVER_SOLVABLE_NAME)
2281                 {
2282                   if (pool->disttype != DISTTYPE_DEB)
2283                     {
2284                       const char *rel = strrchr(pool_id2str(pool, rd->evr), '-');
2285                       set |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
2286                     }
2287                   else
2288                     set |= SOLVER_SETEVR;
2289                 }
2290               if (rd->flags <= 7 && ISRELDEP(rd->name))
2291                 rd = GETRELDEP(pool, rd->name);
2292               if (rd->flags == REL_ARCH)
2293                 set |= SOLVER_SETARCH;
2294             }
2295         }
2296       else
2297         set &= ~SOLVER_NOAUTOSET;
2298       if (!set)
2299         return;
2300       if ((set & SOLVER_SETARCH) != 0 && solv->infarchrules != solv->infarchrules_end)
2301         {
2302           if (select == SOLVER_SOLVABLE)
2303             queue_push2(q, DISABLE_INFARCH, pool->solvables[what].name);
2304           else
2305             {
2306               int qcnt = q->count;
2307               /* does not work for SOLVER_SOLVABLE_ALL and SOLVER_SOLVABLE_REPO, but
2308                  they are not useful for SOLVER_INSTALL jobs anyway */
2309               FOR_JOB_SELECT(p, pp, select, what)
2310                 {
2311                   s = pool->solvables + p;
2312                   /* unify names */
2313                   for (i = qcnt; i < q->count; i += 2)
2314                     if (q->elements[i + 1] == s->name)
2315                       break;
2316                   if (i < q->count)
2317                     continue;
2318                   queue_push2(q, DISABLE_INFARCH, s->name);
2319                 }
2320             }
2321         }
2322       if ((set & SOLVER_SETREPO) != 0 && solv->duprules != solv->duprules_end)
2323         {
2324           if (select == SOLVER_SOLVABLE)
2325             queue_push2(q, DISABLE_DUP, pool->solvables[what].name);
2326           else
2327             {
2328               int qcnt = q->count;
2329               FOR_JOB_SELECT(p, pp, select, what)
2330                 {
2331                   s = pool->solvables + p;
2332                   /* unify names */
2333                   for (i = qcnt; i < q->count; i += 2)
2334                     if (q->elements[i + 1] == s->name)
2335                       break;
2336                   if (i < q->count)
2337                     continue;
2338                   queue_push2(q, DISABLE_DUP, s->name);
2339                 }
2340             }
2341         }
2342       if (!installed || installed->end == installed->start)
2343         return;
2344       /* now the hard part: disable some update rules */
2345
2346       /* first check if we have multiversion or installed packages in the job */
2347       i = j = 0;
2348       FOR_JOB_SELECT(p, pp, select, what)
2349         {
2350           if (pool->solvables[p].repo == installed)
2351             j = p;
2352           else if (solv->multiversion.size && MAPTST(&solv->multiversion, p) && !solv->keepexplicitobsoletes)
2353             return;
2354           i++;
2355         }
2356       if (j)    /* have installed packages */
2357         {
2358           /* this is for dupmap_all jobs, it can go away if we create
2359            * duprules for them */
2360           if (i == 1 && (set & SOLVER_SETREPO) != 0)
2361             queue_push2(q, DISABLE_UPDATE, j);
2362           return;
2363         }
2364
2365       omap.size = 0;
2366       qstart = q->count;
2367       FOR_JOB_SELECT(p, pp, select, what)
2368         {
2369           intersect_obsoletes(solv, p, q, qstart, &omap);
2370           if (q->count == qstart)
2371             break;
2372         }
2373       if (omap.size)
2374         map_free(&omap);
2375
2376       if (qstart == q->count)
2377         return;         /* nothing to prune */
2378
2379       /* convert result to (DISABLE_UPDATE, p) pairs */
2380       i = q->count;
2381       for (j = qstart; j < i; j++)
2382         queue_push(q, q->elements[j]);
2383       for (j = qstart; j < q->count; j += 2)
2384         {
2385           q->elements[j] = DISABLE_UPDATE;
2386           q->elements[j + 1] = q->elements[i++];
2387         }
2388
2389       /* now that we know which installed packages are obsoleted check each of them */
2390       if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
2391         return;         /* all is set, nothing to do */
2392
2393       for (i = j = qstart; i < q->count; i += 2)
2394         {
2395           Solvable *is = pool->solvables + q->elements[i + 1];
2396           FOR_JOB_SELECT(p, pp, select, what)
2397             {
2398               int illegal = 0;
2399               s = pool->solvables + p;
2400               if ((set & SOLVER_SETEVR) != 0)
2401                 illegal |= POLICY_ILLEGAL_DOWNGRADE;    /* ignore */
2402               if ((set & SOLVER_SETNAME) != 0)
2403                 illegal |= POLICY_ILLEGAL_NAMECHANGE;   /* ignore */
2404               if ((set & SOLVER_SETARCH) != 0)
2405                 illegal |= POLICY_ILLEGAL_ARCHCHANGE;   /* ignore */
2406               if ((set & SOLVER_SETVENDOR) != 0)
2407                 illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore */
2408               illegal = policy_is_illegal(solv, is, s, illegal);
2409               if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
2410                 {
2411                   /* it's ok if the EV is different */
2412                   if (pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
2413                     illegal = 0;
2414                 }
2415               if (illegal)
2416                 break;
2417             }
2418           if (!p)
2419             {   
2420               /* no package conflicts with the update rule */
2421               /* thus keep the DISABLE_UPDATE */
2422               q->elements[j + 1] = q->elements[i + 1];
2423               j += 2;
2424             }
2425         }
2426       queue_truncate(q, j);
2427       return;
2428
2429     case SOLVER_ERASE:
2430       if (!installed)
2431         break;
2432       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
2433         {
2434           FOR_REPO_SOLVABLES(installed, p, s)
2435             queue_push2(q, DISABLE_UPDATE, p);
2436         }
2437       FOR_JOB_SELECT(p, pp, select, what)
2438         if (pool->solvables[p].repo == installed)
2439           {
2440             queue_push2(q, DISABLE_UPDATE, p);
2441 #ifdef ENABLE_LINKED_PKGS
2442             if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
2443               queue_push2(q, DISABLE_UPDATE, solv->instbuddy[p - installed->start]);
2444 #endif
2445           }
2446       return;
2447     default:
2448       return;
2449     }
2450 }
2451
2452 /* disable all policy rules that are in conflict with our job list */
2453 void
2454 solver_disablepolicyrules(Solver *solv)
2455 {
2456   Queue *job = &solv->job;
2457   int i, j;
2458   Queue allq;
2459   Rule *r;
2460   Id lastjob = -1;
2461   Id allqbuf[128];
2462
2463   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2464
2465   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2466     {
2467       r = solv->rules + i;
2468       if (r->d < 0)     /* disabled? */
2469         continue;
2470       j = solv->ruletojob.elements[i - solv->jobrules];
2471       if (j == lastjob)
2472         continue;
2473       lastjob = j;
2474       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2475     }
2476   if (solv->cleandepsmap.size)
2477     {
2478       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2479       for (i = solv->installed->start; i < solv->installed->end; i++)
2480         if (MAPTST(&solv->cleandepsmap, i - solv->installed->start))
2481           queue_push2(&allq, DISABLE_UPDATE, i);
2482     }
2483   MAPZERO(&solv->noupdate);
2484   for (i = 0; i < allq.count; i += 2)
2485     {
2486       Id type = allq.elements[i], arg = allq.elements[i + 1];
2487       switch(type)
2488         {
2489         case DISABLE_UPDATE:
2490           disableupdaterule(solv, arg);
2491           break;
2492         case DISABLE_INFARCH:
2493           disableinfarchrule(solv, arg);
2494           break;
2495         case DISABLE_DUP:
2496           disableduprule(solv, arg);
2497           break;
2498         default:
2499           break;
2500         }
2501     }
2502   queue_free(&allq);
2503 }
2504
2505 /* we just disabled job #jobidx, now reenable all policy rules that were
2506  * disabled because of this job */
2507 void
2508 solver_reenablepolicyrules(Solver *solv, int jobidx)
2509 {
2510   Queue *job = &solv->job;
2511   int i, j, k, ai;
2512   Queue q, allq;
2513   Rule *r;
2514   Id lastjob = -1;
2515   Id qbuf[32], allqbuf[32];
2516
2517   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2518   jobtodisablelist(solv, job->elements[jobidx - 1], job->elements[jobidx], &q);
2519   if (!q.count)
2520     {
2521       queue_free(&q);
2522       return;
2523     }
2524   /* now remove everything from q that is disabled by other jobs */
2525
2526   /* first remove cleandeps packages, they count as DISABLE_UPDATE */
2527   if (solv->cleandepsmap.size)
2528     {
2529       solver_createcleandepsmap(solv, &solv->cleandepsmap, 0);
2530       for (j = k = 0; j < q.count; j += 2)
2531         {
2532           if (q.elements[j] == DISABLE_UPDATE)
2533             {
2534               Id p = q.elements[j + 1];
2535               if (p >= solv->installed->start && p < solv->installed->end && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2536                 continue;       /* remove element from q */
2537             }
2538           q.elements[k++] = q.elements[j];
2539           q.elements[k++] = q.elements[j + 1];
2540         }
2541       q.count = k;
2542       if (!q.count)
2543         {
2544           queue_free(&q);
2545           return;
2546         }
2547     }
2548
2549   /* now go through the disable list of all other jobs */
2550   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2551   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2552     {
2553       r = solv->rules + i;
2554       if (r->d < 0)     /* disabled? */
2555         continue;
2556       j = solv->ruletojob.elements[i - solv->jobrules];
2557       if (j == lastjob)
2558         continue;
2559       lastjob = j;
2560       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2561       if (!allq.count)
2562         continue;
2563       /* remove all elements in allq from q */
2564       for (j = k = 0; j < q.count; j += 2)
2565         {
2566           Id type = q.elements[j], arg = q.elements[j + 1];
2567           for (ai = 0; ai < allq.count; ai += 2)
2568             if (allq.elements[ai] == type && allq.elements[ai + 1] == arg)
2569               break;
2570           if (ai < allq.count)
2571             continue;   /* found it in allq, remove element from q */
2572           q.elements[k++] = q.elements[j];
2573           q.elements[k++] = q.elements[j + 1];
2574         }
2575       q.count = k;
2576       if (!q.count)
2577         {
2578           queue_free(&q);
2579           queue_free(&allq);
2580           return;
2581         }
2582       queue_empty(&allq);
2583     }
2584   queue_free(&allq);
2585
2586   /* now re-enable anything that's left in q */
2587   for (j = 0; j < q.count; j += 2)
2588     {
2589       Id type = q.elements[j], arg = q.elements[j + 1];
2590       switch(type)
2591         {
2592         case DISABLE_UPDATE:
2593           reenableupdaterule(solv, arg);
2594           break;
2595         case DISABLE_INFARCH:
2596           reenableinfarchrule(solv, arg);
2597           break;
2598         case DISABLE_DUP:
2599           reenableduprule(solv, arg);
2600           break;
2601         }
2602     }
2603   queue_free(&q);
2604 }
2605
2606 /* we just removed a package from the cleandeps map, now reenable all policy rules that were
2607  * disabled because of this */
2608 void
2609 solver_reenablepolicyrules_cleandeps(Solver *solv, Id pkg)
2610 {
2611   Queue *job = &solv->job;
2612   int i, j;
2613   Queue allq;
2614   Rule *r;
2615   Id lastjob = -1;
2616   Id allqbuf[128];
2617
2618   queue_init_buffer(&allq, allqbuf, sizeof(allqbuf)/sizeof(*allqbuf));
2619   for (i = solv->jobrules; i < solv->jobrules_end; i++)
2620     {
2621       r = solv->rules + i;
2622       if (r->d < 0)     /* disabled? */
2623         continue;
2624       j = solv->ruletojob.elements[i - solv->jobrules];
2625       if (j == lastjob)
2626         continue;
2627       lastjob = j;
2628       jobtodisablelist(solv, job->elements[j], job->elements[j + 1], &allq);
2629     }
2630   for (i = 0; i < allq.count; i += 2)
2631     if (allq.elements[i] == DISABLE_UPDATE && allq.elements[i + 1] == pkg)
2632       break;
2633   if (i == allq.count)
2634     reenableupdaterule(solv, pkg);
2635   queue_free(&allq);
2636 }
2637
2638
2639 /***********************************************************************
2640  ***
2641  ***  Rule info part, tell the user what the rule is about.
2642  ***
2643  ***/
2644
2645 static void
2646 addpkgruleinfo(Solver *solv, Id p, Id p2, Id d, int type, Id dep)
2647 {
2648   Pool *pool = solv->pool;
2649   Rule *r;
2650
2651   if (d)
2652     {
2653       assert(!p2 && d > 0);
2654       if (!pool->whatprovidesdata[d])
2655         d = 0;
2656       else if (!pool->whatprovidesdata[d + 1])
2657         {
2658           p2 = pool->whatprovidesdata[d];
2659           d = 0;
2660         }
2661     }
2662
2663   /* check if this creates the rule we're searching for */
2664   r = solv->rules + solv->ruleinfoq->elements[0];
2665   if (d)
2666     {
2667       /* three or more literals */
2668       Id od = r->d < 0 ? -r->d - 1 : r->d;
2669       if (p != r->p && !od)
2670         return;
2671       if (d != od)
2672         {
2673           Id *dp = pool->whatprovidesdata + d;
2674           Id *odp = pool->whatprovidesdata + od;
2675           while (*dp)
2676             if (*dp++ != *odp++)
2677               return;
2678           if (*odp)
2679             return;
2680         }
2681       if (p < 0 && pool->whatprovidesdata[d] < 0 && type == SOLVER_RULE_PKG_CONFLICTS)
2682         p2 = pool->whatprovidesdata[d];
2683     }
2684   else
2685     {
2686       /* one or two literals */
2687       Id op = p, op2 = p2;
2688       if (op2 && op > op2)      /* normalize */
2689         {
2690           Id o = op;
2691           op = op2;
2692           op2 = o;
2693         }
2694       if (r->p != op || r->w2 != op2 || (r->d && r->d != -1))
2695         return;
2696       if (type == SOLVER_RULE_PKG_CONFLICTS && !p2)
2697         p2 = -SYSTEMSOLVABLE;
2698       if (type == SOLVER_RULE_PKG_SAME_NAME)
2699         {
2700           p = op;       /* we normalize same name order */
2701           p2 = op2;
2702         }
2703     }
2704   /* yep, rule matches. record info */
2705   queue_push(solv->ruleinfoq, type);
2706   queue_push(solv->ruleinfoq, p < 0 ? -p : 0);
2707   queue_push(solv->ruleinfoq, p2 < 0 ? -p2 : 0);
2708   queue_push(solv->ruleinfoq, dep);
2709 }
2710
2711 static int
2712 solver_allruleinfos_cmp(const void *ap, const void *bp, void *dp)
2713 {
2714   const Id *a = ap, *b = bp;
2715   int r;
2716
2717   r = a[0] - b[0];
2718   if (r)
2719     return r;
2720   r = a[1] - b[1];
2721   if (r)
2722     return r;
2723   r = a[2] - b[2];
2724   if (r)
2725     return r;
2726   r = a[3] - b[3];
2727   if (r)
2728     return r;
2729   return 0;
2730 }
2731
2732 static void
2733 getpkgruleinfos(Solver *solv, Rule *r, Queue *rq)
2734 {
2735   Pool *pool = solv->pool;
2736   Id l, pp;
2737   if (r->p >= 0)
2738     return;
2739   queue_push(rq, r - solv->rules);      /* push the rule we're interested in */
2740   solv->ruleinfoq = rq;
2741   FOR_RULELITERALS(l, pp, r)
2742     {
2743       if (l >= 0)
2744         break;
2745       solver_addpkgrulesforsolvable(solv, pool->solvables - l, 0);
2746     }
2747 #ifdef ENABLE_LINKED_PKGS
2748   FOR_RULELITERALS(l, pp, r)
2749     {
2750       if (l < 0)
2751         {
2752           if (l == r->p)
2753             continue;
2754           break;
2755         }
2756       if (!strchr(pool_id2str(pool, pool->solvables[l].name), ':') || !has_package_link(pool, pool->solvables + l))
2757         break;
2758       add_package_link(solv, pool->solvables + l, 0, 0);
2759     }
2760 #endif
2761   solv->ruleinfoq = 0;
2762   queue_shift(rq);
2763 }
2764
2765 int
2766 solver_allruleinfos(Solver *solv, Id rid, Queue *rq)
2767 {
2768   Rule *r = solv->rules + rid;
2769   int i, j;
2770
2771   queue_empty(rq);
2772   if (rid <= 0 || rid >= solv->pkgrules_end)
2773     {
2774       Id type, from, to, dep;
2775       type = solver_ruleinfo(solv, rid, &from, &to, &dep);
2776       queue_push(rq, type);
2777       queue_push(rq, from);
2778       queue_push(rq, to);
2779       queue_push(rq, dep);
2780       return 1;
2781     }
2782   getpkgruleinfos(solv, r, rq);
2783   /* now sort & unify em */
2784   if (!rq->count)
2785     return 0;
2786   solv_sort(rq->elements, rq->count / 4, 4 * sizeof(Id), solver_allruleinfos_cmp, 0);
2787   /* throw out identical entries */
2788   for (i = j = 0; i < rq->count; i += 4)
2789     {
2790       if (j)
2791         {
2792           if (rq->elements[i] == rq->elements[j - 4] &&
2793               rq->elements[i + 1] == rq->elements[j - 3] &&
2794               rq->elements[i + 2] == rq->elements[j - 2] &&
2795               rq->elements[i + 3] == rq->elements[j - 1])
2796             continue;
2797         }
2798       rq->elements[j++] = rq->elements[i];
2799       rq->elements[j++] = rq->elements[i + 1];
2800       rq->elements[j++] = rq->elements[i + 2];
2801       rq->elements[j++] = rq->elements[i + 3];
2802     }
2803   rq->count = j;
2804   return j / 4;
2805 }
2806
2807 SolverRuleinfo
2808 solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
2809 {
2810   Pool *pool = solv->pool;
2811   Rule *r = solv->rules + rid;
2812   SolverRuleinfo type = SOLVER_RULE_UNKNOWN;
2813
2814   if (fromp)
2815     *fromp = 0;
2816   if (top)
2817     *top = 0;
2818   if (depp)
2819     *depp = 0;
2820   if (rid > 0 && rid < solv->pkgrules_end)
2821     {
2822       Queue rq;
2823       int i;
2824
2825       if (r->p >= 0)
2826         return SOLVER_RULE_PKG;
2827       if (fromp)
2828         *fromp = -r->p;
2829       queue_init(&rq);
2830       getpkgruleinfos(solv, r, &rq);
2831       type = SOLVER_RULE_PKG;
2832       for (i = 0; i < rq.count; i += 4)
2833         {
2834           Id qt, qo, qp, qd;
2835           qt = rq.elements[i];
2836           qp = rq.elements[i + 1];
2837           qo = rq.elements[i + 2];
2838           qd = rq.elements[i + 3];
2839           if (type == SOLVER_RULE_PKG || type > qt)
2840             {
2841               type = qt;
2842               if (fromp)
2843                 *fromp = qp;
2844               if (top)
2845                 *top = qo;
2846               if (depp)
2847                 *depp = qd;
2848             }
2849         }
2850       queue_free(&rq);
2851       return type;
2852     }
2853   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2854     {
2855       Id jidx = solv->ruletojob.elements[rid - solv->jobrules];
2856       if (fromp)
2857         *fromp = jidx;
2858       if (top)
2859         *top = solv->job.elements[jidx];
2860       if (depp)
2861         *depp = solv->job.elements[jidx + 1];
2862       if ((r->d == 0 || r->d == -1) && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2863         {
2864           Id how = solv->job.elements[jidx];
2865           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME))
2866             return SOLVER_RULE_JOB_UNKNOWN_PACKAGE;
2867           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES))
2868             return SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP;
2869           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_NAME))
2870             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2871           if ((how & (SOLVER_JOBMASK|SOLVER_SELECTMASK)) == (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES))
2872             return SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM;
2873           return SOLVER_RULE_JOB_UNSUPPORTED;
2874         }
2875       return SOLVER_RULE_JOB;
2876     }
2877   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2878     {
2879       if (fromp)
2880         *fromp = solv->installed->start + (rid - solv->updaterules);
2881       return SOLVER_RULE_UPDATE;
2882     }
2883   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2884     {
2885       if (fromp)
2886         *fromp = solv->installed->start + (rid - solv->featurerules);
2887       return SOLVER_RULE_FEATURE;
2888     }
2889   if (rid >= solv->duprules && rid < solv->duprules_end)
2890     {
2891       if (fromp)
2892         *fromp = -r->p;
2893       if (depp)
2894         *depp = pool->solvables[-r->p].name;
2895       return SOLVER_RULE_DISTUPGRADE;
2896     }
2897   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2898     {
2899       if (fromp)
2900         *fromp = -r->p;
2901       if (depp)
2902         *depp = pool->solvables[-r->p].name;
2903       return SOLVER_RULE_INFARCH;
2904     }
2905   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2906     {
2907       if (fromp && solv->bestrules_pkg[rid - solv->bestrules] > 0)
2908         *fromp = solv->bestrules_pkg[rid - solv->bestrules];
2909       return SOLVER_RULE_BEST;
2910     }
2911   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
2912     {
2913       if (fromp)
2914         *fromp = -r->p;
2915       if (top)
2916         {
2917           /* first solvable is enough, we just need it for the name */
2918           if (!r->d || r->d == -1)
2919             *top = r->w2;
2920           else
2921             *top = pool->whatprovidesdata[r->d < 0 ? -r->d : r->d];
2922         }
2923       if (depp)
2924         *depp = solv->yumobsrules_info[rid - solv->yumobsrules];
2925       return SOLVER_RULE_YUMOBS;
2926     }
2927   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2928     {
2929       return SOLVER_RULE_CHOICE;
2930     }
2931   if (rid >= solv->learntrules)
2932     {
2933       return SOLVER_RULE_LEARNT;
2934     }
2935   return SOLVER_RULE_UNKNOWN;
2936 }
2937
2938 SolverRuleinfo
2939 solver_ruleclass(Solver *solv, Id rid)
2940 {
2941   if (rid <= 0)
2942     return SOLVER_RULE_UNKNOWN;
2943   if (rid > 0 && rid < solv->pkgrules_end)
2944     return SOLVER_RULE_PKG;
2945   if (rid >= solv->jobrules && rid < solv->jobrules_end)
2946     return SOLVER_RULE_JOB;
2947   if (rid >= solv->updaterules && rid < solv->updaterules_end)
2948     return SOLVER_RULE_UPDATE;
2949   if (rid >= solv->featurerules && rid < solv->featurerules_end)
2950     return SOLVER_RULE_FEATURE;
2951   if (rid >= solv->duprules && rid < solv->duprules_end)
2952     return SOLVER_RULE_DISTUPGRADE;
2953   if (rid >= solv->infarchrules && rid < solv->infarchrules_end)
2954     return SOLVER_RULE_INFARCH;
2955   if (rid >= solv->bestrules && rid < solv->bestrules_end)
2956     return SOLVER_RULE_BEST;
2957   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
2958     return SOLVER_RULE_YUMOBS;
2959   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2960     return SOLVER_RULE_CHOICE;
2961   if (rid >= solv->learntrules && rid < solv->nrules)
2962     return SOLVER_RULE_LEARNT;
2963   return SOLVER_RULE_UNKNOWN;
2964 }
2965
2966 void
2967 solver_ruleliterals(Solver *solv, Id rid, Queue *q)
2968 {
2969   Pool *pool = solv->pool;
2970   Id p, pp;
2971   Rule *r;
2972
2973   queue_empty(q);
2974   r = solv->rules + rid;
2975   FOR_RULELITERALS(p, pp, r)
2976     if (p != -SYSTEMSOLVABLE)
2977       queue_push(q, p);
2978   if (!q->count)
2979     queue_push(q, -SYSTEMSOLVABLE);     /* hmm, better to return an empty result? */
2980 }
2981
2982 int
2983 solver_rule2jobidx(Solver *solv, Id rid)
2984 {
2985   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2986     return 0;
2987   return solv->ruletojob.elements[rid - solv->jobrules] + 1;
2988 }
2989
2990 /* job rule introspection */
2991 Id
2992 solver_rule2job(Solver *solv, Id rid, Id *whatp)
2993 {
2994   int idx;
2995   if (rid < solv->jobrules || rid >= solv->jobrules_end)
2996     {
2997       if (whatp)
2998         *whatp = 0;
2999       return 0;
3000     }
3001   idx = solv->ruletojob.elements[rid - solv->jobrules];
3002   if (whatp)
3003     *whatp = solv->job.elements[idx + 1];
3004   return solv->job.elements[idx];
3005 }
3006
3007 /* update/feature rule introspection */
3008 Id
3009 solver_rule2solvable(Solver *solv, Id rid)
3010 {
3011   if (rid >= solv->updaterules && rid < solv->updaterules_end)
3012     return rid - solv->updaterules;
3013   if (rid >= solv->featurerules && rid < solv->featurerules_end)
3014     return rid - solv->featurerules;
3015   return 0;
3016 }
3017
3018 Id
3019 solver_rule2pkgrule(Solver *solv, Id rid)
3020 {
3021   if (rid >= solv->choicerules && rid < solv->choicerules_end)
3022     return solv->choicerules_ref[rid - solv->choicerules];
3023   return 0;
3024 }
3025
3026 static void
3027 solver_rule2rules_rec(Solver *solv, Id rid, Queue *q, Map *seen)
3028 {
3029   int i;
3030   Id rid2;
3031
3032   if (seen)
3033     MAPSET(seen, rid);
3034   for (i = solv->learnt_why.elements[rid - solv->learntrules]; (rid2 = solv->learnt_pool.elements[i]) != 0; i++)
3035     {
3036       if (seen)
3037         {
3038           if (MAPTST(seen, rid2))
3039             continue;
3040           if (rid2 >= solv->learntrules)
3041             solver_rule2rules_rec(solv, rid2, q, seen);
3042           continue;
3043         }
3044       queue_push(q, rid2);
3045     }
3046 }
3047
3048 /* learnt rule introspection */
3049 void
3050 solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
3051 {
3052   queue_empty(q);
3053   if (rid < solv->learntrules || rid >= solv->nrules)
3054     return;
3055   if (recursive)
3056     {
3057       Map seen;
3058       map_init(&seen, solv->nrules);
3059       solver_rule2rules_rec(solv, rid, q, &seen);
3060       map_free(&seen);
3061     }
3062   else
3063     solver_rule2rules_rec(solv, rid, q, 0);
3064 }
3065
3066
3067 /* check if the newest versions of pi still provides the dependency we're looking for */
3068 static int
3069 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
3070 {
3071   Pool *pool = solv->pool;
3072   Rule *ur;
3073   Id p, pp;
3074   int i;
3075
3076   if (!q->count || q->elements[0] != pi)
3077     {
3078       if (q->count)
3079         queue_empty(q);
3080       ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
3081       if (!ur->p)
3082         ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
3083       if (!ur->p)
3084         return 0;
3085       queue_push2(q, pi, 0);
3086       FOR_RULELITERALS(p, pp, ur)
3087         if (p > 0)
3088           queue_push(q, p);
3089     }
3090   if (q->count == 2)
3091     return 1;
3092   if (q->count == 3)
3093     {
3094       p = q->elements[2];
3095       return MAPTST(m, p) ? 0 : 1;
3096     }
3097   if (!q->elements[1])
3098     {
3099       for (i = 2; i < q->count; i++)
3100         if (!MAPTST(m, q->elements[i]))
3101           break;
3102       if (i == q->count)
3103         return 0;       /* all provide it, no need to filter */
3104       /* some don't provide it, have to filter */
3105       queue_deleten(q, 0, 2);
3106       policy_filter_unwanted(solv, q, POLICY_MODE_CHOOSE);
3107       queue_unshift(q, 1);      /* filter mark */
3108       queue_unshift(q, pi);
3109     }
3110   for (i = 2; i < q->count; i++)
3111     if (MAPTST(m, q->elements[i]))
3112       return 0;         /* at least one provides it */
3113   return 1;     /* none of the new packages provided it */
3114 }
3115
3116 static inline void
3117 queue_removeelement(Queue *q, Id el)
3118 {
3119   int i, j;
3120   for (i = 0; i < q->count; i++)
3121     if (q->elements[i] == el)
3122       break;
3123   if (i < q->count)
3124     {
3125       for (j = i++; i < q->count; i++)
3126         if (q->elements[i] != el)
3127           q->elements[j++] = q->elements[i];
3128       queue_truncate(q, j);
3129     }
3130 }
3131
3132 void
3133 solver_addchoicerules(Solver *solv)
3134 {
3135   Pool *pool = solv->pool;
3136   Map m, mneg;
3137   Rule *r;
3138   Queue q, qi, qcheck;
3139   int i, j, rid, havechoice;
3140   Id p, d, pp;
3141   Id p2, pp2;
3142   Solvable *s, *s2;
3143   Id lastaddedp, lastaddedd;
3144   int lastaddedcnt;
3145   unsigned int now;
3146
3147   solv->choicerules = solv->nrules;
3148   if (!pool->installed)
3149     {
3150       solv->choicerules_end = solv->nrules;
3151       return;
3152     }
3153   now = solv_timems(0);
3154   solv->choicerules_ref = solv_calloc(solv->pkgrules_end, sizeof(Id));
3155   queue_init(&q);
3156   queue_init(&qi);
3157   queue_init(&qcheck);
3158   map_init(&m, pool->nsolvables);
3159   map_init(&mneg, pool->nsolvables);
3160   /* set up negative assertion map from infarch and dup rules */
3161   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
3162     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3163       MAPSET(&mneg, -r->p);
3164   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
3165     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
3166       MAPSET(&mneg, -r->p);
3167   lastaddedp = 0;
3168   lastaddedd = 0;
3169   lastaddedcnt = 0;
3170   for (rid = 1; rid < solv->pkgrules_end ; rid++)
3171     {
3172       r = solv->rules + rid;
3173       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
3174         continue;       /* only look at requires rules */
3175       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
3176       queue_empty(&q);
3177       queue_empty(&qi);
3178       havechoice = 0;
3179       FOR_RULELITERALS(p, pp, r)
3180         {
3181           if (p < 0)
3182             continue;
3183           s = pool->solvables + p;
3184           if (!s->repo)
3185             continue;
3186           if (s->repo == pool->installed)
3187             {
3188               queue_push(&q, p);
3189               continue;
3190             }
3191           /* check if this package is "blocked" by a installed package */
3192           s2 = 0;
3193           FOR_PROVIDES(p2, pp2, s->name)
3194             {
3195               s2 = pool->solvables + p2;
3196               if (s2->repo != pool->installed)
3197                 continue;
3198               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
3199                 continue;
3200               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
3201                 continue;
3202               break;
3203             }
3204           if (p2)
3205             {
3206               /* found installed package p2 that we can update to p */
3207               if (MAPTST(&mneg, p))
3208                 continue;
3209               if (policy_is_illegal(solv, s2, s, 0))
3210                 continue;
3211 #if 0
3212               if (solver_choicerulecheck(solv, p2, r, &m))
3213                 continue;
3214               queue_push(&qi, p2);
3215 #else
3216               queue_push2(&qi, p2, p);
3217 #endif
3218               queue_push(&q, p);
3219               continue;
3220             }
3221           if (s->obsoletes)
3222             {
3223               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3224               s2 = 0;
3225               while ((obs = *obsp++) != 0)
3226                 {
3227                   FOR_PROVIDES(p2, pp2, obs)
3228                     {
3229                       s2 = pool->solvables + p2;
3230                       if (s2->repo != pool->installed)
3231                         continue;
3232                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3233                         continue;
3234                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
3235                         continue;
3236                       break;
3237                     }
3238                   if (p2)
3239                     break;
3240                 }
3241               if (obs)
3242                 {
3243                   /* found installed package p2 that we can update to p */
3244                   if (MAPTST(&mneg, p))
3245                     continue;
3246                   if (policy_is_illegal(solv, s2, s, 0))
3247                     continue;
3248 #if 0
3249                   if (solver_choicerulecheck(solv, p2, r, &m))
3250                     continue;
3251                   queue_push(&qi, p2);
3252 #else
3253                   queue_push2(&qi, p2, p);
3254 #endif
3255                   queue_push(&q, p);
3256                   continue;
3257                 }
3258             }
3259           /* package p is independent of the installed ones */
3260           havechoice = 1;
3261         }
3262       if (!havechoice || !q.count || !qi.count)
3263         continue;       /* no choice */
3264
3265       FOR_RULELITERALS(p, pp, r)
3266         if (p > 0)
3267           MAPSET(&m, p);
3268
3269       /* do extra checking */
3270       for (i = j = 0; i < qi.count; i += 2)
3271         {
3272           p2 = qi.elements[i];
3273           if (!p2)
3274             continue;
3275           if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
3276             {
3277               /* oops, remove element p from q */
3278               queue_removeelement(&q, qi.elements[i + 1]);
3279               continue;
3280             }
3281           qi.elements[j++] = p2;
3282         }
3283       queue_truncate(&qi, j);
3284
3285       if (!q.count || !qi.count)
3286         {
3287           FOR_RULELITERALS(p, pp, r)
3288             if (p > 0)
3289               MAPCLR(&m, p);
3290           continue;
3291         }
3292
3293
3294       /* now check the update rules of the installed package.
3295        * if all packages of the update rules are contained in
3296        * the dependency rules, there's no need to set up the choice rule */
3297       for (i = 0; i < qi.count; i++)
3298         {
3299           Rule *ur;
3300           if (!qi.elements[i])
3301             continue;
3302           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
3303           if (!ur->p)
3304             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
3305           if (!ur->p)
3306             continue;
3307           FOR_RULELITERALS(p, pp, ur)
3308             if (!MAPTST(&m, p))
3309               break;
3310           if (p)
3311             break;
3312           for (j = i + 1; j < qi.count; j++)
3313             if (qi.elements[i] == qi.elements[j])
3314               qi.elements[j] = 0;
3315         }
3316       /* empty map again */
3317       FOR_RULELITERALS(p, pp, r)
3318         if (p > 0)
3319           MAPCLR(&m, p);
3320       if (i == qi.count)
3321         {
3322 #if 0
3323           printf("skipping choice ");
3324           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3325 #endif
3326           continue;
3327         }
3328
3329       /* don't add identical rules */
3330       if (lastaddedp == r->p && lastaddedcnt == q.count)
3331         {
3332           for (i = 0; i < q.count; i++)
3333             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
3334               break;
3335           if (i == q.count)
3336             continue;   /* already added that one */
3337         }
3338       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
3339
3340       lastaddedp = r->p;
3341       lastaddedd = d;
3342       lastaddedcnt = q.count;
3343
3344       solver_addrule(solv, r->p, 0, d);
3345       queue_push(&solv->weakruleq, solv->nrules - 1);
3346       solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
3347 #if 0
3348       printf("OLD ");
3349       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3350       printf("WEAK CHOICE ");
3351       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
3352 #endif
3353     }
3354   queue_free(&q);
3355   queue_free(&qi);
3356   queue_free(&qcheck);
3357   map_free(&m);
3358   map_free(&mneg);
3359   solv->choicerules_end = solv->nrules;
3360   /* shrink choicerules_ref */
3361   solv->choicerules_ref = solv_realloc2(solv->choicerules_ref, solv->choicerules_end - solv->choicerules, sizeof(Id));
3362   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
3363 }
3364
3365 /* called when a choice rule is disabled by analyze_unsolvable. We also
3366  * have to disable all other choice rules so that the best packages get
3367  * picked */
3368 void
3369 solver_disablechoicerules(Solver *solv, Rule *r)
3370 {
3371   Id rid, p, pp;
3372   Pool *pool = solv->pool;
3373   Map m;
3374   Rule *or;
3375
3376   or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3377   map_init(&m, pool->nsolvables);
3378   FOR_RULELITERALS(p, pp, or)
3379     if (p > 0)
3380       MAPSET(&m, p);
3381   FOR_RULELITERALS(p, pp, r)
3382     if (p > 0)
3383       MAPCLR(&m, p);
3384   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
3385     {
3386       r = solv->rules + rid;
3387       if (r->d < 0)
3388         continue;
3389       or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3390       FOR_RULELITERALS(p, pp, or)
3391         if (p > 0 && MAPTST(&m, p))
3392           break;
3393       if (p)
3394         solver_disablerule(solv, r);
3395     }
3396 }
3397
3398 static void
3399 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
3400 {
3401   int i, j;
3402   Id p, *cp2;
3403   for (i = j = 0; i < q->count; i++)
3404     {
3405       p = q->elements[i];
3406       for (cp2 = cp; *cp2; cp2++)
3407         if (*cp2 == p)
3408           {
3409             q->elements[j++] = p;
3410             break;
3411           }
3412     }
3413   queue_truncate(q, j);
3414 }
3415
3416 static void
3417 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
3418 {
3419   int i, j;
3420   for (i = j = 0; i < q->count; i++)
3421     {
3422       Id p = q->elements[i];
3423       if (MAPTST(&solv->dupmap, p))
3424         q->elements[j++] = p;
3425     }
3426   queue_truncate(q, j);
3427 }
3428
3429 void
3430 solver_addbestrules(Solver *solv, int havebestinstalljobs)
3431 {
3432   Pool *pool = solv->pool;
3433   Id p;
3434   Solvable *s;
3435   Repo *installed = solv->installed;
3436   Queue q, q2;
3437   Rule *r;
3438   Queue r2pkg;
3439   int i, oldcnt;
3440
3441   solv->bestrules = solv->nrules;
3442   if (!installed)
3443     {
3444       solv->bestrules_end = solv->nrules;
3445       return;
3446     }
3447   queue_init(&q);
3448   queue_init(&q2);
3449   queue_init(&r2pkg);
3450
3451   if (havebestinstalljobs)
3452     {
3453       for (i = 0; i < solv->job.count; i += 2)
3454         {
3455           if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
3456             {
3457               int j;
3458               Id p2, pp2;
3459               for (j = 0; j < solv->ruletojob.count; j++)
3460                 if (solv->ruletojob.elements[j] == i)
3461                   break;
3462               if (j == solv->ruletojob.count)
3463                 continue;
3464               r = solv->rules + solv->jobrules + j;
3465               queue_empty(&q);
3466               FOR_RULELITERALS(p2, pp2, r)
3467                 if (p2 > 0)
3468                   queue_push(&q, p2);
3469               if (!q.count)
3470                 continue;       /* orphaned */
3471               /* select best packages, just look at prio and version */
3472               oldcnt = q.count;
3473               policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3474               if (q.count == oldcnt)
3475                 continue;       /* nothing filtered */
3476               p2 = queue_shift(&q);
3477               if (q.count < 2)
3478                 solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3479               else
3480                 solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3481               queue_push(&r2pkg, -(solv->jobrules + j));
3482             }
3483         }
3484     }
3485
3486   if (solv->bestupdatemap_all || solv->bestupdatemap.size)
3487     {
3488       FOR_REPO_SOLVABLES(installed, p, s)
3489         {
3490           Id d, p2, pp2;
3491           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
3492             continue;
3493           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
3494             continue;
3495           queue_empty(&q);
3496           if (solv->bestobeypolicy)
3497             r = solv->rules + solv->updaterules + (p - installed->start);
3498           else
3499             {
3500               r = solv->rules + solv->featurerules + (p - installed->start);
3501               if (!r->p)        /* identical to update rule? */
3502                 r = solv->rules + solv->updaterules + (p - installed->start);
3503             }
3504           if (solv->specialupdaters && (d = solv->specialupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
3505             {
3506               /* need to check specialupdaters */
3507               if (r->p == p)    /* be careful with the dup case */
3508                 queue_push(&q, p);
3509               while ((p2 = pool->whatprovidesdata[d++]) != 0)
3510                 queue_push(&q, p2);
3511             }
3512           else
3513             {
3514               FOR_RULELITERALS(p2, pp2, r)
3515                 if (p2 > 0)
3516                   queue_push(&q, p2);
3517             }
3518           if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3519             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
3520           if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3521             prune_to_dup_packages(solv, p, &q);
3522           /* select best packages, just look at prio and version */
3523           policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3524           if (!q.count)
3525             continue;   /* orphaned */
3526           if (solv->bestobeypolicy)
3527             {
3528               /* also filter the best of the feature rule packages and add them */
3529               r = solv->rules + solv->featurerules + (p - installed->start);
3530               if (r->p)
3531                 {
3532                   int j;
3533                   queue_empty(&q2);
3534                   FOR_RULELITERALS(p2, pp2, r)
3535                     if (p2 > 0)
3536                       queue_push(&q2, p2);
3537                   if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3538                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
3539                   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3540                     prune_to_dup_packages(solv, p, &q2);
3541                   policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
3542                   for (j = 0; j < q2.count; j++)
3543                     queue_pushunique(&q, q2.elements[j]);
3544                 }
3545             }
3546           p2 = queue_shift(&q);
3547           if (q.count < 2)
3548             solver_addrule(solv, p2, q.count ? q.elements[0] : 0, 0);
3549           else
3550             solver_addrule(solv, p2, 0, pool_queuetowhatprovides(pool, &q));
3551           queue_push(&r2pkg, p);
3552         }
3553     }
3554   if (r2pkg.count)
3555     solv->bestrules_pkg = solv_memdup2(r2pkg.elements, r2pkg.count, sizeof(Id));
3556   solv->bestrules_end = solv->nrules;
3557   queue_free(&q);
3558   queue_free(&q2);
3559   queue_free(&r2pkg);
3560 }
3561
3562
3563
3564
3565 /* yumobs rule handling */
3566
3567 static void
3568 find_obsolete_group(Solver *solv, Id obs, Queue *q)
3569 {
3570   Pool *pool = solv->pool;
3571   Queue qn;
3572   Id p2, pp2, op, *opp, opp2;
3573   int i, j, qnc, ncnt;
3574
3575   queue_empty(q);
3576   FOR_PROVIDES(p2, pp2, obs)
3577     {
3578       Solvable *s2 = pool->solvables + p2;
3579       if (s2->repo != pool->installed)
3580         continue;
3581       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3582         continue;
3583       /* we obsolete installed package s2 with obs. now find all other packages that have the same dep  */
3584       for (opp = solv->obsoletes_data + solv->obsoletes[p2 - solv->installed->start]; (op = *opp++) != 0;)
3585         {
3586           Solvable *os = pool->solvables + op;
3587           Id obs2, *obsp2;
3588           if (!os->obsoletes)
3589             continue;
3590           if (pool->obsoleteusescolors && !pool_colormatch(pool, s2, os))
3591             continue;
3592           obsp2 = os->repo->idarraydata + os->obsoletes; 
3593           while ((obs2 = *obsp2++) != 0)
3594             if (obs2 == obs)
3595               break;
3596           if (obs2)
3597             queue_pushunique(q, op);
3598         }
3599       /* also search packages with the same name */
3600       FOR_PROVIDES(op, opp2, s2->name)
3601         {
3602           Solvable *os = pool->solvables + op;
3603           Id obs2, *obsp2;
3604           if (os->name != s2->name)
3605             continue;
3606           if (!os->obsoletes)
3607             continue;
3608           if (pool->obsoleteusescolors && !pool_colormatch(pool, s2, os))
3609             continue;
3610           obsp2 = os->repo->idarraydata + os->obsoletes; 
3611           while ((obs2 = *obsp2++) != 0)
3612             if (obs2 == obs)
3613               break;
3614           if (obs2)
3615             queue_pushunique(q, op);
3616         }
3617     }
3618   /* find names so that we can build groups */
3619   queue_init_clone(&qn, q);
3620   prune_to_best_version(solv->pool, &qn);
3621 #if 0
3622 {
3623   for (i = 0; i < qn.count; i++)
3624     printf(" + %s\n", pool_solvid2str(pool, qn.elements[i]));
3625 }
3626 #endif
3627   /* filter into name groups */
3628   qnc = qn.count;
3629   if (qnc == 1)
3630     {
3631       queue_free(&qn);
3632       queue_empty(q);
3633       return;
3634     }
3635   ncnt = 0;
3636   for (i = 0; i < qnc; i++)
3637     {
3638       Id n = pool->solvables[qn.elements[i]].name;
3639       int got = 0;
3640       for (j = 0; j < q->count; j++)
3641         {
3642           Id p = q->elements[j];
3643           if (pool->solvables[p].name == n)
3644             {
3645               queue_push(&qn, p);
3646               got = 1;
3647             }
3648         }
3649       if (got)
3650         {
3651           queue_push(&qn, 0);
3652           ncnt++;
3653         }
3654     }
3655   if (ncnt <= 1)
3656     {
3657       queue_empty(q);
3658     }
3659   else
3660     {
3661       queue_empty(q);
3662       queue_insertn(q, 0, qn.count - qnc, qn.elements + qnc);
3663     }
3664   queue_free(&qn);
3665 }
3666
3667 void
3668 solver_addyumobsrules(Solver *solv)
3669 {
3670   Pool *pool = solv->pool;
3671   Repo *installed = solv->installed;
3672   Id p, op, *opp;
3673   Solvable *s;
3674   Queue qo, qq, yumobsinfoq;
3675   int i, j, k;
3676   unsigned int now;
3677
3678   solv->yumobsrules = solv->nrules;
3679   if (!installed || !solv->obsoletes)
3680     {
3681       solv->yumobsrules_end = solv->nrules;
3682       return;
3683     }
3684   now = solv_timems(0);
3685   queue_init(&qo);
3686   FOR_REPO_SOLVABLES(installed, p, s)
3687     {
3688       if (!solv->obsoletes[p - installed->start])
3689         continue;
3690 #if 0
3691 printf("checking yumobs for %s\n", pool_solvable2str(pool, s));
3692 #endif
3693       queue_empty(&qo);
3694       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (op = *opp++) != 0;)
3695         {
3696           Solvable *os = pool->solvables + op;
3697           Id obs, *obsp = os->repo->idarraydata + os->obsoletes;
3698           Id p2, pp2;
3699           while ((obs = *obsp++) != 0)
3700             {
3701               FOR_PROVIDES(p2, pp2, obs)
3702                 {
3703                   Solvable *s2 = pool->solvables + p2;
3704                   if (s2->repo != installed)
3705                     continue;
3706                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3707                     continue;
3708                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
3709                     continue;
3710                   queue_pushunique(&qo, obs);
3711                   break;
3712                 }
3713             }
3714         }
3715     }
3716   if (!qo.count)
3717     {
3718       queue_free(&qo);
3719       return;
3720     }
3721   queue_init(&yumobsinfoq);
3722   queue_init(&qq);
3723   for (i = 0; i < qo.count; i++)
3724     {
3725       int group, groupk, groupstart;
3726       queue_empty(&qq);
3727 #if 0
3728 printf("investigating %s\n", pool_dep2str(pool, qo.elements[i]));
3729 #endif
3730       find_obsolete_group(solv, qo.elements[i], &qq);
3731 #if 0
3732 printf("result:\n");
3733 for (j = 0; j < qq.count; j++)
3734   if (qq.elements[j] == 0)
3735     printf("---\n");
3736   else
3737     printf("%s\n", pool_solvid2str(pool, qq.elements[j]));
3738 #endif
3739   
3740       if (!qq.count)
3741         continue;
3742       /* at least two goups, build rules */
3743       group = 0;
3744       for (j = 0; j < qq.count; j++)
3745         {
3746           p = qq.elements[j];
3747           if (!p)
3748             {
3749               group++;
3750               continue;
3751             }
3752           if (pool->solvables[p].repo == installed)
3753             continue;
3754           groupk = 0;
3755           groupstart = 0;
3756           for (k = 0; k < qq.count; k++)
3757             {
3758               Id pk = qq.elements[k];
3759               if (pk)
3760                 continue;
3761               if (group != groupk && k > groupstart)
3762                 {
3763                   /* add the rule */
3764                   if (k - groupstart == 1)
3765                     solver_addrule(solv, -p, qq.elements[groupstart], 0);
3766                   else
3767                     solver_addrule(solv, -p, 0, pool_ids2whatprovides(pool, qq.elements + groupstart, k - groupstart));
3768                   queue_push(&yumobsinfoq, qo.elements[i]);
3769                 }
3770               groupstart = k + 1;
3771               groupk++;
3772             }
3773         }
3774     }
3775   if (yumobsinfoq.count)
3776     solv->yumobsrules_info = solv_memdup2(yumobsinfoq.elements, yumobsinfoq.count, sizeof(Id));
3777   queue_free(&yumobsinfoq);
3778   queue_free(&qq);
3779   queue_free(&qo);
3780   solv->yumobsrules_end = solv->nrules;
3781   POOL_DEBUG(SOLV_DEBUG_STATS, "yumobs rule creation took %d ms\n", solv_timems(now));
3782 }
3783
3784 #undef CLEANDEPSDEBUG
3785
3786 /*
3787  * This functions collects all packages that are looked at
3788  * when a dependency is checked. We need it to "pin" installed
3789  * packages when removing a supplemented package in createcleandepsmap.
3790  * Here's an not uncommon example:
3791  *   A contains "Supplements: packageand(B, C)"
3792  *   B contains "Requires: A"
3793  * Now if we remove C, the supplements is no longer true,
3794  * thus we also remove A. Without the dep_pkgcheck function, we
3795  * would now also remove B, but this is wrong, as adding back
3796  * C doesn't make the supplements true again. Thus we "pin" B
3797  * when we remove A.
3798  * There's probably a better way to do this, but I haven't come
3799  * up with it yet ;)
3800  */
3801 static inline void
3802 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
3803 {
3804   Pool *pool = solv->pool;
3805   Id p, pp;
3806
3807   if (ISRELDEP(dep))
3808     {
3809       Reldep *rd = GETRELDEP(pool, dep);
3810       if (rd->flags >= 8)
3811         {
3812           if (rd->flags == REL_AND)
3813             {
3814               dep_pkgcheck(solv, rd->name, m, q);
3815               dep_pkgcheck(solv, rd->evr, m, q);
3816               return;
3817             }
3818           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3819             return;
3820         }
3821     }
3822   FOR_PROVIDES(p, pp, dep)
3823     if (!m || MAPTST(m, p))
3824       queue_push(q, p);
3825 }
3826
3827 static int
3828 check_xsupp(Solver *solv, Queue *depq, Id dep)
3829 {
3830   Pool *pool = solv->pool;
3831   Id p, pp;
3832
3833   if (ISRELDEP(dep))
3834     {
3835       Reldep *rd = GETRELDEP(pool, dep);
3836       if (rd->flags >= 8)
3837         {
3838           if (rd->flags == REL_AND)
3839             {
3840               if (!check_xsupp(solv, depq, rd->name))
3841                 return 0;
3842               return check_xsupp(solv, depq, rd->evr);
3843             }
3844           if (rd->flags == REL_OR)
3845             {
3846               if (check_xsupp(solv, depq, rd->name))
3847                 return 1;
3848               return check_xsupp(solv, depq, rd->evr);
3849             }
3850           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3851 #if 0
3852             return solver_splitprovides(solv, rd->evr);
3853 #else
3854             return 0;
3855 #endif
3856         }
3857       if (depq && rd->flags == REL_NAMESPACE)
3858         {
3859           int i;
3860           for (i = 0; i < depq->count; i++)
3861             if (depq->elements[i] == dep || depq->elements[i] == rd->name)
3862              return 1;
3863         }
3864     }
3865   FOR_PROVIDES(p, pp, dep)
3866     if (p == SYSTEMSOLVABLE || pool->solvables[p].repo == solv->installed)
3867       return 1;
3868   return 0;
3869 }
3870
3871 static inline int
3872 queue_contains(Queue *q, Id id)
3873 {
3874   int i;
3875   for (i = 0; i < q->count; i++)
3876     if (q->elements[i] == id)
3877       return 1;
3878   return 0;
3879 }
3880
3881 #ifdef ENABLE_COMPLEX_DEPS
3882 static void
3883 complex_cleandeps_remove(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq)
3884 {
3885   int i;
3886   Queue dq;
3887   Id p;
3888
3889   queue_init(&dq);
3890   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3891   if (i == 0 || i == 1)
3892     {
3893       queue_free(&dq);
3894       return;
3895     }
3896   for (i = 0; i < dq.count; i++)
3897     {
3898       for (; (p = dq.elements[i]) != 0; i++)
3899         {
3900           if (p < 0)
3901             {
3902               if (!MAPTST(installedm, -p))
3903                 break;
3904               continue;
3905             }
3906           if (p != SYSTEMSOLVABLE && MAPTST(im, p))
3907             {
3908 #ifdef CLEANDEPSDEBUG
3909               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3910 #endif
3911               queue_push(iq, p);
3912             }
3913         }
3914       while (dq.elements[i])
3915         i++;
3916     }
3917   queue_free(&dq);
3918 }
3919
3920 static void
3921 complex_cleandeps_addback(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq, Map *userinstalled)
3922 {
3923   int i, blk;
3924   Queue dq;
3925   Id p;
3926
3927   queue_init(&dq);
3928   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3929   if (i == 0 || i == 1)
3930     {
3931       queue_free(&dq);
3932       return;
3933     }
3934   for (i = 0; i < dq.count; i++)
3935     {
3936       blk = i;
3937       for (; (p = dq.elements[i]) != 0; i++)
3938         {
3939           if (p < 0)
3940             {
3941               if (!MAPTST(installedm, -p))
3942                 break;
3943             }
3944           else if (p == ip)
3945             break;
3946         }
3947       if (!p)
3948         {
3949           for (i = blk; (p = dq.elements[i]) != 0; i++)
3950             {
3951               if (p < 0)
3952                 continue;
3953               if (MAPTST(im, p))
3954                 continue;
3955               if (!MAPTST(installedm, p))
3956                 continue;
3957               if (p == ip || MAPTST(userinstalled, p - pool->installed->start))
3958                 continue;
3959 #ifdef CLEANDEPSDEBUG
3960               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3961 #endif
3962               MAPSET(im, p);
3963               queue_push(iq, p);
3964             }
3965         }
3966       while (dq.elements[i])
3967         i++;
3968     }
3969   queue_free(&dq);
3970 }
3971
3972 #endif
3973
3974 /*
3975  * Find all installed packages that are no longer
3976  * needed regarding the current solver job.
3977  *
3978  * The algorithm is:
3979  * - remove pass: remove all packages that could have
3980  *   been dragged in by the obsoleted packages.
3981  *   i.e. if package A is obsolete and contains "Requires: B",
3982  *   also remove B, as installing A will have pulled in B.
3983  *   after this pass, we have a set of still installed packages
3984  *   with broken dependencies.
3985  * - add back pass:
3986  *   now add back all packages that the still installed packages
3987  *   require.
3988  *
3989  * The cleandeps packages are the packages removed in the first
3990  * pass and not added back in the second pass.
3991  *
3992  * If we search for unneeded packages (unneeded is true), we
3993  * simply remove all packages except the userinstalled ones in
3994  * the first pass.
3995  */
3996 static void
3997 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
3998 {
3999   Pool *pool = solv->pool;
4000   Repo *installed = solv->installed;
4001   Queue *job = &solv->job;
4002   Map userinstalled;
4003   Map im;
4004   Map installedm;
4005   Rule *r;
4006   Id rid, how, what, select;
4007   Id p, pp, ip, jp;
4008   Id req, *reqp, sup, *supp;
4009   Solvable *s;
4010   Queue iq, iqcopy, xsuppq;
4011   int i;
4012
4013   map_empty(cleandepsmap);
4014   if (!installed || installed->end == installed->start)
4015     return;
4016   map_init(&userinstalled, installed->end - installed->start);
4017   map_init(&im, pool->nsolvables);
4018   map_init(&installedm, pool->nsolvables);
4019   queue_init(&iq);
4020   queue_init(&xsuppq);
4021
4022   for (i = 0; i < job->count; i += 2)
4023     {
4024       how = job->elements[i];
4025       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
4026         {
4027           what = job->elements[i + 1];
4028           select = how & SOLVER_SELECTMASK;
4029           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
4030             {
4031               FOR_REPO_SOLVABLES(installed, p, s)
4032                 MAPSET(&userinstalled, p - installed->start);
4033             }
4034           FOR_JOB_SELECT(p, pp, select, what)
4035             if (pool->solvables[p].repo == installed)
4036               MAPSET(&userinstalled, p - installed->start);
4037         }
4038       if ((how & (SOLVER_JOBMASK | SOLVER_SELECTMASK)) == (SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES))
4039         {
4040           what = job->elements[i + 1];
4041           if (ISRELDEP(what))
4042             {
4043               Reldep *rd = GETRELDEP(pool, what);
4044               if (rd->flags != REL_NAMESPACE)
4045                 continue;
4046               if (rd->evr == 0)
4047                 {
4048                   queue_pushunique(&iq, rd->name);
4049                   continue;
4050                 }
4051               FOR_PROVIDES(p, pp, what)
4052                 if (p)
4053                   break;
4054               if (p)
4055                 continue;
4056               queue_pushunique(&iq, what);
4057             }
4058         }
4059     }
4060
4061   /* have special namespace cleandeps erases */
4062   if (iq.count)
4063     {
4064       for (ip = installed->start; ip < installed->end; ip++)
4065         {
4066           s = pool->solvables + ip;
4067           if (s->repo != installed)
4068             continue;
4069           if (!s->supplements)
4070             continue;
4071           supp = s->repo->idarraydata + s->supplements;
4072           while ((sup = *supp++) != 0)
4073             if (ISRELDEP(sup) && check_xsupp(solv, &iq, sup) && !check_xsupp(solv, 0, sup))
4074               {
4075 #ifdef CLEANDEPSDEBUG
4076                 printf("xsupp %s from %s\n", pool_dep2str(pool, sup), pool_solvid2str(pool, ip));
4077 #endif
4078                 queue_pushunique(&xsuppq, sup);
4079               }
4080         }
4081       queue_empty(&iq);
4082     }
4083
4084   /* also add visible patterns to userinstalled for openSUSE */
4085   if (1)
4086     {
4087       Dataiterator di;
4088       dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
4089       while (dataiterator_step(&di))
4090         {
4091           Id *dp;
4092           if (di.solvid <= 0)
4093             continue;
4094           s = pool->solvables + di.solvid;
4095           if (!s->repo || !s->requires)
4096             continue;
4097           if (s->repo != installed && !pool_installable(pool, s))
4098             continue;
4099           if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
4100             continue;
4101           dp = s->repo->idarraydata + s->requires;
4102           for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
4103             FOR_PROVIDES(p, pp, *dp)
4104               if (pool->solvables[p].repo == installed)
4105                 {
4106                   if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
4107                     continue;
4108                   MAPSET(&userinstalled, p - installed->start);
4109                 }
4110         }
4111       dataiterator_free(&di);
4112     }
4113   if (1)
4114     {
4115       /* all products and their buddies are userinstalled */
4116       for (p = installed->start; p < installed->end; p++)
4117         {
4118           Solvable *s = pool->solvables + p;
4119           if (s->repo != installed)
4120             continue;
4121           if (!strncmp("product:", pool_id2str(pool, s->name), 8))
4122             {
4123               MAPSET(&userinstalled, p - installed->start);
4124 #ifdef ENABLE_LINKED_PKGS
4125               if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
4126                 {
4127                   Id buddy = solv->instbuddy[p - installed->start];
4128                   if (buddy >= installed->start && buddy < installed->end)
4129                     MAPSET(&userinstalled, buddy - installed->start);
4130                 }
4131 #endif
4132             }
4133         }
4134     }
4135
4136   /* add all positive elements (e.g. locks) to "userinstalled" */
4137   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4138     {
4139       r = solv->rules + rid;
4140       if (r->d < 0)
4141         continue;
4142       i = solv->ruletojob.elements[rid - solv->jobrules];
4143       if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
4144         continue;
4145       FOR_RULELITERALS(p, jp, r)
4146         if (p > 0 && pool->solvables[p].repo == installed)
4147           MAPSET(&userinstalled, p - installed->start);
4148     }
4149
4150   /* add all cleandeps candidates to iq */
4151   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4152     {
4153       r = solv->rules + rid;
4154       if (r->d < 0)                             /* disabled? */
4155         continue;
4156       if (r->d == 0 && r->p < 0 && r->w2 == 0)  /* negative assertion (erase job)? */
4157         {
4158           p = -r->p;
4159           if (pool->solvables[p].repo != installed)
4160             continue;
4161           MAPCLR(&userinstalled, p - installed->start);
4162           if (unneeded)
4163             continue;
4164           i = solv->ruletojob.elements[rid - solv->jobrules];
4165           how = job->elements[i];
4166           if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
4167             queue_push(&iq, p);
4168         }
4169       else if (r->p > 0)                        /* install job */
4170         {
4171           if (unneeded)
4172             continue;
4173           i = solv->ruletojob.elements[rid - solv->jobrules];
4174           if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
4175             {
4176               /* check if the literals all obsolete some installed package */
4177               Map om;
4178               int iqstart;
4179
4180               /* just one installed literal */
4181               if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
4182                 continue;
4183               /* multiversion is bad */
4184               if (solv->multiversion.size && !solv->keepexplicitobsoletes)
4185                 {
4186                   FOR_RULELITERALS(p, jp, r)
4187                     if (MAPTST(&solv->multiversion, p))
4188                       break;
4189                   if (p)
4190                     continue;
4191                 }
4192
4193               om.size = 0;
4194               iqstart = iq.count;
4195               FOR_RULELITERALS(p, jp, r)
4196                 {
4197                   if (p < 0)
4198                     {
4199                       queue_truncate(&iq, iqstart);     /* abort */
4200                       break;
4201                     }
4202                   if (pool->solvables[p].repo == installed)
4203                     {
4204                       if (iq.count == iqstart)
4205                         queue_push(&iq, p);
4206                       else
4207                         {
4208                           for (i = iqstart; i < iq.count; i++)
4209                             if (iq.elements[i] == p)
4210                               break;
4211                           queue_truncate(&iq, iqstart);
4212                           if (i < iq.count)
4213                             queue_push(&iq, p);
4214                         }
4215                     }
4216                   else
4217                     intersect_obsoletes(solv, p, &iq, iqstart, &om);
4218                   if (iq.count == iqstart)
4219                     break;
4220                 }
4221               if (om.size)
4222                 map_free(&om);
4223             }
4224         }
4225     }
4226   queue_init_clone(&iqcopy, &iq);
4227
4228   if (!unneeded)
4229     {
4230       if (solv->cleandeps_updatepkgs)
4231         for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
4232           queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
4233     }
4234
4235   if (unneeded)
4236     queue_empty(&iq);   /* just in case... */
4237
4238   /* clear userinstalled bit for the packages we really want to delete/update */
4239   for (i = 0; i < iq.count; i++)
4240     {
4241       p = iq.elements[i];
4242       if (pool->solvables[p].repo != installed)
4243         continue;
4244       MAPCLR(&userinstalled, p - installed->start);
4245     }
4246
4247   for (p = installed->start; p < installed->end; p++)
4248     {
4249       if (pool->solvables[p].repo != installed)
4250         continue;
4251       MAPSET(&installedm, p);
4252       if (unneeded && !MAPTST(&userinstalled, p - installed->start))
4253         continue;
4254       MAPSET(&im, p);
4255     }
4256   MAPSET(&installedm, SYSTEMSOLVABLE);
4257   MAPSET(&im, SYSTEMSOLVABLE);
4258
4259 #ifdef CLEANDEPSDEBUG
4260   printf("REMOVE PASS\n");
4261 #endif
4262
4263   for (;;)
4264     {
4265       if (!iq.count)
4266         {
4267           if (unneeded)
4268             break;
4269           /* supplements pass */
4270           for (ip = installed->start; ip < installed->end; ip++)
4271             {
4272               if (!MAPTST(&installedm, ip))
4273                 continue;
4274               s = pool->solvables + ip;
4275               if (!s->supplements)
4276                 continue;
4277               if (!MAPTST(&im, ip))
4278                 continue;
4279               if (MAPTST(&userinstalled, ip - installed->start))
4280                 continue;
4281               supp = s->repo->idarraydata + s->supplements;
4282               while ((sup = *supp++) != 0)
4283                 if (dep_possible(solv, sup, &im))
4284                   break;
4285               if (!sup)
4286                 {
4287                   supp = s->repo->idarraydata + s->supplements;
4288                   while ((sup = *supp++) != 0)
4289                     if (dep_possible(solv, sup, &installedm) || (xsuppq.count && queue_contains(&xsuppq, sup)))
4290                       {
4291                         /* no longer supplemented, also erase */
4292                         int iqcount = iq.count;
4293                         /* pin packages, see comment above dep_pkgcheck */
4294                         dep_pkgcheck(solv, sup, &im, &iq);
4295                         for (i = iqcount; i < iq.count; i++)
4296                           {
4297                             Id pqp = iq.elements[i];
4298                             if (pool->solvables[pqp].repo == installed)
4299                               MAPSET(&userinstalled, pqp - installed->start);
4300                           }
4301                         queue_truncate(&iq, iqcount);
4302 #ifdef CLEANDEPSDEBUG
4303                         printf("%s supplemented [%s]\n", pool_solvid2str(pool, ip), pool_dep2str(pool, sup));
4304 #endif
4305                         queue_push(&iq, ip);
4306                       }
4307                 }
4308             }
4309           if (!iq.count)
4310             break;      /* no supplementing package found, we're done */
4311         }
4312       ip = queue_shift(&iq);
4313       s = pool->solvables + ip;
4314       if (!MAPTST(&im, ip))
4315         continue;
4316       if (!MAPTST(&installedm, ip))
4317         continue;
4318       if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
4319         continue;
4320       MAPCLR(&im, ip);
4321 #ifdef CLEANDEPSDEBUG
4322       printf("removing %s\n", pool_solvable2str(pool, s));
4323 #endif
4324       if (s->requires)
4325         {
4326           reqp = s->repo->idarraydata + s->requires;
4327           while ((req = *reqp++) != 0)
4328             {
4329               if (req == SOLVABLE_PREREQMARKER)
4330                 continue;
4331 #ifdef ENABLE_COMPLEX_DEPS
4332               if (pool_is_complex_dep(pool, req))
4333                 {
4334                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
4335                   continue;
4336                 }
4337 #endif
4338               FOR_PROVIDES(p, pp, req)
4339                 {
4340                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
4341                     {
4342 #ifdef CLEANDEPSDEBUG
4343                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4344 #endif
4345                       queue_push(&iq, p);
4346                     }
4347                 }
4348             }
4349         }
4350       if (s->recommends)
4351         {
4352           reqp = s->repo->idarraydata + s->recommends;
4353           while ((req = *reqp++) != 0)
4354             {
4355 #ifdef ENABLE_COMPLEX_DEPS
4356               if (pool_is_complex_dep(pool, req))
4357                 {
4358                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
4359                   continue;
4360                 }
4361 #endif
4362               FOR_PROVIDES(p, pp, req)
4363                 {
4364                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
4365                     {
4366 #ifdef CLEANDEPSDEBUG
4367                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4368 #endif
4369                       queue_push(&iq, p);
4370                     }
4371                 }
4372             }
4373         }
4374     }
4375
4376   /* turn userinstalled into remove set for pruning */
4377   map_empty(&userinstalled);
4378   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4379     {
4380       r = solv->rules + rid;
4381       if (r->p >= 0 || r->d != 0 || r->w2 != 0)
4382         continue;       /* disabled or not erase */
4383       p = -r->p;
4384       MAPCLR(&im, p);
4385       if (pool->solvables[p].repo == installed)
4386         MAPSET(&userinstalled, p - installed->start);
4387     }
4388   if (!unneeded && solv->cleandeps_updatepkgs)
4389     {
4390       for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
4391         {
4392           p = solv->cleandeps_updatepkgs->elements[i];
4393           if (pool->solvables[p].repo == installed)
4394             MAPSET(&userinstalled, p - installed->start);
4395         }
4396     }
4397   MAPSET(&im, SYSTEMSOLVABLE);  /* in case we cleared it above */
4398   for (p = installed->start; p < installed->end; p++)
4399     if (MAPTST(&im, p))
4400       queue_push(&iq, p);
4401   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4402     {
4403       r = solv->rules + rid;
4404       if (r->d < 0)
4405         continue;
4406       FOR_RULELITERALS(p, jp, r)
4407         if (p > 0)
4408           queue_push(&iq, p);
4409     }
4410   /* also put directly addressed packages on the install queue
4411    * so we can mark patterns as installed */
4412   for (i = 0; i < job->count; i += 2)
4413     {
4414       how = job->elements[i];
4415       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
4416         {
4417           what = job->elements[i + 1];
4418           select = how & SOLVER_SELECTMASK;
4419           if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
4420             queue_push(&iq, what);
4421         }
4422     }
4423
4424 #ifdef CLEANDEPSDEBUG
4425   printf("ADDBACK PASS\n");
4426 #endif
4427   for (;;)
4428     {
4429       if (!iq.count)
4430         {
4431           /* supplements pass */
4432           for (ip = installed->start; ip < installed->end; ip++)
4433             {
4434               if (!MAPTST(&installedm, ip))
4435                 continue;
4436               if (MAPTST(&userinstalled, ip - installed->start))
4437                 continue;
4438               s = pool->solvables + ip;
4439               if (!s->supplements)
4440                 continue;
4441               if (MAPTST(&im, ip))
4442                 continue;
4443               supp = s->repo->idarraydata + s->supplements;
4444               while ((sup = *supp++) != 0)
4445                 if (dep_possible(solv, sup, &im))
4446                   break;
4447               if (sup)
4448                 {
4449 #ifdef CLEANDEPSDEBUG
4450                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
4451 #endif
4452                   MAPSET(&im, ip);
4453                   queue_push(&iq, ip);
4454                 }
4455             }
4456           if (!iq.count)
4457             break;
4458         }
4459       ip = queue_shift(&iq);
4460       s = pool->solvables + ip;
4461 #ifdef CLEANDEPSDEBUG
4462       printf("adding back %s\n", pool_solvable2str(pool, s));
4463 #endif
4464       if (s->repo == installed && pool->implicitobsoleteusescolors)
4465         {
4466           Id a, bestarch = 0;
4467           FOR_PROVIDES(p, pp, s->name)
4468             {
4469               Solvable *ps = pool->solvables + p;
4470               if (ps->name != s->name || ps->repo == installed)
4471                 continue;
4472               a = ps->arch;
4473               a = (a <= pool->lastarch) ? pool->id2arch[a] : 0;
4474               if (a && a != 1 && (!bestarch || a < bestarch))
4475                 bestarch = a;
4476             }
4477           if (bestarch && (s->arch > pool->lastarch || pool->id2arch[s->arch] != bestarch))
4478             {
4479               FOR_PROVIDES(p, pp, s->name)
4480                 {
4481                   Solvable *ps = pool->solvables + p;
4482                   if (ps->repo == installed && ps->name == s->name && ps->evr == s->evr && ps->arch != s->arch && ps->arch < pool->lastarch && pool->id2arch[ps->arch] == bestarch)
4483                     if (!MAPTST(&im, p))
4484                       {
4485 #ifdef CLEANDEPSDEBUG
4486                         printf("%s lockstep %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4487 #endif
4488                         MAPSET(&im, p);
4489                         queue_push(&iq, p);
4490                       }
4491                 }
4492             }
4493         }
4494       if (s->requires)
4495         {
4496           reqp = s->repo->idarraydata + s->requires;
4497           while ((req = *reqp++) != 0)
4498             {
4499 #ifdef ENABLE_COMPLEX_DEPS
4500               if (pool_is_complex_dep(pool, req))
4501                 {
4502                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
4503                   continue;
4504                 }
4505 #endif
4506               FOR_PROVIDES(p, pp, req)
4507                 if (p == ip)
4508                   break;
4509               if (p)
4510                 continue;
4511               FOR_PROVIDES(p, pp, req)
4512                 {
4513                   if (MAPTST(&im, p))
4514                     continue;
4515                   if (MAPTST(&installedm, p))
4516                     {
4517                       if (p == ip)
4518                         continue;
4519                       if (MAPTST(&userinstalled, p - installed->start))
4520                         continue;
4521 #ifdef CLEANDEPSDEBUG
4522                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4523 #endif
4524                       MAPSET(&im, p);
4525                       queue_push(&iq, p);
4526                     }
4527                 }
4528             }
4529         }
4530       if (s->recommends)
4531         {
4532           reqp = s->repo->idarraydata + s->recommends;
4533           while ((req = *reqp++) != 0)
4534             {
4535 #ifdef ENABLE_COMPLEX_DEPS
4536               if (pool_is_complex_dep(pool, req))
4537                 {
4538                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
4539                   continue;
4540                 }
4541 #endif
4542               FOR_PROVIDES(p, pp, req)
4543                 if (p == ip)
4544                   break;
4545               if (p)
4546                 continue;
4547               FOR_PROVIDES(p, pp, req)
4548                 {
4549                   if (MAPTST(&im, p))
4550                     continue;
4551                   if (MAPTST(&installedm, p))
4552                     {
4553                       if (p == ip)
4554                         continue;
4555                       if (MAPTST(&userinstalled, p - installed->start))
4556                         continue;
4557 #ifdef CLEANDEPSDEBUG
4558                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4559 #endif
4560                       MAPSET(&im, p);
4561                       queue_push(&iq, p);
4562                     }
4563                 }
4564             }
4565         }
4566     }
4567
4568   queue_free(&iq);
4569   /* make sure the updatepkgs and mistakes are not in the cleandeps map */
4570   if (solv->cleandeps_updatepkgs)
4571     for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
4572       MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
4573   if (solv->cleandeps_mistakes)
4574     for (i = 0; i < solv->cleandeps_mistakes->count; i++)
4575       MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
4576   /* also remove original iq packages */
4577   for (i = 0; i < iqcopy.count; i++)
4578     MAPSET(&im, iqcopy.elements[i]);
4579   queue_free(&iqcopy);
4580   for (p = installed->start; p < installed->end; p++)
4581     {
4582       if (pool->solvables[p].repo != installed)
4583         continue;
4584       if (!MAPTST(&im, p))
4585         MAPSET(cleandepsmap, p - installed->start);
4586     }
4587   map_free(&im);
4588   map_free(&installedm);
4589   map_free(&userinstalled);
4590   queue_free(&xsuppq);
4591 #ifdef CLEANDEPSDEBUG
4592   printf("=== final cleandeps map:\n");
4593   for (p = installed->start; p < installed->end; p++)
4594     if (MAPTST(cleandepsmap, p - installed->start))
4595       printf("  - %s\n", pool_solvid2str(pool, p));
4596 #endif
4597 }
4598
4599
4600 struct trj_data {
4601   Queue *edges;
4602   Id *low;
4603   Id idx;
4604   Id nstack;
4605   Id firstidx;
4606 };
4607
4608 /* Tarjan's SCC algorithm, slightly modifed */
4609 static void
4610 trj_visit(struct trj_data *trj, Id node)
4611 {
4612   Id *low = trj->low;
4613   Queue *edges = trj->edges;
4614   Id nnode, myidx, stackstart;
4615   int i;
4616
4617   low[node] = myidx = trj->idx++;
4618   low[(stackstart = trj->nstack++)] = node;
4619   for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
4620     {
4621       Id l = low[nnode];
4622       if (!l)
4623         {
4624           if (!edges->elements[edges->elements[nnode]])
4625             {
4626               trj->idx++;
4627               low[nnode] = -1;
4628               continue;
4629             }
4630           trj_visit(trj, nnode);
4631           l = low[nnode];
4632         }
4633       if (l < 0)
4634         continue;
4635       if (l < trj->firstidx)
4636         {
4637           int k;
4638           for (k = l; low[low[k]] == l; k++)
4639             low[low[k]] = -1;
4640         }
4641       else if (l < low[node])
4642         low[node] = l;
4643     }
4644   if (low[node] == myidx)
4645     {
4646       if (myidx != trj->firstidx)
4647         myidx = -1;
4648       for (i = stackstart; i < trj->nstack; i++)
4649         low[low[i]] = myidx;
4650       trj->nstack = stackstart;
4651     }
4652 }
4653
4654 #ifdef ENABLE_COMPLEX_DEPS
4655 static void
4656 complex_unneeded(Pool *pool, Id ip, Id req, Queue *edges, Map *cleandepsmap, Queue *unneededq)
4657 {
4658   int i, j;
4659   Queue dq;
4660   Id p;
4661
4662   queue_init(&dq);
4663   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
4664   if (i == 0 || i == 1)
4665     {
4666       queue_free(&dq);
4667       return;
4668     }
4669   for (i = 0; i < dq.count; i++)
4670     {
4671       for (; (p = dq.elements[i]) != 0; i++)
4672         {
4673           if (p < 0)
4674             {
4675               if (pool->solvables[-p].repo != pool->installed)
4676                 break;
4677               continue;
4678             }
4679           if (p == ip || pool->solvables[p].repo != pool->installed || !MAPTST(cleandepsmap, p - pool->installed->start))
4680             continue;
4681           for (j = 0; j < unneededq->count; j++)
4682             if (p == unneededq->elements[j])
4683               {
4684                 if (edges->elements[edges->count - 1] != j + 1)
4685                   queue_push(edges, j + 1);
4686                 break;
4687               }
4688         }
4689       while (dq.elements[i])
4690         i++;
4691     }
4692   queue_free(&dq);
4693 }
4694 #endif
4695
4696 void
4697 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
4698 {
4699   Repo *installed = solv->installed;
4700   int i;
4701   Map cleandepsmap;
4702
4703   queue_empty(unneededq);
4704   if (!installed || installed->end == installed->start)
4705     return;
4706
4707   map_init(&cleandepsmap, installed->end - installed->start);
4708   solver_createcleandepsmap(solv, &cleandepsmap, 1);
4709   for (i = installed->start; i < installed->end; i++)
4710     if (MAPTST(&cleandepsmap, i - installed->start))
4711       queue_push(unneededq, i);
4712
4713   if (filtered && unneededq->count > 1)
4714     {
4715       Pool *pool = solv->pool;
4716       Queue edges;
4717       Id *nrequires;
4718       Map installedm;
4719       int j, pass, count = unneededq->count;
4720       Id *low;
4721
4722       map_init(&installedm, pool->nsolvables);
4723       for (i = installed->start; i < installed->end; i++)
4724         if (pool->solvables[i].repo == installed)
4725           MAPSET(&installedm, i);
4726
4727       nrequires = solv_calloc(count, sizeof(Id));
4728       queue_init(&edges);
4729       queue_prealloc(&edges, count * 4 + 10);   /* pre-size */
4730
4731       /*
4732        * Go through the solvables in the nodes queue and create edges for
4733        * all requires/recommends/supplements between the nodes.
4734        * The edges are stored in the edges queue, we add 1 to the node
4735        * index so that nodes in the edges queue are != 0 and we can
4736        * terminate the edge list with 0.
4737        * Thus for node element 5, the edges are stored starting at
4738        * edges.elements[6] and are 0-terminated.
4739        */
4740       /* leave first element zero to make things easier */
4741       /* also add trailing zero */
4742       queue_insertn(&edges, 0, 1 + count + 1, 0);
4743
4744       /* first requires and recommends */
4745       for (i = 0; i < count; i++)
4746         {
4747           Solvable *s = pool->solvables + unneededq->elements[i];
4748           int oldcount = edges.count;
4749           edges.elements[i + 1] = oldcount;
4750           for (pass = 0; pass < 2; pass++)
4751             {
4752               unsigned int off = pass == 0 ? s->requires : s->recommends;
4753               Id p, pp, dep, *dp;
4754               if (off)
4755                 for (dp = s->repo->idarraydata + off; (dep = *dp) != 0; dp++)
4756                   {
4757 #ifdef ENABLE_COMPLEX_DEPS
4758                     if (pool_is_complex_dep(pool, dep))
4759                       {
4760                         complex_unneeded(pool, s - pool->solvables, dep, &edges, &cleandepsmap, unneededq);
4761                         continue;
4762                       }
4763 #endif
4764                     FOR_PROVIDES(p, pp, dep)
4765                       {
4766                         Solvable *sp = pool->solvables + p;
4767                         if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4768                           continue;
4769                         for (j = 0; j < count; j++)
4770                           if (p == unneededq->elements[j])
4771                             {
4772                               if (edges.elements[edges.count - 1] != j + 1)
4773                                 queue_push(&edges, j + 1);
4774                             }
4775                       }
4776                   }
4777               if (pass == 0)
4778                 nrequires[i] = edges.count - oldcount;
4779             }
4780           queue_push(&edges, 0);
4781         }
4782 #if 0
4783       printf("requires + recommends\n");
4784       for (i = 0; i < count; i++)
4785         {
4786           int j;
4787           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4788           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4789             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4790         }
4791 #endif
4792
4793       /* then add supplements */
4794       for (i = 0; i < count; i++)
4795         {
4796           Solvable *s = pool->solvables + unneededq->elements[i];
4797           if (s->supplements)
4798             {
4799               Id *dp;
4800               int k;
4801               for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
4802                 if (dep_possible(solv, *dp, &installedm))
4803                   {
4804                     Queue iq;
4805                     Id iqbuf[16];
4806                     queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
4807                     dep_pkgcheck(solv, *dp, 0, &iq);
4808                     for (k = 0; k < iq.count; k++)
4809                       {
4810                         Id p = iq.elements[k];
4811                         Solvable *sp = pool->solvables + p;
4812                         if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4813                           continue;
4814                         for (j = 0; j < count; j++)
4815                           if (p == unneededq->elements[j])
4816                             break;
4817                         /* now add edge from j + 1 to i + 1 */
4818                         queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
4819                         /* addapt following edge pointers */
4820                         for (j = j + 2; j < count + 1; j++)
4821                           edges.elements[j]++;
4822                       }
4823                     queue_free(&iq);
4824                   }
4825             }
4826         }
4827 #if 0
4828       /* print result */
4829       printf("+ supplements\n");
4830       for (i = 0; i < count; i++)
4831         {
4832           int j;
4833           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4834           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4835             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4836         }
4837 #endif
4838       map_free(&installedm);
4839
4840       /* now run SCC algo two times, first with requires+recommends+supplements,
4841        * then again without the requires. We run it the second time to get rid
4842        * of packages that got dragged in via recommends/supplements */
4843       /*
4844        * low will contain the result of the SCC search.
4845        * it must be of at least size 2 * (count + 1) and
4846        * must be zero initialized.
4847        * The layout is:
4848        *    0  low low ... low stack stack ...stack 0
4849        *            count              count
4850        */
4851       low = solv_calloc(count + 1, 2 * sizeof(Id));
4852       for (pass = 0; pass < 2; pass++)
4853         {
4854           struct trj_data trj;
4855           if (pass)
4856             {
4857               memset(low, 0, (count + 1) * (2 * sizeof(Id)));
4858               for (i = 0; i < count; i++)
4859                 {
4860                   edges.elements[i + 1] += nrequires[i];
4861                   if (!unneededq->elements[i])
4862                     low[i + 1] = -1;    /* ignore this node */
4863                 }
4864             }
4865           trj.edges = &edges;
4866           trj.low = low;
4867           trj.idx = count + 1;  /* stack starts here */
4868           for (i = 1; i <= count; i++)
4869             {
4870               if (low[i])
4871                 continue;
4872               if (edges.elements[edges.elements[i]])
4873                 {
4874                   trj.firstidx = trj.nstack = trj.idx;
4875                   trj_visit(&trj, i);
4876                 }
4877               else
4878                 {
4879                   Id myidx = trj.idx++;
4880                   low[i] = myidx;
4881                   low[myidx] = i;
4882                 }
4883             }
4884           /* prune packages */
4885           for (i = 0; i < count; i++)
4886             if (low[i + 1] <= 0)
4887               unneededq->elements[i] = 0;
4888         }
4889       solv_free(low);
4890       solv_free(nrequires);
4891       queue_free(&edges);
4892
4893       /* finally remove all pruned entries from unneededq */
4894       for (i = j = 0; i < count; i++)
4895         if (unneededq->elements[i])
4896           unneededq->elements[j++] = unneededq->elements[i];
4897       queue_truncate(unneededq, j);
4898     }
4899   map_free(&cleandepsmap);
4900 }
4901
4902
4903 void
4904 solver_breakorphans(Solver *solv)
4905 {
4906   Pool *pool = solv->pool;
4907   Repo *installed = solv->installed;
4908   int i, rid;
4909   Map m;
4910
4911   if (!installed || solv->droporphanedmap_all)
4912     return;
4913   solv->brokenorphanrules = solv_calloc(1, sizeof(Queue));
4914   queue_init(solv->brokenorphanrules);
4915   map_init(&m, installed->end - installed->start);
4916   for (i = 0; i < solv->orphaned.count; i++)
4917     {
4918       Id p = solv->orphaned.elements[i];
4919       if (pool->solvables[p].repo != installed)
4920         continue;
4921       if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - installed->start))
4922         continue;
4923       MAPSET(&m, p - installed->start);
4924     }
4925   for (rid = 1; rid < solv->pkgrules_end ; rid++)
4926     {
4927       Id p, *dp;
4928       Rule *r = solv->rules + rid;
4929       /* ignore non-deps and simple conflicts */
4930       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
4931         continue;
4932       p = -r->p;
4933       if (p < installed->start || p >= installed->end || !MAPTST(&m, p - installed->start))
4934         {
4935           /* need to check other literals */
4936           if (r->d == 0 || r->d == -1)
4937             continue;
4938           for (dp = pool->whatprovidesdata + (r->d < 0 ? -r->d - 1 : r->d); *dp < 0; dp++)
4939             {
4940               p = -*dp;
4941               if (p >= installed->start && p < installed->end && MAPTST(&m, p - installed->start))
4942                 break;
4943             }
4944           if (*dp >= 0)
4945             continue;
4946         }
4947       /* ok, disable this rule */
4948       queue_push(solv->brokenorphanrules, rid);
4949       if (r->d >= 0)
4950         solver_disablerule(solv, r);
4951     }
4952   map_free(&m);
4953   if (!solv->brokenorphanrules->count)
4954     {
4955       queue_free(solv->brokenorphanrules);
4956       solv->brokenorphanrules = solv_free(solv->brokenorphanrules);
4957     }
4958 }
4959
4960 void
4961 solver_check_brokenorphanrules(Solver *solv, Queue *dq)
4962 {
4963   Pool *pool = solv->pool;
4964   int i;
4965   Id l, pp;
4966   
4967   queue_empty(dq);
4968   if (!solv->brokenorphanrules)
4969     return;
4970   for (i = 0; i < solv->brokenorphanrules->count; i++)
4971     {
4972       int rid = solv->brokenorphanrules->elements[i];
4973       Rule *r = solv->rules + rid;
4974       FOR_RULELITERALS(l, pp, r)
4975         {
4976           if (l < 0)
4977             {
4978               if (solv->decisionmap[-l] <= 0)
4979                 break;
4980             }
4981           else
4982             {
4983               if (solv->decisionmap[l] > 0 && pool->solvables[l].repo != solv->installed)
4984                 break;
4985             }
4986         }
4987       if (l)
4988         continue;
4989       FOR_RULELITERALS(l, pp, r)
4990         if (l > 0 && solv->decisionmap[l] == 0 && pool->solvables[l].repo != solv->installed)
4991           queue_pushunique(dq, l);
4992     }
4993 }
4994