diff options
author | Max Kellermann <max@duempel.org> | 2014-02-02 14:37:52 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-02-03 23:32:10 +0100 |
commit | ca36ac2ba196ee2bbe4b54ee9a71d49174803277 (patch) | |
tree | d365b1ac7872e1785befdcebf254885c1c27a268 /src/playlist | |
parent | ba675d6a55769a6e82a6efaa2f4a812a4eea2362 (diff) |
SongLoader: new class that merges duplicate code
There was quite a lot of duplicate code for loading DetachedSong
objects, with different semantics for "securely" loading local files.
Diffstat (limited to 'src/playlist')
-rw-r--r-- | src/playlist/PlaylistQueue.cxx | 8 | ||||
-rw-r--r-- | src/playlist/PlaylistQueue.hxx | 5 | ||||
-rw-r--r-- | src/playlist/PlaylistSong.cxx | 67 | ||||
-rw-r--r-- | src/playlist/PlaylistSong.hxx | 5 | ||||
-rw-r--r-- | src/playlist/Print.cxx | 5 |
5 files changed, 27 insertions, 63 deletions
diff --git a/src/playlist/PlaylistQueue.cxx b/src/playlist/PlaylistQueue.cxx index 564c94d0a..9adea317f 100644 --- a/src/playlist/PlaylistQueue.cxx +++ b/src/playlist/PlaylistQueue.cxx @@ -32,7 +32,7 @@ PlaylistResult playlist_load_into_queue(const char *uri, SongEnumerator &e, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - bool secure) + const SongLoader &loader) { const std::string base_uri = uri != nullptr ? PathTraitsUTF8::GetParent(uri) @@ -49,7 +49,7 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e, } if (!playlist_check_translate_song(*song, base_uri.c_str(), - secure)) { + loader)) { delete song; continue; } @@ -67,7 +67,7 @@ PlaylistResult playlist_open_into_queue(const char *uri, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - bool secure) + const SongLoader &loader) { Mutex mutex; Cond cond; @@ -80,7 +80,7 @@ playlist_open_into_queue(const char *uri, PlaylistResult result = playlist_load_into_queue(uri, *playlist, start_index, end_index, - dest, pc, secure); + dest, pc, loader); delete playlist; if (is != nullptr) diff --git a/src/playlist/PlaylistQueue.hxx b/src/playlist/PlaylistQueue.hxx index 075191ced..48e42c70e 100644 --- a/src/playlist/PlaylistQueue.hxx +++ b/src/playlist/PlaylistQueue.hxx @@ -26,6 +26,7 @@ #include "PlaylistError.hxx" +class SongLoader; class SongEnumerator; struct playlist; struct PlayerControl; @@ -43,7 +44,7 @@ PlaylistResult playlist_load_into_queue(const char *uri, SongEnumerator &e, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - bool secure); + const SongLoader &loader); /** * Opens a playlist with a playlist plugin and append to the specified @@ -53,7 +54,7 @@ PlaylistResult playlist_open_into_queue(const char *uri, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - bool secure); + const SongLoader &loader); #endif diff --git a/src/playlist/PlaylistSong.cxx b/src/playlist/PlaylistSong.cxx index b382994a6..e3defbddb 100644 --- a/src/playlist/PlaylistSong.cxx +++ b/src/playlist/PlaylistSong.cxx @@ -20,8 +20,7 @@ #include "config.h" #include "PlaylistSong.hxx" #include "Mapper.hxx" -#include "db/DatabaseSong.hxx" -#include "ls.hxx" +#include "SongLoader.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" #include "fs/AllocatedPath.hxx" @@ -65,44 +64,22 @@ apply_song_metadata(DetachedSong &dest, const DetachedSong &src) } static bool -playlist_check_load_song(DetachedSong &song) +playlist_check_load_song(DetachedSong &song, const SongLoader &loader) { - const char *const uri = song.GetURI(); - - if (uri_has_scheme(uri)) { - return true; - } else if (PathTraitsUTF8::IsAbsolute(uri)) { - DetachedSong tmp(uri); - if (!tmp.Update()) - return false; - - apply_song_metadata(song, tmp); - return true; - } else { -#ifdef ENABLE_DATABASE - DetachedSong *tmp = DatabaseDetachSong(uri, IgnoreError()); - if (tmp == nullptr) - return false; - - apply_song_metadata(song, *tmp); - delete tmp; - return true; -#else + DetachedSong *tmp = loader.LoadSong(song.GetURI(), IgnoreError()); + if (tmp == nullptr) return false; -#endif - } + + song.SetURI(tmp->GetURI()); + apply_song_metadata(song, *tmp); + delete tmp; + return true; } bool playlist_check_translate_song(DetachedSong &song, const char *base_uri, - bool secure) + const SongLoader &loader) { - const char *const uri = song.GetURI(); - - if (uri_has_scheme(uri)) - /* valid remote song? */ - return uri_supported_scheme(uri); - if (base_uri != nullptr && strcmp(base_uri, ".") == 0) /* PathTraitsUTF8::GetParent() returns "." when there is no directory name in the given path; clear that @@ -110,26 +87,10 @@ playlist_check_translate_song(DetachedSong &song, const char *base_uri, functions */ base_uri = nullptr; - if (PathTraitsUTF8::IsAbsolute(uri)) { - /* XXX fs_charset vs utf8? */ - const char *suffix = map_to_relative_path(uri); - assert(suffix != nullptr); - - if (suffix != uri) - song.SetURI(std::string(suffix)); - else if (!secure) - /* local files must be relative to the music - directory when "secure" is enabled */ - return false; - - base_uri = nullptr; - } - - if (base_uri != nullptr) { + const char *uri = song.GetURI(); + if (base_uri != nullptr && !uri_has_scheme(uri) && + !PathTraitsUTF8::IsAbsolute(uri)) song.SetURI(PathTraitsUTF8::Build(base_uri, uri)); - /* repeat the above checks */ - return playlist_check_translate_song(song, nullptr, secure); - } - return playlist_check_load_song(song); + return playlist_check_load_song(song, loader); } diff --git a/src/playlist/PlaylistSong.hxx b/src/playlist/PlaylistSong.hxx index 2a47b28db..278df46a8 100644 --- a/src/playlist/PlaylistSong.hxx +++ b/src/playlist/PlaylistSong.hxx @@ -20,18 +20,17 @@ #ifndef MPD_PLAYLIST_SONG_HXX #define MPD_PLAYLIST_SONG_HXX +class SongLoader; class DetachedSong; /** * Verifies the song, returns false if it is unsafe. Translate the * song to a song within the database, if it is a local file. * - * @param secure if true, then local files are only allowed if they - * are relative to base_uri * @return true on success, false if the song should not be used */ bool playlist_check_translate_song(DetachedSong &song, const char *base_uri, - bool secure); + const SongLoader &loader); #endif diff --git a/src/playlist/Print.cxx b/src/playlist/Print.cxx index dc5aa252c..b5398e67d 100644 --- a/src/playlist/Print.cxx +++ b/src/playlist/Print.cxx @@ -25,6 +25,7 @@ #include "SongPrint.hxx" #include "input/InputStream.hxx" #include "DetachedSong.hxx" +#include "SongLoader.hxx" #include "fs/Traits.hxx" #include "thread/Cond.hxx" @@ -36,10 +37,12 @@ playlist_provider_print(Client &client, const char *uri, ? PathTraitsUTF8::GetParent(uri) : std::string("."); + const SongLoader loader(client); + DetachedSong *song; while ((song = e.NextSong()) != nullptr) { if (playlist_check_translate_song(*song, base_uri.c_str(), - false)) { + loader)) { if (detail) song_print_info(client, *song); else |