From cb74fbbff0278eec4282d6b0fab9e2861102d655 Mon Sep 17 00:00:00 2001 From: DanS Date: Thu, 2 Jul 2026 20:49:24 -0500 Subject: [PATCH] refactor(audit): unify config-dir resolution through Platform::getConfigDir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings::getDefaultPath and AddressBook::getDefaultPath each hand-rolled the same per-platform #ifdef (Win SHGetFolderPath / macOS Application Support / XDG ~/.config) with the DRAGONX_APP_NAME variant suffix. Route both through util::Platform::getConfigDir() instead — one place owns the split — and add the missing __APPLE__ branch to getConfigDir so it too returns the macOS-native ~/Library/Application Support/. This also fixes a latent macOS inconsistency: transaction_history_cache already resolves via getConfigDir(), which lacked the __APPLE__ branch, so on macOS the tx cache landed in ~/.config while settings/addressbook went to ~/Library/Application Support. Now all three agree. No migration needed: Linux/Windows paths are unchanged, and macOS packaging is not yet shipped. Removes ~50 lines of duplicated #ifdef. Full-node + Lite build clean; ctest 1/1; hygiene clean. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/config/settings.cpp | 30 ++++------------------------ src/data/address_book.cpp | 41 ++++----------------------------------- src/util/platform.cpp | 3 +++ 3 files changed, 11 insertions(+), 63 deletions(-) diff --git a/src/config/settings.cpp b/src/config/settings.cpp index 28a2ca1..e5834dd 100644 --- a/src/config/settings.cpp +++ b/src/config/settings.cpp @@ -110,33 +110,11 @@ void loadClamped(const json& j, const char* key, T& field, T lo, T hi) std::string Settings::getDefaultPath() { -#ifdef _WIN32 - char path[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, path))) { - std::string dir = std::string(path) + "\\" DRAGONX_APP_NAME; - fs::create_directories(dir); - return dir + "\\settings.json"; - } - return "settings.json"; -#elif defined(__APPLE__) - const char* home = getenv("HOME"); - if (!home) { - struct passwd* pw = getpwuid(getuid()); - home = pw->pw_dir; - } - std::string dir = std::string(home) + "/Library/Application Support/" DRAGONX_APP_NAME; + // Single per-platform, per-variant config dir (util::Platform::getConfigDir handles the + // _WIN32 / __APPLE__ / XDG split and the DRAGONX_APP_NAME variant suffix in one place). + const std::string dir = util::Platform::getConfigDir(); fs::create_directories(dir); - return dir + "/settings.json"; -#else - const char* home = getenv("HOME"); - if (!home) { - struct passwd* pw = getpwuid(getuid()); - home = pw->pw_dir; - } - std::string dir = std::string(home) + "/.config/" DRAGONX_APP_NAME; - fs::create_directories(dir); - return dir + "/settings.json"; -#endif + return (fs::path(dir) / "settings.json").string(); } bool Settings::load() diff --git a/src/data/address_book.cpp b/src/data/address_book.cpp index dd1a56b..1b06a20 100644 --- a/src/data/address_book.cpp +++ b/src/data/address_book.cpp @@ -10,14 +10,6 @@ #include "../util/logger.h" #include "../util/platform.h" -#include "../config/version.h" // DRAGONX_APP_NAME (per-variant: ObsidianDragon / ObsidianDragonLite) - -#ifdef _WIN32 -#include -#else -#include -#include -#endif namespace fs = std::filesystem; using json = nlohmann::json; @@ -30,36 +22,11 @@ AddressBook::~AddressBook() = default; std::string AddressBook::getDefaultPath() { - // Co-locate with settings.json in the per-variant config dir. Use DRAGONX_APP_NAME - // (not a hardcoded "ObsidianDragon") so the Lite build writes to ObsidianDragonLite/ - // rather than polluting the full-node app's directory — matching Settings::getDefaultPath(). -#ifdef _WIN32 - char path[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, path))) { - std::string dir = std::string(path) + "\\" DRAGONX_APP_NAME; - fs::create_directories(dir); - return dir + "\\addressbook.json"; - } - return "addressbook.json"; -#elif defined(__APPLE__) - const char* home = getenv("HOME"); - if (!home) { - struct passwd* pw = getpwuid(getuid()); - home = pw->pw_dir; - } - std::string dir = std::string(home) + "/Library/Application Support/" DRAGONX_APP_NAME; + // Co-locate with settings.json in the per-variant config dir (Lite -> ObsidianDragonLite/). + // util::Platform::getConfigDir() owns the per-platform + per-variant path in one place. + const std::string dir = util::Platform::getConfigDir(); fs::create_directories(dir); - return dir + "/addressbook.json"; -#else - const char* home = getenv("HOME"); - if (!home) { - struct passwd* pw = getpwuid(getuid()); - home = pw->pw_dir; - } - std::string dir = std::string(home) + "/.config/" DRAGONX_APP_NAME; - fs::create_directories(dir); - return dir + "/addressbook.json"; -#endif + return (fs::path(dir) / "addressbook.json").string(); } bool AddressBook::load() diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 8b2e55b..6ebe8bd 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -245,6 +245,9 @@ std::string Platform::getConfigDir() return std::string(path) + "\\" DRAGONX_APP_NAME "\\"; } return getHomeDir() + "\\AppData\\Roaming\\" DRAGONX_APP_NAME "\\"; +#elif defined(__APPLE__) + // macOS-native config location (matches what settings.json / addressbook.json use). + return getHomeDir() + "/Library/Application Support/" DRAGONX_APP_NAME "/"; #else return getHomeDir() + "/.config/" DRAGONX_APP_NAME "/"; #endif