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