1 #include "update_agent_file.h"
3 #include "crypto/crypto.h"
4 #include "logging/logging.h"
5 #include "uptane/manifest.h"
9 bool FileUpdateAgent::isTargetSupported(
const Uptane::Target& target)
const {
return target.type() !=
"OSTREE"; }
12 if (boost::filesystem::exists(target_filepath_)) {
13 auto file_content = Utils::readFile(target_filepath_);
15 installed_image_info.name = current_target_name_;
16 installed_image_info.len = file_content.size();
17 installed_image_info.hash = Uptane::ManifestIssuer::generateVersionHashStr(file_content);
20 auto unknown_target = Uptane::Target::Unknown();
21 installed_image_info.name = unknown_target.filename();
22 installed_image_info.len = unknown_target.length();
23 installed_image_info.hash = unknown_target.sha256Hash();
30 if (!boost::filesystem::exists(new_target_filepath_)) {
31 LOG_ERROR <<
"The target image has not been received";
33 "The target image has not been received");
36 auto received_target_image_size = boost::filesystem::file_size(new_target_filepath_);
37 if (received_target_image_size != target.length()) {
38 LOG_ERROR <<
"Received image size does not match the size specified in Target metadata: "
39 << received_target_image_size <<
" != " << target.length();
40 boost::filesystem::remove(new_target_filepath_);
42 "Received image size does not match the size specified in Target metadata: " +
43 std::to_string(received_target_image_size) +
44 " != " + std::to_string(target.length()));
47 if (!target.MatchHash(new_target_hasher_->getHash())) {
48 LOG_ERROR <<
"The received image's hash does not match the hash specified in Target metadata: "
49 << new_target_hasher_->getHash() <<
" != " << getTargetHash(target).HashString();
51 "The received image's hash does not match the hash specified in Target metadata: " +
52 new_target_hasher_->getHash().HashString() +
53 " != " + getTargetHash(target).HashString());
56 boost::filesystem::rename(new_target_filepath_, target_filepath_);
58 if (boost::filesystem::exists(new_target_filepath_)) {
60 "The target image has not been installed");
63 if (!boost::filesystem::exists(target_filepath_)) {
65 "The target image has not been installed");
68 current_target_name_ = target.filename();
69 new_target_hasher_.reset();
73 void FileUpdateAgent::completeInstall() {}
78 "Applying pending updates is not supported by the file update agent");
82 std::ofstream target_file(new_target_filepath_.c_str(),
83 std::ofstream::out | std::ofstream::binary | std::ofstream::app);
85 if (!target_file.good()) {
86 LOG_ERROR <<
"Failed to open a new target image file";
88 "Failed to open a new target image file");
91 auto current_new_image_size = target_file.tellp();
92 if (-1 == current_new_image_size) {
93 LOG_ERROR <<
"Failed to obtain a size of the new target image that is being uploaded";
96 "Failed to obtain a size of the new target image that is being uploaded");
99 if (
static_cast<uint64_t
>(current_new_image_size) >= target.length()) {
100 LOG_ERROR <<
"The size of the received image data exceeds the expected Target image size: "
101 << current_new_image_size <<
" != " << target.length();
104 "The size of the received image data exceeds the expected Target image size: " +
105 std::to_string(current_new_image_size) +
106 " != " + std::to_string(target.length()));
109 if (current_new_image_size == 0) {
110 new_target_hasher_ = MultiPartHasher::create(getTargetHash(target).type());
113 target_file.write(
reinterpret_cast<const char*
>(
data),
static_cast<std::streamsize
>(size));
114 auto written_data_size = target_file.tellp() - current_new_image_size;
116 if (written_data_size < 0 ||
static_cast<size_t>(written_data_size) != size) {
117 LOG_ERROR <<
"The size of data written is not equal to the received data size: " << written_data_size
121 "The size of data written is not equal to the received data size: " +
122 std::to_string(written_data_size) +
" != " + std::to_string(size));
127 auto total_size = current_new_image_size + written_data_size;
128 LOG_DEBUG <<
"Received and stored data of a new target image."
129 " Received in this request (bytes): "
130 << size <<
"; total received so far: " << total_size <<
"; expected total: " << target.length();
131 if (
static_cast<uint64_t
>(total_size) == target.length()) {
132 LOG_INFO <<
"Successfully received and stored new target image of " << total_size <<
" bytes.";
135 new_target_hasher_->update(
data, size);
142 return target.hashes()[0];