11 #if defined(HAVE_FTOK) && defined(HAVE_SYS_IPC_H)
19 #define _RPMDB_INTERNAL
24 #if !defined(DB_CLIENT)
25 #define DB_CLIENT DB_RPCCLIENT
81 static const char * bfstring(
unsigned int x,
const char * xbf)
84 static char digits[] =
"0123456789abcdefghijklmnopqrstuvwxyz";
85 static char buf[BUFSIZ];
90 radix = (s != NULL ? *s++ : 16);
92 if (radix <= 1 || radix >= 32)
97 case 8: *t++ =
'0';
break;
98 case 16: *t++ =
'0'; *t++ =
'x';
break;
103 do { i++; k /= radix; }
while (k);
108 do { --i; t[i] = digits[k % radix]; k /= radix; }
while (k);
113 while ((c = *s++) !=
'\0') {
114 if (c >
' ')
continue;
117 if (!(x & k))
continue;
119 if (t == te) *t++ =
'=';
126 if (t > te) *t++ =
'>';
132 static const char * dbtFlags =
133 "\20\1APPMALLOC\2ISSET\3MALLOC\4PARTIAL\5REALLOC\6USERMEM\7DUPOK";
135 static const char * dbenvOpenFlags =
136 "\20\1CREATE\2DURABLE_UNKNOWN\3FORCE\4MULTIVERSION\5NOMMAP\6RDONLY\7RECOVER\10THREAD\11TRUNCATE\12TXN_NOSYNC\13TXN_NOT_DURABLEi\14TXN_WRITE_NOSYNC\15USE_ENVIRON\16USE_ENVIRON_ROOT\17CDB\20LOCK\21LOG\22MPOOL\23REP\24TXN\25LOCKDOWN\26PRIVATE\27RECOVER_FATAL\30REGISTER\31SYSTEM_MEM";
138 static const char * dbOpenFlags =
139 "\20\1CREATE\2DURABLE_UNKNOWN\3FORCE\4MULTIVERSION\5NOMMAP\6RDONLY\7RECOVER\10THREAD\11TRUNCATE\12TXN_NOSYNC\13TXN_NOT_DURABLEi\14TXN_WRITE_NOSYNC\15USE_ENVIRON\16USE_ENVIRON_ROOT\17EXCL\20FCNTL_LOCKING\21NO_AUTO_COMMIT\22RDWRMASTER\23WRITEOPEN";
141 static const char * dbenvSetFlags =
142 "\20\1CREATE\2DURABLE_UNKNOWN\3FORCE\4MULTIVERSION\5NOMMAP\6RDONLY\7RECOVER\10THREAD\11TRUNCATE\12TXN_NOSYNC\13TXN_NOT_DURABLEi\14TXN_WRITE_NOSYNC\15USE_ENVIRON\16USE_ENVIRON_ROOT\17CDB_ALLDB\20DIRECT_DB\21DIRECT_LOG\22DSYNC_DB\23DSYNC_LOG\24LOG_AUTOREMOVE\25LOG_INMEMORY\26NOLOCKING\27NOPANIC\30OVERWRITE\31PANIC_ENV\36REGION_INIT\37TIME_NOTGRANTED\40YIELDCPU";
144 static const char * dbSetFlags =
145 "\20\1CREATE\2DURABLE_UNKNOWN\3FORCE\4MULTIVERSION\5NOMMAP\6RDONLY\7RECOVER\10THREAD\11TRUNCATE\12TXN_NOSYNC\13TXN_NOT_DURABLEi\14TXN_WRITE_NOSYNC\15USE_ENVIRON\16USE_ENVIRON_ROOT\17CHKSUM\20DUP\21DUPSORT\22ENCRYPT\23INORDER\24RECNUM\25RENUMBER\26REVSPLITOFF\27SNAPSHOT";
147 static const char * dbiModeFlags =
148 "\20\1WRONLY\2RDWR\7CREAT\10EXCL\11NOCTTY\12TRUNC\13APPEND\14NONBLOCK\15SYNC\16ASYNC\17DIRECT\20LARGEFILE\21DIRECTORY\22NOFOLLOW";
163 DB_VERSION_MAJOR, rc, msg, db_strerror(error));
166 DB_VERSION_MAJOR, rc, db_strerror(error));
182 const char * s =
tagName(value);
185 else if (!strcmp(s,
"Filedigests"))
192 const char * dbsubfile)
197 DB_ENV * dbenv = rpmdb->db_dbenv;
203 rc = dbenv->close(dbenv, 0);
210 if (rpmdb->db_remove_env) {
214 xx = db_env_create(&dbenv, 0);
216 if (!xx && dbenv != NULL) {
218 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
219 xx = dbenv->remove(dbenv, dbhome, DB_FORCE);
221 xx = dbenv->remove(dbenv, dbhome, NULL, 0);
240 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5)
249 static int db3is_alive( DB_ENV *dbenv, pid_t pid, db_threadid_t tid,
256 case DB_MUTEX_PROCESS_ONLY:
259 is_alive = (!(kill(pid, 0) < 0 &&
errno == ESRCH));
269 const char * dbsubfile,
275 static int oneshot = 0;
277 DB_ENV *dbenv = NULL;
283 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
284 xx = db_env_set_func_open((
int (*)(
const char *,
int, ...))
Open);
295 if (rpmdb->db_errfile == NULL)
296 rpmdb->db_errfile = stderr;
299 eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
302 if (eflags & DB_JOINENV) eflags &= DB_JOINENV;
306 dbhome, dbfile, prDbiOpenFlags(eflags, 1));
309 if (dbi->dbi_host == NULL)
313 if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
314 #if defined(HAVE_FTOK)
315 dbi->dbi_shmkey = ftok(dbhome, 0);
317 dbi->dbi_shmkey = 0x44631380;
321 rc = db_env_create(&dbenv, dbi->dbi_ecflags);
323 if (dbenv == NULL || rc)
333 dbenv->set_errcall(dbenv, (
void *)rpmdb->db_errcall);
334 dbenv->set_errfile(dbenv, rpmdb->db_errfile);
335 dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
343 if ((dbi->dbi_ecflags &
DB_CLIENT) && dbi->dbi_host) {
347 if ((home = strrchr(dbhome,
'/')) != NULL)
350 while (retry++ < 5) {
352 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
353 xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
354 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
357 xx = dbenv->set_server(dbenv, dbi->dbi_host,
358 dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
366 #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
367 xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
368 (dbi->dbi_verbose & DB_VERB_CHKPOINT));
370 xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
371 (dbi->dbi_verbose & DB_VERB_DEADLOCK));
372 xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
373 (dbi->dbi_verbose & DB_VERB_RECOVERY));
374 #if defined(DB_VERB_REGISTER)
375 xx = dbenv->set_verbose(dbenv, DB_VERB_REGISTER,
376 (dbi->dbi_verbose & DB_VERB_REGISTER));
378 #if defined(DB_VERB_REPLICATION)
379 xx = dbenv->set_verbose(dbenv, DB_VERB_REPLICATION,
380 (dbi->dbi_verbose & DB_VERB_REPLICATION));
382 xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
383 (dbi->dbi_verbose & DB_VERB_WAITSFOR));
384 #if defined(DB_VERB_FILEOPS)
385 xx = dbenv->set_verbose(dbenv, DB_VERB_FILEOPS,
386 (dbi->dbi_verbose & DB_VERB_FILEOPS));
388 #if defined(DB_VERB_FILEOPS_ALL)
389 xx = dbenv->set_verbose(dbenv, DB_VERB_FILEOPS_ALL,
390 (dbi->dbi_verbose & DB_VERB_FILEOPS_ALL));
393 if (dbi->dbi_mmapsize) {
394 xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize);
397 if (dbi->dbi_tmpdir) {
401 root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
403 if ((root[0] ==
'/' && root[1] ==
'\0') || rpmdb->db_chrootDone)
407 tmpdir =
rpmGenPath(root, dbi->dbi_tmpdir, NULL);
409 xx = dbenv->set_tmp_dir(dbenv, tmpdir);
411 tmpdir =
_free(tmpdir);
417 if (dbi->dbi_lk_detect) {
418 xx = dbenv->set_lk_detect(dbenv, dbi->dbi_lk_detect);
421 #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
422 if (dbi->dbi_lk_max_lockers) {
423 xx = dbenv->set_lk_max_lockers(dbenv, dbi->dbi_lk_max_lockers);
426 if (dbi->dbi_lk_max_locks) {
427 xx = dbenv->set_lk_max_locks(dbenv, dbi->dbi_lk_max_locks);
430 if (dbi->dbi_lk_max_objects) {
431 xx = dbenv->set_lk_max_objects(dbenv, dbi->dbi_lk_max_objects);
435 if (dbi->dbi_lg_bsize) {
436 xx = dbenv->set_lg_bsize(dbenv, dbi->dbi_lg_bsize);
439 if (dbi->dbi_lg_dir) {
440 xx = dbenv->set_lg_dir(dbenv, dbi->dbi_lg_dir);
443 if (dbi->dbi_lg_filemode) {
444 xx = dbenv->set_lg_filemode(dbenv, dbi->dbi_lg_filemode);
447 if (dbi->dbi_lg_max) {
448 xx = dbenv->set_lg_max(dbenv, dbi->dbi_lg_max);
451 if (dbi->dbi_lg_regionmax) {
452 xx = dbenv->set_lg_regionmax(dbenv, dbi->dbi_lg_regionmax);
458 if (dbi->dbi_cachesize) {
459 xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_cachesize, 0);
464 if (dbi->dbi_mutex_align) {
465 xx = dbenv->mutex_set_align(dbenv, dbi->dbi_mutex_align);
468 if (dbi->dbi_mutex_increment) {
469 xx = dbenv->mutex_set_increment(dbenv, dbi->dbi_mutex_increment);
472 if (dbi->dbi_mutex_max) {
473 xx = dbenv->mutex_set_max(dbenv, dbi->dbi_mutex_max);
476 if (dbi->dbi_mutex_tas_spins) {
477 xx = dbenv->mutex_set_tas_spins(dbenv, dbi->dbi_mutex_tas_spins);
492 if (dbi->dbi_tx_max) {
493 xx = dbenv->set_tx_max(dbenv, dbi->dbi_tx_max);
504 if (dbi->dbi_no_fsync) {
505 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
513 if (dbi->dbi_shmkey) {
514 xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey);
518 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5)
521 dbenv->set_msgfile(dbenv, rpmdb->db_errfile);
524 if (dbi->dbi_thread_count >= 8) {
525 xx = dbenv->set_thread_count(dbenv, dbi->dbi_thread_count);
530 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR != 0) || (DB_VERSION_MAJOR == 4)
531 rc = (dbenv->open)(dbenv, dbhome, eflags, dbi->dbi_perms);
533 rc = (dbenv->open)(dbenv, dbhome, NULL, eflags, dbi->dbi_perms);
536 #if defined(DB_VERSION_MISMATCH)
537 if (rc == DB_VERSION_MISMATCH) xx = 0;
539 if (rc == EINVAL) xx = 0;
540 rc =
cvtdberr(dbi,
"dbenv->open", rc, xx);
544 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5)
545 if (!rpmdb->db_verifying && dbi->dbi_thread_count >= 8) {
547 xx = dbenv->set_isalive(dbenv, db3is_alive);
550 xx = dbenv->failchk(dbenv, 0);
552 if (xx == DB_RUNRECOVERY) {
567 xx = dbenv->close(dbenv, 0);
578 DB * db = dbi->dbi_db;
583 rc = db->sync(db, flags);
584 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
588 _printit = (rc == DB_INCOMPLETE ? 0 :
_debug);
590 rc =
cvtdberr(dbi,
"db->sync", rc, _printit);
602 if (dbcp) *dbcp = NULL;
604 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
605 rc = dbcursor->dup(dbcursor, dbcp, flags);
608 rc = dbcursor->c_dup(dbcursor, dbcp, flags);
625 if (dbcursor != NULL) {
626 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
627 rc = dbcursor->close(dbcursor);
630 rc = dbcursor->c_close(dbcursor);
639 DBC ** dbcp,
unsigned int dbiflags)
643 DB * db = dbi->dbi_db;
644 DBC * dbcursor = NULL;
650 if ((dbiflags & DB_WRITECURSOR) &&
651 (dbi->dbi_eflags & DB_INIT_CDB) && !(dbi->dbi_oflags & DB_RDONLY))
653 flags = DB_WRITECURSOR;
657 rc = db->cursor(db, txnid, &dbcursor, flags);
673 DB * db = dbi->dbi_db;
677 if (dbcursor == NULL) {
678 rc = db->put(db, dbi->dbi_txnid, key, data, 0);
681 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
682 rc = dbcursor->put(dbcursor, key, data, DB_KEYLAST);
685 rc = dbcursor->c_put(dbcursor, key, data, DB_KEYLAST);
699 DB * db = dbi->dbi_db;
703 if (dbcursor == NULL) {
704 rc = db->del(db, dbi->dbi_txnid, key, flags);
710 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
711 rc = dbcursor->get(dbcursor, key, data, DB_SET);
713 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
714 rc =
cvtdberr(dbi,
"dbcursor->get", rc, _printit);
716 rc = dbcursor->c_get(dbcursor, key, data, DB_SET);
718 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
719 rc =
cvtdberr(dbi,
"dbcursor->c_get", rc, _printit);
723 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
724 rc = dbcursor->del(dbcursor, flags);
727 rc = dbcursor->c_del(dbcursor, flags);
743 DB * db = dbi->dbi_db;
748 if (dbcursor == NULL) {
750 rc = db->get(db, dbi->dbi_txnid, key, data, 0);
752 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
753 rc =
cvtdberr(dbi,
"db->get", rc, _printit);
755 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
757 rc = dbcursor->get(dbcursor, key, data, flags);
759 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
760 rc =
cvtdberr(dbi,
"dbcursor->get", rc, _printit);
763 rc = dbcursor->c_get(dbcursor, key, data, flags);
765 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
766 rc =
cvtdberr(dbi,
"dbcursor->c_get", rc, _printit);
776 DBT * data,
unsigned int flags)
780 DB * db = dbi->dbi_db;
785 assert(dbcursor != NULL);
787 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
789 rc = dbcursor->pget(dbcursor, key, pkey, data, flags);
791 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
792 rc =
cvtdberr(dbi,
"dbcursor->pget", rc, _printit);
795 rc = dbcursor->c_pget(dbcursor, key, pkey, data, flags);
797 _printit = (rc == DB_NOTFOUND ? 0 :
_debug);
798 rc =
cvtdberr(dbi,
"dbcursor->c_pget", rc, _printit);
806 unsigned int * countp,
811 db_recno_t count = 0;
815 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
816 rc = dbcursor->count(dbcursor, &count, flags);
819 rc = dbcursor->c_count(dbcursor, &count, flags);
824 if (countp) *countp = count;
832 DB * db = dbi->dbi_db;
836 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11) \
837 || (DB_VERSION_MAJOR == 4)
839 rc = db->get_byteswapped(db, &isswapped);
843 rc = db->get_byteswapped(db);
854 DB * db = dbi->dbi_db;
855 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
856 DB_TXN * txnid = NULL;
861 #if defined(DB_FAST_STAT)
863 flags = DB_FAST_STAT;
867 dbi->dbi_stats =
_free(dbi->dbi_stats);
869 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
870 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
871 rc = db->stat(db, txnid, &dbi->dbi_stats, flags);
873 rc = db->stat(db, &dbi->dbi_stats, flags);
876 rc = db->stat(db, &dbi->dbi_stats, NULL, flags);
884 int (*callback)(DB *,
const DBT *,
const DBT *, DBT *),
889 DB * db = dbi->dbi_db;
890 DB * secondary = dbisecondary->dbi_db;
894 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
895 DB_TXN * txnid = NULL;
898 rc = db->associate(db, txnid, secondary, callback, flags);
901 rc = db->associate(db, secondary, callback, flags);
915 DB * db = dbi->dbi_db;
920 rc = db->join(db, curslist, dbcp, flags);
934 const char * urlfn = NULL;
939 const char * dbsubfile;
940 DB * db = dbi->dbi_db;
949 root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
951 if ((root[0] ==
'/' && root[1] ==
'\0') || rpmdb->db_chrootDone)
954 home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
963 (void)
urlPath(urlfn, &dbhome);
964 if (dbi->dbi_temporary) {
969 dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
970 dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile :
mapTagName(dbi->dbi_rpmtag));
972 dbfile = (dbi->dbi_file ? dbi->dbi_file :
mapTagName(dbi->dbi_rpmtag));
978 rc = db->close(db, 0);
980 _printit = (rc == ENOENT ? 0 :
_debug);
981 rc =
cvtdberr(dbi,
"db->close", rc, _printit);
982 db = dbi->dbi_db = NULL;
985 dbhome, (dbfile ? dbfile :
mapTagName(dbi->dbi_rpmtag)));
989 if (rpmdb->db_dbenv != NULL && dbi->dbi_use_dbenv) {
990 if (rpmdb->db_opens == 1) {
992 xx =
db_fini(dbi, (dbhome ? dbhome :
""), dbfile, dbsubfile);
994 rpmdb->db_dbenv = NULL;
999 if (dbi->dbi_verify_on_close && !dbi->dbi_temporary) {
1000 DB_ENV * dbenv = NULL;
1004 rc = db_env_create(&dbenv, 0);
1007 if (rc || dbenv == NULL)
goto exit;
1010 dbenv->set_errcall(dbenv, (
void *)rpmdb->db_errcall);
1011 dbenv->set_errfile(dbenv, rpmdb->db_errfile);
1012 dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
1015 #if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
1016 xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
1017 (dbi->dbi_verbose & DB_VERB_CHKPOINT));
1019 xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
1020 (dbi->dbi_verbose & DB_VERB_DEADLOCK));
1021 xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
1022 (dbi->dbi_verbose & DB_VERB_RECOVERY));
1023 xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
1024 (dbi->dbi_verbose & DB_VERB_WAITSFOR));
1026 if (dbi->dbi_tmpdir) {
1028 const char * tmpdir =
rpmGenPath(root, dbi->dbi_tmpdir, NULL);
1030 rc = dbenv->set_tmp_dir(dbenv, tmpdir);
1032 tmpdir =
_free(tmpdir);
1036 eflags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON;
1037 rc = (dbenv->open)(dbenv, dbhome, eflags, 0);
1042 rc = db_create(&db, dbenv, 0);
1048 const char * dbf =
rpmGetPath(dbhome,
"/", dbfile, NULL);
1051 rc = db->verify(db, dbf, NULL, NULL, flags);
1055 (dbhome ? dbhome :
""),
1056 (dbfile ? dbfile :
mapTagName(dbi->dbi_rpmtag)));
1065 xx = dbenv->close(dbenv, 0);
1067 if (rc == 0 && xx) rc = xx;
1073 urlfn =
_free(urlfn);
1087 extern struct _dbiVec db3vec;
1089 const char * urlfn = NULL;
1092 const char * dbhome;
1093 const char * dbfile;
1094 const char * dbsubfile;
1100 DB_ENV * dbenv = NULL;
1101 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
1102 DB_TXN * txnid = NULL;
1104 DBTYPE dbi_type = DB_UNKNOWN;
1117 if ((dbi = db3New(rpmdb, rpmtag)) == NULL)
1122 dbi->dbi_api = DB_VERSION_MAJOR;
1127 root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
1129 if ((root[0] ==
'/' && root[1] ==
'\0') || rpmdb->db_chrootDone)
1132 home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);
1141 (void)
urlPath(urlfn, &dbhome);
1142 if (dbi->dbi_temporary) {
1147 dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
1148 dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile :
mapTagName(dbi->dbi_rpmtag));
1150 dbfile = (dbi->dbi_file ? dbi->dbi_file :
mapTagName(dbi->dbi_rpmtag));
1155 oflags = (dbi->dbi_oeflags | dbi->dbi_oflags);
1156 oflags &= ~DB_TRUNCATE;
1159 if ( dbi->dbi_mode & O_EXCL) oflags |= DB_EXCL;
1165 if (dbi->dbi_temporary) {
1166 oflags |= DB_CREATE;
1167 dbi->dbi_oeflags |= DB_CREATE;
1168 oflags &= ~DB_RDONLY;
1169 dbi->dbi_oflags &= ~DB_RDONLY;
1171 if (!(dbi->dbi_mode & (O_RDWR|O_WRONLY))) oflags |= DB_RDONLY;
1172 if (dbi->dbi_mode & O_CREAT) {
1173 oflags |= DB_CREATE;
1174 dbi->dbi_oeflags |= DB_CREATE;
1177 if ( dbi->dbi_mode & O_TRUNC) oflags |= DB_TRUNCATE;
1184 (void)
rpmioMkpath(dbhome, 0755, getuid(), getgid());
1189 if (dbi->dbi_use_dbenv) {
1191 if (access(dbhome,
W_OK) == -1) {
1194 oflags &= ~DB_CREATE;
1197 if (dbi->dbi_eflags & DB_PRIVATE) {
1198 dbi->dbi_eflags &= ~DB_JOINENV;
1200 dbi->dbi_eflags |= DB_JOINENV;
1201 dbi->dbi_oeflags &= ~DB_CREATE;
1202 dbi->dbi_oeflags &= ~DB_THREAD;
1204 dbi->dbi_use_dbenv = 0;
1208 if (dbi->dbi_temporary) {
1209 oflags |= DB_CREATE;
1210 dbi->dbi_oeflags |= DB_CREATE;
1211 oflags &= ~DB_RDONLY;
1212 dbi->dbi_oflags &= ~DB_RDONLY;
1214 oflags |= DB_RDONLY;
1216 dbi->dbi_oflags |= DB_RDONLY;
1221 const char * dbf =
rpmGetPath(dbhome,
"/__db.001", NULL);
1224 if (access(dbf,
F_OK) == -1) {
1226 dbi->dbi_oeflags |= DB_CREATE;
1227 dbi->dbi_eflags &= ~DB_JOINENV;
1230 if (dbi->dbi_eflags & DB_PRIVATE) {
1231 dbi->dbi_eflags &= ~DB_JOINENV;
1233 dbi->dbi_eflags |= DB_JOINENV;
1234 dbi->dbi_oeflags &= ~DB_CREATE;
1235 dbi->dbi_oeflags &= ~DB_THREAD;
1245 if ((oflags & DB_CREATE) && (oflags & DB_RDONLY)) {
1247 const char * dbfn = (dbfile ? dbfile :
mapTagName(dbi->dbi_rpmtag));
1249 const char * dbf =
rpmGetPath(dbhome,
"/", dbfn, NULL);
1252 if (access(dbf,
F_OK) == -1) {
1254 oflags &= ~DB_RDONLY;
1257 oflags &= ~DB_CREATE;
1261 if (!(oflags & DB_RDONLY) && access(dbf,
W_OK) == 0) {
1262 dbi->dbi_oflags &= ~DB_RDONLY;
1264 dbi->dbi_oflags |= DB_RDONLY;
1272 if (oflags & DB_CREATE)
1273 dbi_type = dbi->dbi_type;
1278 if (oflags & DB_RDONLY)
1279 dbi->dbi_verify_on_close = 0;
1282 if (dbi->dbi_use_dbenv) {
1284 if (rpmdb->db_dbenv == NULL) {
1285 static int runrecoverycount = 0;
1286 rc =
db_init(dbi, dbhome, dbfile, dbsubfile, &dbenv);
1291 case DB_RUNRECOVERY:
1292 if (runrecoverycount++ >= 1) {
1297 rpmdb =
rpmdbLink(rpmdb,
"DB_RUNRECOVERY");
1298 rpmdb->db_remove_env = 1;
1299 rpmdb->db_verifying = 1;
1302 rpmdb->db_remove_env = 0;
1303 rpmdb->db_verifying = 0;
1305 dbi->dbi_oeflags |= DB_CREATE;
1306 dbi->dbi_eflags &= ~DB_JOINENV;
1307 rc =
db_init(dbi, dbhome, dbfile, dbsubfile, &dbenv);
1314 rpmdb->db_dbenv = dbenv;
1315 rpmdb->db_opens = 1;
1318 #if defined(DB_VERSION_MISMATCH)
1319 case DB_VERSION_MISMATCH:
1324 {
char * filename =
alloca(BUFSIZ);
1328 for (i = 0; i < 16; i++) {
1329 sprintf(filename,
"%s/__db.%03d", dbhome, i);
1331 if (
Stat(filename, &st)
1337 dbi->dbi_oeflags |= DB_CREATE;
1338 dbi->dbi_eflags &= ~DB_JOINENV;
1339 rc =
db_init(dbi, dbhome, dbfile, dbsubfile, &dbenv);
1347 rpmdb->db_dbenv = dbenv;
1348 rpmdb->db_opens = 1;
1352 assert(rpmdb && rpmdb->db_dbenv);
1353 dbenv = rpmdb->db_dbenv;
1361 dbhome, (dbfile ? dbfile :
mapTagName(dbi->dbi_rpmtag)),
1362 prDbiOpenFlags(oflags, 0), dbi->dbi_mode);
1365 static int _lockdbfd = 0;
1368 rc = db_create(&db, dbenv, dbi->dbi_cflags);
1371 if (rc == 0 && db != NULL) {
1374 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3) || (DB_VERSION_MAJOR == 4)
1376 rpmdb->db_malloc && rpmdb->db_realloc && rpmdb->db_free)
1378 rc = db->set_alloc(db,
1379 rpmdb->db_malloc, rpmdb->db_realloc, rpmdb->db_free);
1383 if (rc == 0 && rpmdb->db_malloc) {
1384 rc = db->set_malloc(db, rpmdb->db_malloc);
1390 if (rc == 0 && !dbi->dbi_use_dbenv && dbi->dbi_cachesize) {
1391 rc = db->set_cachesize(db, 0, dbi->dbi_cachesize, 0);
1400 if (rc == 0 && dbi->dbi_lorder) {
1401 rc = db->set_lorder(db, dbi->dbi_lorder);
1404 if (rc == 0 && dbi->dbi_pagesize) {
1405 rc = db->set_pagesize(db, dbi->dbi_pagesize);
1409 if (rc == 0 && oflags & DB_CREATE) {
1410 switch(dbi->dbi_type) {
1413 if (dbi->dbi_h_ffactor) {
1414 rc = db->set_h_ffactor(db, dbi->dbi_h_ffactor);
1418 if (dbi->dbi_h_nelem) {
1419 rc = db->set_h_nelem(db, dbi->dbi_h_nelem);
1423 if (dbi->dbi_h_flags) {
1424 rc = db->set_flags(db, dbi->dbi_h_flags);
1429 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2) || (DB_VERSION_MAJOR == 4)
1430 if (dbi->dbi_h_hash_fcn) {
1431 rc = db->set_h_hash(db, dbi->dbi_h_hash_fcn);
1435 if (dbi->dbi_h_dup_compare_fcn) {
1436 rc = db->set_dup_compare(db, dbi->dbi_h_dup_compare_fcn);
1444 if (dbi->dbi_bt_flags) {
1445 rc = db->set_flags(db, dbi->dbi_bt_flags);
1449 if (dbi->dbi_bt_minkey) {
1450 rc = db->set_bt_minkey(db, dbi->dbi_bt_minkey);
1455 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2) || (DB_VERSION_MAJOR == 4)
1456 if (dbi->dbi_bt_compare_fcn) {
1457 rc = db->set_bt_compare(db, dbi->dbi_bt_compare_fcn);
1461 if (dbi->dbi_bt_dup_compare_fcn) {
1462 rc = db->set_dup_compare(db, dbi->dbi_bt_dup_compare_fcn);
1466 if (dbi->dbi_bt_prefix_fcn) {
1467 rc = db->set_bt_prefix(db, dbi->dbi_bt_prefix_fcn);
1474 if (dbi->dbi_re_delim) {
1476 rc = db->set_re_delim(db, dbi->dbi_re_delim);
1480 if (dbi->dbi_re_len) {
1481 rc = db->set_re_len(db, dbi->dbi_re_len);
1485 if (dbi->dbi_re_pad) {
1486 rc = db->set_re_pad(db, dbi->dbi_re_pad);
1490 if (dbi->dbi_re_source) {
1491 rc = db->set_re_source(db, dbi->dbi_re_source);
1497 if (dbi->dbi_q_extentsize) {
1498 rc = db->set_q_extentsize(db, dbi->dbi_q_extentsize);
1507 const char * dbfullpath;
1508 const char * dbpath;
1512 nb = strlen(dbhome);
1513 if (dbfile) nb += 1 + strlen(dbfile);
1514 dbfullpath = t =
alloca(nb + 1);
1522 dbpath = (!dbi->dbi_use_dbenv && !dbi->dbi_temporary)
1523 ? dbfullpath : dbfile;
1525 dbpath = (!dbi->dbi_temporary)
1526 ? dbfullpath : dbfile;
1529 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
1530 rc = (db->open)(db, txnid, dbpath, dbsubfile,
1531 dbi_type, oflags, dbi->dbi_perms);
1533 rc = (db->open)(db, dbpath, dbsubfile,
1534 dbi_type, oflags, dbi->dbi_perms);
1537 if (rc == 0 && dbi_type == DB_UNKNOWN) {
1538 #if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 3 && DB_VERSION_PATCH == 11) \
1539 || (DB_VERSION_MAJOR == 4)
1540 xx = db->get_type(db, &dbi_type);
1542 dbi->dbi_type = dbi_type;
1544 dbi->dbi_type = db->get_type(db);
1550 _printit = (rc > 0 ? 0 :
_debug);
1551 xx =
cvtdberr(dbi,
"db->open", rc, _printit);
1553 dbi->dbi_txnid = NULL;
1573 if (rc == 0 && dbi->dbi_lockdbfd &&
1574 !((dbi->dbi_ecflags &
DB_CLIENT) && dbi->dbi_host) &&
1575 (!dbi->dbi_use_dbenv || _lockdbfd++ == 0))
1579 if (!(db->fd(db, &fdno) == 0 && fdno >= 0)) {
1584 memset(&l, 0,
sizeof(l));
1589 l.l_type = (dbi->dbi_mode & (O_RDWR|O_WRONLY))
1590 ? F_WRLCK : F_RDLCK;
1593 rc = fcntl(fdno, F_SETLK, (
void *) &l);
1596 rc = ((dbi->dbi_use_dbenv &&
1597 (dbi->dbi_eflags & DB_INIT_CDB) &&
1598 !(dbi->dbi_eflags & DB_PRIVATE))
1601 _(
"cannot get %s lock on %s/%s\n"),
1602 ((dbi->dbi_mode & (O_RDWR|O_WRONLY))
1603 ?
_(
"exclusive") :
_(
"shared")),
1604 dbhome, (dbfile ? dbfile :
""));
1605 }
else if (dbfile) {
1607 D_(
"locked db index %s/%s\n"),
1617 if (rc == 0 && dbi->dbi_db != NULL && dbip != NULL) {
1623 dbi->dbi_verify_on_close = 0;
1627 urlfn =
_free(urlfn);
1639 DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,