From d65ad1bf151383722802d4bb13b12a33b97480e4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 2 Jan 2013 22:43:56 +0100 Subject: mapper: convert to C++ --- src/DecoderThread.cxx | 2 +- src/InotifyUpdate.cxx | 2 +- src/Main.cxx | 2 +- src/Mapper.cxx | 297 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Mapper.hxx | 144 +++++++++++++++++++++++ src/OtherCommands.cxx | 2 +- src/PlaylistFile.cxx | 2 +- src/PlaylistMapper.cxx | 2 +- src/PlaylistSave.cxx | 2 +- src/PlaylistSong.cxx | 2 +- src/QueuePrint.cxx | 2 +- src/SongPrint.cxx | 2 +- src/SongUpdate.cxx | 2 +- src/UpdateArchive.cxx | 2 +- src/UpdateContainer.cxx | 2 +- src/UpdateGlue.cxx | 2 +- src/UpdateIO.cxx | 5 +- src/UpdateWalk.cxx | 2 +- src/mapper.c | 294 ----------------------------------------------- src/mapper.h | 147 ------------------------ 20 files changed, 457 insertions(+), 460 deletions(-) create mode 100644 src/Mapper.cxx create mode 100644 src/Mapper.hxx delete mode 100644 src/mapper.c delete mode 100644 src/mapper.h (limited to 'src') diff --git a/src/DecoderThread.cxx b/src/DecoderThread.cxx index e5b582598..cbdb19188 100644 --- a/src/DecoderThread.cxx +++ b/src/DecoderThread.cxx @@ -23,6 +23,7 @@ #include "decoder_plugin.h" #include "song.h" #include "mpd_error.h" +#include "Mapper.hxx" extern "C" { #include "decoder_control.h" @@ -32,7 +33,6 @@ extern "C" { #include "replay_gain_ape.h" #include "input_stream.h" #include "tag.h" -#include "mapper.h" #include "uri.h" } diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx index 3e438b697..878da888e 100644 --- a/src/InotifyUpdate.cxx +++ b/src/InotifyUpdate.cxx @@ -22,9 +22,9 @@ #include "InotifySource.hxx" #include "InotifyQueue.hxx" #include "database.h" +#include "Mapper.hxx" extern "C" { -#include "mapper.h" #include "path.h" } diff --git a/src/Main.cxx b/src/Main.cxx index d0f88c23e..3d457e31e 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -24,6 +24,7 @@ #include "chunk.h" #include "StateFile.hxx" #include "PlayerThread.hxx" +#include "Mapper.hxx" extern "C" { #include "daemon.h" @@ -38,7 +39,6 @@ extern "C" { #include "cmdline.h" #include "conf.h" #include "path.h" -#include "mapper.h" #include "player_control.h" #include "stats.h" #include "sig_handlers.h" diff --git a/src/Mapper.cxx b/src/Mapper.cxx new file mode 100644 index 000000000..1dc91ed12 --- /dev/null +++ b/src/Mapper.cxx @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Maps directory and song objects to file system paths. + */ + +#include "config.h" +#include "Mapper.hxx" +#include "directory.h" +#include "song.h" + +extern "C" { +#include "path.h" +} + +#include + +#include +#include +#include +#include +#include +#include + +/** + * The absolute path of the music directory encoded in UTF-8. + */ +static char *music_dir_utf8; +static size_t music_dir_utf8_length; + +/** + * The absolute path of the music directory encoded in the filesystem + * character set. + */ +static char *music_dir_fs; +static size_t music_dir_fs_length; + +/** + * The absolute path of the playlist directory encoded in the + * filesystem character set. + */ +static char *playlist_dir_fs; + +/** + * Duplicate a string, chop all trailing slashes. + */ +static char * +strdup_chop_slash(const char *path_fs) +{ + size_t length = strlen(path_fs); + + while (length > 0 && path_fs[length - 1] == G_DIR_SEPARATOR) + --length; + + return g_strndup(path_fs, length); +} + +static void +check_directory(const char *path) +{ + struct stat st; + if (stat(path, &st) < 0) { + g_warning("Failed to stat directory \"%s\": %s", + path, g_strerror(errno)); + return; + } + + if (!S_ISDIR(st.st_mode)) { + g_warning("Not a directory: %s", path); + return; + } + +#ifndef WIN32 + char *x = g_build_filename(path, ".", NULL); + if (stat(x, &st) < 0 && errno == EACCES) + g_warning("No permission to traverse (\"execute\") directory: %s", + path); + g_free(x); +#endif + + DIR *dir = opendir(path); + if (dir != NULL) + closedir(dir); + else if (errno == EACCES) + g_warning("No permission to read directory: %s", path); +} + +static void +mapper_set_music_dir(const char *path_utf8) +{ + music_dir_utf8 = strdup_chop_slash(path_utf8); + music_dir_utf8_length = strlen(music_dir_utf8); + + music_dir_fs = utf8_to_fs_charset(music_dir_utf8); + check_directory(music_dir_fs); + music_dir_fs_length = strlen(music_dir_fs); +} + +static void +mapper_set_playlist_dir(const char *path_utf8) +{ + playlist_dir_fs = utf8_to_fs_charset(path_utf8); + check_directory(playlist_dir_fs); +} + +void mapper_init(const char *_music_dir, const char *_playlist_dir) +{ + if (_music_dir != NULL) + mapper_set_music_dir(_music_dir); + + if (_playlist_dir != NULL) + mapper_set_playlist_dir(_playlist_dir); +} + +void mapper_finish(void) +{ + g_free(music_dir_utf8); + g_free(music_dir_fs); + g_free(playlist_dir_fs); +} + +const char * +mapper_get_music_directory_utf8(void) +{ + return music_dir_utf8; +} + +const char * +mapper_get_music_directory_fs(void) +{ + return music_dir_fs; +} + +const char * +map_to_relative_path(const char *path_utf8) +{ + return music_dir_utf8 != NULL && + memcmp(path_utf8, music_dir_utf8, + music_dir_utf8_length) == 0 && + G_IS_DIR_SEPARATOR(path_utf8[music_dir_utf8_length]) + ? path_utf8 + music_dir_utf8_length + 1 + : path_utf8; +} + +char * +map_uri_fs(const char *uri) +{ + char *uri_fs, *path_fs; + + assert(uri != NULL); + assert(*uri != '/'); + + if (music_dir_fs == NULL) + return NULL; + + uri_fs = utf8_to_fs_charset(uri); + if (uri_fs == NULL) + return NULL; + + path_fs = g_build_filename(music_dir_fs, uri_fs, NULL); + g_free(uri_fs); + + return path_fs; +} + +char * +map_directory_fs(const struct directory *directory) +{ + assert(music_dir_utf8 != NULL); + assert(music_dir_fs != NULL); + + if (directory_is_root(directory)) + return g_strdup(music_dir_fs); + + return map_uri_fs(directory_get_path(directory)); +} + +char * +map_directory_child_fs(const struct directory *directory, const char *name) +{ + assert(music_dir_utf8 != NULL); + assert(music_dir_fs != NULL); + + char *name_fs, *parent_fs, *path; + + /* check for invalid or unauthorized base names */ + if (*name == 0 || strchr(name, '/') != NULL || + strcmp(name, ".") == 0 || strcmp(name, "..") == 0) + return NULL; + + parent_fs = map_directory_fs(directory); + if (parent_fs == NULL) + return NULL; + + name_fs = utf8_to_fs_charset(name); + if (name_fs == NULL) { + g_free(parent_fs); + return NULL; + } + + path = g_build_filename(parent_fs, name_fs, NULL); + g_free(parent_fs); + g_free(name_fs); + + return path; +} + +/** + * Map a song object that was created by song_dup_detached(). It does + * not have a real parent directory, only the dummy object + * #detached_root. + */ +static char * +map_detached_song_fs(const char *uri_utf8) +{ + char *uri_fs = utf8_to_fs_charset(uri_utf8); + if (uri_fs == NULL) + return NULL; + + char *path = g_build_filename(music_dir_fs, uri_fs, NULL); + g_free(uri_fs); + return path; +} + +char * +map_song_fs(const struct song *song) +{ + assert(song_is_file(song)); + + if (song_in_database(song)) + return song_is_detached(song) + ? map_detached_song_fs(song->uri) + : map_directory_child_fs(song->parent, song->uri); + else + return utf8_to_fs_charset(song->uri); +} + +char * +map_fs_to_utf8(const char *path_fs) +{ + if (music_dir_fs != NULL && + strncmp(path_fs, music_dir_fs, music_dir_fs_length) == 0 && + G_IS_DIR_SEPARATOR(path_fs[music_dir_fs_length])) + /* remove musicDir prefix */ + path_fs += music_dir_fs_length + 1; + else if (G_IS_DIR_SEPARATOR(path_fs[0])) + /* not within musicDir */ + return NULL; + + while (path_fs[0] == G_DIR_SEPARATOR) + ++path_fs; + + return fs_charset_to_utf8(path_fs); +} + +const char * +map_spl_path(void) +{ + return playlist_dir_fs; +} + +char * +map_spl_utf8_to_fs(const char *name) +{ + char *filename_utf8, *filename_fs, *path; + + if (playlist_dir_fs == NULL) + return NULL; + + filename_utf8 = g_strconcat(name, PLAYLIST_FILE_SUFFIX, NULL); + filename_fs = utf8_to_fs_charset(filename_utf8); + g_free(filename_utf8); + if (filename_fs == NULL) + return NULL; + + path = g_build_filename(playlist_dir_fs, filename_fs, NULL); + g_free(filename_fs); + + return path; +} diff --git a/src/Mapper.hxx b/src/Mapper.hxx new file mode 100644 index 000000000..d7e4ea1a9 --- /dev/null +++ b/src/Mapper.hxx @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Maps directory and song objects to file system paths. + */ + +#ifndef MPD_MAPPER_HXX +#define MPD_MAPPER_HXX + +#include "gcc.h" +#include "gerror.h" + +#define PLAYLIST_FILE_SUFFIX ".m3u" + +struct directory; +struct song; + +void mapper_init(const char *_music_dir, const char *_playlist_dir); + +void mapper_finish(void); + +/** + * Return the absolute path of the music directory encoded in UTF-8. + */ +gcc_const +const char * +mapper_get_music_directory_utf8(void); + +/** + * Return the absolute path of the music directory encoded in the + * filesystem character set. + */ +gcc_const +const char * +mapper_get_music_directory_fs(void); + +/** + * Returns true if a music directory was configured. + */ +gcc_const +static inline bool +mapper_has_music_directory(void) +{ + return mapper_get_music_directory_utf8() != nullptr; +} + +/** + * If the specified absolute path points inside the music directory, + * this function converts it to a relative path. If not, it returns + * the unmodified string pointer. + */ +gcc_pure +const char * +map_to_relative_path(const char *path_utf8); + +/** + * Determines the absolute file system path of a relative URI. This + * is basically done by converting the URI to the file system charset + * and prepending the music directory. + */ +gcc_malloc +char * +map_uri_fs(const char *uri); + +/** + * Determines the file system path of a directory object. + * + * @param directory the directory object + * @return the path in file system encoding, or nullptr if mapping failed + */ +gcc_malloc +char * +map_directory_fs(const struct directory *directory); + +/** + * Determines the file system path of a directory's child (may be a + * sub directory or a song). + * + * @param directory the parent directory object + * @param name the child's name in UTF-8 + * @return the path in file system encoding, or nullptr if mapping failed + */ +gcc_malloc +char * +map_directory_child_fs(const struct directory *directory, const char *name); + +/** + * Determines the file system path of a song. This must not be a + * remote song. + * + * @param song the song object + * @return the path in file system encoding, or nullptr if mapping failed + */ +gcc_malloc +char * +map_song_fs(const struct song *song); + +/** + * Maps a file system path (relative to the music directory or + * absolute) to a relative path in UTF-8 encoding. + * + * @param path_fs a path in file system encoding + * @return the relative path in UTF-8, or nullptr if mapping failed + */ +gcc_malloc +char * +map_fs_to_utf8(const char *path_fs); + +/** + * Returns the playlist directory. + */ +gcc_const +const char * +map_spl_path(void); + +/** + * Maps a playlist name (without the ".m3u" suffix) to a file system + * path. The return value is allocated on the heap and must be freed + * with g_free(). + * + * @return the path in file system encoding, or nullptr if mapping failed + */ +gcc_pure +char * +map_spl_utf8_to_fs(const char *name); + +#endif diff --git a/src/OtherCommands.cxx b/src/OtherCommands.cxx index 19d5a8c76..01f9b71a4 100644 --- a/src/OtherCommands.cxx +++ b/src/OtherCommands.cxx @@ -27,6 +27,7 @@ #include "SongPrint.hxx" #include "TagPrint.hxx" #include "TimePrint.hxx" +#include "Mapper.hxx" extern "C" { #include "protocol/argparser.h" @@ -46,7 +47,6 @@ extern "C" { #include "client_idle.h" #include "client_file.h" #include "idle.h" -#include "mapper.h" } #ifdef ENABLE_SQLITE diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 3223edee3..896307df9 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -22,10 +22,10 @@ #include "PlaylistSave.hxx" #include "song.h" #include "io_error.h" +#include "Mapper.hxx" extern "C" { #include "text_file.h" -#include "mapper.h" #include "path.h" #include "uri.h" #include "database.h" diff --git a/src/PlaylistMapper.cxx b/src/PlaylistMapper.cxx index f1c9471a7..01b8f7dd8 100644 --- a/src/PlaylistMapper.cxx +++ b/src/PlaylistMapper.cxx @@ -20,10 +20,10 @@ #include "config.h" #include "PlaylistMapper.hxx" #include "PlaylistFile.hxx" +#include "Mapper.hxx" extern "C" { #include "playlist_list.h" -#include "mapper.h" #include "uri.h" } diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 8d1908aac..722ae9c83 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -21,10 +21,10 @@ #include "PlaylistSave.hxx" #include "PlaylistFile.hxx" #include "song.h" +#include "Mapper.hxx" extern "C" { #include "playlist.h" -#include "mapper.h" #include "path.h" #include "uri.h" #include "idle.h" diff --git a/src/PlaylistSong.cxx b/src/PlaylistSong.cxx index 03bf5b703..72d2760c6 100644 --- a/src/PlaylistSong.cxx +++ b/src/PlaylistSong.cxx @@ -19,10 +19,10 @@ #include "config.h" #include "PlaylistSong.hxx" +#include "Mapper.hxx" extern "C" { #include "database.h" -#include "mapper.h" #include "song.h" #include "uri.h" #include "path.h" diff --git a/src/QueuePrint.cxx b/src/QueuePrint.cxx index 32f131ae8..f99181ca5 100644 --- a/src/QueuePrint.cxx +++ b/src/QueuePrint.cxx @@ -21,12 +21,12 @@ #include "QueuePrint.hxx" #include "SongFilter.hxx" #include "SongPrint.hxx" +#include "Mapper.hxx" extern "C" { #include "queue.h" #include "song.h" #include "client.h" -#include "mapper.h" } /** diff --git a/src/SongPrint.cxx b/src/SongPrint.cxx index 371952e86..07cf9c3e2 100644 --- a/src/SongPrint.cxx +++ b/src/SongPrint.cxx @@ -23,11 +23,11 @@ #include "directory.h" #include "TimePrint.hxx" #include "TagPrint.hxx" +#include "Mapper.hxx" extern "C" { #include "client.h" #include "uri.h" -#include "mapper.h" } void diff --git a/src/SongUpdate.cxx b/src/SongUpdate.cxx index 114ad875c..c5258f025 100644 --- a/src/SongUpdate.cxx +++ b/src/SongUpdate.cxx @@ -25,9 +25,9 @@ extern "C" { } #include "directory.h" +#include "Mapper.hxx" extern "C" { -#include "mapper.h" #include "decoder_list.h" #include "decoder_plugin.h" #include "tag_ape.h" diff --git a/src/UpdateArchive.cxx b/src/UpdateArchive.cxx index 441cf71c2..36f5cdc20 100644 --- a/src/UpdateArchive.cxx +++ b/src/UpdateArchive.cxx @@ -23,9 +23,9 @@ #include "DatabaseLock.hxx" #include "directory.h" #include "song.h" +#include "Mapper.hxx" extern "C" { -#include "mapper.h" #include "archive_list.h" #include "archive_plugin.h" } diff --git a/src/UpdateContainer.cxx b/src/UpdateContainer.cxx index 7a904a1e6..909295d98 100644 --- a/src/UpdateContainer.cxx +++ b/src/UpdateContainer.cxx @@ -25,9 +25,9 @@ #include "directory.h" #include "song.h" #include "decoder_plugin.h" +#include "Mapper.hxx" extern "C" { -#include "mapper.h" #include "tag.h" #include "tag_handler.h" } diff --git a/src/UpdateGlue.cxx b/src/UpdateGlue.cxx index 203f16b76..9cf27588a 100644 --- a/src/UpdateGlue.cxx +++ b/src/UpdateGlue.cxx @@ -22,10 +22,10 @@ #include "UpdateQueue.hxx" #include "UpdateWalk.hxx" #include "UpdateRemove.hxx" +#include "Mapper.hxx" extern "C" { #include "database.h" -#include "mapper.h" #include "playlist.h" #include "event_pipe.h" #include "idle.h" diff --git a/src/UpdateIO.cxx b/src/UpdateIO.cxx index fefbc4d11..77ec30413 100644 --- a/src/UpdateIO.cxx +++ b/src/UpdateIO.cxx @@ -20,12 +20,9 @@ #include "config.h" /* must be first for large file support */ #include "UpdateIO.hxx" #include "directory.h" +#include "Mapper.hxx" #include "glib_compat.h" -extern "C" { -#include "mapper.h" -} - #include #include diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index a34e0028d..b277f0477 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -27,12 +27,12 @@ #include "directory.h" #include "song.h" #include "PlaylistVector.hxx" +#include "Mapper.hxx" extern "C" { #include "exclude.h" #include "database.h" #include "uri.h" -#include "mapper.h" #include "path.h" #include "playlist_list.h" #include "conf.h" diff --git a/src/mapper.c b/src/mapper.c deleted file mode 100644 index 1f8a54b46..000000000 --- a/src/mapper.c +++ /dev/null @@ -1,294 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* - * Maps directory and song objects to file system paths. - */ - -#include "config.h" -#include "mapper.h" -#include "directory.h" -#include "song.h" -#include "path.h" - -#include - -#include -#include -#include -#include -#include -#include - -/** - * The absolute path of the music directory encoded in UTF-8. - */ -static char *music_dir_utf8; -static size_t music_dir_utf8_length; - -/** - * The absolute path of the music directory encoded in the filesystem - * character set. - */ -static char *music_dir_fs; -static size_t music_dir_fs_length; - -/** - * The absolute path of the playlist directory encoded in the - * filesystem character set. - */ -static char *playlist_dir_fs; - -/** - * Duplicate a string, chop all trailing slashes. - */ -static char * -strdup_chop_slash(const char *path_fs) -{ - size_t length = strlen(path_fs); - - while (length > 0 && path_fs[length - 1] == G_DIR_SEPARATOR) - --length; - - return g_strndup(path_fs, length); -} - -static void -check_directory(const char *path) -{ - struct stat st; - if (stat(path, &st) < 0) { - g_warning("Failed to stat directory \"%s\": %s", - path, g_strerror(errno)); - return; - } - - if (!S_ISDIR(st.st_mode)) { - g_warning("Not a directory: %s", path); - return; - } - -#ifndef WIN32 - char *x = g_build_filename(path, ".", NULL); - if (stat(x, &st) < 0 && errno == EACCES) - g_warning("No permission to traverse (\"execute\") directory: %s", - path); - g_free(x); -#endif - - DIR *dir = opendir(path); - if (dir != NULL) - closedir(dir); - else if (errno == EACCES) - g_warning("No permission to read directory: %s", path); -} - -static void -mapper_set_music_dir(const char *path_utf8) -{ - music_dir_utf8 = strdup_chop_slash(path_utf8); - music_dir_utf8_length = strlen(music_dir_utf8); - - music_dir_fs = utf8_to_fs_charset(music_dir_utf8); - check_directory(music_dir_fs); - music_dir_fs_length = strlen(music_dir_fs); -} - -static void -mapper_set_playlist_dir(const char *path_utf8) -{ - playlist_dir_fs = utf8_to_fs_charset(path_utf8); - check_directory(playlist_dir_fs); -} - -void mapper_init(const char *_music_dir, const char *_playlist_dir) -{ - if (_music_dir != NULL) - mapper_set_music_dir(_music_dir); - - if (_playlist_dir != NULL) - mapper_set_playlist_dir(_playlist_dir); -} - -void mapper_finish(void) -{ - g_free(music_dir_utf8); - g_free(music_dir_fs); - g_free(playlist_dir_fs); -} - -const char * -mapper_get_music_directory_utf8(void) -{ - return music_dir_utf8; -} - -const char * -mapper_get_music_directory_fs(void) -{ - return music_dir_fs; -} - -const char * -map_to_relative_path(const char *path_utf8) -{ - return music_dir_utf8 != NULL && - memcmp(path_utf8, music_dir_utf8, - music_dir_utf8_length) == 0 && - G_IS_DIR_SEPARATOR(path_utf8[music_dir_utf8_length]) - ? path_utf8 + music_dir_utf8_length + 1 - : path_utf8; -} - -char * -map_uri_fs(const char *uri) -{ - char *uri_fs, *path_fs; - - assert(uri != NULL); - assert(*uri != '/'); - - if (music_dir_fs == NULL) - return NULL; - - uri_fs = utf8_to_fs_charset(uri); - if (uri_fs == NULL) - return NULL; - - path_fs = g_build_filename(music_dir_fs, uri_fs, NULL); - g_free(uri_fs); - - return path_fs; -} - -char * -map_directory_fs(const struct directory *directory) -{ - assert(music_dir_utf8 != NULL); - assert(music_dir_fs != NULL); - - if (directory_is_root(directory)) - return g_strdup(music_dir_fs); - - return map_uri_fs(directory_get_path(directory)); -} - -char * -map_directory_child_fs(const struct directory *directory, const char *name) -{ - assert(music_dir_utf8 != NULL); - assert(music_dir_fs != NULL); - - char *name_fs, *parent_fs, *path; - - /* check for invalid or unauthorized base names */ - if (*name == 0 || strchr(name, '/') != NULL || - strcmp(name, ".") == 0 || strcmp(name, "..") == 0) - return NULL; - - parent_fs = map_directory_fs(directory); - if (parent_fs == NULL) - return NULL; - - name_fs = utf8_to_fs_charset(name); - if (name_fs == NULL) { - g_free(parent_fs); - return NULL; - } - - path = g_build_filename(parent_fs, name_fs, NULL); - g_free(parent_fs); - g_free(name_fs); - - return path; -} - -/** - * Map a song object that was created by song_dup_detached(). It does - * not have a real parent directory, only the dummy object - * #detached_root. - */ -static char * -map_detached_song_fs(const char *uri_utf8) -{ - char *uri_fs = utf8_to_fs_charset(uri_utf8); - if (uri_fs == NULL) - return NULL; - - char *path = g_build_filename(music_dir_fs, uri_fs, NULL); - g_free(uri_fs); - return path; -} - -char * -map_song_fs(const struct song *song) -{ - assert(song_is_file(song)); - - if (song_in_database(song)) - return song_is_detached(song) - ? map_detached_song_fs(song->uri) - : map_directory_child_fs(song->parent, song->uri); - else - return utf8_to_fs_charset(song->uri); -} - -char * -map_fs_to_utf8(const char *path_fs) -{ - if (music_dir_fs != NULL && - strncmp(path_fs, music_dir_fs, music_dir_fs_length) == 0 && - G_IS_DIR_SEPARATOR(path_fs[music_dir_fs_length])) - /* remove musicDir prefix */ - path_fs += music_dir_fs_length + 1; - else if (G_IS_DIR_SEPARATOR(path_fs[0])) - /* not within musicDir */ - return NULL; - - while (path_fs[0] == G_DIR_SEPARATOR) - ++path_fs; - - return fs_charset_to_utf8(path_fs); -} - -const char * -map_spl_path(void) -{ - return playlist_dir_fs; -} - -char * -map_spl_utf8_to_fs(const char *name) -{ - char *filename_utf8, *filename_fs, *path; - - if (playlist_dir_fs == NULL) - return NULL; - - filename_utf8 = g_strconcat(name, PLAYLIST_FILE_SUFFIX, NULL); - filename_fs = utf8_to_fs_charset(filename_utf8); - g_free(filename_utf8); - if (filename_fs == NULL) - return NULL; - - path = g_build_filename(playlist_dir_fs, filename_fs, NULL); - g_free(filename_fs); - - return path; -} diff --git a/src/mapper.h b/src/mapper.h deleted file mode 100644 index b4e314569..000000000 --- a/src/mapper.h +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* - * Maps directory and song objects to file system paths. - */ - -#ifndef MPD_MAPPER_H -#define MPD_MAPPER_H - -#include "gcc.h" -#include "gerror.h" - -#include -#include - -#define PLAYLIST_FILE_SUFFIX ".m3u" - -struct directory; -struct song; - -void mapper_init(const char *_music_dir, const char *_playlist_dir); - -void mapper_finish(void); - -/** - * Return the absolute path of the music directory encoded in UTF-8. - */ -gcc_const -const char * -mapper_get_music_directory_utf8(void); - -/** - * Return the absolute path of the music directory encoded in the - * filesystem character set. - */ -gcc_const -const char * -mapper_get_music_directory_fs(void); - -/** - * Returns true if a music directory was configured. - */ -gcc_const -static inline bool -mapper_has_music_directory(void) -{ - return mapper_get_music_directory_utf8() != NULL; -} - -/** - * If the specified absolute path points inside the music directory, - * this function converts it to a relative path. If not, it returns - * the unmodified string pointer. - */ -gcc_pure -const char * -map_to_relative_path(const char *path_utf8); - -/** - * Determines the absolute file system path of a relative URI. This - * is basically done by converting the URI to the file system charset - * and prepending the music directory. - */ -gcc_malloc -char * -map_uri_fs(const char *uri); - -/** - * Determines the file system path of a directory object. - * - * @param directory the directory object - * @return the path in file system encoding, or NULL if mapping failed - */ -gcc_malloc -char * -map_directory_fs(const struct directory *directory); - -/** - * Determines the file system path of a directory's child (may be a - * sub directory or a song). - * - * @param directory the parent directory object - * @param name the child's name in UTF-8 - * @return the path in file system encoding, or NULL if mapping failed - */ -gcc_malloc -char * -map_directory_child_fs(const struct directory *directory, const char *name); - -/** - * Determines the file system path of a song. This must not be a - * remote song. - * - * @param song the song object - * @return the path in file system encoding, or NULL if mapping failed - */ -gcc_malloc -char * -map_song_fs(const struct song *song); - -/** - * Maps a file system path (relative to the music directory or - * absolute) to a relative path in UTF-8 encoding. - * - * @param path_fs a path in file system encoding - * @return the relative path in UTF-8, or NULL if mapping failed - */ -gcc_malloc -char * -map_fs_to_utf8(const char *path_fs); - -/** - * Returns the playlist directory. - */ -gcc_const -const char * -map_spl_path(void); - -/** - * Maps a playlist name (without the ".m3u" suffix) to a file system - * path. The return value is allocated on the heap and must be freed - * with g_free(). - * - * @return the path in file system encoding, or NULL if mapping failed - */ -gcc_pure -char * -map_spl_utf8_to_fs(const char *name); - -#endif -- cgit v1.2.3