00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifdef HAVE_CONFIG_H
00012 #include "config.h"
00013 #endif
00014
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017
00018 #include "db.h"
00019 #include "WordDBInfo.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028 static void message(const char *errpfx, char *msg)
00029 {
00030 fprintf(stderr, "%s: %s\n", errpfx, msg);
00031 }
00032
00033 WordDBInfo::WordDBInfo(const Configuration& config)
00034 {
00035 dbenv = 0;
00036
00037 if(config.Boolean("wordlist_env_skip")) return;
00038
00039 int error;
00040 if((error = CDB_db_env_create(&dbenv, 0)) != 0) {
00041 fprintf(stderr, "WordDBInfo: CDB_db_env_create %s\n", CDB_db_strerror(error));
00042 return;
00043 }
00044 dbenv->set_errpfx(dbenv, "WordDB");
00045 dbenv->set_errcall(dbenv, message);
00046 if(dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT, 1) != 0)
00047 return;
00048 if(dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1) != 0)
00049 return;
00050 if(dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1) != 0)
00051 return;
00052 if(dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1) != 0)
00053 return;
00054 int cache_size = config.Value("wordlist_cache_size", 10*1024*1024);
00055 if(cache_size > 0) {
00056 if(dbenv->set_cachesize(dbenv, 0, cache_size, 1) != 0)
00057 return;
00058 }
00059
00060 char* dir = 0;
00061 int flags = DB_CREATE;
00062 if(config.Boolean("wordlist_env_share")) {
00063 const String& env_dir = config["wordlist_env_dir"];
00064 if(env_dir.empty()) {
00065 fprintf(stderr, "WordDB: wordlist_env_dir not specified\n");
00066 return;
00067 }
00068 dir = strdup((const char*)env_dir);
00069
00070 if(config.Boolean("wordlist_env_cdb"))
00071 flags |= DB_INIT_CDB;
00072 else
00073 flags |= DB_INIT_LOCK | DB_INIT_MPOOL;
00074
00075 } else {
00076 flags |= DB_PRIVATE | DB_INIT_LOCK | DB_INIT_MPOOL;
00077 }
00078
00079 if((error = dbenv->open(dbenv, (const char*)dir, flags, 0666)) != 0)
00080 dbenv->err(dbenv, error, "open %s", (dir ? dir : ""));
00081 if(dir) free(dir);
00082 }
00083
00084 WordDBInfo::~WordDBInfo()
00085 {
00086 if(dbenv) dbenv->close(dbenv, 0);
00087 }