Imported Upstream version 0.7.18
[platform/upstream/libsolv.git] / src / repo_solv.c
index 0f967b9..761d06e 100644 (file)
@@ -7,9 +7,9 @@
 
 /*
  * repo_solv.c
- * 
+ *
  * Add a repo in solv format
- * 
+ *
  */
 
 
@@ -113,7 +113,7 @@ read_id(Repodata *data, Id max)
       if (!(c & 128))
        {
          x = (x << 7) | c;
-         if (max && x >= max)
+         if (max && x >= (unsigned int)max)
            {
              data->error = pool_error(data->repo->pool, SOLV_ERROR_ID_RANGE, "read_id: id too large (%u/%u)", x, max);
              return 0;
@@ -149,7 +149,7 @@ read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
          continue;
        }
       x = (x << 6) | (c & 63);
-      if (max && x >= max)
+      if (max && x >= (unsigned int)max)
        {
          data->error = pool_error(data->repo->pool, SOLV_ERROR_ID_RANGE, "read_idarray: id too large (%u/%u)", x, max);
          return 0;
@@ -217,13 +217,13 @@ data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, Repodata *da
          continue;
        }
       x = (x << 6) | (c & 63);
-      if (max && x >= max)
+      if (max && x >= (unsigned int)max)
        {
          data->error = pool_error(data->repo->pool, SOLV_ERROR_ID_RANGE, "data_read_idarray: id too large (%u/%u)", x, max);
          data->error = SOLV_ERROR_ID_RANGE;
          break;
        }
-      *store++ = x;
+      *store++ = map ? map[x] : x;
       if ((c & 64) == 0)
         break;
       x = 0;
@@ -261,7 +261,7 @@ data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, Repodata
        }
       x = old + (x - 1);
       old = x;
