c9bbf818cf7c83d3a8490e9b202e7502823cd0e4
[platform/upstream/libsolv.git] / src / rules.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * rules.c
10  *
11  * SAT based dependency solver
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "solver.h"
21 #include "solver_private.h"
22 #include "bitmap.h"
23 #include "pool.h"
24 #include "poolarch.h"
25 #include "util.h"
26 #include "evr.h"
27 #include "policy.h"
28 #include "solverdebug.h"
29 #include "linkedpkg.h"
30 #include "cplxdeps.h"
31
32 #define RULES_BLOCK 63
33
34 static void addpkgruleinfo(Solver *solv, Id p, Id d, int type, Id dep);
35 static void solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded);
36
37 /*-------------------------------------------------------------------
38  * Check if dependency is possible
39  *
40  * mirrors solver_dep_fulfilled but uses map m instead of the decisionmap.
41  * used in solver_addpkgrulesforweak and solver_createcleandepsmap.
42  */
43
44 static inline int
45 dep_possible(Solver *solv, Id dep, Map *m)
46 {
47   Pool *pool = solv->pool;
48   Id p, pp;
49
50   if (ISRELDEP(dep))
51     {
52       Reldep *rd = GETRELDEP(pool, dep);
53       if (rd->flags >= 8)
54          {
55           if (rd->flags == REL_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, m);
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 pkg 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-pkg 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 pkg 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->pkgrules_end)              /* we add pkg 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->pkgrules_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 pkg 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  *** pkg 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 addpkgrule(Solver *solv, Id p, Id d, int type, Id dep)
446 {
447   if (!solv->ruleinfoq)
448     solver_addrule(solv, p, d);
449   else
450     addpkgruleinfo(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     addpkgrule(solv, qr->elements[0], -(s - pool->solvables), SOLVER_RULE_PKG_REQUIRES, req);
473   else
474     addpkgrule(solv, -(s - pool->solvables), pool_queuetowhatprovides(pool, qr), SOLVER_RULE_PKG_REQUIRES, req);
475   if (qp->count > 1)
476     {
477       Id d = pool_queuetowhatprovides(pool, qp);
478       for (i = 0; i < qr->count; i++)
479         addpkgrule(solv, -qr->elements[i], d, SOLVER_RULE_PKG_REQUIRES, prv);
480     }
481   else if (qp->count)
482     {
483       for (i = 0; i < qr->count; i++)
484         addpkgrule(solv, qp->elements[0], -qr->elements[i], SOLVER_RULE_PKG_REQUIRES, prv);
485     }
486   if (!m)
487     return;     /* nothing more to do if called from getpkgruleinfos() */
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_PKG_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           addpkgrule(solv, -p, 0, type == SOLVER_RULE_PKG_REQUIRES ? SOLVER_RULE_PKG_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               addpkgrule(solv, -p, 0, SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP, dep);
592               continue;
593             }
594           addpkgrule(solv, -p, dp - pool->whatprovidesdata, SOLVER_RULE_PKG_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_PKG_CONFLICTS)
615                 {
616                   if (pool->forbidselfconflicts && !is_otherproviders_dep(pool, dep))
617                     addpkgrule(solv, -p, 0, SOLVER_RULE_PKG_SELF_CONFLICT, dep);
618                   continue;
619                 }
620               addpkgrule(solv, -p, 0, type, dep);
621               continue;
622             }
623           if (p2 > 0)
624             addpkgrule(solv, p2, -p, type, dep);        /* hack so that we don't need pool_queuetowhatprovides */
625           else
626             addpkgrule(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               addpkgrule(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_addpkgrulesforsolvable(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 dependency graph */
755           && !(solv->fixmap.size && MAPTST(&solv->fixmap, n - installed->start)))
756         {
757           dontfix = 1;                  /* dont care about broken 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               addpkgrule(solv, -n, 0, SOLVER_RULE_PKG_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_PKG_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                   addpkgrule(solv, -n, 0, SOLVER_RULE_PKG_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               addpkgrule(solv, -n, dp - pool->whatprovidesdata, SOLVER_RULE_PKG_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_PKG_CONFLICTS, 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                       addpkgrule(solv, -n, 0, SOLVER_RULE_PKG_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                   addpkgrule(solv, -n, p == SYSTEMSOLVABLE ? 0 : -p, SOLVER_RULE_PKG_CONFLICTS, 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                         addpkgrule(solv, -n, -p, SOLVER_RULE_PKG_OBSOLETES, obs);
935                       else
936                         addpkgrule(solv, -n, -p, SOLVER_RULE_PKG_INSTALLED_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                     addpkgrule(solv, -n, -p, SOLVER_RULE_PKG_SAME_NAME, 0);
964                   else
965                     addpkgrule(solv, -n, -p, SOLVER_RULE_PKG_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_addpkgrulesforlinked(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_addpkgrulesforsolvable(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_addpkgrulesforweak(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_addpkgrulesforsolvable(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_addpkgrulesforupdaters(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_addpkgrulesforsolvable(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_addpkgrulesforsolvable(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 addpkgruleinfo(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_PKG_CONFLICTS && !w2)
2441         w2 = -SYSTEMSOLVABLE;
2442     }
2443   /* yep, rule matches. record info */
2444   queue_push(solv->ruleinfoq, type);
2445   if (type == SOLVER_RULE_PKG_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 getpkgruleinfos(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_addpkgrulesforsolvable(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->pkgrules_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   getpkgruleinfos(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->pkgrules_end)
2569     {
2570       Queue rq;
2571       int i;
2572
2573       if (r->p >= 0)
2574         return SOLVER_RULE_PKG;
2575       if (fromp)
2576         *fromp = -r->p;
2577       queue_init(&rq);
2578       getpkgruleinfos(solv, r, &rq);
2579       type = SOLVER_RULE_PKG;
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_PKG || 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->pkgrules_end)
2692     return SOLVER_RULE_PKG;
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 Id
2767 solver_rule2pkgrule(Solver *solv, Id rid)
2768 {
2769   if (rid >= solv->choicerules && rid < solv->choicerules_end)
2770     return solv->choicerules_ref[rid - solv->choicerules];
2771   return 0;
2772 }
2773
2774 static void
2775 solver_rule2rules_rec(Solver *solv, Id rid, Queue *q, Map *seen)
2776 {
2777   int i;
2778   Id rid2;
2779
2780   if (seen)
2781     MAPSET(seen, rid);
2782   for (i = solv->learnt_why.elements[rid - solv->learntrules]; (rid2 = solv->learnt_pool.elements[i]) != 0; i++)
2783     {
2784       if (seen)
2785         {
2786           if (MAPTST(seen, rid2))
2787             continue;
2788           if (rid2 >= solv->learntrules)
2789             solver_rule2rules_rec(solv, rid2, q, seen);
2790           continue;
2791         }
2792       queue_push(q, rid2);
2793     }
2794 }
2795
2796 /* learnt rule introspection */
2797 void
2798 solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
2799 {
2800   queue_empty(q);
2801   if (rid < solv->learntrules || rid >= solv->nrules)
2802     return;
2803   if (recursive)
2804     {
2805       Map seen;
2806       map_init(&seen, solv->nrules);
2807       solver_rule2rules_rec(solv, rid, q, &seen);
2808       map_free(&seen);
2809     }
2810   else
2811     solver_rule2rules_rec(solv, rid, q, 0);
2812 }
2813
2814
2815 /* check if the newest versions of pi still provides the dependency we're looking for */
2816 static int
2817 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m)
2818 {
2819   Pool *pool = solv->pool;
2820   Rule *ur;
2821   Queue q;
2822   Id p, pp, qbuf[32];
2823   int i;
2824
2825   ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
2826   if (!ur->p)
2827     ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
2828   if (!ur->p)
2829     return 0;
2830   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
2831   FOR_RULELITERALS(p, pp, ur)
2832     if (p > 0)
2833       queue_push(&q, p);
2834   if (q.count > 1)
2835     policy_filter_unwanted(solv, &q, POLICY_MODE_CHOOSE);
2836   for (i = 0; i < q.count; i++)
2837     if (MAPTST(m, q.elements[i]))
2838       break;
2839   /* 1: none of the newest versions provide it */
2840   i = i == q.count ? 1 : 0;
2841   queue_free(&q);
2842   return i;
2843 }
2844
2845 static inline void
2846 queue_removeelement(Queue *q, Id el)
2847 {
2848   int i, j;
2849   for (i = 0; i < q->count; i++)
2850     if (q->elements[i] == el)
2851       break;
2852   if (i < q->count)
2853     {
2854       for (j = i++; i < q->count; i++)
2855         if (q->elements[i] != el)
2856           q->elements[j++] = q->elements[i];
2857       queue_truncate(q, j);
2858     }
2859 }
2860
2861 void
2862 solver_addchoicerules(Solver *solv)
2863 {
2864   Pool *pool = solv->pool;
2865   Map m, mneg;
2866   Rule *r;
2867   Queue q, qi;
2868   int i, j, rid, havechoice;
2869   Id p, d, pp;
2870   Id p2, pp2;
2871   Solvable *s, *s2;
2872   Id lastaddedp, lastaddedd;
2873   int lastaddedcnt;
2874   unsigned int now;
2875
2876   solv->choicerules = solv->nrules;
2877   if (!pool->installed)
2878     {
2879       solv->choicerules_end = solv->nrules;
2880       return;
2881     }
2882   now = solv_timems(0);
2883   solv->choicerules_ref = solv_calloc(solv->pkgrules_end, sizeof(Id));
2884   queue_init(&q);
2885   queue_init(&qi);
2886   map_init(&m, pool->nsolvables);
2887   map_init(&mneg, pool->nsolvables);
2888   /* set up negative assertion map from infarch and dup rules */
2889   for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
2890     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2891       MAPSET(&mneg, -r->p);
2892   for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
2893     if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
2894       MAPSET(&mneg, -r->p);
2895   lastaddedp = 0;
2896   lastaddedd = 0;
2897   lastaddedcnt = 0;
2898   for (rid = 1; rid < solv->pkgrules_end ; rid++)
2899     {
2900       r = solv->rules + rid;
2901       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
2902         continue;       /* only look at requires rules */
2903       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
2904       queue_empty(&q);
2905       queue_empty(&qi);
2906       havechoice = 0;
2907       FOR_RULELITERALS(p, pp, r)
2908         {
2909           if (p < 0)
2910             continue;
2911           s = pool->solvables + p;
2912           if (!s->repo)
2913             continue;
2914           if (s->repo == pool->installed)
2915             {
2916               queue_push(&q, p);
2917               continue;
2918             }
2919           /* check if this package is "blocked" by a installed package */
2920           s2 = 0;
2921           FOR_PROVIDES(p2, pp2, s->name)
2922             {
2923               s2 = pool->solvables + p2;
2924               if (s2->repo != pool->installed)
2925                 continue;
2926               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2927                 continue;
2928               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
2929                 continue;
2930               break;
2931             }
2932           if (p2)
2933             {
2934               /* found installed package p2 that we can update to p */
2935               if (MAPTST(&mneg, p))
2936                 continue;
2937               if (policy_is_illegal(solv, s2, s, 0))
2938                 continue;
2939 #if 0
2940               if (solver_choicerulecheck(solv, p2, r, &m))
2941                 continue;
2942               queue_push(&qi, p2);
2943 #else
2944               queue_push2(&qi, p2, p);
2945 #endif
2946               queue_push(&q, p);
2947               continue;
2948             }
2949           if (s->obsoletes)
2950             {
2951               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2952               s2 = 0;
2953               while ((obs = *obsp++) != 0)
2954                 {
2955                   FOR_PROVIDES(p2, pp2, obs)
2956                     {
2957                       s2 = pool->solvables + p2;
2958                       if (s2->repo != pool->installed)
2959                         continue;
2960                       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
2961                         continue;
2962                       if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2963                         continue;
2964                       break;
2965                     }
2966                   if (p2)
2967                     break;
2968                 }
2969               if (obs)
2970                 {
2971                   /* found installed package p2 that we can update to p */
2972                   if (MAPTST(&mneg, p))
2973                     continue;
2974                   if (policy_is_illegal(solv, s2, s, 0))
2975                     continue;
2976 #if 0
2977                   if (solver_choicerulecheck(solv, p2, r, &m))
2978                     continue;
2979                   queue_push(&qi, p2);
2980 #else
2981                   queue_push2(&qi, p2, p);
2982 #endif
2983                   queue_push(&q, p);
2984                   continue;
2985                 }
2986             }
2987           /* package p is independent of the installed ones */
2988           havechoice = 1;
2989         }
2990       if (!havechoice || !q.count || !qi.count)
2991         continue;       /* no choice */
2992
2993       FOR_RULELITERALS(p, pp, r)
2994         if (p > 0)
2995           MAPSET(&m, p);
2996
2997       /* do extra checking */
2998       for (i = j = 0; i < qi.count; i += 2)
2999         {
3000           p2 = qi.elements[i];
3001           if (!p2)
3002             continue;
3003           if (solver_choicerulecheck(solv, p2, r, &m))
3004             {
3005               /* oops, remove element p from q */
3006               queue_removeelement(&q, qi.elements[i + 1]);
3007               continue;
3008             }
3009           qi.elements[j++] = p2;
3010         }
3011       queue_truncate(&qi, j);
3012       if (!q.count || !qi.count)
3013         {
3014           FOR_RULELITERALS(p, pp, r)
3015             if (p > 0)
3016               MAPCLR(&m, p);
3017           continue;
3018         }
3019
3020
3021       /* now check the update rules of the installed package.
3022        * if all packages of the update rules are contained in
3023        * the dependency rules, there's no need to set up the choice rule */
3024       for (i = 0; i < qi.count; i++)
3025         {
3026           Rule *ur;
3027           if (!qi.elements[i])
3028             continue;
3029           ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
3030           if (!ur->p)
3031             ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
3032           if (!ur->p)
3033             continue;
3034           FOR_RULELITERALS(p, pp, ur)
3035             if (!MAPTST(&m, p))
3036               break;
3037           if (p)
3038             break;
3039           for (j = i + 1; j < qi.count; j++)
3040             if (qi.elements[i] == qi.elements[j])
3041               qi.elements[j] = 0;
3042         }
3043       /* empty map again */
3044       FOR_RULELITERALS(p, pp, r)
3045         if (p > 0)
3046           MAPCLR(&m, p);
3047       if (i == qi.count)
3048         {
3049 #if 0
3050           printf("skipping choice ");
3051           solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3052 #endif
3053           continue;
3054         }
3055
3056       /* don't add identical rules */
3057       if (lastaddedp == r->p && lastaddedcnt == q.count)
3058         {
3059           for (i = 0; i < q.count; i++)
3060             if (q.elements[i] != pool->whatprovidesdata[lastaddedd + i])
3061               break;
3062           if (i == q.count)
3063             continue;   /* already added that one */
3064         }
3065       d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
3066
3067       lastaddedp = r->p;
3068       lastaddedd = d;
3069       lastaddedcnt = q.count;
3070
3071       solver_addrule(solv, r->p, d);
3072       queue_push(&solv->weakruleq, solv->nrules - 1);
3073       solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
3074 #if 0
3075       printf("OLD ");
3076       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + rid);
3077       printf("WEAK CHOICE ");
3078       solver_printrule(solv, SOLV_DEBUG_RESULT, solv->rules + solv->nrules - 1);
3079 #endif
3080     }
3081   queue_free(&q);
3082   queue_free(&qi);
3083   map_free(&m);
3084   map_free(&mneg);
3085   solv->choicerules_end = solv->nrules;
3086   /* shrink choicerules_ref */
3087   solv->choicerules_ref = solv_realloc2(solv->choicerules_ref, solv->choicerules_end - solv->choicerules, sizeof(Id));
3088   POOL_DEBUG(SOLV_DEBUG_STATS, "choice rule creation took %d ms\n", solv_timems(now));
3089 }
3090
3091 /* called when a choice rule is disabled by analyze_unsolvable. We also
3092  * have to disable all other choice rules so that the best packages get
3093  * picked */
3094 void
3095 solver_disablechoicerules(Solver *solv, Rule *r)
3096 {
3097   Id rid, p, pp;
3098   Pool *pool = solv->pool;
3099   Map m;
3100   Rule *or;
3101
3102   or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3103   map_init(&m, pool->nsolvables);
3104   FOR_RULELITERALS(p, pp, or)
3105     if (p > 0)
3106       MAPSET(&m, p);
3107   FOR_RULELITERALS(p, pp, r)
3108     if (p > 0)
3109       MAPCLR(&m, p);
3110   for (rid = solv->choicerules; rid < solv->choicerules_end; rid++)
3111     {
3112       r = solv->rules + rid;
3113       if (r->d < 0)
3114         continue;
3115       or = solv->rules + solv->choicerules_ref[(r - solv->rules) - solv->choicerules];
3116       FOR_RULELITERALS(p, pp, or)
3117         if (p > 0 && MAPTST(&m, p))
3118           break;
3119       if (p)
3120         solver_disablerule(solv, r);
3121     }
3122 }
3123
3124 static void
3125 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
3126 {
3127   int i, j;
3128   Id p, *cp2;
3129   for (i = j = 0; i < q->count; i++)
3130     {
3131       p = q->elements[i];
3132       for (cp2 = cp; *cp2; cp2++)
3133         if (*cp2 == p)
3134           {
3135             q->elements[j++] = p;
3136             break;
3137           }
3138     }
3139   queue_truncate(q, j);
3140 }
3141
3142 static void
3143 prune_to_dup_packages(Solver *solv, Id p, Queue *q)
3144 {
3145   int i, j;
3146   for (i = j = 0; i < q->count; i++)
3147     {
3148       Id p = q->elements[i];
3149       if (MAPTST(&solv->dupmap, p))
3150         q->elements[j++] = p;
3151     }
3152   queue_truncate(q, j);
3153 }
3154
3155 void
3156 solver_addbestrules(Solver *solv, int havebestinstalljobs)
3157 {
3158   Pool *pool = solv->pool;
3159   Id p;
3160   Solvable *s;
3161   Repo *installed = solv->installed;
3162   Queue q, q2;
3163   Rule *r;
3164   Queue r2pkg;
3165   int i, oldcnt;
3166
3167   solv->bestrules = solv->nrules;
3168   if (!installed)
3169     {
3170       solv->bestrules_end = solv->nrules;
3171       return;
3172     }
3173   queue_init(&q);
3174   queue_init(&q2);
3175   queue_init(&r2pkg);
3176
3177   if (havebestinstalljobs)
3178     {
3179       for (i = 0; i < solv->job.count; i += 2)
3180         {
3181           if ((solv->job.elements[i] & (SOLVER_JOBMASK | SOLVER_FORCEBEST)) == (SOLVER_INSTALL | SOLVER_FORCEBEST))
3182             {
3183               int j;
3184               Id p2, pp2;
3185               for (j = 0; j < solv->ruletojob.count; j++)
3186                 if (solv->ruletojob.elements[j] == i)
3187                   break;
3188               if (j == solv->ruletojob.count)
3189                 continue;
3190               r = solv->rules + solv->jobrules + j;
3191               queue_empty(&q);
3192               FOR_RULELITERALS(p2, pp2, r)
3193                 if (p2 > 0)
3194                   queue_push(&q, p2);
3195               if (!q.count)
3196                 continue;       /* orphaned */
3197               /* select best packages, just look at prio and version */
3198               oldcnt = q.count;
3199               policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3200               if (q.count == oldcnt)
3201                 continue;       /* nothing filtered */
3202               p2 = queue_shift(&q);
3203               solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
3204               queue_push(&r2pkg, -(solv->jobrules + j));
3205             }
3206         }
3207     }
3208
3209   if (solv->bestupdatemap_all || solv->bestupdatemap.size)
3210     {
3211       FOR_REPO_SOLVABLES(installed, p, s)
3212         {
3213           Id d, p2, pp2;
3214           if (!solv->updatemap_all && (!solv->updatemap.size || !MAPTST(&solv->updatemap, p - installed->start)))
3215             continue;
3216           if (!solv->bestupdatemap_all && (!solv->bestupdatemap.size || !MAPTST(&solv->bestupdatemap, p - installed->start)))
3217             continue;
3218           queue_empty(&q);
3219           if (solv->bestobeypolicy)
3220             r = solv->rules + solv->updaterules + (p - installed->start);
3221           else
3222             {
3223               r = solv->rules + solv->featurerules + (p - installed->start);
3224               if (!r->p)        /* identical to update rule? */
3225                 r = solv->rules + solv->updaterules + (p - installed->start);
3226             }
3227           if (solv->specialupdaters && (d = solv->specialupdaters[p - installed->start]) != 0 && r == solv->rules + solv->updaterules + (p - installed->start))
3228             {
3229               /* need to check specialupdaters */
3230               if (r->p == p)    /* be careful with the dup case */
3231                 queue_push(&q, p);
3232               while ((p2 = pool->whatprovidesdata[d++]) != 0)
3233                 queue_push(&q, p2);
3234             }
3235           else
3236             {
3237               FOR_RULELITERALS(p2, pp2, r)
3238                 if (p2 > 0)
3239                   queue_push(&q, p2);
3240             }
3241           if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3242             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q);
3243           if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3244             prune_to_dup_packages(solv, p, &q);
3245           /* select best packages, just look at prio and version */
3246           policy_filter_unwanted(solv, &q, POLICY_MODE_RECOMMEND);
3247           if (!q.count)
3248             continue;   /* orphaned */
3249           if (solv->bestobeypolicy)
3250             {
3251               /* also filter the best of the feature rule packages and add them */
3252               r = solv->rules + solv->featurerules + (p - installed->start);
3253               if (r->p)
3254                 {
3255                   int j;
3256                   queue_empty(&q2);
3257                   FOR_RULELITERALS(p2, pp2, r)
3258                     if (p2 > 0)
3259                       queue_push(&q2, p2);
3260                   if (solv->update_targets && solv->update_targets->elements[p - installed->start])
3261                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[p - installed->start], &q2);
3262                   if (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))
3263                     prune_to_dup_packages(solv, p, &q2);
3264                   policy_filter_unwanted(solv, &q2, POLICY_MODE_RECOMMEND);
3265                   for (j = 0; j < q2.count; j++)
3266                     queue_pushunique(&q, q2.elements[j]);
3267                 }
3268             }
3269           p2 = queue_shift(&q);
3270           solver_addrule(solv, p2, q.count ? pool_queuetowhatprovides(pool, &q) : 0);
3271           queue_push(&r2pkg, p);
3272         }
3273     }
3274   if (r2pkg.count)
3275     solv->bestrules_pkg = solv_memdup2(r2pkg.elements, r2pkg.count, sizeof(Id));
3276   solv->bestrules_end = solv->nrules;
3277   queue_free(&q);
3278   queue_free(&q2);
3279   queue_free(&r2pkg);
3280 }
3281
3282
3283
3284
3285 /* yumobs rule handling */
3286
3287 static void
3288 find_obsolete_group(Solver *solv, Id obs, Queue *q)
3289 {
3290   Pool *pool = solv->pool;
3291   Queue qn;
3292   Id p2, pp2, op, *opp, opp2;
3293   int i, j, qnc, ncnt;
3294
3295   queue_empty(q);
3296   FOR_PROVIDES(p2, pp2, obs)
3297     {
3298       Solvable *s2 = pool->solvables + p2;
3299       if (s2->repo != pool->installed)
3300         continue;
3301       if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3302         continue;
3303       /* we obsolete installed package s2 with obs. now find all other packages that have the same dep  */
3304       for (opp = solv->obsoletes_data + solv->obsoletes[p2 - solv->installed->start]; (op = *opp++) != 0;)
3305         {
3306           Solvable *os = pool->solvables + op;
3307           Id obs2, *obsp2;
3308           if (!os->obsoletes)
3309             continue;
3310           if (pool->obsoleteusescolors && !pool_colormatch(pool, s2, os))
3311             continue;
3312           obsp2 = os->repo->idarraydata + os->obsoletes; 
3313           while ((obs2 = *obsp2++) != 0)
3314             if (obs2 == obs)
3315               break;
3316           if (obs2)
3317             queue_pushunique(q, op);
3318         }
3319       /* also search packages with the same name */
3320       FOR_PROVIDES(op, opp2, s2->name)
3321         {
3322           Solvable *os = pool->solvables + op;
3323           Id obs2, *obsp2;
3324           if (os->name != s2->name)
3325             continue;
3326           if (!os->obsoletes)
3327             continue;
3328           if (pool->obsoleteusescolors && !pool_colormatch(pool, s2, os))
3329             continue;
3330           obsp2 = os->repo->idarraydata + os->obsoletes; 
3331           while ((obs2 = *obsp2++) != 0)
3332             if (obs2 == obs)
3333               break;
3334           if (obs2)
3335             queue_pushunique(q, op);
3336         }
3337     }
3338   /* find names so that we can build groups */
3339   queue_init_clone(&qn, q);
3340   prune_to_best_version(solv->pool, &qn);
3341 #if 0
3342 {
3343   for (i = 0; i < qn.count; i++)
3344     printf(" + %s\n", pool_solvid2str(pool, qn.elements[i]));
3345 }
3346 #endif
3347   /* filter into name groups */
3348   qnc = qn.count;
3349   if (qnc == 1)
3350     {
3351       queue_free(&qn);
3352       queue_empty(q);
3353       return;
3354     }
3355   ncnt = 0;
3356   for (i = 0; i < qnc; i++)
3357     {
3358       Id n = pool->solvables[qn.elements[i]].name;
3359       int got = 0;
3360       for (j = 0; j < q->count; j++)
3361         {
3362           Id p = q->elements[j];
3363           if (pool->solvables[p].name == n)
3364             {
3365               queue_push(&qn, p);
3366               got = 1;
3367             }
3368         }
3369       if (got)
3370         {
3371           queue_push(&qn, 0);
3372           ncnt++;
3373         }
3374     }
3375   if (ncnt <= 1)
3376     {
3377       queue_empty(q);
3378     }
3379   else
3380     {
3381       queue_empty(q);
3382       queue_insertn(q, 0, qn.count - qnc, qn.elements + qnc);
3383     }
3384   queue_free(&qn);
3385 }
3386
3387 void
3388 solver_addyumobsrules(Solver *solv)
3389 {
3390   Pool *pool = solv->pool;
3391   Repo *installed = solv->installed;
3392   Id p, op, *opp;
3393   Solvable *s;
3394   Queue qo, qq, yumobsinfoq;
3395   int i, j, k;
3396   unsigned int now;
3397
3398   solv->yumobsrules = solv->nrules;
3399   if (!installed || !solv->obsoletes)
3400     {
3401       solv->yumobsrules_end = solv->nrules;
3402       return;
3403     }
3404   now = solv_timems(0);
3405   queue_init(&qo);
3406   FOR_REPO_SOLVABLES(installed, p, s)
3407     {
3408       if (!solv->obsoletes[p - installed->start])
3409         continue;
3410 #if 0
3411 printf("checking yumobs for %s\n", pool_solvable2str(pool, s));
3412 #endif
3413       queue_empty(&qo);
3414       for (opp = solv->obsoletes_data + solv->obsoletes[p - installed->start]; (op = *opp++) != 0;)
3415         {
3416           Solvable *os = pool->solvables + op;
3417           Id obs, *obsp = os->repo->idarraydata + os->obsoletes;
3418           Id p2, pp2;
3419           while ((obs = *obsp++) != 0)
3420             {
3421               FOR_PROVIDES(p2, pp2, obs)
3422                 {
3423                   Solvable *s2 = pool->solvables + p2;
3424                   if (s2->repo != installed)
3425                     continue;
3426                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
3427                     continue;
3428                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
3429                     continue;
3430                   queue_pushunique(&qo, obs);
3431                   break;
3432                 }
3433             }
3434         }
3435     }
3436   if (!qo.count)
3437     {
3438       queue_free(&qo);
3439       return;
3440     }
3441   queue_init(&yumobsinfoq);
3442   queue_init(&qq);
3443   for (i = 0; i < qo.count; i++)
3444     {
3445       int group, groupk, groupstart;
3446       queue_empty(&qq);
3447 #if 0
3448 printf("investigating %s\n", pool_dep2str(pool, qo.elements[i]));
3449 #endif
3450       find_obsolete_group(solv, qo.elements[i], &qq);
3451 #if 0
3452 printf("result:\n");
3453 for (j = 0; j < qq.count; j++)
3454   if (qq.elements[j] == 0)
3455     printf("---\n");
3456   else
3457     printf("%s\n", pool_solvid2str(pool, qq.elements[j]));
3458 #endif
3459   
3460       if (!qq.count)
3461         continue;
3462       /* at least two goups, build rules */
3463       group = 0;
3464       for (j = 0; j < qq.count; j++)
3465         {
3466           p = qq.elements[j];
3467           if (!p)
3468             {
3469               group++;
3470               continue;
3471             }
3472           if (pool->solvables[p].repo == installed)
3473             continue;
3474           groupk = 0;
3475           groupstart = 0;
3476           for (k = 0; k < qq.count; k++)
3477             {
3478               Id pk = qq.elements[k];
3479               if (pk)
3480                 continue;
3481               if (group != groupk && k > groupstart)
3482                 {
3483                   /* add the rule */
3484                   Queue qhelper;
3485                   memset(&qhelper, 0, sizeof(qhelper));
3486                   qhelper.count = k - groupstart;
3487                   qhelper.elements = qq.elements + groupstart;
3488                   solver_addrule(solv, -p, pool_queuetowhatprovides(pool, &qhelper));
3489                   queue_push(&yumobsinfoq, qo.elements[i]);
3490                 }
3491               groupstart = k + 1;
3492               groupk++;
3493             }
3494         }
3495     }
3496   if (yumobsinfoq.count)
3497     solv->yumobsrules_info = solv_memdup2(yumobsinfoq.elements, yumobsinfoq.count, sizeof(Id));
3498   queue_free(&yumobsinfoq);
3499   queue_free(&qq);
3500   queue_free(&qo);
3501   solv->yumobsrules_end = solv->nrules;
3502   POOL_DEBUG(SOLV_DEBUG_STATS, "yumobs rule creation took %d ms\n", solv_timems(now));
3503 }
3504
3505 #undef CLEANDEPSDEBUG
3506
3507 /*
3508  * This functions collects all packages that are looked at
3509  * when a dependency is checked. We need it to "pin" installed
3510  * packages when removing a supplemented package in createcleandepsmap.
3511  * Here's an not uncommon example:
3512  *   A contains "Supplements: packageand(B, C)"
3513  *   B contains "Requires: A"
3514  * Now if we remove C, the supplements is no longer true,
3515  * thus we also remove A. Without the dep_pkgcheck function, we
3516  * would now also remove B, but this is wrong, as adding back
3517  * C doesn't make the supplements true again. Thus we "pin" B
3518  * when we remove A.
3519  * There's probably a better way to do this, but I haven't come
3520  * up with it yet ;)
3521  */
3522 static inline void
3523 dep_pkgcheck(Solver *solv, Id dep, Map *m, Queue *q)
3524 {
3525   Pool *pool = solv->pool;
3526   Id p, pp;
3527
3528   if (ISRELDEP(dep))
3529     {
3530       Reldep *rd = GETRELDEP(pool, dep);
3531       if (rd->flags >= 8)
3532         {
3533           if (rd->flags == REL_AND)
3534             {
3535               dep_pkgcheck(solv, rd->name, m, q);
3536               dep_pkgcheck(solv, rd->evr, m, q);
3537               return;
3538             }
3539           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3540             return;
3541           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
3542             return;
3543         }
3544     }
3545   FOR_PROVIDES(p, pp, dep)
3546     if (!m || MAPTST(m, p))
3547       queue_push(q, p);
3548 }
3549
3550 static int
3551 check_xsupp(Solver *solv, Queue *depq, Id dep)
3552 {
3553   Pool *pool = solv->pool;
3554   Id p, pp;
3555
3556   if (ISRELDEP(dep))
3557     {
3558       Reldep *rd = GETRELDEP(pool, dep);
3559       if (rd->flags >= 8)
3560         {
3561           if (rd->flags == REL_AND)
3562             {
3563               if (!check_xsupp(solv, depq, rd->name))
3564                 return 0;
3565               return check_xsupp(solv, depq, rd->evr);
3566             }
3567           if (rd->flags == REL_OR)
3568             {
3569               if (check_xsupp(solv, depq, rd->name))
3570                 return 1;
3571               return check_xsupp(solv, depq, rd->evr);
3572             }
3573           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
3574 #if 0
3575             return solver_splitprovides(solv, rd->evr);
3576 #else
3577             return 0;
3578 #endif
3579           if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
3580             return solver_dep_installed(solv, rd->evr);
3581         }
3582       if (depq && rd->flags == REL_NAMESPACE)
3583         {
3584           int i;
3585           for (i = 0; i < depq->count; i++)
3586             if (depq->elements[i] == dep || depq->elements[i] == rd->name)
3587              return 1;
3588         }
3589     }
3590   FOR_PROVIDES(p, pp, dep)
3591     if (p == SYSTEMSOLVABLE || pool->solvables[p].repo == solv->installed)
3592       return 1;
3593   return 0;
3594 }
3595
3596 static inline int
3597 queue_contains(Queue *q, Id id)
3598 {
3599   int i;
3600   for (i = 0; i < q->count; i++)
3601     if (q->elements[i] == id)
3602       return 1;
3603   return 0;
3604 }
3605
3606 #ifdef ENABLE_COMPLEX_DEPS
3607 static void
3608 complex_cleandeps_remove(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq)
3609 {
3610   int i;
3611   Queue dq;
3612   Id p;
3613
3614   queue_init(&dq);
3615   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3616   if (i == 0 || i == 1)
3617     {
3618       queue_free(&dq);
3619       return;
3620     }
3621   for (i = 0; i < dq.count; i++)
3622     {
3623       for (; (p = dq.elements[i]) != 0; i++)
3624         {
3625           if (p < 0)
3626             {
3627               if (!MAPTST(installedm, -p))
3628                 break;
3629               continue;
3630             }
3631           if (p != SYSTEMSOLVABLE && MAPTST(im, p))
3632             {
3633 #ifdef CLEANDEPSDEBUG
3634               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3635 #endif
3636               queue_push(iq, p);
3637             }
3638         }
3639       while (dq.elements[i])
3640         i++;
3641     }
3642   queue_free(&dq);
3643 }
3644
3645 static void
3646 complex_cleandeps_addback(Pool *pool, Id ip, Id req, Map *im, Map *installedm, Queue *iq, Map *userinstalled)
3647 {
3648   int i, blk;
3649   Queue dq;
3650   Id p;
3651
3652   queue_init(&dq);
3653   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
3654   if (i == 0 || i == 1)
3655     {
3656       queue_free(&dq);
3657       return;
3658     }
3659   for (i = 0; i < dq.count; i++)
3660     {
3661       blk = i;
3662       for (; (p = dq.elements[i]) != 0; i++)
3663         {
3664           if (p < 0)
3665             {
3666               if (!MAPTST(installedm, -p))
3667                 break;
3668               continue;
3669             }
3670           if (MAPTST(im, p))
3671             break;
3672         }
3673       if (!p)
3674         {
3675           for (i = blk; (p = dq.elements[i]) != 0; i++)
3676             {
3677               if (p < 0)
3678                 continue;
3679               if (!MAPTST(installedm, p))
3680                 continue;
3681               if (p == ip || MAPTST(userinstalled, p - pool->installed->start))
3682                 continue;
3683 #ifdef CLEANDEPSDEBUG
3684               printf("%s requires/recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
3685 #endif
3686               MAPSET(im, p);
3687               queue_push(iq, p);
3688             }
3689         }
3690       while (dq.elements[i])
3691         i++;
3692     }
3693   queue_free(&dq);
3694 }
3695
3696 #endif
3697
3698 /*
3699  * Find all installed packages that are no longer
3700  * needed regarding the current solver job.
3701  *
3702  * The algorithm is:
3703  * - remove pass: remove all packages that could have
3704  *   been dragged in by the obsoleted packages.
3705  *   i.e. if package A is obsolete and contains "Requires: B",
3706  *   also remove B, as installing A will have pulled in B.
3707  *   after this pass, we have a set of still installed packages
3708  *   with broken dependencies.
3709  * - add back pass:
3710  *   now add back all packages that the still installed packages
3711  *   require.
3712  *
3713  * The cleandeps packages are the packages removed in the first
3714  * pass and not added back in the second pass.
3715  *
3716  * If we search for unneeded packages (unneeded is true), we
3717  * simply remove all packages except the userinstalled ones in
3718  * the first pass.
3719  */
3720 static void
3721 solver_createcleandepsmap(Solver *solv, Map *cleandepsmap, int unneeded)
3722 {
3723   Pool *pool = solv->pool;
3724   Repo *installed = solv->installed;
3725   Queue *job = &solv->job;
3726   Map userinstalled;
3727   Map im;
3728   Map installedm;
3729   Rule *r;
3730   Id rid, how, what, select;
3731   Id p, pp, ip, jp;
3732   Id req, *reqp, sup, *supp;
3733   Solvable *s;
3734   Queue iq, iqcopy, xsuppq;
3735   int i;
3736
3737   map_empty(cleandepsmap);
3738   if (!installed || installed->end == installed->start)
3739     return;
3740   map_init(&userinstalled, installed->end - installed->start);
3741   map_init(&im, pool->nsolvables);
3742   map_init(&installedm, pool->nsolvables);
3743   queue_init(&iq);
3744   queue_init(&xsuppq);
3745
3746   for (i = 0; i < job->count; i += 2)
3747     {
3748       how = job->elements[i];
3749       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
3750         {
3751           what = job->elements[i + 1];
3752           select = how & SOLVER_SELECTMASK;
3753           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
3754             FOR_REPO_SOLVABLES(installed, p, s)
3755               MAPSET(&userinstalled, p - installed->start);
3756           FOR_JOB_SELECT(p, pp, select, what)
3757             if (pool->solvables[p].repo == installed)
3758               MAPSET(&userinstalled, p - installed->start);
3759         }
3760       if ((how & (SOLVER_JOBMASK | SOLVER_SELECTMASK)) == (SOLVER_ERASE | SOLVER_SOLVABLE_PROVIDES))
3761         {
3762           what = job->elements[i + 1];
3763           if (ISRELDEP(what))
3764             {
3765               Reldep *rd = GETRELDEP(pool, what);
3766               if (rd->flags != REL_NAMESPACE)
3767                 continue;
3768               if (rd->evr == 0)
3769                 {
3770                   queue_pushunique(&iq, rd->name);
3771                   continue;
3772                 }
3773               FOR_PROVIDES(p, pp, what)
3774                 if (p)
3775                   break;
3776               if (p)
3777                 continue;
3778               queue_pushunique(&iq, what);
3779             }
3780         }
3781     }
3782
3783   /* have special namespace cleandeps erases */
3784   if (iq.count)
3785     {
3786       for (ip = solv->installed->start; ip < solv->installed->end; ip++)
3787         {
3788           s = pool->solvables + ip;
3789           if (s->repo != installed)
3790             continue;
3791           if (!s->supplements)
3792             continue;
3793           supp = s->repo->idarraydata + s->supplements;
3794           while ((sup = *supp++) != 0)
3795             if (check_xsupp(solv, &iq, sup) && !check_xsupp(solv, 0, sup))
3796               {
3797 #ifdef CLEANDEPSDEBUG
3798                 printf("xsupp %s from %s\n", pool_dep2str(pool, sup), pool_solvid2str(pool, ip));
3799 #endif
3800                 queue_pushunique(&xsuppq, sup);
3801               }
3802         }
3803       queue_empty(&iq);
3804     }
3805
3806   /* also add visible patterns to userinstalled for openSUSE */
3807   if (1)
3808     {
3809       Dataiterator di;
3810       dataiterator_init(&di, pool, 0, 0, SOLVABLE_ISVISIBLE, 0, 0);
3811       while (dataiterator_step(&di))
3812         {
3813           Id *dp;
3814           if (di.solvid <= 0)
3815             continue;
3816           s = pool->solvables + di.solvid;
3817           if (!s->repo || !s->requires)
3818             continue;
3819           if (s->repo != installed && !pool_installable(pool, s))
3820             continue;
3821           if (strncmp(pool_id2str(pool, s->name), "pattern:", 8) != 0)
3822             continue;
3823           dp = s->repo->idarraydata + s->requires;
3824           for (dp = s->repo->idarraydata + s->requires; *dp; dp++)
3825             FOR_PROVIDES(p, pp, *dp)
3826               if (pool->solvables[p].repo == installed)
3827                 {
3828                   if (strncmp(pool_id2str(pool, pool->solvables[p].name), "pattern", 7) != 0)
3829                     continue;
3830                   MAPSET(&userinstalled, p - installed->start);
3831                 }
3832         }
3833       dataiterator_free(&di);
3834     }
3835   if (1)
3836     {
3837       /* all products and their buddies are userinstalled */
3838       for (p = installed->start; p < installed->end; p++)
3839         {
3840           Solvable *s = pool->solvables + p;
3841           if (s->repo != installed)
3842             continue;
3843           if (!strncmp("product:", pool_id2str(pool, s->name), 8))
3844             {
3845               MAPSET(&userinstalled, p - installed->start);
3846 #ifdef ENABLE_LINKED_PKGS
3847               if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
3848                 {
3849                   Id buddy = solv->instbuddy[p - installed->start];
3850                   if (buddy >= installed->start && buddy < installed->end)
3851                     MAPSET(&userinstalled, buddy - installed->start);
3852                 }
3853 #endif
3854             }
3855         }
3856     }
3857
3858   /* add all positive elements (e.g. locks) to "userinstalled" */
3859   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3860     {
3861       r = solv->rules + rid;
3862       if (r->d < 0)
3863         continue;
3864       i = solv->ruletojob.elements[rid - solv->jobrules];
3865       if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3866         continue;
3867       FOR_RULELITERALS(p, jp, r)
3868         if (p > 0 && pool->solvables[p].repo == installed)
3869           MAPSET(&userinstalled, p - installed->start);
3870     }
3871
3872   /* add all cleandeps candidates to iq */
3873   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
3874     {
3875       r = solv->rules + rid;
3876       if (r->d < 0)                             /* disabled? */
3877         continue;
3878       if (r->d == 0 && r->p < 0 && r->w2 == 0)  /* negative assertion (erase job)? */
3879         {
3880           p = -r->p;
3881           if (pool->solvables[p].repo != installed)
3882             continue;
3883           MAPCLR(&userinstalled, p - installed->start);
3884           if (unneeded)
3885             continue;
3886           i = solv->ruletojob.elements[rid - solv->jobrules];
3887           how = job->elements[i];
3888           if ((how & (SOLVER_JOBMASK|SOLVER_CLEANDEPS)) == (SOLVER_ERASE|SOLVER_CLEANDEPS))
3889             queue_push(&iq, p);
3890         }
3891       else if (r->p > 0)                        /* install job */
3892         {
3893           if (unneeded)
3894             continue;
3895           i = solv->ruletojob.elements[rid - solv->jobrules];
3896           if ((job->elements[i] & SOLVER_CLEANDEPS) == SOLVER_CLEANDEPS)
3897             {
3898               /* check if the literals all obsolete some installed package */
3899               Map om;
3900               int iqstart;
3901
3902               /* just one installed literal */
3903               if (r->d == 0 && r->w2 == 0 && pool->solvables[r->p].repo == installed)
3904                 continue;
3905               /* multiversion is bad */
3906               if (solv->multiversion.size && !solv->keepexplicitobsoletes)
3907                 {
3908                   FOR_RULELITERALS(p, jp, r)
3909                     if (MAPTST(&solv->multiversion, p))
3910                       break;
3911                   if (p)
3912                     continue;
3913                 }
3914
3915               om.size = 0;
3916               iqstart = iq.count;
3917               FOR_RULELITERALS(p, jp, r)
3918                 {
3919                   if (p < 0)
3920                     {
3921                       queue_truncate(&iq, iqstart);     /* abort */
3922                       break;
3923                     }
3924                   if (pool->solvables[p].repo == installed)
3925                     {
3926                       if (iq.count == iqstart)
3927                         queue_push(&iq, p);
3928                       else
3929                         {
3930                           for (i = iqstart; i < iq.count; i++)
3931                             if (iq.elements[i] == p)
3932                               break;
3933                           queue_truncate(&iq, iqstart);
3934                           if (i < iq.count)
3935                             queue_push(&iq, p);
3936                         }
3937                     }
3938                   else
3939                     intersect_obsoletes(solv, p, &iq, iqstart, &om);
3940                   if (iq.count == iqstart)
3941                     break;
3942                 }
3943               if (om.size)
3944                 map_free(&om);
3945             }
3946         }
3947     }
3948   queue_init_clone(&iqcopy, &iq);
3949
3950   if (!unneeded)
3951     {
3952       if (solv->cleandeps_updatepkgs)
3953         for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
3954           queue_push(&iq, solv->cleandeps_updatepkgs->elements[i]);
3955     }
3956
3957   if (unneeded)
3958     queue_empty(&iq);   /* just in case... */
3959
3960   /* clear userinstalled bit for the packages we really want to delete/update */
3961   for (i = 0; i < iq.count; i++)
3962     {
3963       p = iq.elements[i];
3964       if (pool->solvables[p].repo != installed)
3965         continue;
3966       MAPCLR(&userinstalled, p - installed->start);
3967     }
3968
3969   for (p = installed->start; p < installed->end; p++)
3970     {
3971       if (pool->solvables[p].repo != installed)
3972         continue;
3973       MAPSET(&installedm, p);
3974       if (unneeded && !MAPTST(&userinstalled, p - installed->start))
3975         continue;
3976       MAPSET(&im, p);
3977     }
3978   MAPSET(&installedm, SYSTEMSOLVABLE);
3979   MAPSET(&im, SYSTEMSOLVABLE);
3980
3981 #ifdef CLEANDEPSDEBUG
3982   printf("REMOVE PASS\n");
3983 #endif
3984
3985   for (;;)
3986     {
3987       if (!iq.count)
3988         {
3989           if (unneeded)
3990             break;
3991           /* supplements pass */
3992           for (ip = installed->start; ip < installed->end; ip++)
3993             {
3994               if (!MAPTST(&installedm, ip))
3995                 continue;
3996               s = pool->solvables + ip;
3997               if (!s->supplements)
3998                 continue;
3999               if (!MAPTST(&im, ip))
4000                 continue;
4001               if (MAPTST(&userinstalled, ip - installed->start))
4002                 continue;
4003               supp = s->repo->idarraydata + s->supplements;
4004               while ((sup = *supp++) != 0)
4005                 if (dep_possible(solv, sup, &im))
4006                   break;
4007               if (!sup)
4008                 {
4009                   supp = s->repo->idarraydata + s->supplements;
4010                   while ((sup = *supp++) != 0)
4011                     if (dep_possible(solv, sup, &installedm) || (xsuppq.count && queue_contains(&xsuppq, sup)))
4012                       {
4013                         /* no longer supplemented, also erase */
4014                         int iqcount = iq.count;
4015                         /* pin packages, see comment above dep_pkgcheck */
4016                         dep_pkgcheck(solv, sup, &im, &iq);
4017                         for (i = iqcount; i < iq.count; i++)
4018                           {
4019                             Id pqp = iq.elements[i];
4020                             if (pool->solvables[pqp].repo == installed)
4021                               MAPSET(&userinstalled, pqp - installed->start);
4022                           }
4023                         queue_truncate(&iq, iqcount);
4024 #ifdef CLEANDEPSDEBUG
4025                         printf("%s supplemented [%s]\n", pool_solvid2str(pool, ip), pool_dep2str(pool, sup));
4026 #endif
4027                         queue_push(&iq, ip);
4028                       }
4029                 }
4030             }
4031           if (!iq.count)
4032             break;      /* no supplementing package found, we're done */
4033         }
4034       ip = queue_shift(&iq);
4035       s = pool->solvables + ip;
4036       if (!MAPTST(&im, ip))
4037         continue;
4038       if (!MAPTST(&installedm, ip))
4039         continue;
4040       if (s->repo == installed && MAPTST(&userinstalled, ip - installed->start))
4041         continue;
4042       MAPCLR(&im, ip);
4043 #ifdef CLEANDEPSDEBUG
4044       printf("removing %s\n", pool_solvable2str(pool, s));
4045 #endif
4046       if (s->requires)
4047         {
4048           reqp = s->repo->idarraydata + s->requires;
4049           while ((req = *reqp++) != 0)
4050             {
4051               if (req == SOLVABLE_PREREQMARKER)
4052                 continue;
4053 #ifdef ENABLE_COMPLEX_DEPS
4054               if (pool_is_complex_dep(pool, req))
4055                 {
4056                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
4057                   continue;
4058                 }
4059 #endif
4060               FOR_PROVIDES(p, pp, req)
4061                 {
4062                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
4063                     {
4064 #ifdef CLEANDEPSDEBUG
4065                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4066 #endif
4067                       queue_push(&iq, p);
4068                     }
4069                 }
4070             }
4071         }
4072       if (s->recommends)
4073         {
4074           reqp = s->repo->idarraydata + s->recommends;
4075           while ((req = *reqp++) != 0)
4076             {
4077 #ifdef ENABLE_COMPLEX_DEPS
4078               if (pool_is_complex_dep(pool, req))
4079                 {
4080                   complex_cleandeps_remove(pool, ip, req, &im, &installedm, &iq);
4081                   continue;
4082                 }
4083 #endif
4084               FOR_PROVIDES(p, pp, req)
4085                 {
4086                   if (p != SYSTEMSOLVABLE && MAPTST(&im, p))
4087                     {
4088 #ifdef CLEANDEPSDEBUG
4089                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4090 #endif
4091                       queue_push(&iq, p);
4092                     }
4093                 }
4094             }
4095         }
4096     }
4097
4098   /* turn userinstalled into remove set for pruning */
4099   map_empty(&userinstalled);
4100   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4101     {
4102       r = solv->rules + rid;
4103       if (r->p >= 0 || r->d != 0 || r->w2 != 0)
4104         continue;       /* disabled or not erase */
4105       p = -r->p;
4106       MAPCLR(&im, p);
4107       if (pool->solvables[p].repo == installed)
4108         MAPSET(&userinstalled, p - installed->start);
4109     }
4110   if (!unneeded && solv->cleandeps_updatepkgs)
4111     {
4112       for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
4113         {
4114           p = solv->cleandeps_updatepkgs->elements[i];
4115           if (pool->solvables[p].repo == installed)
4116             MAPSET(&userinstalled, p - installed->start);
4117         }
4118     }
4119   MAPSET(&im, SYSTEMSOLVABLE);  /* in case we cleared it above */
4120   for (p = installed->start; p < installed->end; p++)
4121     if (MAPTST(&im, p))
4122       queue_push(&iq, p);
4123   for (rid = solv->jobrules; rid < solv->jobrules_end; rid++)
4124     {
4125       r = solv->rules + rid;
4126       if (r->d < 0)
4127         continue;
4128       FOR_RULELITERALS(p, jp, r)
4129         if (p > 0)
4130           queue_push(&iq, p);
4131     }
4132   /* also put directly addressed packages on the install queue
4133    * so we can mark patterns as installed */
4134   for (i = 0; i < job->count; i += 2)
4135     {
4136       how = job->elements[i];
4137       if ((how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
4138         {
4139           what = job->elements[i + 1];
4140           select = how & SOLVER_SELECTMASK;
4141           if (select == SOLVER_SOLVABLE && pool->solvables[what].repo != installed)
4142             queue_push(&iq, what);
4143         }
4144     }
4145
4146 #ifdef CLEANDEPSDEBUG
4147   printf("ADDBACK PASS\n");
4148 #endif
4149   for (;;)
4150     {
4151       if (!iq.count)
4152         {
4153           /* supplements pass */
4154           for (ip = installed->start; ip < installed->end; ip++)
4155             {
4156               if (!MAPTST(&installedm, ip))
4157                 continue;
4158               if (MAPTST(&userinstalled, ip - installed->start))
4159                 continue;
4160               s = pool->solvables + ip;
4161               if (!s->supplements)
4162                 continue;
4163               if (MAPTST(&im, ip))
4164                 continue;
4165               supp = s->repo->idarraydata + s->supplements;
4166               while ((sup = *supp++) != 0)
4167                 if (dep_possible(solv, sup, &im))
4168                   break;
4169               if (sup)
4170                 {
4171 #ifdef CLEANDEPSDEBUG
4172                   printf("%s supplemented\n", pool_solvid2str(pool, ip));
4173 #endif
4174                   MAPSET(&im, ip);
4175                   queue_push(&iq, ip);
4176                 }
4177             }
4178           if (!iq.count)
4179             break;
4180         }
4181       ip = queue_shift(&iq);
4182       s = pool->solvables + ip;
4183 #ifdef CLEANDEPSDEBUG
4184       printf("adding back %s\n", pool_solvable2str(pool, s));
4185 #endif
4186       if (s->requires)
4187         {
4188           reqp = s->repo->idarraydata + s->requires;
4189           while ((req = *reqp++) != 0)
4190             {
4191 #ifdef ENABLE_COMPLEX_DEPS
4192               if (pool_is_complex_dep(pool, req))
4193                 {
4194                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
4195                   continue;
4196                 }
4197 #endif
4198               FOR_PROVIDES(p, pp, req)
4199                 if (MAPTST(&im, p))
4200                   break;
4201               if (p)
4202                 continue;
4203               FOR_PROVIDES(p, pp, req)
4204                 {
4205                   if (MAPTST(&installedm, p))
4206                     {
4207                       if (p == ip)
4208                         continue;
4209                       if (MAPTST(&userinstalled, p - installed->start))
4210                         continue;
4211 #ifdef CLEANDEPSDEBUG
4212                       printf("%s requires %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4213 #endif
4214                       MAPSET(&im, p);
4215                       queue_push(&iq, p);
4216                     }
4217                 }
4218             }
4219         }
4220       if (s->recommends)
4221         {
4222           reqp = s->repo->idarraydata + s->recommends;
4223           while ((req = *reqp++) != 0)
4224             {
4225 #ifdef ENABLE_COMPLEX_DEPS
4226               if (pool_is_complex_dep(pool, req))
4227                 {
4228                   complex_cleandeps_addback(pool, ip, req, &im, &installedm, &iq, &userinstalled);
4229                   continue;
4230                 }
4231 #endif
4232               FOR_PROVIDES(p, pp, req)
4233                 if (MAPTST(&im, p))
4234                   break;
4235               if (p)
4236                 continue;
4237               FOR_PROVIDES(p, pp, req)
4238                 {
4239                   if (MAPTST(&installedm, p))
4240                     {
4241                       if (p == ip)
4242                         continue;
4243                       if (MAPTST(&userinstalled, p - installed->start))
4244                         continue;
4245 #ifdef CLEANDEPSDEBUG
4246                       printf("%s recommends %s\n", pool_solvid2str(pool, ip), pool_solvid2str(pool, p));
4247 #endif
4248                       MAPSET(&im, p);
4249                       queue_push(&iq, p);
4250                     }
4251                 }
4252             }
4253         }
4254     }
4255
4256   queue_free(&iq);
4257   /* make sure the updatepkgs and mistakes are not in the cleandeps map */
4258   if (solv->cleandeps_updatepkgs)
4259     for (i = 0; i < solv->cleandeps_updatepkgs->count; i++)
4260       MAPSET(&im, solv->cleandeps_updatepkgs->elements[i]);
4261   if (solv->cleandeps_mistakes)
4262     for (i = 0; i < solv->cleandeps_mistakes->count; i++)
4263       MAPSET(&im, solv->cleandeps_mistakes->elements[i]);
4264   /* also remove original iq packages */
4265   for (i = 0; i < iqcopy.count; i++)
4266     MAPSET(&im, iqcopy.elements[i]);
4267   queue_free(&iqcopy);
4268   for (p = installed->start; p < installed->end; p++)
4269     {
4270       if (pool->solvables[p].repo != installed)
4271         continue;
4272       if (!MAPTST(&im, p))
4273         MAPSET(cleandepsmap, p - installed->start);
4274     }
4275   map_free(&im);
4276   map_free(&installedm);
4277   map_free(&userinstalled);
4278   queue_free(&xsuppq);
4279 #ifdef CLEANDEPSDEBUG
4280   printf("=== final cleandeps map:\n");
4281   for (p = installed->start; p < installed->end; p++)
4282     if (MAPTST(cleandepsmap, p - installed->start))
4283       printf("  - %s\n", pool_solvid2str(pool, p));
4284 #endif
4285 }
4286
4287
4288 struct trj_data {
4289   Queue *edges;
4290   Id *low;
4291   Id idx;
4292   Id nstack;
4293   Id firstidx;
4294 };
4295
4296 /* Tarjan's SCC algorithm, slightly modifed */
4297 static void
4298 trj_visit(struct trj_data *trj, Id node)
4299 {
4300   Id *low = trj->low;
4301   Queue *edges = trj->edges;
4302   Id nnode, myidx, stackstart;
4303   int i;
4304
4305   low[node] = myidx = trj->idx++;
4306   low[(stackstart = trj->nstack++)] = node;
4307   for (i = edges->elements[node]; (nnode = edges->elements[i]) != 0; i++)
4308     {
4309       Id l = low[nnode];
4310       if (!l)
4311         {
4312           if (!edges->elements[edges->elements[nnode]])
4313             {
4314               trj->idx++;
4315               low[nnode] = -1;
4316               continue;
4317             }
4318           trj_visit(trj, nnode);
4319           l = low[nnode];
4320         }
4321       if (l < 0)
4322         continue;
4323       if (l < trj->firstidx)
4324         {
4325           int k;
4326           for (k = l; low[low[k]] == l; k++)
4327             low[low[k]] = -1;
4328         }
4329       else if (l < low[node])
4330         low[node] = l;
4331     }
4332   if (low[node] == myidx)
4333     {
4334       if (myidx != trj->firstidx)
4335         myidx = -1;
4336       for (i = stackstart; i < trj->nstack; i++)
4337         low[low[i]] = myidx;
4338       trj->nstack = stackstart;
4339     }
4340 }
4341
4342 #ifdef ENABLE_COMPLEX_DEPS
4343 static void
4344 complex_unneeded(Pool *pool, Id ip, Id req, Queue *edges, Map *cleandepsmap, Queue *unneededq)
4345 {
4346   int i, j;
4347   Queue dq;
4348   Id p;
4349
4350   queue_init(&dq);
4351   i = pool_normalize_complex_dep(pool, req, &dq, CPLXDEPS_EXPAND);
4352   if (i == 0 || i == 1)
4353     {
4354       queue_free(&dq);
4355       return;
4356     }
4357   for (i = 0; i < dq.count; i++)
4358     {
4359       for (; (p = dq.elements[i]) != 0; i++)
4360         {
4361           if (p < 0)
4362             {
4363               if (pool->solvables[-p].repo != pool->installed)
4364                 break;
4365               continue;
4366             }
4367           if (p == ip || pool->solvables[p].repo != pool->installed || !MAPTST(cleandepsmap, p - pool->installed->start))
4368             continue;
4369           for (j = 0; j < unneededq->count; j++)
4370             if (p == unneededq->elements[j])
4371               {
4372                 if (edges->elements[edges->count - 1] != j + 1)
4373                   queue_push(edges, j + 1);
4374                 break;
4375               }
4376         }
4377       while (dq.elements[i])
4378         i++;
4379     }
4380   queue_free(&dq);
4381 }
4382 #endif
4383
4384 void
4385 solver_get_unneeded(Solver *solv, Queue *unneededq, int filtered)
4386 {
4387   Repo *installed = solv->installed;
4388   int i;
4389   Map cleandepsmap;
4390
4391   queue_empty(unneededq);
4392   if (!installed || installed->end == installed->start)
4393     return;
4394
4395   map_init(&cleandepsmap, installed->end - installed->start);
4396   solver_createcleandepsmap(solv, &cleandepsmap, 1);
4397   for (i = installed->start; i < installed->end; i++)
4398     if (MAPTST(&cleandepsmap, i - installed->start))
4399       queue_push(unneededq, i);
4400
4401   if (filtered && unneededq->count > 1)
4402     {
4403       Pool *pool = solv->pool;
4404       Queue edges;
4405       Id *nrequires;
4406       Map installedm;
4407       int j, pass, count = unneededq->count;
4408       Id *low;
4409
4410       map_init(&installedm, pool->nsolvables);
4411       for (i = installed->start; i < installed->end; i++)
4412         if (pool->solvables[i].repo == installed)
4413           MAPSET(&installedm, i);
4414
4415       nrequires = solv_calloc(count, sizeof(Id));
4416       queue_init(&edges);
4417       queue_prealloc(&edges, count * 4 + 10);   /* pre-size */
4418
4419       /*
4420        * Go through the solvables in the nodes queue and create edges for
4421        * all requires/recommends/supplements between the nodes.
4422        * The edges are stored in the edges queue, we add 1 to the node
4423        * index so that nodes in the edges queue are != 0 and we can
4424        * terminate the edge list with 0.
4425        * Thus for node element 5, the edges are stored starting at
4426        * edges.elements[6] and are 0-terminated.
4427        */
4428       /* leave first element zero to make things easier */
4429       /* also add trailing zero */
4430       queue_insertn(&edges, 0, 1 + count + 1, 0);
4431
4432       /* first requires and recommends */
4433       for (i = 0; i < count; i++)
4434         {
4435           Solvable *s = pool->solvables + unneededq->elements[i];
4436           int oldcount = edges.count;
4437           edges.elements[i + 1] = oldcount;
4438           for (pass = 0; pass < 2; pass++)
4439             {
4440               unsigned int off = pass == 0 ? s->requires : s->recommends;
4441               Id p, pp, dep, *dp;
4442               if (off)
4443                 for (dp = s->repo->idarraydata + off; (dep = *dp) != 0; dp++)
4444                   {
4445 #ifdef ENABLE_COMPLEX_DEPS
4446                     if (pool_is_complex_dep(pool, dep))
4447                       {
4448                         complex_unneeded(pool, s - pool->solvables, dep, &edges, &cleandepsmap, unneededq);
4449                         continue;
4450                       }
4451 #endif
4452                     FOR_PROVIDES(p, pp, dep)
4453                       {
4454                         Solvable *sp = pool->solvables + p;
4455                         if (s == sp || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4456                           continue;
4457                         for (j = 0; j < count; j++)
4458                           if (p == unneededq->elements[j])
4459                             {
4460                               if (edges.elements[edges.count - 1] != j + 1)
4461                                 queue_push(&edges, j + 1);
4462                             }
4463                       }
4464                   }
4465               if (pass == 0)
4466                 nrequires[i] = edges.count - oldcount;
4467             }
4468           queue_push(&edges, 0);
4469         }
4470 #if 0
4471       printf("requires + recommends\n");
4472       for (i = 0; i < count; i++)
4473         {
4474           int j;
4475           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4476           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4477             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4478         }
4479 #endif
4480
4481       /* then add supplements */
4482       for (i = 0; i < count; i++)
4483         {
4484           Solvable *s = pool->solvables + unneededq->elements[i];
4485           if (s->supplements)
4486             {
4487               Id *dp;
4488               int k;
4489               for (dp = s->repo->idarraydata + s->supplements; *dp; dp++)
4490                 if (dep_possible(solv, *dp, &installedm))
4491                   {
4492                     Queue iq;
4493                     Id iqbuf[16];
4494                     queue_init_buffer(&iq, iqbuf, sizeof(iqbuf)/sizeof(*iqbuf));
4495                     dep_pkgcheck(solv, *dp, 0, &iq);
4496                     for (k = 0; k < iq.count; k++)
4497                       {
4498                         Id p = iq.elements[k];
4499                         Solvable *sp = pool->solvables + p;
4500                         if (p == unneededq->elements[i] || sp->repo != installed || !MAPTST(&cleandepsmap, p - installed->start))
4501                           continue;
4502                         for (j = 0; j < count; j++)
4503                           if (p == unneededq->elements[j])
4504                             break;
4505                         /* now add edge from j + 1 to i + 1 */
4506                         queue_insert(&edges, edges.elements[j + 1] + nrequires[j], i + 1);
4507                         /* addapt following edge pointers */
4508                         for (j = j + 2; j < count + 1; j++)
4509                           edges.elements[j]++;
4510                       }
4511                     queue_free(&iq);
4512                   }
4513             }
4514         }
4515 #if 0
4516       /* print result */
4517       printf("+ supplements\n");
4518       for (i = 0; i < count; i++)
4519         {
4520           int j;
4521           printf("  %s (%d requires):\n", pool_solvid2str(pool, unneededq->elements[i]), nrequires[i]);
4522           for (j = edges.elements[i + 1]; edges.elements[j]; j++)
4523             printf("    - %s\n", pool_solvid2str(pool, unneededq->elements[edges.elements[j] - 1]));
4524         }
4525 #endif
4526       map_free(&installedm);
4527
4528       /* now run SCC algo two times, first with requires+recommends+supplements,
4529        * then again without the requires. We run it the second time to get rid
4530        * of packages that got dragged in via recommends/supplements */
4531       /*
4532        * low will contain the result of the SCC search.
4533        * it must be of at least size 2 * (count + 1) and
4534        * must be zero initialized.
4535        * The layout is:
4536        *    0  low low ... low stack stack ...stack 0
4537        *            count              count
4538        */
4539       low = solv_calloc(count + 1, 2 * sizeof(Id));
4540       for (pass = 0; pass < 2; pass++)
4541         {
4542           struct trj_data trj;
4543           if (pass)
4544             {
4545               memset(low, 0, (count + 1) * (2 * sizeof(Id)));
4546               for (i = 0; i < count; i++)
4547                 {
4548                   edges.elements[i + 1] += nrequires[i];
4549                   if (!unneededq->elements[i])
4550                     low[i + 1] = -1;    /* ignore this node */
4551                 }
4552             }
4553           trj.edges = &edges;
4554           trj.low = low;
4555           trj.idx = count + 1;  /* stack starts here */
4556           for (i = 1; i <= count; i++)
4557             {
4558               if (low[i])
4559                 continue;
4560               if (edges.elements[edges.elements[i]])
4561                 {
4562                   trj.firstidx = trj.nstack = trj.idx;
4563                   trj_visit(&trj, i);
4564                 }
4565               else
4566                 {
4567                   Id myidx = trj.idx++;
4568                   low[i] = myidx;
4569                   low[myidx] = i;
4570                 }
4571             }
4572           /* prune packages */
4573           for (i = 0; i < count; i++)
4574             if (low[i + 1] <= 0)
4575               unneededq->elements[i] = 0;
4576         }
4577       solv_free(low);
4578       solv_free(nrequires);
4579       queue_free(&edges);
4580
4581       /* finally remove all pruned entries from unneededq */
4582       for (i = j = 0; i < count; i++)
4583         if (unneededq->elements[i])
4584           unneededq->elements[j++] = unneededq->elements[i];
4585       queue_truncate(unneededq, j);
4586     }
4587   map_free(&cleandepsmap);
4588 }
4589
4590
4591 void
4592 solver_breakorphans(Solver *solv)
4593 {
4594   Pool *pool = solv->pool;
4595   Repo *installed = solv->installed;
4596   int i, rid;
4597   Map m;
4598
4599   if (!installed || solv->droporphanedmap_all)
4600     return;
4601   solv->brokenorphanrules = solv_calloc(1, sizeof(Queue));
4602   queue_init(solv->brokenorphanrules);
4603   map_init(&m, installed->end - installed->start);
4604   for (i = 0; i < solv->orphaned.count; i++)
4605     {
4606       Id p = solv->orphaned.elements[i];
4607       if (pool->solvables[p].repo != installed)
4608         continue;
4609       if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - installed->start))
4610         continue;
4611       MAPSET(&m, p - installed->start);
4612     }
4613   for (rid = 1; rid < solv->pkgrules_end ; rid++)
4614     {
4615       Id p, *dp;
4616       Rule *r = solv->rules + rid;
4617       /* ignore non-deps and simple conflicts */
4618       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
4619         continue;
4620       p = -r->p;
4621       if (p < installed->start || p >= installed->end || !MAPTST(&m, p - installed->start))
4622         {
4623           /* need to check other literals */
4624           if (r->d == 0 || r->d == -1)
4625             continue;
4626           for (dp = pool->whatprovidesdata + (r->d < 0 ? -r->d - 1 : r->d); *dp < 0; dp++)
4627             {
4628               p = -*dp;
4629               if (p >= installed->start && p < installed->end && MAPTST(&m, p - installed->start))
4630                 break;
4631             }
4632           if (*dp >= 0)
4633             continue;
4634         }
4635       /* ok, disable this rule */
4636       queue_push(solv->brokenorphanrules, rid);
4637       if (r->d >= 0)
4638         solver_disablerule(solv, r);
4639     }
4640   map_free(&m);
4641   if (!solv->brokenorphanrules->count)
4642     {
4643       queue_free(solv->brokenorphanrules);
4644       solv->brokenorphanrules = solv_free(solv->brokenorphanrules);
4645     }
4646 }
4647
4648 void
4649 solver_check_brokenorphanrules(Solver *solv, Queue *dq)
4650 {
4651   Pool *pool = solv->pool;
4652   int i;
4653   Id l, pp;
4654   
4655   queue_empty(dq);
4656   if (!solv->brokenorphanrules)
4657     return;
4658   for (i = 0; i < solv->brokenorphanrules->count; i++)
4659     {
4660       int rid = solv->brokenorphanrules->elements[i];
4661       Rule *r = solv->rules + rid;
4662       FOR_RULELITERALS(l, pp, r)
4663         {
4664           if (l < 0)
4665             {
4666               if (solv->decisionmap[-l] <= 0)
4667                 break;
4668             }
4669           else
4670             {
4671               if (solv->decisionmap[l] > 0 && pool->solvables[l].repo != solv->installed)
4672                 break;
4673             }
4674         }
4675       if (l)
4676         continue;
4677       FOR_RULELITERALS(l, pp, r)
4678         if (l > 0 && solv->decisionmap[l] == 0 && pool->solvables[l].repo != solv->installed)
4679           queue_pushunique(dq, l);
4680     }
4681 }
4682