MCPcopy Create free account
hub / github.com/ElementsProject/elements / SerializeFileDB

Function SerializeFileDB

src/addrdb.cpp:47–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45
46template <typename Data>
47bool 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
86template <typename Stream, typename Data>
87void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)

Callers 2

DumpPeerAddressesFunction · 0.85
DumpAnchorsFunction · 0.85

Calls 10

GetRandBytesFunction · 0.85
fopenFunction · 0.85
errorFunction · 0.85
PathToStringFunction · 0.85
SerializeDBFunction · 0.85
FileCommitFunction · 0.85
RenameOverFunction · 0.85
IsNullMethod · 0.45
fcloseMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected