Commit a0088ccc authored by Max Kellermann's avatar Max Kellermann

storage: add struct StoragePlugin and a plugin registry

parent be081929
...@@ -425,6 +425,8 @@ if ENABLE_DATABASE ...@@ -425,6 +425,8 @@ if ENABLE_DATABASE
noinst_LIBRARIES += libstorage.a noinst_LIBRARIES += libstorage.a
libstorage_a_SOURCES = \ libstorage_a_SOURCES = \
src/storage/StoragePlugin.hxx \
src/storage/Registry.cxx src/storage/Registry.hxx \
src/storage/StorageInterface.cxx src/storage/StorageInterface.hxx \ src/storage/StorageInterface.cxx src/storage/StorageInterface.hxx \
src/storage/plugins/LocalStorage.cxx src/storage/plugins/LocalStorage.hxx \ src/storage/plugins/LocalStorage.cxx src/storage/plugins/LocalStorage.hxx \
src/storage/FileInfo.hxx src/storage/FileInfo.hxx
......
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
#include "db/Registry.hxx" #include "db/Registry.hxx"
#include "db/DatabasePlugin.hxx" #include "db/DatabasePlugin.hxx"
#include "storage/Registry.hxx"
#include "storage/StoragePlugin.hxx"
#endif #endif
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
...@@ -113,6 +115,12 @@ static void version(void) ...@@ -113,6 +115,12 @@ static void version(void)
for (auto i = database_plugins; *i != nullptr; ++i) for (auto i = database_plugins; *i != nullptr; ++i)
printf(" %s", (*i)->name); printf(" %s", (*i)->name);
puts("\n\n"
"Storage plugins:");
for (auto i = storage_plugins; *i != nullptr; ++i)
printf(" %s", (*i)->name);
#endif #endif
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
......
/*
* Copyright (C) 2003-2014 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.
*/
#include "config.h"
#include "Registry.hxx"
#include "StoragePlugin.hxx"
#include "plugins/LocalStorage.hxx"
#include "plugins/SmbclientStorage.hxx"
#include "util/Error.hxx"
#include <assert.h>
#include <string.h>
const StoragePlugin *const storage_plugins[] = {
&local_storage_plugin,
#ifdef ENABLE_SMBCLIENT
&smbclient_storage_plugin,
#endif
nullptr
};
const StoragePlugin *
GetStoragePluginByName(const char *name)
{
for (auto i = storage_plugins; *i != nullptr; ++i) {
const StoragePlugin &plugin = **i;
if (strcmp(plugin.name, name) == 0)
return *i;
}
return nullptr;
}
Storage *
CreateStorageURI(const char *uri, Error &error)
{
assert(!error.IsDefined());
for (auto i = storage_plugins; *i != nullptr; ++i) {
const StoragePlugin &plugin = **i;
if (plugin.create_uri == nullptr)
continue;
Storage *storage = plugin.create_uri(uri, error);
if (storage != nullptr || error.IsDefined())
return storage;
}
return nullptr;
}
/*
* Copyright (C) 2003-2014 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.
*/
#ifndef MPD_STORAGE_REGISTRY_HXX
#define MPD_STORAGE_REGISTRY_HXX
#include "check.h"
#include "Compiler.h"
struct StoragePlugin;
class Storage;
class Error;
/**
* nullptr terminated list of all storage plugins which were enabled at
* compile time.
*/
extern const StoragePlugin *const storage_plugins[];
gcc_nonnull_all gcc_pure
const StoragePlugin *
GetStoragePluginByName(const char *name);
gcc_nonnull_all gcc_malloc
Storage *
CreateStorageURI(const char *uri, Error &error);
#endif
/*
* Copyright (C) 2003-2014 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.
*/
#ifndef MPD_STORAGE_PLUGIN_HXX
#define MPD_STORAGE_PLUGIN_HXX
#include "check.h"
class Error;
class Storage;
struct StoragePlugin {
const char *name;
Storage *(*create_uri)(const char *uri, Error &error);
};
#endif
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "LocalStorage.hxx" #include "LocalStorage.hxx"
#include "storage/StoragePlugin.hxx"
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
...@@ -210,3 +211,8 @@ CreateLocalStorage(Path base_fs) ...@@ -210,3 +211,8 @@ CreateLocalStorage(Path base_fs)
{ {
return new LocalStorage(base_fs); return new LocalStorage(base_fs);
} }
const StoragePlugin local_storage_plugin = {
"local",
nullptr,
};
...@@ -23,9 +23,12 @@ ...@@ -23,9 +23,12 @@
#include "check.h" #include "check.h"
#include "Compiler.h" #include "Compiler.h"
struct StoragePlugin;
class Storage; class Storage;
class Path; class Path;
extern const StoragePlugin local_storage_plugin;
gcc_malloc gcc_nonnull_all gcc_malloc gcc_nonnull_all
Storage * Storage *
CreateLocalStorage(Path base_fs); CreateLocalStorage(Path base_fs);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "SmbclientStorage.hxx" #include "SmbclientStorage.hxx"
#include "storage/StoragePlugin.hxx"
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx" #include "storage/FileInfo.hxx"
#include "lib/smbclient/Init.hxx" #include "lib/smbclient/Init.hxx"
...@@ -178,9 +179,12 @@ SmbclientDirectoryReader::GetInfo(gcc_unused bool follow, FileInfo &info, ...@@ -178,9 +179,12 @@ SmbclientDirectoryReader::GetInfo(gcc_unused bool follow, FileInfo &info,
return ::GetInfo(path.c_str(), info, error); return ::GetInfo(path.c_str(), info, error);
} }
Storage * static Storage *
CreateSmbclientStorage(const char *base, Error &error) CreateSmbclientStorageURI(const char *base, Error &error)
{ {
if (memcmp(base, "smb://", 6) != 0)
return nullptr;
if (!SmbclientInit(error)) if (!SmbclientInit(error))
return nullptr; return nullptr;
...@@ -200,3 +204,8 @@ CreateSmbclientStorage(const char *base, Error &error) ...@@ -200,3 +204,8 @@ CreateSmbclientStorage(const char *base, Error &error)
return new SmbclientStorage(base, ctx2); return new SmbclientStorage(base, ctx2);
} }
const StoragePlugin smbclient_storage_plugin = {
"smbclient",
CreateSmbclientStorageURI,
};
...@@ -22,10 +22,8 @@ ...@@ -22,10 +22,8 @@
#include "check.h" #include "check.h"
class Error; struct StoragePlugin;
class Storage;
Storage * extern const StoragePlugin smbclient_storage_plugin;
CreateSmbclientStorage(const char *base, Error &error);
#endif #endif
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