blob: eeac68604ee7cba11803a0ec6107f4bc6c3f2848 [file] [log] [blame]
Patrick Ventureaf696252018-12-11 10:22:14 -08001#include "bt.hpp"
2
Patrick Venture664c5bc2019-03-07 08:09:45 -08003#include <ipmiblob/blob_errors.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -07004
5#include <cstdint>
Patrick Venture907f3a72019-01-15 14:13:37 -08006#include <vector>
7
Patrick Venture9b534f02018-12-13 16:10:02 -08008namespace host_tool
9{
10
Patrick Ventureaf696252018-12-11 10:22:14 -080011bool BtDataHandler::sendContents(const std::string& input,
12 std::uint16_t session)
13{
Patrick Venture907f3a72019-01-15 14:13:37 -080014 int inputFd = sys->open(input.c_str(), 0);
15 if (inputFd < 0)
16 {
17 return false;
18 }
19
Patrick Venturecf9b2192019-06-27 12:09:52 -070020 std::int64_t fileSize = sys->getSize(input.c_str());
21 if (fileSize == 0)
22 {
23 std::fprintf(stderr, "Zero-length file, or other file access error\n");
24 return false;
25 }
26
27 progress->start(fileSize);
28
Patrick Venture907f3a72019-01-15 14:13:37 -080029 try
30 {
Patrick Venture63528042019-05-20 18:10:02 -070031 static constexpr int btBufferLen = 50;
32 std::uint8_t readBuffer[btBufferLen];
33 int bytesRead;
34 std::uint32_t offset = 0;
35
Patrick Venture907f3a72019-01-15 14:13:37 -080036 do
37 {
38 bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
39 if (bytesRead > 0)
40 {
41 /* minorly awkward repackaging. */
42 std::vector<std::uint8_t> buffer(&readBuffer[0],
43 &readBuffer[bytesRead]);
44 blob->writeBytes(session, offset, buffer);
45 offset += bytesRead;
Patrick Venturecf9b2192019-06-27 12:09:52 -070046 progress->updateProgress(bytesRead);
Patrick Venture907f3a72019-01-15 14:13:37 -080047 }
48 } while (bytesRead > 0);
49 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080050 catch (const ipmiblob::BlobException& b)
Patrick Venture907f3a72019-01-15 14:13:37 -080051 {
William A. Kennington III0d5bb782021-01-19 15:50:42 -080052 progress->abort();
Patrick Venture907f3a72019-01-15 14:13:37 -080053 sys->close(inputFd);
54 return false;
55 }
56
William A. Kennington III0d5bb782021-01-19 15:50:42 -080057 progress->finish();
Patrick Venture907f3a72019-01-15 14:13:37 -080058 sys->close(inputFd);
59 return true;
Patrick Ventureaf696252018-12-11 10:22:14 -080060}
Patrick Venture9b534f02018-12-13 16:10:02 -080061
62} // namespace host_tool