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: this is an initial version of a file update on Secondary
6 bool FileUpdateAgent::isTargetSupported(const Uptane::Target& target) const { return target.type() != "OSTREE"; }
7 
8 bool FileUpdateAgent::getInstalledImageInfo(Uptane::InstalledImageInfo& installed_image_info) const {
9  if (boost::filesystem::exists(target_filepath_)) {
10  auto file_content = Utils::readFile(target_filepath_);
11 
12  installed_image_info.name = current_target_name_;
13  installed_image_info.len = file_content.size();
14  installed_image_info.hash = Uptane::ManifestIssuer::generateVersionHashStr(file_content);
15  } else {
16  // mimic the Primary's fake package manager behavior
17  auto unknown_target = Uptane::Target::Unknown();
18  installed_image_info.name = unknown_target.filename();
19  installed_image_info.len = unknown_target.length();
20  installed_image_info.hash = unknown_target.sha256Hash();
21  }
22 
23  return true;
24 }
25 
26 bool FileUpdateAgent::download(const Uptane::Target& target, const std::string& data) {
27  auto target_hashes = target.hashes();
28  if (target_hashes.size() == 0) {
29  LOG_ERROR << "No hash found in the target metadata: " << target.filename();
30  return false;
31  }
32 
33  try {
34  auto received_image_data_hash = Uptane::ManifestIssuer::generateVersionHash(data);
35 
36  if (!target.MatchHash(received_image_data_hash)) {
37  LOG_ERROR << "The received image data hash doesn't match the hash specified in the target metadata,"
38  " hash type: "
39  << target_hashes[0].TypeString();
40  return false;
41  }
42 
43  Utils::writeFile(target_filepath_, data);
44  current_target_name_ = target.filename();
45 
46  } catch (const std::exception& exc) {
47  LOG_ERROR << "Failed to generate a hash of the received image data: " << exc.what();
48  return false;
49  }
50  return true;
51 }
52 
53 data::ResultCode::Numeric FileUpdateAgent::install(const Uptane::Target& target) {
54  (void)target;
55  return data::ResultCode::Numeric::kOk;
56 }
57 
58 data::InstallationResult FileUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
59  (void)target;
61  "Applying of the pending updates are not supported by the file update agent");
62 }
data::InstallationResult
Definition: types.h:179
data
General data structures.
Definition: types.cc:44
Uptane::InstalledImageInfo
Definition: tuf.h:132
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric
Numeric
Definition: types.h:125