| 1517 | CAddrDB::CAddrDB() { pathAddr = GetDataDir() / "peers.dat"; } |
| 1518 | |
| 1519 | bool CAddrDB::Write(const CAddrMan& addr) { |
| 1520 | // Generate random temporary filename |
| 1521 | uint16_t randv = 0; |
| 1522 | RAND_bytes((uint8_t*)&randv, sizeof(randv)); |
| 1523 | string tmpfn = strprintf("peers.dat.%04x", randv); |
| 1524 | |
| 1525 | // serialize addresses, checksum data up to that point, then append csum |
| 1526 | CDataStream ssPeers(SER_DISK, CLIENT_VERSION); |
| 1527 | ssPeers << FLATDATA(SysCfg().MessageStart()); |
| 1528 | ssPeers << addr; |
| 1529 | uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); |
| 1530 | ssPeers << hash; |
| 1531 | |
| 1532 | // open temp output file, and associate with CAutoFile |
| 1533 | boost::filesystem::path pathTmp = GetDataDir() / tmpfn; |
| 1534 | FILE* file = fopen(pathTmp.string().c_str(), "wb"); |
| 1535 | CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); |
| 1536 | if (!fileout) |
| 1537 | return ERRORMSG("Failed to open file %s", pathTmp.string()); |
| 1538 | |
| 1539 | // Write and commit header, data |
| 1540 | try { |
| 1541 | fileout << ssPeers; |
| 1542 | } catch (std::exception& e) { |
| 1543 | return ERRORMSG("Serialize or I/O error - %s", e.what()); |
| 1544 | } |
| 1545 | FileCommit(fileout); |
| 1546 | fileout.fclose(); |
| 1547 | |
| 1548 | // replace existing peers.dat, if any, with new peers.dat.XXXX |
| 1549 | if (!RenameOver(pathTmp, pathAddr)) |
| 1550 | return ERRORMSG("Rename-into-path failed"); |
| 1551 | |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
| 1555 | bool CAddrDB::Read(CAddrMan& addr) { |
| 1556 | // open input file, and associate with CAutoFile |
no test coverage detected