diff --git a/src/Config.cpp b/src/Config.cpp index 477260c58b..c7120a9fd9 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -33,7 +33,7 @@ Config theConfig; -Config::Config() +void Config::setup(const std::string& strConf) { boost::system::error_code ec; @@ -45,12 +45,21 @@ Config::Config() CONFIG_DIR = boost::filesystem::current_path(); CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME; + DATA_DIR = CONFIG_DIR / "db"; - if (exists(CONFIG_FILE) + if (!strConf.empty()) + { + // --conf= : everything is relative that file. + CONFIG_FILE = strConf; + CONFIG_DIR = CONFIG_FILE; + CONFIG_DIR.remove_filename(); + DATA_DIR = CONFIG_DIR / "db"; + } + else if (exists(CONFIG_FILE) || (!getenv("HOME") && !getenv("XDG_CONFIG_HOME"))) { - // Current place is fine, put dbs in a subdir. - DATA_DIR = CONFIG_DIR / "db"; + // Current working directory is fine, put dbs in a subdir. + nothing(); } else { @@ -66,21 +75,20 @@ Config::Config() strXdgConfigHome = str(boost::format("%s/.config") % strHome); } - CONFIG_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgConfigHome); - - boost::filesystem::create_directories(CONFIG_DIR, ec); - - if (ec) - throw std::runtime_error(str(boost::format("Can not create %s") % CONFIG_DIR)); - if (strXdgDataHome.empty()) { // $XDG_DATA_HOME was not set, use default based on $HOME. strXdgDataHome = str(boost::format("%s/.local/share") % strHome); } + CONFIG_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgConfigHome); + CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME; DATA_DIR = str(boost::format("%s/" SYSTEM_NAME) % strXdgDataHome); + boost::filesystem::create_directories(CONFIG_DIR, ec); + + if (ec) + throw std::runtime_error(str(boost::format("Can not create %s") % CONFIG_DIR)); } boost::filesystem::create_directories(DATA_DIR, ec); @@ -88,6 +96,7 @@ Config::Config() if (ec) throw std::runtime_error(str(boost::format("Can not create %s") % DATA_DIR)); + std::cerr << "CONFIG FILE: " << CONFIG_FILE << std::endl; std::cerr << "CONFIG DIR: " << CONFIG_DIR << std::endl; std::cerr << "DATA DIR: " << DATA_DIR << std::endl; @@ -97,8 +106,6 @@ Config::Config() VERSION = 1; - CONFIG_FILE = CONFIG_DIR / CONFIG_FILE_NAME; - NETWORK_START_TIME = 1319844908; PEER_PORT = SYSTEM_PEER_PORT; @@ -128,15 +135,17 @@ Config::Config() FEE_DEFAULT = DEFAULT_FEE_DEFAULT; ACCOUNT_PROBE_MAX = 10; + + load(); } void Config::load() { - std::ifstream ifsConfig(CONFIG_FILE_NAME, std::ios::in); + std::ifstream ifsConfig(CONFIG_FILE.c_str(), std::ios::in); if (!ifsConfig) { - std::cerr << "Failed to open '" CONFIG_FILE_NAME "'." << std::endl; + std::cerr << "Failed to open '" << CONFIG_FILE << "'." << std::endl; } else { @@ -147,7 +156,7 @@ void Config::load() if (ifsConfig.bad()) { - std::cerr << "Failed to read '" CONFIG_FILE_NAME "'." << std::endl; + std::cerr << "Failed to read '" << CONFIG_FILE << "'." << std::endl; } else { diff --git a/src/Config.h b/src/Config.h index 74bd42e74a..7b754d4a37 100644 --- a/src/Config.h +++ b/src/Config.h @@ -89,8 +89,7 @@ public: // Client behavior int ACCOUNT_PROBE_MAX; // How far to scan for accounts. - Config(); - + void setup(const std::string& strConf); void load(); }; diff --git a/src/main.cpp b/src/main.cpp index 07e1574b88..24f989988f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,7 +82,7 @@ int main(int argc, char* argv[]) po::options_description desc("Options"); desc.add_options() ("help,h", "Display this message.") - ("conf", "Specify the configuration file.") + ("conf", po::value(), "Specify the configuration file.") ("rpc", "Perform rpc command (default).") ("test,t", "Perform unit tests.") ("parameters", po::value< vector >(), "Specify comma separated parameters.") @@ -95,7 +95,6 @@ int main(int argc, char* argv[]) // // Prepare to run // - theConfig.load(); if (!AddSystemEntropy()) { @@ -127,6 +126,11 @@ int main(int argc, char* argv[]) } } + if (!iResult) + { + theConfig.setup(vm.count("conf") ? vm["conf"].as() : ""); + } + if (iResult) { nothing();