blob: 8a18f43ee2c355b1f676d4753388da9905bc6622 [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,
24 HttpFetcher* http_fetcher)
25 : prefs_(prefs),
26 writer_(NULL),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070027 http_fetcher_(http_fetcher),
Darin Petkov9ce452b2010-11-17 14:33:28 -080028 code_(kActionCodeSuccess),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070029 delegate_(NULL),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -070030 bytes_received_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
32DownloadAction::~DownloadAction() {}
33
34void DownloadAction::PerformAction() {
35 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037 // Get the InstallPlan and read it
38 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070039 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000041
Andrew de los Reyesf9185172010-05-03 11:07:05 -070042 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000043
Andrew de los Reyesf9185172010-05-03 11:07:05 -070044 if (writer_) {
45 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 } else {
Jay Srinivasan51dcf262012-09-13 17:24:32 -070047 delta_performer_.reset(new DeltaPerformer(prefs_, &install_plan_));
Darin Petkov7ed561b2011-10-04 02:59:03 -070048 writer_ = delta_performer_.get();
rspangler@google.com49fdf182009-10-10 00:57:34 +000049 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070050 int rc = writer_->Open(install_plan_.install_path.c_str(),
51 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
52 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070054 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 // report error to processor
Darin Petkovc97435c2010-07-20 12:37:43 -070056 processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 return;
58 }
Darin Petkov7ed561b2011-10-04 02:59:03 -070059 if (delta_performer_.get() &&
60 !delta_performer_->OpenKernel(
61 install_plan_.kernel_install_path.c_str())) {
62 LOG(ERROR) << "Unable to open kernel file "
63 << install_plan_.kernel_install_path.c_str();
64 writer_->Close();
65 processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
66 return;
Andrew de los Reyesf9185172010-05-03 11:07:05 -070067 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070068 if (delegate_) {
69 delegate_->SetDownloadStatus(true); // Set to active.
70 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070071 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000072}
73
74void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -070075 if (writer_) {
76 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
77 writer_ = NULL;
78 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070079 if (delegate_) {
80 delegate_->SetDownloadStatus(false); // Set to inactive.
81 }
Darin Petkov9ce452b2010-11-17 14:33:28 -080082 // Terminates the transfer. The action is terminated, if necessary, when the
83 // TransferTerminated callback is received.
84 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +000085}
86
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070087void DownloadAction::SeekToOffset(off_t offset) {
88 bytes_received_ = offset;
89}
90
rspangler@google.com49fdf182009-10-10 00:57:34 +000091void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
92 const char* bytes,
93 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070094 bytes_received_ += length;
95 if (delegate_)
Jay Srinivasan51dcf262012-09-13 17:24:32 -070096 delegate_->BytesReceived(bytes_received_, install_plan_.payload_size);
97 if (writer_ && !writer_->Write(bytes, length, &code_)) {
98 LOG(ERROR) << "Error " << code_ << " in DeltaPerformer's Write method when "
99 << "processing the received payload -- Terminating processing";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800100 // Don't tell the action processor that the action is complete until we get
101 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
102 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700103 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700104 return;
105 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106}
107
108void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
109 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700110 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 writer_ = NULL;
112 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700113 if (delegate_) {
114 delegate_->SetDownloadStatus(false); // Set to inactive.
115 }
Darin Petkovc97435c2010-07-20 12:37:43 -0700116 ActionExitCode code =
117 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
Darin Petkov7ed561b2011-10-04 02:59:03 -0700118 if (code == kActionCodeSuccess && delta_performer_.get()) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700119 code = delta_performer_->VerifyPayload(install_plan_.payload_hash,
120 install_plan_.payload_size);
Darin Petkov7ed561b2011-10-04 02:59:03 -0700121 if (code != kActionCodeSuccess) {
122 LOG(ERROR) << "Download of " << install_plan_.download_url
123 << " failed due to payload verification error.";
124 } else if (!delta_performer_->GetNewPartitionInfo(
125 &install_plan_.kernel_size,
126 &install_plan_.kernel_hash,
127 &install_plan_.rootfs_size,
128 &install_plan_.rootfs_hash)) {
129 LOG(ERROR) << "Unable to get new partition hash info.";
130 code = kActionCodeDownloadNewPartitionInfoError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131 }
132 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133
Darin Petkovc97435c2010-07-20 12:37:43 -0700134 // Write the path to the output pipe if we're successful.
135 if (code == kActionCodeSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800136 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700137 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138}
139
Darin Petkov9ce452b2010-11-17 14:33:28 -0800140void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
141 if (code_ != kActionCodeSuccess) {
142 processor_->ActionComplete(this, code_);
143 }
144}
145
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146}; // namespace {}