fix: prevent segfault if malicious server sends 1 GB of data through ftpNLST.
[platform/upstream/rpm.git] / rpmdb / rpmdb.h
index 0dc0ad6..afb4609 100644 (file)
@@ -1,5 +1,6 @@
 #ifndef H_RPMDB
 #define H_RPMDB
+/*@-bounds@*/
 
 /** \ingroup rpmdb dbi db1 db3
  * \file rpmdb/rpmdb.h
@@ -7,13 +8,46 @@
  */
 
 #include <assert.h>
-#include <rpmlib.h>
-#include <db.h>
+#include "rpmlib.h"
+#include "rpmsw.h"
+#include "db.h"
+
+/*@-exportlocal@*/
+/*@unchecked@*/
+extern int _rpmdb_debug;
+/*@=exportlocal@*/
+
+#ifdef NOTYET
+/** \ingroup rpmdb
+ * Database of headers and tag value indices.
+ */
+typedef /*@abstract@*/ /*@refcounted@*/ struct rpmdb_s * rpmdb;
+
+/** \ingroup rpmdb
+ * Database iterator.
+ */
+typedef /*@abstract@*/ struct _rpmdbMatchIterator * rpmdbMatchIterator;
+#endif
+
+/**
+ * Tag value pattern match mode.
+ */
+typedef enum rpmMireMode_e {
+    RPMMIRE_DEFAULT    = 0,    /*!< regex with \., .* and ^...$ added */
+    RPMMIRE_STRCMP     = 1,    /*!< strings  using strcmp(3) */
+    RPMMIRE_REGEX      = 2,    /*!< regex(7) patterns through regcomp(3) */
+    RPMMIRE_GLOB       = 3     /*!< glob(7) patterns through fnmatch(3) */
+} rpmMireMode;
 
 /**
  */
 typedef /*@abstract@*/ struct _dbiIndexItem * dbiIndexItem;
 
+/** \ingroup rpmdb
+ * A single element (i.e. inverted list from tag values) of a database.
+ */
+typedef /*@abstract@*/ struct _dbiIndexSet * dbiIndexSet;
+
 /**
  */
 typedef /*@abstract@*/ struct _dbiIndex * dbiIndex;
@@ -28,7 +62,6 @@ struct _dbiIndexItem {
     unsigned int hdrNum;               /*!< header instance in db */
     unsigned int tagNum;               /*!< tag index in header */
     unsigned int fpNum;                        /*!< finger print index */
-    unsigned int dbNum;                        /*!< database index */
 };
 
 /** \ingroup dbi
@@ -53,8 +86,8 @@ struct _dbiVec {
  * @param rpmtag       rpm tag
  * @return             0 on success
  */
