]> Git Repo - VerusCoin.git/blame - src/util.cpp
Auto merge of #2259 - nathan-at-least:simplify-joinsplit-priority2, r=ebfull
[VerusCoin.git] / src / util.cpp
CommitLineData
2097c09a 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
c63a73d1 3// Distributed under the MIT software license, see the accompanying
3a25a2b9 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
36949554 5
4c61ba40
WL
6#if defined(HAVE_CONFIG_H)
7#include "config/bitcoin-config.h"
8#endif
9
51ed9ec9
BD
10#include "util.h"
11
84ce18ca 12#include "chainparamsbase.h"
6354935c 13#include "random.h"
ad49c256 14#include "serialize.h"
51ed9ec9 15#include "sync.h"
ad49c256
WL
16#include "utilstrencodings.h"
17#include "utiltime.h"
51ed9ec9 18
51ed9ec9 19#include <stdarg.h>
51ed9ec9 20
5207f33f
PK
21#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
22#include <pthread.h>
23#include <pthread_np.h>
24#endif
25
288fdc09
PW
26#ifndef WIN32
27// for posix_fallocate
76c49c41 28#ifdef __linux__
51ed9ec9
BD
29
30#ifdef _POSIX_C_SOURCE
31#undef _POSIX_C_SOURCE
32#endif
33
288fdc09 34#define _POSIX_C_SOURCE 200112L
51ed9ec9 35
76c49c41 36#endif // __linux__
51ed9ec9 37
b94595bb 38#include <algorithm>
288fdc09 39#include <fcntl.h>
ba29a559 40#include <sys/resource.h>
51ed9ec9 41#include <sys/stat.h>
1f29d399 42
51ed9ec9 43#else
ed6d0b5f 44
ed6d0b5f
PW
45#ifdef _MSC_VER
46#pragma warning(disable:4786)
47#pragma warning(disable:4804)
48#pragma warning(disable:4805)
49#pragma warning(disable:4717)
50#endif
51ed9ec9 51
ed6d0b5f
PW
52#ifdef _WIN32_WINNT
53#undef _WIN32_WINNT
54#endif
55#define _WIN32_WINNT 0x0501
51ed9ec9 56
ed6d0b5f
PW
57#ifdef _WIN32_IE
58#undef _WIN32_IE
59#endif
234db30d 60#define _WIN32_IE 0x0501
51ed9ec9 61
ed6d0b5f
PW
62#define WIN32_LEAN_AND_MEAN 1
63#ifndef NOMINMAX
64#define NOMINMAX
65#endif
51ed9ec9 66
5f986195 67#include <io.h> /* for _commit */
51ed9ec9 68#include <shlobj.h>
ed6d0b5f 69#endif
2097c09a 70
4c61ba40
WL
71#ifdef HAVE_SYS_PRCTL_H
72#include <sys/prctl.h>
73#endif
74
51ed9ec9
BD
75#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
76#include <boost/algorithm/string/join.hpp>
77#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
78#include <boost/filesystem.hpp>
79#include <boost/filesystem/fstream.hpp>
80#include <boost/foreach.hpp>
81#include <boost/program_options/detail/config_file.hpp>
82#include <boost/program_options/parsers.hpp>
ad49c256 83#include <boost/thread.hpp>
51ed9ec9 84#include <openssl/crypto.h>
25314386 85#include <openssl/conf.h>
51ed9ec9
BD
86
87// Work around clang compilation problem in Boost 1.46:
88// /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup
89// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
90// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
91namespace boost {
e10dcf27 92
51ed9ec9
BD
93 namespace program_options {
94 std::string to_internal(const std::string&);
95 }
51ed9ec9 96
e10dcf27 97} // namespace boost
51ed9ec9 98
2097c09a
WL
99using namespace std;
100
101map<string, string> mapArgs;
102map<string, vector<string> > mapMultiArgs;
103bool fDebug = false;
104bool fPrintToConsole = false;
9e9056cd 105bool fPrintToDebugLog = true;
2097c09a
WL
106bool fDaemon = false;
107bool fServer = false;
2097c09a 108string strMiscWarning;
7a4e0e09
CD
109bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
110bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
111bool fLogIPs = DEFAULT_LOGIPS;
bf673640 112std::atomic<bool> fReopenDebugLog(false);
b74dcb3b 113CTranslationInterface translationInterface;
2097c09a 114
c63a73d1 115/** Init OpenSSL library multithreading support */
7f3ccb59 116static CCriticalSection** ppmutexOpenSSL;
60457d3c 117void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
2097c09a 118{
7f3ccb59
PW
119 if (mode & CRYPTO_LOCK) {
120 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
121 } else {
122 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
123 }
2097c09a
WL
124}
125
126// Init
127class CInit
128{
129public:
130 CInit()
131 {
a7f82808 132 // Init OpenSSL library multithreading support
7f3ccb59 133 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
2097c09a 134 for (int i = 0; i < CRYPTO_num_locks(); i++)
7f3ccb59 135 ppmutexOpenSSL[i] = new CCriticalSection();
2097c09a
WL
136 CRYPTO_set_locking_callback(locking_callback);
137
25314386
CF
138 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
139 // We don't use them so we don't require the config. However some of our libs may call functions
140 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
141 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
142 // that the config appears to have been loaded and there are no modules/engines available.
143 OPENSSL_no_config();
2097c09a
WL
144 }
145 ~CInit()
146 {
a7f82808 147 // Shutdown OpenSSL library multithreading support
2097c09a
WL
148 CRYPTO_set_locking_callback(NULL);
149 for (int i = 0; i < CRYPTO_num_locks(); i++)
150 delete ppmutexOpenSSL[i];
151 OPENSSL_free(ppmutexOpenSSL);
152 }
153}
154instance_of_cinit;
155
c63a73d1
MF
156/**
157 * LogPrintf() has been broken a couple of times now
158 * by well-meaning people adding mutexes in the most straightforward way.
159 * It breaks because it may be called by global destructors during shutdown.
160 * Since the order of destruction of static/global objects is undefined,
161 * defining a mutex as a global object doesn't work (the mutex gets
162 * destroyed, and then some later destructor calls OutputDebugStringF,
163 * maybe indirectly, and you get a core dump at shutdown trying to lock
164 * the mutex).
165 */
2097c09a 166
ee337423 167static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
8550bcfe 168
c63a73d1 169/**
8550bcfe
AW
170 * We use boost::call_once() to make sure mutexDebugLog and
171 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
172 *
173 * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
174 * are leaked on exit. This is ugly, but will be cleaned up by
175 * the OS/libc. When the shutdown sequence is fully audited and
176 * tested, explicit destruction of these objects can be implemented.
c63a73d1 177 */
ee337423
GA
178static FILE* fileout = NULL;
179static boost::mutex* mutexDebugLog = NULL;
8550bcfe
AW
180static list<string> *vMsgsBeforeOpenLog;
181
182static int FileWriteStr(const std::string &str, FILE *fp)
183{
184 return fwrite(str.data(), 1, str.size(), fp);
185}
ee337423
GA
186
187static void DebugPrintInit()
2097c09a 188{
ee337423 189 assert(mutexDebugLog == NULL);
8550bcfe
AW
190 mutexDebugLog = new boost::mutex();
191 vMsgsBeforeOpenLog = new list<string>;
192}
ee337423 193
8550bcfe
AW
194void OpenDebugLog()
195{
196 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
197 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
198
199 assert(fileout == NULL);
200 assert(vMsgsBeforeOpenLog);
ee337423
GA
201 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
202 fileout = fopen(pathDebug.string().c_str(), "a");
203 if (fileout) setbuf(fileout, NULL); // unbuffered
204
8550bcfe
AW
205 // dump buffered messages from before we opened the log
206 while (!vMsgsBeforeOpenLog->empty()) {
207 FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
208 vMsgsBeforeOpenLog->pop_front();
209 }
210
211 delete vMsgsBeforeOpenLog;
212 vMsgsBeforeOpenLog = NULL;
ee337423
GA
213}
214
b77dfdc9 215bool LogAcceptCategory(const char* category)
ee337423 216{
e51321fb
GA
217 if (category != NULL)
218 {
3b570559 219 if (!fDebug)
b77dfdc9 220 return false;
3b570559 221
0b238b27
GA
222 // Give each thread quick access to -debug settings.
223 // This helps prevent issues debugging global destructors,
224 // where mapMultiArgs might be deleted before another
225 // global destructor calls LogPrint()
226 static boost::thread_specific_ptr<set<string> > ptrCategory;
227 if (ptrCategory.get() == NULL)
3b570559 228 {
0b238b27
GA
229 const vector<string>& categories = mapMultiArgs["-debug"];
230 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
231 // thread_specific_ptr automatically deletes the set when the thread ends.
3b570559 232 }
0b238b27
GA
233 const set<string>& setCategories = *ptrCategory.get();
234
235 // if not debugging everything and not debugging specific category, LogPrint does nothing.
236 if (setCategories.count(string("")) == 0 &&
1d2db276 237 setCategories.count(string("1")) == 0 &&
0b238b27 238 setCategories.count(string(category)) == 0)
b77dfdc9 239 return false;
e51321fb 240 }
b77dfdc9
WL
241 return true;
242}
e51321fb 243
8550bcfe
AW
244/**
245 * fStartedNewLine is a state variable held by the calling context that will
246 * suppress printing of the timestamp when multiple calls are made that don't
247 * end in a newline. Initialize it to true, and hold it, in the calling context.
248 */
249static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
250{
251 string strStamped;
252
253 if (!fLogTimestamps)
254 return str;
255
256 if (*fStartedNewLine)
257 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()) + ' ' + str;
258 else
259 strStamped = str;
260
261 if (!str.empty() && str[str.size()-1] == '\n')
262 *fStartedNewLine = true;
263 else
264 *fStartedNewLine = false;
265
266 return strStamped;
267}
268
b77dfdc9
WL
269int LogPrintStr(const std::string &str)
270{
ee337423 271 int ret = 0; // Returns total number of characters written
8550bcfe 272 static bool fStartedNewLine = true;
2097c09a
WL
273 if (fPrintToConsole)
274 {
275 // print to console
b77dfdc9 276 ret = fwrite(str.data(), 1, str.size(), stdout);
e982b574 277 fflush(stdout);
2097c09a 278 }
8550bcfe 279 else if (fPrintToDebugLog)
2097c09a 280 {
ee337423 281 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
ee337423
GA
282 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
283
8550bcfe 284 string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
2097c09a 285
8550bcfe
AW
286 // buffer if we haven't opened the log yet
287 if (fileout == NULL) {
288 assert(vMsgsBeforeOpenLog);
289 ret = strTimestamped.length();
290 vMsgsBeforeOpenLog->push_back(strTimestamped);
291 }
ee337423 292 else
8550bcfe
AW
293 {
294 // reopen the log file, if requested
295 if (fReopenDebugLog) {
296 fReopenDebugLog = false;
297 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
298 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
299 setbuf(fileout, NULL); // unbuffered
300 }
301
302 ret = FileWriteStr(strTimestamped, fileout);
303 }
2097c09a 304 }
2097c09a
WL
305 return ret;
306}
307
d64e124c
CM
308static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
309{
310 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
311 if (name.find("-no") == 0)
312 {
313 std::string positive("-");
314 positive.append(name.begin()+3, name.end());
315 if (mapSettingsRet.count(positive) == 0)
316 {
3260b4c0 317 bool value = !GetBoolArg(name, false);
d64e124c
CM
318 mapSettingsRet[positive] = (value ? "1" : "0");
319 }
320 }
321}
322
3e468840 323void ParseParameters(int argc, const char* const argv[])
2097c09a
WL
324{
325 mapArgs.clear();
326 mapMultiArgs.clear();
71aaff39 327
2097c09a
WL
328 for (int i = 1; i < argc; i++)
329 {
6032e4f4
WL
330 std::string str(argv[i]);
331 std::string strValue;
332 size_t is_index = str.find('=');
333 if (is_index != std::string::npos)
2097c09a 334 {
6032e4f4
WL
335 strValue = str.substr(is_index+1);
336 str = str.substr(0, is_index);
2097c09a 337 }
6032e4f4
WL
338#ifdef WIN32
339 boost::to_lower(str);
340 if (boost::algorithm::starts_with(str, "/"))
341 str = "-" + str.substr(1);
342#endif
71aaff39 343
6032e4f4 344 if (str[0] != '-')
2097c09a 345 break;
3ad9f8a7 346
71aaff39
KZ
347 // Interpret --foo as -foo.
348 // If both --foo and -foo are set, the last takes effect.
349 if (str.length() > 1 && str[1] == '-')
350 str = str.substr(1);
351
6032e4f4
WL
352 mapArgs[str] = strValue;
353 mapMultiArgs[str].push_back(strValue);
2097c09a 354 }
3ad9f8a7
GA
355
356 // New 0.6 features:
357 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
358 {
d64e124c 359 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
71aaff39 360 InterpretNegativeSetting(entry.first, mapArgs);
3ad9f8a7 361 }
2097c09a
WL
362}
363
3ae07355
GA
364std::string GetArg(const std::string& strArg, const std::string& strDefault)
365{
366 if (mapArgs.count(strArg))
367 return mapArgs[strArg];
368 return strDefault;
369}
370
51ed9ec9 371int64_t GetArg(const std::string& strArg, int64_t nDefault)
3ae07355
GA
372{
373 if (mapArgs.count(strArg))
374 return atoi64(mapArgs[strArg]);
375 return nDefault;
376}
377
378bool GetBoolArg(const std::string& strArg, bool fDefault)
379{
380 if (mapArgs.count(strArg))
381 {
382 if (mapArgs[strArg].empty())
383 return true;
384 return (atoi(mapArgs[strArg]) != 0);
385 }
386 return fDefault;
387}
388
0fcf91ea
GA
389bool SoftSetArg(const std::string& strArg, const std::string& strValue)
390{
391 if (mapArgs.count(strArg))
392 return false;
393 mapArgs[strArg] = strValue;
394 return true;
395}
396
7bf8b7c2 397bool SoftSetBoolArg(const std::string& strArg, bool fValue)
0fcf91ea
GA
398{
399 if (fValue)
400 return SoftSetArg(strArg, std::string("1"));
401 else
402 return SoftSetArg(strArg, std::string("0"));
403}
404
1fdb9fa3
LV
405static const int screenWidth = 79;
406static const int optIndent = 2;
407static const int msgIndent = 7;
408
409std::string HelpMessageGroup(const std::string &message) {
410 return std::string(message) + std::string("\n\n");
411}
412
413std::string HelpMessageOpt(const std::string &option, const std::string &message) {
414 return std::string(optIndent,' ') + std::string(option) +
415 std::string("\n") + std::string(msgIndent,' ') +
416 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
417 std::string("\n\n");
418}
419
27df4123 420static std::string FormatException(const std::exception* pex, const char* pszThread)
2097c09a 421{
6853e627 422#ifdef WIN32
3e468840 423 char pszModule[MAX_PATH] = "";
2097c09a
WL
424 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
425#else
8fd71a19 426 const char* pszModule = "Zcash";
2097c09a
WL
427#endif
428 if (pex)
29b79e4c 429 return strprintf(
2097c09a
WL
430 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
431 else
29b79e4c 432 return strprintf(
2097c09a
WL
433 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
434}
435
27df4123 436void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
2097c09a 437{
29b79e4c 438 std::string message = FormatException(pex, pszThread);
7d9d134b 439 LogPrintf("\n\n************************\n%s\n", message);
29b79e4c
WL
440 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
441 strMiscWarning = message;
2097c09a
WL
442}
443
ee12c3d6 444boost::filesystem::path GetDefaultDataDir()
2097c09a 445{
ee12c3d6 446 namespace fs = boost::filesystem;
8a183335
TH
447 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
448 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
449 // Mac: ~/Library/Application Support/Zcash
450 // Unix: ~/.zcash
6853e627 451#ifdef WIN32
2097c09a 452 // Windows
8a183335 453 return GetSpecialFolderPath(CSIDL_APPDATA) / "Zcash";
2097c09a 454#else
ee12c3d6 455 fs::path pathRet;
2097c09a
WL
456 char* pszHome = getenv("HOME");
457 if (pszHome == NULL || strlen(pszHome) == 0)
ee12c3d6
PW
458 pathRet = fs::path("/");
459 else
460 pathRet = fs::path(pszHome);
6853e627 461#ifdef MAC_OSX
2097c09a 462 // Mac
940e22fd 463 pathRet /= "Library/Application Support";
2b7709dc 464 TryCreateDirectory(pathRet);
8a183335 465 return pathRet / "Zcash";
2097c09a
WL
466#else
467 // Unix
8a183335 468 return pathRet / ".zcash";
2097c09a
WL
469#endif
470#endif
471}
472
ebdb9ff6 473static boost::filesystem::path pathCached;
474static boost::filesystem::path pathCachedNetSpecific;
4f1c3798 475static boost::filesystem::path zc_paramsPathCached;
b94595bb
GA
476static CCriticalSection csPathCached;
477
4f1c3798
SB
478static boost::filesystem::path ZC_GetBaseParamsDir()
479{
480 // Copied from GetDefaultDataDir and adapter for zcash params.
481
482 namespace fs = boost::filesystem;
483 // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
484 // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
485 // Mac: ~/Library/Application Support/ZcashParams
486 // Unix: ~/.zcash-params
487#ifdef WIN32
488 // Windows
489 return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
490#else
491 fs::path pathRet;
492 char* pszHome = getenv("HOME");
493 if (pszHome == NULL || strlen(pszHome) == 0)
494 pathRet = fs::path("/");
495 else
496 pathRet = fs::path(pszHome);
497#ifdef MAC_OSX
498 // Mac
499 pathRet /= "Library/Application Support";
500 TryCreateDirectory(pathRet);
501 return pathRet / "ZcashParams";
502#else
503 // Unix
504 return pathRet / ".zcash-params";
505#endif
506#endif
507}
508
509const boost::filesystem::path &ZC_GetParamsDir()
510{
511 namespace fs = boost::filesystem;
512
513 LOCK(csPathCached); // Reuse the same lock as upstream.
514
515 fs::path &path = zc_paramsPathCached;
516
517 // This can be called during exceptions by LogPrintf(), so we cache the
518 // value so we don't have to do memory allocations after that.
519 if (!path.empty())
520 return path;
521
522 path = ZC_GetBaseParamsDir();
4f1c3798
SB
523
524 return path;
525}
526
9064d73b
S
527// Return the user specified export directory. Create directory if it doesn't exist.
528// If user did not set option, return an empty path.
529// If there is a filesystem problem, throw an exception.
530const boost::filesystem::path GetExportDir()
531{
532 namespace fs = boost::filesystem;
533 fs::path path;
534 if (mapArgs.count("-exportdir")) {
535 path = fs::system_complete(mapArgs["-exportdir"]);
536 if (fs::exists(path) && !fs::is_directory(path)) {
537 throw std::runtime_error(strprintf("The -exportdir '%s' already exists and is not a directory", path.string()));
538 }
539 if (!fs::exists(path) && !fs::create_directories(path)) {
540 throw std::runtime_error(strprintf("Failed to create directory at -exportdir '%s'", path.string()));
541 }
542 }
543 return path;
544}
545
546
ee12c3d6 547const boost::filesystem::path &GetDataDir(bool fNetSpecific)
2097c09a 548{
ee12c3d6
PW
549 namespace fs = boost::filesystem;
550
b94595bb
GA
551 LOCK(csPathCached);
552
ebdb9ff6 553 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
ee12c3d6 554
faaeae1e 555 // This can be called during exceptions by LogPrintf(), so we cache the
ee12c3d6 556 // value so we don't have to do memory allocations after that.
b94595bb 557 if (!path.empty())
ee12c3d6
PW
558 return path;
559
ee12c3d6 560 if (mapArgs.count("-datadir")) {
f4203de3
PW
561 path = fs::system_complete(mapArgs["-datadir"]);
562 if (!fs::is_directory(path)) {
563 path = "";
564 return path;
565 }
ee12c3d6
PW
566 } else {
567 path = GetDefaultDataDir();
2097c09a 568 }
0e4b3175 569 if (fNetSpecific)
84ce18ca 570 path /= BaseParams().DataDir();
2097c09a 571
3e9c8bab 572 fs::create_directories(path);
ee12c3d6 573
ee12c3d6 574 return path;
2097c09a
WL
575}
576
b94595bb
GA
577void ClearDatadirCache()
578{
ebdb9ff6 579 pathCached = boost::filesystem::path();
580 pathCachedNetSpecific = boost::filesystem::path();
b94595bb
GA
581}
582
ee12c3d6 583boost::filesystem::path GetConfigFile()
2097c09a 584{
aaf64959 585 boost::filesystem::path pathConfigFile(GetArg("-conf", "zcash.conf"));
ac14bcc1
PK
586 if (!pathConfigFile.is_complete())
587 pathConfigFile = GetDataDir(false) / pathConfigFile;
588
ee12c3d6 589 return pathConfigFile;
2097c09a
WL
590}
591
f4203de3 592void ReadConfigFile(map<string, string>& mapSettingsRet,
2097c09a
WL
593 map<string, vector<string> >& mapMultiSettingsRet)
594{
3e468840 595 boost::filesystem::ifstream streamConfig(GetConfigFile());
2097c09a 596 if (!streamConfig.good())
d87f00c4 597 throw missing_zcash_conf();
2097c09a
WL
598
599 set<string> setOptions;
600 setOptions.insert("*");
ee12c3d6 601
3e468840 602 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
2097c09a 603 {
aaf64959 604 // Don't overwrite existing settings so command line settings override zcash.conf
2097c09a
WL
605 string strKey = string("-") + it->string_key;
606 if (mapSettingsRet.count(strKey) == 0)
d64e124c 607 {
2097c09a 608 mapSettingsRet[strKey] = it->value[0];
3e468840 609 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
d64e124c
CM
610 InterpretNegativeSetting(strKey, mapSettingsRet);
611 }
2097c09a
WL
612 mapMultiSettingsRet[strKey].push_back(it->value[0]);
613 }
b94595bb
GA
614 // If datadir is changed in .conf file:
615 ClearDatadirCache();
2097c09a
WL
616}
617
d6712db3 618#ifndef WIN32
ee12c3d6 619boost::filesystem::path GetPidFile()
2097c09a 620{
aaf64959 621 boost::filesystem::path pathPidFile(GetArg("-pid", "zcashd.pid"));
ee12c3d6
PW
622 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
623 return pathPidFile;
2097c09a
WL
624}
625
ee12c3d6 626void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
2097c09a 627{
ee12c3d6 628 FILE* file = fopen(path.string().c_str(), "w");
f85c0974 629 if (file)
2097c09a
WL
630 {
631 fprintf(file, "%d\n", pid);
632 fclose(file);
633 }
634}
a034c7eb 635#endif
2097c09a 636
768e5d52
JG
637bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
638{
639#ifdef WIN32
5f986195 640 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
8d657a65 641 MOVEFILE_REPLACE_EXISTING) != 0;
768e5d52
JG
642#else
643 int rc = std::rename(src.string().c_str(), dest.string().c_str());
644 return (rc == 0);
645#endif /* WIN32 */
646}
647
c63a73d1
MF
648/**
649 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
650 * Specifically handles case where path p exists, but it wasn't possible for the user to
651 * write to the parent directory.
652 */
2b7709dc
BD
653bool TryCreateDirectory(const boost::filesystem::path& p)
654{
655 try
656 {
657 return boost::filesystem::create_directory(p);
27df4123 658 } catch (const boost::filesystem::filesystem_error&) {
2b7709dc
BD
659 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
660 throw;
661 }
662
663 // create_directory didn't create the directory, it had to have existed already
664 return false;
665}
666
768e5d52
JG
667void FileCommit(FILE *fileout)
668{
4c0b2cde 669 fflush(fileout); // harmless if redundantly called
768e5d52 670#ifdef WIN32
4c0b2cde
PK
671 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
672 FlushFileBuffers(hFile);
768e5d52 673#else
e9965213
PW
674 #if defined(__linux__) || defined(__NetBSD__)
675 fdatasync(fileno(fileout));
e7bad10c
GM
676 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
677 fcntl(fileno(fileout), F_FULLFSYNC, 0);
e9965213 678 #else
768e5d52 679 fsync(fileno(fileout));
e9965213 680 #endif
768e5d52
JG
681#endif
682}
683
1eb57879
PW
684bool TruncateFile(FILE *file, unsigned int length) {
685#if defined(WIN32)
686 return _chsize(_fileno(file), length) == 0;
687#else
688 return ftruncate(fileno(file), length) == 0;
689#endif
690}
691
c63a73d1
MF
692/**
693 * this function tries to raise the file descriptor limit to the requested number.
694 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
695 */
ba29a559
PW
696int RaiseFileDescriptorLimit(int nMinFD) {
697#if defined(WIN32)
698 return 2048;
699#else
700 struct rlimit limitFD;
701 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
702 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
703 limitFD.rlim_cur = nMinFD;
704 if (limitFD.rlim_cur > limitFD.rlim_max)
705 limitFD.rlim_cur = limitFD.rlim_max;
706 setrlimit(RLIMIT_NOFILE, &limitFD);
707 getrlimit(RLIMIT_NOFILE, &limitFD);
708 }
709 return limitFD.rlim_cur;
710 }
711 return nMinFD; // getrlimit failed, assume it's fine
712#endif
713}
714
c63a73d1
MF
715/**
716 * this function tries to make a particular range of a file allocated (corresponding to disk space)
717 * it is advisory, and the range specified in the arguments will never contain live data
718 */
bba89aa8 719void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
288fdc09
PW
720#if defined(WIN32)
721 // Windows-specific version
722 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
723 LARGE_INTEGER nFileSize;
51ed9ec9 724 int64_t nEndPos = (int64_t)offset + length;
288fdc09
PW
725 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
726 nFileSize.u.HighPart = nEndPos >> 32;
727 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
728 SetEndOfFile(hFile);
729#elif defined(MAC_OSX)
730 // OSX specific version
731 fstore_t fst;
732 fst.fst_flags = F_ALLOCATECONTIG;
733 fst.fst_posmode = F_PEOFPOSMODE;
734 fst.fst_offset = 0;
735 fst.fst_length = (off_t)offset + length;
736 fst.fst_bytesalloc = 0;
737 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
738 fst.fst_flags = F_ALLOCATEALL;
739 fcntl(fileno(file), F_PREALLOCATE, &fst);
740 }
741 ftruncate(fileno(file), fst.fst_length);
742#elif defined(__linux__)
743 // Version using posix_fallocate
744 off_t nEndPos = (off_t)offset + length;
745 posix_fallocate(fileno(file), 0, nEndPos);
746#else
747 // Fallback version
748 // TODO: just write one byte per block
bba89aa8
PW
749 static const char buf[65536] = {};
750 fseek(file, offset, SEEK_SET);
751 while (length > 0) {
752 unsigned int now = 65536;
753 if (length < now)
754 now = length;
755 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
756 length -= now;
757 }
288fdc09 758#endif
bba89aa8
PW
759}
760
2097c09a
WL
761void ShrinkDebugFile()
762{
763 // Scroll debug.log if it's getting too big
ee12c3d6
PW
764 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
765 FILE* file = fopen(pathLog.string().c_str(), "r");
a486abd4 766 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
2097c09a
WL
767 {
768 // Restart the file with some of the end
fcb0a1bb 769 std::vector <char> vch(200000,0);
1e735048 770 fseek(file, -((long)vch.size()), SEEK_END);
fcb0a1bb 771 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
2097c09a 772 fclose(file);
f85c0974 773
ee12c3d6 774 file = fopen(pathLog.string().c_str(), "w");
f85c0974 775 if (file)
2097c09a 776 {
fcb0a1bb 777 fwrite(begin_ptr(vch), 1, nBytes, file);
2097c09a
WL
778 fclose(file);
779 }
780 }
3260b4c0
PK
781 else if (file != NULL)
782 fclose(file);
2097c09a
WL
783}
784
ed6d0b5f 785#ifdef WIN32
3e468840
PK
786boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
787{
788 namespace fs = boost::filesystem;
789
790 char pszPath[MAX_PATH] = "";
791
792 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
793 {
794 return fs::path(pszPath);
795 }
796
881a85a2 797 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
3e468840
PK
798 return fs::path("");
799}
ed6d0b5f 800#endif
429039d4 801
597fa4cd
PW
802boost::filesystem::path GetTempPath() {
803#if BOOST_FILESYSTEM_VERSION == 3
804 return boost::filesystem::temp_directory_path();
805#else
806 // TODO: remove when we don't support filesystem v2 anymore
807 boost::filesystem::path path;
808#ifdef WIN32
809 char pszPath[MAX_PATH] = "";
810
811 if (GetTempPathA(MAX_PATH, pszPath))
812 path = boost::filesystem::path(pszPath);
813#else
814 path = boost::filesystem::path("/tmp");
815#endif
816 if (path.empty() || !boost::filesystem::is_directory(path)) {
881a85a2 817 LogPrintf("GetTempPath(): failed to find temp path\n");
597fa4cd
PW
818 return boost::filesystem::path("");
819 }
820 return path;
821#endif
822}
823
db954a65 824void runCommand(const std::string& strCommand)
429039d4
JG
825{
826 int nErr = ::system(strCommand.c_str());
827 if (nErr)
7d9d134b 828 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
429039d4
JG
829}
830
96931d6f
GS
831void RenameThread(const char* name)
832{
b277b0f1 833#if defined(PR_SET_NAME)
96931d6f
GS
834 // Only the first 15 characters are used (16 - NUL terminator)
835 ::prctl(PR_SET_NAME, name, 0, 0, 0);
7f386d2f 836#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
304ca955 837 pthread_set_name_np(pthread_self(), name);
cd58f058 838
850c570d 839#elif defined(MAC_OSX)
c8c2fbe0 840 pthread_setname_np(name);
96931d6f
GS
841#else
842 // Prevent warnings for unused parameters...
843 (void)name;
844#endif
845}
0b47fe6b 846
5248ff40
SC
847void SetupEnvironment()
848{
317e66c7 849 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
850 // may be invalid, in which case the "C" locale is used as fallback.
851#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
852 try {
3da78490 853 std::locale(""); // Raises a runtime error if current locale is invalid
27df4123 854 } catch (const std::runtime_error&) {
ba0fa0d9 855 setenv("LC_ALL", "C", 1);
5248ff40 856 }
ac14bcc1 857#endif
3da78490 858 // The path locale is lazy initialized and to avoid deinitialization errors
317e66c7 859 // in multithreading environments, it is set explicitly by the main thread.
3da78490
JS
860 // A dummy locale is used to extract the internal default locale, used by
861 // boost::filesystem::path, which is then used to explicitly imbue the path.
862 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
ba0fa0d9 863 boost::filesystem::path::imbue(loc);
5248ff40 864}
3e8ac6af 865
167b6231
WL
866bool SetupNetworking()
867{
868#ifdef WIN32
869 // Initialize Windows Sockets
870 WSADATA wsadata;
871 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
872 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
873 return false;
874#endif
875 return true;
876}
877
610a8c07
WL
878void SetThreadPriority(int nPriority)
879{
880#ifdef WIN32
881 SetThreadPriority(GetCurrentThread(), nPriority);
882#else // WIN32
883#ifdef PRIO_THREAD
884 setpriority(PRIO_THREAD, 0, nPriority);
885#else // PRIO_THREAD
886 setpriority(PRIO_PROCESS, 0, nPriority);
887#endif // PRIO_THREAD
888#endif // WIN32
889}
af021144 890
f0d1accb
DH
891std::string PrivacyInfo()
892{
893 return "\n" +
894 FormatParagraph(strprintf(_("In order to ensure you are adequately protecting your privacy when using Zcash, please see <%s>."),
895 "https://z.cash/support/security/index.html")) + "\n";
896}
897
af021144
S
898std::string LicenseInfo()
899{
f0d1accb 900 return "\n" +
ab1d2d57 901 FormatParagraph(strprintf(_("Copyright (C) 2009-%i The Bitcoin Core Developers"), COPYRIGHT_YEAR)) + "\n" +
af021144
S
902 FormatParagraph(strprintf(_("Copyright (C) 2015-%i The Zcash Developers"), COPYRIGHT_YEAR)) + "\n" +
903 "\n" +
904 FormatParagraph(_("This is experimental software.")) + "\n" +
905 "\n" +
906 FormatParagraph(_("Distributed under the MIT software license, see the accompanying file COPYING or <http://www.opensource.org/licenses/mit-license.php>.")) + "\n" +
907 "\n" +
908 FormatParagraph(_("This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit <https://www.openssl.org/> and cryptographic software written by Eric Young and UPnP software written by Thomas Bernard.")) +
909 "\n";
910}
This page took 0.352874 seconds and 4 git commands to generate.