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