Imported Upstream version 0.7.27
[platform/upstream/libsolv.git] / src / rules.c
index f735e5d..7d57cfd 100644 (file)
@@ -183,7 +183,7 @@ solver_unifyrules(Solver *solv)
            binr++;
          else
            {
-             dp = solv->pool->whatprovidesdata + r->d;
+             dp = pool->whatprovidesdata + r->d;
              while (*dp++)
                lits++;
            }
@@ -613,7 +613,7 @@ add_complex_deprules(Solver *solv, Id p, Id dep, int type, int dontfix, Queue *w
        }
       else
        {
-         Id *qele;
+         Id *qele, d;
          int qcnt;
 
          qele = bq.elements + i;
@@ -653,7 +653,21 @@ add_complex_deprules(Solver *solv, Id p, Id dep, int type, int dontfix, Queue *w
              break;
          if (j < qcnt)
            continue;
-         addpkgrule(solv, qele[0], 0, pool_ids2whatprovides(pool, qele + 1, qcnt - 1), type, dep);
+         d = pool_ids2whatprovides(pool, qele + 1, qcnt - 1);
+         if (solv->ruleinfoq && qele[0] != p)
+           {
+             int oldcount = solv->ruleinfoq->count;
+             addpkgrule(solv, qele[0], 0, d, type, dep);
+             /* fixup from element of ruleinfo */
+             if (solv->ruleinfoq->count > oldcount && solv->ruleinfoq->elements[oldcount + 1] != p)
+               {
+                 if (solv->ruleinfoq->elements[oldcount + 2])
+                   solv->ruleinfoq->elements[oldcount + 2] = solv->ruleinfoq->elements[oldcount + 1];
+                 solv->ruleinfoq->elements[oldcount + 1] = p;
+               }
+           }
+         else
+           addpkgrule(solv, qele[0], 0, d, type, dep);
          if (m)
            for (j = 0; j < qcnt; j++)
              if (qele[j] > 0 && !MAPTST(m, qele[j]))
@@ -1658,6 +1672,16 @@ solver_addinfarchrules(Solver *solv, Map *addedmap)
       if (first)
        continue;               /* not the first in the group */
 
+      if (!bestscore && allowedarchs.count > 1 && pool->implicitobsoleteusescolors)
+       {
+         for (j = 0; j < allowedarchs.count; j++)
+           {
+             a = pool_arch2score(pool, allowedarchs.elements[j]);
+             if (a && a != 1 && (!bestscore || a < bestscore))
+               bestscore = a;
+           }
+       }
+
       if (!bestscore)
        continue;               /* did not find a score for this group */
 
@@ -1921,8 +1945,8 @@ solver_addtodupmaps(Solver *solv, Id p, Id how, int targeted)
 void
 solver_createdupmaps(Solver *solv)
 {
-  Queue *job = &solv->job;
   Pool *pool = solv->pool;
+  Queue *job = &solv->job;
   Repo *installed = solv->installed;
   Id select, how, what, p, pp;
   Solvable *s;
@@ -2189,12 +2213,12 @@ reenableblackrule(Solver *solv, Id p)
 void
 solver_addblackrules(Solver *solv)
 {
-  int i;
-  Id how, select, what, p, pp;
-  Queue *job = &solv->job;
   Pool *pool = solv->pool;
   Repo *installed = solv->installed;
+  Id how, select, what, p, pp;
+  Queue *job = &solv->job;
   Map updatemap;
+  int i;
 
   map_init(&updatemap, 0);
   solv->blackrules = solv->nrules;
@@ -2247,6 +2271,89 @@ solver_addblackrules(Solver *solv)
 
 /***********************************************************************
  ***
+ ***  Strict repo prio rule part
+ ***/
+
+/* add rules to exclude solvables provided by lower
+ * precedence repositories */
+void solver_addstrictrepopriorules(struct s_Solver *solv, Map *addedmap)
+{
+  Pool *pool = solv->pool;
+  Solvable *s;
+  Id p, p2, pp2;
+  Map priomap;
+  int max_prio;
+
+  map_init_clone(&priomap, addedmap);
+  solv->strictrepopriorules = solv->nrules;
+
+  FOR_POOL_SOLVABLES(p)
+  {
+    if (!MAPTST(&priomap, p))
+      continue;
+
+    s = pool->solvables + p;
+    max_prio = s->repo->priority;
+    FOR_PROVIDES(p2, pp2, s->name)
+      {
+       Solvable *s2 = pool->solvables + p2;
+       if (s->name != s2->name)
+         continue;
+       if (s2->repo->priority > max_prio)
+         max_prio = s2->repo->priority;
+      }
+         
+    FOR_PROVIDES(p2, pp2, s->name)
+      {
+       Solvable *s2 = pool->solvables + p2;
+       if (s->name != s2->name || !MAPTST(&priomap, p2))
+         continue;
+       MAPCLR(&priomap, p2);
+       if (pool->installed && s2->repo == pool->installed)
+         continue;
+       if (s2->repo->priority < max_prio)
+         solver_addrule(solv, -p2, 0, 0);
+      }
+  }
+  solv->strictrepopriorules_end = solv->nrules;
+  map_free(&priomap);
+}
+
+static inline void
+disablerepopriorule(Solver *solv, Id name)
+{
+  Pool *pool = solv->pool;
+  Rule *r;
+  int i;
+  for (i = solv->strictrepopriorules, r = solv->rules + i; i < solv->strictrepopriorules_end; i++, r++)
+    {
+      if (r->p < 0 && r->d >= 0 && pool->solvables[-r->p].name == name)
+       solver_disablerule(solv, r);
+    }
+}
+
+static inline void
+reenablerepopriorule(Solver *solv, Id name)
+{
+  Pool *pool = solv->pool;
+  Rule *r;
+  int i;
+  for (i = solv->strictrepopriorules, r = solv->rules + i; i < solv->strictrepopriorules_end; i++, r++)
+    {
+      if (r->p < 0 && r->d < 0 && pool->solvables[-r->p].name == name)
+       {
+         solver_enablerule(solv, r);
+         IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
+           {
+             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "@@@ re-enabling ");
+             solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
+           }
+       }
+    }
+}
+
+/***********************************************************************
+ ***
  ***  Policy rule disabling/reenabling
  ***
  ***  Disable all policy rules that conflict with our jobs. If a job
@@ -2254,10 +2361,36 @@ solver_addblackrules(Solver *solv)
  ***
  ***/
 
-#define DISABLE_UPDATE 1
-#define DISABLE_INFARCH        2
-#define DISABLE_DUP    3
-#define DISABLE_BLACK  4
+#define DISABLE_UPDATE  1
+#define DISABLE_INFARCH         2
+#define DISABLE_DUP     3
+#define DISABLE_BLACK   4
+#define DISABLE_REPOPRIO 5
+
+/* check if installed package p is in lock-step with another installed package */
+static int
+installed_is_in_lockstep(Solver *solv, Id p)
+{
+  Pool *pool = solv->pool;
+  Repo *installed = solv->installed;
+  int rid;
+  Id pp, l;
+  Rule *r;
+
+  if (!installed)
+    return 0;
+  for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
+    {
+      if (r->p >= 0)
+       continue;
+      if (pool->solvables[-r->p].repo != installed)
+       continue;
+      FOR_RULELITERALS(l, pp, r)
+       if (l == p)
+         return 1;
+    }
+  return 0;
+}
 
 static void
 jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
@@ -2357,6 +2490,26 @@ jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
                }
            }
        }
+      if ((set & SOLVER_SETREPO) != 0 && solv->strictrepopriorules != solv->strictrepopriorules_end)
+       {
+         if (select == SOLVER_SOLVABLE)
+           queue_push2(q, DISABLE_REPOPRIO, pool->solvables[what].name);
+         else
+           {
+             int qcnt = q->count;
+             FOR_JOB_SELECT(p, pp, select, what)
+               {
+                 s = pool->solvables + p;
+                 /* unify names */
+                 for (i = qcnt; i < q->count; i += 2)
+                   if (q->elements[i + 1] == s->name)
+                     break;
+                 if (i < q->count)
+                   continue;
+                 queue_push2(q, DISABLE_REPOPRIO, s->name);
+               }
+           }
+       }
       if ((set & SOLVER_SETEVR) != 0 && solv->blackrules != solv->blackrules_end)
         {
          if (select == SOLVER_SOLVABLE)
@@ -2371,6 +2524,20 @@ jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
        return;
       /* now the hard part: disable some update rules */
 
+      /* if the job asks for a single solvable to stay, disable the update rule */
+      if (select == SOLVER_SOLVABLE && pool->solvables[what].repo == installed && solv->bestrules_info)
+        if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
+         {
+           int ni = solv->bestrules_end - solv->bestrules;
+           for (i = solv->bestrules_up - solv->bestrules; i < ni; i++)
+             if (solv->bestrules_info[i] == what)
+               {
+                 queue_push2(q, DISABLE_UPDATE, what);         /* will also disable the best rule */
+                 break;
+               }
+           return;
+         }
+
       /* first check if we have installed or multiversion packages in the job */
       FOR_JOB_SELECT(p, pp, select, what)
        {
@@ -2446,6 +2613,20 @@ jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
     case SOLVER_ERASE:
       if (!installed)
        break;
+      set = how & SOLVER_SETMASK;
+      if (!(set & (SOLVER_NOAUTOSET | SOLVER_SETARCH)) && pool->implicitobsoleteusescolors && solv->infarchrules != solv->infarchrules_end)
+       {
+         if (select == SOLVER_SOLVABLE)
+           set |= SOLVER_SETARCH;
+         else if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(what))
+           {
+             Reldep *rd = GETRELDEP(pool, what);
+             if (rd->flags <= 7 && ISRELDEP(rd->name))
+               rd = GETRELDEP(pool, rd->name);
+             if (rd->flags == REL_ARCH)
+               set |= SOLVER_SETARCH;
+           }
+       }
       if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
        {
          FOR_REPO_SOLVABLES(installed, p, s)
@@ -2455,6 +2636,9 @@ jobtodisablelist(Solver *solv, Id how, Id what, Queue *q)
        if (pool->solvables[p].repo == installed)
          {
            queue_push2(q, DISABLE_UPDATE, p);
+           if ((set & SOLVER_SETARCH) != 0 && pool->implicitobsoleteusescolors && solv->infarchrules != solv->infarchrules_end)
+             if (installed_is_in_lockstep(solv, p))
+               queue_push2(q, DISABLE_INFARCH, pool->solvables[p].name);               /* allow to break the lock-step */
 #ifdef ENABLE_LINKED_PKGS
            if (solv->instbuddy && solv->instbuddy[p - installed->start] > 1)
              queue_push2(q, DISABLE_UPDATE, solv->instbuddy[p - installed->start]);
@@ -2543,6 +2727,9 @@ solver_disablepolicyrules(Solver *solv)
        case DISABLE_BLACK:
          disableblackrule(solv, arg);
          break;
+       case DISABLE_REPOPRIO:
+         disablerepopriorule(solv, arg);
+         break;
        default:
          break;
        }
@@ -2649,6 +2836,9 @@ solver_reenablepolicyrules(Solver *solv, int jobidx)
        case DISABLE_BLACK:
          reenableblackrule(solv, arg);
          break;
+       case DISABLE_REPOPRIO:
+         reenablerepopriorule(solv, arg);
+         break;
        }
     }
   queue_free(&q);
