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" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <algorithm> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | #include "update_engine/action_pipe.h" |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 12 | #include "update_engine/subprocess.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 13 | |
| 14 | using std::min; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 15 | using std::string; |
| 16 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 20 | // Use a buffer to reduce the number of IOPS on SSD devices. |
| 21 | const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB |
| 22 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 23 | DownloadAction::DownloadAction(PrefsInterface* prefs, |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 24 | SystemState* system_state, |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 25 | HttpFetcher* http_fetcher) |
| 26 | : prefs_(prefs), |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 27 | system_state_(system_state), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 28 | http_fetcher_(http_fetcher), |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 29 | writer_(NULL), |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 30 | code_(kActionCodeSuccess), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 31 | delegate_(NULL), |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 32 | bytes_received_(0) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
| 34 | DownloadAction::~DownloadAction() {} |
| 35 | |
| 36 | void DownloadAction::PerformAction() { |
| 37 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 39 | // Get the InstallPlan and read it |
| 40 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 41 | install_plan_ = GetInputObject(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 42 | bytes_received_ = 0; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 43 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 44 | install_plan_.Dump(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 45 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 46 | if (writer_) { |
| 47 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | } else { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 49 | delta_performer_.reset(new DeltaPerformer(prefs_, |
| 50 | system_state_, |
| 51 | &install_plan_)); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 52 | writer_ = delta_performer_.get(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 54 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 55 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 56 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 58 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | // report error to processor |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 60 | processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 63 | 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(); |
| 69 | processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError); |
| 70 | return; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 71 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 72 | if (delegate_) { |
| 73 | delegate_->SetDownloadStatus(true); // Set to active. |
| 74 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 75 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void DownloadAction::TerminateProcessing() { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 79 | if (writer_) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame^] | 80 | writer_->Close(); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 81 | writer_ = NULL; |
| 82 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 83 | if (delegate_) { |
| 84 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 85 | } |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 86 | // Terminates the transfer. The action is terminated, if necessary, when the |
| 87 | // TransferTerminated callback is received. |
| 88 | http_fetcher_->TerminateTransfer(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 91 | void DownloadAction::SeekToOffset(off_t offset) { |
| 92 | bytes_received_ = offset; |
| 93 | } |
| 94 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 96 | const char* bytes, |
| 97 | int length) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 98 | bytes_received_ += length; |
| 99 | if (delegate_) |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 100 | 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 Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 104 | // 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 Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 107 | TerminateProcessing(); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 113 | if (writer_) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 114 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 115 | writer_ = NULL; |
| 116 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 117 | if (delegate_) { |
| 118 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 119 | } |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 120 | ActionExitCode code = |
| 121 | successful ? kActionCodeSuccess : kActionCodeDownloadTransferError; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 122 | if (code == kActionCodeSuccess && delta_performer_.get()) { |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 123 | code = delta_performer_->VerifyPayload(install_plan_.payload_hash, |
| 124 | install_plan_.payload_size); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 125 | if (code != kActionCodeSuccess) { |
| 126 | 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."; |
| 134 | code = kActionCodeDownloadNewPartitionInfoError; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 137 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 138 | // Write the path to the output pipe if we're successful. |
| 139 | if (code == kActionCodeSuccess && HasOutputPipe()) |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 140 | SetOutputObject(install_plan_); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 141 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 144 | void DownloadAction::TransferTerminated(HttpFetcher *fetcher) { |
| 145 | if (code_ != kActionCodeSuccess) { |
| 146 | processor_->ActionComplete(this, code_); |
| 147 | } |
| 148 | } |
| 149 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 150 | }; // namespace {} |