Imported Upstream version 0.6.9
[platform/upstream/libsolv.git] / ext / repo_rpmdb.c
index e84b8ec..71c69ab 100644 (file)
 #include "queue.h"
 #include "chksum.h"
 #include "repo_rpmdb.h"
+#include "repo_solv.h"
+#ifdef ENABLE_COMPLEX_DEPS
+#include "pool_parserpmrichdep.h"
+#endif
 
 /* 3: added triggers */
 /* 4: fixed triggers */
-#define RPMDB_COOKIE_VERSION 4
+/* 5: fixed checksum copying */
+#define RPMDB_COOKIE_VERSION 5
 
 #define TAG_NAME               1000
 #define TAG_VERSION            1001
@@ -60,7 +65,7 @@
 #define TAG_BUILDTIME          1006
 #define TAG_BUILDHOST          1007
 #define TAG_INSTALLTIME                1008
-#define TAG_SIZE                1009
+#define TAG_SIZE               1009
 #define TAG_DISTRIBUTION       1010
 #define TAG_VENDOR             1011
 #define TAG_LICENSE            1014
 #define TAG_BASENAMES          1117
 #define TAG_DIRNAMES           1118
 #define TAG_PAYLOADFORMAT      1124
-#define TAG_PATCHESNAME         1133
+#define TAG_PATCHESNAME                1133
 #define TAG_FILECOLORS         1140
-#define TAG_SUGGESTSNAME       1156
-#define TAG_SUGGESTSVERSION    1157
-#define TAG_SUGGESTSFLAGS      1158
-#define TAG_ENHANCESNAME       1159
-#define TAG_ENHANCESVERSION    1160
-#define TAG_ENHANCESFLAGS      1161
+#define TAG_OLDSUGGESTSNAME    1156
+#define TAG_OLDSUGGESTSVERSION 1157
+#define TAG_OLDSUGGESTSFLAGS   1158
+#define TAG_OLDENHANCESNAME    1159
+#define TAG_OLDENHANCESVERSION 1160
+#define TAG_OLDENHANCESFLAGS   1161
 
 /* rpm5 tags */
 #define TAG_DISTEPOCH          1218
 /* rpm4 tags */
 #define TAG_LONGFILESIZES      5008
 #define TAG_LONGSIZE           5009
+#define TAG_RECOMMENDNAME      5046
+#define TAG_RECOMMENDVERSION   5047
+#define TAG_RECOMMENDFLAGS     5048
+#define TAG_SUGGESTNAME                5049
+#define TAG_SUGGESTVERSION     5050
+#define TAG_SUGGESTFLAGS       5051
+#define TAG_SUPPLEMENTNAME     5052
+#define TAG_SUPPLEMENTVERSION  5053
+#define TAG_SUPPLEMENTFLAGS    5054
+#define TAG_ENHANCENAME                5055
+#define TAG_ENHANCEVERSION     5056
+#define TAG_ENHANCEFLAGS       5057
 
 /* signature tags */
 #define        TAG_SIGBASE             256
 #define DEP_GREATER            (1 << 2)
 #define DEP_EQUAL              (1 << 3)
 #define DEP_STRONG             (1 << 27)
-#define DEP_PRE                        ((1 << 6) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12))
+#define DEP_PRE_IN             ((1 << 6) | (1 << 9) | (1 << 10))
+#define DEP_PRE_UN             ((1 << 6) | (1 << 11) | (1 << 12))
+#define DEP_RICH               (1 << 29)
 
 #define FILEFLAG_GHOST         (1 <<  6)
 
 
 #ifdef RPM5
-# define RPM_INDEX_SIZE 4
+# define RPM_INDEX_SIZE 4      /* just the rpmdbid */
 #else
-# define RPM_INDEX_SIZE 8
+# define RPM_INDEX_SIZE 8      /* rpmdbid + array index */
 #endif
 
 
@@ -147,7 +166,7 @@ typedef struct rpmhead {
   int cnt;
   int dcnt;
   unsigned char *dp;
-  int forcebinary;             /* sigh */
+  int forcebinary;             /* sigh, see rh#478907 */
   unsigned char data[1];
 } RpmHead;
 
@@ -212,6 +231,31 @@ headint32(RpmHead *h, int tag)
   return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
 }
 
+static unsigned long long *
+headint64array(RpmHead *h, int tag, int *cnt)
+{
+  unsigned int i, o;
+  unsigned long long *r;
+  unsigned char *d = headfindtag(h, tag);
+
+  if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
+    return 0;
+  o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
+  i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
+  if (o + 8 * i > h->dcnt)
+    return 0;
+  d = h->dp + o;
+  r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
+  if (cnt)
+    *cnt = i;
+  for (o = 0; o < i; o++, d += 8)
+    {
+      unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
+      r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
+    }
+  return r;
+}
+
 /* returns the first entry of an 64bit integer array */
 static unsigned long long
 headint64(RpmHead *h, int tag)
