- fix memory leak
[platform/upstream/libsolv.git] / ext / repo_rpmdb.c
index 117e850..8947f9a 100644 (file)
@@ -2100,10 +2100,8 @@ struct rpmdbentry {
 #define ENTRIES_BLOCK 255
 #define NAMEDATA_BLOCK 1023
 
-#define FLAGS_GET_PUBKEYS 1
-
 static struct rpmdbentry *
-getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap, int flags)
+getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *match, int *nentriesp, char **namedatap)
 {
   DB_ENV *dbenv = 0;
   DB *db = 0;
@@ -2156,19 +2154,8 @@ getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *
     }
   while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
     {
-      if (!match)
-       {
-         if ((flags & FLAGS_GET_PUBKEYS))
-           {
-             if (dbkey.size != 10 || memcmp(dbkey.data, "gpg-pubkey", 10))
-               continue;
-           }
-         else
-           {
-             if (dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
-               continue;
-           }
-       }
+      if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
+       continue;
       dl = dbdata.size;
       dp = dbdata.data;
       while(dl >= 8)
@@ -2203,6 +2190,19 @@ getinstalledrpmdbids(struct rpm_by_state *state, const char *index, const char *
   return entries;
 }
 
+static void
+freestate(struct rpm_by_state *state)
+{
+  /* close down */
+  if (!state)
+    return;
+  if (state->db)
+    state->db->close(state->db, 0);
+  if (state->dbenv)
+    state->dbenv->close(state->dbenv, 0);
+  sat_free(state->rpmhead);
+}
+
 int
 rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match, Queue *rpmdbidq)
 {
@@ -2218,13 +2218,13 @@ rpm_installedrpmdbids(const char *rootdir, const char *index, const char *match,
   memset(&state, 0, sizeof(state));
   if (!(state.dbenv = opendbenv(rootdir)))
     return 0;
-  entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata, 0);
+  entries = getinstalledrpmdbids(&state, index, match, &nentries, &namedata);
   if (rpmdbidq)
     for (i = 0; i < nentries; i++)
       queue_push(rpmdbidq, entries[i].rpmdbid);
   sat_free(entries);
   sat_free(namedata);
-  rpm_byrpmdbid(0, 0, (void **)&state);
+  freestate(&state);
   return nentries;
 }
 
@@ -2240,13 +2240,7 @@ rpm_byrpmdbid(Id rpmdbid, const char *rootdir, void **statep)
   if (!rpmdbid)
     {
       /* close down */
-      if (!state)
-       return 0;
-      if (state->db)
-       state->db->close(state->db, 0);
-      if (state->dbenv)
-        state->dbenv->close(state->dbenv, 0);
-      sat_free(state->rpmhead);
+      freestate(state);
       sat_free(state);
       *statep = (void *)0;
       return 0;
@@ -2964,10 +2958,11 @@ repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
   memset(&state, 0, sizeof(state));
   if (!(state.dbenv = opendbenv(rootdir)))
     return;
-  entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata, FLAGS_GET_PUBKEYS);
+  entries = getinstalledrpmdbids(&state, "Name", "gpg-pubkey", &nentries, &namedata);
   for (i = 0 ; i < nentries; i++)
     {
-      RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, (void **)&state);
+      void *statep = &state;
+      RpmHead *rpmhead = rpm_byrpmdbid(entries[i].rpmdbid, rootdir, &statep);
       if (!rpmhead)
        continue;
       str = headstring(rpmhead, TAG_DESCRIPTION);
@@ -2982,7 +2977,7 @@ repo_add_rpmdb_pubkeys(Repo *repo, const char *rootdir, int flags)
        repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
       repo->rpmdbid[s - pool->solvables - repo->start] = entries[i].rpmdbid;
     }
-  rpm_byrpmdbid(0, 0, (void **)&state);
+  freestate(&state);
   if (!(flags & REPO_NO_INTERNALIZE))
     repodata_internalize(data);
 }