Skip to content

Commit 5460460

Browse files
committed
Add AbsPathForConfigVal to consolidate datadir prefixing for path args
Most commandline/config args are interpreted as relative to datadir if not passed absolute. Consolidate the logic for this normalization.
1 parent a1e1305 commit 5460460

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

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)