Jagpal Singh Gill | 2431e7f | 2024-04-07 23:51:12 -0700 | [diff] [blame] | 1 | #include "software_utils.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/lg2.hpp> |
| 4 | |
| 5 | PHOSPHOR_LOG2_USING; |
| 6 | |
| 7 | namespace phosphor::software::utils |
| 8 | { |
| 9 | |
| 10 | static bool writeToFile(int imageFd, FILE* outStream) |
| 11 | { |
| 12 | const int bSize = 100; |
| 13 | ssize_t nRead = 0; |
| 14 | unsigned char buf[bSize]; |
| 15 | |
| 16 | while ((nRead = read(imageFd, buf, bSize)) > 0) |
| 17 | { |
| 18 | if (fwrite(buf, 1, nRead, outStream) != (size_t)nRead) |
| 19 | { |
| 20 | error("Failed to write to file"); |
| 21 | return false; |
| 22 | } |
| 23 | } |
| 24 | if (nRead < 0) |
| 25 | { |
| 26 | error("Failed to read from input file"); |
| 27 | return false; |
| 28 | } |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | bool unTar(int imageFd, const std::string& extractDirPath) |
| 33 | { |
| 34 | std::string tarCmd = "tar -xf - -C " + extractDirPath + " --no-same-owner"; |
| 35 | info("Executing command: {CMD}", "CMD", tarCmd); |
| 36 | FILE* outStream = popen(tarCmd.c_str(), "w"); |
| 37 | if (outStream == nullptr) |
| 38 | { |
| 39 | error("Failed to open pipe to execute command: {CMD}", "CMD", tarCmd); |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | if (!writeToFile(imageFd, outStream)) |
| 44 | { |
| 45 | error("Failed to write to file"); |
| 46 | pclose(outStream); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if (pclose(outStream) != 0) |
| 51 | { |
| 52 | error("Failed to close pipe"); |
| 53 | return false; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | } // namespace phosphor::software::utils |
| 59 | |
| 60 | boost::asio::io_context& getIOContext() |
| 61 | { |
| 62 | static boost::asio::io_context io; |
| 63 | return io; |
| 64 | } |