- beautify add_array code
authorMichael Schroeder <mls@suse.de>
Wed, 19 Mar 2008 21:19:34 +0000 (21:19 +0000)
committerMichael Schroeder <mls@suse.de>
Wed, 19 Mar 2008 21:19:34 +0000 (21:19 +0000)
- fix bugs and a memory leak in addfileprovides

src/pool.c
src/repo_solv.c
src/repodata.c

index 5caa887..a92e980 100644 (file)
@@ -599,34 +599,35 @@ pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct sea
 {
   Id dep, sid;
   const char *s, *sr;
+  struct searchfiles *csf;
 
   while ((dep = *ida++) != 0)
     {
+      csf = sf;
       while (ISRELDEP(dep))
        {
          Reldep *rd;
          sid = pool->ss.nstrings + GETRELID(dep);
-         if (MAPTST(&sf->seen, sid))
+         if (MAPTST(&csf->seen, sid))
            {
              dep = 0;
              break;
            }
-         MAPSET(&sf->seen, sid);
+         MAPSET(&csf->seen, sid);
          rd = GETRELDEP(pool, dep);
          if (rd->flags < 8)
            dep = rd->name;
          else if (rd->flags == REL_NAMESPACE)
            {
-             if (isf && (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES))
+             if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
                {
-                 sf = isf;
-                 isf = 0;
-                 if (MAPTST(&sf->seen, sid))
+                 csf = isf;
+                 if (!csf || MAPTST(&csf->seen, sid))
                    {
                      dep = 0;
                      break;
                    }
-                 MAPSET(&sf->seen, sid);
+                 MAPSET(&csf->seen, sid);
                }
              dep = rd->evr;
            }
@@ -635,29 +636,29 @@ pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct sea
              Id ids[2];
              ids[0] = rd->name;
              ids[1] = 0;
-             pool_addfileprovides_dep(pool, ids, sf, isf);
+             pool_addfileprovides_dep(pool, ids, csf, isf);
              dep = rd->evr;
            }
        }
       if (!dep)
        continue;
-      if (MAPTST(&sf->seen, dep))
+      if (MAPTST(&csf->seen, dep))
        continue;
-      MAPSET(&sf->seen, dep);
+      MAPSET(&csf->seen, dep);
       s = id2str(pool, dep);
       if (*s != '/')
        continue;
-      sf->ids = sat_extend(sf->ids, sf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
-      sf->dirs = sat_extend(sf->dirs, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
-      sf->names = sat_extend(sf->names, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
-      sf->ids[sf->nfiles] = dep;
+      csf->ids = sat_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
+      csf->dirs = sat_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
+      csf->names = sat_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
+      csf->ids[csf->nfiles] = dep;
       sr = strrchr(s, '/');
-      sf->names[sf->nfiles] = strdup(sr + 1);
-      sf->dirs[sf->nfiles] = sat_malloc(sr - s + 1);
+      csf->names[csf->nfiles] = strdup(sr + 1);
+      csf->dirs[csf->nfiles] = sat_malloc(sr - s + 1);
       if (sr != s)
-        strncpy(sf->dirs[sf->nfiles], s, sr - s);
-      sf->dirs[sf->nfiles][sr - s] = 0;
-      sf->nfiles++;
+        strncpy(csf->dirs[csf->nfiles], s, sr - s);
+      csf->dirs[csf->nfiles][sr - s] = 0;
+      csf->nfiles++;
     }
 }
 
@@ -783,7 +784,7 @@ pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
 {
   Solvable *s;
   Repo *repo;
-  struct searchfiles sf, isf;
+  struct searchfiles sf, isf, *isfp;
   struct addfileprovides_cbdata cbd;
   int i;
 
@@ -792,27 +793,28 @@ pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
   memset(&isf, 0, sizeof(isf));
   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
 
+  isfp = installed ? &isf : 0;
   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
     {
       repo = s->repo;
       if (!repo)
        continue;
       if (s->obsoletes)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
       if (s->conflicts)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
       if (s->requires)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
       if (s->recommends)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
       if (s->suggests)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
       if (s->supplements)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
       if (s->enhances)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
       if (s->freshens)
-        pool_addfileprovides_dep(pool, repo->idarraydata + s->freshens, &sf, &isf);
+        pool_addfileprovides_dep(pool, repo->idarraydata + s->freshens, &sf, isfp);
     }
   map_free(&sf.seen);
   map_free(&isf.seen);
@@ -845,13 +847,14 @@ pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
       sat_free(sf.dirs);
       sat_free(sf.names);
     }
-  if (isf.nfiles && installed)
+  if (isf.nfiles)
     {
 #if 0
       for (i = 0; i < isf.nfiles; i++)
        POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", id2str(pool, isf.ids[i]));
 #endif
-      pool_addfileprovides_search(pool, &cbd, &isf, installed);
+      if (installed)
+        pool_addfileprovides_search(pool, &cbd, &isf, installed);
       sat_free(isf.ids);
       for (i = 0; i < isf.nfiles; i++)
        {
index 8dfcc76..11767e8 100644 (file)
@@ -1347,9 +1347,11 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
                {
                  dps = dp;
                  dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
-                 if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
+                 if (keys[key].storage != KEY_STORAGE_INCORE)
+                   break;
+                 if (idmap)
                    incore_map_idarray(&data, dps, idmap, numid);
-                 else if (keys[key].storage == KEY_STORAGE_INCORE)
+                 else
                    incore_add_blob(&data, dps, dp - dps);
                  break;
                }
index 42db72b..8ab53fa 100644 (file)
@@ -1040,52 +1040,56 @@ repodata_set_str(Repodata *data, Id entry, Id keyname, const char *str)
   data->attrdatalen += l;
 }
 
+static void
+repoadata_add_array(Repodata *data, Id entry, Id keyname, Id keytype, int entrysize)
+{
+  int oldsize;
+  Id *ida, *pp;
+
+  pp = 0;
+  if (data->attrs && data->attrs[entry])
+    for (pp = data->attrs[entry]; *pp; pp += 2)
+      if (data->keys[*pp].name == keyname && data->keys[*pp].type == keytype)
+        break;
+  if (!pp || !*pp)
+    {
+      /* not found. allocate new key */
+      Repokey key;
+      key.name = keyname;
+      key.type = keytype;
+      key.size = 0;
+      key.storage = KEY_STORAGE_INCORE;
+      data->attriddata = sat_extend(data->attriddata, data->attriddatalen, entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
+      repodata_set(data, entry, &key, data->attriddatalen);
+      return;
+    }
+  oldsize = 0;
+  for (ida = data->attriddata + pp[1]; *ida; ida += entrysize)
+    oldsize += entrysize;
+  if (ida + 1 == data->attriddata + data->attriddatalen)
+    {
+      /* this was the last entry, just append it */
+      data->attriddata = sat_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
+      data->attriddatalen--;   /* overwrite terminating 0  */
+    }
+  else
+    {
+      /* too bad. move to back. */
+      data->attriddata = sat_extend(data->attriddata, data->attriddatalen,  oldsize + entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
+      memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
+      pp[1] = data->attriddatalen;
+      data->attriddatalen += oldsize;
+    }
+}
+
 void
 repodata_add_dirnumnum(Repodata *data, Id entry, Id keyname, Id dir, Id num, Id num2)
 {
-  Id *ida, *pp;
-  Repokey key;
 
 #if 0
 fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", entry, dir, num, num2, data->attriddatalen);
 #endif
-  if (data->attrs && data->attrs[entry])
-    {
-      for (pp = data->attrs[entry]; *pp; pp += 2)
-        if (data->keys[*pp].name == keyname && data->keys[*pp].type == REPOKEY_TYPE_DIRNUMNUMARRAY)
-         break;
-      if (*pp)
-       {
-         int oldsize = 0;
-         for (ida = data->attriddata + pp[1]; *ida; ida += 3)
-           oldsize += 3;
-         if (ida + 1 == data->attriddata + data->attriddatalen)
-           {
-             /* this was the last entry, just append it */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 3, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             data->attriddatalen--;    /* overwrite terminating 0  */
-           }
-         else
-           {
-             /* too bad. move to back. */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen,  oldsize + 4, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
-             pp[1] = data->attriddatalen;
-             data->attriddatalen += oldsize;
-           }
-         data->attriddata[data->attriddatalen++] = dir;
-         data->attriddata[data->attriddatalen++] = num;
-         data->attriddata[data->attriddatalen++] = num2;
-         data->attriddata[data->attriddatalen++] = 0;
-         return;
-       }
-    }
-  key.name = keyname;
-  key.type = REPOKEY_TYPE_DIRNUMNUMARRAY;
-  key.size = 0;
-  key.storage = KEY_STORAGE_INCORE;
-  data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 4, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-  repodata_set(data, entry, &key, data->attriddatalen);
+  repoadata_add_array(data, entry, keyname, REPOKEY_TYPE_DIRNUMNUMARRAY, 3);
   data->attriddata[data->attriddatalen++] = dir;
   data->attriddata[data->attriddatalen++] = num;
   data->attriddata[data->attriddatalen++] = num2;
@@ -1095,8 +1099,7 @@ fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", entry, dir, num, nu
 void
 repodata_add_dirstr(Repodata *data, Id entry, Id keyname, Id dir, const char *str)
 {
-  Id *ida, *pp, stroff;
-  Repokey key;
+  Id stroff;
   int l;
 
   l = strlen(str) + 1;
@@ -1108,42 +1111,7 @@ repodata_add_dirstr(Repodata *data, Id entry, Id keyname, Id dir, const char *st
 #if 0
 fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", entry, dir, str,  data->attriddatalen);
 #endif
-  if (data->attrs && data->attrs[entry])
-    {
-      for (pp = data->attrs[entry]; *pp; pp += 2)
-        if (data->keys[*pp].name == keyname && data->keys[*pp].type == REPOKEY_TYPE_DIRSTRARRAY)
-         break;
-      if (*pp)
-       {
-         int oldsize = 0;
-         for (ida = data->attriddata + pp[1]; *ida; ida += 2)
-           oldsize += 2;
-         if (ida + 1 == data->attriddata + data->attriddatalen)
-           {
-             /* this was the last entry, just append it */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 2, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             data->attriddatalen--;    /* overwrite terminating 0  */
-           }
-         else
-           {
-             /* too bad. move to back. */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen, oldsize + 3, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
-             pp[1] = data->attriddatalen;
-             data->attriddatalen += oldsize;
-           }
-         data->attriddata[data->attriddatalen++] = dir;
-         data->attriddata[data->attriddatalen++] = stroff;
-         data->attriddata[data->attriddatalen++] = 0;
-         return;
-       }
-    }
-  key.name = keyname;
-  key.type = REPOKEY_TYPE_DIRSTRARRAY;
-  key.size = 0;
-  key.storage = KEY_STORAGE_INCORE;
-  data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 3, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-  repodata_set(data, entry, &key, data->attriddatalen);
+  repoadata_add_array(data, entry, keyname, REPOKEY_TYPE_DIRSTRARRAY, 2);
   data->attriddata[data->attriddatalen++] = dir;
   data->attriddata[data->attriddatalen++] = stroff;
   data->attriddata[data->attriddatalen++] = 0;
@@ -1152,48 +1120,10 @@ fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", entry, dir, str,  data->a
 void
 repodata_add_idarray(Repodata *data, Id entry, Id keyname, Id id)
 {
-  Id *ida, *pp;
-  Repokey key;
-
 #if 0
 fprintf(stderr, "repodata_add_idarray %d %d (%d)\n", entry, id, data->attriddatalen);
 #endif
-  if (data->attrs && data->attrs[entry])
-    {
-      for (pp = data->attrs[entry]; *pp; pp += 2)
-        if (data->keys[*pp].name == keyname
-           && data->keys[*pp].type == REPOKEY_TYPE_IDARRAY)
-         break;
-      if (*pp)
-       {
-         int oldsize = 0;
-         for (ida = data->attriddata + pp[1]; *ida; ida++)
-           oldsize++;
-         if (ida + 1 == data->attriddata + data->attriddatalen)
-           {
-             /* this was the last entry, just append it */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             data->attriddatalen--;    /* overwrite terminating 0  */
-           }
-         else
-           {
-             /* too bad. move to back. */
-             data->attriddata = sat_extend(data->attriddata, data->attriddatalen,  oldsize + 2, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-             memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
-             pp[1] = data->attriddatalen;
-             data->attriddatalen += oldsize;
-           }
-         data->attriddata[data->attriddatalen++] = id;
-         data->attriddata[data->attriddatalen++] = 0;
-         return;
-       }
-    }
-  key.name = keyname;
-  key.type = REPOKEY_TYPE_IDARRAY;
-  key.size = 0;
-  key.storage = KEY_STORAGE_INCORE;
-  data->attriddata = sat_extend(data->attriddata, data->attriddatalen, 2, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
-  repodata_set(data, entry, &key, data->attriddatalen);
+  repoadata_add_array(data, entry, keyname, REPOKEY_TYPE_IDARRAY, 1);
   data->attriddata[data->attriddatalen++] = id;
   data->attriddata[data->attriddatalen++] = 0;
 }