xref: /openbmc/phosphor-debug-collector/dump_offload.cpp (revision a63f4a6f17cd5fa743d2cb7ad4e03f63d8a916aa)
1 #include "config.h"
2 
3 #include "dump_offload.hpp"
4 
5 #include <fstream>
6 #include <phosphor-logging/elog-errors.hpp>
7 #include <phosphor-logging/elog.hpp>
8 #include <xyz/openbmc_project/Common/File/error.hpp>
9 #include <xyz/openbmc_project/Common/error.hpp>
10 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 namespace offload
16 {
17 
18 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
19 using namespace phosphor::logging;
20 
21 void requestOffload(fs::path file, uint32_t dumpId, std::string writePath)
22 {
23     using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
24     using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO;
25     using PathOpen = xyz::openbmc_project::Common::File::Open::PATH;
26     using ErrnoWrite = xyz::openbmc_project::Common::File::Write::ERRNO;
27     using PathWrite = xyz::openbmc_project::Common::File::Write::PATH;
28 
29     // open a dump file for a transfer.
30     fs::path dumpPath(BMC_DUMP_PATH);
31     dumpPath /= std::to_string(dumpId);
32     dumpPath /= file.filename();
33 
34     std::ifstream infile{dumpPath, std::ios::in | std::ios::binary};
35     if (!infile.good())
36     {
37         // Unable to open the dump file
38         log<level::ERR>("Failed to open the dump from file ",
39                         entry("ERR=%d", errno),
40                         entry("DUMPFILE=%s", dumpPath.c_str()));
41         elog<InternalFailure>();
42     }
43 
44     std::ofstream outfile{writePath, std::ios::out | std::ios::binary};
45     if (!outfile.good())
46     {
47         // Unable to open the write interface
48         auto err = errno;
49         log<level::ERR>("Write device path does not exist at ",
50                         entry("ERR=%d", errno),
51                         entry("WRITEINTERFACE=%s", writePath.c_str()));
52         elog<Open>(ErrnoOpen(err), PathOpen(writePath.c_str()));
53     }
54 
55     infile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
56                       std::ifstream::eofbit);
57     outfile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
58                        std::ifstream::eofbit);
59 
60     try
61     {
62         log<level::INFO>("Opening File for RW ",
63                          entry("FILENAME=%s", file.filename().c_str()));
64         outfile << infile.rdbuf() << std::flush;
65         infile.close();
66         outfile.close();
67     }
68     catch (std::ofstream::failure& oe)
69     {
70         auto err = errno;
71         log<level::ERR>("Failed to write to write interface ",
72                         entry("ERR=%s", oe.what()),
73                         entry("WRITEINTERFACE=%s", writePath.c_str()));
74         elog<Write>(ErrnoWrite(err), PathWrite(writePath.c_str()));
75     }
76     catch (const std::exception& e)
77     {
78         log<level::ERR>("Failed get the dump from file ",
79                         entry("ERR=%s", e.what()),
80                         entry("DUMPFILE=%s", dumpPath.c_str()));
81         elog<InternalFailure>();
82     }
83 }
84 
85 } // namespace offload
86 } // namespace dump
87 } // namespace phosphor
88