From caf595813c88d4457576109fa0194c165b6c70ac Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Wed, 3 Nov 2010 10:58:41 +0100 Subject: [PATCH] Merge rpmdbIndexIteratorKey and rpmdbIndexIteratorKeySize into rpmdbIndexIteratorNext --- lib/rpmdb.c | 39 +++++++++++++++++++-------------------- lib/rpmdb.h | 21 +++++---------------- python/rpmii-py.c | 7 ++++--- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/lib/rpmdb.c b/lib/rpmdb.c index e539ebb..63f9a2c 100644 --- a/lib/rpmdb.c +++ b/lib/rpmdb.c @@ -2272,41 +2272,40 @@ rpmdbIndexIterator rpmdbIndexIteratorInit(rpmdb db, rpmDbiTag rpmtag) return ii; } -int rpmdbIndexIteratorNext(rpmdbIndexIterator ii) +int rpmdbIndexIteratorNext(rpmdbIndexIterator ii, const void ** key, size_t * keylen) { int rc, xx; DBT data; if (ii == NULL) - return 1; + return -1; if (ii->ii_dbc == NULL) xx = dbiCopen(ii->ii_dbi, &ii->ii_dbc, 0); + /* free old data */ + ii->ii_set = dbiFreeIndexSet(ii->ii_set); + memset(&data, 0, sizeof(data)); rc = dbiGet(ii->ii_dbi, ii->ii_dbc, &ii->ii_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(ii->ii_rpmtag)); - } - ii->ii_set = dbiFreeIndexSet(ii->ii_set); - (void) dbt2set(ii->ii_dbi, &data, &ii->ii_set); + if (rc != 0) { + *key = NULL; + *keylen = 0; - return rc; -} + if (rc != DB_NOTFOUND) { + rpmlog(RPMLOG_ERR, + _("error(%d:%s) getting next key from %s index\n"), + rc, db_strerror(rc), rpmTagGetName(ii->ii_rpmtag)); + } + return -1; + } -const void * rpmdbIndexIteratorKey(rpmdbIndexIterator ii) -{ - if (ii == NULL) return NULL; - return ii->ii_key.data; -} + (void) dbt2set(ii->ii_dbi, &data, &ii->ii_set); + *key = ii->ii_key.data; + *keylen = ii->ii_key.size; -size_t rpmdbIndexIteratorKeySize(rpmdbIndexIterator ii) -{ - if (ii == NULL) return (size_t) 0; - return (size_t)(ii->ii_key.size); + return 0; } unsigned int rpmdbIndexIteratorNumPkgs(rpmdbIndexIterator ii) diff --git a/lib/rpmdb.h b/lib/rpmdb.h index 4ea6420..a8479f5 100644 --- a/lib/rpmdb.h +++ b/lib/rpmdb.h @@ -228,25 +228,14 @@ int rpmdbRebuild(const char * prefix, rpmts ts, rpmdbIndexIterator rpmdbIndexIteratorInit(rpmdb db, rpmDbiTag rpmtag); /** \ingroup rpmdb - * Get the next key - must be called before getting the first key + * Get the next key - Warning! Keys are not zero terminated! + * Binary tags may even contain zero bytes * @param ii index iterator + * @param key adress to save the pointer to the key + * @param keylen adress to save the length of the key to * @return 0 on success; != 0 on error or end of index */ -int rpmdbIndexIteratorNext(rpmdbIndexIterator ii); - -/** \ingroup rpmdb - * Get current key - * @param ii index iterator - * @return pointer to key content. Keys are not zero terminated! - */ -const void * rpmdbIndexIteratorKey(rpmdbIndexIterator ii); - -/** \ingroup rpmdb - * Get length of key - * @param ii index iterator - * @return length of key - */ -size_t rpmdbIndexIteratorKeySize(rpmdbIndexIterator ii); +int rpmdbIndexIteratorNext(rpmdbIndexIterator ii, const void ** key, size_t * keylen); /** \ingroup rpmdb * Get number of entries for current key diff --git a/python/rpmii-py.c b/python/rpmii-py.c index 5275a31..d40035c 100644 --- a/python/rpmii-py.c +++ b/python/rpmii-py.c @@ -43,12 +43,13 @@ struct rpmiiObject_s { static PyObject * rpmii_iternext(rpmiiObject * s) { - if (s->ii == NULL || (rpmdbIndexIteratorNext(s->ii)) != 0) { + char * key; + size_t keylen; + if (s->ii == NULL || (rpmdbIndexIteratorNext(s->ii, (const void**)&key, &keylen)) != 0) { s->ii = rpmdbIndexIteratorFree(s->ii); return NULL; } - return PyString_FromStringAndSize(rpmdbIndexIteratorKey(s->ii), - rpmdbIndexIteratorKeySize(s->ii)); + return PyString_FromStringAndSize(key, keylen); }; static PyObject * -- 2.7.4