@@ -2729,7 +2919,8 @@ addpkgruleinfo(Solver *solv, Id p, Id p2, Id d, int type, Id dep)
          if (*odp)
            return;
        }
-      if (p < 0 && pool->whatprovidesdata[d] < 0 && type == SOLVER_RULE_PKG_CONFLICTS)
+      /* set p2 for multiversion conflicts */
+      if (p < 0 && pool->whatprovidesdata[d] < 0 && pool->whatprovidesdata[d + 1] >= 0 && type == SOLVER_RULE_PKG_CONFLICTS)
        p2 = pool->whatprovidesdata[d];
     }
   else
@@ -2887,7 +3078,7 @@ solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
          qp = rq.elements[i + 1];
          qo = rq.elements[i + 2];
          qd = rq.elements[i + 3];
-         if (type == SOLVER_RULE_PKG || type > qt)
+         if (type == SOLVER_RULE_PKG || qt == SOLVER_RULE_PKG_SAME_NAME || type > qt)
            {
              type = qt;
              if (fromp)
@@ -2896,6 +3087,8 @@ solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
                *top = qo;
              if (depp)
                *depp = qd;
+             if (qt == SOLVER_RULE_PKG_SAME_NAME)
+               break;                  /* prefer SOLVER_RULE_PKG_SAME_NAME */
            }
        }
       queue_free(&rq);
