Compilation with db3.

CVS patchset: 3642
CVS date: 2000/03/27 11:13:25
This commit is contained in:
jbj 2000-03-27 11:13:25 +00:00
parent 22a911a3b3
commit 62381feb62
7 changed files with 737 additions and 216 deletions

View File

@ -309,6 +309,11 @@ dnl will fail.
AC_CHECK_LIB(port, writev)
DBLIBOBJS=""
dnl Check for Berkeley db3 API.
AC_CHECK_FUNC(db_create, [DBLIBOBJS="$DBLIBOBJS db3.c"],
AC_CHECK_LIB(db, db_create, [LIBS="$LIBS -ldb"; DBLIBOBJS="$DBLIBOBJS db3.c"],
AC_CHECK_LIB(db-3.0, db_create, [DBLIBOBJS="$DBLIBOBJS db3.c"]))
)
dnl Check for Berkeley db2 API.
AC_CHECK_FUNC(db_open, [DBLIBOBJS="$DBLIBOBJS db2.c"],
AC_CHECK_LIB(db, db_open, [LIBS="$LIBS -ldb"; DBLIBOBJS="$DBLIBOBJS db2.c"])
@ -383,7 +388,7 @@ AC_HEADER_MAJOR
AC_HEADER_DIRENT
AC_HEADER_TIME
AC_CHECK_HEADERS(db_185.h db1/db.h)
AC_CHECK_HEADERS(db_185.h db1/db.h db3/db.h)
AC_CHECK_HEADERS(fcntl.h getopt.h grp.h memory.h netdb.h pwd.h utime.h)
AC_CHECK_HEADERS(sys/socket.h sys/select.h)

View File

@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = 1.4 foreign
INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/build -I$(top_srcdir)/popt @INCPATH@
EXTRA_DIST = db0.[ch] db1.[ch] db2.[ch]
EXTRA_DIST = db0.[ch] db1.[ch] db2.[ch] db2.[ch]
pkgincdir = $(pkgincludedir)
pkginc_HEADERS = \
@ -29,6 +29,17 @@ librpm_la_SOURCES = \
librpm_la_LIBADD = $(subst .c,.lo,$(DBLIBOBJS))
#librpm_la_LIBADD = -lpopt
db3.lo: db3.c $(top_srcdir)/system.h rpmlib.h dbindex.h db3.h
$(LIBTOOL) --mode=compile $(COMPILE) -c $<
for F in $*.o $*.lo ; do \
@__LD@ -r -o $${F}.o $${F} -L/usr/lib -ldb-3.0 ; \
@__OBJCOPY@ `\
@__NM@ -g --defined-only $${F}.o | \
sed -e '/ [TWD] /!d' -e 's/.* [TWD] /-L /' | \
grep -v '^-L $*'` $${F}.o $${F} ; \
rm -f $${F}.o ; \
done
db0.lo: db0.c $(top_srcdir)/system.h rpmlib.h dbindex.h db0.h
$(LIBTOOL) --mode=compile $(COMPILE) -c $<
for F in $*.o $*.lo ; do \

409
lib/db2.c
View File

@ -1,6 +1,6 @@
#include "system.h"
static int _debug = 0;
static int _debug = 1;
#include <db.h>
@ -30,71 +30,200 @@ static inline /*@observer@*/ /*@null@*/ DB * GetDB(dbiIndex dbi) {
return ((DB *)dbi->dbi_db);
}
#if defined(__USE_DB2) || defined(__USE_DB3)
#if defined(__USE_DB2)
static int db_init(dbiIndex dbi, const char *home, int dbflags,
DB_ENV **dbenvp, DB_INFO **dbinfop)
static /*@observer@*/ const char * db_strerror(int error)
{
DB_ENV *dbenv = xcalloc(1, sizeof(*dbenv));
DB_INFO *dbinfo = xcalloc(1, sizeof(*dbinfo));
if (error == 0)
return ("Successful return: 0");
if (error > 0)
return (strerror(error));
switch (error) {
case DB_INCOMPLETE:
return ("DB_INCOMPLETE: Cache flush was unable to complete");
case DB_KEYEMPTY:
return ("DB_KEYEMPTY: Non-existent key/data pair");
case DB_KEYEXIST:
return ("DB_KEYEXIST: Key/data pair already exists");
case DB_LOCK_DEADLOCK:
return ("DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock");
case DB_LOCK_NOTGRANTED:
return ("DB_LOCK_NOTGRANTED: Lock not granted");
case DB_NOTFOUND:
return ("DB_NOTFOUND: No matching key/data pair found");
#if defined(__USE_DB3)
case DB_OLD_VERSION:
return ("DB_OLDVERSION: Database requires a version upgrade");
case DB_RUNRECOVERY:
return ("DB_RUNRECOVERY: Fatal error, run database recovery");
#else
case DB_LOCK_NOTHELD:
return ("DB_LOCK_NOTHELD:");
case DB_REGISTERED:
return ("DB_REGISTERED:");
#endif
default:
{
/*
* !!!
* Room for a 64-bit number + slop. This buffer is only used
* if we're given an unknown error, which should never happen.
* Note, however, we're no longer thread-safe if it does.
*/
static char ebuf[40];
(void)snprintf(ebuf, sizeof(ebuf), "Unknown error: %d", error);
return(ebuf);
}
}
/*@notreached@*/
}
static int db_env_create(DB_ENV **dbenvp, int foo)
{
DB_ENV *dbenv;
if (dbenvp == NULL)
return 1;
dbenv = xcalloc(1, sizeof(*dbenv));
*dbenvp = dbenv;
return 0;
}
#endif /* __USE_DB2 */
static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit) {
int rc = 0;
if (error == 0)
rc = 0;
else if (error < 0)
rc = -1;
else if (error > 0)
rc = 1;
if (printit && rc) {
fprintf(stderr, "*** db%d %s rc %d %s\n", dbi->dbi_major, msg,
rc, db_strerror(error));
}
return rc;
}
static int db_init(dbiIndex dbi, const char *home, int dbflags,
DB_ENV **dbenvp, void **dbinfop)
{
DB_ENV *dbenv = NULL;
FILE * dberrfile = stderr;
const char * dberrpfx = "rpmdb";
int dbcachesize = 1024 * 1024;
int dbpagesize = 32 * 1024; /* 0 - 64K */
int rc;
if (dbenvp) *dbenvp = NULL;
if (dbinfop) *dbinfop = NULL;
if (dbenvp == NULL || dbinfop == NULL)
return 1;
dbenv->db_errfile = stderr;
dbenv->db_errpfx = "rpmdb";
dbenv->mp_size = 1024 * 1024;
#define _DBFMASK (DB_CREATE|DB_NOMMAP|DB_THREAD)
rc = db_appinit(home, NULL, dbenv, (dbflags & _DBFMASK));
rc = db_env_create(&dbenv, 0);
rc = cvtdberr(dbi, "db_env_create", rc, _debug);
if (rc)
goto errxit;
/* XXX W2DO? */
dbinfo->db_pagesize = 32 * 1024;
#if defined(__USE_DB3)
{ int xx;
/* dbenv->set_errcall(???) */
xx = dbenv->set_errfile(dberrfile);
xx = cvtdberr(dbi, "dbenv->set_errfile", xx, _debug);
xx = dbenv->set_errpfx(dberrpfx);
xx = cvtdberr(dbi, "dbenv->set_errpfx", xx, _debug);
/* dbenv->set_paniccall(???) */
/* dbenv->set_verbose(???) */
/* dbenv->set_lg_max(???) */
/* dbenv->set_lk_conflicts(???) */
/* dbenv->set_lk_detect(???) */
/* dbenv->set_lk_max(???) */
/* dbenv->set_mp_mmapsize(???) */
xx = dbenv->set_cachesize(dbcachesize);
xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
/* dbenv->set_tx_max(???) */
/* dbenv->set_tx_recover(???) */
}
#else
dbenv->db_errfile = dberrfile;
dbenv->db_errpfx = dberrpfx;
dbenv->mp_size = dbcachesize;
#endif
if (dbenvp)
*dbenvp = dbenv;
else
free(dbenv);
#define _DBFMASK (DB_CREATE|DB_NOMMAP|DB_THREAD)
if (dbinfop)
#if defined(__USE_DB3)
rc = dbenv->open(dbenv, home, NULL, (dbflags & _DBFMASK), 0);
rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
if (rc)
goto errxit;
xx = db->set_pagesize(db, dbpagesize)
xx = cvtdberr(dbi, "db->set_pagesize", xx, _debug);
*dbinfop = NULL;
#else
rc = db_appinit(home, NULL, dbenv, (dbflags & _DBFMASK));
rc = cvtdberr(dbi, "db_appinit", rc, _debug);
if (rc)
goto errxit;
{ DB_INFO * dbinfo = xcalloc(1, sizeof(*dbinfo));
/* XXX W2DO? */
dbinfo->db_pagesize = dbpagesize;
*dbinfop = dbinfo;
else
free(dbinfo);
}
#endif
*dbenvp = dbenv;
return 0;
errxit:
#if defined(__USE_DB3)
if (dbenv) {
xx = dbenv->close(dbenv);
xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
}
#else
if (dbenv) free(dbenv);
if (dbinfo) free(dbinfo);
#endif
return rc;
}
#endif
#endif /* __USE_DB2 || __USE_DB3 */
int db2open(dbiIndex dbi)
{
int rc = 0;
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
char * dbhome = NULL;
DB * db = NULL;
DB_ENV * dbenv = NULL;
DB_INFO * dbinfo = NULL;
void * dbinfo = NULL;
u_int32_t dbflags;
dbflags = ( !(dbi->dbi_flags & O_RDWR) ? DB_RDONLY :
((dbi->dbi_flags & O_CREAT) ? DB_CREATE : 0));
rc = db_init(dbi, dbhome, dbflags, &dbenv, &dbinfo);
if (_debug)
fprintf(stderr, "*** db%d db_init rc %d errno %d\n", dbi->dbi_major, rc, errno);
if (rc == 0) {
#if defined(__USE_DB3)
rc = db_create(&db, dbenv, 0);
rc = cvtdberr(dbi, "db_create", rc, _debug);
if (rc == 0) {
rc = db->open(db, dbi->dbi_file, NULL, dbi_to_dbtype(dbi->dbi_type),
dbflags, dbi->dbi_perms);
rc = cvtdberr(dbi, "db->open", rc, _debug);
}
#else
rc = db_open(dbi->dbi_file, dbi_to_dbtype(dbi->dbi_type), dbflags,
dbi->dbi_perms, dbenv, dbinfo, &db);
if (_debug)
fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db_open", rc, _debug);
#endif /* __USE_DB3 */
}
dbi->dbi_db = db;
@ -104,7 +233,7 @@ fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
#else
dbi->dbi_db = dbopen(dbi->dbi_file, dbi->dbi_flags, dbi->dbi_perms,
dbi_to_dbtype(dbi->dbi_type), dbi->dbi_openinfo);
#endif
#endif /* __USE_DB2 || __USE_DB3 */
if (rc == 0 && dbi->dbi_db != NULL) {
rc = 0;
@ -119,63 +248,43 @@ fprintf(stderr, "*** db%d db_open rc %d errno %d\n", dbi->dbi_major, rc, errno);
int db2close(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
int rc, xx;
int rc = 0, xx;
#if defined(__USE_DB2)
DB_ENV * dbenv = (DB_ENV *)dbi->dbi_dbenv;
DB_INFO * dbinfo = (DB_INFO *)dbi->dbi_dbinfo;
DBC * dbcursor = (DBC *)dbi->dbi_dbcursor;
#if defined(__USE_DB2) || defined(__USE_DB3)
if (dbcursor) {
if (dbi->dbi_dbcursor) {
DBC * dbcursor = (DBC *)dbi->dbi_dbcursor;
xx = dbcursor->c_close(dbcursor);
if (_debug)
fprintf(stderr, "*** db%d db->c_close rc %d errno %d\n", dbi->dbi_major, xx, errno);
xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
dbi->dbi_dbcursor = NULL;
}
rc = db->close(db, 0);
if (_debug)
fprintf(stderr, "*** db%d db->close rc %d errno %d\n", dbi->dbi_major, rc, errno);
dbi->dbi_db = NULL;
if (db) {
rc = db->close(db, 0);
rc = cvtdberr(dbi, "db->close", rc, _debug);
db = dbi->dbi_db = NULL;
}
if (dbinfo) {
free(dbinfo);
if (dbi->dbi_dbinfo) {
free(dbi->dbi_dbinfo);
dbi->dbi_dbinfo = NULL;
}
if (dbenv) {
if (dbi->dbi_dbenv) {
DB_ENV * dbenv = (DB_ENV *)dbi->dbi_dbenv;
#if defined(__USE_DB3)
xx = dbenv->close(dbenv);
xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
#else
xx = db_appexit(dbenv);
if (_debug)
fprintf(stderr, "*** db%d db_appexit rc %d errno %d\n", dbi->dbi_major, xx, errno);
free(dbenv);
xx = cvtdberr(dbi, "db_appexit", xx, _debug);
#endif
free(dbi->dbi_dbenv);
dbi->dbi_dbenv = NULL;
}
#else
rc = db->close(db);
#endif
switch (rc) {
default:
rc = -1;
break;
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0:
rc = 0;
break;
}
return rc;
}
@ -183,37 +292,13 @@ int db2sync(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
int rc;
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->sync(db, flags);
if (_debug)
fprintf(stderr, "*** db%d db->sync rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db->sync", rc, _debug);
#else
rc = db->sync(db, flags);
#endif
switch (rc) {
default:
rc = -1;
break;
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0:
rc = 0;
break;
}
return rc;
}
@ -232,51 +317,28 @@ int db2GetFirstKey(dbiIndex dbi, const char ** keyp) {
key.data = NULL;
key.size = 0;
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
{ DBC * dbcursor = NULL;
rc = db->cursor(db, NULL, &dbcursor);
if (_debug)
fprintf(stderr, "*** db%d db->cursor rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db->cursor", rc, _debug);
if (rc == 0) {
rc = dbcursor->c_get(dbcursor, &key, &data, DB_FIRST);
if (_debug)
fprintf(stderr, "*** db%d dbcursor->c_get rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "dbcursor->c_get", rc, _debug);
}
if (dbcursor) {
xx = dbcursor->c_close(dbcursor);
if (_debug)
fprintf(stderr, "*** db%d dbcursor->c_close rc %d errno %d\n", dbi->dbi_major, xx, errno);
xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
}
}
#else
rc = db->seq(db, &key, &data, R_FIRST);
#endif
switch (rc) {
default:
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0:
rc = 0;
if (keyp) {
char *k = xmalloc(key.size + 1);
memcpy(k, key.data, key.size);
k[key.size] = '\0';
*keyp = k;
}
break;
if (rc == 0 && keyp) {
char *k = xmalloc(key.size + 1);
memcpy(k, key.data, key.size);
k[key.size] = '\0';
*keyp = k;
}
return rc;
@ -296,41 +358,18 @@ int db2SearchIndex(dbiIndex dbi, const char * str, dbiIndexSet * set) {
data.data = NULL;
data.size = 0;
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->get(db, NULL, &key, &data, 0);
if (_debug)
fprintf(stderr, "*** db%d db->get rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db->get", rc, _debug);
#else
rc = db->get(db, &key, &data, 0);
#endif
switch (rc) {
default:
rc = -1;
break;
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0:
rc = 0;
if (set) {
*set = dbiCreateIndexSet();
(*set)->recs = xmalloc(data.size);
memcpy((*set)->recs, data.data, data.size);
(*set)->count = data.size / sizeof(*(*set)->recs);
}
break;
if (rc == 0 && set) {
*set = dbiCreateIndexSet();
(*set)->recs = xmalloc(data.size);
memcpy((*set)->recs, data.data, data.size);
(*set)->count = data.size / sizeof(*(*set)->recs);
}
return rc;
}
@ -352,64 +391,22 @@ int db2UpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set) {
data.data = set->recs;
data.size = set->count * sizeof(*(set->recs));
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->put(db, NULL, &key, &data, 0);
if (_debug)
fprintf(stderr, "*** db%d db->put rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db->put", rc, _debug);
#else
rc = db->put(db, &key, &data, 0);
#endif
switch (rc) {
default:
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0: /* 0 */
rc = 0;
break;
}
} else {
#if defined(__USE_DB2)
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->del(db, NULL, &key, 0);
if (_debug)
fprintf(stderr, "*** db%d db->del rc %d errno %d\n", dbi->dbi_major, rc, errno);
rc = cvtdberr(dbi, "db->del", rc, _debug);
#else
rc = db->del(db, &key, 0);
#endif
switch (rc) {
default:
case DB_INCOMPLETE:
case DB_KEYEMPTY:
case DB_KEYEXIST:
case DB_LOCK_DEADLOCK:
case DB_LOCK_NOTGRANTED:
case DB_LOCK_NOTHELD:
case DB_NOTFOUND:
case DB_DELETED:
case DB_NEEDSPLIT:
case DB_REGISTERED:
case DB_SWAPBYTES:
case DB_TXN_CKP:
rc = 1;
break;
case 0:
rc = 0;
break;
}
}
return rc;

420
lib/db3.c Normal file
View File

@ -0,0 +1,420 @@
#include "system.h"
static int _debug = 1;
#include <db3/db.h>
#include <rpmlib.h>
#include "dbindex.h"
/*@access dbiIndex@*/
/*@access dbiIndexSet@*/
#include "db3.h"
static const char * dberrpfx = "rpmdb";
static int dbcachesize = 1024 * 1024;
static int dbpagesize = 32 * 1024; /* 0 - 64K */
#if DB_VERSION_MAJOR == 3
#define __USE_DB3 1
#define _mymemset(_a, _b, _c) memset((_a), (_b), (_c))
static inline DBTYPE dbi_to_dbtype(DBI_TYPE dbitype)
{
switch(dbitype) {
case DBI_BTREE: return DB_BTREE;
case DBI_HASH: return DB_HASH;
case DBI_RECNO: return DB_RECNO;
}
/*@notreached@*/ return DB_HASH;
}
static inline /*@observer@*/ /*@null@*/ DB * GetDB(dbiIndex dbi) {
return ((DB *)dbi->dbi_db);
}
#if defined(__USE_DB2) || defined(__USE_DB3)
#if defined(__USE_DB2)
static /*@observer@*/ const char * db_strerror(int error)
{
if (error == 0)
return ("Successful return: 0");
if (error > 0)
return (strerror(error));
switch (error) {
case DB_INCOMPLETE:
return ("DB_INCOMPLETE: Cache flush was unable to complete");
case DB_KEYEMPTY:
return ("DB_KEYEMPTY: Non-existent key/data pair");
case DB_KEYEXIST:
return ("DB_KEYEXIST: Key/data pair already exists");
case DB_LOCK_DEADLOCK:
return ("DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock");
case DB_LOCK_NOTGRANTED:
return ("DB_LOCK_NOTGRANTED: Lock not granted");
case DB_NOTFOUND:
return ("DB_NOTFOUND: No matching key/data pair found");
#if defined(__USE_DB3)
case DB_OLD_VERSION:
return ("DB_OLDVERSION: Database requires a version upgrade");
case DB_RUNRECOVERY:
return ("DB_RUNRECOVERY: Fatal error, run database recovery");
#else
case DB_LOCK_NOTHELD:
return ("DB_LOCK_NOTHELD:");
case DB_REGISTERED:
return ("DB_REGISTERED:");
#endif
default:
{
/*
* !!!
* Room for a 64-bit number + slop. This buffer is only used
* if we're given an unknown error, which should never happen.
* Note, however, we're no longer thread-safe if it does.
*/
static char ebuf[40];
(void)snprintf(ebuf, sizeof(ebuf), "Unknown error: %d", error);
return(ebuf);
}
}
/*@notreached@*/
}
static int db_env_create(DB_ENV **dbenvp, int foo)
{
DB_ENV *dbenv;
if (dbenvp == NULL)
return 1;
dbenv = xcalloc(1, sizeof(*dbenv));
*dbenvp = dbenv;
return 0;
}
#endif /* __USE_DB2 */
static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit) {
int rc = 0;
if (error == 0)
rc = 0;
else if (error < 0)
rc = -1;
else if (error > 0)
rc = 1;
if (printit && rc) {
fprintf(stderr, "*** db%d %s rc %d %s\n", dbi->dbi_major, msg,
rc, db_strerror(error));
}
return rc;
}
static int db_init(dbiIndex dbi, const char *home, int dbflags,
DB_ENV **dbenvp, void **dbinfop)
{
DB_ENV *dbenv = NULL;
FILE * dberrfile = stderr;
int rc;
if (dbenvp == NULL || dbinfop == NULL)
return 1;
rc = db_env_create(&dbenv, 0);
rc = cvtdberr(dbi, "db_env_create", rc, _debug);
if (rc)
goto errxit;
#if defined(__USE_DB3)
{ int xx;
/* dbenv->set_errcall(???) */
dbenv->set_errfile(dbenv, dberrfile);
dbenv->set_errpfx(dbenv, dberrpfx);
/* dbenv->set_paniccall(???) */
/* dbenv->set_verbose(???) */
/* dbenv->set_lg_max(???) */
/* dbenv->set_lk_conflicts(???) */
/* dbenv->set_lk_detect(???) */
/* dbenv->set_lk_max(???) */
/* dbenv->set_mp_mmapsize(???) */
xx = dbenv->set_cachesize(dbenv, 0, dbcachesize, 0);
xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
/* dbenv->set_tx_max(???) */
/* dbenv->set_tx_recover(???) */
}
#else
dbenv->db_errfile = dberrfile;
dbenv->db_errpfx = dberrpfx;
dbenv->mp_size = dbcachesize;
#endif
#define _DBFMASK (DB_CREATE|DB_NOMMAP|DB_THREAD)
#if defined(__USE_DB3)
rc = dbenv->open(dbenv, home, NULL, (dbflags & _DBFMASK), 0);
rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
if (rc)
goto errxit;
*dbinfop = NULL;
#else
rc = db_appinit(home, NULL, dbenv, (dbflags & _DBFMASK));
rc = cvtdberr(dbi, "db_appinit", rc, _debug);
if (rc)
goto errxit;
{ DB_INFO * dbinfo = xcalloc(1, sizeof(*dbinfo));
/* XXX W2DO? */
dbinfo->db_pagesize = dbpagesize;
*dbinfop = dbinfo;
}
#endif
*dbenvp = dbenv;
return 0;
errxit:
#if defined(__USE_DB3)
if (dbenv) {
int xx;
xx = dbenv->close(dbenv, 0);
xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
}
#else
if (dbenv) free(dbenv);
#endif
return rc;
}
#endif /* __USE_DB2 || __USE_DB3 */
int db3open(dbiIndex dbi)
{
int rc = 0;
#if defined(__USE_DB2) || defined(__USE_DB3)
char * dbhome = NULL;
DB * db = NULL;
DB_ENV * dbenv = NULL;
void * dbinfo = NULL;
u_int32_t dbflags;
dbflags = ( !(dbi->dbi_flags & O_RDWR) ? DB_RDONLY :
((dbi->dbi_flags & O_CREAT) ? DB_CREATE : 0));
rc = db_init(dbi, dbhome, dbflags, &dbenv, &dbinfo);
if (rc == 0) {
#if defined(__USE_DB3)
rc = db_create(&db, dbenv, 0);
rc = cvtdberr(dbi, "db_create", rc, _debug);
if (rc == 0) {
int xx;
rc = db->open(db, dbi->dbi_file, NULL, dbi_to_dbtype(dbi->dbi_type),
dbflags, dbi->dbi_perms);
rc = cvtdberr(dbi, "db->open", rc, _debug);
xx = db->set_pagesize(db, dbpagesize);
xx = cvtdberr(dbi, "db->set_pagesize", xx, _debug);
}
#else
rc = db_open(dbi->dbi_file, dbi_to_dbtype(dbi->dbi_type), dbflags,
dbi->dbi_perms, dbenv, dbinfo, &db);
rc = cvtdberr(dbi, "db_open", rc, _debug);
#endif /* __USE_DB3 */
}
dbi->dbi_db = db;
dbi->dbi_dbenv = dbenv;
dbi->dbi_dbinfo = dbinfo;
#else
dbi->dbi_db = dbopen(dbi->dbi_file, dbi->dbi_flags, dbi->dbi_perms,
dbi_to_dbtype(dbi->dbi_type), dbi->dbi_openinfo);
#endif /* __USE_DB2 || __USE_DB3 */
if (rc == 0 && dbi->dbi_db != NULL) {
rc = 0;
dbi->dbi_major = DB_VERSION_MAJOR;
dbi->dbi_minor = DB_VERSION_MINOR;
dbi->dbi_patch = DB_VERSION_PATCH;
} else
rc = 1;
return rc;
}
int db3close(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
int rc = 0, xx;
#if defined(__USE_DB2) || defined(__USE_DB3)
if (dbi->dbi_dbcursor) {
DBC * dbcursor = (DBC *)dbi->dbi_dbcursor;
xx = dbcursor->c_close(dbcursor);
xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
dbi->dbi_dbcursor = NULL;
}
if (db) {
rc = db->close(db, 0);
rc = cvtdberr(dbi, "db->close", rc, _debug);
db = dbi->dbi_db = NULL;
}
if (dbi->dbi_dbinfo) {
free(dbi->dbi_dbinfo);
dbi->dbi_dbinfo = NULL;
}
if (dbi->dbi_dbenv) {
DB_ENV * dbenv = (DB_ENV *)dbi->dbi_dbenv;
#if defined(__USE_DB3)
xx = dbenv->close(dbenv, 0);
xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
#else
xx = db_appexit(dbenv);
xx = cvtdberr(dbi, "db_appexit", xx, _debug);
#endif
free(dbi->dbi_dbenv);
dbi->dbi_dbenv = NULL;
}
#else
rc = db->close(db);
#endif
return rc;
}
int db3sync(dbiIndex dbi, unsigned int flags) {
DB * db = GetDB(dbi);
int rc;
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->sync(db, flags);
rc = cvtdberr(dbi, "db->sync", rc, _debug);
#else
rc = db->sync(db, flags);
#endif
return rc;
}
int db3GetFirstKey(dbiIndex dbi, const char ** keyp) {
DBT key, data;
DB * db;
int rc, xx;
if (dbi == NULL || dbi->dbi_db == NULL)
return 1;
db = GetDB(dbi);
_mymemset(&key, 0, sizeof(key));
_mymemset(&data, 0, sizeof(data));
key.data = NULL;
key.size = 0;
#if defined(__USE_DB2) || defined(__USE_DB3)
{ DBC * dbcursor = NULL;
#if defined(__USE_DB3)
rc = db->cursor(db, NULL, &dbcursor, 0);
#else
rc = db->cursor(db, NULL, &dbcursor);
#endif
rc = cvtdberr(dbi, "db->cursor", rc, _debug);
if (rc == 0) {
rc = dbcursor->c_get(dbcursor, &key, &data, DB_FIRST);
rc = cvtdberr(dbi, "dbcursor->c_get", rc, _debug);
}
if (dbcursor) {
xx = dbcursor->c_close(dbcursor);
xx = cvtdberr(dbi, "dbcursor->c_close", xx, _debug);
}
}
#else
rc = db->seq(db, &key, &data, R_FIRST);
#endif
if (rc == 0 && keyp) {
char *k = xmalloc(key.size + 1);
memcpy(k, key.data, key.size);
k[key.size] = '\0';
*keyp = k;
}
return rc;
}
int db3SearchIndex(dbiIndex dbi, const char * str, dbiIndexSet * set) {
DBT key, data;
DB * db = GetDB(dbi);
int rc;
if (set) *set = NULL;
_mymemset(&key, 0, sizeof(key));
_mymemset(&data, 0, sizeof(data));
key.data = (void *)str;
key.size = strlen(str);
data.data = NULL;
data.size = 0;
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->get(db, NULL, &key, &data, 0);
rc = cvtdberr(dbi, "db->get", rc, _debug);
#else
rc = db->get(db, &key, &data, 0);
#endif
if (rc == 0 && set) {
*set = dbiCreateIndexSet();
(*set)->recs = xmalloc(data.size);
memcpy((*set)->recs, data.data, data.size);
(*set)->count = data.size / sizeof(*(*set)->recs);
}
return rc;
}
/*@-compmempass@*/
int db3UpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set) {
DBT key;
DB * db = GetDB(dbi);
int rc;
_mymemset(&key, 0, sizeof(key));
key.data = (void *)str;
key.size = strlen(str);
if (set->count) {
DBT data;
_mymemset(&data, 0, sizeof(data));
data.data = set->recs;
data.size = set->count * sizeof(*(set->recs));
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->put(db, NULL, &key, &data, 0);
rc = cvtdberr(dbi, "db->put", rc, _debug);
#else
rc = db->put(db, &key, &data, 0);
#endif
} else {
#if defined(__USE_DB2) || defined(__USE_DB3)
rc = db->del(db, NULL, &key, 0);
rc = cvtdberr(dbi, "db->del", rc, _debug);
#else
rc = db->del(db, &key, 0);
#endif
}
return rc;
}
/*@=compmempass@*/
#endif /* DB_VERSION_MAJOR == 3 */

64
lib/db3.h Normal file
View File

@ -0,0 +1,64 @@
#ifndef H_DB3
#define H_DB3
/** \file lib/db3.h
* Access RPM indices using Berkeley db-3.x API.
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Return handle for an index database.
* @param dbi index database handle
* @return 0 success 1 fail
*/
int db3open(dbiIndex dbi);
/**
* Close index database.
* @param dbi index database handle
* @param flags
*/
int db3close(dbiIndex dbi, unsigned int flags);
/**
* Flush pending operations to disk.
* @param dbi index database handle
* @param flags
*/
int db3sync(dbiIndex dbi, unsigned int flags);
/**
* Return first index database key.
* @param dbi index database handle
* @param key address of first key
* @return 0 success - fails if rec is not found
*/
int db3GetFirstKey(dbiIndex dbi, const char ** keyp);
/**
* Return items that match criteria.
* @param dbi index database handle
* @param str search key
* @param set items retrieved from index database
* @return -1 error, 0 success, 1 not found
*/
int db3SearchIndex(dbiIndex dbi, const char * str, dbiIndexSet * set);
/**
* Change/delete items that match criteria.
* @param dbi index database handle
* @param str update key
* @param set items to update in index database
* @return 0 success, 1 not found
*/
int db3UpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set);
#ifdef __cplusplus
}
#endif
#endif /* H_DB3 */

View File

@ -13,6 +13,7 @@ static int _debug = 0;
#include "db0.h"
#include "db1.h"
#include "db2.h"
#include "db3.h"
unsigned int dbiIndexSetCount(dbiIndexSet set) {
return set->count;
@ -61,18 +62,26 @@ int _useDbiMajor = -1;
typedef int (*_dbopen) (dbiIndex dbi);
static _dbopen mydbopens[] = {
#if HAVE_DB1_DB_H
db0open,
#else
NULL,
#endif
#if HAVE_DB_185_H
db1open,
#else
NULL,
#endif
db2open,
#if HAVE_DB3_DB_H
db3open,
#else
NULL,
#endif
NULL
};
@ -144,6 +153,9 @@ int dbiCloseIndex(dbiIndex dbi) {
int rc;
switch (dbi->dbi_major) {
case 3:
rc = db3close(dbi, 0);
break;
case 2:
rc = db2close(dbi, 0);
break;
@ -163,6 +175,9 @@ int dbiSyncIndex(dbiIndex dbi) {
int rc;
switch (dbi->dbi_major) {
case 3:
rc = db3sync(dbi, 0);
break;
case 2:
rc = db2sync(dbi, 0);
break;
@ -184,6 +199,9 @@ int dbiGetFirstKey(dbiIndex dbi, const char ** keyp) {
return 1;
switch (dbi->dbi_major) {
case 3:
rc = db3GetFirstKey(dbi, keyp);
break;
case 2:
rc = db2GetFirstKey(dbi, keyp);
break;
@ -202,6 +220,9 @@ int dbiSearchIndex(dbiIndex dbi, const char * str, dbiIndexSet * set) {
int rc;
switch (dbi->dbi_major) {
case 3:
rc = db3SearchIndex(dbi, str, set);
break;
case 2:
rc = db2SearchIndex(dbi, str, set);
break;
@ -227,13 +248,16 @@ int dbiUpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set) {
int rc;
switch (dbi->dbi_major) {
default:
case 3:
rc = db3UpdateIndex(dbi, str, set);
break;
case 2:
rc = db2UpdateIndex(dbi, str, set);
break;
case 1:
rc = db1UpdateIndex(dbi, str, set);
break;
default:
case 0:
rc = db0UpdateIndex(dbi, str, set);
break;

View File

@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2000-03-25 10:18-0500\n"
"POT-Creation-Date: 2000-03-26 19:57-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -1986,12 +1986,12 @@ msgstr ""
msgid " failed - "
msgstr ""
#: lib/dbindex.c:86
#: lib/dbindex.c:95
#, c-format
msgid "bad db file %s"
msgstr ""
#: lib/dbindex.c:121
#: lib/dbindex.c:130
msgid ""
"\n"
"--> Please run \"rpm --rebuilddb\" as root to convert your database from\n"
@ -1999,22 +1999,22 @@ msgid ""
"\n"
msgstr ""
#: lib/dbindex.c:136
#: lib/dbindex.c:145
#, c-format
msgid "cannot open file %s: %s"
msgstr ""
#: lib/dbindex.c:219
#: lib/dbindex.c:240
#, c-format
msgid "error getting record %s from %s"
msgstr ""
#: lib/dbindex.c:244
#: lib/dbindex.c:268
#, c-format
msgid "error storing record %s into %s"
msgstr ""
#: lib/dbindex.c:249
#: lib/dbindex.c:273
#, c-format
msgid "error removing record %s into %s"
msgstr ""