-      if (max && x >= max)
+      if (max && x >= (unsigned int)max)
        {
          data->error = pool_error(data->repo->pool, SOLV_ERROR_ID_RANGE, "data_read_rel_idarray: id too large (%u/%u)", x, max);
          break;
@@ -360,7 +360,7 @@ incore_add_ideof(Repodata *data, Id sx, int eof)
 static void
 incore_add_blob(Repodata *data, unsigned char *buf, int len)
 {
-  if (data->incoredatafree < len)
+  if (data->incoredatafree < (unsigned int)len)
     {
       data->incoredata = solv_realloc(data->incoredata, data->incoredatalen + INCORE_ADD_CHUNK + len);
       data->incoredatafree = INCORE_ADD_CHUNK + len;
@@ -444,8 +444,8 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
 {
   Pool *pool = repo->pool;
   int i, l;
-  unsigned int numid, numrel, numdir, numsolv;
-  unsigned int numkeys, numschemata;
+  int numid, numrel, numdir, numsolv;
+  int numkeys, numschemata;
 
   Offset sizeid;
   Offset *str;                        /* map Id -> Offset into string space */
@@ -453,9 +453,8 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
   char *sp;                           /* pointer into string space */
   Id *idmap;                          /* map of repo Ids to pool Ids */
   Id id, type;
-  unsigned int hashmask, h;
-  int hh;
-  Id *hashtbl;
+  Hashval hashmask, h, hh;
+  Hashtable hashtbl;
   Id name, evr, did;
   int relflags;
   Reldep *ran;
@@ -479,7 +478,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
   int oldnstrings = pool->ss.nstrings;
   int oldnrels = pool->nrels;
 
-  struct _Stringpool *spool;
+  struct s_Stringpool *spool;
 
   Repodata *parent = 0;
   Repodata data;
@@ -503,7 +502,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
       extendstart = repo->start;
       extendend = repo->end;
     }
-    
+
   memset(&data, 0, sizeof(data));
   data.repo = repo;
   data.fp = fp;
@@ -520,16 +519,26 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
         return pool_error(pool, SOLV_ERROR_UNSUPPORTED, "unsupported SOLV version");
     }
 
-  numid = read_u32(&data);
-  numrel = read_u32(&data);
-  numdir = read_u32(&data);
-  numsolv = read_u32(&data);
-  numkeys = read_u32(&data);
-  numschemata = read_u32(&data);
+  numid = (int)read_u32(&data);
+  numrel = (int)read_u32(&data);
+  numdir = (int)read_u32(&data);
+  numsolv = (int)read_u32(&data);
+  numkeys = (int)read_u32(&data);
+  numschemata = (int)read_u32(&data);
   solvflags = read_u32(&data);
 
-  if (numdir && numdir < 2)
+  if (numid < 0 || numid >= 0x20000000)
+    return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of ids");
+  if (numrel < 0 || numrel >= 0x20000000)
+    return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of rels");
+  if (numdir && (numdir < 2 || numdir >= 0x20000000))
     return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of dirs");
+  if (numsolv < 0 || numsolv >= 0x20000000)
+    return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of solvables");
+  if (numkeys < 0 || numkeys >= 0x20000000)
+    return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of keys");
+  if (numschemata < 0 || numschemata >= 0x20000000)
+    return pool_error(pool, SOLV_ERROR_CORRUPT, "bad number of schematas");
 
   if (numrel && (flags & REPO_LOCALPOOL) != 0)
     return pool_error(pool, SOLV_ERROR_CORRUPT, "relations are forbidden in a local pool");
@@ -549,10 +558,10 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
 
   /*
    * read strings and Ids
-   * 
+   *
    */
 
-  
+
   /*
    * alloc buffers
    */
@@ -562,7 +571,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
       spool = &pool->ss;
       /* alloc max needed string buffer and string pointers, will shrink again later */
 #if 0
-      spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1); 
+      spool->stringspace = solv_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
       spool->strings = solv_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
 #else
       spool->sstrings += sizeid + 1;
@@ -576,9 +585,8 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
     {
       data.localpool = 1;
       spool = &data.spool;
-      spool->sstrings = 7 + sizeid + 1;
-      spool->nstrings = numid < 2 ? 2 : numid;
-      stringpool_shrink(spool);                /* we misuse stringpool_shrink to alloc the stringpool in the correct size */
+      spool->stringspace = solv_malloc(7 + sizeid + 1);
+      spool->strings = solv_malloc2(numid < 2 ?  2 : numid, sizeof(Offset));
       strcpy(spool->stringspace, "<NULL>");
       spool->sstrings = 7;
       spool->nstrings = 1;
@@ -589,26 +597,30 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
   /*
    * read string data and append to old string space
    */
-  
+
   strsp = spool->stringspace + spool->sstrings;        /* append new entries */
   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
     {
       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
-        return pool_error(pool, SOLV_ERROR_EOF, "read error while reading strings");
+       {
+         repodata_freedata(&data);
+         return pool_error(pool, SOLV_ERROR_EOF, "read error while reading strings");
+       }
     }
   else
     {
       unsigned int pfsize = read_u32(&data);
       char *prefix = solv_malloc(pfsize);
       char *pp = prefix;
-      char *old_str = 0;
+      char *old_str = strsp;
       char *dest = strsp;
       int freesp = sizeid;
 
       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
-        {
+       {
          solv_free(prefix);
-          return pool_error(pool, SOLV_ERROR_EOF, "read error while reading strings");
+         repodata_freedata(&data);
+         return pool_error(pool, SOLV_ERROR_EOF, "read error while reading strings");
        }
       for (i = 1; i < numid; i++)
         {
@@ -618,6 +630,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
          if (freesp < 0)
            {
              solv_free(prefix);
+             repodata_freedata(&data);
              return pool_error(pool, SOLV_ERROR_OVERFLOW, "overflow while expanding strings");
            }
          if (same)
@@ -629,7 +642,10 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
        }
       solv_free(prefix);
       if (freesp != 0)
-       return pool_error(pool, SOLV_ERROR_CORRUPT, "expanding strings size mismatch");
+       {
+         repodata_freedata(&data);
+         return pool_error(pool, SOLV_ERROR_CORRUPT, "expanding strings size mismatch");
+       }
     }
   strsp[sizeid] = 0;                  /* make string space \0 terminated */
   sp = strsp;
@@ -644,12 +660,16 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
       if (*sp)
        {
          /* we need id 1 to be '' for directories */
+         repodata_freedata(&data);
          return pool_error(pool, SOLV_ERROR_CORRUPT, "store strings don't start with an empty string");
        }
       for (i = 1; i < spool->nstrings; i++)
        {
          if (sp >= strsp + sizeid && numid >= 2)
-           return pool_error(pool, SOLV_ERROR_OVERFLOW, "not enough strings");
+           {
+             repodata_freedata(&data);
+             return pool_error(pool, SOLV_ERROR_OVERFLOW, "not enough strings");
+           }
          str[i] = sp - spool->stringspace;
          sp += strlen(sp) + 1;
        }
@@ -657,46 +677,32 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
     }
   else
     {
+      Offset oldsstrings = spool->sstrings;
+
       /* alloc id map for name and rel Ids. this maps ids in the solv files
        * to the ids in our pool */
       idmap = solv_calloc(numid + numrel, sizeof(Id));
-
-      /* grow hash if needed, otherwise reuse */
-      hashmask = mkmask(spool->nstrings + numid);
+      stringpool_resize_hash(spool, numid);
+      hashtbl = spool->stringhashtbl;
+      hashmask = spool->stringhashmask;
 #if 0
       POOL_DEBUG(SOLV_DEBUG_STATS, "read %d strings\n", numid);
-      POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d, old %d\n", hashmask + 1, spool->stringhashmask + 1);
+      POOL_DEBUG(SOLV_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
 #endif
-      if (hashmask > spool->stringhashmask)
-       {
-         spool->stringhashtbl = solv_free(spool->stringhashtbl);
-         spool->stringhashmask = hashmask;
-          spool->stringhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
-         for (i = 1; i < spool->nstrings; i++)
-           {
-             h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
-             hh = HASHCHAIN_START;
-             while (hashtbl[h])
-               h = HASHCHAIN_NEXT(h, hh, hashmask);
-             hashtbl[h] = i;
-           }
-       }
-      else
-       {
-         hashtbl = spool->stringhashtbl;
-         hashmask = spool->stringhashmask;
-       }
-
       /*
        * run over strings and merge with pool.
+       * we could use stringpool_str2id, but this is faster.
        * also populate id map (maps solv Id -> pool Id)
        */
       for (i = 1; i < numid; i++)
        {
          if (sp >= strsp + sizeid)
            {
-             solv_free(hashtbl);
              solv_free(idmap);
+             spool->nstrings = oldnstrings;
+             spool->sstrings = oldsstrings;
+             stringpool_freehash(spool);
+             repodata_freedata(&data);
              return pool_error(pool, SOLV_ERROR_OVERFLOW, "not enough strings %d %d", i, numid);
            }
          if (!*sp)                            /* empty string */
@@ -733,53 +739,30 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
          idmap[i] = id;       /* repo relative -> pool relative */
          sp += l;             /* next string */
        }
-      if (hashmask > mkmask(spool->nstrings + 8192))
-       {
-         spool->stringhashtbl = solv_free(spool->stringhashtbl);
-         spool->stringhashmask = 0;
-       }
       stringpool_shrink(spool);                /* vacuum */
     }
 
-  
+
   /*******  Part 2: Relation IDs  ***************************************/
 
   /*
    * read RelDeps
-   * 
+   *
    */
-  
+
   if (numrel)
     {
       /* extend rels */
       pool->rels = solv_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
       ran = pool->rels;
 
-      /* grow hash if needed, otherwise reuse */
-      hashmask = mkmask(pool->nrels + numrel);
+      pool_resize_rels_hash(pool, numrel);
+      hashtbl = pool->relhashtbl;
+      hashmask = pool->relhashmask;
 #if 0
       POOL_DEBUG(SOLV_DEBUG_STATS, "read %d rels\n", numrel);
-      POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d, old %d\n", hashmask + 1, pool->relhashmask + 1);
+      POOL_DEBUG(SOLV_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
 #endif
-      if (hashmask > pool->relhashmask)
-       {
-         pool->relhashtbl = solv_free(pool->relhashtbl);
-         pool->relhashmask = hashmask;
-          pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
-         for (i = 1; i < pool->nrels; i++)
-           {
-             h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
-             hh = HASHCHAIN_START;
-             while (hashtbl[h])
-               h = HASHCHAIN_NEXT(h, hh, hashmask);
-             hashtbl[h] = i;
-           }
-       }
-      else
-       {
-         hashtbl = pool->relhashtbl;
-         hashmask = pool->relhashmask;
-       }
 
       /*
        * read RelDeps from repo
@@ -812,11 +795,6 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
            }
          idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
        }
-      if (hashmask > mkmask(pool->nrels + 4096))
-       {
-         pool->relhashtbl = solv_free(pool->relhashtbl);
-         pool->relhashmask = 0;
-       }
       pool_shrink_rels(pool);          /* vacuum */
     }
 
@@ -848,11 +826,20 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
        {
          id = read_id(&data, i + numid);
          if (id >= numid)
-           data.dirpool.dirs[i] = -(id - numid);
-         else if (idmap)
-           data.dirpool.dirs[i] = idmap[id];
-         else
-           data.dirpool.dirs[i] = id;
+           {
+             data.dirpool.dirs[i++] = -(id - numid);
+             if (i >= numdir)
+               {
+                 data.error = pool_error(pool, SOLV_ERROR_CORRUPT, "last dir entry is not a component");
+                 break;
+               }
+             id = read_id(&data, numid);
+           }
+         if (idmap)
+           id = idmap[id];
+         data.dirpool.dirs[i] = id;
+         if (id <= 0)
+            data.error = pool_error(pool, SOLV_ERROR_CORRUPT, "bad dir component");
        }
     }
 
@@ -872,7 +859,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
        type = idmap[type];
       else if ((flags & REPO_LOCALPOOL) != 0)
         type = pool_str2id(pool, stringpool_id2str(spool, type), 1);
-      if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_FLEXARRAY)
+      if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DELETED)
        {
          data.error = pool_error(pool, SOLV_ERROR_UNSUPPORTED, "unsupported data type '%s'", pool_id2str(pool, type));
          type = REPOKEY_TYPE_VOID;
@@ -892,13 +879,15 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
            data.error = pool_error(pool, SOLV_ERROR_UNSUPPORTED, "main solvable data must use incore storage %d", keys[i].storage);
          keys[i].storage = KEY_STORAGE_SOLVABLE;
        }
+      if ((type == REPOKEY_TYPE_FIXARRAY || type == REPOKEY_TYPE_FLEXARRAY) && keys[i].storage != KEY_STORAGE_INCORE)
+       data.error = pool_error(pool, SOLV_ERROR_UNSUPPORTED, "flex/fixarrays must use incore storage\n");
       /* cannot handle rel idarrays in incore/vertical */
       if (type == REPOKEY_TYPE_REL_IDARRAY && keys[i].storage != KEY_STORAGE_SOLVABLE)
        data.error = pool_error(pool, SOLV_ERROR_UNSUPPORTED, "type REL_IDARRAY is only supported for STORAGE_SOLVABLE");
       /* cannot handle mapped ids in vertical */
       if (!(flags & REPO_LOCALPOOL) && keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && (type == REPOKEY_TYPE_ID || type == REPOKEY_TYPE_IDARRAY))
        data.error = pool_error(pool, SOLV_ERROR_UNSUPPORTED, "mapped ids are not supported for STORAGE_VERTICAL_OFFSET");
+
       if (keys[i].type == REPOKEY_TYPE_CONSTANTID && idmap)
        keys[i].size = idmap[keys[i].size];
 #if 0
@@ -921,7 +910,7 @@ repo_add_solv(Repo *repo, FILE *fp, int flags)
     }
 
   /*******  Part 5: Schemata ********************************************/
