diff --git a/daemon/main.cpp b/daemon/main.cpp index 0e2b9261d..c1ea97310 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -472,28 +472,47 @@ lokinet_main(int argc, char* argv[]) { // when we have an explicit filepath fs::path basedir = configFile->parent_path(); - if (genconfigOnly) { - llarp::ensureConfig(basedir, *configFile, overwrite, opts.isRouter); + try + { + llarp::ensureConfig(basedir, *configFile, overwrite, opts.isRouter); + } + catch (std::exception& ex) + { + LogError("cannot generate config at ", *configFile, ": ", ex.what()); + return 1; + } } else { - std::error_code ec; - if (!fs::exists(*configFile, ec)) + try { - llarp::LogError("Config file not found ", *configFile); + if (!fs::exists(*configFile)) + { + llarp::LogError("Config file not found ", *configFile); + return 1; + } + } + catch (std::exception& ex) + { + LogError("cannot check if ", *configFile, " exists: ", ex.what()); return 1; } - - if (ec) - throw std::runtime_error(llarp::stringify("filesystem error: ", ec)); } } else { - llarp::ensureConfig( - llarp::GetDefaultDataDir(), llarp::GetDefaultConfigPath(), overwrite, opts.isRouter); + try + { + llarp::ensureConfig( + llarp::GetDefaultDataDir(), llarp::GetDefaultConfigPath(), overwrite, opts.isRouter); + } + catch (std::exception& ex) + { + llarp::LogError("cannot ensure config: ", ex.what()); + return 1; + } configFile = llarp::GetDefaultConfigPath(); } diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 171bf01a6..58abb8eee 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -107,6 +107,8 @@ namespace llarp "private keys.", }, [this](fs::path arg) { + if (arg.empty()) + throw std::invalid_argument("[router]:data-dir is empty"); if (not fs::exists(arg)) throw std::runtime_error( stringify("Specified [router]:data-dir ", arg, " does not exist")); @@ -981,7 +983,8 @@ namespace llarp }); } - Config::Config(fs::path datadir) : m_DataDir(std::move(datadir)) + Config::Config(fs::path datadir) + : m_DataDir(datadir.empty() ? fs::current_path() : std::move(datadir)) {} constexpr auto GetOverridesDir = [](auto datadir) -> fs::path { return datadir / "conf.d"; }; @@ -1117,33 +1120,30 @@ namespace llarp } void - ensureConfig( - const fs::path& defaultDataDir, const fs::path& confFile, bool overwrite, bool asRouter) + ensureConfig(fs::path dataDir, fs::path confFile, bool overwrite, bool asRouter) { - std::error_code ec; - // fail to overwrite if not instructed to do so - if (fs::exists(confFile, ec) && !overwrite) + if (fs::exists(confFile) && !overwrite) { LogDebug("Not creating config file; it already exists."); return; } - if (ec) - throw std::runtime_error(stringify("filesystem error: ", ec)); + const auto parent = confFile.parent_path(); // create parent dir if it doesn't exist - if (not fs::exists(confFile.parent_path(), ec)) + if ((not parent.empty()) and (not fs::exists(parent))) { - if (not fs::create_directory(confFile.parent_path())) - throw std::runtime_error(stringify("Failed to create parent directory for ", confFile)); + fs::create_directory(parent); } - if (ec) - throw std::runtime_error(stringify("filesystem error: ", ec)); - llarp::LogInfo("Attempting to create config file, asRouter: ", asRouter, " path: ", confFile); + llarp::LogInfo( + "Attempting to create config file for ", + (asRouter ? "router" : "client"), + " at ", + confFile); - llarp::Config config{defaultDataDir}; + llarp::Config config{dataDir}; std::string confStr; if (asRouter) confStr = config.generateBaseRouterConfig(); diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 1d074872e..4f822a9d5 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -247,8 +247,7 @@ namespace llarp }; void - ensureConfig( - const fs::path& defaultDataDir, const fs::path& confFile, bool overwrite, bool asRouter); + ensureConfig(fs::path dataDir, fs::path confFile, bool overwrite, bool asRouter); } // namespace llarp