Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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, |
| 24 | HttpFetcher* http_fetcher) |
| 25 | : prefs_(prefs), |
| 26 | writer_(NULL), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 27 | http_fetcher_(http_fetcher), |
| 28 | delegate_(NULL), |
| 29 | bytes_received_(0) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | |
| 31 | DownloadAction::~DownloadAction() {} |
| 32 | |
| 33 | void DownloadAction::PerformAction() { |
| 34 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 36 | // Get the InstallPlan and read it |
| 37 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 38 | install_plan_ = GetInputObject(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 39 | bytes_received_ = 0; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 40 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 41 | install_plan_.Dump(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 42 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 43 | if (writer_) { |
| 44 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 45 | } else { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 46 | if (install_plan_.is_full_update) { |
| 47 | kernel_file_writer_.reset(new DirectFileWriter); |
| 48 | rootfs_file_writer_.reset(new DirectFileWriter); |
Darin Petkov | e971f33 | 2010-09-22 16:57:25 -0700 | [diff] [blame] | 49 | kernel_buffered_file_writer_.reset( |
| 50 | new BufferedFileWriter(kernel_file_writer_.get(), |
| 51 | kFileWriterBufferSize)); |
| 52 | rootfs_buffered_file_writer_.reset( |
| 53 | new BufferedFileWriter(rootfs_file_writer_.get(), |
| 54 | kFileWriterBufferSize)); |
| 55 | split_file_writer_.reset( |
| 56 | new SplitFileWriter(kernel_buffered_file_writer_.get(), |
| 57 | rootfs_buffered_file_writer_.get())); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 58 | split_file_writer_->SetFirstOpenArgs( |
| 59 | install_plan_.kernel_install_path.c_str(), |
| 60 | O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, |
| 61 | 0644); |
| 62 | decompressing_file_writer_.reset( |
| 63 | new GzipDecompressingFileWriter(split_file_writer_.get())); |
| 64 | writer_ = decompressing_file_writer_.get(); |
| 65 | } else { |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 66 | delta_performer_.reset(new DeltaPerformer(prefs_)); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 67 | delta_performer_->set_current_kernel_hash( |
| 68 | &install_plan_.current_kernel_hash); |
| 69 | delta_performer_->set_current_rootfs_hash( |
| 70 | &install_plan_.current_rootfs_hash); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 71 | writer_ = delta_performer_.get(); |
| 72 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 74 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 75 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 76 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 78 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | // report error to processor |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 80 | processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 83 | if (!install_plan_.is_full_update) { |
| 84 | if (!delta_performer_->OpenKernel( |
| 85 | install_plan_.kernel_install_path.c_str())) { |
| 86 | LOG(ERROR) << "Unable to open kernel file " |
| 87 | << install_plan_.kernel_install_path.c_str(); |
| 88 | writer_->Close(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 89 | processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 93 | if (delegate_) { |
| 94 | delegate_->SetDownloadStatus(true); // Set to active. |
| 95 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 96 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void DownloadAction::TerminateProcessing() { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 100 | if (writer_) { |
| 101 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
| 102 | writer_ = NULL; |
| 103 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | http_fetcher_->TerminateTransfer(); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 105 | if (delegate_) { |
| 106 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 107 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame^] | 110 | void DownloadAction::SeekToOffset(off_t offset) { |
| 111 | bytes_received_ = offset; |
| 112 | } |
| 113 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 115 | const char* bytes, |
| 116 | int length) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 117 | bytes_received_ += length; |
| 118 | if (delegate_) |
| 119 | delegate_->BytesReceived(bytes_received_, install_plan_.size); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 120 | if (writer_ && writer_->Write(bytes, length) < 0) { |
| 121 | LOG(ERROR) << "Write error -- terminating processing."; |
| 122 | TerminateProcessing(); |
| 123 | processor_->ActionComplete(this, kActionCodeDownloadWriteError); |
| 124 | return; |
| 125 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 126 | omaha_hash_calculator_.Update(bytes, length); |
| 127 | } |
| 128 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 129 | namespace { |
| 130 | void FlushLinuxCaches() { |
| 131 | vector<string> command; |
| 132 | command.push_back("/bin/sync"); |
| 133 | int rc; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 134 | LOG(INFO) << "FlushLinuxCaches-sync..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 135 | Subprocess::SynchronousExec(command, &rc); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 136 | LOG(INFO) << "FlushLinuxCaches-drop_caches..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 137 | |
| 138 | const char* const drop_cmd = "3\n"; |
| 139 | utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd)); |
| 140 | |
| 141 | LOG(INFO) << "FlushLinuxCaches done."; |
| 142 | } |
| 143 | } |
| 144 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 145 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 146 | if (writer_) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 147 | LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 148 | writer_ = NULL; |
| 149 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 150 | if (delegate_) { |
| 151 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 152 | } |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 153 | ActionExitCode code = |
| 154 | successful ? kActionCodeSuccess : kActionCodeDownloadTransferError; |
| 155 | if (code == kActionCodeSuccess) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 156 | if (!install_plan_.is_full_update) { |
| 157 | if (!delta_performer_->VerifyPayload("", |
| 158 | install_plan_.download_hash, |
| 159 | install_plan_.size)) { |
| 160 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 161 | << " failed due to payload verification error."; |
| 162 | code = kActionCodeDownloadPayloadVerificationError; |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 163 | } else if (!delta_performer_->VerifyAppliedUpdate( |
| 164 | install_plan_.install_path, install_plan_.kernel_install_path)) { |
| 165 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 166 | << " failed due to applied update verification error."; |
| 167 | code = kActionCodeDownloadAppliedUpdateVerificationError; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 168 | } |
| 169 | } else { |
| 170 | // Makes sure the hash and size are correct for an old-style full update. |
| 171 | omaha_hash_calculator_.Finalize(); |
| 172 | if (omaha_hash_calculator_.hash() != install_plan_.download_hash) { |
| 173 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 174 | << " failed. Expected hash " << install_plan_.download_hash |
| 175 | << " but got hash " << omaha_hash_calculator_.hash(); |
| 176 | code = kActionCodeDownloadHashMismatchError; |
| 177 | } else if (bytes_received_ != install_plan_.size) { |
| 178 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 179 | << " failed. Expected size " << install_plan_.size |
| 180 | << " but got size " << bytes_received_; |
| 181 | code = kActionCodeDownloadSizeMismatchError; |
| 182 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 185 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 186 | FlushLinuxCaches(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 187 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 188 | // Write the path to the output pipe if we're successful. |
| 189 | if (code == kActionCodeSuccess && HasOutputPipe()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 190 | SetOutputObject(GetInputObject()); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 191 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | }; // namespace {} |