From 077af6b1cd514f9021987513c873704589baeb48 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Tue, 27 Oct 2020 10:18:52 +0100 Subject: [PATCH] refactoring: rename StorageBackendSerialized to StorageBackendFlatbuffers Change-Id: I1d6a8e089b31730e7a8e2f54ea0a3b2fe8548401 --- Makefile.am | 2 +- ...ialized.cpp => storage_backend_flatbuffers.cpp} | 64 +++++++++++----------- src/internal/storage_backend_flatbuffers.hpp | 57 +++++++++++++++++++ src/internal/storage_backend_serialized.hpp | 54 +----------------- 4 files changed, 92 insertions(+), 85 deletions(-) rename src/internal/{storage_backend_serialized.cpp => storage_backend_flatbuffers.cpp} (78%) create mode 100644 src/internal/storage_backend_flatbuffers.hpp diff --git a/Makefile.am b/Makefile.am index 3388f17..b52237d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -64,7 +64,7 @@ COMMON_SRC =\ src/internal/tslog.cpp \ src/internal/serializer_flatbuffers.cpp \ src/internal/print_content.cpp \ - src/internal/storage_backend_serialized.cpp \ + src/internal/storage_backend_flatbuffers.cpp \ src/internal/storage_backend_xml.cpp src_libdbuspolicy1_la_SOURCES =\ diff --git a/src/internal/storage_backend_serialized.cpp b/src/internal/storage_backend_flatbuffers.cpp similarity index 78% rename from src/internal/storage_backend_serialized.cpp rename to src/internal/storage_backend_flatbuffers.cpp index a099a1e..b0c6933 100644 --- a/src/internal/storage_backend_serialized.cpp +++ b/src/internal/storage_backend_flatbuffers.cpp @@ -17,7 +17,7 @@ #include "print_content.hpp" #include "serialized_convert.hpp" #include "serializer.hpp" -#include "storage_backend_serialized.hpp" +#include "storage_backend_flatbuffers.hpp" #include "transaction_guard.hpp" #include "tslog.hpp" #include @@ -123,7 +123,7 @@ ldp_xml_parser::DecisionItem getDecisionItemMaybeNull(const T &item, const P *po } // anonymous namespace -class StorageBackendSerialized::StorageBackendSerializedImpl { +class StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl { int fd{-1}; uint8_t *mem{static_cast(MAP_FAILED)}; size_t length{0}; @@ -148,7 +148,7 @@ public: const M *getPolicySet(); }; -ldp_xml_parser::DecisionItem StorageBackendSerialized::StorageBackendSerializedImpl::getDecisionFromSendIndex(const MatchItemSend &item, +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::getDecisionFromSendIndex(const MatchItemSend &item, const FB::PolicySend *policy) { const auto *index = policy->index(); @@ -217,7 +217,7 @@ ldp_xml_parser::DecisionItem StorageBackendSerialized::StorageBackendSerializedI return ldp_xml_parser::DecisionItem(ldp_xml_parser::Decision::ANY); } -void StorageBackendSerialized::StorageBackendSerializedImpl::releaseMMap() { +void StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::releaseMMap() { assert(MAP_FAILED != mem); assert(0 != length); @@ -228,7 +228,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::releaseMMap() { length = 0; } -void StorageBackendSerialized::StorageBackendSerializedImpl::releaseFD() { +void StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::releaseFD() { assert(-1 != fd); if (close(fd) != 0) @@ -237,7 +237,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::releaseFD() { fd = -1; } -void StorageBackendSerialized::StorageBackendSerializedImpl::release() { +void StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::release() { if (-1 != fd) { // we need to check it, because we may have initialized the storage directly from File * releaseMMap(); releaseFD(); @@ -250,7 +250,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::release() { file = nullptr; } -bool StorageBackendSerialized::StorageBackendSerializedImpl::init(const char *filename, bool verify) { +bool StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::init(const char *filename, bool verify) { assert(nullptr == file); auto err = [filename] (const char *what) { @@ -290,7 +290,7 @@ bool StorageBackendSerialized::StorageBackendSerializedImpl::init(const char *fi return true; } -bool StorageBackendSerialized::StorageBackendSerializedImpl::initFromData(const uint8_t *mem, size_t size, bool verify) { +bool StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::initFromData(const uint8_t *mem, size_t size, bool verify) { assert(nullptr == file); if (verify) { @@ -314,7 +314,7 @@ bool StorageBackendSerialized::StorageBackendSerializedImpl::initFromData(const return file != nullptr; } -bool StorageBackendSerialized::StorageBackendSerializedImpl::initFromXML(const char *config_name) { +bool StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::initFromXML(const char *config_name) { assert(nullptr == file); assert(nullptr == serializer.get()); @@ -328,7 +328,7 @@ bool StorageBackendSerialized::StorageBackendSerializedImpl::initFromXML(const c return initFromData(data, serialized_size, false); } -void StorageBackendSerialized::StorageBackendSerializedImpl::printContent(const bool xml_format) const { +void StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::printContent(const bool xml_format) const { print_content::use_xml_format(xml_format); std::cerr << *file; } @@ -342,7 +342,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::printContent(const typedef FB::Policy##T policy_type; \ }; \ template <> \ - auto StorageBackendSerialized::StorageBackendSerializedImpl::getPolicySet() \ + auto StorageBackendFlatbuffers::StorageBackendFlatbuffersImpl::getPolicySet() \ -> const typename type_helper::policy_set_type * { \ assert(file); \ return file->m_##t##_set(); \ @@ -353,43 +353,43 @@ TYPE_HELPER(Send, send) TYPE_HELPER(Receive, receive) TYPE_HELPER(Access, access) -bool StorageBackendSerialized::init(const char *filename, bool verify) { +bool StorageBackendFlatbuffers::init(const char *filename, bool verify) { return pimpl->init(filename, verify); } -bool StorageBackendSerialized::initFromData(const uint8_t *serialized_data, size_t length, bool verify) { +bool StorageBackendFlatbuffers::initFromData(const uint8_t *serialized_data, size_t length, bool verify) { return pimpl->initFromData(serialized_data, length, verify); } -bool StorageBackendSerialized::initFromXML(const char *config_name) { +bool StorageBackendFlatbuffers::initFromXML(const char *config_name) { return pimpl->initFromXML(config_name); } -void StorageBackendSerialized::release() { +void StorageBackendFlatbuffers::release() { pimpl->release(); } -void StorageBackendSerialized::printContent(const bool xml_format) const { +void StorageBackendFlatbuffers::printContent(const bool xml_format) const { pimpl->printContent(xml_format); } template -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextMandatory(const T &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const T &item) const { return getDecisionItem(item, pimpl->getPolicySet()->context_mandatory()); } template <> -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextMandatory(const MatchItemSend &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const MatchItemSend &item) const { return pimpl->getDecisionFromSendIndex(item, pimpl->getPolicySet()->context_mandatory()); } template <> -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextDefault(const MatchItemSend &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextDefault(const MatchItemSend &item) const { return pimpl->getDecisionFromSendIndex(item, pimpl->getPolicySet()->context_default()); } template <> -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemUser(uid_t uid, const MatchItemSend &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemUser(uid_t uid, const MatchItemSend &item) const { auto *policyPair = pimpl->getPolicySet()->user()->LookupByKey(uid); if (nullptr == policyPair) return ldp_xml_parser::Decision::ANY; @@ -397,28 +397,28 @@ ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemUser(uid_t } template -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextDefault(const T &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextDefault(const T &item) const { return getDecisionItem(item, pimpl->getPolicySet()->context_default()); } template -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemUser(uid_t uid, const T &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemUser(uid_t uid, const T &item) const { return getDecisionItemMaybeNull(item, pimpl->getPolicySet()->user()->LookupByKey(uid)); } template -ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemGroup(gid_t gid, const T &item) const { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemGroup(gid_t gid, const T &item) const { return getDecisionItemMaybeNull(item, pimpl->getPolicySet()->group()->LookupByKey(gid)); } template -bool StorageBackendSerialized::existsPolicyForGroup(gid_t gid) const { +bool StorageBackendFlatbuffers::existsPolicyForGroup(gid_t gid) const { return pimpl->getPolicySet()->group()->LookupByKey(gid) != nullptr; } #define T_INSTANTIATION(T) \ - template ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextMandatory(const ldp_xml_parser::MatchItem##T &item) const; \ - template ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemContextDefault(const ldp_xml_parser::MatchItem##T &item) const; + template ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const ldp_xml_parser::MatchItem##T &item) const; \ + template ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextDefault(const ldp_xml_parser::MatchItem##T &item) const; T_INSTANTIATION(Own) T_INSTANTIATION(Send) @@ -428,9 +428,9 @@ T_INSTANTIATION(Access) #undef T_INSTANTIATION #define T_INSTANTIATION2(T) \ - template ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemUser(uid_t uid, const ldp_xml_parser::MatchItem##T &item) const; \ - template ldp_xml_parser::DecisionItem StorageBackendSerialized::getDecisionItemGroup(gid_t gid, const ldp_xml_parser::MatchItem##T &item) const; \ - template bool StorageBackendSerialized::existsPolicyForGroup(gid_t) const; + template ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemUser(uid_t uid, const ldp_xml_parser::MatchItem##T &item) const; \ + template ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemGroup(gid_t gid, const ldp_xml_parser::MatchItem##T &item) const; \ + template bool StorageBackendFlatbuffers::existsPolicyForGroup(gid_t) const; T_INSTANTIATION2(Own) T_INSTANTIATION2(Send) @@ -438,11 +438,11 @@ T_INSTANTIATION2(Receive) #undef T_INSTANTIATION2 -StorageBackendSerialized::StorageBackendSerialized() - : pimpl{new StorageBackendSerializedImpl} { +StorageBackendFlatbuffers::StorageBackendFlatbuffers() + : pimpl{new StorageBackendFlatbuffersImpl} { } -StorageBackendSerialized::~StorageBackendSerialized() { +StorageBackendFlatbuffers::~StorageBackendFlatbuffers() { pimpl->release(); } diff --git a/src/internal/storage_backend_flatbuffers.hpp b/src/internal/storage_backend_flatbuffers.hpp new file mode 100644 index 0000000..aaba006 --- /dev/null +++ b/src/internal/storage_backend_flatbuffers.hpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +#pragma once + +#include "policy.hpp" + +#include + +namespace ldp_serialized { + +class StorageBackendFlatbuffers { + class StorageBackendFlatbuffersImpl; + std::unique_ptr pimpl; +public: + StorageBackendFlatbuffers(); + ~StorageBackendFlatbuffers(); + + bool init(const char *filename, bool verify = false); + bool initFromData(const uint8_t *serialized_data, size_t length, bool verify = false); + bool initFromXML(const char *config_name); + void release(); + + void printContent(const bool xml_format = false) const; + + // Supported template parameters are: + // MatchPolicyOwn, MatchPolicySend, MatchPolicyReceive + // and - only for Contexts - MatchPolicyAccess + template + ldp_xml_parser::DecisionItem getDecisionItemContextMandatory(const T &item) const; + template + ldp_xml_parser::DecisionItem getDecisionItemContextDefault(const T &item) const; + template + ldp_xml_parser::DecisionItem getDecisionItemUser(uid_t uid, const T &item) const; + template + ldp_xml_parser::DecisionItem getDecisionItemGroup(gid_t gid, const T &item) const; + + // This works with T set to MatchItemOwn, MatchItemSend or MatchItemReceive + // This is needed for filtering mapGroups. Check NaivePolicyChecker. + template + bool existsPolicyForGroup(gid_t gid) const; + +}; + +} diff --git a/src/internal/storage_backend_serialized.hpp b/src/internal/storage_backend_serialized.hpp index 4781b87..7755dd3 100644 --- a/src/internal/storage_backend_serialized.hpp +++ b/src/internal/storage_backend_serialized.hpp @@ -1,57 +1,7 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -*/ #pragma once -#include "policy.hpp" - -#include +#include "storage_backend_flatbuffers.hpp" namespace ldp_serialized { - -class StorageBackendSerialized { - class StorageBackendSerializedImpl; - std::unique_ptr pimpl; -public: - StorageBackendSerialized(); - ~StorageBackendSerialized(); - - bool init(const char *filename, bool verify = false); - bool initFromData(const uint8_t *serialized_data, size_t length, bool verify = false); - bool initFromXML(const char *config_name); - void release(); - - void printContent(const bool xml_format = false) const; - - // Supported template parameters are: - // MatchPolicyOwn, MatchPolicySend, MatchPolicyReceive - // and - only for Contexts - MatchPolicyAccess - template - ldp_xml_parser::DecisionItem getDecisionItemContextMandatory(const T &item) const; - template - ldp_xml_parser::DecisionItem getDecisionItemContextDefault(const T &item) const; - template - ldp_xml_parser::DecisionItem getDecisionItemUser(uid_t uid, const T &item) const; - template - ldp_xml_parser::DecisionItem getDecisionItemGroup(gid_t gid, const T &item) const; - - // This works with T set to MatchItemOwn, MatchItemSend or MatchItemReceive - // This is needed for filtering mapGroups. Check NaivePolicyChecker. - template - bool existsPolicyForGroup(gid_t gid) const; - -}; - +using StorageBackendSerialized = StorageBackendFlatbuffers; } -- 2.7.4