Skip to content

Commit 6048992

Browse files
authored
Merge pull request #1737 from jamescowens/getdatadirfix
util: Fix braindamage in GetDefaultDataDir()
2 parents 859ee25 + 6b592a1 commit 6048992

File tree

5 files changed

+47
-79
lines changed

5 files changed

+47
-79
lines changed

src/init.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ void InitLogging()
427427
}
428428

429429
if (!LogInstance().m_log_timestamps)
430+
{
430431
LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
432+
}
433+
431434
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
432435
LogPrintf("Using data directory %s\n", GetDataDir().string());
433436

@@ -741,10 +744,7 @@ bool AppInit2(ThreadHandlerPtr threads)
741744
#endif
742745

743746
LogPrintf("Using OpenSSL version %s", SSLeay_version(SSLEAY_VERSION));
744-
if (!fLogTimestamps)
745-
LogPrintf("Startup time: %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()));
746-
LogPrintf("Default data directory %s", GetDefaultDataDir().string());
747-
LogPrintf("Used data directory %s", datadir.string());
747+
748748
std::ostringstream strErrors;
749749

750750
fDevbuildCripple = false;

src/logging.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <mutex>
1717
#include <set>
1818

19-
// Unavoidable because these are in util.h.
20-
extern fs::path &GetDataDir(bool fNetSpecific);
19+
// externs unavoidable because these are in util.h.
20+
extern fs::path &GetDataDir(bool fNetSpecific = true);
2121
extern bool GetBoolArg(const std::string& strArg, bool fDefault);
2222
extern int64_t GetArg(const std::string& strArg, int64_t nDefault);
2323

@@ -374,7 +374,7 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
374374
boost::gregorian::date ArchiveCheckDate = boost::posix_time::from_time_t(nTime).date();
375375
fs::path plogfile;
376376
fs::path pfile_temp;
377-
fs::path pathDataDir = GetDataDir(false);
377+
fs::path pathDataDir = GetDataDir();
378378

379379
std::stringstream ssArchiveCheckDate, ssPrevArchiveCheckDate;
380380

@@ -512,4 +512,3 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
512512
return false; // archive condition was not satisfied. Do nothing and return false.
513513
}
514514
}
515-

src/qt/guiutil.cpp

+3-13
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,9 @@ AutoStartupArguments GetAutoStartupArguments()
398398

399399
AutoStartupArguments result;
400400

401-
// We can't use GetDataDir() here because it appends /testnet for
402-
// testnet, which is correct for internal use, but not what we
403-
// need for the command line argument. I think the fNetSpecific flag
404-
// for GetDataDir is not coded right.
405-
// TODO: Review GetDataDir() fix and replace this.
406-
if (mapArgs.count("-datadir"))
407-
{
408-
result.data_dir = fs::system_complete(mapArgs["-datadir"]);
409-
if (!fs::is_directory(result.data_dir))
410-
{
411-
result.data_dir = "";
412-
}
413-
}
401+
// We do NOT want testnet appended here for the path in the autostart
402+
// shortcut, so use false in GetDataDir().
403+
result.data_dir = GetDataDir(false);
414404

415405
result.arguments = "-min";
416406

src/scraper/scraper.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,7 @@ void Scraper(bool bSingleShot)
962962
pathScraper = pathDataDir / "Scraper";
963963
}
964964

965-
// Initialize log singleton. Must be after the imbue.
966-
ScraperLogInstance();
965+
_log(logattribute::INFO, "Scraper", "Using data directory " + pathScraper.string());
967966

968967
if (!bSingleShot)
969968
_log(logattribute::INFO, "Scraper", "Starting Scraper thread.");
@@ -1217,11 +1216,9 @@ void NeuralNetwork()
12171216
{
12181217
pathDataDir = GetDataDir();
12191218
pathScraper = pathDataDir / "Scraper";
1220-
1221-
// Initialize log singleton. Must be after the imbue.
1222-
ScraperLogInstance();
12231219
}
12241220

1221+
_log(logattribute::INFO, "Scraper", "Using data directory " + pathScraper.string());
12251222

