storage/Plugin: return std::unique_ptr<Storage>

This commit is contained in:
Max Kellermann 2018-01-02 16:11:17 +01:00
parent 3f4f7b0a53
commit 3c5e4e2788
15 changed files with 46 additions and 32 deletions

View File

@ -175,13 +175,13 @@ glue_mapper_init()
static void
InitStorage(EventLoop &event_loop)
{
Storage *storage = CreateConfiguredStorage(event_loop);
auto storage = CreateConfiguredStorage(event_loop);
if (storage == nullptr)
return;
CompositeStorage *composite = new CompositeStorage();
instance->storage = composite;
composite->Mount("", storage);
composite->Mount("", storage.release());
}
/**

View File

@ -199,13 +199,13 @@ handle_mount(Client &client, Request args, Response &r)
}
auto &event_loop = instance.io_thread.GetEventLoop();
Storage *storage = CreateStorageURI(event_loop, remote_uri);
auto storage = CreateStorageURI(event_loop, remote_uri);
if (storage == nullptr) {
r.Error(ACK_ERROR_ARG, "Unrecognized storage URI");
return CommandResult::ERROR;
}
composite.Mount(local_uri, storage);
composite.Mount(local_uri, storage.release());
instance.EmitIdle(IDLE_MOUNT);
#ifdef ENABLE_DATABASE

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "Configured.hxx"
#include "Registry.hxx"
#include "StorageInterface.hxx"
#include "plugins/LocalStorage.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigError.hxx"
@ -30,10 +31,10 @@
#include <assert.h>
static Storage *
static std::unique_ptr<Storage>
CreateConfiguredStorageUri(EventLoop &event_loop, const char *uri)
{
Storage *storage = CreateStorageURI(event_loop, uri);
auto storage = CreateStorageURI(event_loop, uri);
if (storage == nullptr)
throw FormatRuntimeError("Unrecognized storage URI: %s", uri);
@ -50,7 +51,7 @@ GetConfiguredMusicDirectory()
return path;
}
static Storage *
static std::unique_ptr<Storage>
CreateConfiguredStorageLocal()
{
AllocatedPath path = GetConfiguredMusicDirectory();
@ -62,7 +63,7 @@ CreateConfiguredStorageLocal()
return CreateLocalStorage(path);
}
Storage *
std::unique_ptr<Storage>
CreateConfiguredStorage(EventLoop &event_loop)
{
auto uri = config_get_string(ConfigOption::MUSIC_DIR);

View File

@ -23,6 +23,8 @@
#include "check.h"
#include "Compiler.h"
#include <memory>
class Storage;
class EventLoop;
@ -32,7 +34,7 @@ class EventLoop;
*
* Throws #std::runtime_error on error.
*/
Storage *
std::unique_ptr<Storage>
CreateConfiguredStorage(EventLoop &event_loop);
/**

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "Registry.hxx"
#include "StoragePlugin.hxx"
#include "StorageInterface.hxx"
#include "plugins/LocalStorage.hxx"
#include "plugins/SmbclientStorage.hxx"
#include "plugins/NfsStorage.hxx"
@ -54,7 +55,7 @@ GetStoragePluginByName(const char *name) noexcept
return nullptr;
}
Storage *
std::unique_ptr<Storage>
CreateStorageURI(EventLoop &event_loop, const char *uri)
{
for (auto i = storage_plugins; *i != nullptr; ++i) {
@ -63,7 +64,7 @@ CreateStorageURI(EventLoop &event_loop, const char *uri)
if (plugin.create_uri == nullptr)
continue;
Storage *storage = plugin.create_uri(event_loop, uri);
auto storage = plugin.create_uri(event_loop, uri);
if (storage != nullptr)
return storage;
}

View File

@ -23,6 +23,8 @@
#include "check.h"
#include "Compiler.h"
#include <memory>
struct StoragePlugin;
class Storage;
class EventLoop;
@ -37,8 +39,8 @@ gcc_nonnull_all gcc_pure
const StoragePlugin *
GetStoragePluginByName(const char *name) noexcept;
gcc_nonnull_all gcc_malloc
Storage *
gcc_nonnull_all
std::unique_ptr<Storage>
CreateStorageURI(EventLoop &event_loop, const char *uri);
#endif

View File

@ -22,6 +22,8 @@
#include "check.h"
#include <memory>
class Storage;
class EventLoop;
@ -31,7 +33,8 @@ struct StoragePlugin {
/**
* Throws #std::runtime_error on error.
*/
Storage *(*create_uri)(EventLoop &event_loop, const char *uri);
std::unique_ptr<Storage> (*create_uri)(EventLoop &event_loop,
const char *uri);
};
#endif

View File