@@ -354,83 +398,16 @@ static char *headtoevr(RpmHead *h)
 static void
 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
 {
-  const unsigned char *cp;
-  int state = 0;
-  int c;
-  unsigned char *buf = 0, *bp;
-
-  /* check if it's already utf8, code taken from screen ;-) */
-  cp = (const unsigned char *)str;
-  while ((c = *cp++) != 0)
-    {
-      if (state)
-       {
-          if ((c & 0xc0) != 0x80)
-            break; /* encoding error */
-          c = (c & 0x3f) | (state << 6);
-          if (!(state & 0x40000000))
-           {
-              /* check for overlong sequences */
-              if ((c & 0x820823e0) == 0x80000000)
-                c = 0xfdffffff;
-              else if ((c & 0x020821f0) == 0x02000000)
-                c = 0xfff7ffff;
-              else if ((c & 0x000820f8) == 0x00080000)
-                c = 0xffffd000;
-              else if ((c & 0x0000207c) == 0x00002000)
-                c = 0xffffff70;
-            }
-        }
-      else
-       {
-          /* new sequence */
-          if (c >= 0xfe)
-            break;
-          else if (c >= 0xfc)
-            c = (c & 0x01) | 0xbffffffc;    /* 5 bytes to follow */
-          else if (c >= 0xf8)
-            c = (c & 0x03) | 0xbfffff00;    /* 4 */
-          else if (c >= 0xf0)
-            c = (c & 0x07) | 0xbfffc000;    /* 3 */
-          else if (c >= 0xe0)
-            c = (c & 0x0f) | 0xbff00000;    /* 2 */
-          else if (c >= 0xc2)
-            c = (c & 0x1f) | 0xfc000000;    /* 1 */
-          else if (c >= 0x80)
-            break;
-        }
-      state = (c & 0x80000000) ? c : 0;
-    }
-  if (c)
+  if (str[solv_validutf8(str)])
     {
-      /* not utf8, assume latin1 */
-      buf = solv_malloc(2 * strlen(str) + 1);
-      cp = (const unsigned char *)str;
-      str = (char *)buf;
-      bp = buf;
-      while ((c = *cp++) != 0)
-       {
-         if (c >= 0xc0)
-           {
-             *bp++ = 0xc3;
-             c ^= 0x80;
-           }
-         else if (c >= 0x80)
-           *bp++ = 0xc2;
-         *bp++ = c;
-       }
-      *bp++ = 0;
+      char *ustr = solv_latin1toutf8(str);     /* not utf8, assume latin1 */
+      repodata_set_str(repodata, handle, tag, ustr);
+      solv_free(ustr);
     }
-  repodata_set_str(repodata, handle, tag, str);
-  if (buf)
-    solv_free(buf);
+  else
+    repodata_set_str(repodata, handle, tag, str);
 }
 
-
-#define MAKEDEPS_FILTER_WEAK   (1 << 0)
-#define MAKEDEPS_FILTER_STRONG (1 << 1)
-#define MAKEDEPS_NO_RPMLIB     (1 << 2)
-
 /*
  * strong: 0: ignore strongness
  *         1: filter to strong
@@ -442,13 +419,45 @@ makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf,
   char **n, **v;
   unsigned int *f;
   int i, cc, nc, vc, fc;
-  int haspre;
+  int haspre, premask;
   unsigned int olddeps;
   Id *ida;
-  int strong;
+  int strong = 0;
 
-  strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
   n = headstringarray(rpmhead, tagn, &nc);
+  if (!n)
+    {
+      switch (tagn)
+       {
+       case TAG_SUGGESTNAME:
+         tagn = TAG_OLDSUGGESTSNAME;
+         tagv = TAG_OLDSUGGESTSVERSION;
+         tagf = TAG_OLDSUGGESTSFLAGS;
+         strong = -1;
+         break;
+       case TAG_ENHANCENAME:
+         tagn = TAG_OLDENHANCESNAME;
+         tagv = TAG_OLDENHANCESVERSION;
+         tagf = TAG_OLDENHANCESFLAGS;
+         strong = -1;
+         break;
+       case TAG_RECOMMENDNAME:
+         tagn = TAG_OLDSUGGESTSNAME;
+         tagv = TAG_OLDSUGGESTSVERSION;
+         tagf = TAG_OLDSUGGESTSFLAGS;
+         strong = 1;
+         break;
+       case TAG_SUPPLEMENTNAME:
+         tagn = TAG_OLDENHANCESNAME;
+         tagv = TAG_OLDENHANCESVERSION;
+         tagf = TAG_OLDENHANCESFLAGS;
+         strong = 1;
+         break;
+       default:
+         return 0;
+       }
+      n = headstringarray(rpmhead, tagn, &nc);
+    }
   if (!n || !nc)
     return 0;
   vc = fc = 0;
@@ -467,27 +476,28 @@ makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf,
 
   cc = nc;
   haspre = 0;  /* add no prereq marker */
-  if (flags)
+  premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
+  if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
     {
       /* we do filtering */
       cc = 0;
       for (i = 0; i < nc; i++)
        {
-         if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
+         if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
            continue;
-         if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
+         if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
            if (!strncmp(n[i], "rpmlib(", 7))
              continue;
-         if ((f[i] & DEP_PRE) != 0)
+         if ((f[i] & premask) != 0)
            haspre = 1;
          cc++;
        }
     }
-  else if (tagn == TAG_REQUIRENAME)
+  else if (premask)
     {
       /* no filtering, just look for the first prereq */
       for (i = 0; i < nc; i++)
-       if ((f[i] & DEP_PRE) != 0)
+       if ((f[i] & premask) != 0)
          {
            haspre = 1;
            break;
@@ -500,11 +510,12 @@ makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf,
       solv_free(f);
       return 0;
     }
-  cc += haspre;
+  cc += haspre;                /* add slot for the prereq marker */
   olddeps = repo_reserve_ids(repo, 0, cc);
   ida = repo->idarraydata + olddeps;
   for (i = 0; ; i++)
     {
+      Id id;
       if (i == nc)
        {
          if (haspre != 1)
@@ -513,34 +524,47 @@ makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf,
          i = 0;
          *ida++ = SOLVABLE_PREREQMARKER;
        }
-      if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
-       continue;
-      if (haspre == 1 && (f[i] & DEP_PRE) != 0)
+      if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
        continue;
-      if (haspre == 2 && (f[i] & DEP_PRE) == 0)
-       continue;
-      if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
+      if (haspre)
+       {
+         if (haspre == 1 && (f[i] & premask) != 0)
+           continue;
+         if (haspre == 2 && (f[i] & premask) == 0)
+           continue;
+       }
+      if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
        if (!strncmp(n[i], "rpmlib(", 7))
          continue;
+#ifdef ENABLE_COMPLEX_DEPS
+      if ((f[i] & (DEP_RICH|DEP_LESS| DEP_EQUAL|DEP_GREATER)) == DEP_RICH && n[i][0] == '(')
+       {
+         id = pool_parserpmrichdep(pool, n[i]);
+         if (id)
+           *ida++ = id;
+         else
+           cc--;
+         continue;
+       }
+#endif
+      id = pool_str2id(pool, n[i], 1);
       if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
        {
-         Id name, evr;
-         int flags = 0;
+         Id evr;
+         int fl = 0;
          if ((f[i] & DEP_LESS) != 0)
-           flags |= 4;
+           fl |= REL_LT;
          if ((f[i] & DEP_EQUAL) != 0)
-           flags |= 2;
+           fl |= REL_EQ;
          if ((f[i] & DEP_GREATER) != 0)
-           flags |= 1;
-         name = pool_str2id(pool, n[i], 1);
+           fl |= REL_GT;
          if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
            evr = pool_str2id(pool, v[i] + 2, 1);
          else
            evr = pool_str2id(pool, v[i], 1);
-         *ida++ = pool_rel2id(pool, name, evr, flags, 1);
+         id = pool_rel2id(pool, id, evr, fl, 1);
        }
-      else
-        *ida++ = pool_str2id(pool, n[i], 1);
+      *ida++ = id;
     }
   *ida++ = 0;
   repo->idarraysize += cc + 1;
@@ -557,17 +581,34 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
   Id did;
   int i, fszc;
   unsigned int *fkb, *fn, *fsz, *fm, *fino;
+  unsigned long long *fsz64;
   unsigned int inotest[256], inotestok;
 
   if (!fc)
     return;
-  /* XXX: use TAG_LONGFILESIZES if available */
-  fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc);
-  if (!fsz || fc != fszc)
+  if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
+    {
+      /* convert to kbyte */
+      fsz = solv_malloc2(fszc, sizeof(*fsz));
+      for (i = 0; i < fszc; i++)
+        fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
+      solv_free(fsz64);
+    }
+  else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
+    {
+      /* convert to kbyte */
+      for (i = 0; i < fszc; i++)
+        if (fsz[i])
+         fsz[i] = fsz[i] / 1024 + 1;
+    }
+  else
+    return;
+  if (fc != fszc)
     {
       solv_free(fsz);
       return;
     }
+
   /* stupid rpm records sizes of directories, so we have to check the mode */
   fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
   if (!fm || fc != fszc)
@@ -584,15 +625,18 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
       solv_free(fino);
       return;
     }
+
+  /* kill hardlinked entries */
   inotestok = 0;
   if (fc < sizeof(inotest))
     {
+      /* quick test just hashing the inode numbers */
       memset(inotest, 0, sizeof(inotest));
       for (i = 0; i < fc; i++)
        {
          int off, bit;
          if (fsz[i] == 0 || !S_ISREG(fm[i]))
-           continue;
+           continue;   /* does not matter */
          off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
          bit = 1 << (fino[i] & 31);
          if ((inotest[off] & bit) != 0)
@@ -600,10 +644,11 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
          inotest[off] |= bit;
        }
       if (i == fc)
-       inotestok = 1;
+       inotestok = 1;  /* no conflict found */
     }
   if (!inotestok)
     {
+      /* hardlinked files are possible, check ino/dev pairs */
       unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
       unsigned int *fx, j;
       unsigned int mask, hash, hh;
@@ -650,16 +695,18 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
       solv_free(fdev);
     }
   solv_free(fino);
