| 45 | |
| 46 | template <typename Data> |
| 47 | bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version) |
| 48 | { |
| 49 | // Generate random temporary filename |
| 50 | uint16_t randv = 0; |
| 51 | GetRandBytes((unsigned char*)&randv, sizeof(randv)); |
| 52 | std::string tmpfn = strprintf("%s.%04x", prefix, randv); |
| 53 | |
| 54 | // open temp output file, and associate with CAutoFile |
| 55 | fs::path pathTmp = gArgs.GetDataDirNet() / tmpfn; |
| 56 | FILE *file = fsbridge::fopen(pathTmp, "wb"); |
| 57 | CAutoFile fileout(file, SER_DISK, version); |
| 58 | if (fileout.IsNull()) { |
| 59 | fileout.fclose(); |
| 60 | remove(pathTmp); |
| 61 | return error("%s: Failed to open file %s", __func__, fs::PathToString(pathTmp)); |
| 62 | } |
| 63 | |
| 64 | // Serialize |
| 65 | if (!SerializeDB(fileout, data)) { |
| 66 | fileout.fclose(); |
| 67 | remove(pathTmp); |
| 68 | return false; |
| 69 | } |
| 70 | if (!FileCommit(fileout.Get())) { |
| 71 | fileout.fclose(); |
| 72 | remove(pathTmp); |
| 73 | return error("%s: Failed to flush file %s", __func__, fs::PathToString(pathTmp)); |
| 74 | } |
| 75 | fileout.fclose(); |
| 76 | |
| 77 | // replace existing file, if any, with new file |
| 78 | if (!RenameOver(pathTmp, path)) { |
| 79 | remove(pathTmp); |
| 80 | return error("%s: Rename-into-place failed", __func__); |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | template <typename Stream, typename Data> |
| 87 | void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true) |
no test coverage detected