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

util: clean obsolete config keys #2424

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions src/gridcoin/gridcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,55 @@ void GRC::ScheduleBackgroundJobs(CScheduler& scheduler)
ScheduleUpdateChecks(scheduler);
ScheduleBeaconDBPassivation(scheduler);
}

bool GRC::CleanConfig() {
fsbridge::ifstream orig_config(GetConfigFile());
if (!orig_config.good()) {
return false;
}

bool commit{false};
std::vector<std::string> new_config_lines;

std::string obsolete_keys[] = {"privatekey", "AutoUpgrade", "cpumining", "enablespeech",
"NEURAL_", "PrimaryCPID", "publickey", "SessionGuid",
"suppressupgrade", "tickers", "UpdatingLeaderboard"};

std::string line;
while (std::getline(orig_config, line)) {
if (!commit) {
commit |= line.rfind(obsolete_keys[0], 0) != std::string::npos;
}

for (const auto& key : obsolete_keys) {
if (line.rfind(key, 0) != std::string::npos) {
goto skip;
}
}
new_config_lines.push_back(line);
skip:;
}
orig_config.close();

if (commit) {
if (!BackupConfigFile(GetBackupFilename("gridcoinresearch.conf"))) {
return error("%s: Config backup failed.", __func__);
}

try {
fs::path new_config_path(GetConfigFile().replace_extension("conf~"));
fsbridge::ofstream new_config_file(new_config_path);

std::ostream_iterator<std::string> out(new_config_file, "\n");
std::copy(new_config_lines.begin(), new_config_lines.end(), out);

new_config_file.close();
fs::rename(new_config_path, GetConfigFile());
} catch (const fs::filesystem_error& e) {
return error("%s: Error saving config: %s", __func__, e.what());
}
}

return true;
}

8 changes: 8 additions & 0 deletions src/gridcoin/gridcoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ void CloseResearcherRegistryFile();
//! \param scheduler Scheduler instance to register jobs with.
//!
void ScheduleBackgroundJobs(CScheduler& scheduler);

//!
//! \brief Cleans the config file of obsolete config keys. Might not make changes
//! if a specific key is not present.
//!
//! \return \c true if no errors occurred.
//!
bool CleanConfig();
} // namespace GRC

#endif // GRIDCOIN_GRIDCOIN_H
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ bool AppInit2(ThreadHandlerPtr threads)
{
LogPrintf("WARNING: failed to create configuration file: %s", e.what());
}
} else {
if (!GRC::CleanConfig()) {
InitWarning("Failed to clean obsolete config keys.");
}
}

//6-10-2014: R Halford: Updating Boost version to 1.5.5 to prevent sync issues; print the boost version to verify:
Expand Down