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_)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 67 | writer_ = delta_performer_.get(); |
| 68 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 70 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 71 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 72 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | // report error to processor |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 76 | processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 79 | if (!install_plan_.is_full_update) { |
| 80 | if (!delta_performer_->OpenKernel( |
| 81 | install_plan_.kernel_install_path.c_str())) { |
| 82 | LOG(ERROR) << "Unable to open kernel file " |
| 83 | << install_plan_.kernel_install_path.c_str(); |
| 84 | writer_->Close(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 85 | processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 89 | if (delegate_) { |
| 90 | delegate_->SetDownloadStatus(true); // Set to active. |
| 91 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 92 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void DownloadAction::TerminateProcessing() { |
| 96 | CHECK(writer_); |
| 97 | CHECK_EQ(writer_->Close(), 0); |
| 98 | writer_ = NULL; |
| 99 | http_fetcher_->TerminateTransfer(); |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 100 | if (delegate_) { |
| 101 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 102 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 106 | const char* bytes, |
| 107 | int length) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 108 | bytes_received_ += length; |
| 109 | if (delegate_) |
| 110 | delegate_->BytesReceived(bytes_received_, install_plan_.size); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 111 | int rc = writer_->Write(bytes, length); |
| 112 | TEST_AND_RETURN(rc >= 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 113 | omaha_hash_calculator_.Update(bytes, length); |
| 114 | } |
| 115 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 116 | namespace { |
| 117 | void FlushLinuxCaches() { |
| 118 | vector<string> command; |
| 119 | command.push_back("/bin/sync"); |
| 120 | int rc; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 121 | LOG(INFO) << "FlushLinuxCaches-sync..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 122 | Subprocess::SynchronousExec(command, &rc); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 123 | LOG(INFO) << "FlushLinuxCaches-drop_caches..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 124 | |
| 125 | const char* const drop_cmd = "3\n"; |
| 126 | utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd)); |
| 127 | |
| 128 | LOG(INFO) << "FlushLinuxCaches done."; |
| 129 | } |
| 130 | } |
| 131 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 132 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 133 | if (writer_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 134 | CHECK_EQ(writer_->Close(), 0) << errno; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | writer_ = NULL; |
| 136 | } |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 137 | if (delegate_) { |
| 138 | delegate_->SetDownloadStatus(false); // Set to inactive. |
| 139 | } |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 140 | ActionExitCode code = |
| 141 | successful ? kActionCodeSuccess : kActionCodeDownloadTransferError; |
| 142 | if (code == kActionCodeSuccess) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 143 | if (!install_plan_.is_full_update) { |
| 144 | if (!delta_performer_->VerifyPayload("", |
| 145 | install_plan_.download_hash, |
| 146 | install_plan_.size)) { |
| 147 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 148 | << " failed due to payload verification error."; |
| 149 | code = kActionCodeDownloadPayloadVerificationError; |
| 150 | } |
| 151 | } else { |
| 152 | // Makes sure the hash and size are correct for an old-style full update. |
| 153 | omaha_hash_calculator_.Finalize(); |
| 154 | if (omaha_hash_calculator_.hash() != install_plan_.download_hash) { |
| 155 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 156 | << " failed. Expected hash " << install_plan_.download_hash |
| 157 | << " but got hash " << omaha_hash_calculator_.hash(); |
| 158 | code = kActionCodeDownloadHashMismatchError; |
| 159 | } else if (bytes_received_ != install_plan_.size) { |
| 160 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 161 | << " failed. Expected size " << install_plan_.size |
| 162 | << " but got size " << bytes_received_; |
| 163 | code = kActionCodeDownloadSizeMismatchError; |
| 164 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 167 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 168 | FlushLinuxCaches(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 170 | // Write the path to the output pipe if we're successful. |
| 171 | if (code == kActionCodeSuccess && HasOutputPipe()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 172 | SetOutputObject(GetInputObject()); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 173 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | }; // namespace {} |