Workaround for read-only working directory
This commit is contained in:
+44
-13
@@ -38,6 +38,7 @@
|
||||
#endif // WITH_GRPC
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef WITH_RANDOMX
|
||||
#include "randomx.h"
|
||||
@@ -220,6 +221,9 @@ int main(int argc, char* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool is_mini = false;
|
||||
bool is_nano = false;
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "/help") || !strcmp(argv[i], "-h") || !strcmp(argv[i], "/h")) {
|
||||
p2pool_usage();
|
||||
@@ -237,25 +241,52 @@ int main(int argc, char* argv[])
|
||||
|
||||
if ((strcmp(argv[i], "--data-dir") == 0) && (i + 1 < argc)) {
|
||||
std::string path = argv[++i];
|
||||
|
||||
if (!path.empty() && (path.back() != '/')
|
||||
#ifdef _WIN32
|
||||
&& (path.back() != '\\')
|
||||
#endif
|
||||
) {
|
||||
path.append(1, '/');
|
||||
}
|
||||
|
||||
p2pool::DATA_DIR = std::move(path);
|
||||
p2pool::fixup_path(p2pool::DATA_DIR);
|
||||
}
|
||||
|
||||
// Try to create it if it doesn't exist
|
||||
if (!p2pool::DATA_DIR.empty()) {
|
||||
std::error_code err;
|
||||
std::filesystem::create_directories(p2pool::DATA_DIR, err);
|
||||
if (!strcmp(argv[i], "--mini")) is_mini = true;
|
||||
if (!strcmp(argv[i], "--nano")) is_nano = true;
|
||||
}
|
||||
|
||||
// If the data directory is not set, check if P2Pool has write access to the current directory
|
||||
// If it doesn't, switch to user's home directory
|
||||
if (p2pool::DATA_DIR.empty()) {
|
||||
std::ofstream f("p2pool.tmp");
|
||||
if (f && f.put(' ')) {
|
||||
f.close();
|
||||
std::remove("p2pool.tmp");
|
||||
}
|
||||
else {
|
||||
char buf[1024];
|
||||
size_t size = sizeof(buf);
|
||||
|
||||
if (uv_os_homedir(buf, &size) == 0) {
|
||||
p2pool::DATA_DIR.assign(buf, size);
|
||||
p2pool::fixup_path(p2pool::DATA_DIR);
|
||||
|
||||
p2pool::DATA_DIR += ".p2pool/";
|
||||
|
||||
if (is_mini) {
|
||||
p2pool::DATA_DIR += "mini/";
|
||||
}
|
||||
else if (is_nano) {
|
||||
p2pool::DATA_DIR += "nano/";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!p2pool::DATA_DIR.empty()) {
|
||||
printf("Using \"%s\" for P2Pool files\n", p2pool::DATA_DIR.c_str());
|
||||
}
|
||||
|
||||
// Try to create it if it doesn't exist
|
||||
if (!p2pool::DATA_DIR.empty()) {
|
||||
std::error_code err;
|
||||
std::filesystem::create_directories(p2pool::DATA_DIR, err);
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && defined(_MSC_VER) && !defined(NDEBUG)
|
||||
SymInitialize(GetCurrentProcess(), NULL, TRUE);
|
||||
#endif
|
||||
|
||||
@@ -103,6 +103,17 @@ std::string p2pool_version()
|
||||
return std::string(buf, s.m_pos);
|
||||
}
|
||||
|
||||
void fixup_path(std::string& path)
|
||||
{
|
||||
if (!path.empty() && (path.back() != '/')
|
||||
#ifdef _WIN32
|
||||
&& (path.back() != '\\')
|
||||
#endif
|
||||
) {
|
||||
path.append(1, '/');
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t ED25519_MASTER_PUBLIC_KEY[32] = {51,175,37,73,203,241,188,115,195,255,123,53,218,120,90,74,186,240,82,178,67,139,124,91,180,106,188,181,187,51,236,10};
|
||||
|
||||
std::string DATA_DIR;
|
||||
|
||||
@@ -383,6 +383,8 @@ void minidump_and_crash(size_t delay);
|
||||
|
||||
std::string p2pool_version();
|
||||
|
||||
void fixup_path(std::string& path);
|
||||
|
||||
} // namespace p2pool
|
||||
|
||||
void memory_tracking_start();
|
||||
|
||||
Reference in New Issue
Block a user