diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 492f5dea3..1762af70a 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -1,146 +1,153 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // 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 #include #include #include #include #include #include #include #include namespace { template bool SerializeDB(const CChainParams &chainParams, Stream &stream, const Data &data) { // Write and commit header, data try { CHashWriter hasher(SER_DISK, CLIENT_VERSION); stream << chainParams.DiskMagic() << data; hasher << chainParams.DiskMagic() << data; stream << hasher.GetHash(); } catch (const std::exception &e) { return error("%s: Serialize or I/O error - %s", __func__, e.what()); } return true; } template bool SerializeFileDB(const CChainParams &chainParams, const std::string &prefix, const fs::path &path, const Data &data) { // Generate random temporary filename unsigned short randv = 0; GetRandBytes((uint8_t *)&randv, sizeof(randv)); std::string tmpfn = strprintf("%s.%04x", prefix, randv); // open temp output file, and associate with CAutoFile fs::path pathTmp = GetDataDir() / tmpfn; FILE *file = fsbridge::fopen(pathTmp, "wb"); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); if (fileout.IsNull()) { + fileout.fclose(); + remove(pathTmp); return error("%s: Failed to open file %s", __func__, pathTmp.string()); } // Serialize if (!SerializeDB(chainParams, fileout, data)) { + fileout.fclose(); + remove(pathTmp); return false; } if (!FileCommit(fileout.Get())) { + fileout.fclose(); + remove(pathTmp); return error("%s: Failed to flush file %s", __func__, pathTmp.string()); } fileout.fclose(); // replace existing file, if any, with new file if (!RenameOver(pathTmp, path)) { + remove(pathTmp); return error("%s: Rename-into-place failed", __func__); } return true; } template bool DeserializeDB(const CChainParams &chainParams, Stream &stream, Data &data, bool fCheckSum = true) { try { CHashVerifier verifier(&stream); // de-serialize file header (network specific magic number) and .. uint8_t pchMsgTmp[4]; verifier >> pchMsgTmp; // ... verify the network matches ours if (memcmp(pchMsgTmp, std::begin(chainParams.DiskMagic()), sizeof(pchMsgTmp))) { return error("%s: Invalid network magic number", __func__); } // de-serialize data verifier >> data; // verify checksum if (fCheckSum) { uint256 hashTmp; stream >> hashTmp; if (hashTmp != verifier.GetHash()) { return error("%s: Checksum mismatch, data corrupted", __func__); } } } catch (const std::exception &e) { return error("%s: Deserialize or I/O error - %s", __func__, e.what()); } return true; } template bool DeserializeFileDB(const CChainParams &chainParams, const fs::path &path, Data &data) { // open input file, and associate with CAutoFile FILE *file = fsbridge::fopen(path, "rb"); CAutoFile filein(file, SER_DISK, CLIENT_VERSION); if (filein.IsNull()) { return error("%s: Failed to open file %s", __func__, path.string()); } return DeserializeDB(chainParams, filein, data); } } // namespace CBanDB::CBanDB(fs::path ban_list_path, const CChainParams &_chainParams) : m_ban_list_path(std::move(ban_list_path)), chainParams(_chainParams) {} bool CBanDB::Write(const banmap_t &banSet) { return SerializeFileDB(chainParams, "banlist", m_ban_list_path, banSet); } bool CBanDB::Read(banmap_t &banSet) { return DeserializeFileDB(chainParams, m_ban_list_path, banSet); } CAddrDB::CAddrDB(const CChainParams &chainParamsIn) : chainParams(chainParamsIn) { pathAddr = GetDataDir() / "peers.dat"; } bool CAddrDB::Write(const CAddrMan &addr) { return SerializeFileDB(chainParams, "peers", pathAddr, addr); } bool CAddrDB::Read(CAddrMan &addr) { return DeserializeFileDB(chainParams, pathAddr, addr); } bool CAddrDB::Read(CAddrMan &addr, CDataStream &ssPeers) { bool ret = DeserializeDB(chainParams, ssPeers, addr, false); if (!ret) { // Ensure addrman is left in a clean state addr.Clear(); } return ret; }