add pool_whatmatchesdep and selection_make_deps to query for packages intersecting...
[platform/upstream/libsolv.git] / src / solvable.c
index 99fe788..b822784 100644 (file)
@@ -20,6 +20,8 @@
 #include "pool.h"
 #include "repo.h"
 #include "util.h"
+#include "policy.h"
+#include "poolvendor.h"
 #include "chksum.h"
 
 const char *
@@ -44,10 +46,16 @@ pool_solvable2str(Pool *pool, Solvable *s)
     }
   p = pool_alloctmpspace(pool, nl + el + al + 3);
   strcpy(p, n);
-  p[nl] = '-';
-  strncpy(p + nl + 1, e, el);
-  p[nl + 1 + el] = '.';
-  strcpy(p + nl + 1 + el + 1, a);
+  if (el)
+    {
+      p[nl++] = '-';
+      strncpy(p + nl, e, el);
+    }
+  if (al)
+    {
+      p[nl + el] = pool->disttype == DISTTYPE_HAIKU ? '-' : '.';
+      strcpy(p + nl + el + 1, a);
+    }
   return p;
 }
 
@@ -89,12 +97,36 @@ solvable_lookup_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
   return repo_lookup_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
 }
 
+static const char *
+solvable_lookup_str_joinarray(Solvable *s, Id keyname, const char *joinstr)
+{
+  Queue q;
+  Id qbuf[10];
+  char *str = 0;
+
+  queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
+  if (solvable_lookup_idarray(s, keyname, &q) && q.count)
+    {
+      Pool *pool = s->repo->pool;
+      int i;
+      str = pool_tmpjoin(pool, pool_id2str(pool, q.elements[0]), 0, 0);
+      for (i = 1; i < q.count; i++)
+       str = pool_tmpappend(pool, str, joinstr, pool_id2str(pool, q.elements[i]));
+    }
+  queue_free(&q);
+  return str;
+}
+
 const char *
 solvable_lookup_str(Solvable *s, Id keyname)
 {
+  const char *str;
   if (!s->repo)
     return 0;
-  return repo_lookup_str(s->repo, s - s->repo->pool->solvables, keyname);
+  str = repo_lookup_str(s->repo, s - s->repo->pool->solvables, keyname);
+  if (!str && (keyname == SOLVABLE_LICENSE || keyname == SOLVABLE_GROUP))
+    str = solvable_lookup_str_joinarray(s, keyname, ", ");
+  return str;
 }
 
 static const char *
@@ -265,11 +297,11 @@ evrid2vrstr(Pool *pool, Id evrid)
     return evr;
   for (p = evr; *p >= '0' && *p <= '9'; p++)
     ;
-  return p != evr && *p == ':' ? p + 1 : evr;
+  return p != evr && *p == ':' && p[1] ? p + 1 : evr;
 }
 
 const char *
-solvable_get_location(Solvable *s, unsigned int *medianrp)
+solvable_lookup_location(Solvable *s, unsigned int *medianrp)
 {
   Pool *pool;
   int l = 0;
@@ -282,7 +314,7 @@ solvable_get_location(Solvable *s, unsigned int *medianrp)
     return 0;
   pool = s->repo->pool;
   if (medianrp)
-    *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
+    *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 0);
   if (solvable_lookup_void(s, SOLVABLE_MEDIADIR))
     mediadir = pool_id2str(pool, s->arch);
   else
@@ -317,6 +349,15 @@ solvable_get_location(Solvable *s, unsigned int *medianrp)
 }
 
 const char *
+solvable_get_location(Solvable *s, unsigned int *medianrp)
+{
+  const char *loc = solvable_lookup_location(s, medianrp);
+  if (medianrp && *medianrp == 0)
+    *medianrp = 1;     /* compat, to be removed */
+  return loc;
+}
+
+const char *
 solvable_lookup_sourcepkg(Solvable *s)
 {
   Pool *pool;
@@ -334,18 +375,7 @@ solvable_lookup_sourcepkg(Solvable *s)
     return 0;
   archid = solvable_lookup_id(s, SOLVABLE_SOURCEARCH);
   if (solvable_lookup_void(s, SOLVABLE_SOURCEEVR))
-    {
-      evr = pool_id2str(pool, s->evr);
-      if (evr && (archid == ARCH_SRC || archid == ARCH_NOSRC))
-       {
-         /* assume rpm, strip epoch */
-         const char *p;
-         for (p = evr; *p >= '0' && *p <= '9'; p++)
-           ;
-         if (*p == ':' && p[1])
-           evr = p + 1;
-       }
-    }
+    evr = evrid2vrstr(pool, s->evr);
   else
     evr = solvable_lookup_str(s, SOLVABLE_SOURCEEVR);
   if (archid == ARCH_SRC || archid == ARCH_NOSRC)
@@ -391,7 +421,7 @@ static int providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id
   return 0;
 }
 
