ConfigFile, CommandLine: use the Path class
This commit is contained in:
@@ -31,7 +31,8 @@
|
||||
#include "PlaylistRegistry.hxx"
|
||||
#include "PlaylistPlugin.hxx"
|
||||
#include "mpd_error.h"
|
||||
#include "glib_compat.h"
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
|
||||
#ifdef ENABLE_ENCODER
|
||||
#include "encoder_list.h"
|
||||
@@ -133,6 +134,16 @@ static void version(void)
|
||||
static const char *summary =
|
||||
"Music Player Daemon - a daemon for playing music.";
|
||||
|
||||
gcc_pure
|
||||
static Path
|
||||
PathBuildChecked(const Path &a, Path::const_pointer b)
|
||||
{
|
||||
if (a.IsNull())
|
||||
return Path::Null();
|
||||
|
||||
return Path::Build(a, b);
|
||||
}
|
||||
|
||||
bool
|
||||
parse_cmdline(int argc, char **argv, struct options *options,
|
||||
GError **error_r)
|
||||
@@ -191,59 +202,44 @@ parse_cmdline(int argc, char **argv, struct options *options,
|
||||
return true;
|
||||
} else if (argc <= 1) {
|
||||
/* default configuration file path */
|
||||
char *path1;
|
||||
|
||||
#ifdef G_OS_WIN32
|
||||
path1 = g_build_filename(g_get_user_config_dir(),
|
||||
CONFIG_FILE_LOCATION, NULL);
|
||||
if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
|
||||
ret = config_read_file(path1, error_r);
|
||||
else {
|
||||
int i = 0;
|
||||
char *system_path = NULL;
|
||||
const char * const *system_config_dirs;
|
||||
Path path = PathBuildChecked(Path::FromUTF8(g_get_user_config_dir()),
|
||||
CONFIG_FILE_LOCATION);
|
||||
if (!path.IsNull() && FileExists(path))
|
||||
return ReadConfigFile(path, error_r);
|
||||
|
||||
system_config_dirs = g_get_system_config_dirs();
|
||||
const char *const*system_config_dirs =
|
||||
g_get_system_config_dirs();
|
||||
|
||||
while(system_config_dirs[i] != NULL) {
|
||||
system_path = g_build_filename(system_config_dirs[i],
|
||||
CONFIG_FILE_LOCATION,
|
||||
NULL);
|
||||
if(g_file_test(system_path,
|
||||
G_FILE_TEST_IS_REGULAR)) {
|
||||
ret = config_read_file(system_path,error_r);
|
||||
g_free(system_path);
|
||||
break;
|
||||
} else
|
||||
g_free(system_path);
|
||||
++i;
|
||||
}
|
||||
for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
|
||||
path = PathBuildChecked(Path::FromUTF8(system_config_dirs[i]),
|
||||
CONFIG_FILE_LOCATION);
|
||||
if (!path.IsNull() && FileExists(path))
|
||||
return ReadConfigFile(path, error_r);
|
||||
}
|
||||
#else /* G_OS_WIN32 */
|
||||
char *path2;
|
||||
path1 = g_build_filename(g_get_home_dir(),
|
||||
USER_CONFIG_FILE_LOCATION1, NULL);
|
||||
path2 = g_build_filename(g_get_home_dir(),
|
||||
USER_CONFIG_FILE_LOCATION2, NULL);
|
||||
if (g_file_test(path1, G_FILE_TEST_IS_REGULAR))
|
||||
ret = config_read_file(path1, error_r);
|
||||
else if (g_file_test(path2, G_FILE_TEST_IS_REGULAR))
|
||||
ret = config_read_file(path2, error_r);
|
||||
else if (g_file_test(SYSTEM_CONFIG_FILE_LOCATION,
|
||||
G_FILE_TEST_IS_REGULAR))
|
||||
ret = config_read_file(SYSTEM_CONFIG_FILE_LOCATION,
|
||||
error_r);
|
||||
Path path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
|
||||
USER_CONFIG_FILE_LOCATION1);
|
||||
if (!path.IsNull() && FileExists(path))
|
||||
return ReadConfigFile(path, error_r);
|
||||
|
||||
path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
|
||||
USER_CONFIG_FILE_LOCATION2);
|
||||
if (!path.IsNull() && FileExists(path))
|
||||
return ReadConfigFile(path, error_r);
|
||||
|
||||
path = Path::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
|
||||
if (!path.IsNull() && FileExists(path))
|
||||
return ReadConfigFile(path, error_r);
|
||||
#endif
|
||||
|
||||
g_free(path1);
|
||||
#ifndef G_OS_WIN32
|
||||
g_free(path2);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
g_set_error(error_r, cmdline_quark(), 0,
|
||||
"No configuration file found");
|
||||
return false;
|
||||
} else if (argc == 2) {
|
||||
/* specified configuration file */
|
||||
return config_read_file(argv[1], error_r);
|
||||
return ReadConfigFile(Path::FromFS(argv[1]), error_r);
|
||||
} else {
|
||||
g_set_error(error_r, cmdline_quark(), 0,
|
||||
"too many arguments");
|
||||
|
@@ -27,6 +27,7 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "mpd_error.h"
|
||||
|
||||
#include <glib.h>
|
||||
@@ -347,20 +348,23 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
|
||||
}
|
||||
|
||||
bool
|
||||
config_read_file(const char *file, GError **error_r)
|
||||
ReadConfigFile(const Path &path, GError **error_r)
|
||||
{
|
||||
assert(!path.IsNull());
|
||||
const std::string path_utf8 = path.ToUTF8();
|
||||
|
||||
FILE *fp;
|
||||
char string[MAX_STRING_SIZE + 1];
|
||||
int count = 0;
|
||||
struct config_entry *entry;
|
||||
struct config_param *param;
|
||||
|
||||
g_debug("loading file %s", file);
|
||||
g_debug("loading file %s", path_utf8.c_str());
|
||||
|
||||
if (!(fp = fopen(file, "r"))) {
|
||||
if (!(fp = FOpen(path, "r"))) {
|
||||
g_set_error(error_r, config_quark(), errno,
|
||||
"Failed to open %s: %s",
|
||||
file, g_strerror(errno));
|
||||
path_utf8.c_str(), g_strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -31,6 +31,7 @@
|
||||
#include "conf.h"
|
||||
#include "Idle.hxx"
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
|
||||
extern "C" {
|
||||
#include "uri.h"
|
||||
@@ -433,14 +434,14 @@ static bool
|
||||
spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs,
|
||||
GError **error_r)
|
||||
{
|
||||
if (!g_file_test(from_path_fs.c_str(), G_FILE_TEST_IS_REGULAR)) {
|
||||
if (!FileExists(from_path_fs)) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_NO_SUCH_LIST,
|
||||
"No such playlist");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (g_file_test(to_path_fs.c_str(), G_FILE_TEST_EXISTS)) {
|
||||
if (FileExists(to_path_fs)) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_LIST_EXISTS,
|
||||
"Playlist exists already");
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "Mapper.hxx"
|
||||
#include "Idle.hxx"
|
||||
#include "fs/Path.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
|
||||
extern "C" {
|
||||
#include "uri.h"
|
||||
@@ -76,7 +77,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue)
|
||||
if (path_fs.IsNull())
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
|
||||
if (g_file_test(path_fs.c_str(), G_FILE_TEST_EXISTS))
|
||||
if (FileExists(path_fs))
|
||||
return PLAYLIST_RESULT_LIST_EXISTS;
|
||||
|
||||
FILE *file = fopen(path_fs.c_str(), "w");
|
||||
|
@@ -22,7 +22,7 @@
|
||||
#include "Directory.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "fs/Path.hxx"
|
||||
#include "glib_compat.h"
|
||||
#include "fs/FileSystem.hxx"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
@@ -68,12 +68,10 @@ directory_exists(const Directory *directory)
|
||||
/* invalid path: cannot exist */
|
||||
return false;
|
||||
|
||||
GFileTest test = directory->device == DEVICE_INARCHIVE ||
|
||||
return directory->device == DEVICE_INARCHIVE ||
|
||||
directory->device == DEVICE_CONTAINER
|
||||
? G_FILE_TEST_IS_REGULAR
|
||||
: G_FILE_TEST_IS_DIR;
|
||||
|
||||
return g_file_test(path_fs.c_str(), test);
|
||||
? FileExists(path_fs)
|
||||
: DirectoryExists(path_fs);
|
||||
}
|
||||
|
||||
bool
|
||||
|
14
src/conf.h
14
src/conf.h
@@ -81,6 +81,10 @@
|
||||
|
||||
#define MAX_FILTER_CHAIN_LENGTH 255
|
||||
|
||||
#ifdef __cplusplus
|
||||
class Path;
|
||||
#endif
|
||||
|
||||
struct block_param {
|
||||
char *name;
|
||||
char *value;
|
||||
@@ -118,8 +122,6 @@ config_quark(void)
|
||||
return g_quark_from_static_string("config");
|
||||
}
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
void config_global_init(void);
|
||||
void config_global_finish(void);
|
||||
|
||||
@@ -129,8 +131,14 @@ void config_global_finish(void);
|
||||
*/
|
||||
void config_global_check(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
bool
|
||||
config_read_file(const char *file, GError **error_r);
|
||||
ReadConfigFile(const Path &path, GError **error_r);
|
||||
|
||||
#endif
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
/* don't free the returned value
|
||||
set _last_ to NULL to get first entry */
|
||||
|
Reference in New Issue
Block a user