]> Git Repo - VerusCoin.git/blob - src/util.cpp
decouple Split function from max eras
[VerusCoin.git] / src / util.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
9
10 #include "util.h"
11
12 #include "chainparamsbase.h"
13 #include "random.h"
14 #include "serialize.h"
15 #include "sync.h"
16 #include "utilstrencodings.h"
17 #include "utiltime.h"
18 #include "komodo_defs.h"
19
20 #include <stdarg.h>
21 #include <sstream>
22 #include <vector>
23 #include <stdio.h>
24
25 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
26 #include <pthread.h>
27 #include <pthread_np.h>
28 #endif
29
30 #ifndef _WIN32
31 // for posix_fallocate
32 #ifdef __linux__
33
34 #ifdef _POSIX_C_SOURCE
35 #undef _POSIX_C_SOURCE
36 #endif
37
38 #define _POSIX_C_SOURCE 200112L
39
40 #endif // __linux__
41
42 #include <algorithm>
43 #include <fcntl.h>
44 #include <sys/resource.h>
45 #include <sys/stat.h>
46
47 #else
48
49 #ifdef _MSC_VER
50 #pragma warning(disable:4786)
51 #pragma warning(disable:4804)
52 #pragma warning(disable:4805)
53 #pragma warning(disable:4717)
54 #endif
55
56 #ifdef _WIN32_WINNT
57 #undef _WIN32_WINNT
58 #endif
59 #define _WIN32_WINNT 0x0501
60
61 #ifdef _WIN32_IE
62 #undef _WIN32_IE
63 #endif
64 #define _WIN32_IE 0x0501
65
66 #define WIN32_LEAN_AND_MEAN 1
67 #ifndef NOMINMAX
68 #define NOMINMAX
69 #endif
70
71 #include <io.h> /* for _commit */
72 #include <shlobj.h>
73 #endif
74
75 #ifdef HAVE_SYS_PRCTL_H
76 #include <sys/prctl.h>
77 #endif
78
79 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
80 #include <boost/algorithm/string/join.hpp>
81 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
82 #include <boost/filesystem.hpp>
83 #include <boost/filesystem/fstream.hpp>
84 #include <boost/foreach.hpp>
85 #include <boost/program_options/detail/config_file.hpp>
86 #include <boost/program_options/parsers.hpp>
87 #include <boost/thread.hpp>
88 #include <openssl/crypto.h>
89 #include <openssl/conf.h>
90
91 // Work around clang compilation problem in Boost 1.46:
92 // /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
93 // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
94 //           http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
95 namespace boost {
96
97     namespace program_options {
98         std::string to_internal(const std::string&);
99     }
100
101 } // namespace boost
102
103 using namespace std;
104
105 map<string, string> mapArgs;
106 map<string, vector<string> > mapMultiArgs;
107 bool fDebug = false;
108 bool fPrintToConsole = false;
109 bool fPrintToDebugLog = true;
110 bool fDaemon = false;
111 bool fServer = false;
112 string strMiscWarning;
113 bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
114 bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
115 bool fLogIPs = DEFAULT_LOGIPS;
116 std::atomic<bool> fReopenDebugLog(false);
117 CTranslationInterface translationInterface;
118
119 extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
120
121 bool _IsVerusActive()
122 {
123     return (strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0 || strcmp(ASSETCHAINS_SYMBOL, "VRSCTEST") == 0);
124 }
125
126 /** Init OpenSSL library multithreading support */
127 static CCriticalSection** ppmutexOpenSSL;
128 void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
129 {
130     if (mode & CRYPTO_LOCK) {
131         ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
132     } else {
133         LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
134     }
135 }
136
137 // Init
138 static class CInit
139 {
140 public:
141     CInit()
142     {
143         // Init OpenSSL library multithreading support
144         ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
145         for (int i = 0; i < CRYPTO_num_locks(); i++)
146             ppmutexOpenSSL[i] = new CCriticalSection();
147         CRYPTO_set_locking_callback(locking_callback);
148
149         // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
150         // We don't use them so we don't require the config. However some of our libs may call functions
151         // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
152         // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
153         // that the config appears to have been loaded and there are no modules/engines available.
154         OPENSSL_no_config();
155     }
156     ~CInit()
157     {
158         // Shutdown OpenSSL library multithreading support
159         CRYPTO_set_locking_callback(NULL);
160         for (int i = 0; i < CRYPTO_num_locks(); i++)
161             delete ppmutexOpenSSL[i];
162         OPENSSL_free(ppmutexOpenSSL);
163     }
164 }
165 instance_of_cinit;
166
167 /**
168  * LogPrintf() has been broken a couple of times now
169  * by well-meaning people adding mutexes in the most straightforward way.
170  * It breaks because it may be called by global destructors during shutdown.
171  * Since the order of destruction of static/global objects is undefined,
172  * defining a mutex as a global object doesn't work (the mutex gets
173  * destroyed, and then some later destructor calls OutputDebugStringF,
174  * maybe indirectly, and you get a core dump at shutdown trying to lock
175  * the mutex).
176  */
177
178 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
179
180 /**
181  * We use boost::call_once() to make sure mutexDebugLog and
182  * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
183  *
184  * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
185  * are leaked on exit. This is ugly, but will be cleaned up by
186  * the OS/libc. When the shutdown sequence is fully audited and
187  * tested, explicit destruction of these objects can be implemented.
188  */
189 static FILE* fileout = NULL;
190 static boost::mutex* mutexDebugLog = NULL;
191 static list<string> *vMsgsBeforeOpenLog;
192
193 [[noreturn]] void new_handler_terminate()
194 {
195     // Rather than throwing std::bad-alloc if allocation fails, terminate
196     // immediately to (try to) avoid chain corruption.
197     // Since LogPrintf may itself allocate memory, set the handler directly
198     // to terminate first.
199     std::set_new_handler(std::terminate);
200     fputs("Error: Out of memory. Terminating.\n", stderr);
201     LogPrintf("Error: Out of memory. Terminating.\n");
202
203     // The log was successful, terminate now.
204     std::terminate();
205 };
206
207 static int FileWriteStr(const std::string &str, FILE *fp)
208 {
209     return fwrite(str.data(), 1, str.size(), fp);
210 }
211
212 static void DebugPrintInit()
213 {
214     assert(mutexDebugLog == NULL);
215     mutexDebugLog = new boost::mutex();
216     vMsgsBeforeOpenLog = new list<string>;
217 }
218
219 void OpenDebugLog()
220 {
221     boost::call_once(&DebugPrintInit, debugPrintInitFlag);
222     boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
223
224     assert(fileout == NULL);
225     assert(vMsgsBeforeOpenLog);
226     boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
227     fileout = fopen(pathDebug.string().c_str(), "a");
228     if (fileout) setbuf(fileout, NULL); // unbuffered
229
230     // dump buffered messages from before we opened the log
231     while (!vMsgsBeforeOpenLog->empty()) {
232         FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
233         vMsgsBeforeOpenLog->pop_front();
234     }
235
236     delete vMsgsBeforeOpenLog;
237     vMsgsBeforeOpenLog = NULL;
238 }
239
240 bool LogAcceptCategory(const char* category)
241 {
242     if (category != NULL)
243     {
244         if (!fDebug)
245             return false;
246
247         // Give each thread quick access to -debug settings.
248         // This helps prevent issues debugging global destructors,
249         // where mapMultiArgs might be deleted before another
250         // global destructor calls LogPrint()
251         static boost::thread_specific_ptr<set<string>> ptrCategory;
252         if (ptrCategory.get() == NULL)
253         {
254             const vector<string>& categories = mapMultiArgs["-debug"];
255             ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
256             // thread_specific_ptr automatically deletes the set when the thread ends.
257         }
258         const set<string>& setCategories = *ptrCategory.get();
259
260         // if not debugging everything and not debugging specific category, LogPrint does nothing.
261         if (setCategories.count(string("")) == 0 &&
262             setCategories.count(string("1")) == 0 &&
263             setCategories.count(string(category)) == 0)
264             return false;
265     }
266     return true;
267 }
268
269 /**
270  * fStartedNewLine is a state variable held by the calling context that will
271  * suppress printing of the timestamp when multiple calls are made that don't
272  * end in a newline. Initialize it to true, and hold it, in the calling context.
273  */
274 static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
275 {
276     string strStamped;
277
278     if (!fLogTimestamps)
279         return str;
280
281     if (*fStartedNewLine)
282         strStamped =  DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()) + ' ' + str;
283     else
284         strStamped = str;
285
286     if (!str.empty() && str[str.size()-1] == '\n')
287         *fStartedNewLine = true;
288     else
289         *fStartedNewLine = false;
290
291     return strStamped;
292 }
293
294 int LogPrintStr(const std::string &str)
295 {
296     int ret = 0; // Returns total number of characters written
297     static bool fStartedNewLine = true;
298     if (fPrintToConsole)
299     {
300         // print to console
301         ret = fwrite(str.data(), 1, str.size(), stdout);
302         fflush(stdout);
303     }
304     else if (fPrintToDebugLog)
305     {
306         boost::call_once(&DebugPrintInit, debugPrintInitFlag);
307         boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
308
309         string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
310
311         // buffer if we haven't opened the log yet
312         if (fileout == NULL) {
313             assert(vMsgsBeforeOpenLog);
314             ret = strTimestamped.length();
315             vMsgsBeforeOpenLog->push_back(strTimestamped);
316         }
317         else
318         {
319             // reopen the log file, if requested
320             if (fReopenDebugLog) {
321                 fReopenDebugLog = false;
322                 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
323                 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
324                     setbuf(fileout, NULL); // unbuffered
325             }
326
327             ret = FileWriteStr(strTimestamped, fileout);
328         }
329     }
330     return ret;
331 }
332
333 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
334 {
335     // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
336     if (name.find("-no") == 0)
337     {
338         std::string positive("-");
339         positive.append(name.begin()+3, name.end());
340         if (mapSettingsRet.count(positive) == 0)
341         {
342             bool value = !GetBoolArg(name, false);
343             mapSettingsRet[positive] = (value ? "1" : "0");
344         }
345     }
346 }
347
348 void ParseParameters(int argc, const char* const argv[])
349 {
350     mapArgs.clear();
351     mapMultiArgs.clear();
352
353     for (int i = 1; i < argc; i++)
354     {
355         std::string str(argv[i]);
356         std::string strValue;
357         size_t is_index = str.find('=');
358         if (is_index != std::string::npos)
359         {
360             strValue = str.substr(is_index+1);
361             str = str.substr(0, is_index);
362         }
363 #ifdef _WIN32
364         boost::to_lower(str);
365         if (boost::algorithm::starts_with(str, "/"))
366             str = "-" + str.substr(1);
367 #endif
368
369         if (str[0] != '-')
370             break;
371
372         // Interpret --foo as -foo.
373         // If both --foo and -foo are set, the last takes effect.
374         if (str.length() > 1 && str[1] == '-')
375             str = str.substr(1);
376
377         mapArgs[str] = strValue;
378         mapMultiArgs[str].push_back(strValue);
379     }
380
381     // New 0.6 features:
382     BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
383     {
384         // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
385         InterpretNegativeSetting(entry.first, mapArgs);
386     }
387 }
388
389 void Split(const std::string& strVal, uint64_t *outVals, const uint64_t nDefault,int maxElements)
390 {
391     stringstream ss(strVal);
392     vector<uint64_t> vec;
393
394     uint64_t i, nLast, numVals = 0;
395
396     while ( ss.peek() == ' ' )
397         ss.ignore();
398
399     while ( numVals < maxElements && ss >> i )
400     {
401         outVals[numVals] = i;
402         numVals += 1;
403
404         while ( ss.peek() == ' ' )
405             ss.ignore();
406         if ( ss.peek() == ',' )
407             ss.ignore();
408         while ( ss.peek() == ' ' )
409             ss.ignore();
410     }
411
412     if ( numVals > 0 )
413         nLast = outVals[numVals - 1];
414     else
415         nLast = nDefault;
416
417     for ( i = numVals; i < maxElements; i++ )
418     {
419         outVals[i] = nLast;
420     }
421 }
422
423 std::string GetArg(const std::string& strArg, const std::string& strDefault)
424 {
425     if (mapArgs.count(strArg))
426         return mapArgs[strArg];
427     return strDefault;
428 }
429
430 int64_t GetArg(const std::string& strArg, int64_t nDefault)
431 {
432     if (mapArgs.count(strArg))
433         return atoi64(mapArgs[strArg]);
434     return nDefault;
435 }
436
437 bool GetBoolArg(const std::string& strArg, bool fDefault)
438 {
439     if (mapArgs.count(strArg))
440     {
441         if (mapArgs[strArg].empty())
442             return true;
443         return (atoi(mapArgs[strArg]) != 0);
444     }
445     return fDefault;
446 }
447
448 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
449 {
450     if (mapArgs.count(strArg))
451         return false;
452     mapArgs[strArg] = strValue;
453     return true;
454 }
455
456 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
457 {
458     if (fValue)
459         return SoftSetArg(strArg, std::string("1"));
460     else
461         return SoftSetArg(strArg, std::string("0"));
462 }
463
464 static const int screenWidth = 79;
465 static const int optIndent = 2;
466 static const int msgIndent = 7;
467
468 std::string HelpMessageGroup(const std::string &message) {
469     return std::string(message) + std::string("\n\n");
470 }
471
472 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
473     return std::string(optIndent,' ') + std::string(option) +
474            std::string("\n") + std::string(msgIndent,' ') +
475            FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
476            std::string("\n\n");
477 }
478
479 static std::string FormatException(const std::exception* pex, const char* pszThread)
480 {
481 #ifdef _WIN32
482     char pszModule[MAX_PATH] = "";
483     GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
484 #else
485     const char* pszModule = "Komodo";
486 #endif
487     if (pex)
488         return strprintf(
489             "EXCEPTION: %s       \n%s       \n%s in %s       \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
490     else
491         return strprintf(
492             "UNKNOWN EXCEPTION       \n%s in %s       \n", pszModule, pszThread);
493 }
494
495 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
496 {
497     std::string message = FormatException(pex, pszThread);
498     LogPrintf("\n\n************************\n%s\n", message);
499     fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
500     strMiscWarning = message;
501 }
502
503 //int64_t MAX_MONEY = 200000000 * 100000000LL;
504
505 boost::filesystem::path GetDefaultDataDir()
506 {
507     namespace fs = boost::filesystem;
508     char symbol[KOMODO_ASSETCHAIN_MAXLEN];
509     if ( ASSETCHAINS_SYMBOL[0] != 0 )
510         strcpy(symbol,ASSETCHAINS_SYMBOL);
511     else symbol[0] = 0;
512     // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
513     // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
514     // Mac: ~/Library/Application Support/Zcash
515     // Unix: ~/.zcash
516 #ifdef _WIN32
517     // Windows
518     if ( symbol[0] == 0 )
519         return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo";
520     else
521     {
522         if (_IsVerusActive())
523         {
524             return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo" / symbol;
525         }
526         else
527         {
528             return GetSpecialFolderPath(CSIDL_APPDATA) / (PBAAS_TESTMODE ? "VerusTest" : "Verus") / "PBAAS" / symbol;
529         }
530     }
531 #else
532     fs::path pathRet;
533     char* pszHome = getenv("HOME");
534     if (pszHome == NULL || strlen(pszHome) == 0)
535         pathRet = fs::path("/");
536     else
537         pathRet = fs::path(pszHome);
538 #ifdef MAC_OSX
539     // Mac
540     pathRet /= "Library/Application Support";
541     TryCreateDirectory(pathRet);
542     if ( symbol[0] == 0 )
543         return pathRet / "Komodo";
544     else
545     {
546         if (_IsVerusActive())
547         {
548             pathRet /= "Komodo";
549             TryCreateDirectory(pathRet);
550         }
551         else
552         {
553             pathRet /= PBAAS_TESTMODE ? "VerusTest" : "Verus";
554             TryCreateDirectory(pathRet);
555             pathRet /= "PBAAS";
556             TryCreateDirectory(pathRet);
557         }
558         return pathRet / symbol;
559     }
560 #else
561     // Unix
562     if ( symbol[0] == 0 )
563         return pathRet / ".komodo";
564     else
565     {
566         if (_IsVerusActive())
567         {
568             return pathRet / ".komodo" / symbol;
569         }
570         else
571         {
572             return pathRet / (PBAAS_TESTMODE ? ".verustest" : ".verus") / "PBAAS" / symbol;
573         }
574     }
575 #endif
576 #endif
577 }
578
579 boost::filesystem::path GetDefaultDataDir(std::string chainName)
580 {
581     char symbol[KOMODO_ASSETCHAIN_MAXLEN];
582     if (chainName.size() >= KOMODO_ASSETCHAIN_MAXLEN)
583         chainName.resize(KOMODO_ASSETCHAIN_MAXLEN - 1);
584     strcpy(symbol, chainName.c_str());
585
586     namespace fs = boost::filesystem;
587
588     // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
589     // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
590     // Mac: ~/Library/Application Support/Zcash
591     // Unix: ~/.zcash
592 #ifdef _WIN32
593     // Windows
594     if (chainName == "VRSC" || chainName == "VRSCTEST")
595     {
596         return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo" / symbol;
597     }
598     else
599     {
600         return GetSpecialFolderPath(CSIDL_APPDATA) / (PBAAS_TESTMODE ? "VerusTest" : "Verus") / "PBAAS" / symbol;
601     }
602 #else
603     fs::path pathRet;
604     char* pszHome = getenv("HOME");
605     if (pszHome == NULL || strlen(pszHome) == 0)
606         pathRet = fs::path("/");
607     else
608         pathRet = fs::path(pszHome);
609 #ifdef MAC_OSX
610     // Mac
611     pathRet /= "Library/Application Support";
612     TryCreateDirectory(pathRet);
613     if (chainName == "VRSC" || chainName == "VRSCTEST")
614     {
615         pathRet /= "Komodo";
616         TryCreateDirectory(pathRet);
617     }
618     else
619     {
620         pathRet /= PBAAS_TESTMODE ? "VerusTest" : "Verus";
621         TryCreateDirectory(pathRet);
622         pathRet /= "PBAAS";
623         TryCreateDirectory(pathRet);
624     }
625     return pathRet / symbol;
626 #else
627     // Unix
628     if (chainName == "VRSC" || chainName == "VRSCTEST")
629     {
630         return pathRet / ".komodo" / symbol;
631     }
632     else
633     {
634         return pathRet / (PBAAS_TESTMODE ? ".verustest" : ".verus") / "PBAAS" / symbol;
635     }
636 #endif
637 #endif
638 }
639
640 static boost::filesystem::path pathCached;
641 static boost::filesystem::path pathCachedNetSpecific;
642 static boost::filesystem::path zc_paramsPathCached;
643 static CCriticalSection csPathCached;
644
645 static boost::filesystem::path ZC_GetBaseParamsDir()
646 {
647     // Copied from GetDefaultDataDir and adapter for zcash params.
648
649     namespace fs = boost::filesystem;
650     // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
651     // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
652     // Mac: ~/Library/Application Support/ZcashParams
653     // Unix: ~/.zcash-params
654     fs::path pathRet;
655 #ifdef _WIN32
656     return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
657 #else
658     char* pszHome = getenv("HOME");
659     if (pszHome == NULL || strlen(pszHome) == 0)
660         pathRet = fs::path("/");
661     else
662         pathRet = fs::path(pszHome);
663 #ifdef MAC_OSX
664     // Mac
665     pathRet /= "Library/Application Support";
666     TryCreateDirectory(pathRet);
667     return pathRet / "ZcashParams";
668 #else
669     // Unix
670     return pathRet / ".zcash-params";
671 #endif
672 #endif
673 }
674
675 const boost::filesystem::path &ZC_GetParamsDir()
676 {
677     namespace fs = boost::filesystem;
678
679     LOCK(csPathCached); // Reuse the same lock as upstream.
680
681     fs::path &path = zc_paramsPathCached;
682
683     // This can be called during exceptions by LogPrintf(), so we cache the
684     // value so we don't have to do memory allocations after that.
685     if (!path.empty())
686         return path;
687
688     path = ZC_GetBaseParamsDir();
689
690     return path;
691 }
692
693 // Return the user specified export directory.  Create directory if it doesn't exist.
694 // If user did not set option, return an empty path.
695 // If there is a filesystem problem, throw an exception.
696 const boost::filesystem::path GetExportDir()
697 {
698     namespace fs = boost::filesystem;
699     fs::path path;
700     if (mapArgs.count("-exportdir")) {
701         path = fs::system_complete(mapArgs["-exportdir"]);
702         if (fs::exists(path) && !fs::is_directory(path)) {
703             throw std::runtime_error(strprintf("The -exportdir '%s' already exists and is not a directory", path.string()));
704         }
705         if (!fs::exists(path) && !fs::create_directories(path)) {
706             throw std::runtime_error(strprintf("Failed to create directory at -exportdir '%s'", path.string()));
707         }
708     }
709     return path;
710 }
711
712
713 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
714 {
715     namespace fs = boost::filesystem;
716
717     LOCK(csPathCached);
718
719     fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
720
721     // This can be called during exceptions by LogPrintf(), so we cache the
722     // value so we don't have to do memory allocations after that.
723     if (!path.empty())
724         return path;
725
726     if (mapArgs.count("-datadir")) {
727         path = fs::system_complete(mapArgs["-datadir"]);
728         if (!fs::is_directory(path)) {
729             path = "";
730             return path;
731         }
732     } else {
733         path = GetDefaultDataDir();
734     }
735     if (fNetSpecific)
736         path /= BaseParams().DataDir();
737
738     fs::create_directories(path);
739     //std::string assetpath = path + "/assets";
740     //boost::filesystem::create_directory(assetpath);
741     return path;
742 }
743
744 const boost::filesystem::path GetDataDir(std::string chainName)
745 {
746     namespace fs = boost::filesystem;
747
748     fs::path path = GetDefaultDataDir(chainName);
749     return path;
750 }
751
752 void ClearDatadirCache()
753 {
754     pathCached = boost::filesystem::path();
755     pathCachedNetSpecific = boost::filesystem::path();
756 }
757
758 boost::filesystem::path GetConfigFile()
759 {
760     char confname[512];
761     if ( ASSETCHAINS_SYMBOL[0] != 0 )
762         sprintf(confname,"%s.conf",ASSETCHAINS_SYMBOL);
763     else
764     {
765 #ifdef __APPLE__
766         strcpy(confname,"Komodo.conf");
767 #else
768         strcpy(confname,"komodo.conf");
769 #endif
770     }
771     boost::filesystem::path pathConfigFile(GetArg("-conf",confname));
772     if (!pathConfigFile.is_complete())
773         pathConfigFile = GetDataDir(false) / pathConfigFile;
774
775     return pathConfigFile;
776 }
777
778 boost::filesystem::path GetConfigFile(std::string chainName)
779 {
780     char confname[512];
781     if (chainName.size() >= KOMODO_ASSETCHAIN_MAXLEN)
782         chainName.resize(KOMODO_ASSETCHAIN_MAXLEN - 1);
783     sprintf(confname, "%s.conf", chainName.c_str());
784     boost::filesystem::path pathConfigFile(GetArg("-conf",confname));
785     if (!pathConfigFile.is_complete())
786         pathConfigFile = GetDataDir(chainName) / pathConfigFile;
787
788     return pathConfigFile;
789 }
790
791 void ReadConfigFile(map<string, string>& mapSettingsRet,
792                     map<string, vector<string> >& mapMultiSettingsRet)
793 {
794     boost::filesystem::ifstream streamConfig(GetConfigFile());
795     if (!streamConfig.good())
796         throw missing_zcash_conf();
797
798     set<string> setOptions;
799     setOptions.insert("*");
800
801     for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
802     {
803         // Don't overwrite existing settings so command line settings override komodo.conf
804         string strKey = string("-") + it->string_key;
805         if (mapSettingsRet.count(strKey) == 0)
806         {
807             mapSettingsRet[strKey] = it->value[0];
808             // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
809             InterpretNegativeSetting(strKey, mapSettingsRet);
810         }
811         mapMultiSettingsRet[strKey].push_back(it->value[0]);
812     }
813     // If datadir is changed in .conf file:
814     ClearDatadirCache();
815     extern uint16_t BITCOIND_RPCPORT;
816     BITCOIND_RPCPORT = GetArg("-rpcport",BaseParams().RPCPort());
817 }
818
819 // for reading an external config file. does not clear the data dir cache
820 bool ReadConfigFile(std::string chainName,
821                     map<string, string>& mapSettingsRet,
822                     map<string, vector<string> >& mapMultiSettingsRet)
823 {
824     boost::filesystem::ifstream streamConfig(GetConfigFile(chainName));
825     if (!streamConfig.good())
826         return false;
827
828     set<string> setOptions;
829     setOptions.insert("*");
830
831     for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
832     {
833         // Don't overwrite existing settings so command line settings override komodo.conf
834         string strKey = string("-") + it->string_key;
835         if (mapSettingsRet.count(strKey) == 0)
836         {
837             mapSettingsRet[strKey] = it->value[0];
838             // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
839             InterpretNegativeSetting(strKey, mapSettingsRet);
840         }
841         mapMultiSettingsRet[strKey].push_back(it->value[0]);
842     }
843     return true;
844 }
845
846 #ifndef _WIN32
847 boost::filesystem::path GetPidFile()
848 {
849     boost::filesystem::path pathPidFile(GetArg("-pid", "komodod.pid"));
850     if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
851     return pathPidFile;
852 }
853
854 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
855 {
856     FILE* file = fopen(path.string().c_str(), "w");
857     if (file)
858     {
859         fprintf(file, "%d\n", pid);
860         fclose(file);
861     }
862 }
863 #endif
864
865 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
866 {
867 #ifdef _WIN32
868     return MoveFileExA(src.string().c_str(), dest.string().c_str(),
869                        MOVEFILE_REPLACE_EXISTING) != 0;
870 #else
871     int rc = std::rename(src.string().c_str(), dest.string().c_str());
872     return (rc == 0);
873 #endif /* _WIN32 */
874 }
875
876 /**
877  * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
878  * Specifically handles case where path p exists, but it wasn't possible for the user to
879  * write to the parent directory.
880  */
881 bool TryCreateDirectory(const boost::filesystem::path& p)
882 {
883     try
884     {
885         return boost::filesystem::create_directory(p);
886     } catch (const boost::filesystem::filesystem_error&) {
887         if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
888             throw;
889     }
890
891     // create_directory didn't create the directory, it had to have existed already
892     return false;
893 }
894
895 void FileCommit(FILE *fileout)
896 {
897     fflush(fileout); // harmless if redundantly called
898 #ifdef _WIN32
899     HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
900     FlushFileBuffers(hFile);
901 #else
902     #if defined(__linux__) || defined(__NetBSD__)
903     fdatasync(fileno(fileout));
904     #elif defined(__APPLE__) && defined(F_FULLFSYNC)
905     fcntl(fileno(fileout), F_FULLFSYNC, 0);
906     #else
907     fsync(fileno(fileout));
908     #endif
909 #endif
910 }
911
912 bool TruncateFile(FILE *file, unsigned int length) {
913 #if defined(WIN32)
914     return _chsize(_fileno(file), length) == 0;
915 #else
916     return ftruncate(fileno(file), length) == 0;
917 #endif
918 }
919
920 /**
921  * this function tries to raise the file descriptor limit to the requested number.
922  * It returns the actual file descriptor limit (which may be more or less than nMinFD)
923  */
924 int RaiseFileDescriptorLimit(int nMinFD) {
925 #if defined(WIN32)
926     return 2048;
927 #else
928     struct rlimit limitFD;
929     if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
930         if (limitFD.rlim_cur < (rlim_t)nMinFD) {
931             limitFD.rlim_cur = nMinFD;
932             if (limitFD.rlim_cur > limitFD.rlim_max)
933                 limitFD.rlim_cur = limitFD.rlim_max;
934             setrlimit(RLIMIT_NOFILE, &limitFD);
935             getrlimit(RLIMIT_NOFILE, &limitFD);
936         }
937         return limitFD.rlim_cur;
938     }
939     return nMinFD; // getrlimit failed, assume it's fine
940 #endif
941 }
942
943 /**
944  * this function tries to make a particular range of a file allocated (corresponding to disk space)
945  * it is advisory, and the range specified in the arguments will never contain live data
946  */
947 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
948 #if defined(WIN32)
949     // Windows-specific version
950     HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
951     LARGE_INTEGER nFileSize;
952     int64_t nEndPos = (int64_t)offset + length;
953     nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
954     nFileSize.u.HighPart = nEndPos >> 32;
955     SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
956     SetEndOfFile(hFile);
957 #elif defined(MAC_OSX)
958     // OSX specific version
959     fstore_t fst;
960     fst.fst_flags = F_ALLOCATECONTIG;
961     fst.fst_posmode = F_PEOFPOSMODE;
962     fst.fst_offset = 0;
963     fst.fst_length = (off_t)offset + length;
964     fst.fst_bytesalloc = 0;
965     if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
966         fst.fst_flags = F_ALLOCATEALL;
967         fcntl(fileno(file), F_PREALLOCATE, &fst);
968     }
969     ftruncate(fileno(file), fst.fst_length);
970 #elif defined(__linux__)
971     // Version using posix_fallocate
972     off_t nEndPos = (off_t)offset + length;
973     posix_fallocate(fileno(file), 0, nEndPos);
974 #else
975     // Fallback version
976     // TODO: just write one byte per block
977     static const char buf[65536] = {};
978     fseek(file, offset, SEEK_SET);
979     while (length > 0) {
980         unsigned int now = 65536;
981         if (length < now)
982             now = length;
983         fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
984         length -= now;
985     }
986 #endif
987 }
988
989 void ShrinkDebugFile()
990 {
991     // Scroll debug.log if it's getting too big
992     boost::filesystem::path pathLog = GetDataDir() / "debug.log";
993     FILE* file = fopen(pathLog.string().c_str(), "r");
994     if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
995     {
996         // Restart the file with some of the end
997         std::vector <char> vch(200000,0);
998         fseek(file, -((long)vch.size()), SEEK_END);
999         int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
1000         fclose(file);
1001
1002         file = fopen(pathLog.string().c_str(), "w");
1003         if (file)
1004         {
1005             fwrite(begin_ptr(vch), 1, nBytes, file);
1006             fclose(file);
1007         }
1008     }
1009     else if (file != NULL)
1010         fclose(file);
1011 }
1012
1013 #ifdef _WIN32
1014 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
1015 {
1016     namespace fs = boost::filesystem;
1017
1018     char pszPath[MAX_PATH] = "";
1019
1020     if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1021     {
1022         return fs::path(pszPath);
1023     }
1024
1025     LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1026     return fs::path("");
1027 }
1028 #endif
1029
1030 boost::filesystem::path GetTempPath() {
1031 #if BOOST_FILESYSTEM_VERSION == 3
1032     return boost::filesystem::temp_directory_path();
1033 #else
1034     // TODO: remove when we don't support filesystem v2 anymore
1035     boost::filesystem::path path;
1036 #ifdef _WIN32
1037     char pszPath[MAX_PATH] = "";
1038
1039     if (GetTempPathA(MAX_PATH, pszPath))
1040         path = boost::filesystem::path(pszPath);
1041 #else
1042     path = boost::filesystem::path("/tmp");
1043 #endif
1044     if (path.empty() || !boost::filesystem::is_directory(path)) {
1045         LogPrintf("GetTempPath(): failed to find temp path\n");
1046         return boost::filesystem::path("");
1047     }
1048     return path;
1049 #endif
1050 }
1051
1052 void runCommand(const std::string& strCommand)
1053 {
1054     int nErr = ::system(strCommand.c_str());
1055     if (nErr)
1056         LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
1057 }
1058
1059 void RenameThread(const char* name)
1060 {
1061 #if defined(PR_SET_NAME)
1062     // Only the first 15 characters are used (16 - NUL terminator)
1063     ::prctl(PR_SET_NAME, name, 0, 0, 0);
1064 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
1065     pthread_set_name_np(pthread_self(), name);
1066
1067 #elif defined(MAC_OSX)
1068     pthread_setname_np(name);
1069 #else
1070     // Prevent warnings for unused parameters...
1071     (void)name;
1072 #endif
1073 }
1074
1075 void SetupEnvironment()
1076 {
1077     // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
1078     // may be invalid, in which case the "C" locale is used as fallback.
1079 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
1080     try {
1081         std::locale(""); // Raises a runtime error if current locale is invalid
1082     } catch (const std::runtime_error&) {
1083         setenv("LC_ALL", "C", 1);
1084     }
1085 #endif
1086     // The path locale is lazy initialized and to avoid deinitialization errors
1087     // in multithreading environments, it is set explicitly by the main thread.
1088     // A dummy locale is used to extract the internal default locale, used by
1089     // boost::filesystem::path, which is then used to explicitly imbue the path.
1090     std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
1091     boost::filesystem::path::imbue(loc);
1092 }
1093
1094 bool SetupNetworking()
1095 {
1096 #ifdef _WIN32
1097     // Initialize Windows Sockets
1098     WSADATA wsadata;
1099     int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
1100     if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
1101         return false;
1102 #endif
1103     return true;
1104 }
1105
1106 void SetThreadPriority(int nPriority)
1107 {
1108 #ifdef _WIN32
1109     SetThreadPriority(GetCurrentThread(), nPriority);
1110 #else // _WIN32
1111 #ifdef PRIO_THREAD
1112     setpriority(PRIO_THREAD, 0, nPriority);
1113 #else // PRIO_THREAD
1114     setpriority(PRIO_PROCESS, 0, nPriority);
1115 #endif // PRIO_THREAD
1116 #endif // _WIN32
1117 }
1118
1119 std::string PrivacyInfo()
1120 {
1121     return "\n" +
1122            FormatParagraph(strprintf(_("In order to ensure you are adequately protecting your privacy when using Zcash, please see <%s>."),
1123                                      "https://z.cash/support/security/")) + "\n";
1124 }
1125
1126 std::string LicenseInfo()
1127 {
1128     return "\n" +
1129            FormatParagraph(strprintf(_("Copyright (C) 2009-%i The Bitcoin Core Developers"), COPYRIGHT_YEAR)) + "\n" +
1130            FormatParagraph(strprintf(_("Copyright (C) 2015-%i The Zcash Developers"), COPYRIGHT_YEAR)) + "\n" +
1131            FormatParagraph(strprintf(_("Copyright (C) 2015-%i jl777 and SuperNET developers"), COPYRIGHT_YEAR)) + "\n" +
1132            FormatParagraph(strprintf(_("Copyright (C) 2018-%i The Verus developers"), COPYRIGHT_YEAR)) + "\n" +
1133            "\n" +
1134            FormatParagraph(_("This is experimental software.")) + "\n" +
1135            "\n" +
1136            FormatParagraph(_("Distributed under the MIT software license, see the accompanying file COPYING or <http://www.opensource.org/licenses/mit-license.php>.")) + "\n" +
1137            "\n" +
1138            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.")) +
1139            "\n";
1140 }
1141
1142 int GetNumCores()
1143 {
1144     return boost::thread::physical_concurrency();
1145 }
1146
This page took 0.100286 seconds and 4 git commands to generate.