Skip to content

Commit f6cd41d

Browse files
committed
Merge #12305: [docs] [refactor] Add help messages for datadir path mangling
5460460 Add AbsPathForConfigVal to consolidate datadir prefixing for path args (James O'Beirne) a1e1305 Clarify help messages for path args to mention datadir prefix (James O'Beirne) Pull request description: Change `-conf`'s help message to indicate that relative path values will be prefixed by the datadir path. This behavior probably merits clarification; it's kind of confusing when attempting to specify a configuration file in the current directory with `-conf=bitcoin.conf`, but instead loading the `bitcoin.conf` file in ~/.bitcoin datadir. ### Edit This PR has been modified to document all cases where relative path configurations are modified to be under datadir. A small refactoring has also been added which consolidates this normalization. Tree-SHA512: be4fc0595fbeba33d17af08f59898af45e76a44f00719ea0282403b155ac6755584604fab765250a3aa14ed6991882c4d1ccbe601184362c5ba97c886bdda344
2 parents 8897135 + 5460460 commit f6cd41d

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::string HelpMessageCli()
3535
std::string strUsage;
3636
strUsage += HelpMessageGroup(_("Options:"));
3737
strUsage += HelpMessageOpt("-?", _("This help message"));
38-
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
38+
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file. Relative paths will be prefixed by datadir location. (default: %s)"), BITCOIN_CONF_FILENAME));
3939
strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
4040
strUsage += HelpMessageOpt("-getinfo", _("Get general information from the remote server. Note that unlike server-side RPC calls, the results of -getinfo is the result of multiple non-atomic requests. Some entries in the result may represent results from different states (e.g. wallet balance may be as of a different block from the chain state reported)"));
4141
AppendParamsHelpMessages(strUsage);

