Add environment variable override for app data path

Historically, ~/sparkdata on linux, and ~/AppData/Roaming/bts on win.
Now that we may run multiple exes at once (api server, pubsub, sparkd, etc.),
we need to support multiple data dirs on the same host.
This commit is contained in:
John Colvin committed 2025-11-15 12:16:32 -05:00
1 parent b4e26d2fbe
commit 787cf0becd
1 file changed
+18 -4
+18 -4
View File
@@ -4,6 +4,7 @@
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_debugger.h"
#include "spark/utils/helper_macros.h"
#include "spark/utils/string_utils.h"
#include <boost/algorithm/string/replace.hpp>
#include <boost/asio.hpp>
@@ -740,10 +741,23 @@ Path app_data_path() {
if (app_data_path_) {
return *app_data_path_;
}
std::string base_path;
bts_assert(try_expand_env(APP_DATA_DIR, base_path));
app_data_path_ = Path{base_path} / "bts" / process_path().stem();
app_data_path_ = app_data_path_->make_preferred();
std::string path_override_key =
fmt::format("{}__APPDATA", get_application_name());
system::string_utils::to_upper(path_override_key);
const char *path_override = std::getenv(path_override_key.c_str());
if (path_override) {
std::string path_override_str = path_override;
if (path_override_str.ends_with('/')) {
path_override_str =
path_override_str.substr(0, path_override_str.size() - 1);
}
app_data_path_ = path_override_str;
} else {
std::string base_path;
bts_assert(try_expand_env(APP_DATA_DIR, base_path));
app_data_path_ = Path{base_path} / "bts" / process_path().stem();
app_data_path_ = app_data_path_->make_preferred();
}
return *app_data_path_;
}