+
+  /* sum up inode count and kbytes for each directory */
   fn = solv_calloc(dc, sizeof(unsigned int));
   fkb = solv_calloc(dc, sizeof(unsigned int));
   for (i = 0; i < fc; i++)
     {
       if (di[i] >= dc)
-       continue;
+       continue;       /* corrupt entry */
       fn[di[i]]++;
       if (fsz[i] == 0 || !S_ISREG(fm[i]))
        continue;
-      fkb[di[i]] += fsz[i] / 1024 + 1;
+      fkb[di[i]] += fsz[i];
     }
   solv_free(fsz);
   solv_free(fm);
@@ -684,8 +731,23 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
   solv_free(fkb);
 }
 
+static int
+is_filtered(const char *dir)
+{
+  if (!dir)
+    return 1;
+  /* the dirs always have a trailing / in rpm */
+  if (strstr(dir, "bin/"))
+    return 0;
+  if (!strncmp(dir, "/etc/", 5))
+    return 0;
+  if (!strcmp(dir, "/usr/lib/"))
+    return 2;
+  return 1;
+}
+
 static void
-addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
+addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
 {
   char **bn;
   char **dn;
@@ -693,7 +755,8 @@ addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
   int bnc, dnc, dic;
   int i;
   Id lastdid = 0;
-  int lastdii = -1;
+  unsigned int lastdii = -1;
+  int lastfiltered = 0;
 
   if (!data)
     return;
@@ -730,14 +793,27 @@ addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
        did = lastdid;
       else
        {
-         did = repodata_str2dir(data, dn[di[i]], 1);
+         if (di[i] >= dnc)
+           continue;   /* corrupt entry */
+         lastdii = di[i];
+         if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
+           {
+             lastfiltered = is_filtered(dn[di[i]]);
+             if (lastfiltered == 1)
+               continue;
+           }
+         did = repodata_str2dir(data, dn[lastdii], 1);
          if (!did)
            did = repodata_str2dir(data, "/", 1);
          lastdid = did;
-         lastdii = di[i];
        }
       if (b && *b == '/')      /* work around rpm bug */
        b++;
+      if (lastfiltered)
+       {
+         if (lastfiltered != 2 || strcmp(b, "sendmail"))
+           continue;
+       }
       repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
     }
   solv_free(bn);
@@ -771,9 +847,9 @@ addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
       if (ct[i])
         repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
       if (cn[i])
-        repodata_set_str(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
+        setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
       if (cx[i])
-        repodata_set_str(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
+        setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
       queue_push(&hq, h);
     }
   for (i = 0; i < hq.count; i++)
@@ -784,6 +860,49 @@ addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
   solv_free(cn);
 }
 
+static void
+set_description_author(Repodata *data, Id handle, char *str)
+{
+  char *aut, *p;
+  for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
+    if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
+      break;
+  if (aut)
+    {
+      /* oh my, found SUSE special author section */
+      int l = aut - str;
+      str = solv_strdup(str);
+      aut = str + l;
+      str[l] = 0;
+      while (l > 0 && str[l - 1] == '\n')
+       str[--l] = 0;
+      if (l)
+       setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
+      p = aut + 19;
+      aut = str;       /* copy over */
+      while (*p == ' ' || *p == '\n')
+       p++;
+      while (*p)
+       {
+         if (*p == '\n')
+           {
+             *aut++ = *p++;
+             while (*p == ' ')
+               p++;
+             continue;
+           }
+         *aut++ = *p++;
+       }
+      while (aut != str && aut[-1] == '\n')
+       aut--;
+      *aut = 0;
+      if (*str)
+       setutf8string(data, handle, SOLVABLE_AUTHORS, str);
+      free(str);
+    }
+  else if (*str)
+    setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
+}
 
 static int
 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
@@ -820,14 +939,15 @@ rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead,
   s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
   if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
     s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
-  s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
+  s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags);
   s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
   s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
 
-  s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
-  s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
-  s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
-  s->enhances  = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
+  s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0);
+  s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0);
+  s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0);
+  s->enhances  = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0);
+
   s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
   s->conflicts = repo_fix_conflicts(repo, s->conflicts);
 
@@ -844,47 +964,7 @@ rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead,
         setutf8string(data, handle, SOLVABLE_SUMMARY, str);
       str = headstring(rpmhead, TAG_DESCRIPTION);
       if (str)
