blob: 516a456287a59dbea8b31544f50f0c47e2e0c3a9 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/download_action.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <errno.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070020
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070022#include <string>
David Zeuthen8f191b22013-08-06 12:27:50 -070023
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/files/file_path.h>
Lann Martin39f57142017-07-14 09:18:42 -060025#include <base/metrics/statistics_recorder.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action_pipe.h"
29#include "update_engine/common/boot_control_interface.h"
Alex Deymoed9cc182016-06-15 16:19:29 -070030#include "update_engine/common/error_code_utils.h"
Sen Jiang5ae865b2017-04-18 14:24:40 -070031#include "update_engine/common/multi_range_http_fetcher.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/utils.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070033#include "update_engine/omaha_request_params.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070034#include "update_engine/p2p_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070035#include "update_engine/payload_state_interface.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000036
Alex Deymof329b932014-10-30 01:37:48 -070037using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070038using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000039
40namespace chromeos_update_engine {
41
Darin Petkov73058b42010-10-06 16:32:19 -070042DownloadAction::DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080043 BootControlInterface* boot_control,
44 HardwareInterface* hardware,
Jay Srinivasanf0572052012-10-23 18:12:56 -070045 SystemState* system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -070046 HttpFetcher* http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -070047 bool interactive)
Darin Petkov73058b42010-10-06 16:32:19 -070048 : prefs_(prefs),
Alex Deymo1b3556c2016-02-03 09:54:02 -080049 boot_control_(boot_control),
50 hardware_(hardware),
Jay Srinivasanedce2832012-10-24 18:57:47 -070051 system_state_(system_state),
Sen Jiang5ae865b2017-04-18 14:24:40 -070052 http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
Amin Hassanied37d682018-04-06 13:22:00 -070053 interactive_(interactive),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070054 writer_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070055 code_(ErrorCode::kSuccess),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070056 delegate_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -070057 p2p_sharing_fd_(-1),
Alex Deymo1b3556c2016-02-03 09:54:02 -080058 p2p_visible_(true) {
Amin Hassani2e4eda52019-01-07 14:01:17 -080059#if BASE_VER < 576279
Lann Martin39f57142017-07-14 09:18:42 -060060 base::StatisticsRecorder::Initialize();
Amin Hassani2e4eda52019-01-07 14:01:17 -080061#endif
Alex Deymo1b3556c2016-02-03 09:54:02 -080062}
rspangler@google.com49fdf182009-10-10 00:57:34 +000063
64DownloadAction::~DownloadAction() {}
65
David Zeuthen8f191b22013-08-06 12:27:50 -070066void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
67 if (p2p_sharing_fd_ != -1) {
68 if (close(p2p_sharing_fd_) != 0) {
69 PLOG(ERROR) << "Error closing p2p sharing fd";
70 }
71 p2p_sharing_fd_ = -1;
72 }
73
74 if (delete_p2p_file) {
Alex Deymof329b932014-10-30 01:37:48 -070075 FilePath path =
Alex Vakulenko75039d72014-03-25 12:36:28 -070076 system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070077 if (unlink(path.value().c_str()) != 0) {
78 PLOG(ERROR) << "Error deleting p2p file " << path.value();
79 } else {
80 LOG(INFO) << "Deleted p2p file " << path.value();
81 }
82 }
83
84 // Don't use p2p from this point onwards.
85 p2p_file_id_.clear();
86}
87
88bool DownloadAction::SetupP2PSharingFd() {
89 P2PManager *p2p_manager = system_state_->p2p_manager();
90
Sen Jiang0affc2c2017-02-10 15:55:05 -080091 if (!p2p_manager->FileShare(p2p_file_id_, payload_->size)) {
David Zeuthen8f191b22013-08-06 12:27:50 -070092 LOG(ERROR) << "Unable to share file via p2p";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070093 CloseP2PSharingFd(true); // delete p2p file
David Zeuthen8f191b22013-08-06 12:27:50 -070094 return false;
95 }
96
97 // File has already been created (and allocated, xattrs been
98 // populated etc.) by FileShare() so just open it for writing.
Alex Deymof329b932014-10-30 01:37:48 -070099 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700100 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
101 if (p2p_sharing_fd_ == -1) {
102 PLOG(ERROR) << "Error opening file " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700103 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700104 return false;
105 }
106
107 // Ensure file to share is world-readable, otherwise
108 // p2p-server and p2p-http-server can't access it.
109 //
110 // (Q: Why doesn't the file have mode 0644 already? A: Because
111 // the process-wide umask is set to 0700 in main.cc.)
112 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
113 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700114 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700115 return false;
116 }
117
118 // All good.
119 LOG(INFO) << "Writing payload contents to " << path.value();
120 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
121 return true;
122}
123
Alex Deymo60ca1a72015-06-18 18:19:15 -0700124void DownloadAction::WriteToP2PFile(const void* data,
David Zeuthen8f191b22013-08-06 12:27:50 -0700125 size_t length,
126 off_t file_offset) {
127 if (p2p_sharing_fd_ == -1) {
128 if (!SetupP2PSharingFd())
129 return;
130 }
131
132 // Check that the file is at least |file_offset| bytes long - if
133 // it's not something is wrong and we must immediately delete the
134 // file to avoid propagating this problem to other peers.
135 //
136 // How can this happen? It could be that we're resuming an update
137 // after a system crash... in this case, it could be that
138 //
139 // 1. the p2p file didn't get properly synced to stable storage; or
140 // 2. the file was deleted at bootup (it's in /var/cache after all); or
141 // 3. other reasons
Gabe Blacka77939e2014-09-09 23:35:08 -0700142 off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
143 if (p2p_size < 0) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700144 PLOG(ERROR) << "Error getting file status for p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700145 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700146 return;
147 }
Gabe Blacka77939e2014-09-09 23:35:08 -0700148 if (p2p_size < file_offset) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700149 LOG(ERROR) << "Wanting to write to file offset " << file_offset
Gabe Blacka77939e2014-09-09 23:35:08 -0700150 << " but existing p2p file is only " << p2p_size
David Zeuthen8f191b22013-08-06 12:27:50 -0700151 << " bytes.";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700152 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700153 return;
154 }
155
156 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
157 if (cur_file_offset != static_cast<off_t>(file_offset)) {
158 PLOG(ERROR) << "Error seeking to position "
159 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700160 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700161 } else {
162 // OK, seeking worked, now write the data
163 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
164 if (bytes_written != static_cast<ssize_t>(length)) {
165 PLOG(ERROR) << "Error writing "
166 << length << " bytes at file offset "
167 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700168 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700169 }
170 }
171}
172
rspangler@google.com49fdf182009-10-10 00:57:34 +0000173void DownloadAction::PerformAction() {
174 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000175
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000176 // Get the InstallPlan and read it
177 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700178 install_plan_ = GetInputObject();
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700179 install_plan_.Dump();
Sen Jiang5ae865b2017-04-18 14:24:40 -0700180
181 bytes_received_ = 0;
Aaron Wood9321f502017-09-07 11:18:54 -0700182 bytes_received_previous_payloads_ = 0;
Sen Jiang5ae865b2017-04-18 14:24:40 -0700183 bytes_total_ = 0;
184 for (const auto& payload : install_plan_.payloads)
185 bytes_total_ += payload.size;
186
187 if (install_plan_.is_resume) {
188 int64_t payload_index = 0;
189 if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
190 static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
191 // Save the index for the resume payload before downloading any previous
192 // payload, otherwise it will be overwritten.
193 resume_payload_index_ = payload_index;
194 for (int i = 0; i < payload_index; i++)
195 install_plan_.payloads[i].already_applied = true;
196 }
197 }
Sen Jiang0affc2c2017-02-10 15:55:05 -0800198 // TODO(senj): check that install plan has at least one payload.
199 if (!payload_)
200 payload_ = &install_plan_.payloads[0];
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000201
Alex Deymo5ed695e2015-10-05 16:59:23 -0700202 LOG(INFO) << "Marking new slot as unbootable";
Alex Deymo1b3556c2016-02-03 09:54:02 -0800203 if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
Alex Deymo5ed695e2015-10-05 16:59:23 -0700204 LOG(WARNING) << "Unable to mark new slot "
205 << BootControlInterface::SlotName(install_plan_.target_slot)
206 << ". Proceeding with the update anyway.";
207 }
208
Sen Jiang6c736682017-03-10 15:01:36 -0800209 StartDownloading();
210}
211
212void DownloadAction::StartDownloading() {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700213 download_active_ = true;
214 http_fetcher_->ClearRanges();
215 if (install_plan_.is_resume &&
216 payload_ == &install_plan_.payloads[resume_payload_index_]) {
217 // Resuming an update so fetch the update manifest metadata first.
218 int64_t manifest_metadata_size = 0;
219 int64_t manifest_signature_size = 0;
220 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
221 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
222 http_fetcher_->AddRange(base_offset_,
223 manifest_metadata_size + manifest_signature_size);
224 // If there're remaining unprocessed data blobs, fetch them. Be careful not
225 // to request data beyond the end of the payload to avoid 416 HTTP response
226 // error codes.
227 int64_t next_data_offset = 0;
228 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
229 uint64_t resume_offset =
230 manifest_metadata_size + manifest_signature_size + next_data_offset;
231 if (!payload_->size) {
232 http_fetcher_->AddRange(base_offset_ + resume_offset);
233 } else if (resume_offset < payload_->size) {
234 http_fetcher_->AddRange(base_offset_ + resume_offset,
235 payload_->size - resume_offset);
236 }
237 } else {
238 if (payload_->size) {
239 http_fetcher_->AddRange(base_offset_, payload_->size);
240 } else {
241 // If no payload size is passed we assume we read until the end of the
242 // stream.
243 http_fetcher_->AddRange(base_offset_);
244 }
245 }
246
Sen Jiang6c736682017-03-10 15:01:36 -0800247 if (writer_ && writer_ != delta_performer_.get()) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700248 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000249 } else {
Amin Hassani7ecda262017-07-11 17:10:50 -0700250 delta_performer_.reset(new DeltaPerformer(prefs_,
251 boot_control_,
252 hardware_,
253 delegate_,
254 &install_plan_,
Sen Jiang18414082018-01-11 14:50:36 -0800255 payload_,
Amin Hassanied37d682018-04-06 13:22:00 -0700256 interactive_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700257 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000258 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700259 if (system_state_ != nullptr) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700260 const PayloadStateInterface* payload_state = system_state_->payload_state();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800261 string file_id = utils::CalculateP2PFileId(payload_->hash, payload_->size);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700262 if (payload_state->GetUsingP2PForSharing()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700263 // If we're sharing the update, store the file_id to convey
264 // that we should write to the file.
265 p2p_file_id_ = file_id;
266 LOG(INFO) << "p2p file id: " << p2p_file_id_;
267 } else {
268 // Even if we're not sharing the update, it could be that
269 // there's a partial file from a previous attempt with the same
270 // hash. If this is the case, we NEED to clean it up otherwise
271 // we're essentially timing out other peers downloading from us
272 // (since we're never going to complete the file).
Alex Deymof329b932014-10-30 01:37:48 -0700273 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
David Zeuthen8f191b22013-08-06 12:27:50 -0700274 if (!path.empty()) {
275 if (unlink(path.value().c_str()) != 0) {
276 PLOG(ERROR) << "Error deleting p2p file " << path.value();
277 } else {
278 LOG(INFO) << "Deleting partial p2p file " << path.value()
279 << " since we're not using p2p to share.";
280 }
281 }
282 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700283
Gilad Arnold74b5f552014-10-07 08:17:16 -0700284 // Tweak timeouts on the HTTP fetcher if we're downloading from a
285 // local peer.
286 if (payload_state->GetUsingP2PForDownloading() &&
287 payload_state->GetP2PUrl() == install_plan_.download_url) {
288 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
289 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
290 kDownloadP2PLowSpeedTimeSeconds);
291 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
292 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
293 }
David Zeuthen34135a92013-08-06 11:16:16 -0700294 }
295
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700296 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000297}
298
Alex Deymof2858572016-02-25 11:20:13 -0800299void DownloadAction::SuspendAction() {
300 http_fetcher_->Pause();
301}
302
303void DownloadAction::ResumeAction() {
304 http_fetcher_->Unpause();
305}
306
rspangler@google.com49fdf182009-10-10 00:57:34 +0000307void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700308 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700309 writer_->Close();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700310 writer_ = nullptr;
Darin Petkov698d0412010-10-13 10:59:44 -0700311 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800312 download_active_ = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700313 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800314 // Terminates the transfer. The action is terminated, if necessary, when the
315 // TransferTerminated callback is received.
316 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000317}
318
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700319void DownloadAction::SeekToOffset(off_t offset) {
320 bytes_received_ = offset;
321}
322
Amin Hassani0cd9d772018-07-31 23:55:43 -0700323bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800324 const void* bytes,
325 size_t length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700326 // Note that bytes_received_ is the current offset.
327 if (!p2p_file_id_.empty()) {
328 WriteToP2PFile(bytes, length, bytes_received_);
329 }
330
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700331 bytes_received_ += length;
Aaron Wood9321f502017-09-07 11:18:54 -0700332 uint64_t bytes_downloaded_total =
333 bytes_received_previous_payloads_ + bytes_received_;
Alex Deymo542c19b2015-12-03 07:43:31 -0300334 if (delegate_ && download_active_) {
Aaron Wood9321f502017-09-07 11:18:54 -0700335 delegate_->BytesReceived(length, bytes_downloaded_total, bytes_total_);
Alex Deymo542c19b2015-12-03 07:43:31 -0300336 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700337 if (writer_ && !writer_->Write(bytes, length, &code_)) {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700338 if (code_ != ErrorCode::kSuccess) {
339 LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
340 << ") in DeltaPerformer's Write method when "
341 << "processing the received payload -- Terminating processing";
342 }
David Zeuthen69bc2732013-11-26 16:08:21 -0800343 // Delete p2p file, if applicable.
344 if (!p2p_file_id_.empty())
345 CloseP2PSharingFd(true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800346 // Don't tell the action processor that the action is complete until we get
347 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
348 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700349 TerminateProcessing();
Amin Hassani0cd9d772018-07-31 23:55:43 -0700350 return false;
Darin Petkov698d0412010-10-13 10:59:44 -0700351 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700352
353 // Call p2p_manager_->FileMakeVisible() when we've successfully
354 // verified the manifest!
Alex Deymo1b3556c2016-02-03 09:54:02 -0800355 if (!p2p_visible_ && system_state_ && delta_performer_.get() &&
356 delta_performer_->IsManifestValid()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700357 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
358 system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
359 p2p_visible_ = true;
360 }
Amin Hassani0cd9d772018-07-31 23:55:43 -0700361 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000362}
363
Alex Deymo60ca1a72015-06-18 18:19:15 -0700364void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000365 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700366 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
Aaron Wood9321f502017-09-07 11:18:54 -0700367 if (delta_performer_.get() == writer_) {
368 // no delta_performer_ in tests, so leave the test writer in place
369 writer_ = nullptr;
370 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000371 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800372 download_active_ = false;
David Zeuthena99981f2013-04-29 13:42:47 -0700373 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700374 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
Aaron Wood9321f502017-09-07 11:18:54 -0700375 if (code == ErrorCode::kSuccess) {
376 if (delta_performer_ && !payload_->already_applied)
Sen Jiang5ae865b2017-04-18 14:24:40 -0700377 code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
Lann Martin39f57142017-07-14 09:18:42 -0600378 if (code == ErrorCode::kSuccess) {
Sen Jiang18414082018-01-11 14:50:36 -0800379 if (payload_ < &install_plan_.payloads.back() &&
380 system_state_->payload_state()->NextPayload()) {
381 LOG(INFO) << "Incrementing to next payload";
382 // No need to reset if this payload was already applied.
383 if (delta_performer_ && !payload_->already_applied)
384 DeltaPerformer::ResetUpdateProgress(prefs_, false);
385 // Start downloading next payload.
386 bytes_received_previous_payloads_ += payload_->size;
387 payload_++;
388 install_plan_.download_url =
389 system_state_->payload_state()->GetCurrentUrl();
390 StartDownloading();
391 return;
392 }
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700393
394 // All payloads have been applied and verified.
395 if (delegate_)
396 delegate_->DownloadComplete();
397
Lann Martin39f57142017-07-14 09:18:42 -0600398 // Log UpdateEngine.DownloadAction.* histograms to help diagnose
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700399 // long-blocking operations.
Lann Martin39f57142017-07-14 09:18:42 -0600400 std::string histogram_output;
401 base::StatisticsRecorder::WriteGraph(
402 "UpdateEngine.DownloadAction.", &histogram_output);
403 LOG(INFO) << histogram_output;
404 } else {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700405 LOG(ERROR) << "Download of " << install_plan_.download_url
406 << " failed due to payload verification error.";
David Zeuthen69bc2732013-11-26 16:08:21 -0800407 // Delete p2p file, if applicable.
408 if (!p2p_file_id_.empty())
409 CloseP2PSharingFd(true);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410 }
411 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700412
Darin Petkovc97435c2010-07-20 12:37:43 -0700413 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700414 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800415 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700416 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000417}
418
Sen Jiang5ae865b2017-04-18 14:24:40 -0700419void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700420 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800421 processor_->ActionComplete(this, code_);
Sen Jiang5ae865b2017-04-18 14:24:40 -0700422 } else if (payload_->already_applied) {
423 LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
424 "payload has already applied, treating as TransferComplete.";
425 TransferComplete(fetcher, true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800426 }
427}
428
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700429} // namespace chromeos_update_engine