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