From f08a80ee755ba5aed0450bb04eaa235e3966ab05 Mon Sep 17 00:00:00 2001
From: Pawel Wieczorek
Date: Tue, 23 Dec 2014 17:18:16 +0100
Subject: [PATCH] Adjust InMemoryStorageBackend to ChecksumValidator
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 | 27 ++++++++++++++++++++-------
src/storage/InMemoryStorageBackend.h | 9 +++++++--
src/storage/Integrity.cpp | 8 +++++++-
test/db/db2/checksum | 1 +
test/db/db3/checksum | 2 ++
test/db/db4/checksum | 3 +++
test/db/db5/checksum | 3 +++
test/db/db6/checksum | 3 +++
test/db/db6/checksum~ | 3 +++
9 files changed, 49 insertions(+), 10 deletions(-)
create mode 100644 test/db/db2/checksum
create mode 100644 test/db/db3/checksum
create mode 100644 test/db/db4/checksum
create mode 100644 test/db/db5/checksum
create mode 100644 test/db/db6/checksum
create mode 100644 test/db/db6/checksum~
diff --git a/src/storage/InMemoryStorageBackend.cpp b/src/storage/InMemoryStorageBackend.cpp
index d6655e2..249eccf 100644
--- a/src/storage/InMemoryStorageBackend.cpp
+++ b/src/storage/InMemoryStorageBackend.cpp
@@ -24,6 +24,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -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();
+ openFileStream(chsStream, chsFilename, isBackupValid);
+ m_checksum.load(*chsStream);
+
auto indexStream = std::make_shared();
- 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 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 strea
if (!stream->is_open()) {
throw FileNotFoundException(filename);
}
+
+ m_checksum.compare(*stream, filename, isBackupValid);
}
void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr stream,
@@ -242,14 +253,16 @@ void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr s
}
std::shared_ptr 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();
try {
- openFileStream(bucketStream, bucketFilename);
+ openFileStream(bucketStream, bucketFilename, isBackupValid);
return std::make_shared(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 */
diff --git a/src/storage/InMemoryStorageBackend.h b/src/storage/InMemoryStorageBackend.h
index 10251c7..65af96b 100644
--- a/src/storage/InMemoryStorageBackend.h
+++ b/src/storage/InMemoryStorageBackend.h
@@ -35,6 +35,7 @@
#include
#include
+#include
#include
#include
#include
@@ -65,9 +66,11 @@ public:
const PolicyKey &filter);
protected:
- void openFileStream(std::shared_ptr stream, const std::string &filename);
+ void openFileStream(std::shared_ptr stream, const std::string &filename,
+ bool isBackupValid);
std::shared_ptr bucketStreamOpener(const PolicyBucketId &bucketId,
- const std::string &fileNameSuffix);
+ const std::string &fileNameSuffix,
+ bool isBackupValid);
virtual void openDumpFileStream(std::shared_ptr 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;
diff --git a/src/storage/Integrity.cpp b/src/storage/Integrity.cpp
index 5dfa624..0b8e583 100644
--- a/src/storage/Integrity.cpp
+++ b/src/storage/Integrity.cpp
@@ -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
index 0000000..a91e13a
--- /dev/null
+++ b/test/db/db2/checksum
@@ -0,0 +1 @@
+buckets;$1$$QYBGLyef4xp18EzYIMd/U0
diff --git a/test/db/db3/checksum b/test/db/db3/checksum
new file mode 100644
index 0000000..39a8d89
--- /dev/null
+++ b/test/db/db3/checksum
@@ -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
index 0000000..e2477f0
--- /dev/null
+++ b/test/db/db4/checksum
@@ -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
index 0000000..b578233
--- /dev/null
+++ b/test/db/db5/checksum
@@ -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
index 0000000..a951841
--- /dev/null
+++ b/test/db/db6/checksum
@@ -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
index 0000000..e2477f0
--- /dev/null
+++ b/test/db/db6/checksum~
@@ -0,0 +1,3 @@
+buckets;$1$$Z6OUrCl62KbAaZ16Cexgm0
+_;$1$$qRPK7m23GJusamGpoGLby/
+_additional;$1$$qRPK7m23GJusamGpoGLby/
--
2.7.4