From feb8d5b82f3e7c67806d5efbef1e0b86170ec6dd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Jan 2013 17:23:35 +0100 Subject: ConfigFile, CommandLine: use the Path class --- Makefile.am | 11 +++++++ src/CommandLine.cxx | 88 ++++++++++++++++++++++++-------------------------- src/ConfigFile.cxx | 12 ++++--- src/PlaylistFile.cxx | 5 +-- src/PlaylistSave.cxx | 3 +- src/UpdateIO.cxx | 10 +++--- src/conf.h | 14 ++++++-- test/DumpDatabase.cxx | 5 +-- test/dump_playlist.cxx | 6 ++-- test/read_conf.cxx | 6 ++-- test/run_filter.cxx | 6 ++-- test/run_output.cxx | 6 ++-- 12 files changed, 98 insertions(+), 74 deletions(-) diff --git a/Makefile.am b/Makefile.am index b544488d..fc03c68e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1044,6 +1044,7 @@ noinst_PROGRAMS += test/read_mixer endif test_read_conf_LDADD = \ + libfs.a \ $(GLIB_LIBS) test_read_conf_SOURCES = test/read_conf.cxx \ src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c @@ -1074,6 +1075,7 @@ test_run_input_LDADD = \ $(INPUT_LIBS) \ $(ARCHIVE_LIBS) \ libevent.a \ + libfs.a \ $(GLIB_LIBS) test_run_input_SOURCES = test/run_input.cxx \ test/stdbin.h \ @@ -1087,6 +1089,7 @@ test_dump_text_file_LDADD = \ $(INPUT_LIBS) \ $(ARCHIVE_LIBS) \ libevent.a \ + libfs.a \ libutil.a \ $(GLIB_LIBS) test_dump_text_file_SOURCES = test/dump_text_file.cxx \ @@ -1106,6 +1109,7 @@ test_dump_playlist_LDADD = \ $(DECODER_LIBS) \ $(TAG_LIBS) \ libevent.a \ + libfs.a \ libutil.a \ $(GLIB_LIBS) test_dump_playlist_SOURCES = test/dump_playlist.cxx \ @@ -1133,6 +1137,7 @@ test_run_decoder_LDADD = \ $(ARCHIVE_LIBS) \ $(TAG_LIBS) \ libevent.a \ + libfs.a \ libutil.a \ $(GLIB_LIBS) test_run_decoder_SOURCES = test/run_decoder.cxx \ @@ -1157,6 +1162,7 @@ test_read_tags_LDADD = \ $(ARCHIVE_LIBS) \ $(TAG_LIBS) \ libevent.a \ + libfs.a \ libutil.a \ $(GLIB_LIBS) test_read_tags_SOURCES = test/read_tags.cxx \ @@ -1182,6 +1188,7 @@ endif test_run_filter_LDADD = \ $(FILTER_LIBS) \ + libfs.a \ $(GLIB_LIBS) test_run_filter_SOURCES = test/run_filter.cxx \ test/FakeReplayGainConfig.cxx \ @@ -1216,6 +1223,7 @@ test_run_encoder_SOURCES = test/run_encoder.c \ test_run_encoder_LDADD = \ $(ENCODER_LIBS) \ libpcm.a \ + libfs.a \ libutil.a \ $(TAG_LIBS) \ $(GLIB_LIBS) @@ -1238,6 +1246,7 @@ test_test_vorbis_encoder_CPPFLAGS = $(AM_CPPFLAGS) \ $(ENCODER_CFLAGS) test_test_vorbis_encoder_LDADD = $(MPD_LIBS) \ $(ENCODER_LIBS) \ + libfs.a \ libutil.a \ $(GLIB_LIBS) endif @@ -1274,6 +1283,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ libmixer_plugins.a \ $(FILTER_LIBS) \ libevent.a \ + libfs.a \ libutil.a \ $(GLIB_LIBS) test_run_output_SOURCES = test/run_output.cxx \ @@ -1306,6 +1316,7 @@ test_read_mixer_LDADD = \ libmixer_plugins.a \ $(OUTPUT_LIBS) \ libevent.a \ + libfs.a \ $(GLIB_LIBS) test_read_mixer_SOURCES = test/read_mixer.c \ src/ConfigFile.cxx src/tokenizer.c src/utils.c src/string_util.c \ diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index f04bf89b..5384fc6d 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -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; - - 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; - } + Path path = PathBuildChecked(Path::FromUTF8(g_get_user_config_dir()), + CONFIG_FILE_LOCATION); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); + + const char *const*system_config_dirs = + g_get_system_config_dirs(); + + 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"); diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx index 614bb4f9..02a705f4 100644 --- a/src/ConfigFile.cxx +++ b/src/ConfigFile.cxx @@ -27,6 +27,7 @@ extern "C" { } #include "fs/Path.hxx" +#include "fs/FileSystem.hxx" #include "mpd_error.h" #include @@ -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; } diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index daec5ca0..278e521b 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -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"); diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 49e475ee..1ff6c856 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -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"); diff --git a/src/UpdateIO.cxx b/src/UpdateIO.cxx index eca5688a..bd5fef69 100644 --- a/src/UpdateIO.cxx +++ b/src/UpdateIO.cxx @@ -22,7 +22,7 @@ #include "Directory.hxx" #include "Mapper.hxx" #include "fs/Path.hxx" -#include "glib_compat.h" +#include "fs/FileSystem.hxx" #include @@ -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 diff --git a/src/conf.h b/src/conf.h index d20dc78b..ca60591d 100644 --- a/src/conf.h +++ b/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 */ diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx index ea0ebb44..501c8020 100644 --- a/test/DumpDatabase.cxx +++ b/test/DumpDatabase.cxx @@ -26,6 +26,7 @@ #include "PlaylistVector.hxx" #include "conf.h" #include "tag.h" +#include "fs/Path.hxx" #include using std::cout; @@ -76,7 +77,7 @@ main(int argc, char **argv) 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 DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); @@ -94,7 +95,7 @@ main(int argc, char **argv) config_global_init(); - if (!config_read_file(config_path, &error)) { + if (!ReadConfigFile(config_path, &error)) { cerr << error->message << endl; g_error_free(error); return EXIT_FAILURE; diff --git a/test/dump_playlist.cxx b/test/dump_playlist.cxx index 175a407e..ec09bcb2 100644 --- a/test/dump_playlist.cxx +++ b/test/dump_playlist.cxx @@ -28,6 +28,7 @@ #include "IOThread.hxx" #include "PlaylistRegistry.hxx" #include "PlaylistPlugin.hxx" +#include "fs/Path.hxx" extern "C" { #include "decoder_list.h" @@ -141,7 +142,6 @@ int main(int argc, char **argv) { const char *uri; struct input_stream *is = NULL; - bool success; GError *error = NULL; struct playlist_provider *playlist; struct song *song; @@ -151,6 +151,7 @@ int main(int argc, char **argv) return 1; } + const Path config_path = Path::FromFS(argv[1]); uri = argv[2]; /* initialize GLib */ @@ -161,8 +162,7 @@ int main(int argc, char **argv) /* initialize MPD */ config_global_init(); - success = config_read_file(argv[1], &error); - if (!success) { + if (!ReadConfigFile(config_path, &error)) { g_printerr("%s\n", error->message); g_error_free(error); return 1; diff --git a/test/read_conf.cxx b/test/read_conf.cxx index 859c8949..a9135d89 100644 --- a/test/read_conf.cxx +++ b/test/read_conf.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "conf.h" +#include "fs/Path.hxx" #include @@ -42,7 +43,7 @@ int main(int argc, char **argv) return 1; } - const char *path = argv[1]; + const Path config_path = Path::FromFS(argv[1]); const char *name = argv[2]; g_log_set_default_handler(my_log_func, NULL); @@ -50,8 +51,7 @@ int main(int argc, char **argv) config_global_init(); GError *error = NULL; - bool success = config_read_file(path, &error); - if (!success) { + if (!ReadConfigFile(config_path, &error)) { g_printerr("%s:", error->message); g_error_free(error); return 1; diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 990a57df..b05e458a 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "conf.h" +#include "fs/Path.hxx" extern "C" { #include "audio_parser.h" @@ -107,6 +108,8 @@ int main(int argc, char **argv) return 1; } + const Path config_path = Path::FromFS(argv[1]); + audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); /* initialize GLib */ @@ -117,8 +120,7 @@ int main(int argc, char **argv) /* read configuration file (mpd.conf) */ config_global_init(); - success = config_read_file(argv[1], &error); - if (!success) { + if (!ReadConfigFile(config_path, &error)) { g_printerr("%s:", error->message); g_error_free(error); return 1; diff --git a/test/run_output.cxx b/test/run_output.cxx index 50542347..38f24fe5 100644 --- a/test/run_output.cxx +++ b/test/run_output.cxx @@ -25,6 +25,7 @@ #include "event/Loop.hxx" #include "GlobalEvents.hxx" #include "IOThread.hxx" +#include "fs/Path.hxx" extern "C" { #include "output_plugin.h" @@ -201,6 +202,8 @@ int main(int argc, char **argv) return 1; } + const Path config_path = Path::FromFS(argv[1]); + audio_format_init(&audio_format, 44100, SAMPLE_FORMAT_S16, 2); g_thread_init(NULL); @@ -208,8 +211,7 @@ int main(int argc, char **argv) /* read configuration file (mpd.conf) */ config_global_init(); - success = config_read_file(argv[1], &error); - if (!success) { + if (!ReadConfigFile(config_path, &error)) { g_printerr("%s:", error->message); g_error_free(error); return 1; -- cgit v1.2.3