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