From ca47bed7b9201a918931bd5e915e212b30bb519e Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Thu, 24 Jul 2025 13:27:59 +0200 Subject: [PATCH] Workaround for read-only working directory --- src/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++------------ src/util.cpp | 11 ++++++++++ src/util.h | 2 ++ 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 971e9c1..61f1bb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,7 @@ #endif // WITH_GRPC #include +#include #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 diff --git a/src/util.cpp b/src/util.cpp index 22697c1..3cd58de 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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; diff --git a/src/util.h b/src/util.h index 2f60fd0..429e37b 100644 --- a/src/util.h +++ b/src/util.h @@ -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();