Imported Upstream version 0.6.29
[platform/upstream/libsolv.git] / ext / repo_rpmdb.c
index fe05bec..9fb5a29 100644 (file)
@@ -177,7 +177,6 @@ typedef struct rpmhead {
   int cnt;
   unsigned int dcnt;
   unsigned char *dp;
-  int forcebinary;             /* sigh, see rh#478907 */
   unsigned char data[1];
 } RpmHead;
 
@@ -262,7 +261,7 @@ headint64array(RpmHead *h, int tag, int *cnt)
   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]);
+      r[o] = (unsigned long long)x << 32 | (unsigned int)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
     }
   return r;
 }
@@ -281,7 +280,7 @@ headint64(RpmHead *h, int tag)
     return 0;
   d = h->dp + o;
   i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
-  return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
+  return (unsigned long long)i << 32 | (unsigned int)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
 }
 
 static unsigned int *
@@ -366,6 +365,18 @@ headbinary(RpmHead *h, int tag, unsigned int *sizep)
   return h->dp + o;
 }
 
+static int
+headissourceheuristic(RpmHead *h)
+{
+  unsigned int i, o;
+  unsigned char *d = headfindtag(h, TAG_DIRNAMES);
+  if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
+    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];
+  return i == 1 && o < h->dcnt && !h->dp[o] ? 1 : 0;
+}
+
 static char *headtoevr(RpmHead *h)
 {
   unsigned int epoch;
@@ -624,6 +635,24 @@ makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf,
   return olddeps;
 }
 
+static Id
+repodata_str2dir_rooted(Repodata *data, char *str, int create)
+{
+  char buf[256], *bp;
+  int l = strlen(str);
+  Id id;
+
+  if (l + 2 <= sizeof(buf))
+    bp = buf;
+  else
+    bp = solv_malloc(l + 2);
+  bp[0] = '/';
+  strcpy(bp + 1, str);
+  id = repodata_str2dir(data, bp, create);
+  if (bp != buf)
+    solv_free(bp);
+  return id;
+}
 
 static void
 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
@@ -765,15 +794,16 @@ adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *
     {
       if (!fn[i])
        continue;
-      did = repodata_str2dir(data, dn[i], 1);
-      if (!did)
+      if (dn[i][0] != '/')
        {
           Solvable *s = data->repo->pool->solvables + handle;
           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
            did = repodata_str2dir(data, "/usr/src", 1);
          else
-           continue;   /* work around rpm bug */
+           did = repodata_str2dir_rooted(data, dn[i], 1);
        }
+      else
+        did = repodata_str2dir(data, dn[i], 1);
       repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
     }
   solv_free(fn);
@@ -803,7 +833,7 @@ addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
   unsigned int *di;
   int bnc, dnc, dic;
   int i;
-  Id lastdid = 0;
+  Id did;
   unsigned int lastdii = -1;
   int lastfiltered = 0;
 
@@ -833,17 +863,16 @@ addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
 
   adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
 
+  did = -1;
   for (i = 0; i < bnc; i++)
     {
-      Id did;
       char *b = bn[i];
 
-      if (lastdid && di[i] == lastdii)
-       did = lastdid;
-      else
+      if (did < 0 || di[i] != lastdii)
        {
          if (di[i] >= dnc)
            continue;   /* corrupt entry */
+         did = 0;
          lastdii = di[i];
          if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
            {
@@ -851,18 +880,17 @@ addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
              if (lastfiltered == 1)
                continue;
            }
-         did = repodata_str2dir(data, dn[lastdii], 1);
-         if (!did)
-           did = repodata_str2dir(data, "/", 1);
-         lastdid = did;
+         if (dn[lastdii][0] != '/')
+           did = repodata_str2dir_rooted(data, dn[lastdii], 1);
+         else
+           did = repodata_str2dir(data, dn[lastdii], 1);
        }
-      if (b && *b == '/')      /* work around rpm bug */
+      if (!b)
+       continue;
+      if (*b == '/')   /* work around rpm bug */
        b++;
-      if (lastfiltered)
-       {
-         if (lastfiltered != 2 || strcmp(b, "sendmail"))
-           continue;
-       }
+      if (lastfiltered && (lastfiltered != 2 || strcmp(b, "sendmail")))
+        continue;
       repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
     }
   solv_free(bn);
@@ -972,7 +1000,7 @@ rpmhead2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhe
     return 0;
   s->name = pool_str2id(pool, name, 1);
   sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
-  if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
+  if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
     s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
   else
     {
@@ -1158,7 +1186,7 @@ rpmdbid2db(unsigned char *db, Id id, int byteswapped)
 #endif
 }
 
-#ifdef FEDORA
+#if defined(FEDORA) || defined(MAGEIA)
 int
 serialize_dbenv_ops(struct rpmdbstate *state)
 {
@@ -1199,7 +1227,7 @@ opendbenv(struct rpmdbstate *state)
 
   if (db_env_create(&dbenv, 0))
     return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
-#if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
+#if (defined(FEDORA) || defined(MAGEIA)) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
   dbenv->set_thread_count(dbenv, 8);
 #endif
   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
@@ -1213,7 +1241,7 @@ opendbenv(struct rpmdbstate *state)
     }
   else
     {
-#ifdef FEDORA
+#if defined(FEDORA) || defined(MAGEIA)
       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)
@@ -1235,13 +1263,13 @@ opendbenv(struct rpmdbstate *state)
 static void
 closedbenv(struct rpmdbstate *state)
 {
-#ifdef FEDORA
+#if defined(FEDORA) || defined(MAGEIA)
   uint32_t eflags = 0;
 #endif
 
   if (!state->dbenv)
     return;
-#ifdef FEDORA
+#if defined(FEDORA) || defined(MAGEIA)
   (void)state->dbenv->get_open_flags(state->dbenv, &eflags);
   if (!(eflags & DB_PRIVATE))
     {
@@ -1449,7 +1477,6 @@ getrpm_dbdata(struct rpmdbstate *state, DBT *dbdata, int dbid)
       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
     }
   rpmhead = state->rpmhead;
-  rpmhead->forcebinary = 1;
   rpmhead->cnt = cnt;
   rpmhead->dcnt = dsize;
   memcpy(rpmhead->data, (unsigned char *)dbdata->data + 8, l);
@@ -1652,12 +1679,14 @@ solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, K
     case REPOKEY_TYPE_DIRNUMNUMARRAY:
       id = kv->id;
       id = copydir(pool, data, fromdata, id, cbdata->dircache);
-      repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
+      if (id)
+        repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
       break;
     case REPOKEY_TYPE_DIRSTRARRAY:
       id = kv->id;
       id = copydir(pool, data, fromdata, id, cbdata->dircache);
-      repodata_add_dirstr(data, handle, keyname, id, kv->str);
+      if (id)
+        repodata_add_dirstr(data, handle, keyname, id, kv->str);
       break;
     case REPOKEY_TYPE_FLEXARRAY:
       if (kv->eof == 2)
@@ -2118,7 +2147,6 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
   Id chksumtype = 0;
   Chksum *chksumh = 0;
   Chksum *leadsigchksumh = 0;
-  int forcebinary = 0;
 
   data = repo_add_repodata(repo, flags);
 
@@ -2148,7 +2176,6 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
       fclose(fp);
       return 0;
     }
-  forcebinary = lead[6] != 0 || lead[7] != 1;
   if (chksumh)
     solv_chksum_add(chksumh, lead, 96 + 16);
   if (leadsigchksumh)
@@ -2196,7 +2223,6 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
        solv_chksum_add(chksumh, rpmhead->data, sigdsize);
       if (leadsigchksumh)
        solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
-      rpmhead->forcebinary = 0;
       rpmhead->cnt = sigcnt;
       rpmhead->dcnt = sigdsize - sigcnt * 16;
       rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
@@ -2288,7 +2314,6 @@ repo_add_rpm(Repo *repo, const char *rpm, int flags)
   rpmhead->data[l] = 0;
   if (chksumh)
     solv_chksum_add(chksumh, rpmhead->data, l);
-  rpmhead->forcebinary = forcebinary;
   rpmhead->cnt = sigcnt;
   rpmhead->dcnt = sigdsize;
   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
@@ -2569,7 +2594,7 @@ rpm_query(void *rpmhandle, Id what)
       if (!name)
        name = "";
       sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
-      if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
+      if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
        arch = headstring(rpmhead, TAG_ARCH);
       else
        {
@@ -2661,14 +2686,12 @@ rpm_byfp(void *rpmstate, FILE *fp, const char *name)
   RpmHead *rpmhead;
   unsigned int sigdsize, sigcnt, l;
   unsigned char lead[4096];
-  int forcebinary = 0;
 
   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
     {
       pool_error(state->pool, 0, "%s: not a rpm", name);
       return 0;
     }
-  forcebinary = lead[6] != 0 || lead[7] != 1;
   if (lead[78] != 0 || lead[79] != 5)
     {
       pool_error(state->pool, 0, "%s: not a V5 header", name);
@@ -2730,7 +2753,6 @@ rpm_byfp(void *rpmstate, FILE *fp, const char *name)
       return 0;
     }
   rpmhead->data[l] = 0;
-  rpmhead->forcebinary = forcebinary;
   rpmhead->cnt = sigcnt;
   rpmhead->dcnt = sigdsize;
   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
@@ -2768,7 +2790,6 @@ rpm_byrpmh(void *rpmstate, Header h)
   memcpy(rpmhead->data, uh + 8, l);
   rpmhead->data[l] = 0;
   free((void *)uh);
-  rpmhead->forcebinary = 0;
   rpmhead->cnt = sigcnt;
   rpmhead->dcnt = sigdsize;
   rpmhead->dp = rpmhead->data + sigcnt * 16;