* @brief Implementation of Cynara::PolicyBucket methods
*/
+#include <cstring>
#include <sstream>
#include <exceptions/InvalidBucketIdException.h>
namespace Cynara {
-const std::string PolicyBucket::m_idSeparators = "-_";
+const char PolicyBucket::m_idSeparators[] = "-_";
PolicyBucket::PolicyBucket(const PolicyBucketId &id, const PolicyResult &defaultPolicy)
: m_defaultPolicy(defaultPolicy), m_id(id) {
}
bool PolicyBucket::isIdSeparator(char c) {
- return m_idSeparators.end() != find(m_idSeparators.begin(), m_idSeparators.end(), c);
+ return strchr(m_idSeparators, c) != nullptr;
}
} // namespace Cynara
PolicyMap m_policyCollection;
PolicyResult m_defaultPolicy;
PolicyBucketId m_id;
- static const std::string m_idSeparators;
+ static const char m_idSeparators[];
};
} /* namespace Cynara */
if (connection == nullptr)
throw CYNARA_API_INVALID_PARAM;
- m_message = dbus_message_new_method_call(m_dbusName.c_str(), m_dbusObject.c_str(),
- m_dbusInterface.c_str(), method.c_str());
+ m_message = dbus_message_new_method_call(m_dbusName, m_dbusObject,
+ m_dbusInterface, method.c_str());
if (m_message == nullptr)
throw CYNARA_API_OUT_OF_MEMORY;
DBusMessage *m_message = nullptr;
DBusMessageIter *m_argsIter = nullptr;
- static const std::string m_dbusName;
- static const std::string m_dbusObject;
- static const std::string m_dbusInterface;
+ static const char m_dbusName[];
+ static const char m_dbusObject[];
+ static const char m_dbusInterface[];
};
-const std::string DBusMethod::m_dbusName = "org.freedesktop.DBus";
-const std::string DBusMethod::m_dbusObject = "/org/freedesktop/DBus";
-const std::string DBusMethod::m_dbusInterface = "org.freedesktop.DBus";
+const char DBusMethod::m_dbusName[] = "org.freedesktop.DBus";
+const char DBusMethod::m_dbusObject[] = "/org/freedesktop/DBus";
+const char DBusMethod::m_dbusInterface[] = "org.freedesktop.DBus";
int getIdFromConnection(DBusConnection *connection, const char *uniqueName,
namespace Cynara {
-const std::string ChecksumValidator::m_checksumFilename(PathConfig::StoragePath::checksumFilename);
-const std::string ChecksumValidator::m_backupFilenameSuffix(
- PathConfig::StoragePath::backupFilenameSuffix);
-
void ChecksumValidator::load(std::istream &stream) {
m_sums.clear();
std::stringstream copyStream;
if (isBackupValid) {
- auto backupSuffixPos = filename.rfind(m_backupFilenameSuffix);
+ auto backupSuffixPos = filename.rfind(PathConfig::StoragePath::backupFilenameSuffix);
+ size_t suffixSize = PathConfig::StoragePath::backupFilenameSuffix.size();
if ((std::string::npos != backupSuffixPos) &&
- (filename.size() == (backupSuffixPos + m_backupFilenameSuffix.size()))) {
+ (filename.size() == (backupSuffixPos + suffixSize))) {
filename.erase(backupSuffixPos);
}
}
}
bool ChecksumValidator::isChecksumIndex(const std::string &filename) const {
- auto checksum = m_dbPath + m_checksumFilename;
- return (filename == checksum || filename == checksum + m_backupFilenameSuffix);
+ auto checksum = m_dbPath + PathConfig::StoragePath::checksumFilename;
+ return (filename == checksum ||
+ filename == checksum + PathConfig::StoragePath::backupFilenameSuffix);
}
} // namespace Cynara
Checksums m_sums;
const std::string m_dbPath;
- static const std::string m_checksumFilename;
- static const std::string m_backupFilenameSuffix;
};
} // namespace Cynara
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);
-const std::string InMemoryStorageBackend::m_bucketFilenamePrefix(
- PathConfig::StoragePath::bucketFilenamePrefix);
-
InMemoryStorageBackend::InMemoryStorageBackend(const std::string &path) : m_dbPath(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;
+ std::string indexFilename = m_dbPath + PathConfig::StoragePath::indexFilename;
+ std::string chsFilename = m_dbPath + PathConfig::StoragePath::checksumFilename;
if (isBackupValid) {
- bucketSuffix += m_backupFilenameSuffix;
- indexFilename += m_backupFilenameSuffix;
- chsFilename += m_backupFilenameSuffix;
+ bucketSuffix += PathConfig::StoragePath::backupFilenameSuffix;
+ indexFilename += PathConfig::StoragePath::backupFilenameSuffix;
+ chsFilename += PathConfig::StoragePath::backupFilenameSuffix;
}
try {
}
void InMemoryStorageBackend::save(void) {
- std::string checksumFilename = m_dbPath + m_chsFilename;
+ std::string checksumFilename = m_dbPath + PathConfig::StoragePath::checksumFilename;
auto chsStream = std::make_shared<std::ofstream>();
- openDumpFileStream<std::ofstream>(*chsStream, checksumFilename + m_backupFilenameSuffix);
+ openDumpFileStream<std::ofstream>(*chsStream,
+ checksumFilename + PathConfig::StoragePath::backupFilenameSuffix);
dumpDatabase(chsStream);
}
void InMemoryStorageBackend::dumpDatabase(const std::shared_ptr<std::ofstream> &chsStream) {
- auto indexStream = std::make_shared<ChecksumStream>(m_indexFilename, chsStream);
- std::string indexFilename = m_dbPath + m_indexFilename;
- openDumpFileStream<ChecksumStream>(*indexStream, indexFilename + m_backupFilenameSuffix);
+ auto indexStream = std::make_shared<ChecksumStream>(PathConfig::StoragePath::indexFilename,
+ chsStream);
+ std::string indexFilename = m_dbPath + PathConfig::StoragePath::indexFilename;
+ openDumpFileStream<ChecksumStream>(*indexStream,
+ indexFilename + PathConfig::StoragePath::backupFilenameSuffix);
StorageSerializer<ChecksumStream> storageSerializer(indexStream);
storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
const PolicyBucketId &bucketId, const std::string &filenameSuffix, bool isBackupValid) {
- std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + filenameSuffix;
+ std::string bucketFilename = m_dbPath + PathConfig::StoragePath::bucketFilenamePrefix +
+ bucketId + filenameSuffix;
auto bucketStream = std::make_shared<std::ifstream>();
try {
openFileStream(*bucketStream, bucketFilename, isBackupValid);
std::shared_ptr<StorageSerializer<ChecksumStream> > InMemoryStorageBackend::bucketDumpStreamOpener(
const PolicyBucketId &bucketId, const std::shared_ptr<std::ofstream> &chsStream) {
- std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix +
- bucketId + m_backupFilenameSuffix;
- auto bucketStream = std::make_shared<ChecksumStream>(m_bucketFilenamePrefix + bucketId,
- chsStream);
+ std::string bucketFilename = m_dbPath + PathConfig::StoragePath::bucketFilenamePrefix +
+ bucketId + PathConfig::StoragePath::backupFilenameSuffix;
+ auto bucketStream = std::make_shared<ChecksumStream>(
+ PathConfig::StoragePath::bucketFilenamePrefix + bucketId, chsStream);
openDumpFileStream<ChecksumStream>(*bucketStream, bucketFilename);
return std::make_shared<StorageSerializer<ChecksumStream> >(bucketStream);
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;
protected:
virtual Buckets &buckets(void) {
namespace StorageConfig = PathConfig::StoragePath;
-const std::string Integrity::m_guardFilename(StorageConfig::guardFilename);
-const std::string Integrity::m_indexFilename(StorageConfig::indexFilename);
-const std::string Integrity::m_backupFilenameSuffix(StorageConfig::backupFilenameSuffix);
-const std::string Integrity::m_bucketFilenamePrefix(StorageConfig::bucketFilenamePrefix);
-
bool Integrity::backupGuardExists(void) const {
struct stat buffer;
- std::string guardFilename = m_dbPath + m_guardFilename;
+ std::string guardFilename = m_dbPath + StorageConfig::guardFilename;
int ret = stat(guardFilename.c_str(), &buffer);
}
void Integrity::createBackupGuard(void) const {
- syncElement(m_dbPath + m_guardFilename, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC);
+ syncElement(m_dbPath + StorageConfig::guardFilename, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC);
syncDirectory(m_dbPath);
}
std::string suffix = "";
if (syncBackup) {
- suffix += m_backupFilenameSuffix;
+ suffix += StorageConfig::backupFilenameSuffix;
}
for (const auto &bucketIter : buckets) {
const auto &bucketId = bucketIter.first;
- const auto &bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId + suffix;
+ const auto &bucketFilename = m_dbPath + StorageConfig::bucketFilenamePrefix +
+ bucketId + suffix;
syncElement(bucketFilename);
}
- syncElement(m_dbPath + m_indexFilename + suffix);
+ syncElement(m_dbPath + StorageConfig::indexFilename + suffix);
syncElement(m_dbPath + PathConfig::StoragePath::checksumFilename + suffix);
syncDirectory(m_dbPath);
}
createPrimaryHardLinks(buckets);
syncDatabase(buckets, false);
- deleteHardLink(m_dbPath + m_guardFilename);
+ deleteHardLink(m_dbPath + StorageConfig::guardFilename);
syncDirectory(m_dbPath);
deleteBackupHardLinks(buckets);
std::string bucketId;
auto nameLength = filename.length();
- auto prefixLength = m_bucketFilenamePrefix.length();
+ auto prefixLength = StorageConfig::bucketFilenamePrefix.length();
//remove if it is impossible that it is a bucket file
if (nameLength < prefixLength) {
//remove if there is no bucket filename prefix
//0 is returned from string::compare() if strings are equal
- if (0 != filename.compare(0, prefixLength, m_bucketFilenamePrefix)) {
+ if (0 != filename.compare(0, prefixLength, StorageConfig::bucketFilenamePrefix)) {
deleteHardLink(m_dbPath + filename);
continue;
}
void Integrity::createPrimaryHardLinks(const Buckets &buckets) {
for (const auto &bucketIter : buckets) {
const auto &bucketId = bucketIter.first;
- const auto &bucketFilename = m_dbPath + m_bucketFilenamePrefix + bucketId;
+ const auto &bucketFilename = m_dbPath + StorageConfig::bucketFilenamePrefix + bucketId;
deleteHardLink(bucketFilename);
- createHardLink(bucketFilename + m_backupFilenameSuffix, bucketFilename);
+ createHardLink(bucketFilename + StorageConfig::backupFilenameSuffix, bucketFilename);
}
- const auto &indexFilename = m_dbPath + m_indexFilename;
+ const auto &indexFilename = m_dbPath + StorageConfig::indexFilename;
const auto &checksumFilename = m_dbPath + PathConfig::StoragePath::checksumFilename;
deleteHardLink(indexFilename);
- createHardLink(indexFilename + m_backupFilenameSuffix, indexFilename);
+ createHardLink(indexFilename + StorageConfig::backupFilenameSuffix, indexFilename);
deleteHardLink(checksumFilename);
- createHardLink(checksumFilename + m_backupFilenameSuffix, checksumFilename);
+ createHardLink(checksumFilename + StorageConfig::backupFilenameSuffix, checksumFilename);
}
void Integrity::deleteBackupHardLinks(const Buckets &buckets) {
for (const auto &bucketIter : buckets) {
const auto &bucketId = bucketIter.first;
- const auto &bucketFilename = m_dbPath + m_bucketFilenamePrefix +
- bucketId + m_backupFilenameSuffix;
+ const auto &bucketFilename = m_dbPath + StorageConfig::bucketFilenamePrefix +
+ bucketId + StorageConfig::backupFilenameSuffix;
deleteHardLink(bucketFilename);
}
- deleteHardLink(m_dbPath + m_indexFilename + m_backupFilenameSuffix);
- deleteHardLink(m_dbPath + PathConfig::StoragePath::checksumFilename + m_backupFilenameSuffix);
+ deleteHardLink(m_dbPath + StorageConfig::indexFilename + StorageConfig::backupFilenameSuffix);
+ deleteHardLink(m_dbPath + StorageConfig::checksumFilename +
+ StorageConfig::backupFilenameSuffix);
}
void Integrity::createHardLink(const std::string &oldName, const std::string &newName) {
private:
const std::string m_dbPath;
- static const std::string m_indexFilename;
- static const std::string m_backupFilenameSuffix;
- static const std::string m_bucketFilenamePrefix;
- static const std::string m_guardFilename;
};
} // namespace Cynara
namespace {
const std::string execName("./cynara-db-chsgen");
-const std::string backupFilenameSuffix(Cynara::PathConfig::StoragePath::backupFilenameSuffix);
+const std::string &backupFilenameSuffix = Cynara::PathConfig::StoragePath::backupFilenameSuffix;
const char fieldSeparator(Cynara::PathConfig::StoragePath::fieldSeparator);
} // namespace
const std::string ChecksumValidatorFixture::m_dbPath("/fake/path/");
const std::string ChecksumValidatorFixture::m_filename("fakeFilename");
const std::string ChecksumValidatorFixture::m_checksum("$1$$fakeChecksum");
-const std::string ChecksumValidatorFixture::m_backupFilenameSuffix(
- PathConfig::StoragePath::backupFilenameSuffix);
+
const char ChecksumValidatorFixture::m_fieldSeparator(PathConfig::StoragePath::fieldSeparator);
const char ChecksumValidatorFixture::m_recordSeparator(PathConfig::StoragePath::recordSeparator);
const size_t ChecksumValidatorFixture::m_firstLine(1);
SCOPED_TRACE(filename);
ASSERT_NO_THROW(validator.compare(fakeFile, filename, false));
- ASSERT_NO_THROW(validator.compare(fakeFile, filename + m_backupFilenameSuffix, true));
+ ASSERT_NO_THROW(validator.compare(fakeFile, filename +
+ PathConfig::StoragePath::backupFilenameSuffix, true));
ASSERT_EQ(contents, fakeFile.str());
}
static const std::string m_dbPath;
static const std::string m_filename;
static const std::string m_checksum;
- static const std::string m_backupFilenameSuffix;
static const char m_fieldSeparator;
static const char m_recordSeparator;
static const size_t m_firstLine;