@@ -2955,8 +3148,12 @@ solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
     }
   if (rid >= solv->bestrules && rid < solv->bestrules_end)
     {
+      /* > 0: the package we are updating */
       if (fromp && solv->bestrules_info[rid - solv->bestrules] > 0)
        *fromp = solv->bestrules_info[rid - solv->bestrules];
+      /* < 0: the job rule */
+      if (top && solv->bestrules_info[rid - solv->bestrules] < 0)
+       *top = -solv->bestrules_info[rid - solv->bestrules];
       return SOLVER_RULE_BEST;
     }
   if (rid >= solv->yumobsrules && rid < solv->yumobsrules_end)
@@ -2981,10 +3178,24 @@ solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp)
        *fromp = -r->p;
       return SOLVER_RULE_BLACK;
     }
+  if (rid >= solv->strictrepopriorules && rid < solv->strictrepopriorules_end)
+    {
+      if (fromp)
+       *fromp = -r->p;
+      return SOLVER_RULE_STRICT_REPO_PRIORITY;
+    }
   if (rid >= solv->choicerules && rid < solv->choicerules_end)
-    return SOLVER_RULE_CHOICE;
+    {
+      if (solv->choicerules_info && fromp)
+       *fromp = solv->choicerules_info[rid - solv->choicerules];
+      return SOLVER_RULE_CHOICE;
+    }
   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
-    return SOLVER_RULE_RECOMMENDS;
+    {
+      if (solv->recommendsrules_info && fromp)
+       *fromp = solv->recommendsrules_info[rid - solv->recommendsrules];
+      return SOLVER_RULE_RECOMMENDS;
+    }
   if (rid >= solv->learntrules)
     return SOLVER_RULE_LEARNT;
   return SOLVER_RULE_UNKNOWN;
@@ -3017,8 +3228,8 @@ solver_ruleclass(Solver *solv, Id rid)
     return SOLVER_RULE_CHOICE;
   if (rid >= solv->recommendsrules && rid < solv->recommendsrules_end)
     return SOLVER_RULE_RECOMMENDS;
-  if (rid >= solv->blackrules && rid < solv->blackrules_end)
-    return SOLVER_RULE_BLACK;
+  if (rid >= solv->strictrepopriorules && rid < solv->strictrepopriorules_end)
+    return SOLVER_RULE_STRICT_REPO_PRIORITY;
   if (rid >= solv->learntrules && rid < solv->nrules)
     return SOLVER_RULE_LEARNT;
   return SOLVER_RULE_UNKNOWN;
@@ -3069,10 +3280,10 @@ solver_rule2job(Solver *solv, Id rid, Id *whatp)
 Id
 solver_rule2solvable(Solver *solv, Id rid)
 {
-  if (rid >= solv->updaterules && rid < solv->updaterules_end)
-    return rid - solv->updaterules;
-  if (rid >= solv->featurerules && rid < solv->featurerules_end)
-    return rid - solv->featurerules;
+  if (rid >= solv->updaterules && rid < solv->updaterules_end && solv->installed)
+    return solv->installed->start + (rid - solv->updaterules);
+  if (rid >= solv->featurerules && rid < solv->featurerules_end && solv->installed)
+    return solv->installed->start + (rid - solv->featurerules);
   return 0;
 }
 
@@ -3128,6 +3339,12 @@ solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
 
 
 /* check if the newest versions of pi still provides the dependency we're looking for */
+/* pi: installed package
+ * r: rule for the dependency
+ * m: map with all positive elements of r
+ * return 0: at least one provider
+ * return 1: the newest versions do not provide the dependency
+ */
 static int
 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
 {
@@ -3176,20 +3393,46 @@ solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
   return 1;    /* none of the new packages provided it */
 }
 
-static inline void
-queue_removeelement(Queue *q, Id el)
+static Id
+choicerule_find_installed(Pool *pool, Id p)
 {
-  int i, j;
-  for (i = 0; i < q->count; i++)
-    if (q->elements[i] == el)
-      break;
-  if (i < q->count)
+  Solvable *s = pool->solvables + p;
+  Id p2, pp2;
+
+  if (!s->repo)
+    return 0;
+  if (s->repo == pool->installed)
+    return p;
+  FOR_PROVIDES(p2, pp2, s->name)
     {
-      for (j = i++; i < q->count; i++)
-       if (q->elements[i] != el)
-         q->elements[j++] = q->elements[i];
-      queue_truncate(q, j);
+      Solvable *s2 = pool->solvables + p2;
+      if (s2->repo != pool->installed)
+       continue;
+      if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
+       continue;
+      if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
+       continue;
+      return p2;
     }
+  if (s->obsoletes)
+    {
+      Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
+      while ((obs = *obsp++) != 0)
+       {
+         FOR_PROVIDES(p2, pp2, obs)
+           {
+             Solvable *s2 = pool->solvables + p2;
+             if (s2->repo != pool->installed)
+               continue;
+             if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
+               continue;
+             if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
+               continue;
+             return p2;
+           }
+       }
+    }
+  return 0;
 }
 
 void
@@ -3199,13 +3442,14 @@ solver_addchoicerules(Solver *solv)
   Map m, mneg;
   Rule *r;
   Queue q, qi, qcheck, infoq;
-  int i, j, rid, havechoice;
-  Id p, d, pp;
-  Id p2, pp2;
-  Solvable *s, *s2;
+  int i, j, rid, havechoice, negcnt;
+  Id p, d, pp, p2;
+  Solvable *s;
   Id lastaddedp, lastaddedd;
   int lastaddedcnt;
   unsigned int now;
+  int isinstalled;
+  int dodowngradecheck = solv->allowdowngrade;
 
   solv->choicerules = solv->nrules;
   if (!pool->installed)
@@ -3214,7 +3458,8 @@ solver_addchoicerules(Solver *solv)
       return;
     }
   now = solv_timems(0);
-  solv->choicerules_info = solv_calloc(solv->pkgrules_end, sizeof(Id));
+  if ((solv->dupinvolvedmap_all || solv->dupinvolvedmap.size) && solv->dup_allowdowngrade)
+    dodowngradecheck = 1;
   queue_init(&q);
   queue_init(&qi);
   queue_init(&qcheck);
@@ -3237,116 +3482,80 @@ solver_addchoicerules(Solver *solv)
       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
        continue;       /* only look at requires rules */
       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
-      queue_empty(&q);
       queue_empty(&qi);
       havechoice = 0;
+      isinstalled = 0;
       FOR_RULELITERALS(p, pp, r)
        {
          if (p < 0)
-           continue;
+           {
+             Solvable *s = pool->solvables - p;
+             p2 = s->repo == pool->installed ? -p : 0;
+             if (p2)
+               {
+                 if (!(solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p2 - solv->installed->start))))
+                   isinstalled = 1;
+               }
+             continue;
+           }
          s = pool->solvables + p;
          if (!s->repo)
            continue;
          if (s->repo == pool->installed)
            {
-             queue_push(&q, p);
+             queue_push2(&qi, p, p);
              continue;
            }
-         /* check if this package is "blocked" by a installed package */
-         s2 = 0;
-         FOR_PROVIDES(p2, pp2, s->name)
-           {
-             s2 = pool->solvables + p2;
-             if (s2->repo != pool->installed)
-               continue;
-             if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
-               continue;
-             if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
-               continue;
-             break;
-           }
+         /* find an installed package p2 that we can update/downgrade to p */
+         p2 = choicerule_find_installed(pool, p);
          if (p2)
            {
-             /* found installed package p2 that we can update to p */
              if (MAPTST(&mneg, p))
                continue;
-             if (policy_is_illegal(solv, s2, s, 0))
-               continue;
-#if 0
-             if (solver_choicerulecheck(solv, p2, r, &m))
+             if (policy_is_illegal(solv, pool->solvables + p2, s, 0))
                continue;
-             queue_push(&qi, p2);
-#else
              queue_push2(&qi, p2, p);
-#endif
-             queue_push(&q, p);
              continue;
            }
