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) |
| 16 | : size_(0), |
| 17 | should_decompress_(false), |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | writer_(NULL), |
| 19 | http_fetcher_(http_fetcher) {} |
| 20 | |
| 21 | DownloadAction::~DownloadAction() {} |
| 22 | |
| 23 | void DownloadAction::PerformAction() { |
| 24 | http_fetcher_->set_delegate(this); |
| 25 | CHECK(!writer_); |
| 26 | direct_file_writer_.reset(new DirectFileWriter); |
| 27 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 28 | // Get the InstallPlan and read it |
| 29 | CHECK(HasInputObject()); |
| 30 | InstallPlan install_plan(GetInputObject()); |
| 31 | |
| 32 | should_decompress_ = install_plan.is_full_update; |
| 33 | url_ = install_plan.download_url; |
| 34 | output_path_ = install_plan.download_path; |
| 35 | hash_ = install_plan.download_hash; |
| 36 | install_plan.Dump(); |
| 37 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | if (should_decompress_) { |
| 39 | decompressing_file_writer_.reset( |
| 40 | new GzipDecompressingFileWriter(direct_file_writer_.get())); |
| 41 | writer_ = decompressing_file_writer_.get(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 42 | output_path_ = install_plan.install_path; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | } else { |
| 44 | writer_ = direct_file_writer_.get(); |
| 45 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | int rc = writer_->Open(output_path_.c_str(), |
| 47 | O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE, 0644); |
| 48 | if (rc < 0) { |
| 49 | LOG(ERROR) << "Unable to open output file " << output_path_; |
| 50 | // report error to processor |
| 51 | processor_->ActionComplete(this, false); |
| 52 | return; |
| 53 | } |
| 54 | http_fetcher_->BeginTransfer(url_); |
| 55 | } |
| 56 | |
| 57 | void DownloadAction::TerminateProcessing() { |
| 58 | CHECK(writer_); |
| 59 | CHECK_EQ(writer_->Close(), 0); |
| 60 | writer_ = NULL; |
| 61 | http_fetcher_->TerminateTransfer(); |
| 62 | } |
| 63 | |
| 64 | void DownloadAction::ReceivedBytes(HttpFetcher *fetcher, |
| 65 | const char* bytes, |
| 66 | int length) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 67 | int rc = writer_->Write(bytes, length); |
| 68 | TEST_AND_RETURN(rc >= 0); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | omaha_hash_calculator_.Update(bytes, length); |
| 70 | } |
| 71 | |
| 72 | void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) { |
| 73 | if (writer_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 74 | CHECK_EQ(writer_->Close(), 0) << errno; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 75 | writer_ = NULL; |
| 76 | } |
| 77 | if (successful) { |
| 78 | // Make sure hash is correct |
| 79 | omaha_hash_calculator_.Finalize(); |
| 80 | if (omaha_hash_calculator_.hash() != hash_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 81 | LOG(ERROR) << "Download of " << url_ << " failed. Expect hash " |
| 82 | << hash_ << " but got hash " << omaha_hash_calculator_.hash(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | successful = false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Write the path to the output pipe if we're successful |
| 88 | if (successful && HasOutputPipe()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame^] | 89 | SetOutputObject(GetInputObject()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | processor_->ActionComplete(this, successful); |
| 91 | } |
| 92 | |
| 93 | }; // namespace {} |