-       {
-         char *aut, *p;
-         for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
-           if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
-             break;
-         if (aut)
-           {
-             /* oh my, found SUSE special author section */
-             int l = aut - str;
-             str = solv_strdup(str);
-             aut = str + l;
-             str[l] = 0;
-             while (l > 0 && str[l - 1] == '\n')
-               str[--l] = 0;
-             if (l)
-                setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
-             p = aut + 19;
-             aut = str;        /* copy over */
-             while (*p == ' ' || *p == '\n')
-               p++;
-             while (*p)
-               {
-                 if (*p == '\n')
-                   {
-                     *aut++ = *p++;
-                     while (*p == ' ')
-                       p++;
-                     continue;
-                   }
-                 *aut++ = *p++;
-               }
-             while (aut != str && aut[-1] == '\n')
-               aut--;
-             *aut = 0;
-             if (*str)
-               setutf8string(data, handle, SOLVABLE_AUTHORS, str);
-             free(str);
-           }
-         else if (*str)
-           setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
-       }
+       set_description_author(data, handle, str);
       str = headstring(rpmhead, TAG_GROUP);
       if (str)
         repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
@@ -955,7 +1035,7 @@ rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead,
            }
        }
       if ((flags & RPM_ADD_NO_FILELIST) == 0)
-       addfilelist(data, handle, rpmhead);
+       addfilelist(data, handle, rpmhead, flags);
       if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
        addchangelog(data, handle, rpmhead);
     }
