config/Global: _get_path() throws exception on error

This commit is contained in:
Max Kellermann 2016-11-02 10:01:29 +01:00
parent 5a63d1d5a4
commit d765182bbb
4 changed files with 25 additions and 60 deletions

View File

@ -129,34 +129,23 @@ Instance *instance;
#ifdef ENABLE_DAEMON #ifdef ENABLE_DAEMON
static bool static void
glue_daemonize_init(const struct options *options, Error &error) glue_daemonize_init(const struct options *options)
{ {
auto pid_file = config_get_path(ConfigOption::PID_FILE, error);
if (pid_file.IsNull() && error.IsDefined())
return false;
daemonize_init(config_get_string(ConfigOption::USER, nullptr), daemonize_init(config_get_string(ConfigOption::USER, nullptr),
config_get_string(ConfigOption::GROUP, nullptr), config_get_string(ConfigOption::GROUP, nullptr),
std::move(pid_file)); config_get_path(ConfigOption::PID_FILE));
if (options->kill) if (options->kill)
daemonize_kill(); daemonize_kill();
return true;
} }
#endif #endif
static bool static void
glue_mapper_init(Error &error) glue_mapper_init()
{ {
auto playlist_dir = config_get_path(ConfigOption::PLAYLIST_DIR, error); mapper_init(config_get_path(ConfigOption::PLAYLIST_DIR));
if (playlist_dir.IsNull() && error.IsDefined())
return false;
mapper_init(std::move(playlist_dir));
return true;
} }
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
@ -232,37 +221,30 @@ InitDatabaseAndStorage()
* Configure and initialize the sticker subsystem. * Configure and initialize the sticker subsystem.
*/ */
static void static void
glue_sticker_init(void) glue_sticker_init()
{ {
#ifdef ENABLE_SQLITE #ifdef ENABLE_SQLITE
Error error; auto sticker_file = config_get_path(ConfigOption::STICKER_FILE);
auto sticker_file = config_get_path(ConfigOption::STICKER_FILE, error); if (sticker_file.IsNull())
if (sticker_file.IsNull()) {
if (error.IsDefined())
FatalError(error);
return; return;
}
sticker_global_init(std::move(sticker_file)); sticker_global_init(std::move(sticker_file));
#endif #endif
} }
static bool static void
glue_state_file_init(Error &error) glue_state_file_init()
{ {
auto path_fs = config_get_path(ConfigOption::STATE_FILE, error); auto path_fs = config_get_path(ConfigOption::STATE_FILE);
if (path_fs.IsNull()) { if (path_fs.IsNull()) {
if (error.IsDefined())
return false;
#ifdef ANDROID #ifdef ANDROID
const auto cache_dir = GetUserCacheDir(); const auto cache_dir = GetUserCacheDir();
if (cache_dir.IsNull()) if (cache_dir.IsNull())
return true; return;
path_fs = AllocatedPath::Build(cache_dir, "state"); path_fs = AllocatedPath::Build(cache_dir, "state");
#else #else
return true; return;
#endif #endif
} }
@ -274,7 +256,6 @@ glue_state_file_init(Error &error)
*instance->partition, *instance->partition,
instance->event_loop); instance->event_loop);
instance->state_file->Read(); instance->state_file->Read();
return true;
} }
/** /**
@ -425,10 +406,7 @@ try {
#endif #endif
#ifdef ENABLE_DAEMON #ifdef ENABLE_DAEMON
if (!glue_daemonize_init(&options, error)) { glue_daemonize_init(&options);
LogError(error);
return EXIT_FAILURE;
}
#endif #endif
stats_global_init(); stats_global_init();
@ -490,10 +468,7 @@ try {
ConfigureFS(); ConfigureFS();
if (!glue_mapper_init(error)) { glue_mapper_init();
LogError(error);
return EXIT_FAILURE;
}
initPermissions(); initPermissions();
spl_global_init(); spl_global_init();
@ -551,10 +526,7 @@ try {
} }
#endif #endif
if (!glue_state_file_init(error)) { glue_state_file_init();
LogError(error);
return EXIT_FAILURE;
}
instance->partition->outputs.SetReplayGainMode(replay_gain_get_real_mode(instance->partition->playlist.queue.random)); instance->partition->outputs.SetReplayGainMode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));

View File

@ -28,7 +28,6 @@
#include "ConfigError.hxx" #include "ConfigError.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "util/Error.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -122,13 +121,13 @@ config_get_string(ConfigOption option, const char *default_value)
} }
AllocatedPath AllocatedPath
config_get_path(ConfigOption option, Error &error) config_get_path(ConfigOption option)
{ {
const auto *param = config_get_param(option); const auto *param = config_get_param(option);
if (param == nullptr) if (param == nullptr)
return AllocatedPath::Null(); return AllocatedPath::Null();
return param->GetPath(error); return param->GetPath();
} }
unsigned unsigned

View File

@ -23,7 +23,6 @@
#include "ConfigOption.hxx" #include "ConfigOption.hxx"
#include "Compiler.h" #include "Compiler.h"
class Error;
class Path; class Path;
class AllocatedPath; class AllocatedPath;
struct ConfigParam; struct ConfigParam;
@ -78,11 +77,12 @@ config_get_string(enum ConfigOption option, const char *default_value=nullptr);
/** /**
* Returns an optional configuration variable which contains an * Returns an optional configuration variable which contains an
* absolute path. If there is a tilde prefix, it is expanded. * absolute path. If there is a tilde prefix, it is expanded.
* Returns AllocatedPath::Null() if the value is not present. If the path * Returns AllocatedPath::Null() if the value is not present.
* could not be parsed, returns AllocatedPath::Null() and sets the error. *
* Throws #std::runtime_error on error.
*/ */
AllocatedPath AllocatedPath
config_get_path(enum ConfigOption option, Error &error); config_get_path(enum ConfigOption option);
gcc_pure gcc_pure
unsigned unsigned

View File

@ -26,7 +26,6 @@
#include "fs/StandardDirectory.hxx" #include "fs/StandardDirectory.hxx"
#include "fs/CheckFile.hxx" #include "fs/CheckFile.hxx"
#include "util/UriUtil.hxx" #include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include <assert.h> #include <assert.h>
@ -44,14 +43,9 @@ CreateConfiguredStorageUri(EventLoop &event_loop, const char *uri)
static AllocatedPath static AllocatedPath
GetConfiguredMusicDirectory() GetConfiguredMusicDirectory()
{ {
Error error; AllocatedPath path = config_get_path(ConfigOption::MUSIC_DIR);
AllocatedPath path = config_get_path(ConfigOption::MUSIC_DIR, error); if (path.IsNull())
if (path.IsNull()) {
if (error.IsDefined())
throw std::runtime_error(error.GetMessage());
path = GetUserMusicDir(); path = GetUserMusicDir();
}
return path; return path;
} }