| 2032 | } |
| 2033 | |
| 2034 | bool CAddrDB::Read(CAddrMan& addr) |
| 2035 | { |
| 2036 | // open input file, and associate with CAutoFile |
| 2037 | FILE *file = fopen(pathAddr.string().c_str(), "rb"); |
| 2038 | CAutoFile filein(file, SER_DISK, CLIENT_VERSION); |
| 2039 | if (filein.IsNull()) |
| 2040 | return error("%s: Failed to open file %s", __func__, pathAddr.string()); |
| 2041 | |
| 2042 | // use file size to size memory buffer |
| 2043 | int fileSize = boost::filesystem::file_size(pathAddr); |
| 2044 | int dataSize = fileSize - sizeof(uint256); |
| 2045 | // Don't try to resize to a negative number if file is small |
| 2046 | if (dataSize < 0) |
| 2047 | dataSize = 0; |
| 2048 | vector<unsigned char> vchData; |
| 2049 | vchData.resize(dataSize); |
| 2050 | uint256 hashIn; |
| 2051 | |
| 2052 | // read data and checksum from file |
| 2053 | try { |
| 2054 | filein.read((char *)&vchData[0], dataSize); |
| 2055 | filein >> hashIn; |
| 2056 | } |
| 2057 | catch (const std::exception& e) { |
| 2058 | return error("%s: Deserialize or I/O error - %s", __func__, e.what()); |
| 2059 | } |
| 2060 | filein.fclose(); |
| 2061 | |
| 2062 | CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION); |
| 2063 | |
| 2064 | // verify stored checksum matches input data |
| 2065 | uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); |
| 2066 | if (hashIn != hashTmp) |
| 2067 | return error("%s: Checksum mismatch, data corrupted", __func__); |
| 2068 | |
| 2069 | return Read(addr, ssPeers); |
| 2070 | } |
| 2071 | |
| 2072 | bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers) |
| 2073 | { |
no test coverage detected