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