12261223
_log(logattribute::INFO, "NeuralNetwork", "Starting Neural Network housekeeping thread (new C++ implementation). \n"
12271224
"Note that this does NOT mean the NN is active. This simply does housekeeping "

src/util.cpp

+35-53
Original file line numberDiff line numberDiff line change
@@ -545,98 +545,80 @@ fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
545545

546546
fs::path GetDefaultDataDir()
547547
{
548-
// Windows < Vista: C:\Documents and Settings\Username\Application Data\gridcoinresearch
549-
// Windows >= Vista: C:\Users\Username\AppData\Roaming\gridcoinresearch
550-
// Mac: ~/Library/Application Support/gridcoinresearch
551-
// Unix: ~/.gridcoin
548+
// Windows < Vista: C:\Documents and Settings\Username\Application Data\GridcoinResearch
549+
// Windows >= Vista: C:\Users\Username\AppData\Roaming\GridcoinResearch
550+
// Mac: ~/Library/Application Support/GridcoinResearch
551+
// Linux/Unix: ~/.GridcoinResearch
552552
#ifdef WIN32
553553
// Windows
554+
555+
// This is the user's base roaming AppData path with GridcoinResearch added.
554556
return GetSpecialFolderPath(CSIDL_APPDATA) / "GridcoinResearch";
555557
#else
556-
//2-25-2015
557-
fs::path pathRet;
558-
char* pszHome = getenv("HOME");
558+
fs::path pathRet;
559559

560-
if (mapArgs.count("-datadir"))
561-
{
562-
fs::path path2015 = fs::system_complete(mapArgs["-datadir"]);
563-
if (fs::is_directory(path2015))
564-
{
565-
pathRet = path2015;
566-
}
567-
}
568-
else
569-
{
570-
if (pszHome == NULL || strlen(pszHome) == 0)
571-
pathRet = fs::path("/");
572-
else
573-
pathRet = fs::path(pszHome);
574-
}
575-
#ifdef MAC_OSX
576-
// Mac
577-
pathRet /= "Library/Application Support";
578-
fs::create_directory(pathRet);
560+
// For everything except for Windows, use the environment variable to get
561+
// the home path.
562+
char* pszHome = getenv("HOME");
579563

580-
if (mapArgs.count("-datadir"))
581-
{
582-
return pathRet;
583-
}
584-
else
585-
{
586-
return pathRet / "GridcoinResearch";
564+
// There is no home path, so default to the root directory.
565+
if (pszHome == nullptr || strlen(pszHome) == 0) {
566+
pathRet = fs::path("/");
567+
} else {
568+
pathRet = fs::path(pszHome);
587569
}
570+
#ifdef MAC_OSX
571+
// The pathRet here represents the HOME directory. Apple
572+
// applications are expected to store their files in
573+
// "~/Library/Application Support/[AppDir].
574+
return pathRet / "Library" / "Application Support" / "GridcoinResearch";
588575
#else
589-
// Unix
590-
if (mapArgs.count("-datadir"))
591-
{
592-
return pathRet;
593-
}
594-
else
595-
{
596-
return pathRet / ".GridcoinResearch";
597-
}
598-
#endif
599-
#endif
576+
// Linux/Unix
577+
return pathRet / ".GridcoinResearch";
578+
#endif // MAC_OSX
579+
#endif // WIN32
600580
}
601581

602582
const fs::path &GetDataDir(bool fNetSpecific)
603583
{
604584
static fs::path pathCached[2];
605-
static CCriticalSection csPathCached;
585+
static CCriticalSection cs_PathCached;
606586
static bool cachedPath[2] = {false, false};
607-
//2-25-2015
587+
608588
fs::path &path = pathCached[fNetSpecific];
609589

610590
// This can be called during exceptions by LogPrintf, so we cache the
611591
// value so we don't have to do memory allocations after that.
612-
if (cachedPath[fNetSpecific] && (fs::is_directory(path)) )
592+
if (cachedPath[fNetSpecific] && fs::is_directory(path))
613593
{
614594
return path;
615595
}
616596

617-
LOCK(csPathCached);
597+
LOCK(cs_PathCached);
618598

619599
if (mapArgs.count("-datadir"))
620600
{
621601
path = fs::system_complete(mapArgs["-datadir"]);
622602
if (!fs::is_directory(path))
623603
{
624-
path = "";
625-
return path;
604+
throw std::runtime_error("Supplied datadir is invalid! "
605+
"Please check your datadir argument. Aborting.");
626606
}
627607
}
628608
else
629609
{
630610
path = GetDefaultDataDir();
631611
}
632-
if ( (fNetSpecific && GetBoolArg("-testnet", false)) || GetBoolArg("-testnet",false) )
612+
613+
if (fNetSpecific && GetBoolArg("-testnet", false))
633614
{
634615
path /= "testnet";
635616
}
636617

637618
if (!fs::exists(path)) fs::create_directory(path);
638619

639-
cachedPath[fNetSpecific]=true;
620+
cachedPath[fNetSpecific] = true;
621+
640622
return path;
641623
}
642624

@@ -687,7 +669,7 @@ fs::path GetProgramDir()
687669
fs::path GetConfigFile()
688670
{
689671
fs::path pathConfigFile(GetArg("-conf", "gridcoinresearch.conf"));
690-
if (!pathConfigFile.is_absolute()) pathConfigFile = GetDataDir(false) / pathConfigFile;
672+
if (!pathConfigFile.is_absolute()) pathConfigFile = GetDataDir() / pathConfigFile;
691673
return pathConfigFile;
692674
}
693675

0 commit comments

Comments
 (0)