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