blob: 1491caa900b2ea92dbff485b1a79fe2720134913 [file] [log] [blame]
Darin Petkov85d02b72011-05-17 13:25:51 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rspangler@google.com49fdf182009-10-10 00:57:34 +00005#include "update_engine/download_action.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00006#include <errno.h>
7#include <algorithm>
Andrew de los Reyesf9714432010-05-04 10:21:23 -07008#include <string>
9#include <vector>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000010#include <glib.h>
11#include "update_engine/action_pipe.h"
Andrew de los Reyesf9714432010-05-04 10:21:23 -070012#include "update_engine/subprocess.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013
14using std::min;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070015using std::string;
16using std::vector;
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
18namespace chromeos_update_engine {
19
Darin Petkove971f332010-09-22 16:57:25 -070020// Use a buffer to reduce the number of IOPS on SSD devices.
21const size_t kFileWriterBufferSize = 128 * 1024; // 128 KiB
22
Darin Petkov73058b42010-10-06 16:32:19 -070023DownloadAction::DownloadAction(PrefsInterface* prefs,
24 HttpFetcher* http_fetcher)
25 : prefs_(prefs),
26 writer_(NULL),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070027 http_fetcher_(http_fetcher),
Darin Petkov9ce452b2010-11-17 14:33:28 -080028 code_(kActionCodeSuccess),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070029 delegate_(NULL),
Andrew de los Reyes21816e12011-04-07 14:18:56 -070030 bytes_received_(0),
31 skip_reporting_signature_fail_(NULL) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000032
33DownloadAction::~DownloadAction() {}
34
35void DownloadAction::PerformAction() {
36 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
adlr@google.comc98a7ed2009-12-04 18:54:03 +000038 // Get the InstallPlan and read it
39 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070040 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070041 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000042
Andrew de los Reyesf9185172010-05-03 11:07:05 -070043 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000044
Andrew de los Reyesf9185172010-05-03 11:07:05 -070045 if (writer_) {
46 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000047 } else {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070048 if (install_plan_.is_full_update) {
49 kernel_file_writer_.reset(new DirectFileWriter);
50 rootfs_file_writer_.reset(new DirectFileWriter);
Darin Petkove971f332010-09-22 16:57:25 -070051 kernel_buffered_file_writer_.reset(
52 new BufferedFileWriter(kernel_file_writer_.get(),
53 kFileWriterBufferSize));
54 rootfs_buffered_file_writer_.reset(
55 new BufferedFileWriter(rootfs_file_writer_.get(),
56 kFileWriterBufferSize));
57 split_file_writer_.reset(
58 new SplitFileWriter(kernel_buffered_file_writer_.get(),
59 rootfs_buffered_file_writer_.get()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -070060 split_file_writer_->SetFirstOpenArgs(
61 install_plan_.kernel_install_path.c_str(),
62 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
63 0644);
64 decompressing_file_writer_.reset(
65 new GzipDecompressingFileWriter(split_file_writer_.get()));
66 writer_ = decompressing_file_writer_.get();
67 } else {
Darin Petkov73058b42010-10-06 16:32:19 -070068 delta_performer_.reset(new DeltaPerformer(prefs_));
Darin Petkov3aefa862010-12-07 14:45:00 -080069 delta_performer_->set_current_kernel_hash(install_plan_.kernel_hash);
70 delta_performer_->set_current_rootfs_hash(install_plan_.rootfs_hash);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070071 writer_ = delta_performer_.get();
72 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070074 int rc = writer_->Open(install_plan_.install_path.c_str(),
75 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
76 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000077 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070078 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 // report error to processor
Darin Petkovc97435c2010-07-20 12:37:43 -070080 processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 return;
82 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070083 if (!install_plan_.is_full_update) {
84 if (!delta_performer_->OpenKernel(
85 install_plan_.kernel_install_path.c_str())) {
86 LOG(ERROR) << "Unable to open kernel file "
87 << install_plan_.kernel_install_path.c_str();
88 writer_->Close();
Darin Petkovc97435c2010-07-20 12:37:43 -070089 processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070090 return;
91 }
92 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070093 if (delegate_) {
94 delegate_->SetDownloadStatus(true); // Set to active.
95 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070096 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000097}
98
99void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700100 if (writer_) {
101 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
102 writer_ = NULL;
103 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700104 if (delegate_) {
105 delegate_->SetDownloadStatus(false); // Set to inactive.
106 }
Darin Petkov9ce452b2010-11-17 14:33:28 -0800107 // Terminates the transfer. The action is terminated, if necessary, when the
108 // TransferTerminated callback is received.
109 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110}
111
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700112void DownloadAction::SeekToOffset(off_t offset) {
113 bytes_received_ = offset;
114}
115
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
117 const char* bytes,
118 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700119 bytes_received_ += length;
120 if (delegate_)
121 delegate_->BytesReceived(bytes_received_, install_plan_.size);
Darin Petkov698d0412010-10-13 10:59:44 -0700122 if (writer_ && writer_->Write(bytes, length) < 0) {
123 LOG(ERROR) << "Write error -- terminating processing.";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800124 // Don't tell the action processor that the action is complete until we get
125 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
126 // objects may get destroyed before all callbacks are complete.
127 code_ = kActionCodeDownloadWriteError;
Darin Petkov698d0412010-10-13 10:59:44 -0700128 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700129 return;
130 }
Darin Petkovcdc20a92010-10-27 14:04:37 -0700131 // DeltaPerformer checks the hashes for delta updates.
132 if (install_plan_.is_full_update) {
133 omaha_hash_calculator_.Update(bytes, length);
134 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135}
136
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700137namespace {
138void FlushLinuxCaches() {
139 vector<string> command;
140 command.push_back("/bin/sync");
141 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700142 LOG(INFO) << "FlushLinuxCaches-sync...";
Darin Petkov85d02b72011-05-17 13:25:51 -0700143 Subprocess::SynchronousExec(command, &rc, NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700144 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700145
146 const char* const drop_cmd = "3\n";
147 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
148
149 LOG(INFO) << "FlushLinuxCaches done.";
150}
151}
152
rspangler@google.com49fdf182009-10-10 00:57:34 +0000153void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
154 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700155 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 writer_ = NULL;
157 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700158 if (delegate_) {
159 delegate_->SetDownloadStatus(false); // Set to inactive.
160 }
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700161 bool signature_verify_failed = false;
Darin Petkovc97435c2010-07-20 12:37:43 -0700162 ActionExitCode code =
163 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
164 if (code == kActionCodeSuccess) {
Darin Petkov437adc42010-10-07 13:12:24 -0700165 if (!install_plan_.is_full_update) {
166 if (!delta_performer_->VerifyPayload("",
167 install_plan_.download_hash,
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -0700168 install_plan_.size,
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700169 &signature_verify_failed)) {
Darin Petkov437adc42010-10-07 13:12:24 -0700170 LOG(ERROR) << "Download of " << install_plan_.download_url
171 << " failed due to payload verification error.";
172 code = kActionCodeDownloadPayloadVerificationError;
Darin Petkov3aefa862010-12-07 14:45:00 -0800173 } else if (!delta_performer_->GetNewPartitionInfo(
174 &install_plan_.kernel_size,
175 &install_plan_.kernel_hash,
176 &install_plan_.rootfs_size,
177 &install_plan_.rootfs_hash)) {
178 LOG(ERROR) << "Unable to get new partition hash info.";
179 code = kActionCodeDownloadNewPartitionInfoError;
Darin Petkov437adc42010-10-07 13:12:24 -0700180 }
181 } else {
182 // Makes sure the hash and size are correct for an old-style full update.
183 omaha_hash_calculator_.Finalize();
184 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
185 LOG(ERROR) << "Download of " << install_plan_.download_url
186 << " failed. Expected hash " << install_plan_.download_hash
187 << " but got hash " << omaha_hash_calculator_.hash();
188 code = kActionCodeDownloadHashMismatchError;
189 } else if (bytes_received_ != install_plan_.size) {
190 LOG(ERROR) << "Download of " << install_plan_.download_url
191 << " failed. Expected size " << install_plan_.size
192 << " but got size " << bytes_received_;
193 code = kActionCodeDownloadSizeMismatchError;
194 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195 }
196 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700197
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700198 if (skip_reporting_signature_fail_.get() &&
199 (code != kActionCodeSuccess || !signature_verify_failed)) {
200 LOG(INFO) << "Suppressing signature pub key verification warning";
201 skip_reporting_signature_fail_->Run();
202 }
203
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700204 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000205
Darin Petkovc97435c2010-07-20 12:37:43 -0700206 // Write the path to the output pipe if we're successful.
207 if (code == kActionCodeSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800208 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700209 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210}
211
Darin Petkov9ce452b2010-11-17 14:33:28 -0800212void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
213 if (code_ != kActionCodeSuccess) {
214 processor_->ActionComplete(this, code_);
215 }
216}
217
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218}; // namespace {}