-static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *noobsoletesmap)
+static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *multiversionmap)
 {
   Id p, pp;
   FOR_PROVIDES(p, pp, dep)
@@ -400,7 +430,7 @@ static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int is
        return -1;
       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
        continue;
-      if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
+      if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
        if (providedbyinstalled_multiversion(pool, installed, p, dep))
          continue;
       if (MAPTST(installed, p))
@@ -426,7 +456,7 @@ static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int is
  * -1: solvable is installable, but doesn't constrain any installed packages
  */
 int
-solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *noobsoletesmap)
+solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *multiversionmap)
 {
   Pool *pool = s->repo->pool;
   Solvable *s2;
@@ -460,15 +490,21 @@ solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsm
       conp = s->repo->idarraydata + s->conflicts;
       while ((con = *conp++) != 0)
        {
-         if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
-           return 0;
+         if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
+           {
+             if (ispatch && solvable_is_irrelevant_patch(s, installedmap))
+               return -1;
+             return 0;
+           }
          if (!interesting && ISRELDEP(con))
            {
               con = dep2name(pool, con);
-             if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
+             if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
                interesting = 1;
            }
        }
+      if (ispatch && interesting && solvable_is_irrelevant_patch(s, installedmap))
+       interesting = 0;
     }
 #if 0
   if (s->repo)
@@ -527,7 +563,7 @@ solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsm
  * by a queue.
  */
 int
-solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *noobsoletesmap)
+solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *multiversionmap)
 {
   Pool *pool = s->repo->pool;
   int i;
@@ -542,7 +578,7 @@ solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *noobsolet
       if (p > 0)               /* makes it work with decisionq */
        MAPSET(&installedmap, p);
     }
-  r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
+  r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
   map_free(&installedmap);
   return r;
 }
@@ -553,7 +589,7 @@ solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *noobsolet
  * by a repo containing the installed solvables.
  */
 int
-solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *noobsoletesmap)
+solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *multiversionmap)
 {
   Pool *pool = s->repo->pool;
   Id p;
@@ -564,11 +600,88 @@ solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *noobsoletes
   map_init(&installedmap, pool->nsolvables);
   FOR_REPO_SOLVABLES(installed, p, s2)
     MAPSET(&installedmap, p);
-  r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
+  r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
   map_free(&installedmap);
   return r;
 }
 
+/* FIXME: this mirrors policy_illegal_vendorchange */
+static int
+pool_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
+{
+  Id v1, v2; 
+  Id vendormask1, vendormask2;
+
+  if (pool->custom_vendorcheck)
+    return pool->custom_vendorcheck(pool, s1, s2);
+  /* treat a missing vendor as empty string */
+  v1 = s1->vendor ? s1->vendor : ID_EMPTY;
+  v2 = s2->vendor ? s2->vendor : ID_EMPTY;
+  if (v1 == v2) 
+    return 0;
+  vendormask1 = pool_vendor2mask(pool, v1);
+  if (!vendormask1)
+    return 1;   /* can't match */
+  vendormask2 = pool_vendor2mask(pool, v2);
+  if ((vendormask1 & vendormask2) != 0)
+    return 0;
+  return 1;     /* no class matches */
+}
+
+/* check if this patch is relevant according to the vendor. To bad that patches
+ * don't have a vendor, so we need to do some careful repo testing. */
+int
+solvable_is_irrelevant_patch(Solvable *s, Map *installedmap)
+{
+  Pool *pool = s->repo->pool;
+  Id con, *conp;
+  int hadpatchpackage = 0;
+
+  if (!s->conflicts)
+    return 0;
+  conp = s->repo->idarraydata + s->conflicts;
+  while ((con = *conp++) != 0)
+    {
+      Reldep *rd;
+      Id p, pp, p2, pp2;
+      if (!ISRELDEP(con))
+       continue;
+      rd = GETRELDEP(pool, con);
+      if (rd->flags != REL_LT)
+       continue;
+      FOR_PROVIDES(p, pp, con)
+       {
+         Solvable *si;
+         if (!MAPTST(installedmap, p))
+           continue;
+         si = pool->solvables + p;
+         if (!pool_match_nevr(pool, si, con))
+           continue;
+         FOR_PROVIDES(p2, pp2, rd->name)
+           {
+             Solvable *s2 = pool->solvables + p2;
+             if (!pool_match_nevr(pool, s2, rd->name))
+               continue;
+             if (pool_match_nevr(pool, s2, con))
+               continue;       /* does not fulfill patch */
+             if (s2->repo == s->repo)
+               {
+                 hadpatchpackage = 1;
+                 /* ok, we have a package from the patch repo that solves the conflict. check vendor */
+                 if (si->vendor == s2->vendor)
+                   return 0;
+                 if (!pool_illegal_vendorchange(pool, si, s2))
+                   return 0;
+                 /* vendor change was illegal, ignore conflict */
+               }
+           }
+       }
+    }
+  /* if we didn't find a patchpackage don't claim that the patch is irrelevant */
+  if (!hadpatchpackage)
+    return 0;
+  return 1;
+}
 
 /*****************************************************************************/
 
@@ -738,3 +851,25 @@ solvable_set_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
   repo_set_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
 }
 
+void
+solvable_unset(Solvable *s, Id keyname)
+{
+  repo_unset(s->repo, s - s->repo->pool->solvables, keyname);
+}
+
+/* return true if a dependency intersects dep in the keyname array */
+int
+solvable_matchesdep(Solvable *s, Id keyname, Id dep)
+{
+  int i;
+  Pool *pool = s->repo->pool;
+  Queue q;
+  queue_init(&q);
+  solvable_lookup_idarray(s, keyname, &q);
+  for (i = 0; i < q.count; i++)
+    if (pool_match_dep(pool, q.elements[i], dep))
+      break;
+  i = i == q.count ? 0 : 1;
+  queue_free(&q);
+  return i;
+}