On client shutdown write directly into "peers.dat"...

...and not into a temporary file which gets renamed to "peers.dat" later.
This prevents softlinks from being replaced by a "real" file, see http://jira.darkcoin.qa/browse/DRK-122

Update of https://github.com/darkcoin/darkcoin/pull/63 to the new Bitcoin codebase.
This commit is contained in:
crowning- 2014-12-13 15:52:57 +01:00
parent 5d14e8c6b3
commit 6b8fb97182

View File

@ -2002,11 +2002,6 @@ CAddrDB::CAddrDB()
bool CAddrDB::Write(const CAddrMan& addr) bool CAddrDB::Write(const CAddrMan& addr)
{ {
// Generate random temporary filename
unsigned short randv = 0;
RAND_bytes((unsigned char *)&randv, sizeof(randv));
std::string tmpfn = strprintf("peers.dat.%04x", randv);
// serialize addresses, checksum data up to that point, then append csum // serialize addresses, checksum data up to that point, then append csum
CDataStream ssPeers(SER_DISK, CLIENT_VERSION); CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
ssPeers << FLATDATA(Params().MessageStart()); ssPeers << FLATDATA(Params().MessageStart());
@ -2014,12 +2009,12 @@ bool CAddrDB::Write(const CAddrMan& addr)
uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash; ssPeers << hash;
// open temp output file, and associate with CAutoFile // open output file, and associate with CAutoFile
boost::filesystem::path pathTmp = GetDataDir() / tmpfn; boost::filesystem::path pathAddr = GetDataDir() / "peers.dat";
FILE *file = fopen(pathTmp.string().c_str(), "wb"); FILE *file = fopen(pathAddr.string().c_str(), "wb");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout) if (!fileout)
return error("%s : Failed to open file %s", __func__, pathTmp.string()); return error("%s : Failed to open file %s", __func__, pathAddr.string());
// Write and commit header, data // Write and commit header, data
try { try {
@ -2031,10 +2026,6 @@ bool CAddrDB::Write(const CAddrMan& addr)
FileCommit(fileout); FileCommit(fileout);
fileout.fclose(); fileout.fclose();
// replace existing peers.dat, if any, with new peers.dat.XXXX
if (!RenameOver(pathTmp, pathAddr))
return error("%s : Rename-into-place failed", __func__);
return true; return true;
} }