Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
update_agent_file.cc
1 #include "update_agent_file.h"
2 #include "logging/logging.h"
3 #include "uptane/manifest.h"
4 
5 // TODO(OTA-4939): Unify this with the check in
6 // SotaUptaneClient::getNewTargets() and make it more generic.
7 bool FileUpdateAgent::isTargetSupported(const Uptane::Target& target) const { return target.type() != "OSTREE"; }
8 
9 bool FileUpdateAgent::getInstalledImageInfo(Uptane::InstalledImageInfo& installed_image_info) const {
10  if (boost::filesystem::exists(target_filepath_)) {
11  auto file_content = Utils::readFile(target_filepath_);
12 
13  installed_image_info.name = current_target_name_;
14  installed_image_info.len = file_content.size();
15  installed_image_info.hash = Uptane::ManifestIssuer::generateVersionHashStr(file_content);
16  } else {
17  // mimic the Primary's fake package manager behavior
18  auto unknown_target = Uptane::Target::Unknown();
19  installed_image_info.name = unknown_target.filename();
20  installed_image_info.len = unknown_target.length();
21  installed_image_info.hash = unknown_target.sha256Hash();
22  }
23 
24  return true;
25 }
26 
27 bool FileUpdateAgent::download(const Uptane::Target& target, const std::string& data) {
28  auto target_hashes = target.hashes();
29  if (target_hashes.size() == 0) {
30  LOG_ERROR << "No hash found in the target metadata: " << target.filename();
31  return false;
32  }
33 
34  try {
35  auto received_image_data_hash = Uptane::ManifestIssuer::generateVersionHash(data);
36 
37  if (!target.MatchHash(received_image_data_hash)) {
38  LOG_ERROR << "The received image data hash doesn't match the hash specified in the target metadata,"
39  " hash type: "
40  << target_hashes[0].TypeString();
41  return false;
42  }
43 
44  Utils::writeFile(target_filepath_, data);
45  current_target_name_ = target.filename();
46 
47  } catch (const std::exception& exc) {
48  LOG_ERROR << "Failed to generate a hash of the received image data: " << exc.what();
49  return false;
50  }
51  return true;
52 }
53 
54 data::ResultCode::Numeric FileUpdateAgent::install(const Uptane::Target& target) {
55  (void)target;
56  return data::ResultCode::Numeric::kOk;
57 }
58 
59 void FileUpdateAgent::completeInstall() {}
60 
61 data::InstallationResult FileUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
62  (void)target;
64  "Applying of the pending updates are not supported by the file update agent");
65 }
General data structures.
Definition: types.cc:55
SWM Internal integrity error.