refactor(audit): unify config-dir resolution through Platform::getConfigDir

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/<AppName>.

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) <noreply@anthropic.com>
This commit is contained in:
2026-07-02 20:49:24 -05:00
parent 121a79e768
commit cb74fbbff0
3 changed files with 11 additions and 63 deletions

View File

@@ -110,33 +110,11 @@ void loadClamped(const json& j, const char* key, T& field, T lo, T hi)
std::string Settings::getDefaultPath() std::string Settings::getDefaultPath()
{ {
#ifdef _WIN32 // Single per-platform, per-variant config dir (util::Platform::getConfigDir handles the
char path[MAX_PATH]; // _WIN32 / __APPLE__ / XDG split and the DRAGONX_APP_NAME variant suffix in one place).
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, path))) { const std::string dir = util::Platform::getConfigDir();
std::string dir = std::string(path) + "\\" DRAGONX_APP_NAME;
fs::create_directories(dir); fs::create_directories(dir);
return dir + "\\settings.json"; return (fs::path(dir) / "settings.json").string();
}
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;
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
} }
bool Settings::load() bool Settings::load()

View File

@@ -10,14 +10,6 @@
#include "../util/logger.h" #include "../util/logger.h"
#include "../util/platform.h" #include "../util/platform.h"
#include "../config/version.h" // DRAGONX_APP_NAME (per-variant: ObsidianDragon / ObsidianDragonLite)
#ifdef _WIN32
#include <shlobj.h>
#else
#include <pwd.h>
#include <unistd.h>
#endif
namespace fs = std::filesystem; namespace fs = std::filesystem;
using json = nlohmann::json; using json = nlohmann::json;
@@ -30,36 +22,11 @@ AddressBook::~AddressBook() = default;
std::string AddressBook::getDefaultPath() std::string AddressBook::getDefaultPath()
{ {
// Co-locate with settings.json in the per-variant config dir. Use DRAGONX_APP_NAME // Co-locate with settings.json in the per-variant config dir (Lite -> ObsidianDragonLite/).
// (not a hardcoded "ObsidianDragon") so the Lite build writes to ObsidianDragonLite/ // util::Platform::getConfigDir() owns the per-platform + per-variant path in one place.
// rather than polluting the full-node app's directory — matching Settings::getDefaultPath(). const std::string dir = util::Platform::getConfigDir();
#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); fs::create_directories(dir);
return dir + "\\addressbook.json"; return (fs::path(dir) / "addressbook.json").string();
}
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;
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
} }
bool AddressBook::load() bool AddressBook::load()

View File

@@ -245,6 +245,9 @@ std::string Platform::getConfigDir()
return std::string(path) + "\\" DRAGONX_APP_NAME "\\"; return std::string(path) + "\\" DRAGONX_APP_NAME "\\";
} }
return getHomeDir() + "\\AppData\\Roaming\\" 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 #else
return getHomeDir() + "/.config/" DRAGONX_APP_NAME "/"; return getHomeDir() + "/.config/" DRAGONX_APP_NAME "/";
#endif #endif