-         if (s->obsoletes)
-           {
-             Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
-             s2 = 0;
-             while ((obs = *obsp++) != 0)
-               {
-                 FOR_PROVIDES(p2, pp2, obs)
-                   {
-                     s2 = pool->solvables + p2;
-                     if (s2->repo != pool->installed)
-                       continue;
-                     if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
-                       continue;
-                     if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
-                       continue;
-                     break;
-                   }
-                 if (p2)
-                   break;
-               }
-             if (obs)
-               {
-                 /* found installed package p2 that we can update to p */
-                 if (MAPTST(&mneg, p))
-                   continue;
-                 if (policy_is_illegal(solv, s2, s, 0))
-                   continue;
-#if 0
-                 if (solver_choicerulecheck(solv, p2, r, &m))
-                   continue;
-                 queue_push(&qi, p2);
-#else
-                 queue_push2(&qi, p2, p);
-#endif
-                 queue_push(&q, p);
-                 continue;
-               }
-           }
          /* package p is independent of the installed ones */
          havechoice = 1;
        }
-      if (!havechoice || !q.count || !qi.count)
+#if 0
+      printf("havechoice: %d qcount %d qicount %d\n", havechoice, q.count, qi.count);
+#endif
+      if (!havechoice || !qi.count)
        continue;       /* no choice */
 
       FOR_RULELITERALS(p, pp, r)
         if (p > 0)
          MAPSET(&m, p);
 
-      /* do extra checking */
-      for (i = j = 0; i < qi.count; i += 2)
+      if (!isinstalled)
        {
-         p2 = qi.elements[i];
-         if (!p2)
-           continue;
-         if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
+         /* do extra checking for packages related to installed packages */
+         for (i = j = 0; i < qi.count; i += 2)
            {
-             /* oops, remove element p from q */
-             queue_removeelement(&q, qi.elements[i + 1]);
-             continue;
+             int isdowngrade = 0;
+             p2 = qi.elements[i];
+             if (dodowngradecheck)
+               {
+                 p = qi.elements[i + 1];
+                 if (pool->solvables[p2].name == pool->solvables[p].name)
+                   if (pool_evrcmp(pool, pool->solvables[p2].evr, pool->solvables[p].evr, EVRCMP_COMPARE) > 0)
+                     isdowngrade = 1;
+               }
+             if (isdowngrade || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p2 - solv->installed->start)))
+               {
+                 if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
+                   continue;
+               }
+             qi.elements[j++] = p2;
+             qi.elements[j++] = qi.elements[i + 1];
            }
-         qi.elements[j++] = p2;
+         queue_truncate(&qi, j);
        }
-      queue_truncate(&qi, j);
 
-      if (!q.count || !qi.count)
+      if (!qi.count)
        {
          FOR_RULELITERALS(p, pp, r)
            if (p > 0)
@@ -3354,6 +3563,15 @@ solver_addchoicerules(Solver *solv)
          continue;
        }
 
+      queue_empty(&q);
+      /* split q from qi */
+      for (i = j = 0; i < qi.count; i += 2)
+       {
+         queue_push(&q, qi.elements[i + 1]);
+         qi.elements[j++] = qi.elements[i];
+       }
+      queue_truncate(&qi, j);
+
 
       /* now check the update rules of the installed package.
        * if all packages of the update rules are contained in
@@ -3373,14 +3591,18 @@ solver_addchoicerules(Solver *solv)
              break;
          if (p)
            break;
+         /* speed improvement: only check each package once */
          for (j = i + 1; j < qi.count; j++)
            if (qi.elements[i] == qi.elements[j])
              qi.elements[j] = 0;
        }
       /* empty map again */
+      negcnt = 0;
       FOR_RULELITERALS(p, pp, r)
         if (p > 0)
          MAPCLR(&m, p);
