blob: d1e4dc74b3d2d88e307d4a386556e686968e403f [file] [log] [blame]
Patrick Ventureaf696252018-12-11 10:22:14 -08001#include "bt.hpp"
2
Patrick Venture907f3a72019-01-15 14:13:37 -08003#include <cstdint>
Patrick Venture664c5bc2019-03-07 08:09:45 -08004#include <ipmiblob/blob_errors.hpp>
Patrick Venture907f3a72019-01-15 14:13:37 -08005#include <vector>
6
Patrick Venture9b534f02018-12-13 16:10:02 -08007namespace host_tool
8{
9
Patrick Ventureaf696252018-12-11 10:22:14 -080010bool BtDataHandler::sendContents(const std::string& input,
11 std::uint16_t session)
12{
Patrick Venture907f3a72019-01-15 14:13:37 -080013 int inputFd = sys->open(input.c_str(), 0);
14 if (inputFd < 0)
15 {
16 return false;
17 }
18
Patrick Venturecf9b2192019-06-27 12:09:52 -070019 std::int64_t fileSize = sys->getSize(input.c_str());
20 if (fileSize == 0)
21 {
22 std::fprintf(stderr, "Zero-length file, or other file access error\n");
23 return false;
24 }
25
26 progress->start(fileSize);
27
Patrick Venture907f3a72019-01-15 14:13:37 -080028 try
29 {
Patrick Venture63528042019-05-20 18:10:02 -070030 static constexpr int btBufferLen = 50;
31 std::uint8_t readBuffer[btBufferLen];
32 int bytesRead;
33 std::uint32_t offset = 0;
34
Patrick Venture907f3a72019-01-15 14:13:37 -080035 do
36 {
37 bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
38 if (bytesRead > 0)
39 {
40 /* minorly awkward repackaging. */
41 std::vector<std::uint8_t> buffer(&readBuffer[0],
42 &readBuffer[bytesRead]);
43 blob->writeBytes(session, offset, buffer);
44 offset += bytesRead;
Patrick Venturecf9b2192019-06-27 12:09:52 -070045 progress->updateProgress(bytesRead);
Patrick Venture907f3a72019-01-15 14:13:37 -080046 }
47 } while (bytesRead > 0);
48 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080049 catch (const ipmiblob::BlobException& b)
Patrick Venture907f3a72019-01-15 14:13:37 -080050 {
51 sys->close(inputFd);
52 return false;
53 }
54
55 sys->close(inputFd);
56 return true;
Patrick Ventureaf696252018-12-11 10:22:14 -080057}
Patrick Venture9b534f02018-12-13 16:10:02 -080058
59} // namespace host_tool