1 #include "config.h"
2 
3 #include "dump_utils.hpp"
4 
5 #include "dump_types.hpp"
6 
7 #include <phosphor-logging/lg2.hpp>
8 
9 #include <ctime>
10 #include <filesystem>
11 #include <optional>
12 #include <regex>
13 #include <tuple>
14 
15 namespace phosphor
16 {
17 namespace dump
18 {
19 
20 std::string getService(sdbusplus::bus_t& bus, const std::string& path,
21                        const std::string& interface)
22 {
23     constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
24     constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
25 
26     auto method = bus.new_method_call(objectMapperName, objectMapperPath,
27                                       objectMapperName, "GetObject");
28 
29     method.append(path);
30     method.append(std::vector<std::string>({interface}));
31 
32     std::vector<std::pair<std::string, std::vector<std::string>>> response;
33 
34     try
35     {
36         auto reply = bus.call(method);
37         reply.read(response);
38         if (response.empty())
39         {
40             lg2::error(
41                 "Error in mapper response for getting service name, PATH: "
42                 "{PATH}, INTERFACE: {INTERFACE}",
43                 "PATH", path, "INTERFACE", interface);
44             return std::string{};
45         }
46     }
47     catch (const sdbusplus::exception_t& e)
48     {
49         lg2::error("Error in mapper method call, errormsg: {ERROR}, "
50                    "PATH: {PATH}, INTERFACE: {INTERFACE}",
51                    "ERROR", e, "PATH", path, "INTERFACE", interface);
52         throw;
53     }
54     return response[0].first;
55 }
56 
57 std::optional<std::tuple<uint32_t, uint64_t, uint64_t>>
58     extractDumpDetails(const std::filesystem::path& file)
59 {
60     static constexpr auto ID_POS = 1;
61     static constexpr auto EPOCHTIME_POS = 2;
62     std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
63 
64     std::smatch match;
65     std::string name = file.filename().string();
66 
67     if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
68     {
69         lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
70                    file);
71         return std::nullopt;
72     }
73 
74     auto idString = match[ID_POS];
75     uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
76 
77     return std::make_tuple(stoul(idString), timestamp,
78                            std::filesystem::file_size(file));
79 }
80 
81 } // namespace dump
82 } // namespace phosphor
83