+       else
+         negcnt++;
       if (i == qi.count)
        {
 #if 0
@@ -3389,7 +3611,14 @@ solver_addchoicerules(Solver *solv)
 #endif
          continue;
        }
-
+      /* add neg elements to the front */
+      if (negcnt > 1)
+       {
+         i = 0;
+         FOR_RULELITERALS(p, pp, r)
+          if (p < 0 && p != r->p)
+            queue_insert(&q, i++, p);
+       }
       /* don't add identical rules */
       if (lastaddedp == r->p && lastaddedcnt == q.count)
        {
@@ -3459,6 +3688,7 @@ solver_disablechoicerules(Solver *solv, Rule *r)
       if (p)
        solver_disablerule(solv, r);
     }
+  map_free(&m);
 }
 
 static void
@@ -3823,7 +4053,7 @@ find_obsolete_group(Solver *solv, Id obs, Queue *q)
     }
   /* find names so that we can build groups */
   queue_init_clone(&qn, q);
-  prune_to_best_version(solv->pool, &qn);
+  prune_to_best_version(pool, &qn);
 #if 0
 {
   for (i = 0; i < qn.count; i++)
@@ -3944,7 +4174,7 @@ for (j = 0; j < qq.count; j++)
 
       if (!qq.count)
        continue;
-      /* at least two goups, build rules */
+      /* at least two groups, build rules */
       group = 0;
       for (j = 0; j < qq.count; j++)
        {
@@ -4126,3 +4356,122 @@ solver_check_brokenorphanrules(Solver *solv, Queue *dq)
     }
 }
 
