BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
util.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
10 #ifndef BITCOIN_UTIL_H
11 #define BITCOIN_UTIL_H
12 
13 #if defined(HAVE_CONFIG_H)
14 #include <config/bitcoin-config.h>
15 #endif
16 
17 #include <compat.h>
18 #include <fs.h>
19 #include <logging.h>
20 #include <sync.h>
21 #include <tinyformat.h>
22 #include <utilmemory.h>
23 #include <utiltime.h>
24 
25 #include <atomic>
26 #include <exception>
27 #include <map>
28 #include <set>
29 #include <stdint.h>
30 #include <string>
31 #include <unordered_set>
32 #include <utility>
33 #include <vector>
34 
35 #include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
36 
37 // Application startup time (used for uptime calculation)
38 int64_t GetStartupTime();
39 
40 extern const char * const BITCOIN_CONF_FILENAME;
41 extern const char * const BITCOIN_PID_FILENAME;
42 
44 const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
45 
50 inline std::string _(const char* psz)
51 {
52  return G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz;
53 }
54 
55 void SetupEnvironment();
56 bool SetupNetworking();
57 
58 template<typename... Args>
59 bool error(const char* fmt, const Args&... args)
60 {
61  LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
62  return false;
63 }
64 
65 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
66 bool FileCommit(FILE *file);
67 bool TruncateFile(FILE *file, unsigned int length);
68 int RaiseFileDescriptorLimit(int nMinFD);
69 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
70 bool RenameOver(fs::path src, fs::path dest);
71 bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
72 bool DirIsWritable(const fs::path& directory);
73 
78 
79 bool TryCreateDirectories(const fs::path& p);
80 fs::path GetDefaultDataDir();
81 const fs::path &GetBlocksDir(bool fNetSpecific = true);
82 const fs::path &GetDataDir(bool fNetSpecific = true);
83 void ClearDatadirCache();
84 fs::path GetConfigFile(const std::string& confPath);
85 #ifndef WIN32
86 fs::path GetPidFile();
87 void CreatePidFile(const fs::path &path, pid_t pid);
88 #endif
89 #ifdef WIN32
90 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
91 #endif
92 void runCommand(const std::string& strCommand);
93 
102 fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
103 
104 inline bool IsSwitchChar(char c)
105 {
106 #ifdef WIN32
107  return c == '-' || c == '/';
108 #else
109  return c == '-';
110 #endif
111 }
112 
113 enum class OptionsCategory {
114  OPTIONS,
115  CONNECTION,
116  WALLET,
118  ZMQ,
119  DEBUG_TEST,
120  CHAINPARAMS,
121  NODE_RELAY,
123  RPC,
124  GUI,
125  COMMANDS,
127 
128  HIDDEN // Always the last option to avoid printing these in the help
129 };
130 
132 {
133 protected:
134  friend class ArgsManagerHelper;
135 
136  struct Arg
137  {
138  std::string m_help_param;
139  std::string m_help_text;
141 
142  Arg(const std::string& help_param, const std::string& help_text, bool debug_only) : m_help_param(help_param), m_help_text(help_text), m_debug_only(debug_only) {};
143  };
144 
146  std::map<std::string, std::vector<std::string>> m_override_args GUARDED_BY(cs_args);
147  std::map<std::string, std::vector<std::string>> m_config_args GUARDED_BY(cs_args);
148  std::string m_network GUARDED_BY(cs_args);
149  std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
150  std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
151 
152  bool ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys = false);
153 
154 public:
155  ArgsManager();
156 
160  void SelectConfigNetwork(const std::string& network);
161 
162  bool ParseParameters(int argc, const char* const argv[], std::string& error);
163  bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false);
164 
171  void WarnForSectionOnlyArgs();
172 
179  std::vector<std::string> GetArgs(const std::string& strArg) const;
180 
187  bool IsArgSet(const std::string& strArg) const;
188 
196  bool IsArgNegated(const std::string& strArg) const;
197 
205  std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
206 
214  int64_t GetArg(const std::string& strArg, int64_t nDefault) const;
215 
223  bool GetBoolArg(const std::string& strArg, bool fDefault) const;
224 
232  bool SoftSetArg(const std::string& strArg, const std::string& strValue);
233 
241  bool SoftSetBoolArg(const std::string& strArg, bool fValue);
242 
243  // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
244  // been set. Also called directly in testing.
245  void ForceSetArg(const std::string& strArg, const std::string& strValue);
246 
251  std::string GetChainName() const;
252 
256  void AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat);
257 
261  void AddHiddenArgs(const std::vector<std::string>& args);
262 
266  void ClearArgs() {
267  LOCK(cs_args);
268  m_available_args.clear();
269  }
270 
274  std::string GetHelpMessage() const;
275 
279  bool IsArgKnown(const std::string& key) const;
280 };
281 
282 extern ArgsManager gArgs;
283 
287 bool HelpRequested(const ArgsManager& args);
288 
295 std::string HelpMessageGroup(const std::string& message);
296 
304 std::string HelpMessageOpt(const std::string& option, const std::string& message);
305 
310 int GetNumCores();
311 
312 void RenameThread(const char* name);
313 
317 template <typename Callable> void TraceThread(const char* name, Callable func)
318 {
319  std::string s = strprintf("bitcoin-%s", name);
320  RenameThread(s.c_str());
321  try
322  {
323  LogPrintf("%s thread start\n", name);
324  func();
325  LogPrintf("%s thread exit\n", name);
326  }
327  catch (const boost::thread_interrupted&)
328  {
329  LogPrintf("%s thread interrupt\n", name);
330  throw;
331  }
332  catch (const std::exception& e) {
334  throw;
335  }
336  catch (...) {
337  PrintExceptionContinue(nullptr, name);
338  throw;
339  }
340 }
341 
342 std::string CopyrightHolders(const std::string& strPrefix);
343 
352 
353 void SetThreadPriority(int nPriority);
354 
355 namespace util {
356 
358 template <typename Tdst, typename Tsrc>
359 inline void insert(Tdst& dst, const Tsrc& src) {
360  dst.insert(dst.begin(), src.begin(), src.end());
361 }
362 template <typename TsetT, typename Tsrc>
363 inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
364  dst.insert(src.begin(), src.end());
365 }
366 
367 #ifdef WIN32
368 class WinCmdLineArgs
369 {
370 public:
371  WinCmdLineArgs();
372  ~WinCmdLineArgs();
373  std::pair<int, char**> get();
374 
375 private:
376  int argc;
377  char** argv;
378  std::vector<std::string> args;
379 };
380 #endif
381 
382 } // namespace util
383 
384 #endif // BITCOIN_UTIL_H
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: util.cpp:887
ArgsManager()
Definition: util.cpp:359
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: util.cpp:502
Definition: util.cpp:1287
bool m_debug_only
Definition: util.h:140
bool HelpRequested(const ArgsManager &args)
Definition: util.cpp:662
bool FileCommit(FILE *file)
Definition: util.cpp:1029
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn&#39;t already have a value.
Definition: util.cpp:558
int64_t GetStartupTime()
Server/client environment: argument handling, config file parsing, thread wrappers, startup time.
Definition: util.cpp:1250
#define strprintf
Definition: tinyformat.h:1066
const fs::path & GetBlocksDir(bool fNetSpecific=true)
Definition: util.cpp:737
void AddHiddenArgs(const std::vector< std::string > &args)
Add many hidden arguments.
Definition: util.cpp:586
void WarnForSectionOnlyArgs()
Log warnings for options in m_section_only_args when they are specified in the default section but no...
Definition: util.cpp:375
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only=false)
Definition: util.cpp:147
bool SetupNetworking()
Definition: util.cpp:1221
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: util.cpp:405
void RenameThread(const char *name)
Definition: util.cpp:1168
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
Definition: util.h:359
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: util.cpp:411
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:542
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: util.cpp:566
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: util.cpp:671
bool IsArgKnown(const std::string &key) const
Check whether we know of this arg.
Definition: util.cpp:466
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn&#39;t already have a value.
Definition: util.cpp:550
void SetupEnvironment()
Definition: util.cpp:1184
std::string GetHelpMessage() const
Get the help string.
Definition: util.cpp:593
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost&#39;s create_directories if the requested directory exists...
Definition: util.cpp:1015
const char *const BITCOIN_PID_FILENAME
Definition: util.cpp:86
fs::path GetDefaultDataDir()
Definition: util.cpp:705
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: util.cpp:508
void CreatePidFile(const fs::path &path, pid_t pid)
Definition: util.cpp:988
#define LOCK(cs)
Definition: sync.h:181
const char * name
Definition: rest.cpp:37
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:967
void TraceThread(const char *name, Callable func)
Definition: util.h:317
void ReleaseDirectoryLocks()
Release all directory locks.
Definition: util.cpp:171
int ScheduleBatchPriority()
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
Definition: util.cpp:1273
fs::path GetPidFile()
Definition: util.cpp:983
int GetNumCores()
Return the number of cores available on the current system.
Definition: util.cpp:1233
CCriticalSection cs_args
Definition: util.h:145
UniValue help(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:202
void ClearArgs()
Clear available arguments.
Definition: util.h:266
std::string m_help_text
Definition: util.h:139
const fs::path & GetDataDir(bool fNetSpecific=true)
Definition: util.cpp:766
void runCommand(const std::string &strCommand)
Definition: util.cpp:1156
Arg(const std::string &help_param, const std::string &help_text, bool debug_only)
Definition: util.h:142
std::map< std::string, std::vector< std::string > > m_override_args GUARDED_BY(cs_args)
void SetThreadPriority(int nPriority)
Definition: util.cpp:1260
void ClearDatadirCache()
Definition: util.cpp:798
fs::path GetConfigFile(const std::string &confPath)
Definition: util.cpp:808
const char *const BITCOIN_CONF_FILENAME
Definition: util.cpp:85
bool IsSwitchChar(char c)
Definition: util.h:104
Internal helper functions for ArgsManager.
Definition: util.cpp:215
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:698
void AddArg(const std::string &name, const std::string &help, const bool debug_only, const OptionsCategory &cat)
Add argument.
Definition: util.cpp:572
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:526
OptionsCategory
Definition: util.h:113
std::string CopyrightHolders(const std::string &strPrefix)
Definition: util.cpp:1238
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:999
bool ReadConfigStream(std::istream &stream, std::string &error, bool ignore_invalid_keys=false)
Definition: util.cpp:857
bool error(const char *fmt, const Args &... args)
Definition: util.h:59
int RaiseFileDescriptorLimit(int nMinFD)
this function tries to raise the file descriptor limit to the requested number.
Definition: util.cpp:1074
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: util.cpp:967
ArgsManager gArgs
Definition: util.cpp:88
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: util.cpp:1097
bool DirIsWritable(const fs::path &directory)
Definition: util.cpp:177
bool TruncateFile(FILE *file, unsigned int length)
Definition: util.cpp:1062
std::string m_help_param
Definition: util.h:138
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: util.cpp:483
fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition: util.cpp:1255
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: util.cpp:675
std::string _(const char *psz)
Translation function.
Definition: util.h:50
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate a message to the native language of the user.