Commit ef24cfa5 authored by Max Kellermann's avatar Max Kellermann

storage/Plugin: add "prefixes"

parent 5d359832
......@@ -58,13 +58,25 @@ GetStoragePluginByName(const char *name) noexcept
return nullptr;
}
const StoragePlugin *
GetStoragePluginByUri(const char *uri) noexcept
{
for (auto i = storage_plugins; *i != nullptr; ++i) {
const StoragePlugin &plugin = **i;
if (plugin.SupportsUri(uri))
return *i;
}
return nullptr;
}
std::unique_ptr<Storage>
CreateStorageURI(EventLoop &event_loop, const char *uri)
{
for (auto i = storage_plugins; *i != nullptr; ++i) {
const StoragePlugin &plugin = **i;
if (plugin.create_uri == nullptr)
if (plugin.create_uri == nullptr || !plugin.SupportsUri(uri))
continue;
auto storage = plugin.create_uri(event_loop, uri);
......
......@@ -38,6 +38,10 @@ gcc_nonnull_all gcc_pure
const StoragePlugin *
GetStoragePluginByName(const char *name) noexcept;
gcc_nonnull_all gcc_pure
const StoragePlugin *
GetStoragePluginByUri(const char *uri) noexcept;
gcc_nonnull_all
std::unique_ptr<Storage>
CreateStorageURI(EventLoop &event_loop, const char *uri);
......
/*
* Copyright 2003-2021 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 "StoragePlugin.hxx"
#include "util/StringCompare.hxx"
bool
StoragePlugin::SupportsUri(const char *uri) const noexcept
{
if (prefixes == nullptr)
return false;
for (auto i = prefixes; *i != nullptr; ++i)
if (StringStartsWithIgnoreCase(uri, *i))
return true;
return false;
}
......@@ -29,10 +29,19 @@ struct StoragePlugin {
const char *name;
/**
* A nullptr-terminated list of URI prefixes handled by this
* plugin. This is usually a string in the form "scheme://".
*/
const char *const*prefixes;
/**
* Throws #std::runtime_error on error.
*/
std::unique_ptr<Storage> (*create_uri)(EventLoop &event_loop,
const char *uri);
[[gnu::pure]]
bool SupportsUri(const char *uri) const noexcept;
};
#endif
storage_api = static_library(
'storage_api',
'StorageInterface.cxx',
'StoragePlugin.cxx',
include_directories: inc,
)
......
......@@ -570,14 +570,15 @@ CurlStorage::OpenDirectory(std::string_view uri_utf8)
static std::unique_ptr<Storage>
CreateCurlStorageURI(EventLoop &event_loop, const char *uri)
{
if (!StringStartsWithCaseASCII(uri, "http://") &&
!StringStartsWithCaseASCII(uri, "https://"))
return nullptr;
return std::make_unique<CurlStorage>(event_loop, uri);
}
static constexpr const char *curl_prefixes[] = {
"http://", "https://", nullptr
};
const StoragePlugin curl_storage_plugin = {
"curl",
curl_prefixes,
CreateCurlStorageURI,
};
......@@ -173,4 +173,5 @@ CreateLocalStorage(Path base_fs)
constexpr StoragePlugin local_storage_plugin = {
"local",
nullptr,
nullptr,
};
......@@ -425,7 +425,10 @@ CreateNfsStorageURI(EventLoop &event_loop, const char *base)
server.c_str(), mount);
}
static constexpr const char *nfs_prefixes[] = { "nfs://", nullptr };
const StoragePlugin nfs_storage_plugin = {
"nfs",
nfs_prefixes,
CreateNfsStorageURI,
};
......@@ -377,7 +377,10 @@ CreateUdisksStorageURI(EventLoop &event_loop, const char *base_uri)
std::move(inside_path));
}
static constexpr const char *udisks_prefixes[] = { "udisks://", nullptr };
const StoragePlugin udisks_storage_plugin = {
"udisks",
udisks_prefixes,
CreateUdisksStorageURI,
};
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