From 921611ff4d38a97e19a3c4673e621a6df07fbcd7 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:20:43 +0100 Subject: [PATCH] Added `--data-dir` command line option --- docs/COMMAND_LINE.MD | 3 ++- src/block_cache.cpp | 12 ++++++++---- src/log.cpp | 22 ++++++++++++++-------- src/main.cpp | 17 ++++++++++++++++- src/mempool.cpp | 1 - src/p2p_server.cpp | 10 +++++++--- src/p2pool.cpp | 7 ++++--- src/params.cpp | 6 ++++++ src/util.cpp | 2 ++ src/util.h | 2 ++ 10 files changed, 61 insertions(+), 21 deletions(-) diff --git a/docs/COMMAND_LINE.MD b/docs/COMMAND_LINE.MD index b768e4b..4e35c7b 100644 --- a/docs/COMMAND_LINE.MD +++ b/docs/COMMAND_LINE.MD @@ -10,8 +10,9 @@ --addpeers Comma-separated list of IP:port of other p2pool nodes to connect to --light-mode Don't allocate RandomX dataset, saves 2GB of RAM --loglevel Verbosity of the log, integer number between 0 and 6 +--data-dir Path to store general p2pool files (log, cache, peer data, etc.), default is current directory --config Name of p2pool sidechain's config file (don't use it unless you want to mine to a different p2pool chain) ---data-api Path to the p2pool JSON data (use it in tandem with an external web-server) +--data-api Path to the p2pool JSON data (use it in tandem with an external web-server). Not affected by --data-dir setting! --local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics --stratum-api An alias for --local-api --no-cache Disable p2pool.cache diff --git a/src/block_cache.cpp b/src/block_cache.cpp index cf84d8c..65a189e 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -36,9 +36,11 @@ struct BlockCache::Impl : public nocopy_nomove Impl() { - m_fd = open(cache_name, O_RDWR | O_CREAT, static_cast(0600)); + const std::string cache_path = DATA_DIR + cache_name; + + m_fd = open(cache_path.c_str(), O_RDWR | O_CREAT, static_cast(0600)); if (m_fd == -1) { - LOGERR(1, "couldn't open/create " << cache_name); + LOGERR(1, "couldn't open/create " << cache_path << ": error " << errno); return; } @@ -87,11 +89,12 @@ struct BlockCache::Impl : public nocopy_nomove #elif defined(_WIN32) Impl() - : m_file(CreateFile(cache_name, GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL)) + : m_cachePath(DATA_DIR + cache_name) + , m_file(CreateFile(m_cachePath.c_str(), GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL)) , m_map(0) { if (m_file == INVALID_HANDLE_VALUE) { - LOGERR(1, "couldn't open " << cache_name << ", error " << static_cast(GetLastError())); + LOGERR(1, "couldn't open " << m_cachePath << ": error " << static_cast(GetLastError())); return; } @@ -142,6 +145,7 @@ struct BlockCache::Impl : public nocopy_nomove } } + std::string m_cachePath; HANDLE m_file; HANDLE m_map; diff --git a/src/log.cpp b/src/log.cpp index 9c76e96..4471276 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -127,7 +127,12 @@ public: std::setlocale(LC_ALL, "en_001"); - m_logFile.open(log_file_name, std::ios::app | std::ios::binary); + m_logFilePath = DATA_DIR + log_file_name; + m_logFile.open(m_logFilePath, std::ios::app | std::ios::binary); + + if (!m_logFile.is_open()) { + fprintf(stderr, "failed to open %s: error %d\n", m_logFilePath.c_str(), errno); + } m_buf.resize(BUF_SIZE); @@ -148,10 +153,6 @@ public: CONSOLE_COLORS = false; } - if (!m_logFile.is_open()) { - fprintf(stderr, "failed to open %s\n", log_file_name); - } - init_uv_threadpool(); const int err = uv_thread_create(&m_worker, run_wrapper, this); @@ -216,6 +217,8 @@ public: uv_cond_signal(&m_cond); } + const std::string& log_file_path() const { return m_logFilePath; } + private: static void init_uv_threadpool() { @@ -324,9 +327,9 @@ private: // Reopen the log file if it's been moved (logrotate support) struct stat buf; - if (stat(log_file_name, &buf) != 0) { + if (stat(m_logFilePath.c_str(), &buf) != 0) { m_logFile.close(); - m_logFile.open(log_file_name, std::ios::app | std::ios::binary); + m_logFile.open(m_logFilePath, std::ios::app | std::ios::binary); } } @@ -373,6 +376,7 @@ private: bool m_stopped; std::ofstream m_logFile; + std::string m_logFilePath; }; static Worker* worker = nullptr; @@ -438,7 +442,9 @@ void start() void reopen() { // This will trigger the worker thread which will then reopen log file if it's been moved - LOGINFO(0, "reopening " << log_file_name); +#ifndef P2POOL_LOG_DISABLE + LOGINFO(0, "reopening " << worker->log_file_path()); +#endif } void stop() diff --git a/src/main.cpp b/src/main.cpp index 30f0a24..de00fee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,8 +47,9 @@ void p2pool_usage() "--addpeers Comma-separated list of IP:port of other p2pool nodes to connect to\n" "--light-mode Don't allocate RandomX dataset, saves 2GB of RAM\n" "--loglevel Verbosity of the log, integer number between 0 and %d\n" + "--data-dir Path to store general p2pool files (log, cache, peer data, etc.), default is current directory\n" "--config Name of the p2pool config file\n" - "--data-api Path to the p2pool JSON data (use it in tandem with an external web-server)\n" + "--data-api Path to the p2pool JSON data (use it in tandem with an external web-server). Not affected by --data-dir setting!\n" "--local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics\n" "--stratum-api An alias for --local-api\n" "--no-cache Disable p2pool.cache\n" @@ -194,6 +195,20 @@ int main(int argc, char* argv[]) if (!strcmp(argv[i], "--test")) { return p2pool_test(); } + + 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); + } } #if defined(_WIN32) && defined(_MSC_VER) && !defined(NDEBUG) diff --git a/src/mempool.cpp b/src/mempool.cpp index ba83921..0522d3b 100644 --- a/src/mempool.cpp +++ b/src/mempool.cpp @@ -17,7 +17,6 @@ #include "common.h" #include "mempool.h" -#include "util.h" LOG_CATEGORY(Mempool) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 0290c78..79d835c 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -447,10 +447,12 @@ void P2PServer::save_peer_list_async() void P2PServer::save_peer_list() { - std::ofstream f(saved_peer_list_file_name, std::ios::binary); + const std::string path = DATA_DIR + saved_peer_list_file_name; + + std::ofstream f(path, std::ios::binary); if (!f.is_open()) { - LOGERR(1, "failed to save peer list " << saved_peer_list_file_name << ", error " << errno); + LOGERR(1, "failed to save peer list " << path << ": error " << errno); return; } @@ -579,7 +581,9 @@ void P2PServer::load_peer_list() } // Finally load peers from p2pool_peers.txt - std::ifstream f(saved_peer_list_file_name); + const std::string path = DATA_DIR + saved_peer_list_file_name; + + std::ifstream f(path); if (f.is_open()) { std::string address; while (f.good()) { diff --git a/src/p2pool.cpp b/src/p2pool.cpp index d355c6c..d46a756 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -1188,7 +1188,7 @@ void p2pool::load_found_blocks() return; } - std::ifstream f(FOUND_BLOCKS_FILE); + std::ifstream f(DATA_DIR + FOUND_BLOCKS_FILE); if (!f.is_open()) { return; } @@ -1721,14 +1721,15 @@ void p2pool::api_update_block_found(const ChainMain* data, const PoolBlock* bloc difficulty_type diff; if (data && get_difficulty_at_height(data->height, diff)) { - std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app); + const std::string path = DATA_DIR + FOUND_BLOCKS_FILE; + std::ofstream f(path, std::ios::app); if (f.is_open()) { f << cur_time << ' ' << data->height << ' ' << data->id << ' ' << diff << ' ' << total_hashes << '\n'; f.flush(); f.close(); } else { - LOGERR(1, "Failed to update " << FOUND_BLOCKS_FILE << ", error " << errno); + LOGERR(1, "Failed to update " << path << ": error " << errno); } } diff --git a/src/params.cpp b/src/params.cpp index 3abeafe..6250986 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -95,6 +95,12 @@ Params::Params(int argc, char* const argv[]) ok = true; } + if ((strcmp(argv[i], "--data-dir") == 0) && (i + 1 < argc)) { + // Processed in main.cpp + ++i; + ok = true; + } + if ((strcmp(argv[i], "--config") == 0) && (i + 1 < argc)) { m_config = argv[++i]; ok = true; diff --git a/src/util.cpp b/src/util.cpp index b4f3271..637a72b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -60,6 +60,8 @@ const char* VERSION = "v" STR2(P2POOL_VERSION_MAJOR) "." STR2(P2POOL_VERSION_MIN #endif " on " __DATE__ ")"; +std::string DATA_DIR; + SoftwareID get_software_id(uint32_t value) { switch (value) { diff --git a/src/util.h b/src/util.h index f531e22..0f6b2ba 100644 --- a/src/util.h +++ b/src/util.h @@ -42,6 +42,8 @@ constexpr uint32_t P2POOL_VERSION = (P2POOL_VERSION_MAJOR << 16) | (P2POOL_VERSI extern const char* VERSION; +extern std::string DATA_DIR; + enum class SoftwareID : uint32_t { P2Pool = 0, GoObserver = 0x624F6F47UL,