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