0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

addrdb: Remove temporary files created in SerializeFileDB. Fixes non-determinism in unit tests.

This commit is contained in:
practicalswift 2019-06-14 08:30:43 +02:00
parent 7524376a81
commit d9753383b9

View file

@ -44,18 +44,30 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
fs::path pathTmp = GetDataDir() / tmpfn; fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fsbridge::fopen(pathTmp, "wb"); FILE *file = fsbridge::fopen(pathTmp, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull()) if (fileout.IsNull()) {
fileout.fclose();
remove(pathTmp);
return error("%s: Failed to open file %s", __func__, pathTmp.string()); return error("%s: Failed to open file %s", __func__, pathTmp.string());
}
// Serialize // Serialize
if (!SerializeDB(fileout, data)) return false; if (!SerializeDB(fileout, data)) {
if (!FileCommit(fileout.Get())) 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()); return error("%s: Failed to flush file %s", __func__, pathTmp.string());
}
fileout.fclose(); fileout.fclose();
// replace existing file, if any, with new file // replace existing file, if any, with new file
if (!RenameOver(pathTmp, path)) if (!RenameOver(pathTmp, path)) {
remove(pathTmp);
return error("%s: Rename-into-place failed", __func__); return error("%s: Rename-into-place failed", __func__);
}
return true; return true;
} }