blob: 89638c4c65d8df82623c32ede4a33afdf595fac8 [file] [log] [blame]
Darin Petkov85d02b72011-05-17 13:25:51 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rspangler@google.com49fdf182009-10-10 00:57:34 +00005#include "update_engine/download_action.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00006#include <errno.h>
7#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -07008#include <string>
9#include <vector>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000010#include <glib.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070011
12#include <base/file_path.h>
13#include <base/stringprintf.h>
14
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015#include "update_engine/action_pipe.h"
David Zeuthen8f191b22013-08-06 12:27:50 -070016#include "update_engine/p2p_manager.h"
Andrew de los Reyesf9714432010-05-04 10:21:23 -070017#include "update_engine/subprocess.h"
David Zeuthen34135a92013-08-06 11:16:16 -070018#include "update_engine/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019
20using std::min;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070021using std::string;
22using std::vector;
David Zeuthen8f191b22013-08-06 12:27:50 -070023using base::FilePath;
24using base::StringPrintf;
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
26namespace chromeos_update_engine {
27
Darin Petkove971f332010-09-22 16:57:25 -070028// Use a buffer to reduce the number of IOPS on SSD devices.
29const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB
30
Darin Petkov73058b42010-10-06 16:32:19 -070031DownloadAction::DownloadAction(PrefsInterface* prefs,
Jay Srinivasanf0572052012-10-23 18:12:56 -070032 SystemState* system_state,
Darin Petkov73058b42010-10-06 16:32:19 -070033 HttpFetcher* http_fetcher)
34 : prefs_(prefs),
Jay Srinivasanedce2832012-10-24 18:57:47 -070035 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070036 http_fetcher_(http_fetcher),
Jay Srinivasanedce2832012-10-24 18:57:47 -070037 writer_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -070038 code_(kErrorCodeSuccess),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039 delegate_(NULL),
David Zeuthen8f191b22013-08-06 12:27:50 -070040 bytes_received_(0),
41 p2p_sharing_fd_(-1),
42 p2p_visible_(true) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000043
44DownloadAction::~DownloadAction() {}
45
David Zeuthen8f191b22013-08-06 12:27:50 -070046void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
47 if (p2p_sharing_fd_ != -1) {
48 if (close(p2p_sharing_fd_) != 0) {
49 PLOG(ERROR) << "Error closing p2p sharing fd";
50 }
51 p2p_sharing_fd_ = -1;
52 }
53
54 if (delete_p2p_file) {
55 FilePath path = system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
56 if (unlink(path.value().c_str()) != 0) {
57 PLOG(ERROR) << "Error deleting p2p file " << path.value();
58 } else {
59 LOG(INFO) << "Deleted p2p file " << path.value();
60 }
61 }
62
63 // Don't use p2p from this point onwards.
64 p2p_file_id_.clear();
65}
66
67bool DownloadAction::SetupP2PSharingFd() {
68 P2PManager *p2p_manager = system_state_->p2p_manager();
69
70 if (!p2p_manager->FileShare(p2p_file_id_, install_plan_.payload_size)) {
71 LOG(ERROR) << "Unable to share file via p2p";
72 CloseP2PSharingFd(true); // delete p2p file
73 return false;
74 }
75
76 // File has already been created (and allocated, xattrs been
77 // populated etc.) by FileShare() so just open it for writing.
78 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
79 p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
80 if (p2p_sharing_fd_ == -1) {
81 PLOG(ERROR) << "Error opening file " << path.value();
82 CloseP2PSharingFd(true); // Delete p2p file.
83 return false;
84 }
85
86 // Ensure file to share is world-readable, otherwise
87 // p2p-server and p2p-http-server can't access it.
88 //
89 // (Q: Why doesn't the file have mode 0644 already? A: Because
90 // the process-wide umask is set to 0700 in main.cc.)
91 if (fchmod(p2p_sharing_fd_, 0644) != 0) {
92 PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
93 CloseP2PSharingFd(true); // Delete p2p file.
94 return false;
95 }
96
97 // All good.
98 LOG(INFO) << "Writing payload contents to " << path.value();
99 p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
100 return true;
101}
102
103void DownloadAction::WriteToP2PFile(const char *data,
104 size_t length,
105 off_t file_offset) {
106 if (p2p_sharing_fd_ == -1) {
107 if (!SetupP2PSharingFd())
108 return;
109 }
110
111 // Check that the file is at least |file_offset| bytes long - if
112 // it's not something is wrong and we must immediately delete the
113 // file to avoid propagating this problem to other peers.
114 //
115 // How can this happen? It could be that we're resuming an update
116 // after a system crash... in this case, it could be that
117 //
118 // 1. the p2p file didn't get properly synced to stable storage; or
119 // 2. the file was deleted at bootup (it's in /var/cache after all); or
120 // 3. other reasons
121 struct stat statbuf;
122 if (fstat(p2p_sharing_fd_, &statbuf) != 0) {
123 PLOG(ERROR) << "Error getting file status for p2p file";
124 CloseP2PSharingFd(true); // Delete p2p file.
125 return;
126 }
127 if (statbuf.st_size < file_offset) {
128 LOG(ERROR) << "Wanting to write to file offset " << file_offset
129 << " but existing p2p file is only " << statbuf.st_size
130 << " bytes.";
131 CloseP2PSharingFd(true); // Delete p2p file.
132 return;
133 }
134
135 off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
136 if (cur_file_offset != static_cast<off_t>(file_offset)) {
137 PLOG(ERROR) << "Error seeking to position "
138 << file_offset << " in p2p file";
139 CloseP2PSharingFd(true); // Delete p2p file.
140 } else {
141 // OK, seeking worked, now write the data
142 ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
143 if (bytes_written != static_cast<ssize_t>(length)) {
144 PLOG(ERROR) << "Error writing "
145 << length << " bytes at file offset "
146 << file_offset << " in p2p file";
147 CloseP2PSharingFd(true); // Delete p2p file.
148 }
149 }
150}
151
rspangler@google.com49fdf182009-10-10 00:57:34 +0000152void DownloadAction::PerformAction() {
153 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155 // Get the InstallPlan and read it
156 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700157 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700158 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000159
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700160 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000161
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700162 if (writer_) {
163 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000164 } else {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700165 delta_performer_.reset(new DeltaPerformer(prefs_,
166 system_state_,
167 &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700168 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700170 int rc = writer_->Open(install_plan_.install_path.c_str(),
171 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
172 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000173 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700174 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000175 // report error to processor
David Zeuthena99981f2013-04-29 13:42:47 -0700176 processor_->ActionComplete(this, kErrorCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000177 return;
178 }
Darin Petkov7ed561b2011-10-04 02:59:03 -0700179 if (delta_performer_.get() &&
180 !delta_performer_->OpenKernel(
181 install_plan_.kernel_install_path.c_str())) {
182 LOG(ERROR) << "Unable to open kernel file "
183 << install_plan_.kernel_install_path.c_str();
184 writer_->Close();
David Zeuthena99981f2013-04-29 13:42:47 -0700185 processor_->ActionComplete(this, kErrorCodeKernelDeviceOpenError);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700186 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700187 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700188 if (delegate_) {
189 delegate_->SetDownloadStatus(true); // Set to active.
190 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700191
192 if (system_state_ != NULL) {
193 string file_id = utils::CalculateP2PFileId(install_plan_.payload_hash,
194 install_plan_.payload_size);
195 if (system_state_->request_params()->use_p2p_for_sharing()) {
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).
206 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
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 }
216 }
217
David Zeuthen34135a92013-08-06 11:16:16 -0700218 // Tweak timeouts on the HTTP fetcher if we're downloading from a
219 // local peer.
220 if (system_state_ != NULL &&
221 system_state_->request_params()->use_p2p_for_downloading() &&
222 system_state_->request_params()->p2p_url() ==
223 install_plan_.download_url) {
224 LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
225 http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
226 kDownloadP2PLowSpeedTimeSeconds);
227 http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
228 http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
229 }
230
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700231 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232}
233
234void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700235 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700236 writer_->Close();
Darin Petkov698d0412010-10-13 10:59:44 -0700237 writer_ = NULL;
238 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700239 if (delegate_) {
240 delegate_->SetDownloadStatus(false); // Set to inactive.
241 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700242 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800243 // Terminates the transfer. The action is terminated, if necessary, when the
244 // TransferTerminated callback is received.
245 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246}
247
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700248void DownloadAction::SeekToOffset(off_t offset) {
249 bytes_received_ = offset;
250}
251
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
253 const char* bytes,
254 int length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700255 // Note that bytes_received_ is the current offset.
256 if (!p2p_file_id_.empty()) {
257 WriteToP2PFile(bytes, length, bytes_received_);
258 }
259
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700260 bytes_received_ += length;
261 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700262 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
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";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800266 // Don't tell the action processor that the action is complete until we get
267 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
268 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700269 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700270 return;
271 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700272
273 // Call p2p_manager_->FileMakeVisible() when we've successfully
274 // verified the manifest!
275 if (!p2p_visible_ &&
276 delta_performer_.get() && delta_performer_->IsManifestValid()) {
277 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
278 system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
279 p2p_visible_ = true;
280 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000281}
282
283void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
284 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700285 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 writer_ = NULL;
287 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700288 if (delegate_) {
289 delegate_->SetDownloadStatus(false); // Set to inactive.
290 }
David Zeuthena99981f2013-04-29 13:42:47 -0700291 ErrorCode code =
292 successful ? kErrorCodeSuccess : kErrorCodeDownloadTransferError;
293 if (code == kErrorCodeSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700294 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
295 install_plan_.payload_size);
David Zeuthena99981f2013-04-29 13:42:47 -0700296 if (code != kErrorCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700297 LOG(ERROR) << "Download of " << install_plan_.download_url
298 << " failed due to payload verification error.";
299 } else if (!delta_performer_->GetNewPartitionInfo(
300 &install_plan_.kernel_size,
301 &install_plan_.kernel_hash,
302 &install_plan_.rootfs_size,
303 &install_plan_.rootfs_hash)) {
304 LOG(ERROR) << "Unable to get new partition hash info.";
David Zeuthena99981f2013-04-29 13:42:47 -0700305 code = kErrorCodeDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000306 }
307 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700308
Darin Petkovc97435c2010-07-20 12:37:43 -0700309 // Write the path to the output pipe if we're successful.
David Zeuthena99981f2013-04-29 13:42:47 -0700310 if (code == kErrorCodeSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800311 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700312 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000313}
314
Darin Petkov9ce452b2010-11-17 14:33:28 -0800315void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
David Zeuthena99981f2013-04-29 13:42:47 -0700316 if (code_ != kErrorCodeSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800317 processor_->ActionComplete(this, code_);
318 }
319}
320
rspangler@google.com49fdf182009-10-10 00:57:34 +0000321}; // namespace {}