Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
androidmanager.cc
1 #include <boost/algorithm/string/find.hpp>
2 #include <boost/filesystem.hpp>
3 #include <forward_list>
4 
5 #include "androidmanager.h"
6 
7 #include "utilities/utils.h"
8 
9 #include <boost/phoenix/stl/algorithm/transformation.hpp>
10 #include <boost/spirit/include/phoenix_container.hpp>
11 #include <boost/spirit/include/phoenix_core.hpp>
12 #include <boost/spirit/include/qi.hpp>
13 
14 namespace qi = boost::spirit::qi;
15 namespace fs = boost::filesystem;
16 
17 const std::string AndroidManager::data_ota_package_dir_ = "/data/ota_package";
18 
19 Json::Value AndroidManager::getInstalledPackages() const {
20  using boost::phoenix::copy;
21  using qi::_1;
22  using qi::char_;
23 
24  std::string pm_output;
25  Json::Value packages(Json::arrayValue);
26  if (0 != Utils::shell("pm list packages --show-versioncode", &pm_output)) {
27  return packages;
28  }
29 
30  qi::rule<std::string::iterator, std::vector<char>()> char_seq = qi::lexeme[*(char_ - ' ')];
31 
32  std::istringstream pv_lines(pm_output);
33  for (std::string line; std::getline(pv_lines, line);) {
34  std::string p, v;
35  if (qi::parse(line.begin(), line.end(),
36  ("package:" >> char_seq[copy(_1, std::back_inserter(p))] >> ' ' >> "versionCode:" >>
37  char_seq[copy(_1, std::back_inserter(v))]))) {
38  Json::Value package;
39  package["name"] = p;
40  package["version"] = v;
41  packages.append(package);
42  }
43  }
44  return packages;
45 }
46 
47 Uptane::Target AndroidManager::getCurrent() const {
48  using boost::phoenix::push_front;
49  using boost::spirit::ascii::xdigit;
50  using qi::_1;
51 
52  std::string getprop_output;
53  if (0 == Utils::shell("getprop ota.last_installed_package_file", &getprop_output)) {
54  std::forward_list<char> hash;
55  qi::phrase_parse(getprop_output.crbegin(), getprop_output.crend(),
56  *(xdigit[push_front(boost::phoenix::ref(hash), _1)]), boost::spirit::ascii::cntrl);
57  std::vector<Uptane::Target> installed_versions;
58  storage_->loadPrimaryInstallationLog(&installed_versions, false);
59  for (const auto& target : installed_versions) {
60  if (std::equal(hash.cbegin(), hash.cend(), target.sha256Hash().cbegin())) {
61  return target;
62  }
63  }
64  }
65  return Uptane::Target::Unknown();
66 }
67 
68 data::InstallationResult AndroidManager::install(const Uptane::Target& target) const {
69  LOG_INFO << "Begin Android package installation";
70  auto package_filename = (fs::path(data_ota_package_dir_) / target.filename()).string() + "." + target.sha256Hash();
71  std::ofstream package_file(package_filename.c_str());
72  if (!package_file.good()) {
73  throw std::runtime_error(std::string("Error opening file ") + package_filename);
74  }
75  package_file << *storage_->openTargetFile(target);
76 
77  if (bootloader_ != nullptr) {
78  bootloader_->rebootFlagSet();
79  }
80  LOG_INFO << "Performing sync()";
81  sync();
82  return data::InstallationResult(data::ResultCode::Numeric::kNeedCompletion, "need reboot");
83 }
84 
85 data::InstallationResult AndroidManager::finalizeInstall(const Uptane::Target& target) const {
86  std::string ota_package_file_path = GetOTAPackageFilePath(target.sha256Hash());
87  if (!ota_package_file_path.empty()) fs::remove(ota_package_file_path);
88  std::string errorMessage{"n/a"};
89  if (installationAborted(&errorMessage)) {
91  }
92  return data::InstallationResult(data::ResultCode::Numeric::kOk, "package installation successfully finalized");
93 }
94 
95 std::string AndroidManager::GetOTAPackageFilePath(const std::string& hash) {
96  fs::directory_iterator entryItEnd, entryIt{fs::path(data_ota_package_dir_)};
97  for (; entryIt != entryItEnd; ++entryIt) {
98  auto& entry_path = entryIt->path();
99  if (boost::filesystem::is_directory(*entryIt)) {
100  continue;
101  }
102  auto ext = entry_path.extension().string();
103  ext = ext.substr(1);
104  if (ext == hash) {
105  return entry_path.string();
106  }
107  }
108  return std::string{};
109 }
110 
111 bool AndroidManager::installationAborted(std::string* errorMessage) const {
112  std::string installation_log_file{"/cache/recovery/last_install"};
113  std::ifstream log(installation_log_file.c_str());
114  if (!log.good()) {
115  throw std::runtime_error(std::string("Error opening file ") + installation_log_file);
116  }
117  for (std::string line; std::getline(log, line);) {
118  if (boost::algorithm::find_first(line, "error:")) {
119  int error_code = std::stoi(line.substr(6));
120  if (error_code != 0) {
121  *errorMessage = std::string("Error code: ") + std::to_string(error_code);
122  return true;
123  }
124  }
125  }
126  return false;
127 }
data::InstallationResult
Definition: types.h:179
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric::kInstallFailed
Package installation failed.