Verify if there are no IO errors during database loading from files.
Change-Id: I49ac887bae05f47b2b75dd6dc6489fc4d54562fb
Signed-off-by: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
--- /dev/null
+/*
+ * Copyright (c) 2018 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/common/exceptions/DatabaseReadException.h
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief This file defines exception thrown when database read fails
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_DATABASEREADEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_DATABASEREADEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/DatabaseException.h>
+
+namespace Cynara {
+
+class DatabaseReadException : public DatabaseException {
+public:
+ DatabaseReadException(const std::string &filename, const std::string &operation)
+ : m_message("Loading the database from the file <" + filename + "> failed during " + operation) {};
+ virtual ~DatabaseReadException() {};
+
+ const std::string &message(void) const {
+ return m_message;
+ }
+
+private:
+ std::string m_message;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_DATABASEREADEXCEPTION_H_ */
/*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2018 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.
#include <config/PathConfig.h>
#include <exceptions/BucketRecordCorruptedException.h>
+#include <exceptions/DatabaseReadException.h>
#include <types/PolicyCollection.h>
#include <types/PolicyResult.h>
#include <types/PolicyType.h>
PolicyCollection BucketDeserializer::loadPolicies(void) {
PolicyCollection policies;
- // TODO: Get someone smart to do error checking on stream
std::string line;
std::size_t lineNum = 1;
- while(std::getline(*m_inStream, line, PathConfig::StoragePath::recordSeparator)) {
+ while (std::getline(*m_inStream, line, PathConfig::StoragePath::recordSeparator)) {
if (line.empty())
- break;
+ continue;
try {
std::size_t beginToken = 0;
}
++lineNum;
}
+ if (m_inStream->fail() && !m_inStream->eof()) {
+ throw DatabaseReadException(m_filename, "reading line " + std::to_string(lineNum));
+ }
return policies;
}
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2018 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.
class BucketDeserializer {
public:
- BucketDeserializer(std::shared_ptr<std::istream> inStream) : m_inStream(inStream) {
+ BucketDeserializer(std::shared_ptr<std::istream> inStream, const std::string &filename = std::string())
+ : m_inStream(inStream), m_filename(filename) {
}
PolicyCollection loadPolicies(void);
private:
std::shared_ptr<std::istream> m_inStream;
+ std::string m_filename;
};
} /* namespace Cynara */
/*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2018 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.
#include <memory>
#include <new>
#include <sstream>
+#include <string>
#include <config/PathConfig.h>
#include <exceptions/ChecksumRecordCorruptedException.h>
+#include <exceptions/DatabaseReadException.h>
#include <exceptions/UnexpectedErrorException.h>
#include <log/log.h>
#include <md5wrapper.h>
throw ex.withLineNumber(lineNum);
}
}
+ if (stream.fail() && !stream.eof()) {
+ throw DatabaseReadException(PathConfig::StoragePath::checksumFilename, "reading line " + std::to_string(lineNum));
+ }
};
const std::string ChecksumValidator::generate(const std::string &data) {
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(copyStream));
stream.seekg(0);
+ if (stream.fail() && !stream.eof()) {
+ throw DatabaseReadException(filename, "making a copy for checksum validation");
+ }
if (m_sums[filename] != generate(copyStream.str())) {
throw ChecksumRecordCorruptedException(m_sums[filename]);
auto bucketStream = std::make_shared<std::ifstream>();
try {
openFileStream(*bucketStream, bucketFilename, isBackupValid);
- return std::make_shared<BucketDeserializer>(bucketStream);
+ return std::make_shared<BucketDeserializer>(bucketStream, bucketFilename);
} catch (const FileNotFoundException &) {
return nullptr;
} catch (const std::bad_alloc &) {
/*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2018 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.
#include <config/PathConfig.h>
#include <exceptions/BucketDeserializationException.h>
#include <exceptions/BucketRecordCorruptedException.h>
+#include <exceptions/DatabaseReadException.h>
#include <types/PolicyType.h>
#include <storage/BucketDeserializer.h>
std::string line;
while (std::getline(*m_inStream, line, PathConfig::StoragePath::recordSeparator)) {
if (line.empty())
- break;
+ continue;
std::size_t beginToken = 0;
auto bucketId = parseBucketId(line, beginToken);
buckets.insert({ bucketId, PolicyBucket(bucketId, PolicyResult(policyType, metadata)) });
++lineNum;
}
+ if (m_inStream->fail() && !m_inStream->eof()) {
+ throw DatabaseReadException(PathConfig::StoragePath::indexFilename, "reading line " + std::to_string(lineNum));
+ }
}
void StorageDeserializer::loadBuckets(Buckets &buckets) {