-    int (*open) (rpmdb rpmdb, int rpmtag, /*@out@*/ dbiIndex * dbip)
-       /*@globals fileSystem@*/
+    int (*open) (rpmdb rpmdb, rpmTag rpmtag, /*@out@*/ dbiIndex * dbip)
+       /*@globals fileSystem @*/
        /*@modifies *dbip, fileSystem @*/;
 
 /** \ingroup dbi
@@ -64,7 +97,7 @@ struct _dbiVec {
  * @return             0 on success
  */
     int (*close) (/*@only@*/ dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, fileSystem @*/;
 
 /** \ingroup dbi
@@ -74,20 +107,47 @@ struct _dbiVec {
  * @return             0 on success
  */
     int (*sync) (dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies fileSystem @*/;
 
 /** \ingroup dbi
+ * Associate secondary database with primary.
+ * @param dbi          index database handle
+ * @param dbisecondary secondary index database handle
+ * @param callback     create secondary key from primary (NULL if DB_RDONLY)
+ * @param flags                DB_CREATE or 0
+ * @return             0 on success
+ */
+    int (*associate) (dbiIndex dbi, dbiIndex dbisecondary,
+                int (*callback) (DB *, const DBT *, const DBT *, DBT *),
+                unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies dbi, fileSystem @*/;
+
+/** \ingroup dbi
+ * Return join cursor for list of cursors.
+ * @param dbi          index database handle
+ * @param curslist     NULL terminated list of database cursors
+ * @retval dbcp                address of join database cursor
+ * @param flags                DB_JOIN_NOSORT or 0
+ * @return             0 on success
+ */
+    int (*join) (dbiIndex dbi, DBC ** curslist, /*@out@*/ DBC ** dbcp,
+                unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies dbi, *dbcp, fileSystem @*/;
+
+/** \ingroup dbi
  * Open database cursor.
  * @param dbi          index database handle
  * @param txnid                database transaction handle
- * @retval dbcp                address of database cursor
- * @param flags                (unused)
+ * @retval dbcp                address of new database cursor
+ * @param dbiflags     DB_WRITECURSOR or 0
  * @return             0 on success
  */
     int (*copen) (dbiIndex dbi, /*@null@*/ DB_TXN * txnid,
-                       /*@out@*/ DBC ** dbcp, unsigned int flags)
-       /*@globals fileSystem@*/
+                       /*@out@*/ DBC ** dbcp, unsigned int dbiflags)
+       /*@globals fileSystem @*/
        /*@modifies dbi, *txnid, *dbcp, fileSystem @*/;
 
 /** \ingroup dbi
@@ -98,10 +158,23 @@ struct _dbiVec {
  * @return             0 on success
  */
     int (*cclose) (dbiIndex dbi, /*@only@*/ DBC * dbcursor, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, *dbcursor, fileSystem @*/;
 
 /** \ingroup dbi
+ * Duplicate a database cursor.
+ * @param dbi          index database handle
+ * @param dbcursor     database cursor
+ * @retval dbcp                address of new database cursor
+ * @param flags                DB_POSITION for same position, 0 for uninitialized
+ * @return             0 on success
+ */
+    int (*cdup) (dbiIndex dbi, DBC * dbcursor, /*@out@*/ DBC ** dbcp,
+               unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies dbi, *dbcp, fileSystem @*/;
+
+/** \ingroup dbi
  * Delete (key,data) pair(s) using db->del or dbcursor->c_del.
  * @param dbi          index database handle
  * @param dbcursor     database cursor (NULL will use db->del)
@@ -112,7 +185,7 @@ struct _dbiVec {
  */
     int (*cdel) (dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                        unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies *dbcursor, fileSystem @*/;
 
 /** \ingroup dbi
@@ -126,10 +199,25 @@ struct _dbiVec {
  */
     int (*cget) (dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                        unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies *dbcursor, *key, *data, fileSystem @*/;
 
 /** \ingroup dbi
+ * Retrieve (key,data) pair using dbcursor->c_pget.
+ * @param dbi          index database handle
+ * @param dbcursor     database cursor
+ * @param key          secondary retrieve key value/length/flags
+ * @param pkey         primary retrieve key value/length/flags
+ * @param data         primary retrieve data value/length/flags
+ * @param flags                DB_NEXT, DB_SET, or 0
+ * @return             0 on success
+ */
+    int (*cpget) (dbiIndex dbi, /*@null@*/ DBC * dbcursor,
+               DBT * key, DBT * pkey, DBT * data, unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies *dbcursor, *key, *pkey, *data, fileSystem @*/;
+
+/** \ingroup dbi
  * Store (key,data) pair using db->put or dbcursor->c_put.
  * @param dbi          index database handle
  * @param dbcursor     database cursor (NULL will use db->put)
@@ -140,7 +228,7 @@ struct _dbiVec {
  */
     int (*cput) (dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                        unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies *dbcursor, fileSystem @*/;
 
 /** \ingroup dbi
@@ -154,7 +242,7 @@ struct _dbiVec {
     int (*ccount) (dbiIndex dbi, DBC * dbcursor,
                        /*@out@*/ unsigned int * countp,
                        unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies *dbcursor, fileSystem @*/;
 
 /** \ingroup dbi
@@ -163,8 +251,8 @@ struct _dbiVec {
  * @return             0 no
  */
     int (*byteswapped) (dbiIndex dbi)
-       /*@globals fileSystem@*/
-       /*@modifies fileSystem@*/;
+       /*@globals fileSystem @*/
+       /*@modifies fileSystem @*/;
 
 /** \ingroup dbi
  * Save statistics in database handle.
@@ -173,7 +261,7 @@ struct _dbiVec {
  * @return             0 on success
  */
     int (*stat) (dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, fileSystem @*/;
 
 };
@@ -207,8 +295,6 @@ struct _dbiIndex {
     int        dbi_api;                /*!< Berkeley API type */
 
     int        dbi_verify_on_close;
-    int        dbi_tear_down;          /*!< tear down dbenv on close */
-    int        dbi_use_cursors;        /*!< access with cursors? (always) */
     int        dbi_use_dbenv;          /*!< use db environment? */
     int        dbi_permit_dups;        /*!< permit duplicate entries? */
     int        dbi_no_fsync;           /*!< no-op fsync for db */
@@ -218,15 +304,16 @@ struct _dbiIndex {
     int        dbi_debug;
     int        dbi_byteswapped;
 
-/*@null@*/ char * dbi_host;
-    long dbi_cl_timeout;
-    long dbi_sv_timeout;
+/*@null@*/
+    char * dbi_host;
+    unsigned long dbi_cl_timeout;
+    unsigned long dbi_sv_timeout;
 
        /* dbenv parameters */
     int        dbi_lorder;
 /*@unused@*/
     void (*db_errcall) (const char *db_errpfx, char *buffer)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies fileSystem @*/;
 /*@unused@*/ /*@shared@*/
     FILE *     dbi_errfile;
@@ -235,8 +322,8 @@ struct _dbiIndex {
     int        dbi_region_init;
     int        dbi_tas_spins;
        /* mpool sub-system parameters */
-    int        dbi_mp_mmapsize;        /*!< (10Mb) */
-    int        dbi_mp_size;    /*!< (128Kb) */
+    int        dbi_mmapsize;   /*!< (10Mb) */
+    int        dbi_cachesize;  /*!< (128Kb) */
        /* lock sub-system parameters */
     unsigned int dbi_lk_max;
     unsigned int dbi_lk_detect;
@@ -250,11 +337,10 @@ struct _dbiIndex {
 #if 0
     int        (*dbi_tx_recover) (DB_ENV *dbenv, DBT *log_rec,
                                DB_LSN *lsnp, int redo, void *info)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies fileSystem @*/;
 #endif
        /* dbinfo parameters */
-    int        dbi_cachesize;          /*!< */
     int        dbi_pagesize;           /*!< (fs blksize) */
 /*@unused@*/ /*@null@*/
     void * (*dbi_malloc) (size_t nbytes)
@@ -287,12 +373,10 @@ struct _dbiIndex {
     unsigned int dbi_q_extentsize;
 
 /*@refcounted@*/
-    rpmdb dbi_rpmdb;
+    rpmdb dbi_rpmdb;           /*!< the parent rpm database */
     rpmTag dbi_rpmtag;         /*!< rpm tag used for index */
     int        dbi_jlen;               /*!< size of join key */
 
-    unsigned int dbi_lastoffset;       /*!< db1 with falloc.c needs this */
-
 /*@only@*//*@null@*/
     DB * dbi_db;               /*!< Berkeley DB * handle */
 /*@only@*//*@null@*/
@@ -309,32 +393,48 @@ struct _dbiIndex {
  * Describes the collection of index databases used by rpm.
  */
 struct rpmdb_s {
-/*@owned@*/ const char * db_root;/*!< path prefix */
-/*@owned@*/ const char * db_home;/*!< directory path */
+/*@owned@*/
+    const char * db_root;/*!< path prefix */
+/*@owned@*/
+    const char * db_home;/*!< directory path */
     int                db_flags;
     int                db_mode;        /*!< open mode */
     int                db_perms;       /*!< open permissions */
     int                db_api;         /*!< Berkeley API type */
-/*@owned@*/ const char * db_errpfx;
+/*@owned@*/
+    const char * db_errpfx;
     int                db_remove_env;
     int                db_filter_dups;
     int                db_chrootDone;  /*!< If chroot(2) done, ignore db_root. */
     void (*db_errcall) (const char *db_errpfx, char *buffer)
        /*@*/;
-/*@shared@*/ FILE *    db_errfile;
-/*@only@*/ void * (*db_malloc) (size_t nbytes)
+/*@shared@*/
+    FILE *     db_errfile;
+/*@only@*/
+    void * (*db_malloc) (size_t nbytes)
        /*@*/;
-/*@only@*/ void * (*db_realloc) (/*@only@*//*@null@*/ void * ptr,
+/*@only@*/
+    void * (*db_realloc) (/*@only@*//*@null@*/ void * ptr,
                                                size_t nbytes)
        /*@*/;
     void (*db_free) (/*@only@*/ void * ptr)
        /*@modifies *ptr @*/;
+/*@only@*/ /*@null@*/
+    unsigned char * db_bits;   /*!< package instance bit mask. */
+    int                db_nbits;       /*!< no. of bits in mask. */
+    rpmdb      db_next;
     int                db_opens;
-/*@only@*//*@null@*/ void * db_dbenv;  /*!< Berkeley DB_ENV handle */
+/*@only@*/ /*@null@*/
+    void *     db_dbenv;       /*!< Berkeley DB_ENV handle. */
     int                db_ndbi;        /*!< No. of tag indices. */
-    dbiIndex * _dbi;           /*!< Tag indices. */
+    dbiIndex * _dbi;           /*!< Tag indices. */
 
-/*@refs@*/ int nrefs;          /*!< Reference count. */
+    struct rpmop_s db_getops;
+    struct rpmop_s db_putops;
+    struct rpmop_s db_delops;
+
+/*@refs@*/
+    int nrefs;                 /*!< Reference count. */
 };
 
 /* for RPM's internal use only */
@@ -357,11 +457,11 @@ extern "C" {
 /** \ingroup db3
  * Return new configured index database handle instance.
  * @param rpmdb                rpm database
- * @param rpmtag
+ * @param rpmtag       rpm tag
  * @return             index database handle
  */
 /*@unused@*/ /*@only@*/ /*@null@*/
-dbiIndex db3New(rpmdb rpmdb, int rpmtag)
+dbiIndex db3New(rpmdb rpmdb, rpmTag rpmtag)
        /*@globals rpmGlobalMacroContext @*/
        /*@modifies rpmGlobalMacroContext @*/;
 
@@ -393,11 +493,12 @@ extern const char *const prDbiOpenFlags(int dbflags, int print_dbenv_flags)
  * @param flags                (unused)
  * @return             index database handle
  */
-/*@only@*/ /*@null@*/ dbiIndex dbiOpen(/*@null@*/ rpmdb db, int rpmtag,
+/*@only@*/ /*@null@*/ dbiIndex dbiOpen(/*@null@*/ rpmdb db, rpmTag rpmtag,
                unsigned int flags)
-       /*@modifies db @*/;
+       /*@globals rpmGlobalMacroContext, errno @*/
+       /*@modifies db, rpmGlobalMacroContext, errno @*/;
 
-/*@-globuse -mods -mustmod @*/
+/*@-globuse -mustmod @*/ /* FIX: vector annotations */
 /** \ingroup dbi
  * Open a database cursor.
  * @param dbi          index database handle
@@ -409,7 +510,7 @@ extern const char *const prDbiOpenFlags(int dbflags, int print_dbenv_flags)
 /*@unused@*/ static inline
 int dbiCopen(dbiIndex dbi, /*@null@*/ DB_TXN * txnid,
                /*@out@*/ DBC ** dbcp, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, *dbcp, fileSystem @*/
 {
     return (*dbi->dbi_vec->copen) (dbi, txnid, dbcp, flags);
@@ -424,13 +525,29 @@ int dbiCopen(dbiIndex dbi, /*@null@*/ DB_TXN * txnid,
  */
 /*@unused@*/ static inline
 int dbiCclose(dbiIndex dbi, /*@only@*/ DBC * dbcursor, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, *dbcursor, fileSystem @*/
 {
     return (*dbi->dbi_vec->cclose) (dbi, dbcursor, flags);
 }
 
 /** \ingroup dbi
+ * Duplicate a database cursor.
+ * @param dbi          index database handle
+ * @param dbcursor     database cursor
+ * @retval dbcp                address of new database cursor
+ * @param flags                DB_POSITION for same position, 0 for uninitialized
+ * @return             0 on success
+ */
+/*@unused@*/ static inline
+int dbiCdup(dbiIndex dbi, DBC * dbcursor, /*@out@*/ DBC ** dbcp,
+               unsigned int flags)
+       /*@modifies dbi, *dbcp @*/
+{
+    return (*dbi->dbi_vec->cdup) (dbi, dbcursor, dbcp, flags);
+}
+
+/** \ingroup dbi
  * Delete (key,data) pair(s) from index database.
  * @param dbi          index database handle
  * @param dbcursor     database cursor (NULL will use db->del)
@@ -442,11 +559,15 @@ int dbiCclose(dbiIndex dbi, /*@only@*/ DBC * dbcursor, unsigned int flags)
 /*@unused@*/ static inline
 int dbiDel(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem@*/
-       /*@modifies *dbcursor, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, fileSystem, internalState @*/
 {
-    assert(key->size > 0);
-    return (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags);
+    int rc;
+    assert(key->data != NULL && key->size > 0);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_delops, 0);
+    rc = (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_delops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -461,11 +582,39 @@ int dbiDel(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
 /*@unused@*/ static inline
 int dbiGet(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem@*/
-       /*@modifies *dbcursor, *key, *data, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, *data, fileSystem, internalState @*/
+{
+    int rc;
+    assert((flags == DB_NEXT) || (key->data != NULL && key->size > 0));
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_getops, 0);
+    rc = (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_getops, data->size);
+    return rc;
+}
+
+/** \ingroup dbi
+ * Retrieve (key,data) pair using dbcursor->c_pget.
+ * @param dbi          index database handle
+ * @param dbcursor     database cursor (NULL will use db->get)
+ * @param key          secondary retrieve key value/length/flags
+ * @param pkey         primary retrieve key value/length/flags
+ * @param data         primary retrieve data value/length/flags
+ * @param flags                DB_NEXT, DB_SET, or 0
+ * @return             0 on success
+ */
+/*@unused@*/ static inline
+int dbiPget(dbiIndex dbi, /*@null@*/ DBC * dbcursor,
+               DBT * key, DBT * pkey, DBT * data, unsigned int flags)
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, *pkey, *data, fileSystem, internalState @*/
 {
-    assert(key->size > 0);
-    return (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags);
+    int rc;
+    assert((flags == DB_NEXT) || (key->data != NULL && key->size > 0));
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_getops, 0);
+    rc = (dbi->dbi_vec->cpget) (dbi, dbcursor, key, pkey, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_getops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -480,11 +629,15 @@ int dbiGet(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
 /*@unused@*/ static inline
 int dbiPut(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem@*/
-       /*@modifies *dbcursor, *key, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, fileSystem, internalState @*/
 {
-    assert(key->size > 0);
-    return (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags);
+    int rc;
+    assert(key->data != NULL && key->size > 0 && data->data != NULL && data->size > 0);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_putops, 0);
+    rc = (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_putops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -498,7 +651,7 @@ int dbiPut(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
 /*@unused@*/ static inline
 int dbiCount(dbiIndex dbi, DBC * dbcursor, /*@out@*/ unsigned int * countp,
                unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies *dbcursor, fileSystem @*/
 {
     return (*dbi->dbi_vec->ccount) (dbi, dbcursor, countp, flags);
@@ -512,7 +665,7 @@ int dbiCount(dbiIndex dbi, DBC * dbcursor, /*@out@*/ unsigned int * countp,
  */
 /*@unused@*/ static inline
 int dbiVerify(/*@only@*/ dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, fileSystem @*/
 {
     dbi->dbi_verify_on_close = 1;
@@ -527,7 +680,7 @@ int dbiVerify(/*@only@*/ dbiIndex dbi, unsigned int flags)
  */
 /*@unused@*/ static inline
 int dbiClose(/*@only@*/ dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies dbi, fileSystem @*/
 {
     return (*dbi->dbi_vec->close) (dbi, flags);
@@ -541,28 +694,73 @@ int dbiClose(/*@only@*/ dbiIndex dbi, unsigned int flags)
  */
 /*@unused@*/ static inline
 int dbiSync (dbiIndex dbi, unsigned int flags)
-       /*@globals fileSystem@*/
+       /*@globals fileSystem @*/
        /*@modifies fileSystem @*/
 {
     return (*dbi->dbi_vec->sync) (dbi, flags);
 }
 
 /** \ingroup dbi
+ * Associate secondary database with primary.
+ * @param dbi          index database handle
+ * @param dbisecondary secondary index database handle
+ * @param callback     create secondary key from primary (NULL if DB_RDONLY)
+ * @param flags                DB_CREATE or 0
+ * @return             0 on success
+ */
+/*@unused@*/ static inline
+int dbiAssociate(dbiIndex dbi, dbiIndex dbisecondary,
+                int (*callback) (DB *, const DBT *, const DBT *, DBT *),
+                unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies dbi, fileSystem @*/
+{
+    return (*dbi->dbi_vec->associate) (dbi, dbisecondary, callback, flags);
+}
+
+/** \ingroup dbi
+ * Return join cursor for list of cursors.
+ * @param dbi          index database handle
+ * @param curslist     NULL terminated list of database cursors
+ * @retval dbcp                address of join database cursor
+ * @param flags                DB_JOIN_NOSORT or 0
+ * @return             0 on success
+ */
+/*@unused@*/ static inline
+int dbiJoin(dbiIndex dbi, DBC ** curslist, /*@out@*/ DBC ** dbcp,
+                unsigned int flags)
+       /*@globals fileSystem @*/
+       /*@modifies dbi, *dbcp, fileSystem @*/
+{
+    return (*dbi->dbi_vec->join) (dbi, curslist, dbcp, flags);
+}
+
+/** \ingroup dbi
  * Is database byte swapped?
  * @param dbi          index database handle
- * @return             0 no
+ * @return             0 same order, 1 swapped order
  */
 /*@unused@*/ static inline
 int dbiByteSwapped(dbiIndex dbi)
-       /*@*/
+       /*@modifies dbi @*/
 {
-/*@-mods@*/ /* FIX: shrug */
     if (dbi->dbi_byteswapped == -1)
         dbi->dbi_byteswapped = (*dbi->dbi_vec->byteswapped) (dbi);
-/*@=mods@*/
     return dbi->dbi_byteswapped;
 }
-/*@=globuse =mods =mustmod @*/
+/** \ingroup dbi
+ * Is database byte swapped?
+ * @param dbi          index database handle
+ * @param flags                DB_FAST_STAT or 0
+ * @return             0 on success
+ */
+/*@unused@*/ static inline
+int dbiStat(dbiIndex dbi, unsigned int flags)
+       /*@modifies dbi @*/
+{
+    return (*dbi->dbi_vec->stat) (dbi, flags);
+}
+/*@=globuse =mustmod @*/
 
 /*@=exportlocal@*/
 
@@ -605,6 +803,300 @@ unsigned int dbiIndexRecordOffset(dbiIndexSet set, int recno)
 unsigned int dbiIndexRecordFileNumber(dbiIndexSet set, int recno)
        /*@*/;
 
+/** \ingroup rpmdb
+ * Tags for which rpmdb indices will be built.
+ */
+/*@-exportlocal@*/
+/*@unchecked@*/
+/*@only@*/ /*@null@*/ extern int * dbiTags;
+/*@unchecked@*/
+extern int dbiTagsMax;
+/*@=exportlocal@*/
+
+/** \ingroup rpmdb
+ * Unreference a database instance.
+ * @param db           rpm database
+ * @param msg
+ * @return             NULL always
+ */
+/*@unused@*/ /*@null@*/
+rpmdb rpmdbUnlink (/*@killref@*/ /*@only@*/ rpmdb db, const char * msg)
+       /*@modifies db @*/;
+
+/** @todo Remove debugging entry from the ABI. */
+/*@-exportlocal@*/
+/*@null@*/
+rpmdb XrpmdbUnlink (/*@killref@*/ /*@only@*/ rpmdb db, const char * msg,
+               const char * fn, unsigned ln)
+       /*@modifies db @*/;
+/*@=exportlocal@*/
+#define        rpmdbUnlink(_db, _msg)  XrpmdbUnlink(_db, _msg, __FILE__, __LINE__)
+
+/** \ingroup rpmdb
+ * Reference a database instance.
+ * @param db           rpm database
+ * @param msg
+ * @return             new rpm database reference
+ */
+/*@unused@*/
+rpmdb rpmdbLink (rpmdb db, const char * msg)
+       /*@modifies db @*/;
+
+/** @todo Remove debugging entry from the ABI. */
+/*@-exportlocal@*/
+rpmdb XrpmdbLink (rpmdb db, const char * msg,
+               const char * fn, unsigned ln)
+        /*@modifies db @*/;
+/*@=exportlocal@*/
+#define        rpmdbLink(_db, _msg)    XrpmdbLink(_db, _msg, __FILE__, __LINE__)
+
+/** \ingroup rpmdb
+ * Open rpm database.
+ * @param prefix       path to top of install tree
+ * @retval dbp         address of rpm database
+ * @param mode         open(2) flags:  O_RDWR or O_RDONLY (O_CREAT also)
+ * @param perms                database permissions
+ * @return             0 on success
+ */
+int rpmdbOpen (/*@null@*/ const char * prefix, /*@null@*/ /*@out@*/ rpmdb * dbp,
+               int mode, int perms)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies *dbp, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Initialize database.
+ * @param prefix       path to top of install tree
+ * @param perms                database permissions
+ * @return             0 on success
+ */
+int rpmdbInit(/*@null@*/ const char * prefix, int perms)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Verify database components.
+ * @param prefix       path to top of install tree
+ * @return             0 on success
+ */
+int rpmdbVerify(/*@null@*/ const char * prefix)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/**
+ * Close a single database index.
+ * @param db           rpm database
+ * @param rpmtag       rpm tag
+ * @return              0 on success
+ */
+int rpmdbCloseDBI(/*@null@*/ rpmdb db, int rpmtag)
+       /*@globals fileSystem @*/
+       /*@modifies db, fileSystem @*/;
+
+/** \ingroup rpmdb
+ * Close all database indices and free rpmdb.
+ * @param db           rpm database
+ * @return             0 on success
+ */
+int rpmdbClose (/*@killref@*/ /*@only@*/ /*@null@*/ rpmdb db)
+       /*@globals fileSystem @*/
+       /*@modifies db, fileSystem @*/;
+
+/** \ingroup rpmdb
+ * Sync all database indices.
+ * @param db           rpm database
+ * @return             0 on success
+ */
+int rpmdbSync (/*@null@*/ rpmdb db)
+       /*@globals fileSystem @*/
+       /*@modifies fileSystem @*/;
+
+/** \ingroup rpmdb
+ * Open all database indices.
+ * @param db           rpm database
+ * @return             0 on success
+ */
+/*@-exportlocal@*/
+int rpmdbOpenAll (/*@null@*/ rpmdb db)
+       /*@globals rpmGlobalMacroContext @*/
+       /*@modifies db, rpmGlobalMacroContext @*/;
+/*@=exportlocal@*/
+
+/** \ingroup rpmdb
+ * Return number of instances of package in rpm database.
+ * @param db           rpm database
+ * @param name         rpm package name
+ * @return             number of instances
+ */
+int rpmdbCountPackages(/*@null@*/ rpmdb db, const char * name)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Return header join key for current position of rpm database iterator.
+ * @param mi           rpm database iterator
+ * @return             current header join key
+ */
+unsigned int rpmdbGetIteratorOffset(/*@null@*/ rpmdbMatchIterator mi)
+       /*@*/;
+
+/** \ingroup rpmdb
+ * Return number of elements in rpm database iterator.
+ * @param mi           rpm database iterator
+ * @return             number of elements
+ */
+int rpmdbGetIteratorCount(/*@null@*/ rpmdbMatchIterator mi)
+       /*@*/;
+
+/** \ingroup rpmdb
+ * Append items to set of package instances to iterate.
+ * @param mi           rpm database iterator
+ * @param hdrNums      array of package instances
+ * @param nHdrNums     number of elements in array
+ * @return             0 on success, 1 on failure (bad args)
+ */
+int rpmdbAppendIterator(/*@null@*/ rpmdbMatchIterator mi,
+               /*@null@*/ const int * hdrNums, int nHdrNums)
+       /*@modifies mi @*/;
+
+/** \ingroup rpmdb
+ * Remove items from set of package instances to iterate.
+ * @note Sorted hdrNums are always passed in rpmlib.
+ * @param mi           rpm database iterator
+ * @param hdrNums      array of package instances
+ * @param nHdrNums     number of elements in array
+ * @param sorted       is the array sorted? (array will be sorted on return)
+ * @return             0 on success, 1 on failure (bad args)
+ */
+int rpmdbPruneIterator(/*@null@*/ rpmdbMatchIterator mi,
+               /*@null@*/ int * hdrNums, int nHdrNums, int sorted)
+       /*@modifies mi, hdrNums @*/;
+
+/** \ingroup rpmdb
+ * Add pattern to iterator selector.
+ * @param mi           rpm database iterator
+ * @param tag          rpm tag
+ * @param mode         type of pattern match
+ * @param pattern      pattern to match
+ * @return             0 on success
+ */
+int rpmdbSetIteratorRE(/*@null@*/ rpmdbMatchIterator mi, rpmTag tag,
+               rpmMireMode mode, /*@null@*/ const char * pattern)
+       /*@globals rpmGlobalMacroContext @*/
+       /*@modifies mi, mode, rpmGlobalMacroContext @*/;
+
+/** \ingroup rpmdb
+ * Prepare iterator for lazy writes.
+ * @note Must be called before rpmdbNextIterator() with CDB model database.
+ * @param mi           rpm database iterator
+ * @param rewrite      new value of rewrite
+ * @return             previous value
+ */
+int rpmdbSetIteratorRewrite(/*@null@*/ rpmdbMatchIterator mi, int rewrite)
+       /*@modifies mi @*/;
+
+/** \ingroup rpmdb
+ * Modify iterator to mark header for lazy write on release.
+ * @param mi           rpm database iterator
+ * @param modified     new value of modified
+ * @return             previous value
+ */
+int rpmdbSetIteratorModified(/*@null@*/ rpmdbMatchIterator mi, int modified)
+       /*@modifies mi @*/;
+
+/** \ingroup rpmdb
+ * Modify iterator to verify retrieved header blobs.
+ * @param mi           rpm database iterator
+ * @param ts           transaction set
+ * @param (*hdrchk)    headerCheck() vector
+ * @return             0 always
+ */
+int rpmdbSetHdrChk(/*@null@*/ rpmdbMatchIterator mi, /*@null@*/ rpmts ts,
+               /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void * uh, size_t uc, const char ** msg))
+       /*@modifies mi @*/;
+
+/** \ingroup rpmdb
+ * Return database iterator.
+ * @param db           rpm database
+ * @param rpmtag       rpm tag
+ * @param keyp         key data (NULL for sequential access)
+ * @param keylen       key data length (0 will use strlen(keyp))
+ * @return             NULL on failure
+ */
+/*@only@*/ /*@null@*/
+rpmdbMatchIterator rpmdbInitIterator(/*@null@*/ rpmdb db, rpmTag rpmtag,
+                       /*@null@*/ const void * keyp, size_t keylen)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Return next package header from iteration.
+ * @param mi           rpm database iterator
+ * @return             NULL on end of iteration.
+ */
+/*@null@*/
+Header rpmdbNextIterator(/*@null@*/ rpmdbMatchIterator mi)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Check rpmdb signal handler for trapped signal exit.
+ */
+/*@mayexit@*/
+int rpmdbCheckSignals(void)
+       /*@globals fileSystem, internalState @*/
+       /*@modifies fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Destroy rpm database iterator.
+ * @param mi           rpm database iterator
+ * @return             NULL always
+ */
+/*@null@*/
+rpmdbMatchIterator rpmdbFreeIterator(/*@only@*/ /*@null@*/rpmdbMatchIterator mi)
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Add package header to rpm database and indices.
+ * @param db           rpm database
+ * @param iid          install transaction id (iid = 0 or -1 to skip)
+ * @param h            header
+ * @param ts           (unused) transaction set (or NULL)
+ * @param (*hdrchk)    (unused) headerCheck() vector (or NULL)
+ * @return             0 on success
+ */
+int rpmdbAdd(/*@null@*/ rpmdb db, int iid, Header h, /*@null@*/ rpmts ts,
+               /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, h, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Remove package header from rpm database and indices.
+ * @param db           rpm database
+ * @param rid          (unused) remove transaction id (rid = 0 or -1 to skip)
+ * @param hdrNum       package instance number in database
+ * @param ts           (unused) transaction set (or NULL)
+ * @param (*hdrchk)    (unused) headerCheck() vector (or NULL)
+ * @return             0 on success
+ */
+int rpmdbRemove(/*@null@*/ rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum,
+               /*@null@*/ rpmts ts,
+               /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
+
+/** \ingroup rpmdb
+ * Rebuild database indices from package headers.
+ * @param prefix       path to top of install tree
+ * @param ts           transaction set (or NULL)
+ * @param (*hdrchk)    headerCheck() vector (or NULL)
+ * @return             0 on success
+ */
+int rpmdbRebuild(/*@null@*/ const char * prefix, /*@null@*/ rpmts ts,
+               /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/;
+
 /**
  * Mergesort, same arguments as qsort(2).
  */
@@ -618,4 +1110,5 @@ int mergesort(void *base, size_t nmemb, size_t size,
 }
 #endif
 
+/*@=bounds@*/
 #endif /* H_RPMDB */