Skip to content

Commit

Permalink
addrdb: Remove temporary files created in SerializeFileDB. Fixes non-…
Browse files Browse the repository at this point in the history
…determinism in unit tests.

Github-Pull: #16212
Rebased-From: d975338
  • Loading branch information
practicalswift authored and fanquake committed Sep 23, 2019
1 parent c52dd12 commit 1175410
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/addrdb.cpp
Expand Up @@ -44,18 +44,30 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fsbridge::fopen(pathTmp, "wb");
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());
}

// Serialize
if (!SerializeDB(fileout, data)) return false;
if (!FileCommit(fileout.Get()))
if (!SerializeDB(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))
if (!RenameOver(pathTmp, path)) {
remove(pathTmp);
return error("%s: Rename-into-place failed", __func__);
}

return true;
}
Expand Down

0 comments on commit 1175410

Please sign in to comment.