Lay out initial parts for the Storage framework

This includes a manager, component manager, BIF and script code, and
parts to support new storage backend plugins.
This commit is contained in:
Tim Wojtulewicz 2023-09-11 12:21:58 -07:00
parent 3d6e7c85b0
commit 2ea0f3e70a
32 changed files with 874 additions and 1 deletions

58
src/storage/Manager.cc Normal file
View file

@ -0,0 +1,58 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek/storage/Manager.h"
#include "zeek/Desc.h"
namespace zeek::storage {
Manager::Manager() : plugin::ComponentManager<storage::Component>("Storage", "Backend") {}
void Manager::InitPostScript() { detail::backend_opaque = make_intrusive<OpaqueType>("Storage::Backend"); }
zeek::expected<BackendPtr, std::string> Manager::OpenBackend(const Tag& type, RecordValPtr options) {
Component* c = Lookup(type);
if ( ! c ) {
return zeek::unexpected<std::string>(
util::fmt("Request to open unknown backend (%d:%d)", type.Type(), type.Subtype()));
}
if ( ! c->Factory() ) {
return zeek::unexpected<std::string>(
util::fmt("Factory invalid for backend %s", GetComponentName(type).c_str()));
}
ODesc d;
type.AsVal()->Describe(&d);
BackendPtr bp = c->Factory()(d.Description());
if ( ! bp ) {
return zeek::unexpected<std::string>(
util::fmt("Failed to instantiate backend %s", GetComponentName(type).c_str()));
}
if ( auto res = bp->Open(std::move(options)); res.has_value() ) {
return zeek::unexpected<std::string>(
util::fmt("Failed to open backend %s: %s", GetComponentName(type).c_str(), res.value().c_str()));
}
// TODO: post Storage::backend_opened event
backends.push_back(bp);
return bp;
}
void Manager::CloseBackend(BackendPtr backend) {
auto it = std::find(backends.begin(), backends.end(), backend);
if ( it == backends.end() )
return;
backends.erase(it);
backend->Close();
// TODO: post Storage::backend_lost event
}
} // namespace zeek::storage