Adjust InMemoryStorageBackend to ChecksumValidator 77/32777/28
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 23 Dec 2014 16:18:16 +0000 (17:18 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Wed, 4 Mar 2015 13:08:48 +0000 (14:08 +0100)
InMemoryStorageBackend uses ChecksumValidator as a checksum loader and
comparator. This patch also includes files needed by storage unit tests
to work properly.

Change-Id: I541975351275bd6a30e7cf627697c9657161312f

src/storage/InMemoryStorageBackend.cpp
src/storage/InMemoryStorageBackend.h
src/storage/Integrity.cpp
test/db/db2/checksum [new file with mode: 0644]
test/db/db3/checksum [new file with mode: 0644]
test/db/db4/checksum [new file with mode: 0644]
test/db/db5/checksum [new file with mode: 0644]
test/db/db6/checksum [new file with mode: 0644]
test/db/db6/checksum~ [new file with mode: 0644]

index d6655e2..249eccf 100644 (file)
@@ -24,6 +24,7 @@
 #include <fstream>
 #include <functional>
 #include <memory>
+#include <new>
 #include <stdexcept>
 #include <string>
 #include <string.h>
@@ -51,6 +52,7 @@
 
 namespace Cynara {
 
+const std::string InMemoryStorageBackend::m_chsFilename(PathConfig::StoragePath::checksumFilename);
 const std::string InMemoryStorageBackend::m_indexFilename(PathConfig::StoragePath::indexFilename);
 const std::string InMemoryStorageBackend::m_backupFilenameSuffix(
         PathConfig::StoragePath::backupFilenameSuffix);
@@ -58,26 +60,32 @@ const std::string InMemoryStorageBackend::m_bucketFilenamePrefix(
         PathConfig::StoragePath::bucketFilenamePrefix);
 
 InMemoryStorageBackend::InMemoryStorageBackend(const std::string &path) : m_dbPath(path),
-    m_integrity(path) {
+    m_checksum(path), m_integrity(path) {
 }
 
 void InMemoryStorageBackend::load(void) {
     bool isBackupValid = m_integrity.backupGuardExists();
     std::string bucketSuffix = "";
     std::string indexFilename = m_dbPath + m_indexFilename;
+    std::string chsFilename = m_dbPath + m_chsFilename;
 
     if (isBackupValid) {
         bucketSuffix += m_backupFilenameSuffix;
         indexFilename += m_backupFilenameSuffix;
+        chsFilename += m_backupFilenameSuffix;
     }
 
     try {
+        auto chsStream = std::make_shared<std::ifstream>();
+        openFileStream(chsStream, chsFilename, isBackupValid);
+        m_checksum.load(*chsStream);
+
         auto indexStream = std::make_shared<std::ifstream>();
-        openFileStream(indexStream, indexFilename);
+        openFileStream(indexStream, indexFilename, isBackupValid);
 
         StorageDeserializer storageDeserializer(indexStream,
             std::bind(&InMemoryStorageBackend::bucketStreamOpener, this,
-                      std::placeholders::_1, bucketSuffix));
+                      std::placeholders::_1, bucketSuffix, isBackupValid));
 
         storageDeserializer.initBuckets(buckets());
         storageDeserializer.loadBuckets(buckets());
@@ -86,6 +94,7 @@ void InMemoryStorageBackend::load(void) {
         buckets().clear();
         // TODO: Implement emergency mode toggle
     }
+    m_checksum.clear();
 
     if (!hasBucket(defaultPolicyBucketId)) {
         LOGN("Creating defaultBucket.");
@@ -222,7 +231,7 @@ void InMemoryStorageBackend::erasePolicies(const PolicyBucketId &bucketId, bool
 }
 
 void InMemoryStorageBackend::openFileStream(std::shared_ptr<std::ifstream> stream,
-                                            const std::string &filename) {
+                                            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);
@@ -230,6 +239,8 @@ void InMemoryStorageBackend::openFileStream(std::shared_ptr<std::ifstream> strea
     if (!stream->is_open()) {
         throw FileNotFoundException(filename);
     }
+
+    m_checksum.compare(*stream, filename, isBackupValid);
 }
 
 void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr<std::ofstream> stream,
@@ -242,14 +253,16 @@ void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr<std::ofstream> s
 }
 
 std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
-        const PolicyBucketId &bucketId, const std::string &filenameSuffix) {
+        const PolicyBucketId &bucketId, const std::string &filenameSuffix, bool isBackupValid) {
     std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix;
     auto bucketStream = std::make_shared<std::ifstream>();
     try {
-        openFileStream(bucketStream, bucketFilename);
+        openFileStream(bucketStream, bucketFilename, isBackupValid);
         return std::make_shared<BucketDeserializer>(bucketStream);
     } catch (const FileNotFoundException &) {
         return nullptr;
+    } catch (const std::bad_alloc &) {
+        return nullptr;
     }
 }
 
@@ -269,7 +282,7 @@ void InMemoryStorageBackend::postLoadCleanup(bool isBackupValid) {
     }
     //in case there were unnecessary files in db directory
     m_integrity.deleteNonIndexedFiles(std::bind(&InMemoryStorageBackend::hasBucket, this,
-                                       std::placeholders::_1));
+                                      std::placeholders::_1));
 }
 
 } /* namespace Cynara */
index 10251c7..65af96b 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <storage/BucketDeserializer.h>
 #include <storage/Buckets.h>
