blob: aeeaeb7014a64cd67cd6c011eb7111228c09ae8f [file] [log] [blame]
Jagpal Singh Gill2431e7f2024-04-07 23:51:12 -07001#include "software_utils.hpp"
2
3#include <phosphor-logging/lg2.hpp>
4
5PHOSPHOR_LOG2_USING;
6
7namespace phosphor::software::utils
8{
9
10static 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
32bool 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
60boost::asio::io_context& getIOContext()
61{
62 static boost::asio::io_context io;
63 return io;
64}