<commit_before>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <wallet/db.h>

#include <addrman.h>
#include <fs.h>
#include <hash.h>
#include <protocol.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <wallet/walletutil.h>

#include <boost/thread.hpp> // boost::this_thread::interruption_point() (mingw)

#include <cstdint>
#ifndef WIN32
#include <sys/stat.h>
#endif

namespace {
//! Make sure database has a unique fileid within the environment. If it
//! doesn't, throw an error. BDB caches do not work properly when more than one
//! open database has the same fileid (values written to one database may show
//! up in reads to other databases).
//!
//! BerkeleyDB generates unique fileids by default
//! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html),
//! so bitcoin should never create different databases with the same fileid, but
//! this error can be triggered if users manually copy database files.
void CheckUniqueFileid(const BerkeleyEnvironment &env,
                       const std::string &filename, Db &db,
                       WalletDatabaseFileId &fileid) {
    if (env.IsMock()) {
        return;
    }

    int ret = db.get_mpf()->get_fileid(fileid.value);
    if (ret != 0) {
        throw std::runtime_error(strprintf(
            "BerkeleyBatch: Can't open database %s (get_fileid failed with %d)",
            filename, ret));
    }

    for (const auto &item : env.m_fileids) {
        if (fileid == item.second && &fileid != &item.second) {
            throw std::runtime_error(strprintf(
                "BerkeleyBatch: Can't open database %s (duplicates fileid %s "
                "from %s)",
                filename,
                HexStr(std::begin(item.second.value),
                       std::end(item.second.value)),
                item.first));
        }
    }
}

CCriticalSection cs_db;

//!< Map from directory name to open db environment.
std::map<std::string, BerkeleyEnvironment> g_dbenvs;
} // namespace

bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId &rhs) const {
    return memcmp(value, &rhs.value, sizeof(value)) == 0;
}

static void SplitWalletPath(const fs::path &wallet_path,<commit_msg>Add Clang thread safety annotations for variables guarded by cs_db<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <wallet/db.h>

#include <addrman.h>
#include <fs.h>
#include <hash.h>
#include <protocol.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <wallet/walletutil.h>

#include <boost/thread.hpp> // boost::this_thread::interruption_point() (mingw)

#include <cstdint>
#ifndef WIN32
#include <sys/stat.h>
#endif

namespace {
//! Make sure database has a unique fileid within the environment. If it
//! doesn't, throw an error. BDB caches do not work properly when more than one
//! open database has the same fileid (values written to one database may show
//! up in reads to other databases).
//!
//! BerkeleyDB generates unique fileids by default
//! (https://docs.oracle.com/cd/E17275_01/html/programmer_reference/program_copy.html),
//! so bitcoin should never create different databases with the same fileid, but
//! this error can be triggered if users manually copy database files.
void CheckUniqueFileid(const BerkeleyEnvironment &env,
                       const std::string &filename, Db &db,
                       WalletDatabaseFileId &fileid) {
    if (env.IsMock()) {
        return;
    }

    int ret = db.get_mpf()->get_fileid(fileid.value);
    if (ret != 0) {
        throw std::runtime_error(strprintf(
            "BerkeleyBatch: Can't open database %s (get_fileid failed with %d)",
            filename, ret));
    }

    for (const auto &item : env.m_fileids) {
        if (fileid == item.second && &fileid != &item.second) {
            throw std::runtime_error(strprintf(
                "BerkeleyBatch: Can't open database %s (duplicates fileid %s "
                "from %s)",
                filename,
                HexStr(std::begin(item.second.value),
                       std::end(item.second.value)),
                item.first));
        }
    }
}

CCriticalSection cs_db;

//!< Map from directory name to open db environment.
std::map<std::string, BerkeleyEnvironment> g_dbenvs GUARDED_BY(cs_db);
} // namespace

bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId &rhs) const {
    return memcmp(value, &rhs.value, sizeof(value)) == 0;
}

static void SplitWalletPath(const fs::path &wallet_path,
{'fraction_non_alphanumeric': 0.12164652877329511, 'fraction_numerical': 0.011058775343026827, 'mean_word_length': 3.0397022332506203, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7175222', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1439, 'n_words': 441}