Skip to content

Commit 629ff8c

Browse files
committed
-includeconf=<path> support in config handler, for including external configuration files
1 parent a0079d4 commit 629ff8c

File tree

8 files changed

+41
-7
lines changed

8 files changed

+41
-7
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int AppInitRPC(int argc, char* argv[])
107107
return EXIT_FAILURE;
108108
}
109109
try {
110-
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
110+
gArgs.ReadConfigFiles();
111111
} catch (const std::exception& e) {
112112
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
113113
return EXIT_FAILURE;

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool AppInit(int argc, char* argv[])
9292
}
9393
try
9494
{
95-
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
95+
gArgs.ReadConfigFiles();
9696
} catch (const std::exception& e) {
9797
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
9898
return false;

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ std::string HelpMessage(HelpMessageMode mode)
361361
strUsage += HelpMessageOpt("-debuglogfile=<file>", strprintf(_("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)"), DEFAULT_DEBUGLOGFILE));
362362
if (showDebug)
363363
strUsage += HelpMessageOpt("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER));
364+
strUsage += HelpMessageOpt("-includeconf=<file>", _("Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)"));
364365
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file on startup"));
365366
strUsage += HelpMessageOpt("-maxmempool=<n>", strprintf(_("Keep the transaction memory pool below <n> megabytes (default: %u)"), DEFAULT_MAX_MEMPOOL_SIZE));
366367
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NodeImpl : public Node
5252
{
5353
gArgs.ParseParameters(argc, argv);
5454
}
55-
void readConfigFile(const std::string& conf_path) override { gArgs.ReadConfigFile(conf_path); }
55+
void readConfigFiles() override { gArgs.ReadConfigFiles(); }
5656
bool softSetArg(const std::string& arg, const std::string& value) override { return gArgs.SoftSetArg(arg, value); }
5757
bool softSetBoolArg(const std::string& arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); }
5858
void selectParams(const std::string& network) override { SelectParams(network); }

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Node
4848
virtual bool softSetBoolArg(const std::string& arg, bool value) = 0;
4949

5050
//! Load settings from configuration file.
51-
virtual void readConfigFile(const std::string& conf_path) = 0;
51+
virtual void readConfigFiles() = 0;
5252

5353
//! Choose network parameters.
5454
virtual void selectParams(const std::string& network) = 0;

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ int main(int argc, char *argv[])
616616
return EXIT_FAILURE;
617617
}
618618
try {
619-
node->readConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
619+
node->readConfigFiles();
620620
} catch (const std::exception& e) {
621621
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME),
622622
QObject::tr("Error: Cannot parse configuration file: %1. Only use key=value syntax.").arg(e.what()));

src/util.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,17 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[])
445445
m_override_args[key].push_back(val);
446446
}
447447
}
448+
449+
// we do not allow -includeconf from command line, so we clear it here
450+
auto it = m_override_args.find("-includeconf");
451+
if (it != m_override_args.end()) {
452+
if (it->second.size() > 0) {
453+
for (const auto& ic : it->second) {
454+
fprintf(stderr, "warning: -includeconf cannot be used from commandline; ignoring -includeconf=%s\n", ic.c_str());
455+
}
456+
m_override_args.erase(it);
457+
}
458+
}
448459
}
449460

450461
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
@@ -706,18 +717,40 @@ void ArgsManager::ReadConfigStream(std::istream& stream)
706717
}
707718
}
708719

709-
void ArgsManager::ReadConfigFile(const std::string& confPath)
720+
void ArgsManager::ReadConfigFiles()
710721
{
711722
{
712723
LOCK(cs_args);
713724
m_config_args.clear();
714725
}
715726

727+
const std::string confPath = GetArg("-conf", BITCOIN_CONF_FILENAME);
716728
fs::ifstream stream(GetConfigFile(confPath));
717729

718730
// ok to not have a config file
719731
if (stream.good()) {
720732
ReadConfigStream(stream);
733+
// if there is an -includeconf in the override args, but it is empty, that means the user
734+
// passed '-noincludeconf' on the command line, in which case we should not include anything
735+
if (m_override_args.count("-includeconf") == 0) {
736+
std::vector<std::string> includeconf(GetArgs("-includeconf"));
737+
{
738+
// We haven't set m_network yet (that happens in SelectParams()), so manually check
739+
// for network.includeconf args.
740+
std::vector<std::string> includeconf_net(GetArgs(std::string("-") + GetChainName() + ".includeconf"));
741+
includeconf.insert(includeconf.end(), includeconf_net.begin(), includeconf_net.end());
742+
}
743+
744+
for (const std::string& to_include : includeconf) {
745+
fs::ifstream include_config(GetConfigFile(to_include));
746+
if (include_config.good()) {
747+
ReadConfigStream(include_config);
748+
LogPrintf("Included configuration file %s\n", to_include.c_str());
749+
} else {
750+
fprintf(stderr, "Failed to include configuration file %s\n", to_include.c_str());
751+
}
752+
}
753+
}
721754
}
722755

723756
// If datadir is changed in .conf file:

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class ArgsManager
140140
void SelectConfigNetwork(const std::string& network);
141141

142142
void ParseParameters(int argc, const char*const argv[]);
143-
void ReadConfigFile(const std::string& confPath);
143+
void ReadConfigFiles();
144144

145145
/**
146146
* Log warnings for options in m_section_only_args when

0 commit comments

Comments
 (0)