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