blob: 7f885179c1b78c77e65aaa5b6ccdc9a68c8ced0d [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");
William A. Kennington IIIebccf632021-01-25 15:54:39 -080024 sys->close(inputFd);
Patrick Venturecf9b2192019-06-27 12:09:52 -070025 return false;
26 }
27
28 progress->start(fileSize);
29
Patrick Venture907f3a72019-01-15 14:13:37 -080030 try
31 {
Patrick Venture63528042019-05-20 18:10:02 -070032 static constexpr int btBufferLen = 50;
33 std::uint8_t readBuffer[btBufferLen];
34 int bytesRead;
35 std::uint32_t offset = 0;
36
Patrick Venture907f3a72019-01-15 14:13:37 -080037 do
38 {
39 bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
40 if (bytesRead > 0)
41 {
42 /* minorly awkward repackaging. */
43 std::vector<std::uint8_t> buffer(&readBuffer[0],
44 &readBuffer[bytesRead]);
45 blob->writeBytes(session, offset, buffer);
46 offset += bytesRead;
Patrick Venturecf9b2192019-06-27 12:09:52 -070047 progress->updateProgress(bytesRead);
Patrick Venture907f3a72019-01-15 14:13:37 -080048 }
49 } while (bytesRead > 0);
50 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080051 catch (const ipmiblob::BlobException& b)
Patrick Venture907f3a72019-01-15 14:13:37 -080052 {
William A. Kennington III0d5bb782021-01-19 15:50:42 -080053 progress->abort();
Patrick Venture907f3a72019-01-15 14:13:37 -080054 sys->close(inputFd);
55 return false;
56 }
57
William A. Kennington III0d5bb782021-01-19 15:50:42 -080058 progress->finish();
Patrick Venture907f3a72019-01-15 14:13:37 -080059 sys->close(inputFd);
60 return true;
Patrick Ventureaf696252018-12-11 10:22:14 -080061}
Patrick Venture9b534f02018-12-13 16:10:02 -080062
63} // namespace host_tool