Skip to content

Commit

Permalink
stored: remove BackendInterface layer
Browse files Browse the repository at this point in the history
Instead of returning a factory that will then provide Device*, we can
return the Device* from the PluginRegistry's factory directly. This
removes the BackendInterface layer that wrapped the factory again.
  • Loading branch information
arogge committed Nov 7, 2022
1 parent ef44197 commit 0a34670
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 34 deletions.
10 changes: 2 additions & 8 deletions core/src/stored/dev.cc
Expand Up @@ -122,20 +122,14 @@ Device* FactoryCreateDevice(JobControlRecord* jcr,
Dmsg1(400, "max_block_size in device_resource res is %u\n",
device_resource->max_block_size);

if (!PluginRegistry<BackendInterface>::IsRegistered(
device_resource->dev_type)) {
if (!PluginRegistry<Device>::IsRegistered(device_resource->dev_type)) {
Jmsg2(jcr, M_ERROR, 0, _("%s has an unknown device type %s\n"),
device_resource->archive_device_string,
device_resource->dev_type.c_str());
return nullptr;
}

// FIXME: the PluginRegistry should return the device instead of the factory
// so we save one call and a delete.
auto factory
= PluginRegistry<BackendInterface>::Create(device_resource->dev_type);
Device* dev = factory->GetDevice();
delete factory;
Device* dev = PluginRegistry<Device>::Create(device_resource->dev_type);

dev->device_resource = device_resource;
device_resource->dev = dev;
Expand Down
26 changes: 5 additions & 21 deletions core/src/stored/sd_backends.h
@@ -1,8 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2014-2014 Planets Communications B.V.
Copyright (C) 2014-2022 Bareos GmbH & Co. KG
Copyright (C) 2022-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -19,11 +18,6 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
// Marco van Wieringen, June 2014
/**
* @file
* Dynamic loading of SD backend plugins.
*/

#ifndef BAREOS_STORED_SD_BACKENDS_H_
#define BAREOS_STORED_SD_BACKENDS_H_
Expand All @@ -32,18 +26,8 @@

namespace storagedaemon {

class BackendInterface {
public:
virtual ~BackendInterface() = default;
virtual Device* GetDevice() = 0;
};

template <typename T> class Backend : public BackendInterface {
public:
Device* GetDevice(void) override { return new T(); }
};

template <typename T> BackendInterface* BackendFactory(void) { return new T(); }
class Device;
template <typename T> Device* DeviceFactory(void) { return new T(); }

#if defined(HAVE_DYNAMIC_SD_BACKENDS)
bool LoadStorageBackend(const std::string& dev_type,
Expand All @@ -52,8 +36,8 @@ bool LoadStorageBackend(const std::string& dev_type,

#define REGISTER_SD_BACKEND(backend_name, backend_class) \
[[maybe_unused]] static bool backend_name##_backend_ \
= PluginRegistry<BackendInterface>::Add( \
#backend_name, BackendFactory<Backend<backend_class>>);
= PluginRegistry<Device>::Add(#backend_name, \
DeviceFactory<backend_class>);

} // namespace storagedaemon

Expand Down
2 changes: 1 addition & 1 deletion core/src/stored/sd_backends_dynamic.cc
Expand Up @@ -65,7 +65,7 @@ bool LoadStorageBackend(const std::string& dev_type,
return false;
}

if (!PluginRegistry<BackendInterface>::IsRegistered(dev_type)) {
if (!PluginRegistry<Device>::IsRegistered(dev_type)) {
Jmsg(nullptr, M_ERROR_TERM, 0,
"Loaded backend library for %s did not register its backend. This is "
"probably a bug in the backend library.\n",
Expand Down
5 changes: 1 addition & 4 deletions core/src/stored/stored_conf.cc
Expand Up @@ -547,20 +547,17 @@ static void GuessMissingDeviceTypes(ConfigurationParser& my_config)

static void CheckAndLoadDeviceBackends(ConfigurationParser& my_config)
{
PluginRegistry<BackendInterface>::DumpDbg();

#if defined(HAVE_DYNAMIC_SD_BACKENDS)
auto storage_res
= dynamic_cast<StorageResource*>(my_config.GetNextRes(R_STORAGE, NULL));
#endif

BareosResource* p = nullptr;

while ((p = my_config.GetNextRes(R_DEVICE, p)) != nullptr) {
DeviceResource* d = dynamic_cast<DeviceResource*>(p);
if (d) {
to_lower(d->dev_type);
if (!PluginRegistry<BackendInterface>::IsRegistered(d->dev_type)) {
if (!PluginRegistry<Device>::IsRegistered(d->dev_type)) {
#if defined(HAVE_DYNAMIC_SD_BACKENDS)
if (!storage_res || storage_res->backend_directories.empty()) {
Jmsg2(nullptr, M_ERROR_TERM, 0,
Expand Down

0 comments on commit 0a34670

Please sign in to comment.