blob: b9611a36ae4fffed5300ac419e19e62c42d45b6f [file] [log] [blame]
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05001#include "utils.hpp"
2
Jayashankar Padatha0135602019-04-22 16:22:58 +05303#include <phosphor-logging/log.hpp>
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05004
Jayashankar Padatha0135602019-04-22 16:22:58 +05305namespace utils
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05006{
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05007
Jayashankar Padatha0135602019-04-22 16:22:58 +05308using namespace phosphor::logging;
9
10std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
11 const std::string& interface)
12{
13 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
14 MAPPER_BUSNAME, "GetObject");
15
16 method.append(path);
17 method.append(std::vector<std::string>({interface}));
18
19 std::vector<std::pair<std::string, std::vector<std::string>>> response;
20
21 try
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050022 {
Jayashankar Padatha0135602019-04-22 16:22:58 +053023 auto reply = bus.call(method);
24 reply.read(response);
25 if (response.empty())
26 {
27 log<level::ERR>("Error in mapper response for getting service name",
28 entry("PATH=%s", path.c_str()),
29 entry("INTERFACE=%s", interface.c_str()));
30 return std::string{};
31 }
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050032 }
Jayashankar Padatha0135602019-04-22 16:22:58 +053033 catch (const sdbusplus::exception::SdBusError& e)
34 {
35 log<level::ERR>("Error in mapper method call",
36 entry("ERROR=%s", e.what()),
37 entry("PATH=%s", path.c_str()),
38 entry("INTERFACE=%s", interface.c_str()));
39 return std::string{};
40 }
41 return response[0].first;
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050042}
43
George Liu0a06e972020-12-17 09:17:04 +080044void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile)
45{
46 std::ofstream outFile(dstFile, std::ios::out);
47 for (auto& file : srcFiles)
48 {
49 std::ifstream inFile;
50 inFile.open(file, std::ios_base::in);
51 if (!inFile)
52 {
53 continue;
54 }
55
56 inFile.peek();
57 if (inFile.eof())
58 {
59 inFile.close();
60 continue;
61 }
62
63 outFile << inFile.rdbuf();
64 inFile.close();
65 }
66 outFile.close();
67}
68
Jayashankar Padatha0135602019-04-22 16:22:58 +053069} // namespace utils