From: Zofia Abramowska Date: Mon, 16 Mar 2015 16:53:15 +0000 (+0100) Subject: Remove shared_ptr from storage/parsers X-Git-Tag: accepted/tizen/common/20150410.075011~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5e9b6ecdf1224dcaa8a51f08e0521e49e6be1331;p=platform%2Fcore%2Fsecurity%2Fcynara.git Remove shared_ptr from storage/parsers Remove unnecessary shared pointer parameters/memebers from storage/serialization where no shared ownership takes place Change-Id: I5292058da94180d3f7ebd19cf6c464d84b56b862 --- diff --git a/src/cyad/AdminPolicyParser.cpp b/src/cyad/AdminPolicyParser.cpp index 4b8d42e..0649123 100644 --- a/src/cyad/AdminPolicyParser.cpp +++ b/src/cyad/AdminPolicyParser.cpp @@ -30,7 +30,7 @@ namespace Cynara { namespace AdminPolicyParser { -CynaraAdminPolicies parse(const std::shared_ptr &input, +CynaraAdminPolicies parse(std::istream &input, std::function translatePolicy) { CynaraAdminPolicies policies; @@ -57,7 +57,7 @@ CynaraAdminPolicies parse(const std::shared_ptr &input, std::string line; std::size_t lineNum = 1; - while (std::getline(*input, line, PathConfig::StoragePath::recordSeparator)) { + while (std::getline(input, line, PathConfig::StoragePath::recordSeparator)) { if (line.empty()) break; diff --git a/src/cyad/AdminPolicyParser.h b/src/cyad/AdminPolicyParser.h index 0fb1b93..53dde23 100644 --- a/src/cyad/AdminPolicyParser.h +++ b/src/cyad/AdminPolicyParser.h @@ -32,8 +32,7 @@ namespace Cynara { namespace AdminPolicyParser { -CynaraAdminPolicies parse(const std::shared_ptr &input, - std::function); +CynaraAdminPolicies parse(std::istream &input, std::function); } /* namespace AdminPolicyParser */ diff --git a/src/cyad/CommandsDispatcher.cpp b/src/cyad/CommandsDispatcher.cpp index ef36654..d385dd6 100644 --- a/src/cyad/CommandsDispatcher.cpp +++ b/src/cyad/CommandsDispatcher.cpp @@ -137,7 +137,7 @@ int CommandsDispatcher::execute(SetPolicyBulkCyadCommand &result) { try { using Cynara::AdminPolicyParser::parse; - auto policies = parse(input, std::bind(&PolicyTypeTranslator::translate, + auto policies = parse(*input, std::bind(&PolicyTypeTranslator::translate, &m_policyTranslator, std::placeholders::_1)); auto ret = m_adminApiWrapper.cynara_admin_set_policies(m_cynaraAdmin, policies.data()); if (ret != CYNARA_API_SUCCESS) diff --git a/src/storage/InMemoryStorageBackend.cpp b/src/storage/InMemoryStorageBackend.cpp index 141f006..15f6a8a 100644 --- a/src/storage/InMemoryStorageBackend.cpp +++ b/src/storage/InMemoryStorageBackend.cpp @@ -77,12 +77,12 @@ void InMemoryStorageBackend::load(void) { } try { - auto chsStream = std::make_shared(); + std::ifstream chsStream; openFileStream(chsStream, chsFilename, isBackupValid); - m_checksum.load(*chsStream); + m_checksum.load(chsStream); auto indexStream = std::make_shared(); - openFileStream(indexStream, indexFilename, isBackupValid); + openFileStream(*indexStream, indexFilename, isBackupValid); StorageDeserializer storageDeserializer(indexStream, std::bind(&InMemoryStorageBackend::bucketStreamOpener, this, @@ -108,7 +108,7 @@ void InMemoryStorageBackend::load(void) { void InMemoryStorageBackend::save(void) { std::string checksumFilename = m_dbPath + m_chsFilename; auto chsStream = std::make_shared(); - openDumpFileStream(chsStream, checksumFilename + m_backupFilenameSuffix); + openDumpFileStream(*chsStream, checksumFilename + m_backupFilenameSuffix); dumpDatabase(chsStream); @@ -232,24 +232,24 @@ void InMemoryStorageBackend::erasePolicies(const PolicyBucketId &bucketId, bool void InMemoryStorageBackend::dumpDatabase(const std::shared_ptr &chsStream) { auto indexStream = std::make_shared(m_indexFilename, chsStream); std::string indexFilename = m_dbPath + m_indexFilename; - openDumpFileStream(indexStream, indexFilename + m_backupFilenameSuffix); + openDumpFileStream(*indexStream, indexFilename + m_backupFilenameSuffix); StorageSerializer storageSerializer(indexStream); storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener, this, std::placeholders::_1, chsStream)); } -void InMemoryStorageBackend::openFileStream(const std::shared_ptr &stream, - const std::string &filename, bool isBackupValid) { +void InMemoryStorageBackend::openFileStream(std::ifstream &stream, const std::string &filename, + bool isBackupValid) { // TODO: Consider adding exceptions to streams and handling them: // stream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - stream->open(filename); + stream.open(filename); - if (!stream->is_open()) { + if (!stream.is_open()) { throw FileNotFoundException(filename); } - m_checksum.compare(*stream, filename, isBackupValid); + m_checksum.compare(stream, filename, isBackupValid); } std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( @@ -257,7 +257,7 @@ std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix; auto bucketStream = std::make_shared(); try { - openFileStream(bucketStream, bucketFilename, isBackupValid); + openFileStream(*bucketStream, bucketFilename, isBackupValid); return std::make_shared(bucketStream); } catch (const FileNotFoundException &) { return nullptr; @@ -273,7 +273,7 @@ std::shared_ptr > InMemoryStorageBackend::buck auto bucketStream = std::make_shared(m_bucketFilenamePrefix + bucketId, chsStream); - openDumpFileStream(bucketStream, bucketFilename); + openDumpFileStream(*bucketStream, bucketFilename); return std::make_shared >(bucketStream); } diff --git a/src/storage/InMemoryStorageBackend.h b/src/storage/InMemoryStorageBackend.h index ea1f2bd..80a505c 100644 --- a/src/storage/InMemoryStorageBackend.h +++ b/src/storage/InMemoryStorageBackend.h @@ -69,15 +69,13 @@ public: protected: void dumpDatabase(const std::shared_ptr &chsStream); - void openFileStream(const std::shared_ptr &stream, const std::string &filename, - bool isBackupValid); + void openFileStream(std::ifstream &stream, const std::string &filename, bool isBackupValid); std::shared_ptr bucketStreamOpener(const PolicyBucketId &bucketId, const std::string &fileNameSuffix, bool isBackupValid); template - void openDumpFileStream(const std::shared_ptr &stream, - const std::string &filename); + void openDumpFileStream(StreamType &stream, const std::string &filename); std::shared_ptr > bucketDumpStreamOpener( const PolicyBucketId &bucketId, const std::shared_ptr &chsStream); @@ -103,11 +101,11 @@ protected: }; template -void InMemoryStorageBackend::openDumpFileStream(const std::shared_ptr &stream, +void InMemoryStorageBackend::openDumpFileStream(StreamType &stream, const std::string &filename) { - stream->open(filename, std::ofstream::out | std::ofstream::trunc); + stream.open(filename, std::ofstream::out | std::ofstream::trunc); - if (!stream->is_open()) { + if (!stream.is_open()) { throw CannotCreateFileException(filename); } } diff --git a/test/cyad/policy_parser.cpp b/test/cyad/policy_parser.cpp index a3e2f5e..73f439f 100644 --- a/test/cyad/policy_parser.cpp +++ b/test/cyad/policy_parser.cpp @@ -43,7 +43,7 @@ TEST(AdminPolicyParser, parseInvalid) { *input << "invalid input" << std::endl; - ASSERT_THROW(Cynara::AdminPolicyParser::parse(input, translatePolicy), + ASSERT_THROW(Cynara::AdminPolicyParser::parse(*input, translatePolicy), Cynara::BucketRecordCorruptedException); } @@ -53,7 +53,7 @@ TEST(AdminPolicyParser, parse0) { Cynara::CynaraAdminPolicies expectedPolicies; expectedPolicies.seal(); - auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy); + auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy); ASSERT_TRUE(policies.sealed()); ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data())); @@ -68,7 +68,7 @@ TEST(AdminPolicyParser, parse1) { expectedPolicies.add("b", { 0, "m" }, { "c", "u", "p" }); expectedPolicies.seal(); - auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy); + auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy); ASSERT_TRUE(policies.sealed()); ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data())); @@ -85,7 +85,7 @@ TEST(AdminPolicyParser, parse2) { expectedPolicies.add("b2", { 0, "m2" }, { "c2", "u2", "p2" }); expectedPolicies.seal(); - auto policies = Cynara::AdminPolicyParser::parse(input, translatePolicy); + auto policies = Cynara::AdminPolicyParser::parse(*input, translatePolicy); ASSERT_TRUE(policies.sealed()); ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));