From 8698b73036d31f589776a3164a3cd995a5b86748 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 10 Mar 2020 15:57:47 +0700 Subject: [PATCH] Added command line option --data-dir. --- src/base/kernel/Base.cpp | 6 +- src/base/kernel/Base.h | 4 +- src/base/kernel/Env.cpp | 14 +-- src/base/kernel/Env.h | 4 +- src/base/kernel/Process.cpp | 123 +++++++++++++++++---------- src/base/kernel/Process.h | 10 ++- src/base/kernel/interfaces/IConfig.h | 1 + src/base/tools/Arguments.cpp | 21 ++++- src/base/tools/Arguments.h | 5 +- src/core/config/Config_platform.h | 1 + 10 files changed, 126 insertions(+), 63 deletions(-) diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 42d274676..b7d3b4814 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -127,7 +127,7 @@ private: return config.release(); } - chain.addFile(Process::location(Process::ExeLocation, "config.json")); + chain.addFile(Process::location(Process::DataLocation, "config.json")); if (read(chain, config)) { return config.release(); diff --git a/src/base/kernel/Base.h b/src/base/kernel/Base.h index ef850eddf..756b6cafc 100644 --- a/src/base/kernel/Base.h +++ b/src/base/kernel/Base.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/Env.cpp b/src/base/kernel/Env.cpp index 3498f2ec5..9b018e6ed 100644 --- a/src/base/kernel/Env.cpp +++ b/src/base/kernel/Env.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -56,9 +56,13 @@ static std::map variables; static void createVariables() { - variables.insert({ "XMRIG_VERSION", APP_VERSION }); - variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation, "") }); - variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation, "") }); + variables.insert({ "XMRIG_VERSION", APP_VERSION }); + variables.insert({ "XMRIG_EXE", Process::exepath() }); + variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation) }); + variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation) }); + variables.insert({ "XMRIG_HOME_DIR", Process::location(Process::HomeLocation) }); + variables.insert({ "XMRIG_TEMP_DIR", Process::location(Process::TempLocation) }); + variables.insert({ "XMRIG_DATA_DIR", Process::location(Process::DataLocation) }); } #endif diff --git a/src/base/kernel/Env.h b/src/base/kernel/Env.h index 2f33abf94..dd06297d0 100644 --- a/src/base/kernel/Env.h +++ b/src/base/kernel/Env.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 4fc3ea3a3..34f5d02b1 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ #include +#include #include @@ -34,20 +35,75 @@ namespace xmrig { -static size_t getLocation(Process::Location location, char *buf, size_t max) -{ - using namespace xmrig; +static char pathBuf[520]; +static std::string dataDir; + + +static std::string getPath(Process::Location location) +{ + size_t size = sizeof(pathBuf); + + if (location == Process::DataLocation) { + if (!dataDir.empty()) { + return dataDir; + } + + location = Process::ExeLocation; + } + + if (location == Process::HomeLocation) { +# if UV_VERSION_HEX >= 0x010600 + return uv_os_homedir(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); +# else + location = Process::ExeLocation; +# endif + } + + if (location == Process::TempLocation) { +# if UV_VERSION_HEX >= 0x010900 + return uv_os_tmpdir(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); +# else + location = Process::ExeLocation; +# endif + } - size_t size = max; if (location == Process::ExeLocation) { - return uv_exepath(buf, &size) < 0 ? 0 : size; + if (uv_exepath(pathBuf, &size) < 0) { + return {}; + } + + const auto path = std::string(pathBuf, size); + const auto pos = path.rfind(Process::kDirSeparator); + + if (pos != std::string::npos) { + return path.substr(0, pos); + } + + return path; } if (location == Process::CwdLocation) { - return uv_cwd(buf, &size) < 0 ? 0 : size; + return uv_cwd(pathBuf, &size) < 0 ? "" : std::string(pathBuf, size); } - return 0; + return {}; +} + + +static void setDataDir(const char *path) +{ + if (path == nullptr) { + return; + } + + std::string dir = path; + if (!dir.empty() && (dir.back() == '/' || dir.back() == '\\')) { + dir.pop_back(); + } + + if (!dir.empty() && uv_chdir(dir.c_str()) == 0) { + dataDir = dir; + } } @@ -58,46 +114,25 @@ xmrig::Process::Process(int argc, char **argv) : m_arguments(argc, argv) { srand(static_cast(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast(this))); + + setDataDir(m_arguments.value("--data-dir")); +} + + +xmrig::String xmrig::Process::exepath() +{ + size_t size = sizeof(pathBuf); + + return uv_exepath(pathBuf, &size) < 0 ? "" : String(pathBuf, size); } xmrig::String xmrig::Process::location(Location location, const char *fileName) { - constexpr const size_t max = 520; - - char *buf = new char[max](); - size_t size = getLocation(location, buf, max); - - if (size == 0) { - delete [] buf; - - return String(); + auto path = getPath(location); + if (path.empty() || fileName == nullptr) { + return path.c_str(); } - if (fileName == nullptr) { - return buf; - } - - if (location == ExeLocation) { - char *p = strrchr(buf, kDirSeparator); - - if (p == nullptr) { - delete [] buf; - - return String(); - } - - size = static_cast(p - buf); - } - - if ((size + strlen(fileName) + 2) >= max) { - delete [] buf; - - return String(); - } - - buf[size] = kDirSeparator; - strcpy(buf + size + 1, fileName); - - return buf; + return (path + kDirSeparator + fileName).c_str(); } diff --git a/src/base/kernel/Process.h b/src/base/kernel/Process.h index 12cfc904c..29f247f2b 100644 --- a/src/base/kernel/Process.h +++ b/src/base/kernel/Process.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +37,10 @@ class Process public: enum Location { ExeLocation, - CwdLocation + CwdLocation, + DataLocation, + HomeLocation, + TempLocation }; # ifdef WIN32 @@ -48,6 +51,7 @@ public: Process(int argc, char **argv); + static String exepath(); static String location(Location location, const char *fileName = nullptr); inline const Arguments &arguments() const { return m_arguments; } diff --git a/src/base/kernel/interfaces/IConfig.h b/src/base/kernel/interfaces/IConfig.h index 93025fc9f..806dcd4d2 100644 --- a/src/base/kernel/interfaces/IConfig.h +++ b/src/base/kernel/interfaces/IConfig.h @@ -73,6 +73,7 @@ public: DaemonKey = 1018, DaemonPollKey = 1019, SelfSelectKey = 1028, + DataDirKey = 1035, // xmrig common CPUPriorityKey = 1021, diff --git a/src/base/tools/Arguments.cpp b/src/base/tools/Arguments.cpp index 933173aa3..d0352939b 100644 --- a/src/base/tools/Arguments.cpp +++ b/src/base/tools/Arguments.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,6 +52,23 @@ bool xmrig::Arguments::hasArg(const char *name) const } +const char *xmrig::Arguments::value(const char *key) const +{ + const size_t size = m_data.size(); + if (size < 3) { + return nullptr; + } + + for (size_t i = 1; i < size - 1; ++i) { + if (m_data[i] == key) { + return m_data[i + 1]; + } + } + + return nullptr; +} + + void xmrig::Arguments::add(const char *arg) { if (arg == nullptr) { diff --git a/src/base/tools/Arguments.h b/src/base/tools/Arguments.h index e47e39e38..0016c519f 100644 --- a/src/base/tools/Arguments.h +++ b/src/base/tools/Arguments.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,6 +41,7 @@ public: Arguments(int argc, char **argv); bool hasArg(const char *name) const; + const char *value(const char *key) const; inline char **argv() const { return m_argv; } inline const std::vector &data() const { return m_data; } diff --git a/src/core/config/Config_platform.h b/src/core/config/Config_platform.h index b6bed7f86..6dc139a09 100644 --- a/src/core/config/Config_platform.h +++ b/src/core/config/Config_platform.h @@ -89,6 +89,7 @@ static const option options[] = { { "cpu-no-yield", 0, nullptr, IConfig::YieldKey }, { "verbose", 0, nullptr, IConfig::VerboseKey }, { "proxy", 1, nullptr, IConfig::ProxyKey }, + { "data-dir", 1, nullptr, IConfig::DataDirKey }, # ifdef XMRIG_FEATURE_TLS { "tls", 0, nullptr, IConfig::TlsKey }, { "tls-fingerprint", 1, nullptr, IConfig::FingerprintKey },