blob: 7940d2e3ea3610ca754ddec5f5a4616ae54bd313 [file] [log] [blame]
Lei YU01539e72019-07-31 10:57:38 +08001#include "version.hpp"
2
3#include "item_updater.hpp"
4
5#include <fstream>
6#include <iostream>
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
9#include <sstream>
10#include <stdexcept>
11#include <string>
12#include <xyz/openbmc_project/Common/error.hpp>
13
14namespace phosphor
15{
16namespace software
17{
18namespace updater
19{
20
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22using namespace phosphor::logging;
23using Argument = xyz::openbmc_project::Common::InvalidArgument;
24
25std::map<std::string, std::string>
26 Version::getValue(const std::string& filePath,
27 std::map<std::string, std::string> keys)
28{
29 if (filePath.empty())
30 {
31 log<level::ERR>("Error filePath is empty");
32 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
33 Argument::ARGUMENT_VALUE(filePath.c_str()));
34 }
35
36 std::ifstream efile;
37 std::string line;
38 efile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
39 std::ifstream::eofbit);
40
41 try
42 {
43 efile.open(filePath);
44 while (getline(efile, line))
45 {
46 for (auto& key : keys)
47 {
48 auto value = key.first + "=";
49 auto keySize = value.length();
50 if (line.compare(0, keySize, value) == 0)
51 {
52 key.second = line.substr(keySize);
53 break;
54 }
55 }
56 }
57 efile.close();
58 }
59 catch (const std::exception& e)
60 {
61 if (!efile.eof())
62 {
63 log<level::ERR>("Error in reading file");
64 }
65 efile.close();
66 }
67
68 return keys;
69}
70
71void Delete::delete_()
72{
73 if (version.eraseCallback)
74 {
75 version.eraseCallback(version.getVersionId());
76 }
77}
78
79} // namespace updater
80} // namespace software
81} // namespace phosphor