storage: add struct StoragePlugin and a plugin registry
This commit is contained in:
parent
be081929f4
commit
a0088ccce1
@ -425,6 +425,8 @@ if ENABLE_DATABASE
|
||||
noinst_LIBRARIES += libstorage.a
|
||||
|
||||
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/plugins/LocalStorage.cxx src/storage/plugins/LocalStorage.hxx \
|
||||
src/storage/FileInfo.hxx
|
||||
|
@ -43,6 +43,8 @@
|
||||
#ifdef ENABLE_DATABASE
|
||||
#include "db/Registry.hxx"
|
||||
#include "db/DatabasePlugin.hxx"
|
||||
#include "storage/Registry.hxx"
|
||||
#include "storage/StoragePlugin.hxx"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
||||
@ -113,6 +115,12 @@ static void version(void)
|
||||
|
||||
for (auto i = database_plugins; *i != nullptr; ++i)
|
||||
printf(" %s", (*i)->name);
|
||||
|
||||
puts("\n\n"
|
||||
"Storage plugins:");
|
||||
|
||||
for (auto i = storage_plugins; *i != nullptr; ++i)
|
||||
printf(" %s", (*i)->name);
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
||||
|
67
src/storage/Registry.cxx
Normal file
67
src/storage/Registry.cxx
Normal file
@ -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 <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;
|
||||
}
|
44
src/storage/Registry.hxx
Normal file
44
src/storage/Registry.hxx
Normal file
@ -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
|
34
src/storage/StoragePlugin.hxx
Normal file
34
src/storage/StoragePlugin.hxx
Normal file
@ -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
|
@ -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,
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user