Added command line option --data-dir.
This commit is contained in:
parent
64650bf121
commit
8698b73036
10 changed files with 126 additions and 63 deletions
|
@ -5,8 +5,8 @@
|
|||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <ctime>
|
||||
#include <string>
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
|
@ -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<unsigned int>(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast<uintptr_t>(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<size_t>(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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue