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