@@ -969,7 +1049,7 @@ rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead,
  */
 
 struct rpmdbstate {
-  Pool *pool; 
+  Pool *pool;
   char *rootdir;
 
   RpmHead *rpmhead;    /* header storage space */
@@ -979,6 +1059,7 @@ struct rpmdbstate {
   DB_ENV *dbenv;       /* database environment */
   DB *db;              /* packages database */
   int byteswapped;     /* endianess of packages database */
+  int is_ostree;       /* read-only db that lives in /usr/share/rpm */
 };
 
 struct rpmdbentry {
@@ -1022,10 +1103,41 @@ static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
 #endif
 }
 
+#ifdef FEDORA
+int
+serialize_dbenv_ops(struct rpmdbstate *state)
+{
+  char lpath[PATH_MAX];
+  mode_t oldmask;
+  int fd;
+  struct flock fl;
+
+  snprintf(lpath, PATH_MAX, "%s/var/lib/rpm/.dbenv.lock", state->rootdir ? state->rootdir : "");
+  oldmask = umask(022);
+  fd = open(lpath, (O_RDWR|O_CREAT), 0644);
+  umask(oldmask);
+  if (fd < 0)
+    return -1;
+  memset(&fl, 0, sizeof(fl));
+  fl.l_type = F_WRLCK;
+  fl.l_whence = SEEK_SET;
+  for (;;)
+    {
+      if (fcntl(fd, F_SETLKW, &fl) != -1)
+       return fd;
+      if (errno != EINTR)
+       break;
+    }
+  close(fd);
+  return -1;
+}
+#endif
+
 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
 static int
-opendbenv(struct rpmdbstate *state, const char *rootdir)
+opendbenv(struct rpmdbstate *state)
 {
+  const char *rootdir = state->rootdir;
   char dbpath[PATH_MAX];
   DB_ENV *dbenv = 0;
   int r;
@@ -1038,12 +1150,19 @@ opendbenv(struct rpmdbstate *state, const char *rootdir)
   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
   if (access(dbpath, W_OK) == -1)
     {
+      snprintf(dbpath, PATH_MAX, "%s/usr/share/rpm/Packages", rootdir ? rootdir : "");
+      if (access(dbpath, R_OK) == 0)
+       state->is_ostree = 1;
+      snprintf(dbpath, PATH_MAX, "%s%s", rootdir ? rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
     }
   else
     {
 #ifdef FEDORA
+      int serialize_fd = serialize_dbenv_ops(state);
       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
+      if (serialize_fd >= 0)
+       close(serialize_fd);
 #else
       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
 #endif
@@ -1058,20 +1177,45 @@ opendbenv(struct rpmdbstate *state, const char *rootdir)
   return 1;
 }
 
+static void
+closedbenv(struct rpmdbstate *state)
+{
+#ifdef FEDORA
+  uint32_t eflags = 0;
+#endif
+
+  if (!state->dbenv)
+    return;
+#ifdef FEDORA
+  (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
+  if (!(eflags & DB_PRIVATE))
+    {
+      int serialize_fd = serialize_dbenv_ops(state);
+      state->dbenv->close(state->dbenv, 0);
+      if (serialize_fd >= 0)
+       close(serialize_fd);
+    }
+  else
+    state->dbenv->close(state->dbenv, 0);
+#else
+  state->dbenv->close(state->dbenv, 0);
+#endif
+  state->dbenv = 0;
+}
+
 static int
 openpkgdb(struct rpmdbstate *state)
 {
   if (state->dbopened)
     return state->dbopened > 0 ? 1 : 0;
   state->dbopened = -1;
-  if (!state->dbenv && !opendbenv(state, state->rootdir))
+  if (!state->dbenv && !opendbenv(state))
     return 0;
   if (db_create(&state->db, state->dbenv, 0))
     {
       pool_error(state->pool, 0, "db_create: %s", strerror(errno));
       state->db = 0;
-      state->dbenv->close(state->dbenv, 0);
-      state->dbenv = 0;
+      closedbenv(state);
       return 0;
     }
   if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
@@ -1079,8 +1223,7 @@ openpkgdb(struct rpmdbstate *state)
       pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
       state->db->close(state->db, 0);
       state->db = 0;
-      state->dbenv->close(state->dbenv, 0);
-      state->dbenv = 0;
+      closedbenv(state);
       return 0;
     }
   if (state->db->get_byteswapped(state->db, &state->byteswapped))
@@ -1088,8 +1231,7 @@ openpkgdb(struct rpmdbstate *state)
       pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
       state->db->close(state->db, 0);
       state->db = 0;
-      state->dbenv->close(state->dbenv, 0);
-      state->dbenv = 0;
+      closedbenv(state);
       return 0;
     }
   state->dbopened = 1;
@@ -1120,7 +1262,7 @@ getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *ma
   if (namedatap)
     *namedatap = 0;
 
-  if (!state->dbenv && !opendbenv(state, state->rootdir))
+  if (!state->dbenv && !opendbenv(state))
     return 0;
   dbenv = state->dbenv;
   if (db_create(&db, dbenv, 0))
@@ -1286,12 +1428,12 @@ freestate(struct rpmdbstate *state)
   /* close down */
   if (!state)
     return;
-  if (state->rootdir)
-    solv_free(state->rootdir);
   if (state->db)
     state->db->close(state->db, 0);
   if (state->dbenv)
-    state->dbenv->close(state->dbenv, 0);
+    closedbenv(state);
+  if (state->rootdir)
+    solv_free(state->rootdir);
   solv_free(state->rpmhead);
 }
 
@@ -1314,8 +1456,9 @@ rpm_state_free(void *state)
 }
 
 static int
-count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
+count_headers(struct rpmdbstate *state)
 {
+  Pool *pool = state->pool;
   char dbpath[PATH_MAX];
   struct stat statbuf;
   DB *db = 0;
@@ -1324,12 +1467,12 @@ count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
   DBT dbkey;
   DBT dbdata;
 
-  snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
+  snprintf(dbpath, PATH_MAX, "%s%s/Name", state->rootdir ? state->rootdir : "", state->is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
   if (stat(dbpath, &statbuf))
     return 0;
   memset(&dbkey, 0, sizeof(dbkey));
   memset(&dbdata, 0, sizeof(dbdata));
-  if (db_create(&db, dbenv, 0))
+  if (db_create(&db, state->dbenv, 0))
     {
       pool_error(pool, 0, "db_create: %s", strerror(errno));
       return 0;
@@ -1355,29 +1498,12 @@ count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
 
 /******************************************************************/
 
-static Id
-copyreldep(Pool *pool, Pool *frompool, Id id)
-{
-  Reldep *rd = GETRELDEP(frompool, id);
-  Id name = rd->name, evr = rd->evr;
-  if (ISRELDEP(name))
-    name = copyreldep(pool, frompool, name);
-  else
-    name = pool_str2id(pool, pool_id2str(frompool, name), 1);
-  if (ISRELDEP(evr))
-    evr = copyreldep(pool, frompool, evr);
-  else
-    evr = pool_str2id(pool, pool_id2str(frompool, evr), 1);
-  return pool_rel2id(pool, name, evr, rd->flags, 1);
-}
-
 static Offset
 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
 {
   int cc;
-  Id id, *ida, *from;
+  Id *ida, *from;
   Offset ido;
-  Pool *frompool = fromrepo->pool;
 
   if (!fromoff)
     return 0;
@@ -1388,46 +1514,32 @@ copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
     return 0;
   ido = repo_reserve_ids(repo, 0, cc);
   ida = repo->idarraydata + ido;
-  if (frompool && pool != frompool)
-    {
-      while (*from)
-       {
-         id = *from++;
-         if (ISRELDEP(id))
-           id = copyreldep(pool, frompool, id);
-         else
-           id = pool_str2id(pool, pool_id2str(frompool, id), 1);
-         *ida++ = id;
-       }
-      *ida = 0;
-    }
-  else
-    memcpy(ida, from, (cc + 1) * sizeof(Id));
+  memcpy(ida, from, (cc + 1) * sizeof(Id));
   repo->idarraysize += cc + 1;
   return ido;
 }
 
 #define COPYDIR_DIRCACHE_SIZE 512
 
-static Id copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache);
+static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
 
 static inline Id
-copydir(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
+copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
 {
   if (cache && cache[did & 255] == did)
     return cache[(did & 255) + 256];
-  return copydir_complex(pool, data, fromspool, fromdata, did, cache);
+  return copydir_complex(pool, data, fromdata, did, cache);
 }
 
 static Id
-copydir_complex(Pool *pool, Repodata *data, Stringpool *fromspool, Repodata *fromdata, Id did, Id *cache)
+copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
 {
   Id parent = dirpool_parent(&fromdata->dirpool, did);
   Id compid = dirpool_compid(&fromdata->dirpool, did);
   if (parent)
-    parent = copydir(pool, data, fromspool, fromdata, parent, cache);
-  if (fromspool != &pool->ss)
-    compid = pool_str2id(pool, stringpool_id2str(fromspool, compid), 1);
+    parent = copydir(pool, data, fromdata, parent, cache);
+  if (data->localpool || fromdata->localpool)
+    compid = repodata_translate_id(data, fromdata, compid, 1);
   compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
   if (cache)
     {
@@ -1451,26 +1563,17 @@ solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, K
   Id id, keyname;
   Repodata *data = cbdata->data;
   Id handle = cbdata->handle;
-  Pool *pool = data->repo->pool, *frompool = fromdata->repo->pool;
-  Stringpool *fromspool = fromdata->localpool ? &fromdata->spool : &frompool->ss;
+  Pool *pool = data->repo->pool;
 
   keyname = key->name;
-  if (keyname >= ID_NUM_INTERNAL && pool != frompool)
-    keyname = pool_str2id(pool, pool_id2str(frompool, keyname), 1);
   switch(key->type)
     {
     case REPOKEY_TYPE_ID:
     case REPOKEY_TYPE_CONSTANTID:
     case REPOKEY_TYPE_IDARRAY: /* used for triggers */
       id = kv->id;
-      assert(!data->localpool);        /* implement me! */
-      if (pool != frompool || fromdata->localpool)
-       {
-         if (ISRELDEP(id))
-           id = copyreldep(pool, frompool, id);
-         else
-           id = pool_str2id(pool, stringpool_id2str(fromspool, id), 1);
-       }
+      if (data->localpool || fromdata->localpool)
+       id = repodata_translate_id(data, fromdata, id, 1);
       if (key->type == REPOKEY_TYPE_ID)
         repodata_set_id(data, handle, keyname, id);
       else if (key->type == REPOKEY_TYPE_CONSTANTID)
@@ -1492,14 +1595,12 @@ solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, K
       break;
     case REPOKEY_TYPE_DIRNUMNUMARRAY:
       id = kv->id;
-      assert(!data->localpool);        /* implement me! */
-      id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
+      id = copydir(pool, data, fromdata, id, cbdata->dircache);
       repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
       break;
     case REPOKEY_TYPE_DIRSTRARRAY:
       id = kv->id;
-      assert(!data->localpool);        /* implement me! */
-      id = copydir(pool, data, fromspool, fromdata, id, cbdata->dircache);
+      id = copydir(pool, data, fromdata, id, cbdata->dircache);
       repodata_add_dirstr(data, handle, keyname, id, kv->str);
       break;
     case REPOKEY_TYPE_FLEXARRAY:
@@ -1519,6 +1620,11 @@ solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, K
       repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
       break;
     default:
+      if (solv_chksum_len(key->type))
+       {
+         repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
+         break;
+       }
       break;
     }
   return 0;
@@ -1527,30 +1633,17 @@ solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, K
 static void
 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
 {
+  int p, i;
   Repo *repo = s->repo;
-  Repo *fromrepo = r->repo;
   Pool *pool = repo->pool;
+  Repo *fromrepo = r->repo;
   struct solvable_copy_cbdata cbdata;
 
   /* copy solvable data */
-  if (pool == fromrepo->pool)
-    {
-      s->name = r->name;
-      s->evr = r->evr;
-      s->arch = r->arch;
-      s->vendor = r->vendor;
-    }
-  else
-    {
-      if (r->name)
-       s->name = pool_str2id(pool, pool_id2str(fromrepo->pool, r->name), 1);
-      if (r->evr)
-       s->evr = pool_str2id(pool, pool_id2str(fromrepo->pool, r->evr), 1);
-      if (r->arch)
-       s->arch = pool_str2id(pool, pool_id2str(fromrepo->pool, r->arch), 1);
-      if (r->vendor)
-       s->vendor = pool_str2id(pool, pool_id2str(fromrepo->pool, r->vendor), 1);
-    }
+  s->name = r->name;
+  s->evr = r->evr;
+  s->arch = r->arch;
+  s->vendor = r->vendor;
   s->provides = copydeps(pool, repo, r->provides, fromrepo);
   s->requires = copydeps(pool, repo, r->requires, fromrepo);
   s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
@@ -1567,7 +1660,17 @@ solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
   cbdata.handle = s - pool->solvables;
   cbdata.subhandle = 0;
   cbdata.dircache = dircache;
-  repo_search(fromrepo, (r - fromrepo->pool->solvables), 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
+  p = r - fromrepo->pool->solvables;
+#if 0
+  repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
+#else
+  FOR_REPODATAS(fromrepo, i, data)
+    {
+      if (p >= data->start && p < data->end)
+        repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
+      cbdata.dircache = 0;     /* only for first repodata */
+    }
+#endif
 }
 
 /* used to sort entries by package name that got returned in some database order */
@@ -1619,12 +1722,24 @@ swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
 }
 
 static void
-mkrpmdbcookie(struct stat *st, unsigned char *cookie)
+mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
 {
+  int f = 0;
   memset(cookie, 0, 32);
   cookie[3] = RPMDB_COOKIE_VERSION;
   memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
   memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
+  if ((flags & RPM_ADD_WITH_PKGID) != 0)
+    f |= 1;
+  if ((flags & RPM_ADD_WITH_HDRID) != 0)
+    f |= 2;
+  if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
+    f |= 4;
+  if ((flags & RPM_ADD_NO_FILELIST) == 0)
+    f |= 8;
+  if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
+    cookie[1] = 1;
+  cookie[0] = f;
 }
 
 /*
@@ -1643,7 +1758,6 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
   Id oldcookietype = 0;
   Repodata *data;
   int count = 0, done = 0;
-  const char *rootdir = 0;
   struct rpmdbstate state;
   int i;
   Solvable *s;
@@ -1652,26 +1766,33 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
   now = solv_timems(0);
   memset(&state, 0, sizeof(state));
   state.pool = pool;
+  if (flags & REPO_USE_ROOTDIR)
+    state.rootdir = solv_strdup(pool_get_rootdir(pool));
 
   data = repo_add_repodata(repo, flags);
 
-  if (ref && !(ref->nsolvables && ref->rpmdbid))
-    ref = 0;
+  if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
+    {
+      if ((flags & RPMDB_EMPTY_REFREPO) != 0)
+       repo_empty(ref, 1);
+      ref = 0;
+    }
 
-  if (flags & REPO_USE_ROOTDIR)
-    rootdir = pool_get_rootdir(pool);
-  if (!opendbenv(&state, rootdir))
-    return -1;
+  if (!opendbenv(&state))
+    {
+      solv_free(state.rootdir);
+      return -1;
+    }
 
   /* XXX: should get ro lock of Packages database! */
-  snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
+  snprintf(dbpath, PATH_MAX, "%s%s/Packages", state.rootdir ? state.rootdir : "", state.is_ostree ? "/usr/share/rpm" : "/var/lib/rpm");
   if (stat(dbpath, &packagesstat))
     {
       pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
       freestate(&state);
       return -1;
     }
-  mkrpmdbcookie(&packagesstat, newcookie);
+  mkrpmdbcookie(&packagesstat, newcookie, flags);
   repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
 
   if (ref)
@@ -1682,8 +1803,10 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
       Id dbid;
       DBC *dbc = 0;
 
+      if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
+       repo_empty(ref, 1);     /* get it out of the way */
       if ((flags & RPMDB_REPORT_PROGRESS) != 0)
-       count = count_headers(pool, rootdir, state.dbenv);
+       count = count_headers(&state);
       if (!openpkgdb(&state))
        {
          freestate(&state);
@@ -1820,7 +1943,10 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
            }
         }
 
-      s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
+      if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
+        s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
+      else
+        s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
       if (!repo->rpmdbid)
         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
 
@@ -1851,7 +1977,7 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
          if (res <= 0)
            {
              if (!res)
-               return pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
+               pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
              freestate(&state);
              solv_free(entries);
              solv_free(namedata);
@@ -1871,6 +1997,8 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
       solv_free(entries);
       solv_free(namedata);
       solv_free(refhash);
+      if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
+       repo_empty(ref, 1);
     }
 
   freestate(&state);
@@ -1884,6 +2012,32 @@ repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
   return 0;
 }
 
+int
+repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
+{
+  int res;
+  Repo *ref = 0;
+
+  if (!fp)
+    return repo_add_rpmdb(repo, 0, flags);
+  ref = repo_create(repo->pool, "add_rpmdb_reffp");
+  if (repo_add_solv(ref, fp, 0) != 0)
+    {
+      repo_free(ref, 1);
+      ref = 0;
+    }
+  if (ref && ref->start == ref->end)
+    {
+      repo_free(ref, 1);
+      ref = 0;
+    }
+  if (ref)
+    repo_disable_paging(ref);
+  res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
+  if (ref)
+    repo_free(ref, 1);
+  return res;
+}
 
 static inline unsigned int
 getu32(const unsigned char *dp)
@@ -1911,8 +2065,8 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
   unsigned char hdrid[32];
   int pkgidtype, leadsigidtype, hdridtype;
   Id chksumtype = 0;
-  void *chksumh = 0;
-  void *leadsigchksumh = 0;
+  Chksum *chksumh = 0;
+  Chksum *leadsigchksumh = 0;
   int forcebinary = 0;
 
   data = repo_add_repodata(repo, flags);
@@ -2132,8 +2286,7 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
       repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
       chksumh = solv_chksum_free(chksumh, 0);
     }
-  if (rpmhead)
-    solv_free(rpmhead);
+  solv_free(rpmhead);
   if (!(flags & REPO_NO_INTERNALIZE))
     repodata_internalize(data);
   return s - pool->solvables;