+#include <storage/ChecksumValidator.h>
 #include <storage/Integrity.h>
 #include <storage/StorageBackend.h>
 #include <storage/StorageSerializer.h>
@@ -65,9 +66,11 @@ public:
                                const PolicyKey &filter);
 
 protected:
-    void openFileStream(std::shared_ptr<std::ifstream> stream, const std::string &filename);
+    void openFileStream(std::shared_ptr<std::ifstream> stream, const std::string &filename,
+                        bool isBackupValid);
     std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId,
-                                                           const std::string &fileNameSuffix);
+                                                           const std::string &fileNameSuffix,
+                                                           bool isBackupValid);
 
     virtual void openDumpFileStream(std::shared_ptr<std::ofstream> stream,
                                     const std::string &filename);
@@ -79,7 +82,9 @@ protected:
 private:
     std::string m_dbPath;
     Buckets m_buckets;
+    ChecksumValidator m_checksum;
     Integrity m_integrity;
+    static const std::string m_chsFilename;
     static const std::string m_indexFilename;
     static const std::string m_backupFilenameSuffix;
     static const std::string m_bucketFilenamePrefix;
index 5dfa624..0b8e583 100644 (file)
@@ -83,6 +83,7 @@ void Integrity::syncDatabase(const Buckets &buckets, bool syncBackup) {
     }
 
     syncElement(m_dbPath + m_indexFilename + suffix);
+    syncElement(m_dbPath + PathConfig::StoragePath::checksumFilename + suffix);
     syncDirectory(m_dbPath);
 }
 
@@ -152,7 +153,8 @@ void Integrity::syncElement(const std::string &filename, int flags, mode_t mode)
     if (fileFd < 0) {
         int err = errno;
         if (err != EEXIST) {
-            LOGE("'open' function error [%d] : <%s>", err, strerror(err));
+            LOGE("File <%s> : 'open' function error [%d] : <%s>", filename.c_str(), err,
+                 strerror(err));
             throw UnexpectedErrorException(err, strerror(err));
         } else {
             throw CannotCreateFileException(filename);
@@ -194,9 +196,12 @@ void Integrity::createPrimaryHardLinks(const Buckets &buckets) {
     }
 
     const auto &indexFilename = m_dbPath + m_indexFilename;
+    const auto &checksumFilename = m_dbPath + PathConfig::StoragePath::checksumFilename;
 
     deleteHardLink(indexFilename);
     createHardLink(indexFilename + m_backupFilenameSuffix, indexFilename);
+    deleteHardLink(checksumFilename);
+    createHardLink(checksumFilename + m_backupFilenameSuffix, checksumFilename);
 }
 
 void Integrity::deleteBackupHardLinks(const Buckets &buckets) {
@@ -209,6 +214,7 @@ void Integrity::deleteBackupHardLinks(const Buckets &buckets) {
     }
 
     deleteHardLink(m_dbPath + m_indexFilename + m_backupFilenameSuffix);
+    deleteHardLink(m_dbPath + PathConfig::StoragePath::checksumFilename + m_backupFilenameSuffix);
 }
 
 void Integrity::createHardLink(const std::string &oldName, const std::string &newName) {
diff --git a/test/db/db2/checksum b/test/db/db2/checksum
new file mode 100644 (file)
index 0000000..a91e13a
--- /dev/null
@@ -0,0 +1 @@
+buckets;$1$$QYBGLyef4xp18EzYIMd/U0
diff --git a/test/db/db3/checksum b/test/db/db3/checksum
new file mode 100644 (file)
index 0000000..39a8d89
--- /dev/null
@@ -0,0 +1,2 @@
+buckets;$1$$QYBGLyef4xp18EzYIMd/U0
+_;$1$$qRPK7m23GJusamGpoGLby/
diff --git a/test/db/db4/checksum b/test/db/db4/checksum
new file mode 100644 (file)
index 0000000..e2477f0
--- /dev/null
@@ -0,0 +1,3 @@
+buckets;$1$$Z6OUrCl62KbAaZ16Cexgm0
+_;$1$$qRPK7m23GJusamGpoGLby/
+_additional;$1$$qRPK7m23GJusamGpoGLby/
diff --git a/test/db/db5/checksum b/test/db/db5/checksum
new file mode 100644 (file)
index 0000000..b578233
--- /dev/null
@@ -0,0 +1,3 @@
+buckets;$1$$Z6OUrCl62KbAaZ16Cexgm0
+_;$1$$qRPK7m23GJusamGpoGLby/
+_additional;$1$$NIRf1PrhNP0QOAOGJ4VDf/
diff --git a/test/db/db6/checksum b/test/db/db6/checksum
new file mode 100644 (file)
index 0000000..a951841
--- /dev/null
@@ -0,0 +1,3 @@
+buckets;$1$$Z6OUrCl62KbAaZ16Cexgm0
+_;$1$$qRPK7m23GJusamGpoGLby/
+_additional;$1$$Shj03wOSJf.nmFjldRTO.1
diff --git a/test/db/db6/checksum~ b/test/db/db6/checksum~
new file mode 100644 (file)
index 0000000..e2477f0
--- /dev/null
@@ -0,0 +1,3 @@
+buckets;$1$$Z6OUrCl62KbAaZ16Cexgm0
+_;$1$$qRPK7m23GJusamGpoGLby/
+_additional;$1$$qRPK7m23GJusamGpoGLby/