From c1f325126cf51d28dce8da74bfdf5cd05ab237ea Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Mon, 29 Apr 2019 22:46:34 +0300 Subject: Return absolute path early in AbsPathForConfigVal This prevents premature GetDataDir() calls, e.g., when config file is not read yet. --- src/util/system.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/util') diff --git a/src/util/system.cpp b/src/util/system.cpp index 72b37b9187..d9e23199ec 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -1202,6 +1202,9 @@ int64_t GetStartupTime() fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific) { + if (path.is_absolute()) { + return path; + } return fs::absolute(path, GetDataDir(net_specific)); } -- cgit v1.2.3 From 740d41ce9f7fdf209366e930bd0fdcc6b1bc6b79 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 24 Jul 2019 03:21:25 +0300 Subject: Add CheckDataDirOption() function --- src/util/system.cpp | 11 +++++++++-- src/util/system.h | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/util') diff --git a/src/util/system.cpp b/src/util/system.cpp index d9e23199ec..520ed35504 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -741,8 +741,9 @@ const fs::path &GetDataDir(bool fNetSpecific) // this function if (!path.empty()) return path; - if (gArgs.IsArgSet("-datadir")) { - path = fs::system_complete(gArgs.GetArg("-datadir", "")); + std::string datadir = gArgs.GetArg("-datadir", ""); + if (!datadir.empty()) { + path = fs::system_complete(datadir); if (!fs::is_directory(path)) { path = ""; return path; @@ -761,6 +762,12 @@ const fs::path &GetDataDir(bool fNetSpecific) return path; } +bool CheckDataDirOption() +{ + std::string datadir = gArgs.GetArg("-datadir", ""); + return datadir.empty() || fs::is_directory(fs::system_complete(datadir)); +} + void ClearDatadirCache() { LOCK(csPathCached); diff --git a/src/util/system.h b/src/util/system.h index dda9156488..9ed9f1f0df 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -83,6 +83,8 @@ fs::path GetDefaultDataDir(); // The blocks directory is always net specific. const fs::path &GetBlocksDir(); const fs::path &GetDataDir(bool fNetSpecific = true); +// Return true if -datadir option points to a valid directory or is not specified. +bool CheckDataDirOption(); /** Tests only */ void ClearDatadirCache(); fs::path GetConfigFile(const std::string& confPath); -- cgit v1.2.3 From 50824093bb2d68fe1393dfd636fab5f8795faa5d Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 24 Jul 2019 03:29:40 +0300 Subject: Fix datadir handling in bitcoind This prevents premature tries to access or create the default datadir. This is useful when the -datadir option is specified and the default datadir is unreachable. --- src/util/system.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/util') diff --git a/src/util/system.cpp b/src/util/system.cpp index 520ed35504..4d8aa9ed90 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -941,7 +941,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys) // If datadir is changed in .conf file: ClearDatadirCache(); - if (!fs::is_directory(GetDataDir(false))) { + if (!CheckDataDirOption()) { error = strprintf("specified data directory \"%s\" does not exist.", gArgs.GetArg("-datadir", "").c_str()); return false; } -- cgit v1.2.3