allow for relative file paths in config (#1432)

* llarp::Config constructor can take an empty fs::path now and it will default to fs::current_path
* llarp::ensureConfig now throws on fs error
* updates to daemon/main.cpp to reflect changes to llarp::ensureConfig's new throwability
* dowse parts of the code with holy water for the 0.8.1 tag
pull/1436/head
Jeff 4 years ago committed by GitHub
parent 569bfe14b4
commit 0e1e0aaef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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();
}

@ -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();

@ -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

Loading…
Cancel
Save