src/init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ std::string HelpMessage(HelpMessageMode mode)
336336
strUsage += HelpMessageOpt("-blockreconstructionextratxn=<n>", strprintf(_("Extra transactions to keep in memory for compact block reconstructions (default: %u)"), DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN));
337337
if (showDebug)
338338
strUsage += HelpMessageOpt("-blocksonly", strprintf(_("Whether to operate in a blocks only mode (default: %u)"), DEFAULT_BLOCKSONLY));
339-
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
339+
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file. Relative paths will be prefixed by datadir location. (default: %s)"), BITCOIN_CONF_FILENAME));
340340
if (mode == HMM_BITCOIND)
341341
{
342342
#if HAVE_DECL_DAEMON
@@ -348,7 +348,7 @@ std::string HelpMessage(HelpMessageMode mode)
348348
strUsage += HelpMessageOpt("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize));
349349
}
350350
strUsage += HelpMessageOpt("-dbcache=<n>", strprintf(_("Set database cache size in megabytes (%d to %d, default: %d)"), nMinDbCache, nMaxDbCache, nDefaultDbCache));
351-
strUsage += HelpMessageOpt("-debuglogfile=<file>", strprintf(_("Specify location of debug log file: this can be an absolute path or a path relative to the data directory (default: %s)"), DEFAULT_DEBUGLOGFILE));
351+
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));
352352
if (showDebug)
353353
strUsage += HelpMessageOpt("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER));
354354
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file on startup"));
@@ -362,7 +362,7 @@ std::string HelpMessage(HelpMessageMode mode)
362362
-GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS));
363363
strUsage += HelpMessageOpt("-persistmempool", strprintf(_("Whether to save the mempool on shutdown and load on restart (default: %u)"), DEFAULT_PERSIST_MEMPOOL));
364364
#ifndef WIN32
365-
strUsage += HelpMessageOpt("-pid=<file>", strprintf(_("Specify pid file (default: %s)"), BITCOIN_PID_FILENAME));
365+
strUsage += HelpMessageOpt("-pid=<file>", strprintf(_("Specify pid file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)"), BITCOIN_PID_FILENAME));
366366
#endif
367367
strUsage += HelpMessageOpt("-prune=<n>", strprintf(_("Reduce storage requirements by enabling pruning (deleting) of old blocks. This allows the pruneblockchain RPC to be called to delete specific blocks, and enables automatic pruning of old blocks if a target size in MiB is provided. This mode is incompatible with -txindex and -rescan. "
368368
"Warning: Reverting this setting requires re-downloading the entire blockchain. "
@@ -500,7 +500,7 @@ std::string HelpMessage(HelpMessageMode mode)
500500
strUsage += HelpMessageOpt("-rpcallowip=<ip>", _("Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option can be specified multiple times"));
501501
strUsage += HelpMessageOpt("-rpcauth=<userpw>", _("Username and hashed password for JSON-RPC connections. The field <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included in share/rpcuser. The client then connects normally using the rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This option can be specified multiple times"));
502502
strUsage += HelpMessageOpt("-rpcbind=<addr>[:port]", _("Bind to given address to listen for JSON-RPC connections. This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)"));
503-
strUsage += HelpMessageOpt("-rpccookiefile=<loc>", _("Location of the auth cookie (default: data dir)"));
503+
strUsage += HelpMessageOpt("-rpccookiefile=<loc>", _("Location of the auth cookie. Relative paths will be prefixed by a net-specific datadir location. (default: data dir)"));
504504
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
505505
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Listen for JSON-RPC connections on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
506506
strUsage += HelpMessageOpt("-rpcserialversion", strprintf(_("Sets the serialization of raw transaction or block hex returned in non-verbose mode, non-segwit(0) or segwit(1) (default: %d)"), DEFAULT_RPC_SERIALIZE_VERSION));

src/rpc/protocol.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ static fs::path GetAuthCookieFile(bool temp=false)
7272
if (temp) {
7373
arg += ".tmp";
7474
}
75-
fs::path path(arg);
76-
if (!path.is_complete()) path = GetDataDir() / path;
77-
return path;
75+
return AbsPathForConfigVal(fs::path(arg));
7876
}
7977

8078
bool GenerateAuthCookie(std::string *cookie_out)

src/util.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <util.h>
7+
#include <fs.h>
78

89
#include <chainparamsbase.h>
910
#include <random.h>
@@ -188,11 +189,7 @@ static void DebugPrintInit()
188189
fs::path GetDebugLogPath()
189190
{
190191
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
191-
if (logfile.is_absolute()) {
192-
return logfile;
193-
} else {
194-
return GetDataDir() / logfile;
195-
}
192+
return AbsPathForConfigVal(logfile);
196193
}
197194

198195
bool OpenDebugLog()
@@ -624,11 +621,7 @@ void ClearDatadirCache()
624621

625622
fs::path GetConfigFile(const std::string& confPath)
626623
{
627-
fs::path pathConfigFile(confPath);
628-
if (!pathConfigFile.is_complete())
629-
pathConfigFile = GetDataDir(false) / pathConfigFile;
630-
631-
return pathConfigFile;
624+
return AbsPathForConfigVal(fs::path(confPath), false);
632625
}
633626

634627
void ArgsManager::ReadConfigFile(const std::string& confPath)
@@ -663,9 +656,7 @@ void ArgsManager::ReadConfigFile(const std::string& confPath)
663656
#ifndef WIN32
664657
fs::path GetPidFile()
665658
{
666-
fs::path pathPidFile(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME));
667-
if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
668-
return pathPidFile;
659+
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
669660
}
670661

671662
void CreatePidFile(const fs::path &path, pid_t pid)
@@ -936,3 +927,8 @@ int64_t GetStartupTime()
936927
{
937928
return nStartupTime;
938929
}
930+
931+
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
932+
{
933+
return fs::absolute(path, GetDataDir(net_specific));
934+
}

src/util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ bool OpenDebugLog();
191191
void ShrinkDebugFile();
192192
void runCommand(const std::string& strCommand);
193193

194+
/**
195+
* Most paths passed as configuration arguments are treated as relative to
196+
* the datadir if they are not absolute.
197+
*
198+
* @param path The path to be conditionally prefixed with datadir.
199+
* @param net_specific Forwarded to GetDataDir().
200+
* @return The normalized path.
201+
*/
202+
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
203+
194204
inline bool IsSwitchChar(char c)
195205
{
196206
#ifdef WIN32

0 commit comments

Comments
 (0)