db/DatabaseSong: make the Storage optional

Some database plugins don't use a Storage (e.g. UPnP), and with this
plugin, DatabaseDetachSong() can crash.
This commit is contained in:
Max Kellermann 2017-02-08 09:47:43 +01:00
parent f689e28958
commit 777e15bd78
9 changed files with 15 additions and 15 deletions

View File

@ -41,7 +41,7 @@ SongLoader::LoadFromDatabase(const char *uri) const
{
#ifdef ENABLE_DATABASE
if (db != nullptr)
return DatabaseDetachSong(*db, *storage, uri);
return DatabaseDetachSong(*db, storage, uri);
#else
(void)uri;
#endif

View File

@ -134,7 +134,7 @@ handle_searchaddpl(Client &client, Request args, Response &r)
const Database &db = client.GetDatabaseOrThrow();
search_add_to_playlist(db, *client.GetStorage(),
search_add_to_playlist(db, client.GetStorage(),
"", playlist, &filter);
return CommandResult::OK;
}

View File

@ -166,7 +166,7 @@ handle_playlistadd(Client &client, Request args, gcc_unused Response &r)
#ifdef ENABLE_DATABASE
const Database &db = client.GetDatabaseOrThrow();
search_add_to_playlist(db, *client.GetStorage(),
search_add_to_playlist(db, client.GetStorage(),
uri, playlist, nullptr);
#else
r.Error(ACK_ERROR_NO_EXIST, "directory or file not found");

View File

@ -28,7 +28,7 @@
#include <functional>
static void
AddSong(const Storage &storage, const char *playlist_path_utf8,
AddSong(const Storage *storage, const char *playlist_path_utf8,
const LightSong &song)
{
spl_append_song(playlist_path_utf8,
@ -36,14 +36,14 @@ AddSong(const Storage &storage, const char *playlist_path_utf8,
}
void
search_add_to_playlist(const Database &db, const Storage &storage,
search_add_to_playlist(const Database &db, const Storage *storage,
const char *uri, const char *playlist_path_utf8,
const SongFilter *filter)
{
const DatabaseSelection selection(uri, true, filter);
using namespace std::placeholders;
const auto f = std::bind(AddSong, std::ref(storage),
const auto f = std::bind(AddSong, storage,
playlist_path_utf8, _1);
db.Visit(selection, f);
}

View File

@ -28,7 +28,7 @@ class SongFilter;
gcc_nonnull(3,4)
void
search_add_to_playlist(const Database &db, const Storage &storage,
search_add_to_playlist(const Database &db, const Storage *storage,
const char *uri, const char *path_utf8,
const SongFilter *filter);

View File

@ -30,7 +30,7 @@
static void
AddToQueue(Partition &partition, const LightSong &song)
{
const Storage &storage = *partition.instance.storage;
const auto *storage = partition.instance.storage;
partition.playlist.AppendSong(partition.pc,
DatabaseDetachSong(storage,
song));

View File

@ -28,21 +28,21 @@
#include <assert.h>
DetachedSong
DatabaseDetachSong(const Storage &storage, const LightSong &song)
DatabaseDetachSong(const Storage *storage, const LightSong &song)
{
DetachedSong detached(song);
assert(detached.IsInDatabase());
if (!detached.HasRealURI()) {
if (!detached.HasRealURI() && storage != nullptr) {
const auto uri = song.GetURI();
detached.SetRealURI(storage.MapUTF8(uri.c_str()));
detached.SetRealURI(storage->MapUTF8(uri.c_str()));
}
return detached;
}
DetachedSong
DatabaseDetachSong(const Database &db, const Storage &storage, const char *uri)
DatabaseDetachSong(const Database &db, const Storage *storage, const char *uri)
{
const LightSong *tmp = db.GetSong(uri);
assert(tmp != nullptr);

View File

@ -33,7 +33,7 @@ class DetachedSong;
*/
gcc_pure
DetachedSong
DatabaseDetachSong(const Storage &storage, const LightSong &song);
DatabaseDetachSong(const Storage *storage, const LightSong &song);
/**
* Look up a song in the database and convert it to a #DetachedSong
@ -43,7 +43,7 @@ DatabaseDetachSong(const Storage &storage, const LightSong &song);
*/
gcc_pure
DetachedSong
DatabaseDetachSong(const Database &db, const Storage &storage,
DatabaseDetachSong(const Database &db, const Storage *storage,
const char *uri);
#endif

View File

@ -109,7 +109,7 @@ static const char *uri2 = "foo/bar.ogg";
DetachedSong
DatabaseDetachSong(gcc_unused const Database &db,
gcc_unused const Storage &_storage,
gcc_unused const Storage *_storage,
const char *uri)
{
if (strcmp(uri, uri2) == 0)