-  
+
   id = read_id(&data, 0);
   schemadata = solv_calloc(id + 1, sizeof(Id));
   schemadatap = schemadata + 1;
@@ -1086,13 +1075,13 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
        case REPOKEY_TYPE_ID:
          dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data);
          if (s && id == SOLVABLE_NAME)
-           s->name = did; 
+           s->name = did;
          else if (s && id == SOLVABLE_ARCH)
-           s->arch = did; 
+           s->arch = did;
          else if (s && id == SOLVABLE_EVR)
-           s->evr = did; 
+           s->evr = did;
          else if (s && id == SOLVABLE_VENDOR)
-           s->vendor = did; 
+           s->vendor = did;
          else if (keys[key].storage == KEY_STORAGE_INCORE)
            incore_add_id(&data, did);
 #if 0
@@ -1229,12 +1218,9 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
            }
          /* FALLTHROUGH */
        default:
-         if (id == RPM_RPMDBID && s && (keys[key].type == REPOKEY_TYPE_U32 || keys[key].type == REPOKEY_TYPE_NUM))
+         if (id == RPM_RPMDBID && s && keys[key].type == REPOKEY_TYPE_NUM)
            {
-             if (keys[key].type == REPOKEY_TYPE_U32)
-               dp = data_read_u32(dp, (unsigned int *)&id);
-             else
-               dp = data_read_id_max(dp, &id, 0, 0, &data);
+             dp = data_read_id(dp, &id);
              if (!repo->rpmdbid)
                repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
              repo->rpmdbid[(s - pool->solvables) - repo->start] = id;
@@ -1275,15 +1261,21 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
       data.incoredata = solv_realloc(data.incoredata, data.incoredatalen);
       data.incoredatafree = 0;
     }
