blob: da6b1fc9fa92542e2524ec0217d4bbf0c7a051ea [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>
11#include "update_engine/action_pipe.h"
Andrew de los Reyesf9714432010-05-04 10:21:23 -070012#include "update_engine/subprocess.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013
14using std::min;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070015using std::string;
16using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
18namespace chromeos_update_engine {
19
Darin Petkove971f332010-09-22 16:57:25 -070020// Use a buffer to reduce the number of IOPS on SSD devices.
21const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB
22
Darin Petkov73058b42010-10-06 16:32:19 -070023DownloadAction::DownloadAction(PrefsInterface* prefs,
Jay Srinivasanf0572052012-10-23 18:12:56 -070024 SystemState* system_state,
Darin Petkov73058b42010-10-06 16:32:19 -070025 HttpFetcher* http_fetcher)
26 : prefs_(prefs),
Jay Srinivasanedce2832012-10-24 18:57:47 -070027 system_state_(system_state),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070028 http_fetcher_(http_fetcher),
Jay Srinivasanedce2832012-10-24 18:57:47 -070029 writer_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -070030 code_(kErrorCodeSuccess),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070031 delegate_(NULL),
Jay Srinivasanedce2832012-10-24 18:57:47 -070032 bytes_received_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
34DownloadAction::~DownloadAction() {}
35
36void DownloadAction::PerformAction() {
37 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
adlr@google.comc98a7ed2009-12-04 18:54:03 +000039 // Get the InstallPlan and read it
40 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070041 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070042 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000043
Andrew de los Reyesf9185172010-05-03 11:07:05 -070044 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000045
Andrew de los Reyesf9185172010-05-03 11:07:05 -070046 if (writer_) {
47 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 } else {
Jay Srinivasanf0572052012-10-23 18:12:56 -070049 delta_performer_.reset(new DeltaPerformer(prefs_,
50 system_state_,
51 &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -070052 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070054 int rc = writer_->Open(install_plan_.install_path.c_str(),
55 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
56 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070058 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 // report error to processor
David Zeuthena99981f2013-04-29 13:42:47 -070060 processor_->ActionComplete(this, kErrorCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 return;
62 }
Darin Petkov7ed561b2011-10-04 02:59:03 -070063 if (delta_performer_.get() &&
64 !delta_performer_->OpenKernel(
65 install_plan_.kernel_install_path.c_str())) {
66 LOG(ERROR) << "Unable to open kernel file "
67 << install_plan_.kernel_install_path.c_str();
68 writer_->Close();
David Zeuthena99981f2013-04-29 13:42:47 -070069 processor_->ActionComplete(this, kErrorCodeKernelDeviceOpenError);
Darin Petkov7ed561b2011-10-04 02:59:03 -070070 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -070071 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070072 if (delegate_) {
73 delegate_->SetDownloadStatus(true); // Set to active.
74 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070075 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000076}
77
78void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -070079 if (writer_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070080 writer_->Close();
Darin Petkov698d0412010-10-13 10:59:44 -070081 writer_ = NULL;
82 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070083 if (delegate_) {
84 delegate_->SetDownloadStatus(false); // Set to inactive.
85 }
Darin Petkov9ce452b2010-11-17 14:33:28 -080086 // Terminates the transfer. The action is terminated, if necessary, when the
87 // TransferTerminated callback is received.
88 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +000089}
90
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070091void DownloadAction::SeekToOffset(off_t offset) {
92 bytes_received_ = offset;
93}
94
rspangler@google.com49fdf182009-10-10 00:57:34 +000095void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
96 const char* bytes,
97 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070098 bytes_received_ += length;
99 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700100 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
101 if (writer_ && !writer_->Write(bytes, length, &code_)) {
102 LOG(ERROR) << "Error " << code_ << " in DeltaPerformer's Write method when "
103 << "processing the received payload -- Terminating processing";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800104 // Don't tell the action processor that the action is complete until we get
105 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
106 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700107 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700108 return;
109 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110}
111
112void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
113 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700114 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 writer_ = NULL;
116 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700117 if (delegate_) {
118 delegate_->SetDownloadStatus(false); // Set to inactive.
119 }
David Zeuthena99981f2013-04-29 13:42:47 -0700120 ErrorCode code =
121 successful ? kErrorCodeSuccess : kErrorCodeDownloadTransferError;
122 if (code == kErrorCodeSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700123 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
124 install_plan_.payload_size);
David Zeuthena99981f2013-04-29 13:42:47 -0700125 if (code != kErrorCodeSuccess) {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700126 LOG(ERROR) << "Download of " << install_plan_.download_url
127 << " failed due to payload verification error.";
128 } else if (!delta_performer_->GetNewPartitionInfo(
129 &install_plan_.kernel_size,
130 &install_plan_.kernel_hash,
131 &install_plan_.rootfs_size,
132 &install_plan_.rootfs_hash)) {
133 LOG(ERROR) << "Unable to get new partition hash info.";
David Zeuthena99981f2013-04-29 13:42:47 -0700134 code = kErrorCodeDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135 }
136 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700137
Darin Petkovc97435c2010-07-20 12:37:43 -0700138 // Write the path to the output pipe if we're successful.
David Zeuthena99981f2013-04-29 13:42:47 -0700139 if (code == kErrorCodeSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800140 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700141 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142}
143
Darin Petkov9ce452b2010-11-17 14:33:28 -0800144void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
David Zeuthena99981f2013-04-29 13:42:47 -0700145 if (code_ != kErrorCodeSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800146 processor_->ActionComplete(this, code_);
147 }
148}
149
rspangler@google.com49fdf182009-10-10 00:57:34 +0000150}; // namespace {}