Add a helper function to convert cursor retrievals to dbiIndexSets
authorPanu Matilainen <pmatilai@redhat.com>
Wed, 8 Jun 2011 09:52:12 +0000 (12:52 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Wed, 8 Jun 2011 09:52:12 +0000 (12:52 +0300)
- This stuff is repeated in rpmdb.c all over the place, to some
  this suggests making it into a function:
  - Hide the DBTs inside the function
  - Handle keylen fixups, set conversions and error logging centrally

lib/rpmdb.c

index 6f68f3c..eb983d1 100644 (file)
@@ -445,6 +445,39 @@ static dbiIndexSet dbiIndexSetFree(dbiIndexSet set)
     return NULL;
 }
 
+static int dbiGetToSet(dbiIndex dbi, const char *keyp, size_t keylen,
+                      dbiIndexSet *set)
+{
+    int rc = EINVAL;
+    dbiCursor dbc = dbiCursorInit(dbi, 0);
+    if (dbc != NULL) {
+       DBT data, key;
+       memset(&data, 0, sizeof(data));
+       memset(&key, 0, sizeof(key));
+
+       if (keyp && keylen == 0) {
+           keylen = strlen(keyp);
+           if (keylen == 0)
+               keylen++; /* XXX "/" fixup */
+       }
+
+       key.data = (void *) keyp; /* discards const */
+       key.size = keylen;
+
+       rc = dbiCursorGet(dbc, &key, &data, DB_SET);
+
+       if (rc == 0) {
+           dbt2set(dbi, &data, set);
+       } else if (rc != DB_NOTFOUND) {
+           rpmlog(RPMLOG_ERR,
+                  _("error(%d) getting \"%s\" records from %s index: %s\n"),
+                  rc, keyp ? keyp : "???", dbiName(dbi), db_strerror(rc));
+       }
+       dbiCursorFree(dbc);
+    }
+    return rc;
+}
+
 typedef struct miRE_s {
     rpmTagVal          tag;            /*!< header tag */
     rpmMireMode                mode;           /*!< pattern match mode */