From a0088ccce1749dbef6503fbf489f7096d824c11d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 5 Feb 2014 19:23:02 +0100 Subject: storage: add struct StoragePlugin and a plugin registry --- src/storage/Registry.cxx | 67 ++++++++++++++++++++++++++++++++ src/storage/Registry.hxx | 44 +++++++++++++++++++++ src/storage/StoragePlugin.hxx | 34 ++++++++++++++++ src/storage/plugins/LocalStorage.cxx | 6 +++ src/storage/plugins/LocalStorage.hxx | 3 ++ src/storage/plugins/SmbclientStorage.cxx | 13 ++++++- src/storage/plugins/SmbclientStorage.hxx | 6 +-- 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 src/storage/Registry.cxx create mode 100644 src/storage/Registry.hxx create mode 100644 src/storage/StoragePlugin.hxx (limited to 'src/storage') diff --git a/src/storage/Registry.cxx b/src/storage/Registry.cxx new file mode 100644 index 000000000..b81349510 --- /dev/null +++ b/src/storage/Registry.cxx @@ -0,0 +1,67 @@ +/* + * 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 +#include + +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; +} diff --git a/src/storage/Registry.hxx b/src/storage/Registry.hxx new file mode 100644 index 000000000..9696b3de1 --- /dev/null +++ b/src/storage/Registry.hxx @@ -0,0 +1,44 @@ +/* + * 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 diff --git a/src/storage/StoragePlugin.hxx b/src/storage/StoragePlugin.hxx new file mode 100644 index 000000000..d91caf24b --- /dev/null +++ b/src/storage/StoragePlugin.hxx @@ -0,0 +1,34 @@ +/* + * 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 diff --git a/src/storage/plugins/LocalStorage.cxx b/src/storage/plugins/LocalStorage.cxx index 2bf430b2e..34d11569c 100644 --- a/src/storage/plugins/LocalStorage.cxx +++ b/src/storage/plugins/LocalStorage.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "LocalStorage.hxx" +#include "storage/StoragePlugin.hxx" #include "storage/StorageInterface.hxx" #include "storage/FileInfo.hxx" #include "util/Error.hxx" @@ -210,3 +211,8 @@ CreateLocalStorage(Path base_fs) { return new LocalStorage(base_fs); } + +const StoragePlugin local_storage_plugin = { + "local", + nullptr, +}; diff --git a/src/storage/plugins/LocalStorage.hxx b/src/storage/plugins/LocalStorage.hxx index e80fd8276..7295d38e7 100644 --- a/src/storage/plugins/LocalStorage.hxx +++ b/src/storage/plugins/LocalStorage.hxx @@ -23,9 +23,12 @@ #include "check.h" #include "Compiler.h" +struct StoragePlugin; class Storage; class Path; +extern const StoragePlugin local_storage_plugin; + gcc_malloc gcc_nonnull_all Storage * CreateLocalStorage(Path base_fs); diff --git a/src/storage/plugins/SmbclientStorage.cxx b/src/storage/plugins/SmbclientStorage.cxx index 6d61ab75e..a73c8d65c 100644 --- a/src/storage/plugins/SmbclientStorage.cxx +++ b/src/storage/plugins/SmbclientStorage.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "SmbclientStorage.hxx" +#include "storage/StoragePlugin.hxx" #include "storage/StorageInterface.hxx" #include "storage/FileInfo.hxx" #include "lib/smbclient/Init.hxx" @@ -178,9 +179,12 @@ SmbclientDirectoryReader::GetInfo(gcc_unused bool follow, FileInfo &info, return ::GetInfo(path.c_str(), info, error); } -Storage * -CreateSmbclientStorage(const char *base, Error &error) +static Storage * +CreateSmbclientStorageURI(const char *base, Error &error) { + if (memcmp(base, "smb://", 6) != 0) + return nullptr; + if (!SmbclientInit(error)) return nullptr; @@ -200,3 +204,8 @@ CreateSmbclientStorage(const char *base, Error &error) return new SmbclientStorage(base, ctx2); } + +const StoragePlugin smbclient_storage_plugin = { + "smbclient", + CreateSmbclientStorageURI, +}; diff --git a/src/storage/plugins/SmbclientStorage.hxx b/src/storage/plugins/SmbclientStorage.hxx index 3a77ebf1d..7c198d920 100644 --- a/src/storage/plugins/SmbclientStorage.hxx +++ b/src/storage/plugins/SmbclientStorage.hxx @@ -22,10 +22,8 @@ #include "check.h" -class Error; -class Storage; +struct StoragePlugin; -Storage * -CreateSmbclientStorage(const char *base, Error &error); +extern const StoragePlugin smbclient_storage_plugin; #endif -- cgit v1.2.3