1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/*
* 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.
* 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 initial-value-loader.cpp
* @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
* @version 1.0
* @brief
*/
#include <initial-value-loader.h>
#include <unistd.h>
#include <fstream>
#include <ckm-logic.h>
#include <for-each-file.h>
#include <InitialValuesFile.h>
#include <dpl/errno_string.h>
namespace {
const char *const INIT_VALUES_XSD = RO_DATA_DIR "/initial_values.xsd";
const char *const INIT_VALUES_FILE_SUFFIX = ".xml";
const char *const INIT_VALUES_RO_IMPORT_FLAG = INITIAL_VALUES_DIR_RW "/ro_import";
} // namespace anonymous
namespace CKM {
namespace InitialValues {
void LoadFiles(CKMLogic &logic)
{
try {
std::vector<std::string> filesToParse;
std::string currentDir;
auto addXmlFiles = [&](const std::string & filename) {
std::string lowercaseFilename = filename;
std::transform(lowercaseFilename.begin(), lowercaseFilename.end(),
lowercaseFilename.begin(), ::tolower);
if (lowercaseFilename.find(INIT_VALUES_FILE_SUFFIX) == std::string::npos)
return;
filesToParse.emplace_back(currentDir + "/" + filename);
};
std::ifstream ro_flag(INIT_VALUES_RO_IMPORT_FLAG);
if (ro_flag) {
currentDir = INITIAL_VALUES_DIR_RO;
forEachFile(INITIAL_VALUES_DIR_RO, addXmlFiles);
if (-1 == unlink(INIT_VALUES_RO_IMPORT_FLAG))
LogError("Unlink() failed for " << INIT_VALUES_RO_IMPORT_FLAG << ":" <<
CKM::GetErrnoString(errno));
}
currentDir = INITIAL_VALUES_DIR_RW;
forEachFile(INITIAL_VALUES_DIR_RW, addXmlFiles);
// parse
for (const auto &file : filesToParse) {
InitialValues::InitialValuesFile xmlFile(file.c_str(), logic);
int rc = xmlFile.Validate(INIT_VALUES_XSD);
if (rc == XML::Parser::PARSE_SUCCESS) {
rc = xmlFile.Parse();
if (rc != XML::Parser::PARSE_SUCCESS)
LogError("invalid initial values file: " << file << ", parsing code: " << rc);
} else {
LogError("invalid initial values file: " << file << ", validation code: " <<
rc);
}
if (file.compare(0, sizeof(INITIAL_VALUES_DIR_RW) - 1, INITIAL_VALUES_DIR_RW) == 0) {
if (-1 == unlink(file.c_str()))
LogError("Unlink() failed for " << file << ":" << CKM::GetErrnoString(errno));
}
}
} catch (...) {
LogError("The implementation of exception handling in xml parser is broken!");
}
}
} // namespace InitialValues
} // namespace CKM
|