Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize getfilecontents() to reduce chance of race for client_state.xml #1426

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5069,41 +5069,31 @@ int GetFilesize(FILE* file)
}




std::string getfilecontents(std::string filename)
{
std::string buffer;
std::string line;
ifstream myfile;
if (fDebug10) LogPrintf("loading file to string %s",filename);
if (!filesystem::exists(filename)) {
LogPrintf("getfilecontents: file does not exist %s", filename);
return "-1";
}

filesystem::path path = filename;
std::ifstream in(filename, std::ios::in | std::ios::binary);

if (!filesystem::exists(path)) {
LogPrintf("the file does not exist %s",path.string());
if (in.fail()) {
LogPrintf("getfilecontents: error opening file %s", filename);
return "-1";
}

FILE *file = fopen(filename.c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
int fileSize = GetFilesize(filein);
filein.fclose();
if (fDebug10) LogPrintf("loading file to string %s", filename);

myfile.open(filename.c_str());
std::ostringstream out;

buffer.reserve(fileSize);
if (fDebug10) LogPrintf("opening file %s",filename);
out << in.rdbuf();

if(myfile)
{
while(getline(myfile, line))
{
buffer = buffer + line + "\n";
}
}
myfile.close();
return buffer;
// Immediately close instead of waiting for the destructor to decrease the
// chance of a race when calling this to read BOINC's client_state.xml:
in.close();

return out.str();
}

bool IsCPIDValidv3(std::string cpidv2, bool allow_investor)
Expand Down Expand Up @@ -7448,8 +7438,8 @@ void HarvestCPIDs(bool cleardata)
try
{
std::string sourcefile = GetBoincDataDir() + "client_state.xml";
std::string sout = "";
sout = getfilecontents(sourcefile);
std::string sout = getfilecontents(sourcefile);

if (sout == "-1")
{
LogPrintf("Unable to obtain Boinc CPIDs ");
Expand Down