+  solv_free(idmap);
+
+  /* fixup the special idarray type */
+  for (i = 1; i < numkeys; i++)
+    if (keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
+      keys[i].type = REPOKEY_TYPE_IDARRAY;
 
   for (i = 1; i < numkeys; i++)
-    if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
+    if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET && keys[i].size)
       break;
   if (i < numkeys && !data.error)
     {
       Id fileoffset = 0;
       unsigned int pagesize;
-      
+
       /* we have vertical data, make it available */
       data.verticaloffset = solv_calloc(numkeys, sizeof(Id));
       for (i = 1; i < numkeys; i++)
@@ -1294,23 +1286,22 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
          }
       data.lastverticaloffset = fileoffset;
       pagesize = read_u32(&data);
-      data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
-      if (data.error == SOLV_ERROR_EOF)
-        pool_error(pool, data.error, "repopagestore setup: unexpected EOF");
-      else if (data.error)
-        pool_error(pool, data.error, "repopagestore setup failed");
-    }
-  else
-    {
-      /* no longer needed */
-      data.fp = 0;
+      if (!data.error)
+       {
+         data.error = repopagestore_read_or_setup_pages(&data.store, data.fp, pagesize, fileoffset);
+         if (data.error == SOLV_ERROR_EOF)
+           pool_error(pool, data.error, "repopagestore setup: unexpected EOF");
+         else if (data.error)
+           pool_error(pool, data.error, "repopagestore setup failed");
+       }
     }
