blob: 17d45383cb3b619824bad4f37688398c864f0dc2 [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"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000018
19using std::min;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070020using std::string;
21using std::vector;
David Zeuthen8f191b22013-08-06 12:27:50 -070022using base::FilePath;
23using base::StringPrintf;
rspangler@google.com49fdf182009-10-10 00:57:34 +000024
25namespace chromeos_update_engine {
26
Darin Petkove971f332010-09-22 16:57:25 -070027// Use a buffer to reduce the number of IOPS on SSD devices.
28const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB
29
Darin Petkov73058b42010-10-06 16:32:19 -070030DownloadAction::DownloadAction(PrefsInterface* prefs,
Jay Srinivasanf0572052012-10-23 18:12:56 -070031 SystemState* system_state,
Darin Petkov73058b42010-10-06 16:32:19 -070032 HttpFetcher* http_fetcher)
33 : prefs_(prefs),
Jay Srinivasanedce2832012-10-24 18:57:47 -070034 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070035 http_fetcher_(http_fetcher),
Jay Srinivasanedce2832012-10-24 18:57:47 -070036 writer_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -070037 code_(kErrorCodeSuccess),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070038 delegate_(NULL),
David Zeuthen8f191b22013-08-06 12:27:50 -070039 bytes_received_(0),
40 p2p_sharing_fd_(-1),
41 p2p_visible_(true) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000042
43DownloadAction::~DownloadAction() {}
44
David Zeuthen8f191b22013-08-06 12:27:50 -070045void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
46 if (p2p_sharing_fd_ != -1) {
47 if (close(p2p_sharing_fd_) != 0) {
48 PLOG(ERROR) << "Error closing p2p sharing fd";
49 }
50 p2p_sharing_fd_ = -1;
51 }
52
53 if (delete_p2p_file) {
54 FilePath path = system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
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
66bool 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";
71 CloseP2PSharingFd(true); // delete p2p file
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.
77 FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
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();
81 CloseP2PSharingFd(true); // Delete p2p file.
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();
92 CloseP2PSharingFd(true); // Delete p2p file.
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
102void DownloadAction::WriteToP2PFile(const char *data,
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
120 struct stat statbuf;
121 if (fstat(p2p_sharing_fd_, &statbuf) != 0) {
122 PLOG(ERROR) << "Error getting file status for p2p file";
123 CloseP2PSharingFd(true); // Delete p2p file.
124 return;
125 }
126 if (statbuf.st_size < file_offset) {
127 LOG(ERROR) << "Wanting to write to file offset " << file_offset
128 << " but existing p2p file is only " << statbuf.st_size
129 << " bytes.";
130 CloseP2PSharingFd(true); // Delete p2p file.
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";
138 CloseP2PSharingFd(true); // Delete p2p file.
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";
146 CloseP2PSharingFd(true); // Delete p2p file.
147 }
148 }
149}
150
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151void DownloadAction::PerformAction() {
152 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000153
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000154 // Get the InstallPlan and read it
155 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700156 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700157 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000158
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700159 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000160
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700161 if (writer_) {
162 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000163 } else {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700164 delta_performer_.reset(new DeltaPerformer(prefs_,
165 system_state_,
166 &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -0700167 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000168 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700169 int rc = writer_->Open(install_plan_.install_path.c_str(),
170 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
171 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000172 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700173 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000174 // report error to processor
David Zeuthena99981f2013-04-29 13:42:47 -0700175 processor_->ActionComplete(this, kErrorCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176 return;
177 }
Darin Petkov7ed561b2011-10-04 02:59:03 -0700178 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();
David Zeuthena99981f2013-04-29 13:42:47 -0700184 processor_->ActionComplete(this, kErrorCodeKernelDeviceOpenError);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700185 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700186 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700187 if (delegate_) {
188 delegate_->SetDownloadStatus(true); // Set to active.
189 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700190
191 if (system_state_ != NULL) {
192 string file_id = utils::CalculateP2PFileId(install_plan_.payload_hash,
193 install_plan_.payload_size);
194 if (system_state_->request_params()->use_p2p_for_sharing()) {
195 // If we're sharing the update, store the file_id to convey
196 // that we should write to the file.
197 p2p_file_id_ = file_id;
198 LOG(INFO) << "p2p file id: " << p2p_file_id_;
199 } else {
200 // Even if we're not sharing the update, it could be that
201 // there's a partial file from a previous attempt with the same
202 // hash. If this is the case, we NEED to clean it up otherwise
203 // we're essentially timing out other peers downloading from us
204 // (since we're never going to complete the file).
205 FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
206 if (!path.empty()) {
207 if (unlink(path.value().c_str()) != 0) {
208 PLOG(ERROR) << "Error deleting p2p file " << path.value();
209 } else {
210 LOG(INFO) << "Deleting partial p2p file " << path.value()
211 << " since we're not using p2p to share.";
212 }
213 }
214 }
215 }
216
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700217 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218}
219
220void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700221 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700222 writer_->Close();
Darin Petkov698d0412010-10-13 10:59:44 -0700223 writer_ = NULL;
224 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700225 if (delegate_) {
226 delegate_->SetDownloadStatus(false); // Set to inactive.
227 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700228 CloseP2PSharingFd(false); // Keep p2p file.
Darin Petkov9ce452b2010-11-17 14:33:28 -0800229 // Terminates the transfer. The action is terminated, if necessary, when the
230 // TransferTerminated callback is received.
231 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232}
233
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700234void DownloadAction::SeekToOffset(off_t offset) {
235 bytes_received_ = offset;
236}
237
rspangler@google.com49fdf182009-10-10 00:57:34 +0000238void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
239 const char* bytes,
240 int length) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700241 // Note that bytes_received_ is the current offset.
242 if (!p2p_file_id_.empty()) {
243 WriteToP2PFile(bytes, length, bytes_received_);
244 }
245
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700246 bytes_received_ += length;
247 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700248 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
249 if (writer_ && !writer_->Write(bytes, length, &code_)) {
250 LOG(ERROR) << "Error " << code_ << " in DeltaPerformer's Write method when "
251 << "processing the received payload -- Terminating processing";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800252 // Don't tell the action processor that the action is complete until we get
253 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
254 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700255 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700256 return;
257 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700258
259 // Call p2p_manager_->FileMakeVisible() when we've successfully
260 // verified the manifest!
261 if (!p2p_visible_ &&
262 delta_performer_.get() && delta_performer_->IsManifestValid()) {
263 LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
264 system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
265 p2p_visible_ = true;
266 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267}
268
269void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
270 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700271 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000272 writer_ = NULL;
273 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700274 if (delegate_) {
275 delegate_->SetDownloadStatus(false); // Set to inactive.
276 }
David Zeuthena99981f2013-04-29 13:42:47 -0700277 ErrorCode code =
278 successful ? kErrorCodeSuccess : kErrorCodeDownloadTransferError;
279 if (code == kErrorCodeSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700280 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
281 install_plan_.payload_size);
David Zeuthena99981f2013-04-29 13:42:47 -0700282 if (code != kErrorCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700283 LOG(ERROR) << "Download of " << install_plan_.download_url
284 << " failed due to payload verification error.";
285 } else if (!delta_performer_->GetNewPartitionInfo(
286 &install_plan_.kernel_size,
287 &install_plan_.kernel_hash,
288 &install_plan_.rootfs_size,
289 &install_plan_.rootfs_hash)) {
290 LOG(ERROR) << "Unable to get new partition hash info.";
David Zeuthena99981f2013-04-29 13:42:47 -0700291 code = kErrorCodeDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000292 }
293 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700294
Darin Petkovc97435c2010-07-20 12:37:43 -0700295 // Write the path to the output pipe if we're successful.
David Zeuthena99981f2013-04-29 13:42:47 -0700296 if (code == kErrorCodeSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800297 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700298 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000299}
300
Darin Petkov9ce452b2010-11-17 14:33:28 -0800301void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
David Zeuthena99981f2013-04-29 13:42:47 -0700302 if (code_ != kErrorCodeSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800303 processor_->ActionComplete(this, code_);
304 }
305}
306
rspangler@google.com49fdf182009-10-10 00:57:34 +0000307}; // namespace {}