rpmdb/db3.c

Go to the documentation of this file.
00001 /*@-type@*/ /* FIX: annotate db3 methods */
00006 /*@unchecked@*/
00007 static int _debug = 1;  /* XXX if < 0 debugging, > 0 unusual error returns */
00008 
00009 #include "system.h"
00010 
00011 #if defined(HAVE_FTOK) && defined(HAVE_SYS_IPC_H)
00012 #include <sys/ipc.h>
00013 #endif
00014 
00015 #include <rpmlib.h>
00016 #include <rpmmacro.h>
00017 #include <rpmurl.h>     /* XXX urlPath proto */
00018 
00019 #include <rpmdb.h>
00020 
00021 #include "debug.h"
00022 
00023 #if !defined(DB_CLIENT) /* XXX db-4.2.42 retrofit */
00024 #define DB_CLIENT       DB_RPCCLIENT
00025 #endif
00026 
00027 /*@access rpmdb @*/
00028 /*@access dbiIndex @*/
00029 /*@access dbiIndexSet @*/
00030 
00034 /*@-fielduse@*/
00035 struct dbiHStats_s {
00036     unsigned int hash_magic;    
00037     unsigned int hash_version;  
00038     unsigned int hash_nkeys;    
00039     unsigned int hash_ndata;    
00040     unsigned int hash_pagesize; 
00041     unsigned int hash_nelem;    
00042     unsigned int hash_ffactor;  
00043     unsigned int hash_buckets;  
00044     unsigned int hash_free;     
00045     unsigned int hash_bfree;    
00046     unsigned int hash_bigpages; 
00047     unsigned int hash_big_bfree;
00048     unsigned int hash_overflows;
00049     unsigned int hash_ovfl_free;
00050     unsigned int hash_dup;      
00051     unsigned int hash_dup_free; 
00052 };
00053 
00057 struct dbiBStats_s {
00058     unsigned int bt_magic;      
00059     unsigned int bt_version;    
00060     unsigned int bt_nkeys;      
00061     unsigned int bt_ndata;      
00062     unsigned int bt_pagesize;   
00063     unsigned int bt_minkey;     
00064     unsigned int bt_re_len;     
00065     unsigned int bt_re_pad;     
00066     unsigned int bt_levels;     
00067     unsigned int bt_int_pg;     
00068     unsigned int bt_leaf_pg;    
00069     unsigned int bt_dup_pg;     
00070     unsigned int bt_over_pg;    
00071     unsigned int bt_free;       
00072     unsigned int bt_int_pgfree; 
00073     unsigned int bt_leaf_pgfree;
00074     unsigned int bt_dup_pgfree; 
00075     unsigned int bt_over_pgfree;
00076 };
00077 /*@=fielduse@*/
00078 
00079 #ifdef  NOTNOW
00080 static const char * bfstring(unsigned int x, const char * xbf)
00081 {
00082     const char * s = xbf;
00083     static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
00084     static char buf[256];
00085     char * t, * te;
00086     unsigned radix;
00087     unsigned c, i, k;
00088 
00089     radix = (s != NULL ? *s++ : 16);
00090 
00091     if (radix <= 1 || radix >= 32)
00092         radix = 16;
00093 
00094     t = buf;
00095     switch (radix) {
00096     case 8:     *t++ = '0';     break;
00097     case 16:    *t++ = '0';     *t++ = 'x';     break;
00098     }
00099 
00100     i = 0;
00101     k = x;
00102     do { i++; k /= radix; } while (k);
00103 
00104     te = t + i;
00105 
00106     k = x;
00107     do { --i; t[i] = digits[k % radix]; k /= radix; } while (k);
00108 
00109     t = te;
00110     i = '<';
00111     if (s != NULL)
00112     while ((c = *s++) != '\0') {
00113         if (c > ' ') continue;
00114 
00115         k = (1 << (c - 1));
00116         if (!(x & k)) continue;
00117 
00118         if (t == te) *t++ = '=';
00119 
00120         *t++ = i;
00121         i = ',';
00122         while (*s > ' ')
00123             *t++ = *s++;
00124     }
00125     if (t > te) *t++ = '>';
00126     *t = '\0';
00127     return buf;
00128 }
00129 
00130 static const char * dbtFlags =
00131         "\20\1APPMALLOC\2ISSET\3MALLOC\4PARTIAL\5REALLOC\6USERMEM\7DUPOK";
00132 
00133 static const char * dbenvOpenFlags =
00134         "\20\1CREATE\2NO_EXCEPTIONS\3FORCE\4NOMMAP\5RDONLY\6RECOVER\7THREAD\10TXN_NOSYNC\11USE_ENVIRON\12USE_ENVIRON_ROOT\13CDB\14LOCK\15LOG\16MPOOL\17TXN\20JOINENV\21LOCKDOWN\22PRIVATE\23RECOVER_FATAL\24SYSTEM_MEM";
00135 
00136 static const char * dbOpenFlags =
00137         "\20\1CREATE\2NO_EXCEPTIONS\3FORCE\4NOMMAP\5RDONLY\6RECOVER\7THREAD\10TXN_NOSYNC\11USE_ENVIRON\12USE_ENVIRON_ROOT\13EXCL\14FCNTL_LOCKING\15RDWRMASTER\16TRUNCATE\17EXTENT\20APPLY_LOGREG";
00138 
00139 static const char * dbenvSetFlags =
00140         "\20\1CREATE\2NO_EXCEPTIONS\3FORCE\4NOMMAP\5RDONLY\6RECOVER\7THREAD\10TXN_NOSYNC\11USE_ENVIRON\12USE_ENVIRON_ROOT\13CDB_ALLDB\14NOLOCKING\15NOPANIC\16PANIC_ENV\17REGION_INIT\20YIELDCPU";
00141 
00142 static const char * dbSetFlags =
00143         "\20\1DUP\2DUPSORT\3RECNUM\4RENUMBER\5REVSPLITOFF\6SNAPSHOT";
00144 
00145 static const char * dbiModeFlags =
00146         "\20\1WRONLY\2RDWR\7CREAT\10EXCL\11NOCTTY\12TRUNC\13APPEND\14NONBLOCK\15SYNC\16ASYNC\17DIRECT\20LARGEFILE\21DIRECTORY\22NOFOLLOW";
00147 #endif  /* NOTNOW */
00148 
00149 
00150 /*@-globuse -mustmod @*/        /* FIX: rpmError not annotated yet. */
00151 static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit)
00152         /*@globals fileSystem @*/
00153         /*@modifies fileSystem @*/
00154 {
00155     int rc = error;
00156 
00157     if (printit && rc) {
00158         /*@-moduncon@*/ /* FIX: annotate db3 methods */
00159         if (msg)
00160             rpmError(RPMERR_DBERR, _("db%d error(%d) from %s: %s\n"),
00161                 dbi->dbi_api, rc, msg, db_strerror(error));
00162         else
00163             rpmError(RPMERR_DBERR, _("db%d error(%d): %s\n"),
00164                 dbi->dbi_api, rc, db_strerror(error));
00165         /*@=moduncon@*/
00166     }
00167 
00168     return rc;
00169 }
00170 /*@=globuse =mustmod @*/
00171 
00172 static int db_fini(dbiIndex dbi, const char * dbhome,
00173                 /*@null@*/ const char * dbfile,
00174                 /*@unused@*/ /*@null@*/ const char * dbsubfile)
00175         /*@globals fileSystem @*/
00176         /*@modifies fileSystem @*/
00177 {
00178     rpmdb rpmdb = dbi->dbi_rpmdb;
00179     DB_ENV * dbenv = rpmdb->db_dbenv;
00180     int rc;
00181 
00182     if (dbenv == NULL)
00183         return 0;
00184 
00185     rc = dbenv->close(dbenv, 0);
00186     rc = cvtdberr(dbi, "dbenv->close", rc, _debug);
00187 
00188     if (dbfile)
00189         rpmMessage(RPMMESS_DEBUG, _("closed   db environment %s/%s\n"),
00190                         dbhome, dbfile);
00191 
00192     if (rpmdb->db_remove_env) {
00193         int xx;
00194 
00195         /*@-moduncon@*/ /* FIX: annotate db3 methods */
00196         xx = db_env_create(&dbenv, 0);
00197         /*@=moduncon@*/
00198         xx = cvtdberr(dbi, "db_env_create", xx, _debug);
00199 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
00200         xx = dbenv->remove(dbenv, dbhome, 0);
00201 #else
00202         xx = dbenv->remove(dbenv, dbhome, NULL, 0);
00203 #endif
00204         xx = cvtdberr(dbi, "dbenv->remove", xx, _debug);
00205 
00206         if (dbfile)
00207             rpmMessage(RPMMESS_DEBUG, _("removed  db environment %s/%s\n"),
00208                         dbhome, dbfile);
00209 
00210     }
00211     return rc;
00212 }
00213 
00214 static int db3_fsync_disable(/*@unused@*/ int fd)
00215         /*@*/
00216 {
00217     return 0;
00218 }
00219 
00220 #if 0
00221 #if HAVE_LIBPTHREAD
00222 #if HAVE_PTHREAD_H
00223 #include <pthread.h>
00224 #endif
00225 
00230 static int db3_pthread_nptl(void)
00231         /*@*/
00232 {
00233     pthread_mutex_t mutex;
00234     pthread_mutexattr_t mutexattr, *mutexattrp = NULL;
00235     pthread_cond_t cond;
00236     pthread_condattr_t condattr, *condattrp = NULL;
00237     int ret = 0;
00238 
00239     ret = pthread_mutexattr_init(&mutexattr);
00240     if (ret == 0) {
00241         ret = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
00242         mutexattrp = &mutexattr;
00243     }
00244 
00245     if (ret == 0)
00246         ret = pthread_mutex_init(&mutex, mutexattrp);
00247     if (mutexattrp != NULL)
00248         pthread_mutexattr_destroy(mutexattrp);
00249     if (ret)
00250         return ret;
00251     (void) pthread_mutex_destroy(&mutex);
00252 
00253     ret = pthread_condattr_init(&condattr);
00254     if (ret == 0) {
00255         ret = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED);
00256         condattrp = &condattr;
00257     }
00258 
00259     if (ret == 0)
00260         ret = pthread_cond_init(&cond, condattrp);
00261 
00262     if (condattrp != NULL)
00263         (void)pthread_condattr_destroy(condattrp);
00264     if (ret == 0)
00265         (void) pthread_cond_destroy(&cond);
00266     return ret;
00267 }
00268 #endif
00269 #endif
00270 
00271 /*@-moduncon@*/ /* FIX: annotate db3 methods */
00272 static int db_init(dbiIndex dbi, const char * dbhome,
00273                 /*@null@*/ const char * dbfile,
00274                 /*@unused@*/ /*@null@*/ const char * dbsubfile,
00275                 /*@out@*/ DB_ENV ** dbenvp)
00276         /*@globals rpmGlobalMacroContext, h_errno,
00277                 fileSystem @*/
00278         /*@modifies dbi, *dbenvp, fileSystem @*/
00279 {
00280     rpmdb rpmdb = dbi->dbi_rpmdb;
00281     DB_ENV *dbenv = NULL;
00282     int eflags;
00283     int rc;
00284 
00285     if (dbenvp == NULL)
00286         return 1;
00287 
00288     /* XXX HACK */
00289     /*@-assignexpose@*/
00290     if (rpmdb->db_errfile == NULL)
00291         rpmdb->db_errfile = stderr;
00292     /*@=assignexpose@*/
00293 
00294     eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
00295     if (eflags & DB_JOINENV) eflags &= DB_JOINENV;
00296 
00297     if (dbfile)
00298         rpmMessage(RPMMESS_DEBUG, _("opening  db environment %s/%s %s\n"),
00299                 dbhome, dbfile, prDbiOpenFlags(eflags, 1));
00300 
00301     /* XXX Can't do RPC w/o host. */
00302     if (dbi->dbi_host == NULL)
00303         dbi->dbi_ecflags &= ~DB_CLIENT;
00304 
00305     /* XXX Set a default shm_key. */
00306     if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
00307 #if defined(HAVE_FTOK)
00308         dbi->dbi_shmkey = ftok(dbhome, 0);
00309 #else
00310         dbi->dbi_shmkey = 0x44631380;
00311 #endif
00312     }
00313 
00314     rc = db_env_create(&dbenv, dbi->dbi_ecflags);
00315     rc = cvtdberr(dbi, "db_env_create", rc, _debug);
00316     if (dbenv == NULL || rc)
00317         goto errxit;
00318 
00319   { int xx;
00320     /*@-noeffectuncon@*/ /* FIX: annotate db3 methods */
00321 
00322  /* 4.1: dbenv->set_app_dispatch(???) */
00323  /* 4.1: dbenv->set_alloc(???) */
00324  /* 4.1: dbenv->set_data_dir(???) */
00325  /* 4.1: dbenv->set_encrypt(???) */
00326 
00327     dbenv->set_errcall(dbenv, rpmdb->db_errcall);
00328     dbenv->set_errfile(dbenv, rpmdb->db_errfile);
00329     dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
00330     /*@=noeffectuncon@*/
00331 
00332  /* 4.1: dbenv->set_feedback(???) */
00333  /* 4.1: dbenv->set_flags(???) */
00334 
00335  /* dbenv->set_paniccall(???) */
00336 
00337     if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
00338         const char * home;
00339         int retry = 0;
00340 
00341         if ((home = strrchr(dbhome, '/')) != NULL)
00342             dbhome = ++home;
00343 
00344         while (retry++ < 5) {
00345 /* XXX 3.3.4 change. */
00346 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
00347             xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
00348                 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
00349             xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
00350 #else
00351             xx = dbenv->set_server(dbenv, dbi->dbi_host,
00352                 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
00353             xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
00354 #endif
00355             if (!xx)
00356                 break;
00357             (void) sleep(15);
00358         }
00359     } else {
00360 #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
00361         xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
00362                 (dbi->dbi_verbose & DB_VERB_CHKPOINT));
00363 #endif
00364         xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
00365                 (dbi->dbi_verbose & DB_VERB_DEADLOCK));
00366         xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
00367                 (dbi->dbi_verbose & DB_VERB_RECOVERY));
00368         xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
00369                 (dbi->dbi_verbose & DB_VERB_WAITSFOR));
00370 
00371         if (dbi->dbi_mmapsize) {
00372             xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize);
00373             xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug);
00374         }
00375         if (dbi->dbi_tmpdir) {
00376             const char * root;
00377             const char * tmpdir;
00378 
00379             root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
00380 /*@-boundsread@*/
00381             if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
00382                 root = NULL;
00383 /*@=boundsread@*/
00384 /*@-mods@*/
00385             tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
00386 /*@=mods@*/
00387             xx = dbenv->set_tmp_dir(dbenv, tmpdir);
00388             xx = cvtdberr(dbi, "dbenv->set_tmp_dir", xx, _debug);
00389             tmpdir = _free(tmpdir);
00390         }
00391     }
00392 
00393  /* dbenv->set_lk_conflicts(???) */
00394  /* dbenv->set_lk_detect(???) */
00395  /* 4.1: dbenv->set_lk_max_lockers(???) */
00396  /* 4.1: dbenv->set_lk_max_locks(???) */
00397  /* 4.1: dbenv->set_lk_max_objects(???) */
00398 
00399  /* 4.1: dbenv->set_lg_bsize(???) */
00400  /* 4.1: dbenv->set_lg_dir(???) */
00401  /* 4.1: dbenv->set_lg_max(???) */
00402  /* 4.1: dbenv->set_lg_regionmax(???) */
00403 
00404     if (dbi->dbi_cachesize) {
00405         xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_cachesize, 0);
00406         xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
00407     }
00408 
00409  /* 4.1 dbenv->set_timeout(???) */
00410  /* dbenv->set_tx_max(???) */
00411  /* 4.1: dbenv->set_tx_timestamp(???) */
00412  /* dbenv->set_tx_recover(???) */
00413 
00414  /* dbenv->set_rep_transport(???) */
00415  /* dbenv->set_rep_limit(???) */
00416 
00417     if (dbi->dbi_no_fsync) {
00418 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
00419         xx = db_env_set_func_fsync(db3_fsync_disable);
00420 #else
00421         xx = dbenv->set_func_fsync(dbenv, db3_fsync_disable);
00422 #endif
00423         xx = cvtdberr(dbi, "db_env_set_func_fsync", xx, _debug);
00424     }
00425 
00426     if (dbi->dbi_shmkey) {
00427         xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey);
00428         xx = cvtdberr(dbi, "dbenv->set_shm_key", xx, _debug);
00429     }
00430   }
00431 
00432 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
00433     rc = (dbenv->open)(dbenv, dbhome, eflags, dbi->dbi_perms);
00434 #else
00435     rc = (dbenv->open)(dbenv, dbhome, NULL, eflags, dbi->dbi_perms);
00436 #endif
00437     rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
00438     if (rc)
00439         goto errxit;
00440 
00441 /*@-boundswrite@*/
00442     *dbenvp = dbenv;
00443 /*@=boundswrite@*/
00444 
00445     return 0;
00446 
00447 errxit:
00448     if (dbenv) {
00449         int xx;
00450         xx = dbenv->close(dbenv, 0);
00451         xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
00452     }
00453     return rc;
00454 }
00455 /*@=moduncon@*/
00456 
00457 static int db3sync(dbiIndex dbi, unsigned int flags)
00458         /*@globals fileSystem @*/
00459         /*@modifies fileSystem @*/
00460 {
00461     DB * db = dbi->dbi_db;
00462     int rc = 0;
00463     int _printit;
00464 
00465     if (db != NULL)
00466         rc = db->sync(db, flags);
00467     /* XXX DB_INCOMPLETE is returned occaisionally with multiple access. */
00468 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
00469     _printit = _debug;
00470 #else
00471     _printit = (rc == DB_INCOMPLETE ? 0 : _debug);
00472 #endif
00473     rc = cvtdberr(dbi, "db->sync", rc, _printit);
00474     return rc;
00475 }
00476 
00477 static int db3cdup(dbiIndex dbi, DBC * dbcursor, DBC ** dbcp,
00478                 unsigned int flags)
00479         /*@globals fileSystem @*/
00480         /*@modifies *dbcp, fileSystem @*/
00481 {
00482     int rc;
00483 
00484 /*@-boundswrite@*/
00485     if (dbcp) *dbcp = NULL;
00486 /*@=boundswrite@*/
00487     rc = dbcursor->c_dup(dbcursor, dbcp, flags);
00488     rc = cvtdberr(dbi, "dbcursor->c_dup", rc, _debug);
00489     /*@-nullstate @*/ /* FIX: *dbcp can be NULL */
00490     return rc;
00491     /*@=nullstate @*/
00492 }
00493 
00494 /*@-mustmod@*/
00495 static int db3cclose(dbiIndex dbi, /*@only@*/ /*@null@*/ DBC * dbcursor,
00496                 /*@unused@*/ unsigned int flags)
00497         /*@globals fileSystem @*/
00498         /*@modifies dbi, fileSystem @*/
00499 {
00500     int rc = -2;
00501 
00502     /* XXX db3copen error pathways come through here. */
00503     if (dbcursor != NULL) {
00504         rc = dbcursor->c_close(dbcursor);
00505         rc = cvtdberr(dbi, "dbcursor->c_close", rc, _debug);
00506     }
00507     return rc;
00508 }
00509 /*@=mustmod@*/
00510 
00511 static int db3copen(dbiIndex dbi, DB_TXN * txnid,
00512                 /*@null@*/ /*@out@*/ DBC ** dbcp, unsigned int dbiflags)
00513         /*@globals fileSystem @*/
00514         /*@modifies dbi, *dbcp, fileSystem @*/
00515 {
00516     DB * db = dbi->dbi_db;
00517     DBC * dbcursor = NULL;
00518     int flags;
00519     int rc;
00520 
00521    /* XXX DB_WRITECURSOR cannot be used with sunrpc dbenv. */
00522     assert(db != NULL);
00523     if ((dbiflags & DB_WRITECURSOR) &&
00524         (dbi->dbi_eflags & DB_INIT_CDB) && !(dbi->dbi_oflags & DB_RDONLY))
00525     {
00526         flags = DB_WRITECURSOR;
00527     } else
00528         flags = 0;
00529 
00530     rc = db->cursor(db, txnid, &dbcursor, flags);
00531     rc = cvtdberr(dbi, "db->cursor", rc, _debug);
00532 
00533     if (dbcp)
00534         /*@-boundswrite -onlytrans@*/ *dbcp = dbcursor; /*@=boundswrite =onlytrans@*/
00535     else
00536         (void) db3cclose(dbi, dbcursor, 0);
00537 
00538     return rc;
00539 }
00540 
00541 static int db3cput(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
00542                 /*@unused@*/ unsigned int flags)
00543         /*@globals fileSystem @*/
00544         /*@modifies fileSystem @*/
00545 {
00546     DB * db = dbi->dbi_db;
00547     int rc;
00548 
00549     assert(db != NULL);
00550     if (dbcursor == NULL) {
00551         rc = db->put(db, dbi->dbi_txnid, key, data, 0);
00552         rc = cvtdberr(dbi, "db->put", rc, _debug);
00553     } else {
00554         rc = dbcursor->c_put(dbcursor, key, data, DB_KEYLAST);
00555         rc = cvtdberr(dbi, "dbcursor->c_put", rc, _debug);
00556     }
00557 
00558     return rc;
00559 }
00560 
00561 /*@-mustmod@*/
00562 static int db3cdel(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
00563                 unsigned int flags)
00564         /*@globals fileSystem @*/
00565         /*@modifies *dbcursor, fileSystem @*/
00566 {
00567     DB * db = dbi->dbi_db;
00568     int rc;
00569 
00570     assert(db != NULL);
00571     if (dbcursor == NULL) {
00572         rc = db->del(db, dbi->dbi_txnid, key, flags);
00573         rc = cvtdberr(dbi, "db->del", rc, _debug);
00574     } else {
00575         int _printit;
00576 
00577         /* XXX TODO: insure that cursor is positioned with duplicates */
00578         rc = dbcursor->c_get(dbcursor, key, data, DB_SET);
00579         /* XXX DB_NOTFOUND can be returned */
00580         _printit = (rc == DB_NOTFOUND ? 0 : _debug);
00581         rc = cvtdberr(dbi, "dbcursor->c_get", rc, _printit);
00582 
00583         if (rc == 0) {
00584             rc = dbcursor->c_del(dbcursor, flags);
00585             rc = cvtdberr(dbi, "dbcursor->c_del", rc, _debug);
00586         }
00587     }
00588 
00589     return rc;
00590 }
00591 /*@=mustmod@*/
00592 
00593 /*@-mustmod@*/
00594 static int db3cget(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
00595                 unsigned int flags)
00596         /*@globals fileSystem @*/
00597         /*@modifies *dbcursor, *key, *data, fileSystem @*/
00598 {
00599     DB * db = dbi->dbi_db;
00600     int _printit;
00601     int rc;
00602 
00603     assert(db != NULL);
00604     if (dbcursor == NULL) {
00605         /* XXX duplicates require cursors. */
00606         rc = db->get(db, dbi->dbi_txnid, key, data, 0);
00607         /* XXX DB_NOTFOUND can be returned */
00608         _printit = (rc == DB_NOTFOUND ? 0 : _debug);
00609         rc = cvtdberr(dbi, "db->get", rc, _printit);
00610     } else {
00611         /* XXX db3 does DB_FIRST on uninitialized cursor */
00612         rc = dbcursor->c_get(dbcursor, key, data, flags);
00613         /* XXX DB_NOTFOUND can be returned */
00614         _printit = (rc == DB_NOTFOUND ? 0 : _debug);
00615         rc = cvtdberr(dbi, "dbcursor->c_get", rc, _printit);
00616     }
00617 
00618     return rc;
00619 }
00620 /*@=mustmod@*/
00621 
00622 /*@-mustmod@*/
00623 static int db3cpget(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * pkey,
00624                 DBT * data, unsigned int flags)
00625         /*@globals fileSystem @*/
00626         /*@modifies *dbcursor, *key, *data, fileSystem @*/
00627 {
00628     DB * db = dbi->dbi_db;
00629     int _printit;
00630     int rc;
00631 
00632     assert(db != NULL);
00633     assert(dbcursor != NULL);
00634 
00635     /* XXX db3 does DB_FIRST on uninitialized cursor */
00636     rc = dbcursor->c_pget(dbcursor, key, pkey, data, flags);
00637     /* XXX DB_NOTFOUND can be returned */
00638     _printit = (rc == DB_NOTFOUND ? 0 : _debug);
00639     rc = cvtdberr(dbi, "dbcursor->c_pget", rc, _printit);
00640 
00641     return rc;
00642 }
00643 /*@=mustmod@*/
00644 
00645 static int db3ccount(dbiIndex dbi, DBC * dbcursor,
00646                 /*@null@*/ /*@out@*/ unsigned int * countp,
00647                 /*@unused@*/ unsigned int flags)
00648         /*@globals fileSystem @*/
00649         /*@modifies *countp, fileSystem @*/
00650 {
00651     db_recno_t count = 0;
00652     int rc = 0;
00653 
00654     flags = 0;
00655     rc = dbcursor->c_count(dbcursor, &count, flags);
00656     rc = cvtdberr(dbi, "dbcursor->c_count", rc, _debug);
00657     if (rc) return rc;
00658 /*@-boundswrite@*/
00659     if (countp) *countp = count;
00660 /*@=boundswrite@*/
00661 
00662     return rc;
00663 }
00664 
00665 static int db3byteswapped(dbiIndex dbi) /*@*/
00666 {
00667     DB * db = dbi->dbi_db;
00668     int rc = 0;
00669 
00670     if (db != NULL) {
00671 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11) \
00672  || (DB_VERSION_MAJOR == 4)
00673         int isswapped = 0;
00674         rc = db->get_byteswapped(db, &isswapped);
00675         if (rc == 0)
00676             rc = isswapped;
00677 #else
00678         rc = db->get_byteswapped(db);
00679 #endif
00680     }
00681 
00682     return rc;
00683 }
00684 
00685 static int db3stat(dbiIndex dbi, unsigned int flags)
00686         /*@globals fileSystem @*/
00687         /*@modifies dbi, fileSystem @*/
00688 {
00689     DB * db = dbi->dbi_db;
00690 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
00691     DB_TXN * txnid = NULL;
00692 #endif
00693     int rc = 0;
00694 
00695     assert(db != NULL);
00696 #if defined(DB_FAST_STAT)
00697     if (flags)
00698         flags = DB_FAST_STAT;
00699     else
00700 #endif
00701         flags = 0;
00702     dbi->dbi_stats = _free(dbi->dbi_stats);
00703 /* XXX 3.3.4 change. */
00704 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
00705 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
00706     rc = db->stat(db, txnid, &dbi->dbi_stats, flags);
00707 #else
00708     rc = db->stat(db, &dbi->dbi_stats, flags);
00709 #endif
00710 #else
00711     rc = db->stat(db, &dbi->dbi_stats, NULL, flags);
00712 #endif
00713     rc = cvtdberr(dbi, "db->stat", rc, _debug);
00714     return rc;
00715 }
00716 
00717 /*@-mustmod@*/
00718 static int db3associate(dbiIndex dbi, dbiIndex dbisecondary,
00719                 int (*callback)(DB *, const DBT *, const DBT *, DBT *),
00720                 unsigned int flags)
00721         /*@globals fileSystem @*/
00722         /*@modifies dbi, fileSystem @*/
00723 {
00724     DB * db = dbi->dbi_db;
00725     DB * secondary = dbisecondary->dbi_db;
00726     int rc;
00727 
00728 /*@-moduncon@*/ /* FIX: annotate db3 methods */
00729 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
00730     DB_TXN * txnid = NULL;
00731 
00732 assert(db != NULL);
00733     rc = db->associate(db, txnid, secondary, callback, flags);
00734 #else
00735 assert(db != NULL);
00736     rc = db->associate(db, secondary, callback, flags);
00737 #endif
00738 /*@=moduncon@*/
00739     rc = cvtdberr(dbi, "db->associate", rc, _debug);
00740     return rc;
00741 }
00742 /*@=mustmod@*/
00743 
00744 /*@-mustmod@*/
00745 static int db3join(dbiIndex dbi, DBC ** curslist, DBC ** dbcp,
00746                 unsigned int flags)
00747         /*@globals fileSystem @*/
00748         /*@modifies dbi, fileSystem @*/
00749 {
00750     DB * db = dbi->dbi_db;
00751     int rc;
00752 
00753 assert(db != NULL);
00754 /*@-moduncon@*/ /* FIX: annotate db3 methods */
00755     rc = db->join(db, curslist, dbcp, flags);
00756 /*@=moduncon@*/
00757     rc = cvtdberr(dbi, "db->join", rc, _debug);
00758     return rc;
00759 }
00760 /*@=mustmod@*/
00761 
00762 /*@-moduncon@*/ /* FIX: annotate db3 methods */
00763 static int db3close(/*@only@*/ dbiIndex dbi, /*@unused@*/ unsigned int flags)
00764         /*@globals rpmGlobalMacroContext, h_errno,
00765                 fileSystem @*/
00766         /*@modifies dbi, fileSystem @*/
00767 {
00768     rpmdb rpmdb = dbi->dbi_rpmdb;
00769     const char * urlfn = NULL;
00770     const char * root;
00771     const char * home;
00772     const char * dbhome;
00773     const char * dbfile;
00774     const char * dbsubfile;
00775     DB * db = dbi->dbi_db;
00776     int _printit;
00777     int rc = 0, xx;
00778 
00779     flags = 0;  /* XXX unused */
00780 
00781     /*
00782      * Get the prefix/root component and directory path.
00783      */
00784     root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
00785 /*@-boundsread@*/
00786     if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
00787         root = NULL;
00788 /*@=boundsread@*/
00789     home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
00790 
00791     /*
00792      * Either the root or directory components may be a URL. Concatenate,
00793      * convert the URL to a path, and add the name of the file.
00794      */
00795     /*@-mods@*/
00796     urlfn = rpmGenPath(root, home, NULL);
00797     /*@=mods@*/
00798     (void) urlPath(urlfn, &dbhome);
00799     if (dbi->dbi_temporary) {
00800         dbfile = NULL;
00801         dbsubfile = NULL;
00802     } else {
00803 #ifdef  HACK    /* XXX necessary to support dbsubfile */
00804         dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
00805         dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : tagName(dbi->dbi_rpmtag));
00806 #else
00807         dbfile = (dbi->dbi_file ? dbi->dbi_file : tagName(dbi->dbi_rpmtag));
00808         dbsubfile = NULL;
00809 #endif
00810     }
00811 
00812     if (db) {
00813         rc = db->close(db, 0);
00814         /* XXX ignore not found error messages. */
00815         _printit = (rc == ENOENT ? 0 : _debug);
00816         rc = cvtdberr(dbi, "db->close", rc, _printit);
00817         db = dbi->dbi_db = NULL;
00818 
00819         rpmMessage(RPMMESS_DEBUG, _("closed   db index       %s/%s\n"),
00820                 dbhome, (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)));
00821 
00822     }
00823 
00824     if (rpmdb->db_dbenv != NULL && dbi->dbi_use_dbenv) {
00825         if (rpmdb->db_opens == 1) {
00826             /*@-nullstate@*/
00827             xx = db_fini(dbi, (dbhome ? dbhome : ""), dbfile, dbsubfile);
00828             /*@=nullstate@*/
00829             rpmdb->db_dbenv = NULL;
00830         }
00831         rpmdb->db_opens--;
00832     }
00833 
00834     if (dbi->dbi_verify_on_close && !dbi->dbi_temporary) {
00835         DB_ENV * dbenv = NULL;
00836 
00837         /*@-moduncon@*/ /* FIX: annotate db3 methods */
00838         rc = db_env_create(&dbenv, 0);
00839         /*@=moduncon@*/
00840         rc = cvtdberr(dbi, "db_env_create", rc, _debug);
00841         if (rc || dbenv == NULL) goto exit;
00842 
00843         /*@-noeffectuncon@*/ /* FIX: annotate db3 methods */
00844         dbenv->set_errcall(dbenv, rpmdb->db_errcall);
00845         dbenv->set_errfile(dbenv, rpmdb->db_errfile);
00846         dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
00847  /*     dbenv->set_paniccall(???) */
00848         /*@=noeffectuncon@*/
00849 #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
00850         xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
00851                 (dbi->dbi_verbose & DB_VERB_CHKPOINT));
00852 #endif
00853         xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
00854                 (dbi->dbi_verbose & DB_VERB_DEADLOCK));
00855         xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
00856                 (dbi->dbi_verbose & DB_VERB_RECOVERY));
00857         xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
00858                 (dbi->dbi_verbose & DB_VERB_WAITSFOR));
00859 
00860         if (dbi->dbi_tmpdir) {
00861             /*@-mods@*/
00862             const char * tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
00863             /*@=mods@*/
00864             rc = dbenv->set_tmp_dir(dbenv, tmpdir);
00865             rc = cvtdberr(dbi, "dbenv->set_tmp_dir", rc, _debug);
00866             tmpdir = _free(tmpdir);
00867             if (rc) goto exit;
00868         }
00869             
00870         rc = (dbenv->open)(dbenv, dbhome,
00871             DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
00872         rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
00873         if (rc) goto exit;
00874 
00875         /*@-moduncon -nullstate@*/ /* FIX: annotate db3 methods */
00876         rc = db_create(&db, dbenv, 0);
00877         /*@=moduncon =nullstate@*/
00878         rc = cvtdberr(dbi, "db_create", rc, _debug);
00879 
00880         if (db != NULL) {
00881                 /*@-mods@*/
00882                 const char * dbf = rpmGetPath(dbhome, "/", dbfile, NULL);
00883                 /*@=mods@*/
00884 
00885                 rc = db->verify(db, dbf, NULL, NULL, flags);
00886                 rc = cvtdberr(dbi, "db->verify", rc, _debug);
00887 
00888                 rpmMessage(RPMMESS_DEBUG, _("verified db index       %s/%s\n"),
00889                         (dbhome ? dbhome : ""),
00890                         (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)));
00891 
00892                 /*
00893                  * The DB handle may not be accessed again after
00894                  * DB->verify is called, regardless of its return.
00895                  */
00896                 db = NULL;
00897                 dbf = _free(dbf);
00898         }
00899         xx = dbenv->close(dbenv, 0);
00900         xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
00901         if (rc == 0 && xx) rc = xx;
00902     }
00903 
00904 exit:
00905     dbi->dbi_db = NULL;
00906 
00907     urlfn = _free(urlfn);
00908 
00909     dbi = db3Free(dbi);
00910 
00911     return rc;
00912 }
00913 /*@=moduncon@*/
00914 
00915 static int db3open(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip)
00916         /*@globals rpmGlobalMacroContext, h_errno,
00917                 fileSystem, internalState @*/
00918         /*@modifies *dbip, fileSystem, internalState @*/
00919 {
00920     /*@-nestedextern -shadow@*/
00921     extern struct _dbiVec db3vec;
00922     /*@=nestedextern =shadow@*/
00923     const char * urlfn = NULL;
00924     const char * root;
00925     const char * home;
00926     const char * dbhome;
00927     const char * dbfile;
00928     const char * dbsubfile;
00929     dbiIndex dbi = NULL;
00930     int rc = 0;
00931     int xx;
00932 
00933     DB * db = NULL;
00934     DB_ENV * dbenv = NULL;
00935 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
00936     DB_TXN * txnid = NULL;
00937 #endif
00938     u_int32_t oflags;
00939     int _printit;
00940 
00941 /*@-boundswrite@*/
00942     if (dbip)
00943         *dbip = NULL;
00944 /*@=boundswrite@*/
00945 
00946     /*
00947      * Parse db configuration parameters.
00948      */
00949     /*@-mods@*/
00950     if ((dbi = db3New(rpmdb, rpmtag)) == NULL)
00951         /*@-nullstate@*/
00952         return 1;
00953         /*@=nullstate@*/
00954     /*@=mods@*/
00955     dbi->dbi_api = DB_VERSION_MAJOR;
00956 
00957     /*
00958      * Get the prefix/root component and directory path.
00959      */
00960     root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
00961 /*@-boundsread@*/
00962     if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
00963         root = NULL;
00964 /*@=boundsread@*/
00965     home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
00966 
00967     /*
00968      * Either the root or directory components may be a URL. Concatenate,
00969      * convert the URL to a path, and add the name of the file.
00970      */
00971     /*@-mods@*/
00972     urlfn = rpmGenPath(root, home, NULL);
00973     /*@=mods@*/
00974     (void) urlPath(urlfn, &dbhome);
00975     if (dbi->dbi_temporary) {
00976         dbfile = NULL;
00977         dbsubfile = NULL;
00978     } else {
00979 #ifdef  HACK    /* XXX necessary to support dbsubfile */
00980         dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
00981         dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : tagName(dbi->dbi_rpmtag));
00982 #else
00983         dbfile = (dbi->dbi_file ? dbi->dbi_file : tagName(dbi->dbi_rpmtag));
00984         dbsubfile = NULL;
00985 #endif
00986     }
00987 
00988     oflags = (dbi->dbi_oeflags | dbi->dbi_oflags);
00989     oflags &= ~DB_TRUNCATE;     /* XXX this is dangerous */
00990 
00991 #if 0   /* XXX rpmdb: illegal flag combination specified to DB->open */
00992     if ( dbi->dbi_mode & O_EXCL) oflags |= DB_EXCL;
00993 #endif
00994 
00995     /*
00996      * Map open mode flags onto configured database/environment flags.
00997      */
00998     if (dbi->dbi_temporary) {
00999         oflags |= DB_CREATE;
01000         dbi->dbi_oeflags |= DB_CREATE;
01001         oflags &= ~DB_RDONLY;
01002         dbi->dbi_oflags &= ~DB_RDONLY;
01003     } else {
01004         if (!(dbi->dbi_mode & (O_RDWR|O_WRONLY))) oflags |= DB_RDONLY;
01005         if (dbi->dbi_mode & O_CREAT) {
01006             oflags |= DB_CREATE;
01007             dbi->dbi_oeflags |= DB_CREATE;
01008         }
01009 #ifdef  DANGEROUS
01010         if ( dbi->dbi_mode & O_TRUNC) oflags |= DB_TRUNCATE;
01011 #endif
01012     }
01013 
01014     /*
01015      * Create the /var/lib/rpm directory if it doesn't exist (root only).
01016      */
01017     (void) rpmioMkpath(dbhome, 0755, getuid(), getgid());
01018 
01019     /*
01020      * Avoid incompatible DB_CREATE/DB_RDONLY flags on DBENV->open.
01021      */
01022     if (dbi->dbi_use_dbenv) {
01023 
01024 #if 0
01025 #if HAVE_LIBPTHREAD
01026         if (rpmdb->db_dbenv == NULL) {
01027             /* Set DB_PRIVATE if posix mutexes are not shared. */
01028             xx = db3_pthread_nptl();
01029             if (xx) {
01030                 dbi->dbi_eflags |= DB_PRIVATE;
01031                 rpmMessage(RPMMESS_DEBUG, _("unshared posix mutexes found(%d), adding DB_PRIVATE, using fcntl lock\n"), xx);
01032             }
01033         }
01034 #endif
01035 #endif
01036 
01037         if (access(dbhome, W_OK) == -1) {
01038 
01039             /* dbhome is unwritable, don't attempt DB_CREATE on DB->open ... */
01040             oflags &= ~DB_CREATE;
01041 
01042             /* ... but DBENV->open might still need DB_CREATE ... */
01043             if (dbi->dbi_eflags & DB_PRIVATE) {
01044                 dbi->dbi_eflags &= ~DB_JOINENV;
01045             } else {
01046                 dbi->dbi_eflags |= DB_JOINENV;
01047                 dbi->dbi_oeflags &= ~DB_CREATE;
01048                 dbi->dbi_oeflags &= ~DB_THREAD;
01049                 /* ... but, unless DB_PRIVATE is used, skip DBENV. */
01050                 dbi->dbi_use_dbenv = 0;
01051             }
01052 
01053             /* ... DB_RDONLY maps dbhome perms across files ...  */
01054             if (dbi->dbi_temporary) {
01055                 oflags |= DB_CREATE;
01056                 dbi->dbi_oeflags |= DB_CREATE;
01057                 oflags &= ~DB_RDONLY;
01058                 dbi->dbi_oflags &= ~DB_RDONLY;
01059             } else {
01060                 oflags |= DB_RDONLY;
01061                 /* ... and DB_WRITECURSOR won't be needed ...  */
01062                 dbi->dbi_oflags |= DB_RDONLY;
01063             }
01064 
01065         } else {        /* dbhome is writable, check for persistent dbenv. */
01066             /*@-mods@*/
01067             const char * dbf = rpmGetPath(dbhome, "/__db.001", NULL);
01068             /*@=mods@*/
01069 
01070             if (access(dbf, F_OK) == -1) {
01071                 /* ... non-existent (or unwritable) DBENV, will create ... */
01072                 dbi->dbi_oeflags |= DB_CREATE;
01073                 dbi->dbi_eflags &= ~DB_JOINENV;
01074             } else {
01075                 /* ... pre-existent (or bogus) DBENV, will join ... */
01076                 if (dbi->dbi_eflags & DB_PRIVATE) {
01077                     dbi->dbi_eflags &= ~DB_JOINENV;
01078                 } else {
01079                     dbi->dbi_eflags |= DB_JOINENV;
01080                     dbi->dbi_oeflags &= ~DB_CREATE;
01081                     dbi->dbi_oeflags &= ~DB_THREAD;
01082                 }
01083             }
01084             dbf = _free(dbf);
01085         }
01086     }
01087 
01088     /*
01089      * Avoid incompatible DB_CREATE/DB_RDONLY flags on DB->open.
01090      */
01091     if ((oflags & DB_CREATE) && (oflags & DB_RDONLY)) {
01092         /* dbhome is writable, and DB->open flags may conflict. */
01093         const char * dbfn = (dbfile ? dbfile : tagName(dbi->dbi_rpmtag));
01094         /*@-mods@*/
01095         const char * dbf = rpmGetPath(dbhome, "/", dbfn, NULL);
01096         /*@=mods@*/
01097 
01098         if (access(dbf, F_OK) == -1) {
01099             /* File does not exist, DB->open might create ... */
01100             oflags &= ~DB_RDONLY;
01101         } else {
01102             /* File exists, DB->open need not create ... */
01103             oflags &= ~DB_CREATE;
01104         }
01105 
01106         /* Only writers need DB_WRITECURSOR ... */
01107         if (!(oflags & DB_RDONLY) && access(dbf, W_OK) == 0) {
01108             dbi->dbi_oflags &= ~DB_RDONLY;
01109         } else {
01110             dbi->dbi_oflags |= DB_RDONLY;
01111         }
01112         dbf = _free(dbf);
01113     }
01114 
01115     /*
01116      * Turn off verify-on-close if opening read-only.
01117      */
01118     if (oflags & DB_RDONLY)
01119         dbi->dbi_verify_on_close = 0;
01120 
01121     if (dbi->dbi_use_dbenv) {
01122         /*@-mods@*/
01123         if (rpmdb->db_dbenv == NULL) {
01124             rc = db_init(dbi, dbhome, dbfile, dbsubfile, &dbenv);
01125             if (rc == 0) {
01126                 rpmdb->db_dbenv = dbenv;
01127                 rpmdb->db_opens = 1;
01128             }
01129         } else {
01130             dbenv = rpmdb->db_dbenv;
01131             rpmdb->db_opens++;
01132         }
01133         /*@=mods@*/
01134     }
01135 
01136     rpmMessage(RPMMESS_DEBUG, _("opening  db index       %s/%s %s mode=0x%x\n"),
01137                 dbhome, (dbfile ? dbfile : tagName(dbi->dbi_rpmtag)),
01138                 prDbiOpenFlags(oflags, 0), dbi->dbi_mode);
01139 
01140     if (rc == 0) {
01141         static int _lockdbfd = 0;
01142 
01143         /*@-moduncon@*/ /* FIX: annotate db3 methods */
01144         rc = db_create(&db, dbenv, dbi->dbi_cflags);
01145         /*@=moduncon@*/
01146         rc = cvtdberr(dbi, "db_create", rc, _debug);
01147         if (rc == 0 && db != NULL) {
01148 
01149 /* XXX 3.3.4 change. */
01150 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
01151             if (rc == 0 &&
01152                         rpmdb->db_malloc && rpmdb->db_realloc && rpmdb->db_free)
01153             {
01154                 rc = db->set_alloc(db,
01155                         rpmdb->db_malloc, rpmdb->db_realloc, rpmdb->db_free);
01156                 rc = cvtdberr(dbi, "db->set_alloc", rc, _debug);
01157             }
01158 #else
01159             if (rc == 0 && rpmdb->db_malloc) {
01160                 rc = db->set_malloc(db, rpmdb->db_malloc);
01161                 rc = cvtdberr(dbi, "db->set_malloc", rc, _debug);
01162             }
01163 #endif
01164 
01165 /* 4.1: db->set_cache_priority(???) */
01166             if (rc == 0 && !dbi->dbi_use_dbenv && dbi->dbi_cachesize) {
01167                 rc = db->set_cachesize(db, 0, dbi->dbi_cachesize, 0);
01168                 rc = cvtdberr(dbi, "db->set_cachesize", rc, _debug);
01169             }
01170 /* 4.1: db->set_encrypt(???) */
01171 /* 4.1: db->set_errcall(dbenv, rpmdb->db_errcall); */
01172 /* 4.1: db->set_errfile(dbenv, rpmdb->db_errfile); */
01173 /* 4.1: db->set_errpfx(dbenv, rpmdb->db_errpfx); */
01174  /* 4.1: db->set_feedback(???) */
01175 
01176             if (rc == 0 && dbi->dbi_lorder) {
01177                 rc = db->set_lorder(db, dbi->dbi_lorder);
01178                 rc = cvtdberr(dbi, "db->set_lorder", rc, _debug);
01179             }
01180             if (rc == 0 && dbi->dbi_pagesize) {
01181                 rc = db->set_pagesize(db, dbi->dbi_pagesize);
01182                 rc = cvtdberr(dbi, "db->set_pagesize", rc, _debug);
01183             }
01184  /* 4.1: db->set_paniccall(???) */
01185             if (rc == 0 && oflags & DB_CREATE) {
01186                 switch(dbi->dbi_type) {
01187                 default:
01188                 case DB_HASH:
01189                     if (dbi->dbi_h_ffactor) {
01190                         rc = db->set_h_ffactor(db, dbi->dbi_h_ffactor);
01191                         rc = cvtdberr(dbi, "db->set_h_ffactor", rc, _debug);
01192                         if (rc) break;
01193                     }
01194                     if (dbi->dbi_h_nelem) {
01195                         rc = db->set_h_nelem(db, dbi->dbi_h_nelem);
01196                         rc = cvtdberr(dbi, "db->set_h_nelem", rc, _debug);
01197                         if (rc) break;
01198                     }
01199                     if (dbi->dbi_h_flags) {
01200                         rc = db->set_flags(db, dbi->dbi_h_flags);
01201                         rc = cvtdberr(dbi, "db->set_h_flags", rc, _debug);
01202                         if (rc) break;
01203                     }
01204 /* XXX db-3.2.9 has added a DB arg to the call. */
01205 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2) || (DB_VERSION_MAJOR == 4)
01206                     if (dbi->dbi_h_hash_fcn) {
01207                         rc = db->set_h_hash(db, dbi->dbi_h_hash_fcn);
01208                         rc = cvtdberr(dbi, "db->set_h_hash", rc, _debug);
01209                         if (rc) break;
01210                     }
01211                     if (dbi->dbi_h_dup_compare_fcn) {
01212                         rc = db->set_dup_compare(db, dbi->dbi_h_dup_compare_fcn);
01213                         rc = cvtdberr(dbi, "db->set_dup_compare", rc, _debug);
01214                         if (rc) break;
01215                     }
01216 #endif
01217                     break;
01218                 case DB_BTREE:
01219 /* 4.1: db->set_append_recno(???) */
01220                     if (dbi->dbi_bt_flags) {
01221                         rc = db->set_flags(db, dbi->dbi_bt_flags);
01222                         rc = cvtdberr(dbi, "db->set_bt_flags", rc, _debug);
01223                         if (rc) break;
01224                     }
01225                     if (dbi->dbi_bt_minkey) {
01226                         rc = db->set_bt_minkey(db, dbi->dbi_bt_minkey);
01227                         rc = cvtdberr(dbi, "db->set_bt_minkey", rc, _debug);
01228                         if (rc) break;
01229                     }
01230 /* XXX db-3.2.9 has added a DB arg to the call. */
01231 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2) || (DB_VERSION_MAJOR == 4)
01232                     if (dbi->dbi_bt_compare_fcn) {
01233                         rc = db->set_bt_compare(db, dbi->dbi_bt_compare_fcn);
01234                         rc = cvtdberr(dbi, "db->set_bt_compare", rc, _debug);
01235                         if (rc) break;
01236                     }
01237                     if (dbi->dbi_bt_dup_compare_fcn) {
01238                         rc = db->set_dup_compare(db, dbi->dbi_bt_dup_compare_fcn);
01239                         rc = cvtdberr(dbi, "db->set_dup_compare", rc, _debug);
01240                         if (rc) break;
01241                     }
01242                     if (dbi->dbi_bt_prefix_fcn) {
01243                         rc = db->set_bt_prefix(db, dbi->dbi_bt_prefix_fcn);
01244                         rc = cvtdberr(dbi, "db->set_bt_prefix", rc, _debug);
01245                         if (rc) break;
01246                     }
01247 #endif
01248                     break;
01249                 case DB_RECNO:
01250                     if (dbi->dbi_re_delim) {
01251 /* 4.1: db->set_append_recno(???) */
01252                         rc = db->set_re_delim(db, dbi->dbi_re_delim);
01253                         rc = cvtdberr(dbi, "db->set_re_selim", rc, _debug);
01254                         if (rc) break;
01255                     }
01256                     if (dbi->dbi_re_len) {
01257                         rc = db->set_re_len(db, dbi->dbi_re_len);
01258                         rc = cvtdberr(dbi, "db->set_re_len", rc, _debug);
01259                         if (rc) break;
01260                     }
01261                     if (dbi->dbi_re_pad) {
01262                         rc = db->set_re_pad(db, dbi->dbi_re_pad);
01263                         rc = cvtdberr(dbi, "db->set_re_pad", rc, _debug);
01264                         if (rc) break;
01265                     }
01266                     if (dbi->dbi_re_source) {
01267                         rc = db->set_re_source(db, dbi->dbi_re_source);
01268                         rc = cvtdberr(dbi, "db->set_re_source", rc, _debug);
01269                         if (rc) break;
01270                     }
01271                     break;
01272                 case DB_QUEUE:
01273                     if (dbi->dbi_q_extentsize) {
01274                         rc = db->set_q_extentsize(db, dbi->dbi_q_extentsize);
01275                         rc = cvtdberr(dbi, "db->set_q_extentsize", rc, _debug);
01276                         if (rc) break;
01277                     }
01278                     break;
01279                 }
01280             }
01281 
01282             if (rc == 0) {
01283                 const char * dbfullpath;
01284                 const char * dbpath;
01285                 char * t;
01286                 int nb;
01287 
01288                 nb = strlen(dbhome);
01289                 if (dbfile)     nb += 1 + strlen(dbfile);
01290                 dbfullpath = t = alloca(nb + 1);
01291 
01292 /*@-boundswrite@*/
01293                 t = stpcpy(t, dbhome);
01294                 if (dbfile)
01295                     t = stpcpy( stpcpy( t, "/"), dbfile);
01296 /*@=boundswrite@*/
01297 #ifdef  HACK    /* XXX necessary to support dbsubfile */
01298                 dbpath = (!dbi->dbi_use_dbenv && !dbi->dbi_temporary)
01299                         ? dbfullpath : dbfile;
01300 #else
01301                 dbpath = (!dbi->dbi_temporary)
01302                         ? dbfullpath : dbfile;
01303 #endif
01304 
01305 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
01306                 rc = (db->open)(db, txnid, dbpath, dbsubfile,
01307                     dbi->dbi_type, oflags, dbi->dbi_perms);
01308 #else
01309                 rc = (db->open)(db, dbpath, dbsubfile,
01310                     dbi->dbi_type, oflags, dbi->dbi_perms);
01311 #endif
01312 
01313                 if (rc == 0 && dbi->dbi_type == DB_UNKNOWN) {
01314 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11) \
01315  || (DB_VERSION_MAJOR == 4)
01316                     DBTYPE dbi_type = DB_UNKNOWN;
01317                     xx = db->get_type(db, &dbi_type);
01318                     if (xx == 0)
01319                         dbi->dbi_type = dbi_type;
01320 #else
01321                     dbi->dbi_type = db->get_type(db);
01322 #endif
01323                 }
01324             }
01325 
01326             /* XXX return rc == errno without printing */
01327             _printit = (rc > 0 ? 0 : _debug);
01328             xx = cvtdberr(dbi, "db->open", rc, _printit);
01329 
01330             dbi->dbi_txnid = NULL;
01331 
01332             /*
01333              * Lock a file using fcntl(2). Traditionally this is Packages,
01334              * the file used to store metadata of installed header(s),
01335              * as Packages is always opened, and should be opened first,
01336              * for any rpmdb access.
01337              *
01338              * If no DBENV is used, then access is protected with a
01339              * shared/exclusive locking scheme, as always.
01340              *
01341              * With a DBENV, the fcntl(2) lock is necessary only to keep
01342              * the riff-raff from playing where they don't belong, as
01343              * the DBENV should provide it's own locking scheme. So try to
01344              * acquire a lock, but permit failures, as some other
01345              * DBENV player may already have acquired the lock.
01346              *
01347              * With NPTL posix mutexes, revert to fcntl lock on non-functioning
01348              * glibc/kernel combinations.
01349              */
01350             if (rc == 0 && dbi->dbi_lockdbfd &&
01351                 !((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) &&
01352                 (!dbi->dbi_use_dbenv || _lockdbfd++ == 0))
01353             {
01354                 int fdno = -1;
01355 
01356                 if (!(db->fd(db, &fdno) == 0 && fdno >= 0)) {
01357                     rc = 1;
01358                 } else {
01359                     struct flock l;
01360 /*@-boundswrite@*/
01361                     memset(&l, 0, sizeof(l));
01362 /*@=boundswrite@*/
01363                     l.l_whence = 0;
01364                     l.l_start = 0;
01365                     l.l_len = 0;
01366                     l.l_type = (dbi->dbi_mode & (O_RDWR|O_WRONLY))
01367                                 ? F_WRLCK : F_RDLCK;
01368                     l.l_pid = 0;
01369 
01370                     rc = fcntl(fdno, F_SETLK, (void *) &l);
01371                     if (rc) {
01372                         /* Warning iff using non-private CDB locking. */
01373                         rc = ((dbi->dbi_use_dbenv &&
01374                                 (dbi->dbi_eflags & DB_INIT_CDB) &&
01375                                 !(dbi->dbi_eflags & DB_PRIVATE))
01376                             ? 0 : 1);
01377                         rpmError( (rc ? RPMERR_FLOCK : RPMWARN_FLOCK),
01378                                 _("cannot get %s lock on %s/%s\n"),
01379                                 ((dbi->dbi_mode & (O_RDWR|O_WRONLY))
01380                                         ? _("exclusive") : _("shared")),
01381                                 dbhome, (dbfile ? dbfile : ""));
01382                     } else if (dbfile) {
01383                         rpmMessage(RPMMESS_DEBUG,
01384                                 _("locked   db index       %s/%s\n"),
01385                                 dbhome, dbfile);
01386                     }
01387                 }
01388             }
01389         }
01390     }
01391 
01392     dbi->dbi_db = db;
01393 
01394     if (rc == 0 && dbi->dbi_db != NULL && dbip != NULL) {
01395         dbi->dbi_vec = &db3vec;
01396 /*@-boundswrite@*/
01397         *dbip = dbi;
01398 /*@=boundswrite@*/
01399     } else {
01400         dbi->dbi_verify_on_close = 0;
01401         (void) db3close(dbi, 0);
01402     }
01403 
01404     urlfn = _free(urlfn);
01405 
01406     /*@-nullstate -compmempass@*/
01407     return rc;
01408     /*@=nullstate =compmempass@*/
01409 }
01410 
01413 /*@-exportheadervar@*/
01414 /*@observer@*/ /*@unchecked@*/
01415 struct _dbiVec db3vec = {
01416     DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,
01417     db3open, db3close, db3sync, db3associate, db3join,
01418     db3copen, db3cclose, db3cdup, db3cdel, db3cget, db3cpget, db3cput, db3ccount,
01419     db3byteswapped, db3stat
01420 };
01421 /*@=exportheadervar@*/
01422 /*@=type@*/

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2