-// Open a database environment
-DB_ENV *open_dbenv(char *dirname) {
-
- DB_ENV *dbenv = NULL;
-
- int ret;
- int i;
- u_int32_t flags = 0;
- int dbversion_major, dbversion_minor, dbversion_patch;
- db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
-
- // Create synthetic integer version numbers and compare them.
- // Never run with a libdb other than the one with which it was compiled.
- int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
- int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
- if (compiled_db_version != linked_db_version) {
- fprintf(stderr,
- "db: ctdlload is running with a version of libdb other than the one with which it was compiled.\n"
- "db: compiled: %d\n"
- "db: linked: %d\n"
- "db: This is an invalid configuration. ctdlload will now exit to prevent data loss.",
- compiled_db_version,
- linked_db_version
- );
- exit(CTDLEXIT_DB);
- }
-
- ret = db_env_create(&dbenv, 0);
- if (ret) {
- fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
- fprintf(stderr,"db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
- ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
- if (ret) {
- fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr,"db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
- fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr,"db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
- ret = dbenv->open(dbenv, dirname, flags, 0);
- if (ret) {
- fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr,"db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- return(dbenv);
-}
-
-
-void close_dbenv(DB_ENV *dbenv) {
- int ret = dbenv->close(dbenv, 0);
- if (ret) {
- fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
- }
-}
-
-