| 34 | |
| 35 | template <typename Data> |
| 36 | bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data) |
| 37 | { |
| 38 | // Generate random temporary filename |
| 39 | unsigned short randv = 0; |
| 40 | GetRandBytes((unsigned char*)&randv, sizeof(randv)); |
| 41 | std::string tmpfn = strprintf("%s.%04x", prefix, randv); |
| 42 | |
| 43 | // open temp output file, and associate with CAutoFile |
| 44 | fs::path pathTmp = GetDataDir() / tmpfn; |
| 45 | FILE *file = fsbridge::fopen(pathTmp, "wb"); |
| 46 | CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); |
| 47 | if (fileout.IsNull()) |
| 48 | return error("%s: Failed to open file %s", __func__, pathTmp.string()); |
| 49 | |
| 50 | // Serialize |
| 51 | if (!SerializeDB(fileout, data)) return false; |
| 52 | if (!FileCommit(fileout.Get())) |
| 53 | return error("%s: Failed to flush file %s", __func__, pathTmp.string()); |
| 54 | fileout.fclose(); |
| 55 | |
| 56 | // replace existing file, if any, with new file |
| 57 | if (!RenameOver(pathTmp, path)) |
| 58 | return error("%s: Rename-into-place failed", __func__); |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | template <typename Stream, typename Data> |
| 64 | bool DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true) |
no test coverage detected