From 4c7949ff8d00fdc35067554c1fd2c7efed22c78d Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 4 Jun 2018 16:14:38 -0400 Subject: [PATCH] SPARK-1198 Directory Shuffle --- src/lib/config/ConfigurationState.cpp | 32 +++------------------------ src/lib/os/os.cpp | 24 +++++++++----------- src/lib/os/os.h | 7 +++--- src/server/sparkd/http/WebServer.cpp | 27 +++++++++------------- src/server/sparkd/http/WebServer.h | 2 +- 5 files changed, 27 insertions(+), 65 deletions(-) diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index 7e062bae4..dfded1b21 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -110,38 +110,12 @@ string ConfigurationState::settings_path() std::string cmd_arg = get_command_argument("-ConfigServer"); if (!cmd_arg.empty()) return cmd_arg; + // Then the BTSCONFIG environment variable const char* env_server = getenv(CONFIG_SERVER_ENV_VAR); if (env_server) return env_server; - std::string path = os::get_install_path() + "/conf/"; - if (boost::filesystem::exists(path)) - { - return path; - } - else - { - path = os::bts_user_path() + "conf/"; - if (boost::filesystem::exists(path)) - { - return path; - } - else - { - path = os::bts_system_path(); - if (os::is_windows()) path += "conf/"; - if (boost::filesystem::exists(path)) - { - return path; - } - else - { - // Cannot bts_assert during config initialization - const char* error_message = "Local configuration path does not exist. This machine is not configured."; - std::cout << error_message << std::endl; - throw ConfigurationException(error_message); - } - } - } + // then look in sparkdata + return os::sparkdata_path("conf"); } void ConfigurationState::initialize_defaults(IConfigLog& log) diff --git a/src/lib/os/os.cpp b/src/lib/os/os.cpp index 03fa7e667..e1b38d67e 100755 --- a/src/lib/os/os.cpp +++ b/src/lib/os/os.cpp @@ -635,31 +635,27 @@ std::string bts_user_path() { bts_assert(path); return std::string(path) + "/bts/"; #else + if (boost::filesystem::is_directory("/sparkdata")) return "/sparkdata/"; const char *path = getenv("HOME"); bts_assert_msg(path, "Environment variable $HOME must be defined"); - return std::string(path) + "/.bts/"; + return std::string(path) + "/sparkdata/"; #endif } -std::string bts_system_path() { -#ifdef _WIN32 - const char *path = getenv("PROGRAMDATA"); - bts_assert(path); - return std::string(path) + "/bts/"; -#else - return "/etc/bts/"; -#endif -} - -std::string bts_data_path() { - std::string path = bts_user_path() + "data/"; - +std::string sparkdata_path(const std::string& rel) { + std::string path = bts_user_path() + rel; + if (path[path.size()-1] != '/') path += '/'; if (!boost::filesystem::is_directory(path)) { boost::filesystem::create_directories(path); } return normalize_path(path); } +std::string bts_data_path() { + return sparkdata_path("data/"); +} + + std::string username() { #ifdef _WIN32 char buffer[1024]; diff --git a/src/lib/os/os.h b/src/lib/os/os.h index 7a0999a28..7521e0c67 100755 --- a/src/lib/os/os.h +++ b/src/lib/os/os.h @@ -68,10 +68,9 @@ BTS_EXPORT void enable_core_dumps(); extern "C" BTS_EXPORT int get_random_port(); extern "C" BTS_EXPORT int num_cores(); -BTS_EXPORT std::string bts_user_path(); -BTS_EXPORT std::string bts_system_path(); -BTS_EXPORT std::string bts_data_path(); -BTS_EXPORT std::string username(); +std::string sparkdata_path(const std::string&); +std::string bts_data_path(); +std::string username(); BTS_EXPORT char* strncpy(char* dest, const char* src, size_t count); BTS_EXPORT void setenv(const char* name, const char* value); diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index f7c87090c..4f7551443 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -47,20 +47,21 @@ namespace http { WsServer _server; int _port; std::string _www; + std::string _conf; const api::ServiceContext& _ctx; - ServerState(const api::ServiceContext& ctx, int port, const char* web_root) : + ServerState(const api::ServiceContext& ctx, int port) : _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")), _session(), _server(), _port(port), - _www(web_root), + _www(), _ctx(ctx) { - if (_www.empty()) { - _www = std::string(getenv("HOME")) + "/www"; - } + _www = os::sparkdata_path("www"); + _conf = os::sparkdata_path("conf"); + _server.init_asio(_dispatcher.io_service()); _server.set_open_handler(bind(&ServerState::on_open, this, ::_1)); _server.set_close_handler(bind(&ServerState::on_close, this, ::_1)); @@ -112,7 +113,7 @@ namespace http { void return_dotall(connection_ptr& con) { std::vector files; - auto iter = boost::filesystem::directory_iterator(_www + "/config/"); + auto iter = boost::filesystem::directory_iterator(_conf); auto end = boost::filesystem::directory_iterator(); // Collect all .conf file for (; iter != end; ++iter) { @@ -183,21 +184,13 @@ namespace http { boost::filesystem::path map_path_to_filesystem(const std::string& resource) { std::string config = "/config"; std::string files = "/files"; - std::string error_reports = "/error_reports"; if (resource.compare(0, config.size(), config) == 0) { - return _www + resource; + return _conf; } else if (resource.compare(0, files.size(), files) == 0) { return _www + resource.substr(files.size()); } - #ifndef RELEASE - // To enable this in release it probably needs a password - else if (resource.compare(0, error_reports.size(), error_reports) == 0) { - auto errp = boost::filesystem::path(getenv("HOME")) / ".bts/data/error_reports" / resource.substr(error_reports.size()); - if (boost::filesystem::exists(errp)) return errp; - } - #endif return web_ui_path(os::get_install_path()) / resource; } @@ -254,8 +247,8 @@ namespace http { -SparkWebServer::SparkWebServer(const api::ServiceContext& ctx, int port, const char* web_root) : - _me(std::make_shared(ctx, port, web_root)) +SparkWebServer::SparkWebServer(const api::ServiceContext& ctx, int port) : + _me(std::make_shared(ctx, port)) { } diff --git a/src/server/sparkd/http/WebServer.h b/src/server/sparkd/http/WebServer.h index 6e28780d5..6a707c878 100644 --- a/src/server/sparkd/http/WebServer.h +++ b/src/server/sparkd/http/WebServer.h @@ -10,7 +10,7 @@ namespace http { class SparkWebServer { std::shared_ptr _me; public: - SparkWebServer(const api::ServiceContext& ctx, int port, const char* web_root=""); + SparkWebServer(const api::ServiceContext& ctx, int port); }; }} \ No newline at end of file