+const char *
+solver_ruleinfo2str(Solver *solv, SolverRuleinfo type, Id source, Id target, Id dep)
+{
+  Pool *pool = solv->pool;
+  char *s;
+  Solvable *ss;
+  switch (type)
+    {
+    case SOLVER_RULE_DISTUPGRADE:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " does not belong to a distupgrade repository", 0);
+    case SOLVER_RULE_INFARCH:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " has inferior architecture", 0);
+    case SOLVER_RULE_UPDATE:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " needs to stay installed or be updated", 0);
+    case SOLVER_RULE_FEATURE:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " needs to stay installed or be updated/downgraded", 0);
+    case SOLVER_RULE_JOB:
+      return pool_tmpjoin(pool, "job ", pool_job2str(pool, target, dep, 0), 0);
+    case SOLVER_RULE_JOB_UNSUPPORTED:
+      return pool_tmpjoin(pool, "unsupported job ", pool_job2str(pool, target, dep, 0), 0);
+    case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
+      return pool_tmpjoin(pool, "nothing provides requested ", pool_dep2str(pool, dep), 0);
+    case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
+      return pool_tmpjoin(pool, "requested package ", pool_dep2str(pool, dep), " does not exist");
+    case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
+      return pool_tmpjoin(pool, "requested ", pool_dep2str(pool, dep), " is provided by the system");
+    case SOLVER_RULE_BEST:
+      if (source > 0)
+        return pool_tmpjoin(pool, "install best update candidate for ", pool_solvid2str(pool, source), 0);
+      if (target > 0)
+       {
+         target = solver_rule2job(solv, target, &dep);
+         return pool_tmpjoin(pool, "best package for job ", pool_job2str(pool, target, dep, 0), 0);
+       }
+     return "best rule";
+    case SOLVER_RULE_PKG:
+      return "bad pkg rule type";
+    case SOLVER_RULE_PKG_NOT_INSTALLABLE:
+      ss = pool->solvables + source;
+      if (pool_disabled_solvable(pool, ss))
+        return pool_tmpjoin(pool, pool_solvid2str(pool, source), " is disabled", 0);
+      if (ss->arch && ss->arch != ARCH_SRC && ss->arch != ARCH_NOSRC &&
+          pool->id2arch && pool_arch2score(pool, ss->arch) == 0)
+        return pool_tmpjoin(pool, pool_solvid2str(pool, source), " does not have a compatible architecture", 0);
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " is not installable", 0);
+    case SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP:
+      s = pool_tmpjoin(pool, "nothing provides ", pool_dep2str(pool, dep), 0);
+      return pool_tmpappend(pool, s, " needed by ", pool_solvid2str(pool, source));
+    case SOLVER_RULE_PKG_SAME_NAME:
+      s = pool_tmpjoin(pool, "cannot install both ", pool_solvid2str(pool, source), 0);
+      return pool_tmpappend(pool, s, " and ", pool_solvid2str(pool, target));
+    case SOLVER_RULE_PKG_CONFLICTS:
+      s = pool_tmpappend(pool, pool_solvid2str(pool, source), " conflicts with ", pool_dep2str(pool, dep));
+      if (target)
+        s = pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
+      return s;
+    case SOLVER_RULE_PKG_SELF_CONFLICT:
+      s = pool_tmpjoin(pool, pool_solvid2str(pool, source), " conflicts with ", 0);
+      return pool_tmpappend(pool, s, pool_dep2str(pool, dep), " provided by itself");
+    case SOLVER_RULE_PKG_OBSOLETES:
+      s = pool_tmpappend(pool, pool_solvid2str(pool, source), " obsoletes ", pool_dep2str(pool, dep));
+      if (target)
+       s = pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
+      return s;
+    case SOLVER_RULE_PKG_INSTALLED_OBSOLETES:
+      s = pool_tmpjoin(pool, "installed ", pool_solvid2str(pool, source), 0);
+      s = pool_tmpappend(pool, s, " obsoletes ", pool_dep2str(pool, dep));
+      if (target)
+       s = pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
+      return s;
+    case SOLVER_RULE_PKG_IMPLICIT_OBSOLETES:
+      s = pool_tmpappend(pool, pool_solvid2str(pool, source), " implicitly obsoletes ", pool_dep2str(pool, dep));
+      if (target)
+       s = pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
+      return s;
+    case SOLVER_RULE_PKG_REQUIRES:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " requires ", pool_dep2str(pool, dep));
+    case SOLVER_RULE_PKG_RECOMMENDS:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " recommends ", pool_dep2str(pool, dep));
+    case SOLVER_RULE_PKG_CONSTRAINS:
+      s = pool_tmpappend(pool, pool_solvid2str(pool, source), " has constraint ", pool_dep2str(pool, dep));
+      return pool_tmpappend(pool, s, " conflicting with ", pool_solvid2str(pool, target));
+    case SOLVER_RULE_PKG_SUPPLEMENTS:
+      s = pool_tmpjoin(pool, pool_solvid2str(pool, source), " supplements ", pool_dep2str(pool, dep));
+      if (target)
+       s = pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
+      return s;
+    case SOLVER_RULE_YUMOBS:
+      s = pool_tmpjoin(pool, "both ", pool_solvid2str(pool, source), " and ");
+      s = pool_tmpjoin(pool, s, pool_solvid2str(pool, target), " obsolete ");
+      return pool_tmpappend(pool, s, pool_dep2str(pool, dep), 0);
+    case SOLVER_RULE_BLACK:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " can only be installed by a direct request", 0);
+    case SOLVER_RULE_STRICT_REPO_PRIORITY:
+      return pool_tmpjoin(pool, pool_solvid2str(pool, source), " is excluded by strict repo priority", 0);
+    case SOLVER_RULE_LEARNT:
+      return "learnt rule";
+    case SOLVER_RULE_CHOICE:
+      if (source > 0)
+       {
+         const char *s2;
+          type = solver_ruleinfo(solv, source, &source, &target, &dep);
+         s2 = solver_ruleinfo2str(solv, type, source, target, dep);
+         return pool_tmpjoin(pool, s2, " (limited version)", 0);
+       }
+      return "choice rule";
+    case SOLVER_RULE_RECOMMENDS:
+      if (source > 0)
+       {
+         const char *s2;
+          type = solver_ruleinfo(solv, source, &source, &target, &dep);
+         s2 = solver_ruleinfo2str(solv, type, source, target, dep);
+         return pool_tmpjoin(pool, s2, " (limited version)", 0);
+       }
+      return "recommends rule";
+    default:
+      return "bad rule type";
+    }
+}