summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFlorian Festi <ffesti@redhat.com>2009-12-15 13:15:10 +0100
committerFlorian Festi <ffesti@redhat.com>2010-10-21 10:32:20 +0200
commit5914d0ec2995d4f4d128c3e96b79f22ec8f48d64 (patch)
tree32cf33c6ca6a04f465cf803d714761ea935f50e8 /lib
parenta916e399ab0734816f1e4567b34db0b55e9f5c7c (diff)
downloadlibrpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.tar.gz
librpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.tar.bz2
librpm-tizen-5914d0ec2995d4f4d128c3e96b79f22ec8f48d64.zip
rpmdbKeyIterator: loop over keys in the database indexes
Diffstat (limited to 'lib')
-rw-r--r--lib/rpmdb.c106
-rw-r--r--lib/rpmdb.h40
-rw-r--r--lib/rpmtypes.h1
3 files changed, 146 insertions, 1 deletions
diff --git a/lib/rpmdb.c b/lib/rpmdb.c
index cd8a4ffbd..73ca89a51 100644
--- a/lib/rpmdb.c
+++ b/lib/rpmdb.c
@@ -512,9 +512,18 @@ struct rpmdbMatchIterator_s {
};
-static rpmdb rpmdbRock;
+struct rpmdbKeyIterator_s {
+ rpmdbKeyIterator ki_next;
+ rpmdb ki_db;
+ dbiIndex ki_dbi;
+ rpmTag ki_rpmtag;
+ DBC * ki_dbc;
+ DBT ki_key;
+};
+static rpmdb rpmdbRock;
static rpmdbMatchIterator rpmmiRock;
+static rpmdbKeyIterator rpmkiRock;
int rpmdbCheckTerminate(int terminate)
{
@@ -2252,6 +2261,101 @@ static int td2key(rpmtd tagdata, DBT *key, int *freedata)
return 1;
}
+/*
+ * rpmdbKeyIterator
+ */
+
+rpmdbKeyIterator rpmdbKeyIteratorInit(rpmdb db, rpmTag rpmtag)
+{
+ rpmdbKeyIterator ki;
+ dbiIndex dbi = NULL;
+ int rc = 0;
+
+ if (db == NULL)
+ return NULL;
+
+ (void) rpmdbCheckSignals();
+
+ rc = dbiOpen(db, rpmtag, &dbi, 0);
+ if (dbi == NULL)
+ return NULL;
+
+ /* Chain cursors for teardown on abnormal exit. */
+ ki = xcalloc(1, sizeof(*ki));
+ ki->ki_next = rpmkiRock;
+ rpmkiRock = ki;
+
+ ki->ki_db = rpmdbLink(db);
+ ki->ki_rpmtag = rpmtag;
+ ki->ki_dbi = dbi;
+
+ return ki;
+}
+
+int rpmdbKeyIteratorNext(rpmdbKeyIterator ki)
+{
+ int rc, xx;
+ DBT data;
+
+ if (ki == NULL)
+ return 1;
+
+ if (ki->ki_dbc == NULL)
+ xx = dbiCopen(ki->ki_dbi, &ki->ki_dbc, 0);
+
+ memset(&data, 0, sizeof(data));
+ data.flags = DB_DBT_PARTIAL;
+ rc = dbiGet(ki->ki_dbi, ki->ki_dbc, &ki->ki_key, &data, DB_NEXT);
+
+ if (rc != 0 && rc != DB_NOTFOUND) {
+ rpmlog(RPMLOG_ERR,
+ _("error(%d:%s) getting next key from %s index\n"),
+ rc, db_strerror(rc), rpmTagGetName(ki->ki_rpmtag));
+ }
+ return rc;
+}
+
+const void * rpmdbKeyIteratorKey(rpmdbKeyIterator ki)
+{
+ if (ki == NULL) return NULL;
+ return ki->ki_key.data;
+}
+
+size_t rpmdbKeyIteratorKeySize(rpmdbKeyIterator ki)
+{
+ if (ki == NULL) return (size_t) 0;
+ return (size_t)(ki->ki_key.size);
+}
+
+
+rpmdbKeyIterator rpmdbKeyIteratorFree(rpmdbKeyIterator ki)
+{
+ rpmdbKeyIterator * prev, next;
+ int xx;
+
+ if (ki == NULL)
+ return ki;
+
+ prev = &rpmkiRock;
+ while ((next = *prev) != NULL && next != ki)
+ prev = &next->ki_next;
+ if (next) {
+ *prev = next->ki_next;
+ next->ki_next = NULL;
+ }
+
+ if (ki->ki_dbc)
+ xx = dbiCclose(ki->ki_dbi, ki->ki_dbc, 0);
+ ki->ki_dbc = NULL;
+ ki->ki_dbi = NULL; /* ??? */
+ ki->ki_db = rpmdbUnlink(ki->ki_db);
+
+ ki = _free(ki);
+ return ki;
+}
+
+
+
static void logAddRemove(const char *dbiname, int removing, rpmtd tagdata)
{
diff --git a/lib/rpmdb.h b/lib/rpmdb.h
index c130a4222..ee5ebf75b 100644
--- a/lib/rpmdb.h
+++ b/lib/rpmdb.h
@@ -216,6 +216,46 @@ rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi);
int rpmdbRebuild(const char * prefix, rpmts ts,
rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg));
+
+
+
+/** \ingroup rpmdb
+ * Get a key iterator for an index
+ * @param db rpm database
+ * @param rpmtag the index to iterate over
+ * @return the key iterator
+ */
+rpmdbKeyIterator rpmdbKeyIteratorInit(rpmdb db, rpmTag rpmtag);
+
+/** \ingroup rpmdb
+ * Get the next key - must be called before getting the first key
+ * @param ki key iterator
+ * @return 0 on success; != 0 on error or end of index
+ */
+int rpmdbKeyIteratorNext(rpmdbKeyIterator ki);
+
+/** \ingroup rpmdb
+ * Get current key
+ * @param ki key iterator
+ * @return pointer to key content. Keys are not zero terminated!
+ */
+const void * rpmdbKeyIteratorKey(rpmdbKeyIterator ki);
+
+/** \ingroup rpmdb
+ * Get length of key
+ * @param ki key iterator
+ * @return length of key
+ */
+size_t rpmdbKeyIteratorKeySize(rpmdbKeyIterator ki);
+
+/** \ingroup rpmdb
+ * Free key iterator
+ * @param ki key iterator
+ * return NULL
+ */
+rpmdbKeyIterator rpmdbKeyIteratorFree(rpmdbKeyIterator ki);
+
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/rpmtypes.h b/lib/rpmtypes.h
index c6f3805f6..8e2bb7dcd 100644
--- a/lib/rpmtypes.h
+++ b/lib/rpmtypes.h
@@ -67,6 +67,7 @@ typedef struct rpmdbMatchIterator_s * rpmdbMatchIterator;
typedef struct rpmtsi_s * rpmtsi;
typedef struct rpmps_s * rpmps;
+typedef struct rpmdbKeyIterator_s * rpmdbKeyIterator;
typedef const void * fnpyKey;
typedef void * rpmCallbackData;
/** @} */