Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/download_action.h" |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 20 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 22 | #include <string> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | #include <base/files/file_path.h> |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 25 | #include <base/metrics/statistics_recorder.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 26 | #include <base/strings/stringprintf.h> |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/action_pipe.h" |
| 29 | #include "update_engine/common/boot_control_interface.h" |
Alex Deymo | ed9cc18 | 2016-06-15 16:19:29 -0700 | [diff] [blame] | 30 | #include "update_engine/common/error_code_utils.h" |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 31 | #include "update_engine/common/multi_range_http_fetcher.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/utils.h" |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 33 | #include "update_engine/omaha_request_params.h" |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 34 | #include "update_engine/p2p_manager.h" |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 35 | #include "update_engine/payload_state_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 36 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 37 | using base::FilePath; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 38 | using std::string; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 42 | DownloadAction::DownloadAction(PrefsInterface* prefs, |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 43 | BootControlInterface* boot_control, |
| 44 | HardwareInterface* hardware, |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 45 | SystemState* system_state, |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 46 | HttpFetcher* http_fetcher, |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 47 | bool interactive) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 48 | : prefs_(prefs), |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 49 | boot_control_(boot_control), |
| 50 | hardware_(hardware), |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 51 | system_state_(system_state), |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 52 | http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)), |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 53 | interactive_(interactive), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 54 | writer_(nullptr), |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 55 | code_(ErrorCode::kSuccess), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 56 | delegate_(nullptr), |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 57 | p2p_sharing_fd_(-1), |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 58 | p2p_visible_(true) { |
| 59 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | |
| 61 | DownloadAction::~DownloadAction() {} |
| 62 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 63 | void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) { |
| 64 | if (p2p_sharing_fd_ != -1) { |
| 65 | if (close(p2p_sharing_fd_) != 0) { |
| 66 | PLOG(ERROR) << "Error closing p2p sharing fd"; |
| 67 | } |
| 68 | p2p_sharing_fd_ = -1; |
| 69 | } |
| 70 | |
| 71 | if (delete_p2p_file) { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 72 | FilePath path = system_state_->p2p_manager()->FileGetPath(p2p_file_id_); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 73 | if (unlink(path.value().c_str()) != 0) { |
| 74 | PLOG(ERROR) << "Error deleting p2p file " << path.value(); |
| 75 | } else { |
| 76 | LOG(INFO) << "Deleted p2p file " << path.value(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Don't use p2p from this point onwards. |
| 81 | p2p_file_id_.clear(); |
| 82 | } |
| 83 | |
| 84 | bool DownloadAction::SetupP2PSharingFd() { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 85 | P2PManager* p2p_manager = system_state_->p2p_manager(); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 86 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 87 | if (!p2p_manager->FileShare(p2p_file_id_, payload_->size)) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 88 | LOG(ERROR) << "Unable to share file via p2p"; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 89 | CloseP2PSharingFd(true); // delete p2p file |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // File has already been created (and allocated, xattrs been |
| 94 | // populated etc.) by FileShare() so just open it for writing. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 95 | FilePath path = p2p_manager->FileGetPath(p2p_file_id_); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 96 | p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY); |
| 97 | if (p2p_sharing_fd_ == -1) { |
| 98 | PLOG(ERROR) << "Error opening file " << path.value(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 99 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Ensure file to share is world-readable, otherwise |
| 104 | // p2p-server and p2p-http-server can't access it. |
| 105 | // |
| 106 | // (Q: Why doesn't the file have mode 0644 already? A: Because |
| 107 | // the process-wide umask is set to 0700 in main.cc.) |
| 108 | if (fchmod(p2p_sharing_fd_, 0644) != 0) { |
| 109 | PLOG(ERROR) << "Error setting mode 0644 on " << path.value(); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 110 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // All good. |
| 115 | LOG(INFO) << "Writing payload contents to " << path.value(); |
| 116 | p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_); |
| 117 | return true; |
| 118 | } |
| 119 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 120 | void DownloadAction::WriteToP2PFile(const void* data, |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 121 | size_t length, |
| 122 | off_t file_offset) { |
| 123 | if (p2p_sharing_fd_ == -1) { |
| 124 | if (!SetupP2PSharingFd()) |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // Check that the file is at least |file_offset| bytes long - if |
| 129 | // it's not something is wrong and we must immediately delete the |
| 130 | // file to avoid propagating this problem to other peers. |
| 131 | // |
| 132 | // How can this happen? It could be that we're resuming an update |
| 133 | // after a system crash... in this case, it could be that |
| 134 | // |
| 135 | // 1. the p2p file didn't get properly synced to stable storage; or |
| 136 | // 2. the file was deleted at bootup (it's in /var/cache after all); or |
| 137 | // 3. other reasons |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 138 | off_t p2p_size = utils::FileSize(p2p_sharing_fd_); |
| 139 | if (p2p_size < 0) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 140 | PLOG(ERROR) << "Error getting file status for p2p file"; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 141 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 144 | if (p2p_size < file_offset) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 145 | LOG(ERROR) << "Wanting to write to file offset " << file_offset |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 146 | << " but existing p2p file is only " << p2p_size << " bytes."; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 147 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
| 151 | off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET); |
| 152 | if (cur_file_offset != static_cast<off_t>(file_offset)) { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 153 | PLOG(ERROR) << "Error seeking to position " << file_offset |
| 154 | << " in p2p file"; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 155 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 156 | } else { |
| 157 | // OK, seeking worked, now write the data |
| 158 | ssize_t bytes_written = write(p2p_sharing_fd_, data, length); |
| 159 | if (bytes_written != static_cast<ssize_t>(length)) { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 160 | PLOG(ERROR) << "Error writing " << length << " bytes at file offset " |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 161 | << file_offset << " in p2p file"; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 162 | CloseP2PSharingFd(true); // Delete p2p file. |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | void DownloadAction::PerformAction() { |
| 168 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 170 | // Get the InstallPlan and read it |
| 171 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 172 | install_plan_ = GetInputObject(); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 173 | install_plan_.Dump(); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 174 | |
| 175 | bytes_received_ = 0; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 176 | bytes_received_previous_payloads_ = 0; |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 177 | bytes_total_ = 0; |
| 178 | for (const auto& payload : install_plan_.payloads) |
| 179 | bytes_total_ += payload.size; |
| 180 | |
| 181 | if (install_plan_.is_resume) { |
| 182 | int64_t payload_index = 0; |
| 183 | if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) && |
| 184 | static_cast<size_t>(payload_index) < install_plan_.payloads.size()) { |
| 185 | // Save the index for the resume payload before downloading any previous |
| 186 | // payload, otherwise it will be overwritten. |
| 187 | resume_payload_index_ = payload_index; |
| 188 | for (int i = 0; i < payload_index; i++) |
| 189 | install_plan_.payloads[i].already_applied = true; |
| 190 | } |
| 191 | } |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 192 | // TODO(senj): check that install plan has at least one payload. |
| 193 | if (!payload_) |
| 194 | payload_ = &install_plan_.payloads[0]; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 195 | |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 196 | LOG(INFO) << "Marking new slot as unbootable"; |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 197 | if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) { |
Alex Deymo | 5ed695e | 2015-10-05 16:59:23 -0700 | [diff] [blame] | 198 | LOG(WARNING) << "Unable to mark new slot " |
| 199 | << BootControlInterface::SlotName(install_plan_.target_slot) |
| 200 | << ". Proceeding with the update anyway."; |
| 201 | } |
| 202 | |
Sen Jiang | 6c73668 | 2017-03-10 15:01:36 -0800 | [diff] [blame] | 203 | StartDownloading(); |
| 204 | } |
| 205 | |
| 206 | void DownloadAction::StartDownloading() { |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 207 | download_active_ = true; |
| 208 | http_fetcher_->ClearRanges(); |
| 209 | if (install_plan_.is_resume && |
| 210 | payload_ == &install_plan_.payloads[resume_payload_index_]) { |
| 211 | // Resuming an update so fetch the update manifest metadata first. |
| 212 | int64_t manifest_metadata_size = 0; |
| 213 | int64_t manifest_signature_size = 0; |
| 214 | prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size); |
| 215 | prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size); |
| 216 | http_fetcher_->AddRange(base_offset_, |
| 217 | manifest_metadata_size + manifest_signature_size); |
| 218 | // If there're remaining unprocessed data blobs, fetch them. Be careful not |
| 219 | // to request data beyond the end of the payload to avoid 416 HTTP response |
| 220 | // error codes. |
| 221 | int64_t next_data_offset = 0; |
| 222 | prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset); |
| 223 | uint64_t resume_offset = |
| 224 | manifest_metadata_size + manifest_signature_size + next_data_offset; |
| 225 | if (!payload_->size) { |
| 226 | http_fetcher_->AddRange(base_offset_ + resume_offset); |
| 227 | } else if (resume_offset < payload_->size) { |
| 228 | http_fetcher_->AddRange(base_offset_ + resume_offset, |
| 229 | payload_->size - resume_offset); |
| 230 | } |
| 231 | } else { |
| 232 | if (payload_->size) { |
| 233 | http_fetcher_->AddRange(base_offset_, payload_->size); |
| 234 | } else { |
| 235 | // If no payload size is passed we assume we read until the end of the |
| 236 | // stream. |
| 237 | http_fetcher_->AddRange(base_offset_); |
| 238 | } |
| 239 | } |
| 240 | |
Sen Jiang | 6c73668 | 2017-03-10 15:01:36 -0800 | [diff] [blame] | 241 | if (writer_ && writer_ != delta_performer_.get()) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 242 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 243 | } else { |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 244 | delta_performer_.reset(new DeltaPerformer(prefs_, |
| 245 | boot_control_, |
| 246 | hardware_, |
| 247 | delegate_, |
| 248 | &install_plan_, |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 249 | payload_, |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 250 | interactive_)); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 251 | writer_ = delta_performer_.get(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 252 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 253 | if (system_state_ != nullptr) { |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 254 | const PayloadStateInterface* payload_state = system_state_->payload_state(); |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 255 | string file_id = utils::CalculateP2PFileId(payload_->hash, payload_->size); |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 256 | if (payload_state->GetUsingP2PForSharing()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 257 | // If we're sharing the update, store the file_id to convey |
| 258 | // that we should write to the file. |
| 259 | p2p_file_id_ = file_id; |
| 260 | LOG(INFO) << "p2p file id: " << p2p_file_id_; |
| 261 | } else { |
| 262 | // Even if we're not sharing the update, it could be that |
| 263 | // there's a partial file from a previous attempt with the same |
| 264 | // hash. If this is the case, we NEED to clean it up otherwise |
| 265 | // we're essentially timing out other peers downloading from us |
| 266 | // (since we're never going to complete the file). |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 267 | FilePath path = system_state_->p2p_manager()->FileGetPath(file_id); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 268 | if (!path.empty()) { |
| 269 | if (unlink(path.value().c_str()) != 0) { |
| 270 | PLOG(ERROR) << "Error deleting p2p file " << path.value(); |
| 271 | } else { |
| 272 | LOG(INFO) << "Deleting partial p2p file " << path.value() |
| 273 | << " since we're not using p2p to share."; |
| 274 | } |
| 275 | } |
| 276 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 277 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 278 | // Tweak timeouts on the HTTP fetcher if we're downloading from a |
| 279 | // local peer. |
| 280 | if (payload_state->GetUsingP2PForDownloading() && |
| 281 | payload_state->GetP2PUrl() == install_plan_.download_url) { |
| 282 | LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p"; |
| 283 | http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps, |
| 284 | kDownloadP2PLowSpeedTimeSeconds); |
| 285 | http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount); |
| 286 | http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds); |
| 287 | } |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 290 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 293 | void DownloadAction::SuspendAction() { |
| 294 | http_fetcher_->Pause(); |
| 295 | } |
| 296 | |
| 297 | void DownloadAction::ResumeAction() { |
| 298 | http_fetcher_->Unpause(); |
| 299 | } |
| 300 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 301 | void DownloadAction::TerminateProcessing() { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 302 | if (writer_) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 303 | writer_->Close(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 304 | writer_ = nullptr; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 305 | } |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 306 | download_active_ = false; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 307 | CloseP2PSharingFd(false); // Keep p2p file. |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 308 | // Terminates the transfer. The action is terminated, if necessary, when the |
| 309 | // TransferTerminated callback is received. |
| 310 | http_fetcher_->TerminateTransfer(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 313 | void DownloadAction::SeekToOffset(off_t offset) { |
| 314 | bytes_received_ = offset; |
| 315 | } |
| 316 | |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 317 | bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 318 | const void* bytes, |
| 319 | size_t length) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 320 | // Note that bytes_received_ is the current offset. |
| 321 | if (!p2p_file_id_.empty()) { |
| 322 | WriteToP2PFile(bytes, length, bytes_received_); |
| 323 | } |
| 324 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 325 | bytes_received_ += length; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 326 | uint64_t bytes_downloaded_total = |
| 327 | bytes_received_previous_payloads_ + bytes_received_; |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 328 | if (delegate_ && download_active_) { |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 329 | delegate_->BytesReceived(length, bytes_downloaded_total, bytes_total_); |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 330 | } |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 331 | if (writer_ && !writer_->Write(bytes, length, &code_)) { |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 332 | if (code_ != ErrorCode::kSuccess) { |
| 333 | LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_ |
| 334 | << ") in DeltaPerformer's Write method when " |
| 335 | << "processing the received payload -- Terminating processing"; |
| 336 | } |
David Zeuthen | 69bc273 | 2013-11-26 16:08:21 -0800 | [diff] [blame] | 337 | // Delete p2p file, if applicable. |
| 338 | if (!p2p_file_id_.empty()) |
| 339 | CloseP2PSharingFd(true); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 340 | // Don't tell the action processor that the action is complete until we get |
| 341 | // the TransferTerminated callback. Otherwise, this and the HTTP fetcher |
| 342 | // objects may get destroyed before all callbacks are complete. |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 343 | TerminateProcessing(); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 344 | return false; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 345 | } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 346 | |
| 347 | // Call p2p_manager_->FileMakeVisible() when we've successfully |
| 348 | // verified the manifest! |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 349 | if (!p2p_visible_ && system_state_ && delta_performer_.get() && |
| 350 | delta_performer_->IsManifestValid()) { |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 351 | LOG(INFO) << "Manifest has been validated. Making p2p file visible."; |
| 352 | system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_); |
| 353 | p2p_visible_ = true; |
| 354 | } |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 355 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 358 | void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 359 | if (writer_) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 360 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 361 | if (delta_performer_.get() == writer_) { |
| 362 | // no delta_performer_ in tests, so leave the test writer in place |
| 363 | writer_ = nullptr; |
| 364 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 365 | } |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 366 | download_active_ = false; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 367 | ErrorCode code = |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 368 | successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 369 | if (code == ErrorCode::kSuccess) { |
| 370 | if (delta_performer_ && !payload_->already_applied) |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 371 | code = delta_performer_->VerifyPayload(payload_->hash, payload_->size); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 372 | if (code == ErrorCode::kSuccess) { |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 373 | if (payload_ < &install_plan_.payloads.back() && |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 374 | system_state_->payload_state()->NextPayload()) { |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 375 | LOG(INFO) << "Incrementing to next payload"; |
| 376 | // No need to reset if this payload was already applied. |
| 377 | if (delta_performer_ && !payload_->already_applied) |
| 378 | DeltaPerformer::ResetUpdateProgress(prefs_, false); |
| 379 | // Start downloading next payload. |
| 380 | bytes_received_previous_payloads_ += payload_->size; |
| 381 | payload_++; |
| 382 | install_plan_.download_url = |
| 383 | system_state_->payload_state()->GetCurrentUrl(); |
| 384 | StartDownloading(); |
| 385 | return; |
| 386 | } |
Sen Jiang | 7ebfccf | 2018-03-15 13:55:55 -0700 | [diff] [blame] | 387 | |
| 388 | // All payloads have been applied and verified. |
| 389 | if (delegate_) |
| 390 | delegate_->DownloadComplete(); |
| 391 | |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 392 | // Log UpdateEngine.DownloadAction.* histograms to help diagnose |
Sen Jiang | 7ebfccf | 2018-03-15 13:55:55 -0700 | [diff] [blame] | 393 | // long-blocking operations. |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 394 | std::string histogram_output; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 395 | base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.", |
| 396 | &histogram_output); |
Lann Martin | 39f5714 | 2017-07-14 09:18:42 -0600 | [diff] [blame] | 397 | LOG(INFO) << histogram_output; |
| 398 | } else { |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 399 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 400 | << " failed due to payload verification error."; |
David Zeuthen | 69bc273 | 2013-11-26 16:08:21 -0800 | [diff] [blame] | 401 | // Delete p2p file, if applicable. |
| 402 | if (!p2p_file_id_.empty()) |
| 403 | CloseP2PSharingFd(true); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 406 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 407 | // Write the path to the output pipe if we're successful. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 408 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 409 | SetOutputObject(install_plan_); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 410 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 413 | void DownloadAction::TransferTerminated(HttpFetcher* fetcher) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 414 | if (code_ != ErrorCode::kSuccess) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 415 | processor_->ActionComplete(this, code_); |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 416 | } else if (payload_->already_applied) { |
| 417 | LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current " |
| 418 | "payload has already applied, treating as TransferComplete."; |
| 419 | TransferComplete(fetcher, true); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 423 | } // namespace chromeos_update_engine |