| 115 | } |
| 116 | |
| 117 | CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate) |
| 118 | : m_name{fs::PathToString(path.stem())} |
| 119 | { |
| 120 | penv = nullptr; |
| 121 | readoptions.verify_checksums = true; |
| 122 | iteroptions.verify_checksums = true; |
| 123 | iteroptions.fill_cache = false; |
| 124 | syncoptions.sync = true; |
| 125 | options = GetOptions(nCacheSize); |
| 126 | options.create_if_missing = true; |
| 127 | if (fMemory) { |
| 128 | penv = leveldb::NewMemEnv(leveldb::Env::Default()); |
| 129 | options.env = penv; |
| 130 | } else { |
| 131 | if (fWipe) { |
| 132 | LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(path)); |
| 133 | leveldb::Status result = leveldb::DestroyDB(fs::PathToString(path), options); |
| 134 | dbwrapper_private::HandleError(result); |
| 135 | } |
| 136 | TryCreateDirectories(path); |
| 137 | LogPrintf("Opening LevelDB in %s\n", fs::PathToString(path)); |
| 138 | } |
| 139 | // PathToString() return value is safe to pass to leveldb open function, |
| 140 | // because on POSIX leveldb passes the byte string directly to ::open(), and |
| 141 | // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW |
| 142 | // (see env_posix.cc and env_windows.cc). |
| 143 | leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(path), &pdb); |
| 144 | dbwrapper_private::HandleError(status); |
| 145 | LogPrintf("Opened LevelDB successfully\n"); |
| 146 | |
| 147 | if (gArgs.GetBoolArg("-forcecompactdb", false)) { |
| 148 | LogPrintf("Starting database compaction of %s\n", fs::PathToString(path)); |
| 149 | pdb->CompactRange(nullptr, nullptr); |
| 150 | LogPrintf("Finished database compaction of %s\n", fs::PathToString(path)); |
| 151 | } |
| 152 | |
| 153 | // The base-case obfuscation key, which is a noop. |
| 154 | obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000'); |
| 155 | |
| 156 | bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key); |
| 157 | |
| 158 | if (!key_exists && obfuscate && IsEmpty()) { |
| 159 | // Initialize non-degenerate obfuscation if it won't upset |
| 160 | // existing, non-obfuscated data. |
| 161 | std::vector<unsigned char> new_key = CreateObfuscateKey(); |
| 162 | |
| 163 | // Write `new_key` so we don't obfuscate the key with itself |
| 164 | Write(OBFUSCATE_KEY_KEY, new_key); |
| 165 | obfuscate_key = new_key; |
| 166 | |
| 167 | LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(path), HexStr(obfuscate_key)); |
| 168 | } |
| 169 | |
| 170 | LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(path), HexStr(obfuscate_key)); |
| 171 | } |
| 172 | |
| 173 | CDBWrapper::~CDBWrapper() |
| 174 | { |
nothing calls this directly
no test coverage detected