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.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
12 #include "chainparamsbase.h"
14 #include "serialize.h"
16 #include "utilstrencodings.h"
18 #include "komodo_defs.h"
25 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
27 #include <pthread_np.h>
31 // for posix_fallocate
34 #ifdef _POSIX_C_SOURCE
35 #undef _POSIX_C_SOURCE
38 #define _POSIX_C_SOURCE 200112L
44 #include <sys/resource.h>
50 #pragma warning(disable:4786)
51 #pragma warning(disable:4804)
52 #pragma warning(disable:4805)
53 #pragma warning(disable:4717)
59 #define _WIN32_WINNT 0x0501
64 #define _WIN32_IE 0x0501
66 #define WIN32_LEAN_AND_MEAN 1
71 #include <io.h> /* for _commit */
75 #ifdef HAVE_SYS_PRCTL_H
76 #include <sys/prctl.h>
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>
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
97 namespace program_options {
98 std::string to_internal(const std::string&);
105 map<string, string> mapArgs;
106 map<string, vector<string> > mapMultiArgs;
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;
119 extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
121 bool _IsVerusActive()
123 return (strcmp(ASSETCHAINS_SYMBOL, "VRSC") == 0 || strcmp(ASSETCHAINS_SYMBOL, "VRSCTEST") == 0);
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
130 if (mode & CRYPTO_LOCK) {
131 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
133 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
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);
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.
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);
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
178 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
181 * We use boost::call_once() to make sure mutexDebugLog and
182 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
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.
189 static FILE* fileout = NULL;
190 static boost::mutex* mutexDebugLog = NULL;
191 static list<string> *vMsgsBeforeOpenLog;
193 [[noreturn]] void new_handler_terminate()
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");
203 // The log was successful, terminate now.
207 static int FileWriteStr(const std::string &str, FILE *fp)
209 return fwrite(str.data(), 1, str.size(), fp);
212 static void DebugPrintInit()
214 assert(mutexDebugLog == NULL);
215 mutexDebugLog = new boost::mutex();
216 vMsgsBeforeOpenLog = new list<string>;
221 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
222 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
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
230 // dump buffered messages from before we opened the log
231 while (!vMsgsBeforeOpenLog->empty()) {
232 FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
233 vMsgsBeforeOpenLog->pop_front();
236 delete vMsgsBeforeOpenLog;
237 vMsgsBeforeOpenLog = NULL;
240 bool LogAcceptCategory(const char* category)
242 if (category != NULL)
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)
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.
258 const set<string>& setCategories = *ptrCategory.get();
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)
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.
274 static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
281 if (*fStartedNewLine)
282 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()) + ' ' + str;
286 if (!str.empty() && str[str.size()-1] == '\n')
287 *fStartedNewLine = true;
289 *fStartedNewLine = false;
294 int LogPrintStr(const std::string &str)
296 int ret = 0; // Returns total number of characters written
297 static bool fStartedNewLine = true;
301 ret = fwrite(str.data(), 1, str.size(), stdout);
304 else if (fPrintToDebugLog)
306 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
307 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
309 string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
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);
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
327 ret = FileWriteStr(strTimestamped, fileout);
333 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
335 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
336 if (name.find("-no") == 0)
338 std::string positive("-");
339 positive.append(name.begin()+3, name.end());
340 if (mapSettingsRet.count(positive) == 0)
342 bool value = !GetBoolArg(name, false);
343 mapSettingsRet[positive] = (value ? "1" : "0");
348 void ParseParameters(int argc, const char* const argv[])
351 mapMultiArgs.clear();
353 for (int i = 1; i < argc; i++)
355 std::string str(argv[i]);
356 std::string strValue;
357 size_t is_index = str.find('=');
358 if (is_index != std::string::npos)
360 strValue = str.substr(is_index+1);
361 str = str.substr(0, is_index);
364 boost::to_lower(str);
365 if (boost::algorithm::starts_with(str, "/"))
366 str = "-" + str.substr(1);
372 // Interpret --foo as -foo.
373 // If both --foo and -foo are set, the last takes effect.
374 if (str.length() > 1 && str[1] == '-')
377 mapArgs[str] = strValue;
378 mapMultiArgs[str].push_back(strValue);
382 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
384 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
385 InterpretNegativeSetting(entry.first, mapArgs);
389 void Split(const std::string& strVal, uint64_t *outVals, const uint64_t nDefault,int maxElements)
391 stringstream ss(strVal);
392 vector<uint64_t> vec;
394 uint64_t i, nLast, numVals = 0;
396 while ( ss.peek() == ' ' )
399 while ( numVals < maxElements && ss >> i )
401 outVals[numVals] = i;
404 while ( ss.peek() == ' ' )
406 if ( ss.peek() == ',' )
408 while ( ss.peek() == ' ' )
413 nLast = outVals[numVals - 1];
417 for ( i = numVals; i < maxElements; i++ )
423 std::string GetArg(const std::string& strArg, const std::string& strDefault)
425 if (mapArgs.count(strArg))
426 return mapArgs[strArg];
430 int64_t GetArg(const std::string& strArg, int64_t nDefault)
432 if (mapArgs.count(strArg))
433 return atoi64(mapArgs[strArg]);
437 bool GetBoolArg(const std::string& strArg, bool fDefault)
439 if (mapArgs.count(strArg))
441 if (mapArgs[strArg].empty())
443 return (atoi(mapArgs[strArg]) != 0);
448 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
450 if (mapArgs.count(strArg))
452 mapArgs[strArg] = strValue;
456 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
459 return SoftSetArg(strArg, std::string("1"));
461 return SoftSetArg(strArg, std::string("0"));
464 static const int screenWidth = 79;
465 static const int optIndent = 2;
466 static const int msgIndent = 7;
468 std::string HelpMessageGroup(const std::string &message) {
469 return std::string(message) + std::string("\n\n");
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) +
479 static std::string FormatException(const std::exception* pex, const char* pszThread)
482 char pszModule[MAX_PATH] = "";
483 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
485 const char* pszModule = "Komodo";
489 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
492 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
495 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
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;
503 //int64_t MAX_MONEY = 200000000 * 100000000LL;
505 boost::filesystem::path GetDefaultDataDir()
507 namespace fs = boost::filesystem;
508 char symbol[KOMODO_ASSETCHAIN_MAXLEN];
509 if ( ASSETCHAINS_SYMBOL[0] != 0 )
510 strcpy(symbol,ASSETCHAINS_SYMBOL);
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
518 if ( symbol[0] == 0 )
519 return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo";
522 if (_IsVerusActive())
524 return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo" / symbol;
528 return GetSpecialFolderPath(CSIDL_APPDATA) / (PBAAS_TESTMODE ? "VerusTest" : "Verus") / "PBAAS" / symbol;
533 char* pszHome = getenv("HOME");
534 if (pszHome == NULL || strlen(pszHome) == 0)
535 pathRet = fs::path("/");
537 pathRet = fs::path(pszHome);
540 pathRet /= "Library/Application Support";
541 TryCreateDirectory(pathRet);
542 if ( symbol[0] == 0 )
543 return pathRet / "Komodo";
546 if (_IsVerusActive())
549 TryCreateDirectory(pathRet);
553 pathRet /= PBAAS_TESTMODE ? "VerusTest" : "Verus";
554 TryCreateDirectory(pathRet);
556 TryCreateDirectory(pathRet);
558 return pathRet / symbol;
562 if ( symbol[0] == 0 )
563 return pathRet / ".komodo";
566 if (_IsVerusActive())
568 return pathRet / ".komodo" / symbol;
572 return pathRet / (PBAAS_TESTMODE ? ".verustest" : ".verus") / "PBAAS" / symbol;
579 boost::filesystem::path GetDefaultDataDir(std::string chainName)
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());
586 namespace fs = boost::filesystem;
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
594 if (chainName == "VRSC" || chainName == "VRSCTEST")
596 return GetSpecialFolderPath(CSIDL_APPDATA) / "Komodo" / symbol;
600 return GetSpecialFolderPath(CSIDL_APPDATA) / (PBAAS_TESTMODE ? "VerusTest" : "Verus") / "PBAAS" / symbol;
604 char* pszHome = getenv("HOME");
605 if (pszHome == NULL || strlen(pszHome) == 0)
606 pathRet = fs::path("/");
608 pathRet = fs::path(pszHome);
611 pathRet /= "Library/Application Support";
612 TryCreateDirectory(pathRet);
613 if (chainName == "VRSC" || chainName == "VRSCTEST")
616 TryCreateDirectory(pathRet);
620 pathRet /= PBAAS_TESTMODE ? "VerusTest" : "Verus";
621 TryCreateDirectory(pathRet);
623 TryCreateDirectory(pathRet);
625 return pathRet / symbol;
628 if (chainName == "VRSC" || chainName == "VRSCTEST")
630 return pathRet / ".komodo" / symbol;
634 return pathRet / (PBAAS_TESTMODE ? ".verustest" : ".verus") / "PBAAS" / symbol;
640 static boost::filesystem::path pathCached;
641 static boost::filesystem::path pathCachedNetSpecific;
642 static boost::filesystem::path zc_paramsPathCached;
643 static CCriticalSection csPathCached;
645 static boost::filesystem::path ZC_GetBaseParamsDir()
647 // Copied from GetDefaultDataDir and adapter for zcash params.
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
656 return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
658 char* pszHome = getenv("HOME");
659 if (pszHome == NULL || strlen(pszHome) == 0)
660 pathRet = fs::path("/");
662 pathRet = fs::path(pszHome);
665 pathRet /= "Library/Application Support";
666 TryCreateDirectory(pathRet);
667 return pathRet / "ZcashParams";
670 return pathRet / ".zcash-params";
675 const boost::filesystem::path &ZC_GetParamsDir()
677 namespace fs = boost::filesystem;
679 LOCK(csPathCached); // Reuse the same lock as upstream.
681 fs::path &path = zc_paramsPathCached;
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.
688 path = ZC_GetBaseParamsDir();
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()
698 namespace fs = boost::filesystem;
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()));
705 if (!fs::exists(path) && !fs::create_directories(path)) {
706 throw std::runtime_error(strprintf("Failed to create directory at -exportdir '%s'", path.string()));
713 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
715 namespace fs = boost::filesystem;
719 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
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.
726 if (mapArgs.count("-datadir")) {
727 path = fs::system_complete(mapArgs["-datadir"]);
728 if (!fs::is_directory(path)) {
733 path = GetDefaultDataDir();
736 path /= BaseParams().DataDir();
738 fs::create_directories(path);
739 //std::string assetpath = path + "/assets";
740 //boost::filesystem::create_directory(assetpath);
744 const boost::filesystem::path GetDataDir(std::string chainName)
746 namespace fs = boost::filesystem;
748 fs::path path = GetDefaultDataDir(chainName);
752 void ClearDatadirCache()
754 pathCached = boost::filesystem::path();
755 pathCachedNetSpecific = boost::filesystem::path();
758 boost::filesystem::path GetConfigFile()
761 if ( ASSETCHAINS_SYMBOL[0] != 0 )
762 sprintf(confname,"%s.conf",ASSETCHAINS_SYMBOL);
766 strcpy(confname,"Komodo.conf");
768 strcpy(confname,"komodo.conf");
771 boost::filesystem::path pathConfigFile(GetArg("-conf",confname));
772 if (!pathConfigFile.is_complete())
773 pathConfigFile = GetDataDir(false) / pathConfigFile;
775 return pathConfigFile;
778 boost::filesystem::path GetConfigFile(std::string chainName)
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;
788 return pathConfigFile;
791 void ReadConfigFile(map<string, string>& mapSettingsRet,
792 map<string, vector<string> >& mapMultiSettingsRet)
794 boost::filesystem::ifstream streamConfig(GetConfigFile());
795 if (!streamConfig.good())
796 throw missing_zcash_conf();
798 set<string> setOptions;
799 setOptions.insert("*");
801 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
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)
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);
811 mapMultiSettingsRet[strKey].push_back(it->value[0]);
813 // If datadir is changed in .conf file:
815 extern uint16_t BITCOIND_RPCPORT;
816 BITCOIND_RPCPORT = GetArg("-rpcport",BaseParams().RPCPort());
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)
824 boost::filesystem::ifstream streamConfig(GetConfigFile(chainName));
825 if (!streamConfig.good())
828 set<string> setOptions;
829 setOptions.insert("*");
831 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
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)
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);
841 mapMultiSettingsRet[strKey].push_back(it->value[0]);
847 boost::filesystem::path GetPidFile()
849 boost::filesystem::path pathPidFile(GetArg("-pid", "komodod.pid"));
850 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
854 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
856 FILE* file = fopen(path.string().c_str(), "w");
859 fprintf(file, "%d\n", pid);
865 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
868 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
869 MOVEFILE_REPLACE_EXISTING) != 0;
871 int rc = std::rename(src.string().c_str(), dest.string().c_str());
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.
881 bool TryCreateDirectory(const boost::filesystem::path& p)
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))
891 // create_directory didn't create the directory, it had to have existed already
895 void FileCommit(FILE *fileout)
897 fflush(fileout); // harmless if redundantly called
899 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
900 FlushFileBuffers(hFile);
902 #if defined(__linux__) || defined(__NetBSD__)
903 fdatasync(fileno(fileout));
904 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
905 fcntl(fileno(fileout), F_FULLFSYNC, 0);
907 fsync(fileno(fileout));
912 bool TruncateFile(FILE *file, unsigned int length) {
914 return _chsize(_fileno(file), length) == 0;
916 return ftruncate(fileno(file), length) == 0;
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)
924 int RaiseFileDescriptorLimit(int nMinFD) {
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);
937 return limitFD.rlim_cur;
939 return nMinFD; // getrlimit failed, assume it's fine
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
947 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
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);
957 #elif defined(MAC_OSX)
958 // OSX specific version
960 fst.fst_flags = F_ALLOCATECONTIG;
961 fst.fst_posmode = F_PEOFPOSMODE;
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);
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);
976 // TODO: just write one byte per block
977 static const char buf[65536] = {};
978 fseek(file, offset, SEEK_SET);
980 unsigned int now = 65536;
983 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
989 void ShrinkDebugFile()
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)
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);
1002 file = fopen(pathLog.string().c_str(), "w");
1005 fwrite(begin_ptr(vch), 1, nBytes, file);
1009 else if (file != NULL)
1014 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
1016 namespace fs = boost::filesystem;
1018 char pszPath[MAX_PATH] = "";
1020 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1022 return fs::path(pszPath);
1025 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1026 return fs::path("");
1030 boost::filesystem::path GetTempPath() {
1031 #if BOOST_FILESYSTEM_VERSION == 3
1032 return boost::filesystem::temp_directory_path();
1034 // TODO: remove when we don't support filesystem v2 anymore
1035 boost::filesystem::path path;
1037 char pszPath[MAX_PATH] = "";
1039 if (GetTempPathA(MAX_PATH, pszPath))
1040 path = boost::filesystem::path(pszPath);
1042 path = boost::filesystem::path("/tmp");
1044 if (path.empty() || !boost::filesystem::is_directory(path)) {
1045 LogPrintf("GetTempPath(): failed to find temp path\n");
1046 return boost::filesystem::path("");
1052 void runCommand(const std::string& strCommand)
1054 int nErr = ::system(strCommand.c_str());
1056 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
1059 void RenameThread(const char* name)
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);
1067 #elif defined(MAC_OSX)
1068 pthread_setname_np(name);
1070 // Prevent warnings for unused parameters...
1075 void SetupEnvironment()
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__)
1081 std::locale(""); // Raises a runtime error if current locale is invalid
1082 } catch (const std::runtime_error&) {
1083 setenv("LC_ALL", "C", 1);
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);
1094 bool SetupNetworking()
1097 // Initialize Windows Sockets
1099 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
1100 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
1106 void SetThreadPriority(int nPriority)
1109 SetThreadPriority(GetCurrentThread(), nPriority);
1112 setpriority(PRIO_THREAD, 0, nPriority);
1113 #else // PRIO_THREAD
1114 setpriority(PRIO_PROCESS, 0, nPriority);
1115 #endif // PRIO_THREAD
1119 std::string PrivacyInfo()
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";
1126 std::string LicenseInfo()
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" +
1134 FormatParagraph(_("This is experimental software.")) + "\n" +
1136 FormatParagraph(_("Distributed under the MIT software license, see the accompanying file COPYING or <http://www.opensource.org/licenses/mit-license.php>.")) + "\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.")) +
1144 return boost::thread::physical_concurrency();