2011-05-14 17:25:05 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2019-06-27 20:11:25 +02:00
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-03-31 15:22:45 +02:00
|
|
|
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/system.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <chainparamsbase.h>
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/strencodings.h>
|
2019-06-17 09:56:52 +02:00
|
|
|
#include <util/translation.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-03-27 13:19:49 +01:00
|
|
|
#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-28 00:07:51 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
// for posix_fallocate
|
2014-08-12 12:05:03 +02:00
|
|
|
#ifdef __linux__
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#ifdef _POSIX_C_SOURCE
|
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2013-01-28 00:07:51 +01:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-08-12 12:05:03 +02:00
|
|
|
#endif // __linux__
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2013-07-17 04:20:09 +02:00
|
|
|
#include <algorithm>
|
2013-01-28 00:07:51 +01:00
|
|
|
#include <fcntl.h>
|
2018-03-06 18:50:20 +01:00
|
|
|
#include <sched.h>
|
2013-04-26 00:46:47 +02:00
|
|
|
#include <sys/resource.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <sys/stat.h>
|
2012-04-15 11:42:40 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#else
|
2012-04-15 22:10:54 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable:4786)
|
|
|
|
#pragma warning(disable:4804)
|
|
|
|
#pragma warning(disable:4805)
|
|
|
|
#pragma warning(disable:4717)
|
|
|
|
#endif
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#ifdef _WIN32_IE
|
|
|
|
#undef _WIN32_IE
|
|
|
|
#endif
|
2012-05-03 02:21:43 +02:00
|
|
|
#define _WIN32_IE 0x0501
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
2018-08-06 03:03:33 +02:00
|
|
|
#include <codecvt>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-05-17 18:30:09 +02:00
|
|
|
#include <io.h> /* for _commit */
|
2018-08-05 18:38:25 +02:00
|
|
|
#include <shellapi.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <shlobj.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#endif
|
2011-05-14 17:25:05 +02:00
|
|
|
|
2017-03-30 09:37:47 +02:00
|
|
|
#ifdef HAVE_MALLOPT_ARENA_MAX
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-25 03:34:23 +02:00
|
|
|
#include <thread>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-05-14 20:18:26 +02:00
|
|
|
// Application startup time (used for uptime calculation)
|
|
|
|
const int64_t nStartupTime = GetTime();
|
2011-05-14 17:25:05 +02:00
|
|
|
|
2015-06-27 21:21:41 +02:00
|
|
|
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
ArgsManager gArgs;
|
2016-11-29 02:00:11 +01:00
|
|
|
|
2018-02-13 13:53:17 +01:00
|
|
|
/** A map that contains all the currently held directory locks. After
|
|
|
|
* successful locking, these will be held here until the global destructor
|
|
|
|
* cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
|
|
|
|
* is called.
|
|
|
|
*/
|
2018-07-25 11:33:22 +02:00
|
|
|
static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks;
|
2018-02-13 13:53:17 +01:00
|
|
|
/** Mutex to protect dir_locks. */
|
|
|
|
static std::mutex cs_dir_locks;
|
|
|
|
|
2017-12-26 07:41:55 +01:00
|
|
|
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
|
|
|
|
{
|
2018-02-13 13:53:17 +01:00
|
|
|
std::lock_guard<std::mutex> ulock(cs_dir_locks);
|
2017-12-26 07:41:55 +01:00
|
|
|
fs::path pathLockFile = directory / lockfile_name;
|
2018-02-13 14:12:30 +01:00
|
|
|
|
|
|
|
// If a lock for this directory already exists in the map, don't try to re-lock it
|
2018-02-13 13:53:17 +01:00
|
|
|
if (dir_locks.count(pathLockFile.string())) {
|
2018-02-13 14:12:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create empty lock file if it doesn't exist.
|
|
|
|
FILE* file = fsbridge::fopen(pathLockFile, "a");
|
2017-12-26 07:41:55 +01:00
|
|
|
if (file) fclose(file);
|
2018-07-25 11:33:22 +02:00
|
|
|
auto lock = MakeUnique<fsbridge::FileLock>(pathLockFile);
|
|
|
|
if (!lock->TryLock()) {
|
|
|
|
return error("Error while attempting to lock directory %s: %s", directory.string(), lock->GetReason());
|
|
|
|
}
|
|
|
|
if (!probe_only) {
|
|
|
|
// Lock successful and we're not just probing, put it into the map
|
|
|
|
dir_locks.emplace(pathLockFile.string(), std::move(lock));
|
2017-12-26 07:41:55 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:05:18 +01:00
|
|
|
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(cs_dir_locks);
|
|
|
|
dir_locks.erase((directory / lockfile_name).string());
|
|
|
|
}
|
|
|
|
|
2018-02-13 13:53:17 +01:00
|
|
|
void ReleaseDirectoryLocks()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> ulock(cs_dir_locks);
|
|
|
|
dir_locks.clear();
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:08:55 +01:00
|
|
|
bool DirIsWritable(const fs::path& directory)
|
|
|
|
{
|
|
|
|
fs::path tmpFile = directory / fs::unique_path();
|
|
|
|
|
|
|
|
FILE* file = fsbridge::fopen(tmpFile, "a");
|
|
|
|
if (!file) return false;
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
remove(tmpFile);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:20:57 +01:00
|
|
|
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes)
|
2018-09-02 00:18:02 +02:00
|
|
|
{
|
2019-01-24 20:20:57 +01:00
|
|
|
constexpr uint64_t min_disk_space = 52428800; // 50 MiB
|
2018-09-02 00:18:02 +02:00
|
|
|
|
2019-01-24 20:20:57 +01:00
|
|
|
uint64_t free_bytes_available = fs::space(dir).available;
|
|
|
|
return free_bytes_available >= min_disk_space + additional_bytes;
|
2018-09-02 00:18:02 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
/**
|
|
|
|
* Interpret a string argument as a boolean.
|
|
|
|
*
|
|
|
|
* The definition of atoi() requires that non-numeric string values like "foo",
|
|
|
|
* return 0. This means that if a user unintentionally supplies a non-integer
|
|
|
|
* argument here, the return value is always false. This means that -foo=false
|
|
|
|
* does what the user probably expects, but -foo=true is well defined but does
|
|
|
|
* not do what they probably expected.
|
|
|
|
*
|
|
|
|
* The return value of atoi() is undefined when given input not representable as
|
|
|
|
* an int. On most systems this means string value between "-2147483648" and
|
|
|
|
* "2147483647" are well defined (this method will return true). Setting
|
|
|
|
* -txindex=2147483648 on most systems, however, is probably undefined.
|
|
|
|
*
|
|
|
|
* For a more extensive discussion of this topic (and a wide range of opinions
|
|
|
|
* on the Right Way to change this code), see PR12713.
|
|
|
|
*/
|
2015-06-15 17:17:23 +02:00
|
|
|
static bool InterpretBool(const std::string& strValue)
|
2012-02-16 21:08:32 +01:00
|
|
|
{
|
2015-06-15 17:17:23 +02:00
|
|
|
if (strValue.empty())
|
|
|
|
return true;
|
|
|
|
return (atoi(strValue) != 0);
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:01:00 +02:00
|
|
|
/** Internal helper functions for ArgsManager */
|
|
|
|
class ArgsManagerHelper {
|
|
|
|
public:
|
|
|
|
typedef std::map<std::string, std::vector<std::string>> MapArgs;
|
|
|
|
|
2018-04-04 10:06:00 +02:00
|
|
|
/** Determine whether to use config settings in the default section,
|
|
|
|
* See also comments around ArgsManager::ArgsManager() below. */
|
2018-08-29 22:33:33 +02:00
|
|
|
static inline bool UseDefaultSection(const ArgsManager& am, const std::string& arg) EXCLUSIVE_LOCKS_REQUIRED(am.cs_args)
|
2018-04-04 10:06:00 +02:00
|
|
|
{
|
|
|
|
return (am.m_network == CBaseChainParams::MAIN || am.m_network_only_args.count(arg) == 0);
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:03:00 +02:00
|
|
|
/** Convert regular argument into the network-specific setting */
|
|
|
|
static inline std::string NetworkArg(const ArgsManager& am, const std::string& arg)
|
|
|
|
{
|
|
|
|
assert(arg.length() > 1 && arg[0] == '-');
|
|
|
|
return "-" + am.m_network + "." + arg.substr(1);
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:01:00 +02:00
|
|
|
/** Find arguments in a map and add them to a vector */
|
|
|
|
static inline void AddArgs(std::vector<std::string>& res, const MapArgs& map_args, const std::string& arg)
|
|
|
|
{
|
|
|
|
auto it = map_args.find(arg);
|
|
|
|
if (it != map_args.end()) {
|
|
|
|
res.insert(res.end(), it->second.begin(), it->second.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true/false if an argument is set in a map, and also
|
|
|
|
* return the first (or last) of the possibly multiple values it has
|
|
|
|
*/
|
|
|
|
static inline std::pair<bool,std::string> GetArgHelper(const MapArgs& map_args, const std::string& arg, bool getLast = false)
|
|
|
|
{
|
|
|
|
auto it = map_args.find(arg);
|
|
|
|
|
|
|
|
if (it == map_args.end() || it->second.empty()) {
|
|
|
|
return std::make_pair(false, std::string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getLast) {
|
|
|
|
return std::make_pair(true, it->second.back());
|
|
|
|
} else {
|
|
|
|
return std::make_pair(true, it->second.front());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the string value of an argument, returning a pair of a boolean
|
|
|
|
* indicating the argument was found, and the value for the argument
|
|
|
|
* if it was found (or the empty string if not found).
|
|
|
|
*/
|
|
|
|
static inline std::pair<bool,std::string> GetArg(const ArgsManager &am, const std::string& arg)
|
|
|
|
{
|
|
|
|
LOCK(am.cs_args);
|
|
|
|
std::pair<bool,std::string> found_result(false, std::string());
|
|
|
|
|
|
|
|
// We pass "true" to GetArgHelper in order to return the last
|
|
|
|
// argument value seen from the command line (so "bitcoind -foo=bar
|
|
|
|
// -foo=baz" gives GetArg(am,"foo")=={true,"baz"}
|
|
|
|
found_result = GetArgHelper(am.m_override_args, arg, true);
|
|
|
|
if (found_result.first) {
|
|
|
|
return found_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// But in contrast we return the first argument seen in a config file,
|
|
|
|
// so "foo=bar \n foo=baz" in the config file gives
|
|
|
|
// GetArg(am,"foo")={true,"bar"}
|
2018-04-04 10:03:00 +02:00
|
|
|
if (!am.m_network.empty()) {
|
|
|
|
found_result = GetArgHelper(am.m_config_args, NetworkArg(am, arg));
|
|
|
|
if (found_result.first) {
|
|
|
|
return found_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:06:00 +02:00
|
|
|
if (UseDefaultSection(am, arg)) {
|
|
|
|
found_result = GetArgHelper(am.m_config_args, arg);
|
|
|
|
if (found_result.first) {
|
|
|
|
return found_result;
|
|
|
|
}
|
2018-04-04 10:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return found_result;
|
|
|
|
}
|
2018-04-04 10:09:00 +02:00
|
|
|
|
|
|
|
/* Special test for -testnet and -regtest args, because we
|
|
|
|
* don't want to be confused by craziness like "[regtest] testnet=1"
|
|
|
|
*/
|
2018-08-29 22:33:33 +02:00
|
|
|
static inline bool GetNetBoolArg(const ArgsManager &am, const std::string& net_arg) EXCLUSIVE_LOCKS_REQUIRED(am.cs_args)
|
2018-04-04 10:09:00 +02:00
|
|
|
{
|
|
|
|
std::pair<bool,std::string> found_result(false,std::string());
|
|
|
|
found_result = GetArgHelper(am.m_override_args, net_arg, true);
|
|
|
|
if (!found_result.first) {
|
|
|
|
found_result = GetArgHelper(am.m_config_args, net_arg, true);
|
|
|
|
if (!found_result.first) {
|
|
|
|
return false; // not set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InterpretBool(found_result.second); // is set, so evaluate
|
|
|
|
}
|
2018-04-04 10:01:00 +02:00
|
|
|
};
|
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
/**
|
|
|
|
* Interpret -nofoo as if the user supplied -foo=0.
|
|
|
|
*
|
2018-04-04 10:02:00 +02:00
|
|
|
* This method also tracks when the -no form was supplied, and if so,
|
|
|
|
* checks whether there was a double-negative (-nofoo=0 -> -foo=1).
|
|
|
|
*
|
|
|
|
* If there was not a double negative, it removes the "no" from the key,
|
|
|
|
* and returns true, indicating the caller should clear the args vector
|
|
|
|
* to indicate a negated option.
|
|
|
|
*
|
|
|
|
* If there was a double negative, it removes "no" from the key, sets the
|
|
|
|
* value to "1" and returns false.
|
|
|
|
*
|
|
|
|
* If there was no "no", it leaves key and value untouched and returns
|
|
|
|
* false.
|
|
|
|
*
|
|
|
|
* Where an option was negated can be later checked using the
|
2018-03-22 03:24:17 +01:00
|
|
|
* IsArgNegated() method. One use case for this is to have a way to disable
|
|
|
|
* options that are not normally boolean (e.g. using -nodebuglogfile to request
|
|
|
|
* that debug log output is not sent to any file at all).
|
|
|
|
*/
|
2018-04-04 10:02:00 +02:00
|
|
|
static bool InterpretNegatedOption(std::string& key, std::string& val)
|
2015-06-15 17:17:23 +02:00
|
|
|
{
|
2018-04-04 10:03:00 +02:00
|
|
|
assert(key[0] == '-');
|
|
|
|
|
|
|
|
size_t option_index = key.find('.');
|
|
|
|
if (option_index == std::string::npos) {
|
|
|
|
option_index = 1;
|
|
|
|
} else {
|
|
|
|
++option_index;
|
|
|
|
}
|
|
|
|
if (key.substr(option_index, 2) == "no") {
|
2018-03-22 03:24:17 +01:00
|
|
|
bool bool_val = InterpretBool(val);
|
2018-04-04 10:03:00 +02:00
|
|
|
key.erase(option_index, 2);
|
2018-03-22 03:24:17 +01:00
|
|
|
if (!bool_val ) {
|
|
|
|
// Double negatives like -nofoo=0 are supported (but discouraged)
|
|
|
|
LogPrintf("Warning: parsed potentially confusing double-negative %s=%s\n", key, val);
|
2018-04-04 10:02:00 +02:00
|
|
|
val = "1";
|
|
|
|
} else {
|
|
|
|
return true;
|
2018-03-22 03:24:17 +01:00
|
|
|
}
|
2012-02-16 21:08:32 +01:00
|
|
|
}
|
2018-04-04 10:02:00 +02:00
|
|
|
return false;
|
2012-02-16 21:08:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 10:06:00 +02:00
|
|
|
ArgsManager::ArgsManager() :
|
|
|
|
/* These options would cause cross-contamination if values for
|
|
|
|
* mainnet were used while running on regtest/testnet (or vice-versa).
|
|
|
|
* Setting them as section_only_args ensures that sharing a config file
|
|
|
|
* between mainnet and regtest/testnet won't cause problems due to these
|
|
|
|
* parameters by accident. */
|
|
|
|
m_network_only_args{
|
|
|
|
"-addnode", "-connect",
|
|
|
|
"-port", "-bind",
|
|
|
|
"-rpcport", "-rpcbind",
|
|
|
|
"-wallet",
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
2018-11-12 03:06:36 +01:00
|
|
|
const std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
|
2018-04-04 10:07:00 +02:00
|
|
|
{
|
2018-11-12 03:06:36 +01:00
|
|
|
std::set<std::string> unsuitables;
|
|
|
|
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
|
|
|
|
2018-04-04 10:07:00 +02:00
|
|
|
// if there's no section selected, don't worry
|
2018-11-12 03:06:36 +01:00
|
|
|
if (m_network.empty()) return std::set<std::string> {};
|
2018-04-04 10:07:00 +02:00
|
|
|
|
|
|
|
// if it's okay to use the default section for this network, don't worry
|
2018-11-12 03:06:36 +01:00
|
|
|
if (m_network == CBaseChainParams::MAIN) return std::set<std::string> {};
|
2018-04-04 10:07:00 +02:00
|
|
|
|
|
|
|
for (const auto& arg : m_network_only_args) {
|
|
|
|
std::pair<bool, std::string> found_result;
|
|
|
|
|
|
|
|
// if this option is overridden it's fine
|
|
|
|
found_result = ArgsManagerHelper::GetArgHelper(m_override_args, arg);
|
|
|
|
if (found_result.first) continue;
|
|
|
|
|
|
|
|
// if there's a network-specific value for this option, it's fine
|
|
|
|
found_result = ArgsManagerHelper::GetArgHelper(m_config_args, ArgsManagerHelper::NetworkArg(*this, arg));
|
|
|
|
if (found_result.first) continue;
|
|
|
|
|
|
|
|
// if there isn't a default value for this option, it's fine
|
|
|
|
found_result = ArgsManagerHelper::GetArgHelper(m_config_args, arg);
|
|
|
|
if (!found_result.first) continue;
|
|
|
|
|
|
|
|
// otherwise, issue a warning
|
2018-11-12 03:06:36 +01:00
|
|
|
unsuitables.insert(arg);
|
2018-04-04 10:07:00 +02:00
|
|
|
}
|
2018-11-12 03:06:36 +01:00
|
|
|
return unsuitables;
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:53:19 +01:00
|
|
|
const std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
|
2018-11-12 03:06:36 +01:00
|
|
|
{
|
|
|
|
// Section names to be recognized in the config file.
|
|
|
|
static const std::set<std::string> available_sections{
|
|
|
|
CBaseChainParams::REGTEST,
|
|
|
|
CBaseChainParams::TESTNET,
|
|
|
|
CBaseChainParams::MAIN
|
|
|
|
};
|
|
|
|
|
|
|
|
LOCK(cs_args);
|
2019-02-04 04:53:19 +01:00
|
|
|
std::list<SectionInfo> unrecognized = m_config_sections;
|
|
|
|
unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.find(appeared.m_name) != available_sections.end(); });
|
|
|
|
return unrecognized;
|
2018-04-04 10:07:00 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 10:03:00 +02:00
|
|
|
void ArgsManager::SelectConfigNetwork(const std::string& network)
|
|
|
|
{
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
2018-04-04 10:03:00 +02:00
|
|
|
m_network = network;
|
|
|
|
}
|
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2016-11-30 03:52:44 +01:00
|
|
|
LOCK(cs_args);
|
2018-04-04 10:01:00 +02:00
|
|
|
m_override_args.clear();
|
2018-03-22 03:24:17 +01:00
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
std::string key(argv[i]);
|
|
|
|
std::string val;
|
|
|
|
size_t is_index = key.find('=');
|
|
|
|
if (is_index != std::string::npos) {
|
|
|
|
val = key.substr(is_index + 1);
|
|
|
|
key.erase(is_index);
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
2012-10-02 21:36:39 +02:00
|
|
|
#ifdef WIN32
|
2018-10-29 09:15:11 +01:00
|
|
|
std::transform(key.begin(), key.end(), key.begin(), ToLower);
|
2018-03-22 03:24:17 +01:00
|
|
|
if (key[0] == '/')
|
|
|
|
key[0] = '-';
|
2012-10-02 21:36:39 +02:00
|
|
|
#endif
|
2014-06-04 06:36:45 +02:00
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
if (key[0] != '-')
|
2011-05-14 17:25:05 +02:00
|
|
|
break;
|
2012-02-06 19:55:11 +01:00
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
// Transform --foo to -foo
|
|
|
|
if (key.length() > 1 && key[1] == '-')
|
|
|
|
key.erase(0, 1);
|
|
|
|
|
2018-04-04 10:02:00 +02:00
|
|
|
// Check for -nofoo
|
|
|
|
if (InterpretNegatedOption(key, val)) {
|
|
|
|
m_override_args[key].clear();
|
|
|
|
} else {
|
|
|
|
m_override_args[key].push_back(val);
|
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
|
|
|
|
// Check that the arg is known
|
|
|
|
if (!(IsSwitchChar(key[0]) && key.size() == 1)) {
|
2018-07-21 11:53:54 +02:00
|
|
|
if (!IsArgKnown(key)) {
|
2018-04-29 01:40:51 +02:00
|
|
|
error = strprintf("Invalid parameter %s", key.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
2018-04-18 07:35:44 +02:00
|
|
|
|
|
|
|
// we do not allow -includeconf from command line, so we clear it here
|
|
|
|
auto it = m_override_args.find("-includeconf");
|
|
|
|
if (it != m_override_args.end()) {
|
|
|
|
if (it->second.size() > 0) {
|
|
|
|
for (const auto& ic : it->second) {
|
2018-06-01 19:28:14 +02:00
|
|
|
error += "-includeconf cannot be used from commandline; -includeconf=" + ic + "\n";
|
2018-04-18 07:35:44 +02:00
|
|
|
}
|
2018-06-01 19:28:14 +02:00
|
|
|
return false;
|
2018-04-18 07:35:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-21 11:53:54 +02:00
|
|
|
bool ArgsManager::IsArgKnown(const std::string& key) const
|
2018-04-29 01:40:51 +02:00
|
|
|
{
|
|
|
|
size_t option_index = key.find('.');
|
|
|
|
std::string arg_no_net;
|
|
|
|
if (option_index == std::string::npos) {
|
|
|
|
arg_no_net = key;
|
|
|
|
} else {
|
|
|
|
arg_no_net = std::string("-") + key.substr(option_index + 1, std::string::npos);
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
2018-04-29 01:40:51 +02:00
|
|
|
for (const auto& arg_map : m_available_args) {
|
|
|
|
if (arg_map.second.count(arg_no_net)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
|
2017-05-06 01:36:47 +02:00
|
|
|
{
|
2018-04-04 10:01:00 +02:00
|
|
|
std::vector<std::string> result = {};
|
2018-04-04 10:02:00 +02:00
|
|
|
if (IsArgNegated(strArg)) return result; // special case
|
2018-04-04 10:01:00 +02:00
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
LOCK(cs_args);
|
2018-04-04 10:06:00 +02:00
|
|
|
|
2018-04-04 10:01:00 +02:00
|
|
|
ArgsManagerHelper::AddArgs(result, m_override_args, strArg);
|
2018-04-04 10:03:00 +02:00
|
|
|
if (!m_network.empty()) {
|
|
|
|
ArgsManagerHelper::AddArgs(result, m_config_args, ArgsManagerHelper::NetworkArg(*this, strArg));
|
|
|
|
}
|
2018-04-04 10:06:00 +02:00
|
|
|
|
|
|
|
if (ArgsManagerHelper::UseDefaultSection(*this, strArg)) {
|
|
|
|
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:01:00 +02:00
|
|
|
return result;
|
2017-05-06 01:36:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
bool ArgsManager::IsArgSet(const std::string& strArg) const
|
2016-11-30 02:51:30 +01:00
|
|
|
{
|
2018-04-04 10:02:00 +02:00
|
|
|
if (IsArgNegated(strArg)) return true; // special case
|
2018-04-04 10:01:00 +02:00
|
|
|
return ArgsManagerHelper::GetArg(*this, strArg).first;
|
2016-11-30 02:51:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
bool ArgsManager::IsArgNegated(const std::string& strArg) const
|
|
|
|
{
|
|
|
|
LOCK(cs_args);
|
2018-04-04 10:02:00 +02:00
|
|
|
|
|
|
|
const auto& ov = m_override_args.find(strArg);
|
|
|
|
if (ov != m_override_args.end()) return ov->second.empty();
|
|
|
|
|
2018-04-04 10:03:00 +02:00
|
|
|
if (!m_network.empty()) {
|
|
|
|
const auto& cfs = m_config_args.find(ArgsManagerHelper::NetworkArg(*this, strArg));
|
|
|
|
if (cfs != m_config_args.end()) return cfs->second.empty();
|
|
|
|
}
|
|
|
|
|
2018-04-04 10:02:00 +02:00
|
|
|
const auto& cf = m_config_args.find(strArg);
|
|
|
|
if (cf != m_config_args.end()) return cf->second.empty();
|
|
|
|
|
|
|
|
return false;
|
2018-03-22 03:24:17 +01:00
|
|
|
}
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
|
2012-02-06 18:37:49 +01:00
|
|
|
{
|
2018-04-04 10:02:00 +02:00
|
|
|
if (IsArgNegated(strArg)) return "0";
|
2018-04-04 10:01:00 +02:00
|
|
|
std::pair<bool,std::string> found_res = ArgsManagerHelper::GetArg(*this, strArg);
|
|
|
|
if (found_res.first) return found_res.second;
|
2012-02-06 18:37:49 +01:00
|
|
|
return strDefault;
|
|
|
|
}
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
|
2012-02-06 18:37:49 +01:00
|
|
|
{
|
2018-04-04 10:02:00 +02:00
|
|
|
if (IsArgNegated(strArg)) return 0;
|
2018-04-04 10:01:00 +02:00
|
|
|
std::pair<bool,std::string> found_res = ArgsManagerHelper::GetArg(*this, strArg);
|
|
|
|
if (found_res.first) return atoi64(found_res.second);
|
2012-02-06 18:37:49 +01:00
|
|
|
return nDefault;
|
|
|
|
}
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
|
2012-02-06 18:37:49 +01:00
|
|
|
{
|
2018-04-04 10:02:00 +02:00
|
|
|
if (IsArgNegated(strArg)) return false;
|
2018-04-04 10:01:00 +02:00
|
|
|
std::pair<bool,std::string> found_res = ArgsManagerHelper::GetArg(*this, strArg);
|
|
|
|
if (found_res.first) return InterpretBool(found_res.second);
|
2012-02-06 18:37:49 +01:00
|
|
|
return fDefault;
|
|
|
|
}
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
|
2012-01-03 16:14:22 +01:00
|
|
|
{
|
2016-11-30 03:52:44 +01:00
|
|
|
LOCK(cs_args);
|
2017-07-22 01:00:52 +02:00
|
|
|
if (IsArgSet(strArg)) return false;
|
2017-05-06 21:23:21 +02:00
|
|
|
ForceSetArg(strArg, strValue);
|
2012-01-03 16:14:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
|
2012-01-03 16:14:22 +01:00
|
|
|
{
|
|
|
|
if (fValue)
|
|
|
|
return SoftSetArg(strArg, std::string("1"));
|
|
|
|
else
|
|
|
|
return SoftSetArg(strArg, std::string("0"));
|
|
|
|
}
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
|
2016-12-24 17:28:44 +01:00
|
|
|
{
|
|
|
|
LOCK(cs_args);
|
2018-04-04 10:01:00 +02:00
|
|
|
m_override_args[strArg] = {strValue};
|
2016-12-24 17:28:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 22:54:58 +02:00
|
|
|
void ArgsManager::AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat)
|
|
|
|
{
|
2018-05-16 21:15:18 +02:00
|
|
|
// Split arg name from its help param
|
|
|
|
size_t eq_index = name.find('=');
|
|
|
|
if (eq_index == std::string::npos) {
|
|
|
|
eq_index = name.size();
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
2018-05-16 21:15:18 +02:00
|
|
|
std::map<std::string, Arg>& arg_map = m_available_args[cat];
|
|
|
|
auto ret = arg_map.emplace(name.substr(0, eq_index), Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only));
|
|
|
|
assert(ret.second); // Make sure an insertion actually happened
|
2018-04-28 22:54:58 +02:00
|
|
|
}
|
|
|
|
|
2018-06-11 23:23:13 +02:00
|
|
|
void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
|
|
|
|
{
|
|
|
|
for (const std::string& name : names) {
|
|
|
|
AddArg(name, "", false, OptionsCategory::HIDDEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 11:53:54 +02:00
|
|
|
std::string ArgsManager::GetHelpMessage() const
|
2018-04-28 22:54:58 +02:00
|
|
|
{
|
|
|
|
const bool show_debug = gArgs.GetBoolArg("-help-debug", false);
|
|
|
|
|
2018-05-16 21:15:18 +02:00
|
|
|
std::string usage = "";
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
2018-05-16 21:15:18 +02:00
|
|
|
for (const auto& arg_map : m_available_args) {
|
|
|
|
switch(arg_map.first) {
|
|
|
|
case OptionsCategory::OPTIONS:
|
|
|
|
usage += HelpMessageGroup("Options:");
|
|
|
|
break;
|
|
|
|
case OptionsCategory::CONNECTION:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Connection options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::ZMQ:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("ZeroMQ notification options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::DEBUG_TEST:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Debugging/Testing options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::NODE_RELAY:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Node relay options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::BLOCK_CREATION:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Block creation options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::RPC:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("RPC server options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::WALLET:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Wallet options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::WALLET_DEBUG_TEST:
|
|
|
|
if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
|
|
|
|
break;
|
|
|
|
case OptionsCategory::CHAINPARAMS:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Chain selection options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::GUI:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("UI Options:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::COMMANDS:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Commands:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
case OptionsCategory::REGISTER_COMMANDS:
|
2018-05-29 18:49:44 +02:00
|
|
|
usage += HelpMessageGroup("Register Commands:");
|
2018-05-16 21:15:18 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-04-28 22:54:58 +02:00
|
|
|
}
|
2018-05-16 21:15:18 +02:00
|
|
|
|
|
|
|
// When we get to the hidden options, stop
|
|
|
|
if (arg_map.first == OptionsCategory::HIDDEN) break;
|
|
|
|
|
|
|
|
for (const auto& arg : arg_map.second) {
|
|
|
|
if (show_debug || !arg.second.m_debug_only) {
|
|
|
|
std::string name;
|
|
|
|
if (arg.second.m_help_param.empty()) {
|
|
|
|
name = arg.first;
|
|
|
|
} else {
|
|
|
|
name = arg.first + arg.second.m_help_param;
|
|
|
|
}
|
|
|
|
usage += HelpMessageOpt(name, arg.second.m_help_text);
|
|
|
|
}
|
2018-04-28 22:54:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
2018-03-30 22:47:36 +02:00
|
|
|
bool HelpRequested(const ArgsManager& args)
|
|
|
|
{
|
2018-09-12 15:01:58 +02:00
|
|
|
return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
|
2018-03-30 22:47:36 +02:00
|
|
|
}
|
2016-12-24 17:28:44 +01:00
|
|
|
|
2019-02-06 19:57:52 +01:00
|
|
|
void SetupHelpOptions(ArgsManager& args)
|
|
|
|
{
|
|
|
|
args.AddArg("-?", "Print this help message and exit", false, OptionsCategory::OPTIONS);
|
|
|
|
args.AddHiddenArgs({"-h", "-help"});
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:11:49 +01:00
|
|
|
static const int screenWidth = 79;
|
|
|
|
static const int optIndent = 2;
|
|
|
|
static const int msgIndent = 7;
|
|
|
|
|
|
|
|
std::string HelpMessageGroup(const std::string &message) {
|
|
|
|
return std::string(message) + std::string("\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HelpMessageOpt(const std::string &option, const std::string &message) {
|
|
|
|
return std::string(optIndent,' ') + std::string(option) +
|
|
|
|
std::string("\n") + std::string(msgIndent,' ') +
|
|
|
|
FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
|
|
|
|
std::string("\n\n");
|
|
|
|
}
|
|
|
|
|
2014-12-07 13:29:06 +01:00
|
|
|
static std::string FormatException(const std::exception* pex, const char* pszThread)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifdef WIN32
|
2012-04-22 16:22:45 +02:00
|
|
|
char pszModule[MAX_PATH] = "";
|
2017-08-07 07:36:37 +02:00
|
|
|
GetModuleFileNameA(nullptr, pszModule, sizeof(pszModule));
|
2011-05-14 17:25:05 +02:00
|
|
|
#else
|
|
|
|
const char* pszModule = "bitcoin";
|
|
|
|
#endif
|
|
|
|
if (pex)
|
2012-05-14 19:53:02 +02:00
|
|
|
return strprintf(
|
2011-05-14 17:25:05 +02:00
|
|
|
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
|
|
|
|
else
|
2012-05-14 19:53:02 +02:00
|
|
|
return strprintf(
|
2011-05-14 17:25:05 +02:00
|
|
|
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
|
|
|
|
}
|
|
|
|
|
2014-12-07 13:29:06 +01:00
|
|
|
void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2012-05-14 19:53:02 +02:00
|
|
|
std::string message = FormatException(pex, pszThread);
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("\n\n************************\n%s\n", message);
|
2019-06-13 15:16:10 +02:00
|
|
|
tfm::format(std::cerr, "\n\n************************\n%s\n", message.c_str());
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetDefaultDataDir()
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2012-04-22 16:22:45 +02:00
|
|
|
// Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
|
|
|
|
// Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
|
2011-05-14 17:25:05 +02:00
|
|
|
// Mac: ~/Library/Application Support/Bitcoin
|
|
|
|
// Unix: ~/.bitcoin
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifdef WIN32
|
2011-05-14 17:25:05 +02:00
|
|
|
// Windows
|
2012-04-22 16:22:45 +02:00
|
|
|
return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
|
2011-05-14 17:25:05 +02:00
|
|
|
#else
|
2012-04-09 23:50:56 +02:00
|
|
|
fs::path pathRet;
|
2011-05-14 17:25:05 +02:00
|
|
|
char* pszHome = getenv("HOME");
|
2017-08-07 07:36:37 +02:00
|
|
|
if (pszHome == nullptr || strlen(pszHome) == 0)
|
2012-04-09 23:50:56 +02:00
|
|
|
pathRet = fs::path("/");
|
|
|
|
else
|
|
|
|
pathRet = fs::path(pszHome);
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifdef MAC_OSX
|
2011-05-14 17:25:05 +02:00
|
|
|
// Mac
|
2016-04-09 15:30:07 +02:00
|
|
|
return pathRet / "Library/Application Support/Bitcoin";
|
2011-05-14 17:25:05 +02:00
|
|
|
#else
|
|
|
|
// Unix
|
2012-04-09 23:50:56 +02:00
|
|
|
return pathRet / ".bitcoin";
|
2011-05-14 17:25:05 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-09 05:12:43 +01:00
|
|
|
static fs::path g_blocks_path_cache_net_specific;
|
2017-03-01 17:05:50 +01:00
|
|
|
static fs::path pathCached;
|
|
|
|
static fs::path pathCachedNetSpecific;
|
2019-06-27 20:11:25 +02:00
|
|
|
static RecursiveMutex csPathCached;
|
2013-07-17 04:20:09 +02:00
|
|
|
|
2018-10-05 20:04:08 +02:00
|
|
|
const fs::path &GetBlocksDir()
|
2018-03-09 05:12:43 +01:00
|
|
|
{
|
|
|
|
LOCK(csPathCached);
|
2018-10-05 20:04:08 +02:00
|
|
|
fs::path &path = g_blocks_path_cache_net_specific;
|
2018-03-09 05:12:43 +01:00
|
|
|
|
2019-06-27 20:11:25 +02:00
|
|
|
// Cache the path to avoid calling fs::create_directories on every call of
|
|
|
|
// this function
|
|
|
|
if (!path.empty()) return path;
|
2018-03-09 05:12:43 +01:00
|
|
|
|
|
|
|
if (gArgs.IsArgSet("-blocksdir")) {
|
|
|
|
path = fs::system_complete(gArgs.GetArg("-blocksdir", ""));
|
|
|
|
if (!fs::is_directory(path)) {
|
|
|
|
path = "";
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path = GetDataDir(false);
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:04:08 +02:00
|
|
|
path /= BaseParams().DataDir();
|
2018-03-09 05:12:43 +01:00
|
|
|
path /= "blocks";
|
|
|
|
fs::create_directories(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
const fs::path &GetDataDir(bool fNetSpecific)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2013-07-17 04:20:09 +02:00
|
|
|
LOCK(csPathCached);
|
2014-10-11 20:56:27 +02:00
|
|
|
fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
|
2012-04-09 23:50:56 +02:00
|
|
|
|
2019-06-27 20:11:25 +02:00
|
|
|
// Cache the path to avoid calling fs::create_directories on every call of
|
|
|
|
// this function
|
|
|
|
if (!path.empty()) return path;
|
2012-04-09 23:50:56 +02:00
|
|
|
|
2017-06-15 22:45:55 +02:00
|
|
|
if (gArgs.IsArgSet("-datadir")) {
|
|
|
|
path = fs::system_complete(gArgs.GetArg("-datadir", ""));
|
2012-04-22 14:35:22 +02:00
|
|
|
if (!fs::is_directory(path)) {
|
|
|
|
path = "";
|
|
|
|
return path;
|
|
|
|
}
|
2012-04-09 23:50:56 +02:00
|
|
|
} else {
|
|
|
|
path = GetDefaultDataDir();
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
2013-05-07 15:16:25 +02:00
|
|
|
if (fNetSpecific)
|
2014-06-19 15:10:04 +02:00
|
|
|
path /= BaseParams().DataDir();
|
2011-05-14 17:25:05 +02:00
|
|
|
|
2017-10-12 11:04:46 +02:00
|
|
|
if (fs::create_directories(path)) {
|
|
|
|
// This is the first run, create wallets subdirectory too
|
|
|
|
fs::create_directories(path / "wallets");
|
|
|
|
}
|
2012-04-09 23:50:56 +02:00
|
|
|
|
|
|
|
return path;
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
2013-07-17 04:20:09 +02:00
|
|
|
void ClearDatadirCache()
|
|
|
|
{
|
2016-11-30 03:52:44 +01:00
|
|
|
LOCK(csPathCached);
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
pathCached = fs::path();
|
|
|
|
pathCachedNetSpecific = fs::path();
|
2018-03-09 05:12:43 +01:00
|
|
|
g_blocks_path_cache_net_specific = fs::path();
|
2013-07-17 04:20:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetConfigFile(const std::string& confPath)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2018-01-31 04:33:49 +01:00
|
|
|
return AbsPathForConfigVal(fs::path(confPath), false);
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-06-16 04:31:26 +02:00
|
|
|
static std::string TrimString(const std::string& str, const std::string& pattern)
|
|
|
|
{
|
|
|
|
std::string::size_type front = str.find_first_not_of(pattern);
|
|
|
|
if (front == std::string::npos) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
std::string::size_type end = str.find_last_not_of(pattern);
|
|
|
|
return str.substr(front, end - front + 1);
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:53:19 +01:00
|
|
|
static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
|
2018-06-16 04:31:26 +02:00
|
|
|
{
|
|
|
|
std::string str, prefix;
|
|
|
|
std::string::size_type pos;
|
2018-08-30 13:04:19 +02:00
|
|
|
int linenr = 1;
|
2018-06-16 04:31:26 +02:00
|
|
|
while (std::getline(stream, str)) {
|
2018-10-16 07:19:40 +02:00
|
|
|
bool used_hash = false;
|
2018-06-16 04:31:26 +02:00
|
|
|
if ((pos = str.find('#')) != std::string::npos) {
|
|
|
|
str = str.substr(0, pos);
|
2018-10-16 07:19:40 +02:00
|
|
|
used_hash = true;
|
2018-06-16 04:31:26 +02:00
|
|
|
}
|
|
|
|
const static std::string pattern = " \t\r\n";
|
|
|
|
str = TrimString(str, pattern);
|
|
|
|
if (!str.empty()) {
|
|
|
|
if (*str.begin() == '[' && *str.rbegin() == ']') {
|
2018-11-12 03:06:36 +01:00
|
|
|
const std::string section = str.substr(1, str.size() - 2);
|
2019-02-04 04:53:19 +01:00
|
|
|
sections.emplace_back(SectionInfo{section, filepath, linenr});
|
2018-11-12 03:06:36 +01:00
|
|
|
prefix = section + '.';
|
2018-08-30 13:04:19 +02:00
|
|
|
} else if (*str.begin() == '-') {
|
|
|
|
error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
|
|
|
|
return false;
|
2018-06-16 04:31:26 +02:00
|
|
|
} else if ((pos = str.find('=')) != std::string::npos) {
|
|
|
|
std::string name = prefix + TrimString(str.substr(0, pos), pattern);
|
|
|
|
std::string value = TrimString(str.substr(pos + 1), pattern);
|
2019-01-03 11:54:19 +01:00
|
|
|
if (used_hash && name.find("rpcpassword") != std::string::npos) {
|
2018-10-16 07:19:40 +02:00
|
|
|
error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-16 04:31:26 +02:00
|
|
|
options.emplace_back(name, value);
|
2019-02-04 04:53:19 +01:00
|
|
|
if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) {
|
|
|
|
sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr});
|
2018-11-12 03:06:36 +01:00
|
|
|
}
|
2018-08-30 13:04:19 +02:00
|
|
|
} else {
|
|
|
|
error = strprintf("parse error on line %i: %s", linenr, str);
|
|
|
|
if (str.size() >= 2 && str.substr(0, 2) == "no") {
|
|
|
|
error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);
|
|
|
|
}
|
|
|
|
return false;
|
2018-06-16 04:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 13:04:19 +02:00
|
|
|
++linenr;
|
2018-06-16 04:31:26 +02:00
|
|
|
}
|
2018-08-30 13:04:19 +02:00
|
|
|
return true;
|
2018-06-16 04:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 04:53:19 +01:00
|
|
|
bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys)
|
2011-05-14 17:25:05 +02:00
|
|
|
{
|
2018-03-29 07:03:00 +02:00
|
|
|
LOCK(cs_args);
|
2018-08-30 13:04:19 +02:00
|
|
|
std::vector<std::pair<std::string, std::string>> options;
|
2019-02-04 04:53:19 +01:00
|
|
|
if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
|
2018-08-30 13:04:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const std::pair<std::string, std::string>& option : options) {
|
2018-06-16 04:31:26 +02:00
|
|
|
std::string strKey = std::string("-") + option.first;
|
|
|
|
std::string strValue = option.second;
|
2018-04-29 01:40:51 +02:00
|
|
|
|
2018-04-04 10:02:00 +02:00
|
|
|
if (InterpretNegatedOption(strKey, strValue)) {
|
|
|
|
m_config_args[strKey].clear();
|
|
|
|
} else {
|
|
|
|
m_config_args[strKey].push_back(strValue);
|
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
|
|
|
|
// Check that the arg is known
|
2018-07-30 09:14:18 +02:00
|
|
|
if (!IsArgKnown(strKey)) {
|
|
|
|
if (!ignore_invalid_keys) {
|
|
|
|
error = strprintf("Invalid configuration value %s", option.first.c_str());
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
LogPrintf("Ignoring unknown configuration value %s\n", option.first);
|
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
}
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
return true;
|
2018-03-29 07:02:00 +02:00
|
|
|
}
|
2016-11-30 03:52:44 +01:00
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
2018-03-29 07:02:00 +02:00
|
|
|
{
|
2018-08-30 10:02:49 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_args);
|
|
|
|
m_config_args.clear();
|
2019-02-04 04:53:19 +01:00
|
|
|
m_config_sections.clear();
|
2018-08-30 10:02:49 +02:00
|
|
|
}
|
2018-04-04 10:01:00 +02:00
|
|
|
|
2018-04-18 07:35:44 +02:00
|
|
|
const std::string confPath = GetArg("-conf", BITCOIN_CONF_FILENAME);
|
2018-08-04 15:32:13 +02:00
|
|
|
fsbridge::ifstream stream(GetConfigFile(confPath));
|
2018-03-29 07:03:00 +02:00
|
|
|
|
|
|
|
// ok to not have a config file
|
|
|
|
if (stream.good()) {
|
2019-02-04 04:53:19 +01:00
|
|
|
if (!ReadConfigStream(stream, confPath, error, ignore_invalid_keys)) {
|
2018-04-29 01:40:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-18 07:35:44 +02:00
|
|
|
// if there is an -includeconf in the override args, but it is empty, that means the user
|
|
|
|
// passed '-noincludeconf' on the command line, in which case we should not include anything
|
2018-08-30 10:02:49 +02:00
|
|
|
bool emptyIncludeConf;
|
|
|
|
{
|
|
|
|
LOCK(cs_args);
|
|
|
|
emptyIncludeConf = m_override_args.count("-includeconf") == 0;
|
|
|
|
}
|
|
|
|
if (emptyIncludeConf) {
|
2018-06-01 19:28:14 +02:00
|
|
|
std::string chain_id = GetChainName();
|
2018-04-18 07:35:44 +02:00
|
|
|
std::vector<std::string> includeconf(GetArgs("-includeconf"));
|
|
|
|
{
|
|
|
|
// We haven't set m_network yet (that happens in SelectParams()), so manually check
|
|
|
|
// for network.includeconf args.
|
2018-06-01 19:28:14 +02:00
|
|
|
std::vector<std::string> includeconf_net(GetArgs(std::string("-") + chain_id + ".includeconf"));
|
2018-04-18 07:35:44 +02:00
|
|
|
includeconf.insert(includeconf.end(), includeconf_net.begin(), includeconf_net.end());
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:49:59 +02:00
|
|
|
// Remove -includeconf from configuration, so we can warn about recursion
|
|
|
|
// later
|
2018-08-30 10:02:49 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_args);
|
|
|
|
m_config_args.erase("-includeconf");
|
|
|
|
m_config_args.erase(std::string("-") + chain_id + ".includeconf");
|
|
|
|
}
|
2018-05-09 09:49:59 +02:00
|
|
|
|
2018-04-18 07:35:44 +02:00
|
|
|
for (const std::string& to_include : includeconf) {
|
2018-08-04 15:32:13 +02:00
|
|
|
fsbridge::ifstream include_config(GetConfigFile(to_include));
|
2018-04-18 07:35:44 +02:00
|
|
|
if (include_config.good()) {
|
2019-02-04 04:53:19 +01:00
|
|
|
if (!ReadConfigStream(include_config, to_include, error, ignore_invalid_keys)) {
|
2018-04-29 01:40:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-18 07:35:44 +02:00
|
|
|
LogPrintf("Included configuration file %s\n", to_include.c_str());
|
|
|
|
} else {
|
2018-06-01 19:28:14 +02:00
|
|
|
error = "Failed to include configuration file " + to_include;
|
|
|
|
return false;
|
2018-04-18 07:35:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-09 09:49:59 +02:00
|
|
|
|
|
|
|
// Warn about recursive -includeconf
|
|
|
|
includeconf = GetArgs("-includeconf");
|
|
|
|
{
|
2018-06-01 19:28:14 +02:00
|
|
|
std::vector<std::string> includeconf_net(GetArgs(std::string("-") + chain_id + ".includeconf"));
|
2018-05-09 09:49:59 +02:00
|
|
|
includeconf.insert(includeconf.end(), includeconf_net.begin(), includeconf_net.end());
|
2018-06-01 19:28:14 +02:00
|
|
|
std::string chain_id_final = GetChainName();
|
|
|
|
if (chain_id_final != chain_id) {
|
|
|
|
// Also warn about recursive includeconf for the chain that was specified in one of the includeconfs
|
|
|
|
includeconf_net = GetArgs(std::string("-") + chain_id_final + ".includeconf");
|
|
|
|
includeconf.insert(includeconf.end(), includeconf_net.begin(), includeconf_net.end());
|
|
|
|
}
|
2018-05-09 09:49:59 +02:00
|
|
|
}
|
|
|
|
for (const std::string& to_include : includeconf) {
|
2019-06-13 15:16:10 +02:00
|
|
|
tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", to_include.c_str());
|
2018-05-09 09:49:59 +02:00
|
|
|
}
|
2018-04-18 07:35:44 +02:00
|
|
|
}
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
2018-03-29 07:02:00 +02:00
|
|
|
|
2013-07-17 04:20:09 +02:00
|
|
|
// If datadir is changed in .conf file:
|
|
|
|
ClearDatadirCache();
|
2017-12-05 12:24:06 +01:00
|
|
|
if (!fs::is_directory(GetDataDir(false))) {
|
2018-04-29 01:40:51 +02:00
|
|
|
error = strprintf("specified data directory \"%s\" does not exist.", gArgs.GetArg("-datadir", "").c_str());
|
|
|
|
return false;
|
2017-12-05 12:24:06 +01:00
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
return true;
|
2011-05-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 07:00:00 +02:00
|
|
|
std::string ArgsManager::GetChainName() const
|
|
|
|
{
|
2018-08-27 23:19:18 +02:00
|
|
|
LOCK(cs_args);
|
2018-04-04 10:09:00 +02:00
|
|
|
bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest");
|
|
|
|
bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet");
|
2018-03-29 07:00:00 +02:00
|
|
|
|
|
|
|
if (fTestNet && fRegTest)
|
|
|
|
throw std::runtime_error("Invalid combination of -regtest and -testnet.");
|
|
|
|
if (fRegTest)
|
|
|
|
return CBaseChainParams::REGTEST;
|
|
|
|
if (fTestNet)
|
|
|
|
return CBaseChainParams::TESTNET;
|
|
|
|
return CBaseChainParams::MAIN;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
bool RenameOver(fs::path src, fs::path dest)
|
2012-05-12 07:24:27 +02:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2018-08-06 03:32:11 +02:00
|
|
|
return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
|
2014-09-06 21:59:59 +02:00
|
|
|
MOVEFILE_REPLACE_EXISTING) != 0;
|
2012-05-12 07:24:27 +02:00
|
|
|
#else
|
|
|
|
int rc = std::rename(src.string().c_str(), dest.string().c_str());
|
|
|
|
return (rc == 0);
|
|
|
|
#endif /* WIN32 */
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
2017-02-22 10:10:00 +01:00
|
|
|
* Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
|
2014-11-17 04:04:01 +01:00
|
|
|
* Specifically handles case where path p exists, but it wasn't possible for the user to
|
|
|
|
* write to the parent directory.
|
|
|
|
*/
|
2017-02-22 10:10:00 +01:00
|
|
|
bool TryCreateDirectories(const fs::path& p)
|
2014-03-24 02:14:43 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-02-22 10:10:00 +01:00
|
|
|
return fs::create_directories(p);
|
2017-03-01 17:05:50 +01:00
|
|
|
} catch (const fs::filesystem_error&) {
|
|
|
|
if (!fs::exists(p) || !fs::is_directory(p))
|
2014-03-24 02:14:43 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:10:00 +01:00
|
|
|
// create_directories didn't create the directory, it had to have existed already
|
2014-03-24 02:14:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-20 11:21:08 +02:00
|
|
|
bool FileCommit(FILE *file)
|
2012-05-12 07:24:27 +02:00
|
|
|
{
|
2018-04-20 11:21:08 +02:00
|
|
|
if (fflush(file) != 0) { // harmless if redundantly called
|
|
|
|
LogPrintf("%s: fflush failed: %d\n", __func__, errno);
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-12 07:24:27 +02:00
|
|
|
#ifdef WIN32
|
2016-09-02 20:50:59 +02:00
|
|
|
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
|
2018-04-20 11:21:08 +02:00
|
|
|
if (FlushFileBuffers(hFile) == 0) {
|
|
|
|
LogPrintf("%s: FlushFileBuffers failed: %d\n", __func__, GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-12 07:24:27 +02:00
|
|
|
#else
|
2012-09-28 14:27:20 +02:00
|
|
|
#if defined(__linux__) || defined(__NetBSD__)
|
2018-04-20 11:21:08 +02:00
|
|
|
if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
|
|
|
|
LogPrintf("%s: fdatasync failed: %d\n", __func__, errno);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 09:14:29 +02:00
|
|
|
#elif defined(MAC_OSX) && defined(F_FULLFSYNC)
|
2018-04-20 11:21:08 +02:00
|
|
|
if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
|
|
|
|
LogPrintf("%s: fcntl F_FULLFSYNC failed: %d\n", __func__, errno);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-28 14:27:20 +02:00
|
|
|
#else
|
2018-04-20 11:21:08 +02:00
|
|
|
if (fsync(fileno(file)) != 0 && errno != EINVAL) {
|
|
|
|
LogPrintf("%s: fsync failed: %d\n", __func__, errno);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-28 14:27:20 +02:00
|
|
|
#endif
|
2012-05-12 07:24:27 +02:00
|
|
|
#endif
|
2018-04-20 11:21:08 +02:00
|
|
|
return true;
|
2012-05-12 07:24:27 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 04:17:33 +01:00
|
|
|
bool TruncateFile(FILE *file, unsigned int length) {
|
|
|
|
#if defined(WIN32)
|
|
|
|
return _chsize(_fileno(file), length) == 0;
|
|
|
|
#else
|
|
|
|
return ftruncate(fileno(file), length) == 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* this function tries to raise the file descriptor limit to the requested number.
|
|
|
|
* It returns the actual file descriptor limit (which may be more or less than nMinFD)
|
|
|
|
*/
|
2013-04-26 00:46:47 +02:00
|
|
|
int RaiseFileDescriptorLimit(int nMinFD) {
|
|
|
|
#if defined(WIN32)
|
|
|
|
return 2048;
|
|
|
|
#else
|
|
|
|
struct rlimit limitFD;
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
|
|
|
|
if (limitFD.rlim_cur < (rlim_t)nMinFD) {
|
|
|
|
limitFD.rlim_cur = nMinFD;
|
|
|
|
if (limitFD.rlim_cur > limitFD.rlim_max)
|
|
|
|
limitFD.rlim_cur = limitFD.rlim_max;
|
|
|
|
setrlimit(RLIMIT_NOFILE, &limitFD);
|
|
|
|
getrlimit(RLIMIT_NOFILE, &limitFD);
|
|
|
|
}
|
|
|
|
return limitFD.rlim_cur;
|
|
|
|
}
|
|
|
|
return nMinFD; // getrlimit failed, assume it's fine
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* this function tries to make a particular range of a file allocated (corresponding to disk space)
|
|
|
|
* it is advisory, and the range specified in the arguments will never contain live data
|
|
|
|
*/
|
2012-08-16 02:21:28 +02:00
|
|
|
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
|
2013-01-28 00:07:51 +01:00
|
|
|
#if defined(WIN32)
|
|
|
|
// Windows-specific version
|
|
|
|
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
|
|
|
|
LARGE_INTEGER nFileSize;
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nEndPos = (int64_t)offset + length;
|
2013-01-28 00:07:51 +01:00
|
|
|
nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
|
|
|
|
nFileSize.u.HighPart = nEndPos >> 32;
|
|
|
|
SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
|
|
|
|
SetEndOfFile(hFile);
|
|
|
|
#elif defined(MAC_OSX)
|
|
|
|
// OSX specific version
|
|
|
|
fstore_t fst;
|
|
|
|
fst.fst_flags = F_ALLOCATECONTIG;
|
|
|
|
fst.fst_posmode = F_PEOFPOSMODE;
|
|
|
|
fst.fst_offset = 0;
|
|
|
|
fst.fst_length = (off_t)offset + length;
|
|
|
|
fst.fst_bytesalloc = 0;
|
|
|
|
if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
|
|
|
|
fst.fst_flags = F_ALLOCATEALL;
|
|
|
|
fcntl(fileno(file), F_PREALLOCATE, &fst);
|
|
|
|
}
|
|
|
|
ftruncate(fileno(file), fst.fst_length);
|
2019-03-23 05:20:40 +01:00
|
|
|
#else
|
|
|
|
#if defined(__linux__)
|
2013-01-28 00:07:51 +01:00
|
|
|
// Version using posix_fallocate
|
|
|
|
off_t nEndPos = (off_t)offset + length;
|
2019-03-23 05:20:40 +01:00
|
|
|
if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
|
|
|
|
#endif
|
2013-01-28 00:07:51 +01:00
|
|
|
// Fallback version
|
|
|
|
// TODO: just write one byte per block
|
2012-08-16 02:21:28 +02:00
|
|
|
static const char buf[65536] = {};
|
2018-05-02 12:12:55 +02:00
|
|
|
if (fseek(file, offset, SEEK_SET)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-16 02:21:28 +02:00
|
|
|
while (length > 0) {
|
|
|
|
unsigned int now = 65536;
|
|
|
|
if (length < now)
|
|
|
|
now = length;
|
|
|
|
fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
|
|
|
|
length -= now;
|
|
|
|
}
|
2013-01-28 00:07:51 +01:00
|
|
|
#endif
|
2012-08-16 02:21:28 +02:00
|
|
|
}
|
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#ifdef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
|
2012-04-22 16:22:45 +02:00
|
|
|
{
|
2018-07-31 17:03:52 +02:00
|
|
|
WCHAR pszPath[MAX_PATH] = L"";
|
2012-04-22 16:22:45 +02:00
|
|
|
|
2018-07-31 17:03:52 +02:00
|
|
|
if(SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate))
|
2012-04-22 16:22:45 +02:00
|
|
|
{
|
|
|
|
return fs::path(pszPath);
|
|
|
|
}
|
|
|
|
|
2018-07-31 17:03:52 +02:00
|
|
|
LogPrintf("SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
|
2012-04-22 16:22:45 +02:00
|
|
|
return fs::path("");
|
|
|
|
}
|
2012-04-15 22:10:54 +02:00
|
|
|
#endif
|
2012-05-24 05:10:59 +02:00
|
|
|
|
2019-07-05 18:30:15 +02:00
|
|
|
#if HAVE_SYSTEM
|
2015-05-31 15:36:44 +02:00
|
|
|
void runCommand(const std::string& strCommand)
|
2012-05-24 05:10:59 +02:00
|
|
|
{
|
2017-07-27 11:35:01 +02:00
|
|
|
if (strCommand.empty()) return;
|
2018-08-06 03:03:33 +02:00
|
|
|
#ifndef WIN32
|
2012-05-24 05:10:59 +02:00
|
|
|
int nErr = ::system(strCommand.c_str());
|
2018-08-06 03:03:33 +02:00
|
|
|
#else
|
|
|
|
int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
|
|
|
|
#endif
|
2012-05-24 05:10:59 +02:00
|
|
|
if (nErr)
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
|
2012-05-24 05:10:59 +02:00
|
|
|
}
|
2019-03-14 11:30:37 +01:00
|
|
|
#endif
|
2012-05-24 05:10:59 +02:00
|
|
|
|
2014-05-13 12:15:00 +02:00
|
|
|
void SetupEnvironment()
|
|
|
|
{
|
2017-03-30 09:37:47 +02:00
|
|
|
#ifdef HAVE_MALLOPT_ARENA_MAX
|
|
|
|
// glibc-specific: On 32-bit systems set the number of arenas to 1.
|
|
|
|
// By default, since glibc 2.10, the C library will create up to two heap
|
|
|
|
// arenas per core. This is known to cause excessive virtual address space
|
|
|
|
// usage in our usage. Work around it by setting the maximum number of
|
|
|
|
// arenas to 1.
|
|
|
|
if (sizeof(void*) == 4) {
|
|
|
|
mallopt(M_ARENA_MAX, 1);
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-11 13:34:20 +01:00
|
|
|
// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
|
|
|
|
// may be invalid, in which case the "C" locale is used as fallback.
|
|
|
|
#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
|
|
|
|
try {
|
2015-05-02 10:10:31 +02:00
|
|
|
std::locale(""); // Raises a runtime error if current locale is invalid
|
2014-12-07 13:29:06 +01:00
|
|
|
} catch (const std::runtime_error&) {
|
2015-03-27 01:07:49 +01:00
|
|
|
setenv("LC_ALL", "C", 1);
|
2014-05-13 12:15:00 +02:00
|
|
|
}
|
2018-08-05 18:38:25 +02:00
|
|
|
#elif defined(WIN32)
|
|
|
|
// Set the default input/output charset is utf-8
|
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2014-05-10 14:54:20 +02:00
|
|
|
#endif
|
2015-05-02 10:10:31 +02:00
|
|
|
// The path locale is lazy initialized and to avoid deinitialization errors
|
2015-03-11 13:34:20 +01:00
|
|
|
// in multithreading environments, it is set explicitly by the main thread.
|
2015-05-02 10:10:31 +02:00
|
|
|
// A dummy locale is used to extract the internal default locale, used by
|
2017-03-01 17:05:50 +01:00
|
|
|
// fs::path, which is then used to explicitly imbue the path.
|
|
|
|
std::locale loc = fs::path::imbue(std::locale::classic());
|
2018-08-04 18:39:14 +02:00
|
|
|
#ifndef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path::imbue(loc);
|
2018-08-04 18:39:14 +02:00
|
|
|
#else
|
|
|
|
fs::path::imbue(std::locale(loc, new std::codecvt_utf8_utf16<wchar_t>()));
|
|
|
|
#endif
|
2014-05-13 12:15:00 +02:00
|
|
|
}
|
2014-05-08 18:01:10 +02:00
|
|
|
|
2015-09-02 16:18:16 +02:00
|
|
|
bool SetupNetworking()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
// Initialize Windows Sockets
|
|
|
|
WSADATA wsadata;
|
|
|
|
int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
|
|
|
|
if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:38:15 +02:00
|
|
|
int GetNumCores()
|
|
|
|
{
|
2017-04-25 03:34:23 +02:00
|
|
|
return std::thread::hardware_concurrency();
|
2015-07-01 17:38:15 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 06:38:27 +01:00
|
|
|
std::string CopyrightHolders(const std::string& strPrefix)
|
2015-12-22 13:29:13 +01:00
|
|
|
{
|
2019-06-27 21:07:17 +02:00
|
|
|
const auto copyright_devs = strprintf(_(COPYRIGHT_HOLDERS), COPYRIGHT_HOLDERS_SUBSTITUTION);
|
|
|
|
std::string strCopyrightHolders = strPrefix + copyright_devs;
|
2016-06-29 23:38:33 +02:00
|
|
|
|
2019-06-27 21:07:17 +02:00
|
|
|
// Make sure Bitcoin Core copyright is not removed by accident
|
|
|
|
if (copyright_devs.find("Bitcoin Core") == std::string::npos) {
|
2016-02-03 06:38:27 +01:00
|
|
|
strCopyrightHolders += "\n" + strPrefix + "The Bitcoin Core developers";
|
|
|
|
}
|
|
|
|
return strCopyrightHolders;
|
2017-01-12 22:22:50 +01:00
|
|
|
}
|
2017-05-14 20:18:26 +02:00
|
|
|
|
|
|
|
// Obtain the application startup time (used for uptime calculation)
|
|
|
|
int64_t GetStartupTime()
|
|
|
|
{
|
|
|
|
return nStartupTime;
|
|
|
|
}
|
2018-01-31 04:33:49 +01:00
|
|
|
|
|
|
|
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
|
|
|
|
{
|
|
|
|
return fs::absolute(path, GetDataDir(net_specific));
|
|
|
|
}
|
2018-03-06 18:50:20 +01:00
|
|
|
|
2018-09-13 19:36:41 +02:00
|
|
|
int ScheduleBatchPriority()
|
2018-03-06 18:50:20 +01:00
|
|
|
{
|
|
|
|
#ifdef SCHED_BATCH
|
2018-09-28 00:08:39 +02:00
|
|
|
const static sched_param param{};
|
2018-04-09 15:44:46 +02:00
|
|
|
if (int ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m)) {
|
2018-03-06 18:50:20 +01:00
|
|
|
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
2018-08-05 18:38:25 +02:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
#ifdef WIN32
|
|
|
|
WinCmdLineArgs::WinCmdLineArgs()
|
|
|
|
{
|
|
|
|
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt;
|
|
|
|
argv = new char*[argc];
|
|
|
|
args.resize(argc);
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
args[i] = utf8_cvt.to_bytes(wargv[i]);
|
|
|
|
argv[i] = &*args[i].begin();
|
|
|
|
}
|
|
|
|
LocalFree(wargv);
|
|
|
|
}
|
|
|
|
|
|
|
|
WinCmdLineArgs::~WinCmdLineArgs()
|
|
|
|
{
|
|
|
|
delete[] argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, char**> WinCmdLineArgs::get()
|
|
|
|
{
|
|
|
|
return std::make_pair(argc, argv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace util
|