blob: adae1281b2d03b3d10dba1d549d2bf8767603716 [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/common/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"
Amin Hassani538bd592020-11-04 20:46:08 -080032#include "update_engine/common/system_state.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070034#include "update_engine/cros/omaha_request_params.h"
35#include "update_engine/cros/p2p_manager.h"
36#include "update_engine/cros/payload_state_interface.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037
Alex Deymof329b932014-10-30 01:37:48 -070038using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070039using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
41namespace chromeos_update_engine {
42
Darin Petkov73058b42010-10-06 16:32:19 -070043DownloadAction::DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080044 BootControlInterface* boot_control,
45 HardwareInterface* hardware,
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),
Sen Jiang5ae865b2017-04-18 14:24:40 -070051 http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
Amin Hassanied37d682018-04-06 13:22:00 -070052 interactive_(interactive),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 writer_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070054 code_(ErrorCode::kSuccess),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070055 delegate_(nullptr),
David Zeuthen8f191b22013-08-06 12:27:50 -070056 p2p_sharing_fd_(-1),
Kelvin Zhangcc011d32020-07-10 18:20:08 -040057 p2p_visible_(true) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000058
59DownloadAction::~DownloadAction() {}
60
David Zeuthen8f191b22013-08-06 12:27:50 -070061void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
62 if (p2p_sharing_fd_ != -1) {
63 if (close(p2p_sharing_fd_) != 0) {
64 PLOG(ERROR) << "Error closing p2p sharing fd";
65 }
66 p2p_sharing_fd_ = -1;
67 }
68
69 if (delete_p2p_file) {
Amin Hassani538bd592020-11-04 20:46:08 -080070 FilePath path =
71 SystemState::Get()->p2p_manager()->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070072 if (unlink(path.value().c_str()) != 0) {
73 PLOG(ERROR) << "Error deleting p2p file " << path.value();
74 } else {
75 LOG(INFO) << "Deleted p2p file " << path.value();
76 }
77 }
78
79 // Don't use p2p from this point onwards.
80 p2p_file_id_.clear();
81}
82
83bool DownloadAction::SetupP2PSharingFd() {
Amin Hassani538bd592020-11-04 20:46:08 -080084 P2PManager* p2p_manager = SystemState::Get()->p2p_manager();
David Zeuthen8f191b22013-08-06 12:27:50 -070085
Sen Jiang0affc2c2017-02-10 15:55:05 -080086 if (!p2p_manager->FileShare(p2p_file_id_, payload_->size)) {
David Zeuthen8f191b22013-08-06 12:27:50 -070087 LOG(ERROR) << "Unable to share file via p2p";
Alex Vakulenkod2779df2014-06-16 13:19:00 -070088 CloseP2PSharingFd(true); // delete p2p file
David Zeuthen8f191b22013-08-06 12:27:50 -070089 return false;
90 }
91
92 // File has already been created (and allocated, xattrs been
93 // populated etc.) by FileShare() so just open it for writing.
Alex Deymof329b932014-10-30 01:37:48 -070094 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -070095 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
96 if (p2p_sharing_fd_ == -1) {
97 PLOG(ERROR) << "Error opening file " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -070098 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -070099 return false;
100 }
101
102 // Ensure file to share is world-readable, otherwise
103 // p2p-server and p2p-http-server can't access it.
104 //
105 // (Q: Why doesn't the file have mode 0644 already? A: Because
106 // the process-wide umask is set to 0700 in main.cc.)
107 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
108 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700109 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700110 return false;
111 }
112
113 // All good.
114 LOG(INFO) << "Writing payload contents to " << path.value();
115 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
116 return true;
117}
118
Alex Deymo60ca1a72015-06-18 18:19:15 -0700119void DownloadAction::WriteToP2PFile(const void* data,
David Zeuthen8f191b22013-08-06 12:27:50 -0700120 size_t length,
121 off_t file_offset) {
122 if (p2p_sharing_fd_ == -1) {
123 if (!SetupP2PSharingFd())
124 return;
125 }
126
127 // Check that the file is at least |file_offset| bytes long - if
128 // it's not something is wrong and we must immediately delete the
129 // file to avoid propagating this problem to other peers.
130 //
131 // How can this happen? It could be that we're resuming an update
132 // after a system crash... in this case, it could be that
133 //
134 // 1. the p2p file didn't get properly synced to stable storage; or
135 // 2. the file was deleted at bootup (it's in /var/cache after all); or
136 // 3. other reasons
Gabe Blacka77939e2014-09-09 23:35:08 -0700137 off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
138 if (p2p_size < 0) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700139 PLOG(ERROR) << "Error getting file status for p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700140 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700141 return;
142 }
Gabe Blacka77939e2014-09-09 23:35:08 -0700143 if (p2p_size < file_offset) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700144 LOG(ERROR) << "Wanting to write to file offset " << file_offset
Amin Hassani008c4582019-01-13 16:22:47 -0800145 << " but existing p2p file is only " << p2p_size << " bytes.";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700146 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700147 return;
148 }
149
150 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
151 if (cur_file_offset != static_cast<off_t>(file_offset)) {
Amin Hassani008c4582019-01-13 16:22:47 -0800152 PLOG(ERROR) << "Error seeking to position " << file_offset
153 << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700154 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700155 } else {
156 // OK, seeking worked, now write the data
157 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
158 if (bytes_written != static_cast<ssize_t>(length)) {
Amin Hassani008c4582019-01-13 16:22:47 -0800159 PLOG(ERROR) << "Error writing " << length << " bytes at file offset "
David Zeuthen8f191b22013-08-06 12:27:50 -0700160 << file_offset << " in p2p file";
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700161 CloseP2PSharingFd(true); // Delete p2p file.
David Zeuthen8f191b22013-08-06 12:27:50 -0700162 }
163 }
164}
165
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166void DownloadAction::PerformAction() {
167 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000168
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169 // Get the InstallPlan and read it
170 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700171 install_plan_ = GetInputObject();
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700172 install_plan_.Dump();
Sen Jiang5ae865b2017-04-18 14:24:40 -0700173
174 bytes_received_ = 0;
Aaron Wood9321f502017-09-07 11:18:54 -0700175 bytes_received_previous_payloads_ = 0;
Sen Jiang5ae865b2017-04-18 14:24:40 -0700176 bytes_total_ = 0;
177 for (const auto& payload : install_plan_.payloads)
178 bytes_total_ += payload.size;
179
180 if (install_plan_.is_resume) {
181 int64_t payload_index = 0;
182 if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
183 static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
184 // Save the index for the resume payload before downloading any previous
185 // payload, otherwise it will be overwritten.
186 resume_payload_index_ = payload_index;
187 for (int i = 0; i < payload_index; i++)
188 install_plan_.payloads[i].already_applied = true;
189 }
190 }
Sen Jiang0affc2c2017-02-10 15:55:05 -0800191 // TODO(senj): check that install plan has at least one payload.
192 if (!payload_)
193 payload_ = &install_plan_.payloads[0];
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000194
Alex Deymo5ed695e2015-10-05 16:59:23 -0700195 LOG(INFO) << "Marking new slot as unbootable";
Alex Deymo1b3556c2016-02-03 09:54:02 -0800196 if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
Alex Deymo5ed695e2015-10-05 16:59:23 -0700197 LOG(WARNING) << "Unable to mark new slot "
198 << BootControlInterface::SlotName(install_plan_.target_slot)
199 << ". Proceeding with the update anyway.";
200 }
201
Sen Jiang6c736682017-03-10 15:01:36 -0800202 StartDownloading();
203}
204
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400205bool DownloadAction::LoadCachedManifest(int64_t manifest_size) {
206 std::string cached_manifest_bytes;
207 if (!prefs_->GetString(kPrefsManifestBytes, &cached_manifest_bytes) ||
208 cached_manifest_bytes.size() <= 0) {
209 LOG(INFO) << "Cached Manifest data not found";
210 return false;
211 }
212 if (static_cast<int64_t>(cached_manifest_bytes.size()) != manifest_size) {
213 LOG(WARNING) << "Cached metadata has unexpected size: "
214 << cached_manifest_bytes.size() << " vs. " << manifest_size;
215 return false;
216 }
217
218 ErrorCode error;
219 const bool success =
220 delta_performer_->Write(
221 cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
222 delta_performer_->IsManifestValid();
223 if (success) {
224 LOG(INFO) << "Successfully parsed cached manifest";
225 } else {
226 // If parsing of cached data failed, fall back to fetch them using HTTP
227 LOG(WARNING) << "Cached manifest data fails to load, error code:"
228 << static_cast<int>(error) << "," << error;
229 }
230 return success;
231}
232
Sen Jiang6c736682017-03-10 15:01:36 -0800233void DownloadAction::StartDownloading() {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700234 download_active_ = true;
235 http_fetcher_->ClearRanges();
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400236
237 if (writer_ && writer_ != delta_performer_.get()) {
238 LOG(INFO) << "Using writer for test.";
239 } else {
240 delta_performer_.reset(new DeltaPerformer(prefs_,
241 boot_control_,
242 hardware_,
243 delegate_,
244 &install_plan_,
245 payload_,
246 interactive_));
247 writer_ = delta_performer_.get();
248 }
249
Sen Jiang5ae865b2017-04-18 14:24:40 -0700250 if (install_plan_.is_resume &&
251 payload_ == &install_plan_.payloads[resume_payload_index_]) {
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400252 // Resuming an update so parse the cached manifest first
Sen Jiang5ae865b2017-04-18 14:24:40 -0700253 int64_t manifest_metadata_size = 0;
254 int64_t manifest_signature_size = 0;
255 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
256 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400257
258 // TODO(zhangkelvin) Add unittest for success and fallback route
259 if (!LoadCachedManifest(manifest_metadata_size + manifest_signature_size)) {
260 if (delta_performer_) {
261 // Create a new DeltaPerformer to reset all its state
262 delta_performer_ = std::make_unique<DeltaPerformer>(prefs_,
263 boot_control_,
264 hardware_,
265 delegate_,
266 &install_plan_,
267 payload_,
268 interactive_);
269 writer_ = delta_performer_.get();
270 }
271 http_fetcher_->AddRange(base_offset_,
272 manifest_metadata_size + manifest_signature_size);
273 }
274
Sen Jiang5ae865b2017-04-18 14:24:40 -0700275 // If there're remaining unprocessed data blobs, fetch them. Be careful not
276 // to request data beyond the end of the payload to avoid 416 HTTP response
277 // error codes.
278 int64_t next_data_offset = 0;
279 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
280 uint64_t resume_offset =
281 manifest_metadata_size + manifest_signature_size + next_data_offset;
282 if (!payload_->size) {
283 http_fetcher_->AddRange(base_offset_ + resume_offset);
284 } else if (resume_offset < payload_->size) {
285 http_fetcher_->AddRange(base_offset_ + resume_offset,
286 payload_->size - resume_offset);
287 }
288 } else {
289 if (payload_->size) {
290 http_fetcher_->AddRange(base_offset_, payload_->size);
291 } else {
292 // If no payload size is passed we assume we read until the end of the
293 // stream.
294 http_fetcher_->AddRange(base_offset_);
295 }
296 }
297
Amin Hassani538bd592020-11-04 20:46:08 -0800298 if (SystemState::Get() != nullptr) {
299 const PayloadStateInterface* payload_state =
300 SystemState::Get()->payload_state();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800301 string file_id = utils::CalculateP2PFileId(payload_->hash, payload_->size);
Gilad Arnold74b5f552014-10-07 08:17:16 -0700302 if (payload_state->GetUsingP2PForSharing()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700303 // If we're sharing the update, store the file_id to convey
304 // that we should write to the file.
305 p2p_file_id_ = file_id;
306 LOG(INFO) << "p2p file id: " << p2p_file_id_;
307 } else {
308 // Even if we're not sharing the update, it could be that
309 // there's a partial file from a previous attempt with the same
310 // hash. If this is the case, we NEED to clean it up otherwise
311 // we're essentially timing out other peers downloading from us
312 // (since we're never going to complete the file).
Amin Hassani538bd592020-11-04 20:46:08 -0800313 FilePath path = SystemState::Get()->p2p_manager()->FileGetPath(file_id);
David Zeuthen8f191b22013-08-06 12:27:50 -0700314 if (!path.empty()) {
315 if (unlink(path.value().c_str()) != 0) {
316 PLOG(ERROR) << "Error deleting p2p file " << path.value();
317 } else {
318 LOG(INFO) << "Deleting partial p2p file " << path.value()
319 << " since we're not using p2p to share.";
320 }
321 }
322 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700323
Gilad Arnold74b5f552014-10-07 08:17:16 -0700324 // Tweak timeouts on the HTTP fetcher if we're downloading from a
325 // local peer.
326 if (payload_state->GetUsingP2PForDownloading() &&
327 payload_state->GetP2PUrl() == install_plan_.download_url) {
328 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
329 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
330 kDownloadP2PLowSpeedTimeSeconds);
331 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
332 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
333 }
David Zeuthen34135a92013-08-06 11:16:16 -0700334 }
335
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700336 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337}
338
Alex Deymof2858572016-02-25 11:20:13 -0800339void DownloadAction::SuspendAction() {
340 http_fetcher_->Pause();
341}
342
343void DownloadAction::ResumeAction() {
344 http_fetcher_->Unpause();
345}
346
rspangler@google.com49fdf182009-10-10 00:57:34 +0000347void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700348 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700349 writer_->Close();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700350 writer_ = nullptr;
Darin Petkov698d0412010-10-13 10:59:44 -0700351 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800352 download_active_ = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700353 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800354 // Terminates the transfer. The action is terminated, if necessary, when the
355 // TransferTerminated callback is received.
356 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000357}
358
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700359void DownloadAction::SeekToOffset(off_t offset) {
360 bytes_received_ = offset;
361}
362
Amin Hassani0cd9d772018-07-31 23:55:43 -0700363bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800364 const void* bytes,
365 size_t length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700366 // Note that bytes_received_ is the current offset.
367 if (!p2p_file_id_.empty()) {
368 WriteToP2PFile(bytes, length, bytes_received_);
369 }
370
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700371 bytes_received_ += length;
Aaron Wood9321f502017-09-07 11:18:54 -0700372 uint64_t bytes_downloaded_total =
373 bytes_received_previous_payloads_ + bytes_received_;
Alex Deymo542c19b2015-12-03 07:43:31 -0300374 if (delegate_ && download_active_) {
Aaron Wood9321f502017-09-07 11:18:54 -0700375 delegate_->BytesReceived(length, bytes_downloaded_total, bytes_total_);
Alex Deymo542c19b2015-12-03 07:43:31 -0300376 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700377 if (writer_ && !writer_->Write(bytes, length, &code_)) {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700378 if (code_ != ErrorCode::kSuccess) {
379 LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
380 << ") in DeltaPerformer's Write method when "
381 << "processing the received payload -- Terminating processing";
382 }
David Zeuthen69bc2732013-11-26 16:08:21 -0800383 // Delete p2p file, if applicable.
384 if (!p2p_file_id_.empty())
385 CloseP2PSharingFd(true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800386 // Don't tell the action processor that the action is complete until we get
387 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
388 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700389 TerminateProcessing();
Amin Hassani0cd9d772018-07-31 23:55:43 -0700390 return false;
Darin Petkov698d0412010-10-13 10:59:44 -0700391 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700392
393 // Call p2p_manager_->FileMakeVisible() when we've successfully
394 // verified the manifest!
Amin Hassani538bd592020-11-04 20:46:08 -0800395 if (!p2p_visible_ && SystemState::Get() && delta_performer_.get() &&
Alex Deymo1b3556c2016-02-03 09:54:02 -0800396 delta_performer_->IsManifestValid()) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700397 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
Amin Hassani538bd592020-11-04 20:46:08 -0800398 SystemState::Get()->p2p_manager()->FileMakeVisible(p2p_file_id_);
David Zeuthen8f191b22013-08-06 12:27:50 -0700399 p2p_visible_ = true;
400 }
Amin Hassani0cd9d772018-07-31 23:55:43 -0700401 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000402}
403
Alex Deymo60ca1a72015-06-18 18:19:15 -0700404void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000405 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700406 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
Aaron Wood9321f502017-09-07 11:18:54 -0700407 if (delta_performer_.get() == writer_) {
408 // no delta_performer_ in tests, so leave the test writer in place
409 writer_ = nullptr;
410 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000411 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800412 download_active_ = false;
David Zeuthena99981f2013-04-29 13:42:47 -0700413 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700414 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
Aaron Wood9321f502017-09-07 11:18:54 -0700415 if (code == ErrorCode::kSuccess) {
416 if (delta_performer_ && !payload_->already_applied)
Sen Jiang5ae865b2017-04-18 14:24:40 -0700417 code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
Lann Martin39f57142017-07-14 09:18:42 -0600418 if (code == ErrorCode::kSuccess) {
Sen Jiang18414082018-01-11 14:50:36 -0800419 if (payload_ < &install_plan_.payloads.back() &&
Amin Hassani538bd592020-11-04 20:46:08 -0800420 SystemState::Get()->payload_state()->NextPayload()) {
Sen Jiang18414082018-01-11 14:50:36 -0800421 LOG(INFO) << "Incrementing to next payload";
422 // No need to reset if this payload was already applied.
423 if (delta_performer_ && !payload_->already_applied)
424 DeltaPerformer::ResetUpdateProgress(prefs_, false);
425 // Start downloading next payload.
426 bytes_received_previous_payloads_ += payload_->size;
427 payload_++;
428 install_plan_.download_url =
Amin Hassani538bd592020-11-04 20:46:08 -0800429 SystemState::Get()->payload_state()->GetCurrentUrl();
Sen Jiang18414082018-01-11 14:50:36 -0800430 StartDownloading();
431 return;
432 }
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700433
434 // All payloads have been applied and verified.
435 if (delegate_)
436 delegate_->DownloadComplete();
437
Lann Martin39f57142017-07-14 09:18:42 -0600438 // Log UpdateEngine.DownloadAction.* histograms to help diagnose
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700439 // long-blocking operations.
Lann Martin39f57142017-07-14 09:18:42 -0600440 std::string histogram_output;
Amin Hassani008c4582019-01-13 16:22:47 -0800441 base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.",
442 &histogram_output);
Lann Martin39f57142017-07-14 09:18:42 -0600443 LOG(INFO) << histogram_output;
444 } else {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700445 LOG(ERROR) << "Download of " << install_plan_.download_url
446 << " failed due to payload verification error.";
David Zeuthen69bc2732013-11-26 16:08:21 -0800447 // Delete p2p file, if applicable.
448 if (!p2p_file_id_.empty())
449 CloseP2PSharingFd(true);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000450 }
451 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700452
Darin Petkovc97435c2010-07-20 12:37:43 -0700453 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700454 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800455 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700456 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000457}
458
Sen Jiang5ae865b2017-04-18 14:24:40 -0700459void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700460 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800461 processor_->ActionComplete(this, code_);
Sen Jiang5ae865b2017-04-18 14:24:40 -0700462 } else if (payload_->already_applied) {
463 LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
464 "payload has already applied, treating as TransferComplete.";
465 TransferComplete(fetcher, true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800466 }
467}
468
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700469} // namespace chromeos_update_engine