-  solv_free(idmap);
+  data.fp = 0; /* no longer needed */
 
   if (data.error)
     {
-      /* XXX: free repodata? */
-      return data.error;
+      i = data.error;
+      repodata_freedata(&data);
+      return i;
     }
 
   if (parent)
@@ -1318,6 +1309,7 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
       /* overwrite stub repodata */
       repodata_freedata(parent);
       data.repodataid = parent->repodataid;
+      data.loadcallback = parent->loadcallback;
       *parent = data;
     }
   else
@@ -1334,6 +1326,17 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
       repo->repodata[repo->nrepodata++] = data;
     }
 
+  if ((flags & REPO_EXTEND_SOLVABLES) != 0)
+    {
+      if (repodata_has_keyname(&data, SOLVABLE_FILELIST))
+       repodata_set_filelisttype(repo->repodata + data.repodataid, REPODATA_FILELIST_EXTENSION);
+    }
+  else
+    {
+      if (repodata_lookup_type(&data, SOLVID_META, REPOSITORY_FILTEREDFILELIST))
+        repodata_set_filelisttype(repo->repodata + data.repodataid, REPODATA_FILELIST_FILTERED);
+    }
+
   /* create stub repodata entries for all external */
   if (!(flags & SOLV_ADD_NO_STUBS) && !parent)
     {
@@ -1341,7 +1344,7 @@ printf("=> %s %s %p\n", pool_id2str(pool, keys[key].name), pool_id2str(pool, key
        if (data.keys[key].name == REPOSITORY_EXTERNAL && data.keys[key].type == REPOKEY_TYPE_FLEXARRAY)
          break;
       if (key < data.nkeys)
-       repodata_create_stubs(repo->repodata + (repo->nrepodata - 1));
+       repodata_create_stubs(repo->repodata + data.repodataid);
     }
 
   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_solv took %d ms\n", solv_timems(now));