Introduce ChecksumStream 12/33812/27
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Thu, 15 Jan 2015 07:19:15 +0000 (08:19 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Wed, 4 Mar 2015 10:32:45 +0000 (11:32 +0100)
ChecksumStream will replace std::ofstream for saving database contents
in storage. This way data will be not only written to the files, but
also its checksums will be computed and stored in given stream (database
index equivalent for storing checksums).

Checksum computing is performed during stream destruction in order to be
sure that all necessary data was already collected.

Change-Id: I4a9ff2e29361f337cacd790d77364feca854a706

src/storage/CMakeLists.txt
src/storage/ChecksumStream.cpp [new file with mode: 0644]
src/storage/ChecksumStream.h [new file with mode: 0644]
test/CMakeLists.txt

index 72f0ec2..f75bc06 100644 (file)
@@ -24,6 +24,7 @@ SET(CYNARA_LIB_CYNARA_STORAGE_PATH ${CYNARA_PATH}/storage)
 
 SET(LIB_CYNARA_STORAGE_SOURCES
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/BucketDeserializer.cpp
+    ${CYNARA_LIB_CYNARA_STORAGE_PATH}/ChecksumStream.cpp
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/ChecksumValidator.cpp
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/InMemoryStorageBackend.cpp
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/Integrity.cpp
diff --git a/src/storage/ChecksumStream.cpp b/src/storage/ChecksumStream.cpp
new file mode 100644 (file)
index 0000000..551307e
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2015 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
+ */
+/**
+ * @file        src/storage/ChecksumStream.cpp
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       This file contains ChecksumStream implementation.
+ */
+
+#include <exception>
+#include <unistd.h>
+
+#include <config/PathConfig.h>
+
+#include <storage/ChecksumValidator.h>
+
+#include "ChecksumStream.h"
+
+namespace Cynara {
+
+const char ChecksumStream::m_fieldSeparator(PathConfig::StoragePath::fieldSeparator);
+const char ChecksumStream::m_recordSeparator(PathConfig::StoragePath::recordSeparator);
+
+ChecksumStream::~ChecksumStream() {
+    if (!std::uncaught_exception()) {
+        save();
+    }
+}
+
+ChecksumStream& ChecksumStream::operator<<(std::ostream& (*manip)(std::ostream&)) {
+    m_bufStream << manip;
+    m_outStream << manip;
+    return *this;
+}
+
+void ChecksumStream::open(const std::string &filename, std::ios_base::openmode mode) {
+    m_outStream.open(filename, mode);
+}
+
+bool ChecksumStream::is_open(void) const {
+    return m_outStream.is_open();
+}
+
+std::ios_base::fmtflags ChecksumStream::flags(void) const {
+    return m_outStream.flags();
+}
+
+std::ios_base::fmtflags ChecksumStream::flags(std::ios_base::fmtflags flags) {
+    return m_outStream.flags(flags);
+}
+
+void ChecksumStream::save() {
+    m_outStream.close();
+    *m_chsStream << m_filename << m_fieldSeparator << ChecksumValidator::generate(m_bufStream.str())
+                 << m_recordSeparator;
+}
+
+} // namespace Cynara
diff --git a/src/storage/ChecksumStream.h b/src/storage/ChecksumStream.h
new file mode 100644 (file)
index 0000000..ddc6e6f
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ *  Copyright (c) 2015 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
+ */
+/**
+ * @file        src/storage/ChecksumStream.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       This file contains ChecksumStream header.
+ */
+
+#ifndef SRC_STORAGE_CHECKSUMSTREAM_H_
+#define SRC_STORAGE_CHECKSUMSTREAM_H_
+
+#include <fstream>
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+
+namespace Cynara {
+
+class ChecksumStream {
+public:
+    ChecksumStream(const std::string &filename, const std::shared_ptr<std::ofstream> &stream)
+    : m_chsStream(stream), m_filename(filename) {
+    }
+    ~ChecksumStream();
+
+    template<typename Type>
+    ChecksumStream& operator<<(const Type &item);
+    ChecksumStream& operator<<(std::ostream& (*manip)(std::ostream&));
+
+    void open(const std::string &filename, std::ios_base::openmode mode);
+    bool is_open(void) const;
+    std::ios_base::fmtflags flags(void) const;
+    std::ios_base::fmtflags flags(std::ios_base::fmtflags flags);
+
+private:
+    void save(void);
+
+    std::shared_ptr<std::ofstream> m_chsStream;
+    std::ofstream m_outStream;
+    std::stringstream m_bufStream;
+    const std::string m_filename;
+    static const char m_fieldSeparator;
+    static const char m_recordSeparator;
+};
+
+template<typename Type>
+ChecksumStream& ChecksumStream::operator<<(const Type& item) {
+    m_bufStream << item;
+    m_outStream << item;
+    return *this;
+}
+
+} // namespace Cynara
+
+#endif // SRC_STORAGE_CHECKSUMSTREAM_H_
index 9cf89d7..189268d 100644 (file)
@@ -63,6 +63,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp
     ${CYNARA_SRC}/service/main/CmdlineParser.cpp
     ${CYNARA_SRC}/storage/BucketDeserializer.cpp
+    ${CYNARA_SRC}/storage/ChecksumStream.cpp
     ${CYNARA_SRC}/storage/ChecksumValidator.cpp
     ${CYNARA_SRC}/storage/InMemoryStorageBackend.cpp
     ${CYNARA_SRC}/storage/Integrity.cpp