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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 20 | DownloadAction::DownloadAction(HttpFetcher* http_fetcher) |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 21 | : writer_(NULL), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 22 | http_fetcher_(http_fetcher), |
| 23 | delegate_(NULL), |
| 24 | bytes_received_(0) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | |
| 26 | DownloadAction::~DownloadAction() {} |
| 27 | |
| 28 | void DownloadAction::PerformAction() { |
| 29 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 31 | // Get the InstallPlan and read it |
| 32 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 33 | install_plan_ = GetInputObject(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 34 | bytes_received_ = 0; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 35 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 36 | install_plan_.Dump(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 37 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 38 | if (writer_) { |
| 39 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 40 | } else { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 41 | if (install_plan_.is_full_update) { |
| 42 | kernel_file_writer_.reset(new DirectFileWriter); |
| 43 | rootfs_file_writer_.reset(new DirectFileWriter); |
| 44 | split_file_writer_.reset(new SplitFileWriter(kernel_file_writer_.get(), |
| 45 | rootfs_file_writer_.get())); |
| 46 | split_file_writer_->SetFirstOpenArgs( |
| 47 | install_plan_.kernel_install_path.c_str(), |
| 48 | O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, |
| 49 | 0644); |
| 50 | decompressing_file_writer_.reset( |
| 51 | new GzipDecompressingFileWriter(split_file_writer_.get())); |
| 52 | writer_ = decompressing_file_writer_.get(); |
| 53 | } else { |
| 54 | delta_performer_.reset(new DeltaPerformer); |
| 55 | writer_ = delta_performer_.get(); |
| 56 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 58 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 59 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 60 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 62 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 63 | // report error to processor |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 64 | processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 67 | if (!install_plan_.is_full_update) { |
| 68 | if (!delta_performer_->OpenKernel( |
| 69 | install_plan_.kernel_install_path.c_str())) { |
| 70 | LOG(ERROR) << "Unable to open kernel file " |
| 71 | << install_plan_.kernel_install_path.c_str(); |
| 72 | writer_->Close(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 73 | processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | } |
| 77 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void DownloadAction::TerminateProcessing() { |
| 81 | CHECK(writer_); |
| 82 | CHECK_EQ(writer_->Close(), 0); |
| 83 | writer_ = NULL; |
| 84 | http_fetcher_->TerminateTransfer(); |
| 85 | } |
| 86 | |
| 87 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 88 | const char* bytes, |
| 89 | int length) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 90 | bytes_received_ += length; |
| 91 | if (delegate_) |
| 92 | delegate_->BytesReceived(bytes_received_, install_plan_.size); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 93 | int rc = writer_->Write(bytes, length); |
| 94 | TEST_AND_RETURN(rc >= 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | omaha_hash_calculator_.Update(bytes, length); |
| 96 | } |
| 97 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 98 | namespace { |
| 99 | void FlushLinuxCaches() { |
| 100 | vector<string> command; |
| 101 | command.push_back("/bin/sync"); |
| 102 | int rc; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 103 | LOG(INFO) << "FlushLinuxCaches-sync..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 104 | Subprocess::SynchronousExec(command, &rc); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 105 | LOG(INFO) << "FlushLinuxCaches-drop_caches..."; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 106 | |
| 107 | const char* const drop_cmd = "3\n"; |
| 108 | utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd)); |
| 109 | |
| 110 | LOG(INFO) << "FlushLinuxCaches done."; |
| 111 | } |
| 112 | } |
| 113 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 115 | if (writer_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 116 | CHECK_EQ(writer_->Close(), 0) << errno; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 117 | writer_ = NULL; |
| 118 | } |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 119 | ActionExitCode code = |
| 120 | successful ? kActionCodeSuccess : kActionCodeDownloadTransferError; |
| 121 | if (code == kActionCodeSuccess) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 122 | // Make sure hash is correct |
| 123 | omaha_hash_calculator_.Finalize(); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 124 | if (omaha_hash_calculator_.hash() != install_plan_.download_hash) { |
| 125 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 126 | << " failed. Expect hash " << install_plan_.download_hash |
| 127 | << " but got hash " << omaha_hash_calculator_.hash(); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 128 | code = kActionCodeDownloadHashMismatchError; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 131 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 132 | FlushLinuxCaches(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 133 | |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 134 | // Write the path to the output pipe if we're successful. |
| 135 | if (code == kActionCodeSuccess && HasOutputPipe()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 136 | SetOutputObject(GetInputObject()); |
Darin Petkov | c97435c | 2010-07-20 12:37:43 -0700 | [diff] [blame] | 137 | processor_->ActionComplete(this, code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | }; // namespace {} |