@ -101,7 +101,7 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
FormatDebug(storage_domain, "Restoring mount %s => %s", uri.c_str(), url.c_str());
auto &event_loop = instance.io_thread.GetEventLoop();
Storage *storage = CreateStorageURI(event_loop, url.c_str());
auto storage = CreateStorageURI(event_loop, url.c_str());
if (storage == nullptr) {
FormatError(storage_domain, "Unrecognized storage URI: %s", url.c_str());
return true;
@ -112,7 +112,6 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
try {
((SimpleDatabase *)db)->Mount(uri.c_str(), url.c_str());
} catch (...) {
delete storage;
FormatError(std::current_exception(),
"Failed to restore mount to %s",
url.c_str());
@ -120,7 +119,8 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
}
}
((CompositeStorage*)instance.storage)->Mount(uri.c_str(), storage);
((CompositeStorage*)instance.storage)->Mount(uri.c_str(),
storage.release());
return true;
}

View File

@ -549,14 +549,14 @@ CurlStorage::OpenDirectory(const char *uri_utf8)
return HttpListDirectoryOperation(*curl, uri.c_str()).Perform();
}
static Storage *
static std::unique_ptr<Storage>
CreateCurlStorageURI(EventLoop &event_loop, const char *uri)
{
if (strncmp(uri, "http://", 7) != 0 &&
strncmp(uri, "https://", 8) != 0)
return nullptr;
return new CurlStorage(event_loop, uri);
return std::make_unique<CurlStorage>(event_loop, uri);
}
const StoragePlugin curl_storage_plugin = {

View File

@ -179,10 +179,10 @@ LocalDirectoryReader::GetInfo(bool follow)
return Stat(AllocatedPath::Build(base_fs, reader.GetEntry()), follow);
}
Storage *
std::unique_ptr<Storage>
CreateLocalStorage(Path base_fs)
{
return new LocalStorage(base_fs);
return std::make_unique<LocalStorage>(base_fs);
}
const StoragePlugin local_storage_plugin = {

View File

@ -23,14 +23,16 @@
#include "check.h"
#include "Compiler.h"
#include <memory>
struct StoragePlugin;
class Storage;
class Path;
extern const StoragePlugin local_storage_plugin;
gcc_malloc gcc_nonnull_all
Storage *
gcc_nonnull_all
std::unique_ptr<Storage>
CreateLocalStorage(Path base_fs);
#endif

View File

@ -390,7 +390,7 @@ NfsStorage::OpenDirectory(const char *uri_utf8)
return operation.ToReader();
}
static Storage *
static std::unique_ptr<Storage>
CreateNfsStorageURI(EventLoop &event_loop, const char *base)
{
if (strncmp(base, "nfs://", 6) != 0)
@ -406,7 +406,8 @@ CreateNfsStorageURI(EventLoop &event_loop, const char *base)
nfs_set_base(server.c_str(), mount);
return new NfsStorage(event_loop, base, server.c_str(), mount);
return std::make_unique<NfsStorage>(event_loop, base,
server.c_str(), mount);
}
const StoragePlugin nfs_storage_plugin = {

View File

@ -179,7 +179,7 @@ SmbclientDirectoryReader::GetInfo(gcc_unused bool follow)
return ::GetInfo(path.c_str());
}
static Storage *
static std::unique_ptr<Storage>
CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base)
{
if (strncmp(base, "smb://", 6) != 0)
@ -198,7 +198,7 @@ CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base)
throw MakeErrno("smbc_new_context() failed");
}
return new SmbclientStorage(base, ctx2);
return std::make_unique<SmbclientStorage>(base, ctx2);
}
const StoragePlugin smbclient_storage_plugin = {

View File

@ -34,10 +34,10 @@
#include <string.h>
#include <time.h>
static Storage *
static std::unique_ptr<Storage>
MakeStorage(EventLoop &event_loop, const char *uri)
{
Storage *storage = CreateStorageURI(event_loop, uri);
auto storage = CreateStorageURI(event_loop, uri);
if (storage == nullptr)
throw std::runtime_error("Unrecognized storage URI");
@ -108,8 +108,8 @@ try {
const char *const path = argv[3];
std::unique_ptr<Storage> storage(MakeStorage(io_thread.GetEventLoop(),
storage_uri));
auto storage = MakeStorage(io_thread.GetEventLoop(),
storage_uri);
return Ls(*storage, path);
} else {

View File

@ -14,6 +14,7 @@
#include "ls.hxx"
#include "Log.hxx"
#include "db/DatabaseSong.hxx"
#include "storage/StorageInterface.hxx"
#include "storage/plugins/LocalStorage.hxx"
#include "Mapper.hxx"
#include "util/ChronoUtil.hxx"
@ -309,7 +310,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(TranslateSongTest);
int
main(gcc_unused int argc, gcc_unused char **argv)
{
storage = CreateLocalStorage(Path::FromFS(music_directory));
auto _storage = CreateLocalStorage(Path::FromFS(music_directory));
storage = _storage.get();
CppUnit::TextUi::TestRunner runner;
auto &registry = CppUnit::TestFactoryRegistry::getRegistry();