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> |
| 8 | #include <glib.h> |
| 9 | #include "update_engine/action_pipe.h" |
| 10 | |
| 11 | using std::min; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 15 | DownloadAction::DownloadAction(HttpFetcher* http_fetcher) |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 16 | : writer_(NULL), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | http_fetcher_(http_fetcher) {} |
| 18 | |
| 19 | DownloadAction::~DownloadAction() {} |
| 20 | |
| 21 | void DownloadAction::PerformAction() { |
| 22 | http_fetcher_->set_delegate(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | // Get the InstallPlan and read it |
| 25 | CHECK(HasInputObject()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 26 | install_plan_ = GetInputObject(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 27 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 28 | install_plan_.Dump(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 29 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 30 | if (writer_) { |
| 31 | LOG(INFO) << "Using writer for test."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | } else { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 33 | if (install_plan_.is_full_update) { |
| 34 | kernel_file_writer_.reset(new DirectFileWriter); |
| 35 | rootfs_file_writer_.reset(new DirectFileWriter); |
| 36 | split_file_writer_.reset(new SplitFileWriter(kernel_file_writer_.get(), |
| 37 | rootfs_file_writer_.get())); |
| 38 | split_file_writer_->SetFirstOpenArgs( |
| 39 | install_plan_.kernel_install_path.c_str(), |
| 40 | O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, |
| 41 | 0644); |
| 42 | decompressing_file_writer_.reset( |
| 43 | new GzipDecompressingFileWriter(split_file_writer_.get())); |
| 44 | writer_ = decompressing_file_writer_.get(); |
| 45 | } else { |
| 46 | delta_performer_.reset(new DeltaPerformer); |
| 47 | writer_ = delta_performer_.get(); |
| 48 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 50 | int rc = writer_->Open(install_plan_.install_path.c_str(), |
| 51 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, |
| 52 | 0644); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | if (rc < 0) { |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 54 | LOG(ERROR) << "Unable to open output file " << install_plan_.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | // report error to processor |
| 56 | processor_->ActionComplete(this, false); |
| 57 | return; |
| 58 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 59 | if (!install_plan_.is_full_update) { |
| 60 | if (!delta_performer_->OpenKernel( |
| 61 | install_plan_.kernel_install_path.c_str())) { |
| 62 | LOG(ERROR) << "Unable to open kernel file " |
| 63 | << install_plan_.kernel_install_path.c_str(); |
| 64 | writer_->Close(); |
| 65 | processor_->ActionComplete(this, false); |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | http_fetcher_->BeginTransfer(install_plan_.download_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void DownloadAction::TerminateProcessing() { |
| 73 | CHECK(writer_); |
| 74 | CHECK_EQ(writer_->Close(), 0); |
| 75 | writer_ = NULL; |
| 76 | http_fetcher_->TerminateTransfer(); |
| 77 | } |
| 78 | |
| 79 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 80 | const char* bytes, |
| 81 | int length) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 82 | int rc = writer_->Write(bytes, length); |
| 83 | TEST_AND_RETURN(rc >= 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | omaha_hash_calculator_.Update(bytes, length); |
| 85 | } |
| 86 | |
| 87 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 88 | if (writer_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 89 | CHECK_EQ(writer_->Close(), 0) << errno; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | writer_ = NULL; |
| 91 | } |
| 92 | if (successful) { |
| 93 | // Make sure hash is correct |
| 94 | omaha_hash_calculator_.Finalize(); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 95 | if (omaha_hash_calculator_.hash() != install_plan_.download_hash) { |
| 96 | LOG(ERROR) << "Download of " << install_plan_.download_url |
| 97 | << " failed. Expect hash " << install_plan_.download_hash |
| 98 | << " but got hash " << omaha_hash_calculator_.hash(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 99 | successful = false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Write the path to the output pipe if we're successful |
| 104 | if (successful && HasOutputPipe()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 105 | SetOutputObject(GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 106 | processor_->ActionComplete(this, successful); |
| 107 | } |
| 108 | |
| 109 | }; // namespace {} |