]> Git Repo - VerusCoin.git/blame - src/util.cpp
Rename bitcoin.conf and bitcoind.pid to zcash.conf and zcashd.pid in qa/ and src/
[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
BD
84#include <openssl/crypto.h>
85#include <openssl/rand.h>
25314386 86#include <openssl/conf.h>
51ed9ec9
BD
87
88// Work around clang compilation problem in Boost 1.46:
89// /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
90// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
91// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
92namespace boost {
e10dcf27 93
51ed9ec9
BD
94 namespace program_options {
95 std::string to_internal(const std::string&);
96 }
51ed9ec9 97
e10dcf27 98} // namespace boost
51ed9ec9 99
2097c09a
WL
100using namespace std;
101
102map<string, string> mapArgs;
103map<string, vector<string> > mapMultiArgs;
104bool fDebug = false;
105bool fPrintToConsole = false;
9e9056cd 106bool fPrintToDebugLog = true;
2097c09a
WL
107bool fDaemon = false;
108bool fServer = false;
2097c09a 109string strMiscWarning;
2097c09a 110bool fLogTimestamps = false;
2e36866f 111bool fLogIPs = false;
ee337423 112volatile 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();
144
6853e627 145#ifdef WIN32
cf04d836 146 // Seed OpenSSL PRNG with current contents of the screen
2097c09a
WL
147 RAND_screen();
148#endif
149
cf04d836 150 // Seed OpenSSL PRNG with performance counter
2097c09a
WL
151 RandAddSeed();
152 }
153 ~CInit()
154 {
cf04d836
PK
155 // Securely erase the memory used by the PRNG
156 RAND_cleanup();
a7f82808 157 // Shutdown OpenSSL library multithreading support
2097c09a
WL
158 CRYPTO_set_locking_callback(NULL);
159 for (int i = 0; i < CRYPTO_num_locks(); i++)
160 delete ppmutexOpenSSL[i];
161 OPENSSL_free(ppmutexOpenSSL);
162 }
163}
164instance_of_cinit;
165
c63a73d1
MF
166/**
167 * LogPrintf() has been broken a couple of times now
168 * by well-meaning people adding mutexes in the most straightforward way.
169 * It breaks because it may be called by global destructors during shutdown.
170 * Since the order of destruction of static/global objects is undefined,
171 * defining a mutex as a global object doesn't work (the mutex gets
172 * destroyed, and then some later destructor calls OutputDebugStringF,
173 * maybe indirectly, and you get a core dump at shutdown trying to lock
174 * the mutex).
175 */
2097c09a 176
ee337423 177static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
c63a73d1
MF
178/**
179 * We use boost::call_once() to make sure these are initialized
180 * in a thread-safe manner the first time called:
181 */
ee337423
GA
182static FILE* fileout = NULL;
183static boost::mutex* mutexDebugLog = NULL;
184
185static void DebugPrintInit()
2097c09a 186{
ee337423
GA
187 assert(fileout == NULL);
188 assert(mutexDebugLog == NULL);
189
190 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
191 fileout = fopen(pathDebug.string().c_str(), "a");
192 if (fileout) setbuf(fileout, NULL); // unbuffered
193
194 mutexDebugLog = new boost::mutex();
195}
196
b77dfdc9 197bool LogAcceptCategory(const char* category)
ee337423 198{
e51321fb
GA
199 if (category != NULL)
200 {
3b570559 201 if (!fDebug)
b77dfdc9 202 return false;
3b570559 203
0b238b27
GA
204 // Give each thread quick access to -debug settings.
205 // This helps prevent issues debugging global destructors,
206 // where mapMultiArgs might be deleted before another
207 // global destructor calls LogPrint()
208 static boost::thread_specific_ptr<set<string> > ptrCategory;
209 if (ptrCategory.get() == NULL)
3b570559 210 {
0b238b27
GA
211 const vector<string>& categories = mapMultiArgs["-debug"];
212 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
213 // thread_specific_ptr automatically deletes the set when the thread ends.
3b570559 214 }
0b238b27
GA
215 const set<string>& setCategories = *ptrCategory.get();
216
217 // if not debugging everything and not debugging specific category, LogPrint does nothing.
218 if (setCategories.count(string("")) == 0 &&
219 setCategories.count(string(category)) == 0)
b77dfdc9 220 return false;
e51321fb 221 }
b77dfdc9
WL
222 return true;
223}
e51321fb 224
b77dfdc9
WL
225int LogPrintStr(const std::string &str)
226{
ee337423 227 int ret = 0; // Returns total number of characters written
2097c09a
WL
228 if (fPrintToConsole)
229 {
230 // print to console
b77dfdc9 231 ret = fwrite(str.data(), 1, str.size(), stdout);
e982b574 232 fflush(stdout);
2097c09a 233 }
96ff9d64 234 else if (fPrintToDebugLog && AreBaseParamsConfigured())
2097c09a 235 {
ee337423
GA
236 static bool fStartedNewLine = true;
237 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
2097c09a 238
ee337423
GA
239 if (fileout == NULL)
240 return ret;
241
242 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
243
244 // reopen the log file, if requested
245 if (fReopenDebugLog) {
246 fReopenDebugLog = false;
ee12c3d6 247 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
ee337423
GA
248 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
249 setbuf(fileout, NULL); // unbuffered
2097c09a 250 }
2097c09a 251
ee337423
GA
252 // Debug print useful for profiling
253 if (fLogTimestamps && fStartedNewLine)
254 ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
b77dfdc9 255 if (!str.empty() && str[str.size()-1] == '\n')
ee337423
GA
256 fStartedNewLine = true;
257 else
258 fStartedNewLine = false;
2097c09a 259
b77dfdc9 260 ret = fwrite(str.data(), 1, str.size(), fileout);
2097c09a
WL
261 }
262
2097c09a
WL
263 return ret;
264}
265
d64e124c
CM
266static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
267{
268 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
269 if (name.find("-no") == 0)
270 {
271 std::string positive("-");
272 positive.append(name.begin()+3, name.end());
273 if (mapSettingsRet.count(positive) == 0)
274 {
3260b4c0 275 bool value = !GetBoolArg(name, false);
d64e124c
CM
276 mapSettingsRet[positive] = (value ? "1" : "0");
277 }
278 }
279}
280
3e468840 281void ParseParameters(int argc, const char* const argv[])
2097c09a
WL
282{
283 mapArgs.clear();
284 mapMultiArgs.clear();
71aaff39 285
2097c09a
WL
286 for (int i = 1; i < argc; i++)
287 {
6032e4f4
WL
288 std::string str(argv[i]);
289 std::string strValue;
290 size_t is_index = str.find('=');
291 if (is_index != std::string::npos)
2097c09a 292 {
6032e4f4
WL
293 strValue = str.substr(is_index+1);
294 str = str.substr(0, is_index);
2097c09a 295 }
6032e4f4
WL
296#ifdef WIN32
297 boost::to_lower(str);
298 if (boost::algorithm::starts_with(str, "/"))
299 str = "-" + str.substr(1);
300#endif
71aaff39 301
6032e4f4 302 if (str[0] != '-')
2097c09a 303 break;
3ad9f8a7 304
71aaff39
KZ
305 // Interpret --foo as -foo.
306 // If both --foo and -foo are set, the last takes effect.
307 if (str.length() > 1 && str[1] == '-')
308 str = str.substr(1);
309
6032e4f4
WL
310 mapArgs[str] = strValue;
311 mapMultiArgs[str].push_back(strValue);
2097c09a 312 }
3ad9f8a7
GA
313
314 // New 0.6 features:
315 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
316 {
d64e124c 317 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
71aaff39 318 InterpretNegativeSetting(entry.first, mapArgs);
3ad9f8a7 319 }
2097c09a
WL
320}
321
3ae07355
GA
322std::string GetArg(const std::string& strArg, const std::string& strDefault)
323{
324 if (mapArgs.count(strArg))
325 return mapArgs[strArg];
326 return strDefault;
327}
328
51ed9ec9 329int64_t GetArg(const std::string& strArg, int64_t nDefault)
3ae07355
GA
330{
331 if (mapArgs.count(strArg))
332 return atoi64(mapArgs[strArg]);
333 return nDefault;
334}
335
336bool GetBoolArg(const std::string& strArg, bool fDefault)
337{
338 if (mapArgs.count(strArg))
339 {
340 if (mapArgs[strArg].empty())
341 return true;
342 return (atoi(mapArgs[strArg]) != 0);
343 }
344 return fDefault;
345}
346
0fcf91ea
GA
347bool SoftSetArg(const std::string& strArg, const std::string& strValue)
348{
349 if (mapArgs.count(strArg))
350 return false;
351 mapArgs[strArg] = strValue;
352 return true;
353}
354
7bf8b7c2 355bool SoftSetBoolArg(const std::string& strArg, bool fValue)
0fcf91ea
GA
356{
357 if (fValue)
358 return SoftSetArg(strArg, std::string("1"));
359 else
360 return SoftSetArg(strArg, std::string("0"));
361}
362
1fdb9fa3
LV
363static const int screenWidth = 79;
364static const int optIndent = 2;
365static const int msgIndent = 7;
366
367std::string HelpMessageGroup(const std::string &message) {
368 return std::string(message) + std::string("\n\n");
369}
370
371std::string HelpMessageOpt(const std::string &option, const std::string &message) {
372 return std::string(optIndent,' ') + std::string(option) +
373 std::string("\n") + std::string(msgIndent,' ') +
374 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
375 std::string("\n\n");
376}
377
27df4123 378static std::string FormatException(const std::exception* pex, const char* pszThread)
2097c09a 379{
6853e627 380#ifdef WIN32
3e468840 381 char pszModule[MAX_PATH] = "";
2097c09a
WL
382 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
383#else
384 const char* pszModule = "bitcoin";
385#endif
386 if (pex)
29b79e4c 387 return strprintf(
2097c09a
WL
388 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
389 else
29b79e4c 390 return strprintf(
2097c09a
WL
391 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
392}
393
27df4123 394void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
2097c09a 395{
29b79e4c 396 std::string message = FormatException(pex, pszThread);
7d9d134b 397 LogPrintf("\n\n************************\n%s\n", message);
29b79e4c
WL
398 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
399 strMiscWarning = message;
2097c09a
WL
400}
401
ee12c3d6 402boost::filesystem::path GetDefaultDataDir()
2097c09a 403{
ee12c3d6 404 namespace fs = boost::filesystem;
8a183335
TH
405 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
406 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
407 // Mac: ~/Library/Application Support/Zcash
408 // Unix: ~/.zcash
6853e627 409#ifdef WIN32
2097c09a 410 // Windows
8a183335 411 return GetSpecialFolderPath(CSIDL_APPDATA) / "Zcash";
2097c09a 412#else
ee12c3d6 413 fs::path pathRet;
2097c09a
WL
414 char* pszHome = getenv("HOME");
415 if (pszHome == NULL || strlen(pszHome) == 0)
ee12c3d6
PW
416 pathRet = fs::path("/");
417 else
418 pathRet = fs::path(pszHome);
6853e627 419#ifdef MAC_OSX
2097c09a 420 // Mac
940e22fd 421 pathRet /= "Library/Application Support";
2b7709dc 422 TryCreateDirectory(pathRet);
8a183335 423 return pathRet / "Zcash";
2097c09a
WL
424#else
425 // Unix
8a183335 426 return pathRet / ".zcash";
2097c09a
WL
427#endif
428#endif
429}
430
ebdb9ff6 431static boost::filesystem::path pathCached;
432static boost::filesystem::path pathCachedNetSpecific;
4f1c3798 433static boost::filesystem::path zc_paramsPathCached;
b94595bb
GA
434static CCriticalSection csPathCached;
435
4f1c3798
SB
436static boost::filesystem::path ZC_GetBaseParamsDir()
437{
438 // Copied from GetDefaultDataDir and adapter for zcash params.
439
440 namespace fs = boost::filesystem;
441 // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
442 // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
443 // Mac: ~/Library/Application Support/ZcashParams
444 // Unix: ~/.zcash-params
445#ifdef WIN32
446 // Windows
447 return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
448#else
449 fs::path pathRet;
450 char* pszHome = getenv("HOME");
451 if (pszHome == NULL || strlen(pszHome) == 0)
452 pathRet = fs::path("/");
453 else
454 pathRet = fs::path(pszHome);
455#ifdef MAC_OSX
456 // Mac
457 pathRet /= "Library/Application Support";
458 TryCreateDirectory(pathRet);
459 return pathRet / "ZcashParams";
460#else
461 // Unix
462 return pathRet / ".zcash-params";
463#endif
464#endif
465}
466
467const boost::filesystem::path &ZC_GetParamsDir()
468{
469 namespace fs = boost::filesystem;
470
471 LOCK(csPathCached); // Reuse the same lock as upstream.
472
473 fs::path &path = zc_paramsPathCached;
474
475 // This can be called during exceptions by LogPrintf(), so we cache the
476 // value so we don't have to do memory allocations after that.
477 if (!path.empty())
478 return path;
479
480 path = ZC_GetBaseParamsDir();
481 path /= BaseParams().DataDir();
482
483 return path;
484}
485
ee12c3d6 486const boost::filesystem::path &GetDataDir(bool fNetSpecific)
2097c09a 487{
ee12c3d6
PW
488 namespace fs = boost::filesystem;
489
b94595bb
GA
490 LOCK(csPathCached);
491
ebdb9ff6 492 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
ee12c3d6 493
faaeae1e 494 // This can be called during exceptions by LogPrintf(), so we cache the
ee12c3d6 495 // value so we don't have to do memory allocations after that.
b94595bb 496 if (!path.empty())
ee12c3d6
PW
497 return path;
498
ee12c3d6 499 if (mapArgs.count("-datadir")) {
f4203de3
PW
500 path = fs::system_complete(mapArgs["-datadir"]);
501 if (!fs::is_directory(path)) {
502 path = "";
503 return path;
504 }
ee12c3d6
PW
505 } else {
506 path = GetDefaultDataDir();
2097c09a 507 }
0e4b3175 508 if (fNetSpecific)
84ce18ca 509 path /= BaseParams().DataDir();
2097c09a 510
3e9c8bab 511 fs::create_directories(path);
ee12c3d6 512
ee12c3d6 513 return path;
2097c09a
WL
514}
515
b94595bb
GA
516void ClearDatadirCache()
517{
ebdb9ff6 518 pathCached = boost::filesystem::path();
519 pathCachedNetSpecific = boost::filesystem::path();
b94595bb
GA
520}
521
ee12c3d6 522boost::filesystem::path GetConfigFile()
2097c09a 523{
aaf64959 524 boost::filesystem::path pathConfigFile(GetArg("-conf", "zcash.conf"));
ac14bcc1
PK
525 if (!pathConfigFile.is_complete())
526 pathConfigFile = GetDataDir(false) / pathConfigFile;
527
ee12c3d6 528 return pathConfigFile;
2097c09a
WL
529}
530
f4203de3 531void ReadConfigFile(map<string, string>& mapSettingsRet,
2097c09a
WL
532 map<string, vector<string> >& mapMultiSettingsRet)
533{
3e468840 534 boost::filesystem::ifstream streamConfig(GetConfigFile());
2097c09a 535 if (!streamConfig.good())
aaf64959 536 return; // No zcash.conf file is OK
2097c09a
WL
537
538 set<string> setOptions;
539 setOptions.insert("*");
ee12c3d6 540
3e468840 541 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
2097c09a 542 {
aaf64959 543 // Don't overwrite existing settings so command line settings override zcash.conf
2097c09a
WL
544 string strKey = string("-") + it->string_key;
545 if (mapSettingsRet.count(strKey) == 0)
d64e124c 546 {
2097c09a 547 mapSettingsRet[strKey] = it->value[0];
3e468840 548 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
d64e124c
CM
549 InterpretNegativeSetting(strKey, mapSettingsRet);
550 }
2097c09a
WL
551 mapMultiSettingsRet[strKey].push_back(it->value[0]);
552 }
b94595bb
GA
553 // If datadir is changed in .conf file:
554 ClearDatadirCache();
2097c09a
WL
555}
556
d6712db3 557#ifndef WIN32
ee12c3d6 558boost::filesystem::path GetPidFile()
2097c09a 559{
aaf64959 560 boost::filesystem::path pathPidFile(GetArg("-pid", "zcashd.pid"));
ee12c3d6
PW
561 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
562 return pathPidFile;
2097c09a
WL
563}
564
ee12c3d6 565void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
2097c09a 566{
ee12c3d6 567 FILE* file = fopen(path.string().c_str(), "w");
f85c0974 568 if (file)
2097c09a
WL
569 {
570 fprintf(file, "%d\n", pid);
571 fclose(file);
572 }
573}
a034c7eb 574#endif
2097c09a 575
768e5d52
JG
576bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
577{
578#ifdef WIN32
5f986195 579 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
8d657a65 580 MOVEFILE_REPLACE_EXISTING) != 0;
768e5d52
JG
581#else
582 int rc = std::rename(src.string().c_str(), dest.string().c_str());
583 return (rc == 0);
584#endif /* WIN32 */
585}
586
c63a73d1
MF
587/**
588 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
589 * Specifically handles case where path p exists, but it wasn't possible for the user to
590 * write to the parent directory.
591 */
2b7709dc
BD
592bool TryCreateDirectory(const boost::filesystem::path& p)
593{
594 try
595 {
596 return boost::filesystem::create_directory(p);
27df4123 597 } catch (const boost::filesystem::filesystem_error&) {
2b7709dc
BD
598 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
599 throw;
600 }
601
602 // create_directory didn't create the directory, it had to have existed already
603 return false;
604}
605
768e5d52
JG
606void FileCommit(FILE *fileout)
607{
4c0b2cde 608 fflush(fileout); // harmless if redundantly called
768e5d52 609#ifdef WIN32
4c0b2cde
PK
610 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
611 FlushFileBuffers(hFile);
768e5d52 612#else
e9965213
PW
613 #if defined(__linux__) || defined(__NetBSD__)
614 fdatasync(fileno(fileout));
e7bad10c
GM
615 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
616 fcntl(fileno(fileout), F_FULLFSYNC, 0);
e9965213 617 #else
768e5d52 618 fsync(fileno(fileout));
e9965213 619 #endif
768e5d52
JG
620#endif
621}
622
1eb57879
PW
623bool TruncateFile(FILE *file, unsigned int length) {
624#if defined(WIN32)
625 return _chsize(_fileno(file), length) == 0;
626#else
627 return ftruncate(fileno(file), length) == 0;
628#endif
629}
630
c63a73d1
MF
631/**
632 * this function tries to raise the file descriptor limit to the requested number.
633 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
634 */
ba29a559
PW
635int RaiseFileDescriptorLimit(int nMinFD) {
636#if defined(WIN32)
637 return 2048;
638#else
639 struct rlimit limitFD;
640 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
641 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
642 limitFD.rlim_cur = nMinFD;
643 if (limitFD.rlim_cur > limitFD.rlim_max)
644 limitFD.rlim_cur = limitFD.rlim_max;
645 setrlimit(RLIMIT_NOFILE, &limitFD);
646 getrlimit(RLIMIT_NOFILE, &limitFD);
647 }
648 return limitFD.rlim_cur;
649 }
650 return nMinFD; // getrlimit failed, assume it's fine
651#endif
652}
653
c63a73d1
MF
654/**
655 * this function tries to make a particular range of a file allocated (corresponding to disk space)
656 * it is advisory, and the range specified in the arguments will never contain live data
657 */
bba89aa8 658void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
288fdc09
PW
659#if defined(WIN32)
660 // Windows-specific version
661 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
662 LARGE_INTEGER nFileSize;
51ed9ec9 663 int64_t nEndPos = (int64_t)offset + length;
288fdc09
PW
664 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
665 nFileSize.u.HighPart = nEndPos >> 32;
666 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
667 SetEndOfFile(hFile);
668#elif defined(MAC_OSX)
669 // OSX specific version
670 fstore_t fst;
671 fst.fst_flags = F_ALLOCATECONTIG;
672 fst.fst_posmode = F_PEOFPOSMODE;
673 fst.fst_offset = 0;
674 fst.fst_length = (off_t)offset + length;
675 fst.fst_bytesalloc = 0;
676 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
677 fst.fst_flags = F_ALLOCATEALL;
678 fcntl(fileno(file), F_PREALLOCATE, &fst);
679 }
680 ftruncate(fileno(file), fst.fst_length);
681#elif defined(__linux__)
682 // Version using posix_fallocate
683 off_t nEndPos = (off_t)offset + length;
684 posix_fallocate(fileno(file), 0, nEndPos);
685#else
686 // Fallback version
687 // TODO: just write one byte per block
bba89aa8
PW
688 static const char buf[65536] = {};
689 fseek(file, offset, SEEK_SET);
690 while (length > 0) {
691 unsigned int now = 65536;
692 if (length < now)
693 now = length;
694 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
695 length -= now;
696 }
288fdc09 697#endif
bba89aa8
PW
698}
699
2097c09a
WL
700void ShrinkDebugFile()
701{
702 // Scroll debug.log if it's getting too big
ee12c3d6
PW
703 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
704 FILE* file = fopen(pathLog.string().c_str(), "r");
a486abd4 705 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
2097c09a
WL
706 {
707 // Restart the file with some of the end
fcb0a1bb 708 std::vector <char> vch(200000,0);
1e735048 709 fseek(file, -((long)vch.size()), SEEK_END);
fcb0a1bb 710 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
2097c09a 711 fclose(file);
f85c0974 712
ee12c3d6 713 file = fopen(pathLog.string().c_str(), "w");
f85c0974 714 if (file)
2097c09a 715 {
fcb0a1bb 716 fwrite(begin_ptr(vch), 1, nBytes, file);
2097c09a
WL
717 fclose(file);
718 }
719 }
3260b4c0
PK
720 else if (file != NULL)
721 fclose(file);
2097c09a
WL
722}
723
ed6d0b5f 724#ifdef WIN32
3e468840
PK
725boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
726{
727 namespace fs = boost::filesystem;
728
729 char pszPath[MAX_PATH] = "";
730
731 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
732 {
733 return fs::path(pszPath);
734 }
735
881a85a2 736 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
3e468840
PK
737 return fs::path("");
738}
ed6d0b5f 739#endif
429039d4 740
597fa4cd
PW
741boost::filesystem::path GetTempPath() {
742#if BOOST_FILESYSTEM_VERSION == 3
743 return boost::filesystem::temp_directory_path();
744#else
745 // TODO: remove when we don't support filesystem v2 anymore
746 boost::filesystem::path path;
747#ifdef WIN32
748 char pszPath[MAX_PATH] = "";
749
750 if (GetTempPathA(MAX_PATH, pszPath))
751 path = boost::filesystem::path(pszPath);
752#else
753 path = boost::filesystem::path("/tmp");
754#endif
755 if (path.empty() || !boost::filesystem::is_directory(path)) {
881a85a2 756 LogPrintf("GetTempPath(): failed to find temp path\n");
597fa4cd
PW
757 return boost::filesystem::path("");
758 }
759 return path;
760#endif
761}
762
429039d4
JG
763void runCommand(std::string strCommand)
764{
765 int nErr = ::system(strCommand.c_str());
766 if (nErr)
7d9d134b 767 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
429039d4
JG
768}
769
96931d6f
GS
770void RenameThread(const char* name)
771{
b277b0f1 772#if defined(PR_SET_NAME)
96931d6f
GS
773 // Only the first 15 characters are used (16 - NUL terminator)
774 ::prctl(PR_SET_NAME, name, 0, 0, 0);
7f386d2f 775#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
304ca955 776 pthread_set_name_np(pthread_self(), name);
cd58f058 777
850c570d 778#elif defined(MAC_OSX)
c8c2fbe0 779 pthread_setname_np(name);
96931d6f
GS
780#else
781 // Prevent warnings for unused parameters...
782 (void)name;
783#endif
784}
0b47fe6b 785
5248ff40
SC
786void SetupEnvironment()
787{
317e66c7 788 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
789 // may be invalid, in which case the "C" locale is used as fallback.
790#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
791 try {
3da78490 792 std::locale(""); // Raises a runtime error if current locale is invalid
27df4123 793 } catch (const std::runtime_error&) {
ba0fa0d9 794 setenv("LC_ALL", "C", 1);
5248ff40 795 }
ac14bcc1 796#endif
3da78490 797 // The path locale is lazy initialized and to avoid deinitialization errors
317e66c7 798 // in multithreading environments, it is set explicitly by the main thread.
3da78490
JS
799 // A dummy locale is used to extract the internal default locale, used by
800 // boost::filesystem::path, which is then used to explicitly imbue the path.
801 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
ba0fa0d9 802 boost::filesystem::path::imbue(loc);
5248ff40 803}
3e8ac6af 804
610a8c07
WL
805void SetThreadPriority(int nPriority)
806{
807#ifdef WIN32
808 SetThreadPriority(GetCurrentThread(), nPriority);
809#else // WIN32
810#ifdef PRIO_THREAD
811 setpriority(PRIO_THREAD, 0, nPriority);
812#else // PRIO_THREAD
813 setpriority(PRIO_PROCESS, 0, nPriority);
814#endif // PRIO_THREAD
815#endif // WIN32
816}
This page took 0.364314 seconds and 4 git commands to generate.