Commit feb8d5b8 authored by Max Kellermann's avatar Max Kellermann

ConfigFile, CommandLine: use the Path class

parent eb8922f3
...@@ -1044,6 +1044,7 @@ noinst_PROGRAMS += test/read_mixer ...@@ -1044,6 +1044,7 @@ noinst_PROGRAMS += test/read_mixer
endif endif
test_read_conf_LDADD = \ test_read_conf_LDADD = \
libfs.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_conf_SOURCES = test/read_conf.cxx \ test_read_conf_SOURCES = test/read_conf.cxx \
src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c
...@@ -1074,6 +1075,7 @@ test_run_input_LDADD = \ ...@@ -1074,6 +1075,7 @@ test_run_input_LDADD = \
$(INPUT_LIBS) \ $(INPUT_LIBS) \
$(ARCHIVE_LIBS) \ $(ARCHIVE_LIBS) \
libevent.a \ libevent.a \
libfs.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_input_SOURCES = test/run_input.cxx \ test_run_input_SOURCES = test/run_input.cxx \
test/stdbin.h \ test/stdbin.h \
...@@ -1087,6 +1089,7 @@ test_dump_text_file_LDADD = \ ...@@ -1087,6 +1089,7 @@ test_dump_text_file_LDADD = \
$(INPUT_LIBS) \ $(INPUT_LIBS) \
$(ARCHIVE_LIBS) \ $(ARCHIVE_LIBS) \
libevent.a \ libevent.a \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_dump_text_file_SOURCES = test/dump_text_file.cxx \ test_dump_text_file_SOURCES = test/dump_text_file.cxx \
...@@ -1106,6 +1109,7 @@ test_dump_playlist_LDADD = \ ...@@ -1106,6 +1109,7 @@ test_dump_playlist_LDADD = \
$(DECODER_LIBS) \ $(DECODER_LIBS) \
$(TAG_LIBS) \ $(TAG_LIBS) \
libevent.a \ libevent.a \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_dump_playlist_SOURCES = test/dump_playlist.cxx \ test_dump_playlist_SOURCES = test/dump_playlist.cxx \
...@@ -1133,6 +1137,7 @@ test_run_decoder_LDADD = \ ...@@ -1133,6 +1137,7 @@ test_run_decoder_LDADD = \
$(ARCHIVE_LIBS) \ $(ARCHIVE_LIBS) \
$(TAG_LIBS) \ $(TAG_LIBS) \
libevent.a \ libevent.a \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_decoder_SOURCES = test/run_decoder.cxx \ test_run_decoder_SOURCES = test/run_decoder.cxx \
...@@ -1157,6 +1162,7 @@ test_read_tags_LDADD = \ ...@@ -1157,6 +1162,7 @@ test_read_tags_LDADD = \
$(ARCHIVE_LIBS) \ $(ARCHIVE_LIBS) \
$(TAG_LIBS) \ $(TAG_LIBS) \
libevent.a \ libevent.a \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_tags_SOURCES = test/read_tags.cxx \ test_read_tags_SOURCES = test/read_tags.cxx \
...@@ -1182,6 +1188,7 @@ endif ...@@ -1182,6 +1188,7 @@ endif
test_run_filter_LDADD = \ test_run_filter_LDADD = \
$(FILTER_LIBS) \ $(FILTER_LIBS) \
libfs.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_filter_SOURCES = test/run_filter.cxx \ test_run_filter_SOURCES = test/run_filter.cxx \
test/FakeReplayGainConfig.cxx \ test/FakeReplayGainConfig.cxx \
...@@ -1216,6 +1223,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \ ...@@ -1216,6 +1223,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \
test_run_encoder_LDADD = \ test_run_encoder_LDADD = \
$(ENCODER_LIBS) \ $(ENCODER_LIBS) \
libpcm.a \ libpcm.a \
libfs.a \
libutil.a \ libutil.a \
$(TAG_LIBS) \ $(TAG_LIBS) \
$(GLIB_LIBS) $(GLIB_LIBS)
...@@ -1238,6 +1246,7 @@ test_test_vorbis_encoder_CPPFLAGS = $(AM_CPPFLAGS) \ ...@@ -1238,6 +1246,7 @@ test_test_vorbis_encoder_CPPFLAGS = $(AM_CPPFLAGS) \
$(ENCODER_CFLAGS) $(ENCODER_CFLAGS)
test_test_vorbis_encoder_LDADD = $(MPD_LIBS) \ test_test_vorbis_encoder_LDADD = $(MPD_LIBS) \
$(ENCODER_LIBS) \ $(ENCODER_LIBS) \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
endif endif
...@@ -1274,6 +1283,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ ...@@ -1274,6 +1283,7 @@ test_run_output_LDADD = $(MPD_LIBS) \
libmixer_plugins.a \ libmixer_plugins.a \
$(FILTER_LIBS) \ $(FILTER_LIBS) \
libevent.a \ libevent.a \
libfs.a \
libutil.a \ libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_output_SOURCES = test/run_output.cxx \ test_run_output_SOURCES = test/run_output.cxx \
...@@ -1306,6 +1316,7 @@ test_read_mixer_LDADD = \ ...@@ -1306,6 +1316,7 @@ test_read_mixer_LDADD = \
libmixer_plugins.a \ libmixer_plugins.a \
$(OUTPUT_LIBS) \ $(OUTPUT_LIBS) \
libevent.a \ libevent.a \
libfs.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_read_mixer_SOURCES = test/read_mixer.c \ test_read_mixer_SOURCES = test/read_mixer.c \
src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c \ src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c \
......
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
#include "PlaylistRegistry.hxx" #include "PlaylistRegistry.hxx"
#include "PlaylistPlugin.hxx" #include "PlaylistPlugin.hxx"
#include "mpd_error.h" #include "mpd_error.h"
#include "glib_compat.h" #include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
#ifdef ENABLE_ENCODER #ifdef ENABLE_ENCODER
#include "encoder_list.h" #include "encoder_list.h"
...@@ -133,6 +134,16 @@ static void version(void) ...@@ -133,6 +134,16 @@ static void version(void)
static const char *summary = static const char *summary =
"Music Player Daemon - a daemon for playing music."; "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 bool
parse_cmdline(int argc, char **argv, struct options *options, parse_cmdline(int argc, char **argv, struct options *options,
GError **error_r) GError **error_r)
...@@ -191,59 +202,44 @@ parse_cmdline(int argc, char **argv, struct options *options, ...@@ -191,59 +202,44 @@ parse_cmdline(int argc, char **argv, struct options *options,
return true; return true;
} else if (argc <= 1) { } else if (argc <= 1) {
/* default configuration file path */ /* default configuration file path */
char *path1;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
path1 = g_build_filename(g_get_user_config_dir(), Path path = PathBuildChecked(Path::FromUTF8(g_get_user_config_dir()),
CONFIG_FILE_LOCATION, NULL); CONFIG_FILE_LOCATION);
if (g_file_test(path1, G_FILE_TEST_IS_REGULAR)) if (!path.IsNull() && FileExists(path))
ret = config_read_file(path1, error_r); return ReadConfigFile(path, error_r);
else {
int i = 0; const char *const*system_config_dirs =
char *system_path = NULL; g_get_system_config_dirs();
const char * const *system_config_dirs;
for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
system_config_dirs = g_get_system_config_dirs(); path = PathBuildChecked(Path::FromUTF8(system_config_dirs[i]),
CONFIG_FILE_LOCATION);
while(system_config_dirs[i] != NULL) { if (!path.IsNull() && FileExists(path))
system_path = g_build_filename(system_config_dirs[i], return ReadConfigFile(path, error_r);
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;
}
} }
#else /* G_OS_WIN32 */ #else /* G_OS_WIN32 */
char *path2; Path path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
path1 = g_build_filename(g_get_home_dir(), USER_CONFIG_FILE_LOCATION1);
USER_CONFIG_FILE_LOCATION1, NULL); if (!path.IsNull() && FileExists(path))
path2 = g_build_filename(g_get_home_dir(), return ReadConfigFile(path, error_r);
USER_CONFIG_FILE_LOCATION2, NULL);
if (g_file_test(path1, G_FILE_TEST_IS_REGULAR)) path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()),
ret = config_read_file(path1, error_r); USER_CONFIG_FILE_LOCATION2);
else if (g_file_test(path2, G_FILE_TEST_IS_REGULAR)) if (!path.IsNull() && FileExists(path))
ret = config_read_file(path2, error_r); return ReadConfigFile(path, error_r);
else if (g_file_test(SYSTEM_CONFIG_FILE_LOCATION,
G_FILE_TEST_IS_REGULAR)) path = Path::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
ret = config_read_file(SYSTEM_CONFIG_FILE_LOCATION, if (!path.IsNull() && FileExists(path))
error_r); return ReadConfigFile(path, error_r);
#endif #endif
g_free(path1); g_set_error(error_r, cmdline_quark(), 0,
#ifndef G_OS_WIN32 "No configuration file found");
g_free(path2); return false;
#endif
return ret;
} else if (argc == 2) { } else if (argc == 2) {
/* specified configuration file */ /* specified configuration file */
return config_read_file(argv[1], error_r); return ReadConfigFile(Path::FromFS(argv[1]), error_r);
} else { } else {
g_set_error(error_r, cmdline_quark(), 0, g_set_error(error_r, cmdline_quark(), 0,
"too many arguments"); "too many arguments");
......
...@@ -27,6 +27,7 @@ extern "C" { ...@@ -27,6 +27,7 @@ extern "C" {
} }
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
#include "mpd_error.h" #include "mpd_error.h"
#include <glib.h> #include <glib.h>
...@@ -347,20 +348,23 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) ...@@ -347,20 +348,23 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
} }
bool 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; FILE *fp;
char string[MAX_STRING_SIZE + 1]; char string[MAX_STRING_SIZE + 1];
int count = 0; int count = 0;
struct config_entry *entry; struct config_entry *entry;
struct config_param *param; 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, g_set_error(error_r, config_quark(), errno,
"Failed to open %s: %s", "Failed to open %s: %s",
file, g_strerror(errno)); path_utf8.c_str(), g_strerror(errno));
return false; return false;
} }
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "conf.h" #include "conf.h"
#include "Idle.hxx" #include "Idle.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
extern "C" { extern "C" {
#include "uri.h" #include "uri.h"
...@@ -433,14 +434,14 @@ static bool ...@@ -433,14 +434,14 @@ static bool
spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs, spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs,
GError **error_r) 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(), g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_NO_SUCH_LIST, PLAYLIST_RESULT_NO_SUCH_LIST,
"No such playlist"); "No such playlist");
return false; 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(), g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_LIST_EXISTS, PLAYLIST_RESULT_LIST_EXISTS,
"Playlist exists already"); "Playlist exists already");
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "Mapper.hxx" #include "Mapper.hxx"
#include "Idle.hxx" #include "Idle.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "fs/FileSystem.hxx"
extern "C" { extern "C" {
#include "uri.h" #include "uri.h"
...@@ -76,7 +77,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue) ...@@ -76,7 +77,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue)
if (path_fs.IsNull()) if (path_fs.IsNull())
return PLAYLIST_RESULT_BAD_NAME; 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; return PLAYLIST_RESULT_LIST_EXISTS;
FILE *file = fopen(path_fs.c_str(), "w"); FILE *file = fopen(path_fs.c_str(), "w");
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "Directory.hxx" #include "Directory.hxx"
#include "Mapper.hxx" #include "Mapper.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "glib_compat.h" #include "fs/FileSystem.hxx"
#include <glib.h> #include <glib.h>
...@@ -68,12 +68,10 @@ directory_exists(const Directory *directory) ...@@ -68,12 +68,10 @@ directory_exists(const Directory *directory)
/* invalid path: cannot exist */ /* invalid path: cannot exist */
return false; return false;
GFileTest test = directory->device == DEVICE_INARCHIVE || return directory->device == DEVICE_INARCHIVE ||
directory->device == DEVICE_CONTAINER directory->device == DEVICE_CONTAINER
? G_FILE_TEST_IS_REGULAR ? FileExists(path_fs)
: G_FILE_TEST_IS_DIR; : DirectoryExists(path_fs);
return g_file_test(path_fs.c_str(), test);
} }
bool bool
......
...@@ -81,6 +81,10 @@ ...@@ -81,6 +81,10 @@
#define MAX_FILTER_CHAIN_LENGTH 255 #define MAX_FILTER_CHAIN_LENGTH 255
#ifdef __cplusplus
class Path;
#endif
struct block_param { struct block_param {
char *name; char *name;
char *value; char *value;
...@@ -118,8 +122,6 @@ config_quark(void) ...@@ -118,8 +122,6 @@ config_quark(void)
return g_quark_from_static_string("config"); return g_quark_from_static_string("config");
} }
G_BEGIN_DECLS
void config_global_init(void); void config_global_init(void);
void config_global_finish(void); void config_global_finish(void);
...@@ -129,8 +131,14 @@ void config_global_finish(void); ...@@ -129,8 +131,14 @@ void config_global_finish(void);
*/ */
void config_global_check(void); void config_global_check(void);
#ifdef __cplusplus
bool 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 /* don't free the returned value
set _last_ to NULL to get first entry */ set _last_ to NULL to get first entry */
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "PlaylistVector.hxx" #include "PlaylistVector.hxx"
#include "conf.h" #include "conf.h"
#include "tag.h" #include "tag.h"
#include "fs/Path.hxx"
#include <iostream> #include <iostream>
using std::cout; using std::cout;
...@@ -76,7 +77,7 @@ main(int argc, char **argv) ...@@ -76,7 +77,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
const char *const config_path = argv[1]; const Path config_path = Path::FromFS(argv[1]);
const char *const plugin_name = argv[2]; const char *const plugin_name = argv[2];
const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
...@@ -94,7 +95,7 @@ main(int argc, char **argv) ...@@ -94,7 +95,7 @@ main(int argc, char **argv)
config_global_init(); config_global_init();
if (!config_read_file(config_path, &error)) { if (!ReadConfigFile(config_path, &error)) {
cerr << error->message << endl; cerr << error->message << endl;
g_error_free(error); g_error_free(error);
return EXIT_FAILURE; return EXIT_FAILURE;
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "IOThread.hxx" #include "IOThread.hxx"
#include "PlaylistRegistry.hxx" #include "PlaylistRegistry.hxx"
#include "PlaylistPlugin.hxx" #include "PlaylistPlugin.hxx"
#include "fs/Path.hxx"
extern "C" { extern "C" {
#include "decoder_list.h" #include "decoder_list.h"
...@@ -141,7 +142,6 @@ int main(int argc, char **argv) ...@@ -141,7 +142,6 @@ int main(int argc, char **argv)
{ {
const char *uri; const char *uri;
struct input_stream *is = NULL; struct input_stream *is = NULL;
bool success;
GError *error = NULL; GError *error = NULL;
struct playlist_provider *playlist; struct playlist_provider *playlist;
struct song *song; struct song *song;
...@@ -151,6 +151,7 @@ int main(int argc, char **argv) ...@@ -151,6 +151,7 @@ int main(int argc, char **argv)
return 1; return 1;
} }
const Path config_path = Path::FromFS(argv[1]);
uri = argv[2]; uri = argv[2];
/* initialize GLib */ /* initialize GLib */
...@@ -161,8 +162,7 @@ int main(int argc, char **argv) ...@@ -161,8 +162,7 @@ int main(int argc, char **argv)
/* initialize MPD */ /* initialize MPD */
config_global_init(); config_global_init();
success = config_read_file(argv[1], &error); if (!ReadConfigFile(config_path, &error)) {
if (!success) {
g_printerr("%s\n", error->message); g_printerr("%s\n", error->message);
g_error_free(error); g_error_free(error);
return 1; return 1;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "conf.h" #include "conf.h"
#include "fs/Path.hxx"
#include <glib.h> #include <glib.h>
...@@ -42,7 +43,7 @@ int main(int argc, char **argv) ...@@ -42,7 +43,7 @@ int main(int argc, char **argv)
return 1; return 1;
} }
const char *path = argv[1]; const Path config_path = Path::FromFS(argv[1]);
const char *name = argv[2]; const char *name = argv[2];
g_log_set_default_handler(my_log_func, NULL); g_log_set_default_handler(my_log_func, NULL);
...@@ -50,8 +51,7 @@ int main(int argc, char **argv) ...@@ -50,8 +51,7 @@ int main(int argc, char **argv)
config_global_init(); config_global_init();
GError *error = NULL; GError *error = NULL;
bool success = config_read_file(path, &error); if (!ReadConfigFile(config_path, &error)) {
if (!success) {
g_printerr("%s:", error->message); g_printerr("%s:", error->message);
g_error_free(error); g_error_free(error);
return 1; return 1;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "conf.h" #include "conf.h"
#include "fs/Path.hxx"
extern "C" { extern "C" {
#include "audio_parser.h" #include "audio_parser.h"
...@@ -107,6 +108,8 @@ int main(int argc, char **argv) ...@@ -107,6 +108,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
const Path config_path = Path::FromFS(argv[1]);
audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
/* initialize GLib */ /* initialize GLib */
...@@ -117,8 +120,7 @@ int main(int argc, char **argv) ...@@ -117,8 +120,7 @@ int main(int argc, char **argv)
/* read configuration file (mpd.conf) */ /* read configuration file (mpd.conf) */
config_global_init(); config_global_init();
success = config_read_file(argv[1], &error); if (!ReadConfigFile(config_path, &error)) {
if (!success) {
g_printerr("%s:", error->message); g_printerr("%s:", error->message);
g_error_free(error); g_error_free(error);
return 1; return 1;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "GlobalEvents.hxx" #include "GlobalEvents.hxx"
#include "IOThread.hxx" #include "IOThread.hxx"
#include "fs/Path.hxx"
extern "C" { extern "C" {
#include "output_plugin.h" #include "output_plugin.h"
...@@ -201,6 +202,8 @@ int main(int argc, char **argv) ...@@ -201,6 +202,8 @@ int main(int argc, char **argv)
return 1; return 1;
} }
const Path config_path = Path::FromFS(argv[1]);
audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2);
g_thread_init(NULL); g_thread_init(NULL);
...@@ -208,8 +211,7 @@ int main(int argc, char **argv) ...@@ -208,8 +211,7 @@ int main(int argc, char **argv)
/* read configuration file (mpd.conf) */ /* read configuration file (mpd.conf) */
config_global_init(); config_global_init();
success = config_read_file(argv[1], &error); if (!ReadConfigFile(config_path, &error)) {
if (!success) {
g_printerr("%s:", error->message); g_printerr("%s:", error->message);
g_error_free(error); g_error_free(error);
return 1; return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment