blob: d06fb790c44596df512403af6849296a796ca80c [file] [log] [blame]
Darin Petkovc1a8b422010-07-19 11:34:49 -07001// Copyright (c) 2010 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),
30 bytes_received_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
32DownloadAction::~DownloadAction() {}
33
34void DownloadAction::PerformAction() {
35 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037 // Get the InstallPlan and read it
38 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070039 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000041
Andrew de los Reyesf9185172010-05-03 11:07:05 -070042 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000043
Andrew de los Reyesf9185172010-05-03 11:07:05 -070044 if (writer_) {
45 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 } else {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070047 if (install_plan_.is_full_update) {
48 kernel_file_writer_.reset(new DirectFileWriter);
49 rootfs_file_writer_.reset(new DirectFileWriter);
Darin Petkove971f332010-09-22 16:57:25 -070050 kernel_buffered_file_writer_.reset(
51 new BufferedFileWriter(kernel_file_writer_.get(),
52 kFileWriterBufferSize));
53 rootfs_buffered_file_writer_.reset(
54 new BufferedFileWriter(rootfs_file_writer_.get(),
55 kFileWriterBufferSize));
56 split_file_writer_.reset(
57 new SplitFileWriter(kernel_buffered_file_writer_.get(),
58 rootfs_buffered_file_writer_.get()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -070059 split_file_writer_->SetFirstOpenArgs(
60 install_plan_.kernel_install_path.c_str(),
61 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
62 0644);
63 decompressing_file_writer_.reset(
64 new GzipDecompressingFileWriter(split_file_writer_.get()));
65 writer_ = decompressing_file_writer_.get();
66 } else {
Darin Petkov73058b42010-10-06 16:32:19 -070067 delta_performer_.reset(new DeltaPerformer(prefs_));
Darin Petkov698d0412010-10-13 10:59:44 -070068 delta_performer_->set_current_kernel_hash(
69 &install_plan_.current_kernel_hash);
70 delta_performer_->set_current_rootfs_hash(
71 &install_plan_.current_rootfs_hash);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070072 writer_ = delta_performer_.get();
73 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070075 int rc = writer_->Open(install_plan_.install_path.c_str(),
76 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
77 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070079 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000080 // report error to processor
Darin Petkovc97435c2010-07-20 12:37:43 -070081 processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 return;
83 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070084 if (!install_plan_.is_full_update) {
85 if (!delta_performer_->OpenKernel(
86 install_plan_.kernel_install_path.c_str())) {
87 LOG(ERROR) << "Unable to open kernel file "
88 << install_plan_.kernel_install_path.c_str();
89 writer_->Close();
Darin Petkovc97435c2010-07-20 12:37:43 -070090 processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070091 return;
92 }
93 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070094 if (delegate_) {
95 delegate_->SetDownloadStatus(true); // Set to active.
96 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070097 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000098}
99
100void DownloadAction::TerminateProcessing() {
Darin Petkov698d0412010-10-13 10:59:44 -0700101 if (writer_) {
102 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
103 writer_ = NULL;
104 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700105 if (delegate_) {
106 delegate_->SetDownloadStatus(false); // Set to inactive.
107 }
Darin Petkov9ce452b2010-11-17 14:33:28 -0800108 // Terminates the transfer. The action is terminated, if necessary, when the
109 // TransferTerminated callback is received.
110 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111}
112
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700113void DownloadAction::SeekToOffset(off_t offset) {
114 bytes_received_ = offset;
115}
116
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
118 const char* bytes,
119 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700120 bytes_received_ += length;
121 if (delegate_)
122 delegate_->BytesReceived(bytes_received_, install_plan_.size);
Darin Petkov698d0412010-10-13 10:59:44 -0700123 if (writer_ && writer_->Write(bytes, length) < 0) {
124 LOG(ERROR) << "Write error -- terminating processing.";
Darin Petkov9ce452b2010-11-17 14:33:28 -0800125 // Don't tell the action processor that the action is complete until we get
126 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
127 // objects may get destroyed before all callbacks are complete.
128 code_ = kActionCodeDownloadWriteError;
Darin Petkov698d0412010-10-13 10:59:44 -0700129 TerminateProcessing();
Darin Petkov698d0412010-10-13 10:59:44 -0700130 return;
131 }
Darin Petkovcdc20a92010-10-27 14:04:37 -0700132 // DeltaPerformer checks the hashes for delta updates.
133 if (install_plan_.is_full_update) {
134 omaha_hash_calculator_.Update(bytes, length);
135 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136}
137
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700138namespace {
139void FlushLinuxCaches() {
140 vector<string> command;
141 command.push_back("/bin/sync");
142 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700144 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700145 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700146
147 const char* const drop_cmd = "3\n";
148 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
149
150 LOG(INFO) << "FlushLinuxCaches done.";
151}
152}
153
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
155 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700156 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157 writer_ = NULL;
158 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700159 if (delegate_) {
160 delegate_->SetDownloadStatus(false); // Set to inactive.
161 }
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,
168 install_plan_.size)) {
169 LOG(ERROR) << "Download of " << install_plan_.download_url
170 << " failed due to payload verification error.";
171 code = kActionCodeDownloadPayloadVerificationError;
Darin Petkov2dd01092010-10-08 15:43:05 -0700172 } else if (!delta_performer_->VerifyAppliedUpdate(
173 install_plan_.install_path, install_plan_.kernel_install_path)) {
174 LOG(ERROR) << "Download of " << install_plan_.download_url
175 << " failed due to applied update verification error.";
176 code = kActionCodeDownloadAppliedUpdateVerificationError;
Darin Petkov437adc42010-10-07 13:12:24 -0700177 }
178 } else {
179 // Makes sure the hash and size are correct for an old-style full update.
180 omaha_hash_calculator_.Finalize();
181 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
182 LOG(ERROR) << "Download of " << install_plan_.download_url
183 << " failed. Expected hash " << install_plan_.download_hash
184 << " but got hash " << omaha_hash_calculator_.hash();
185 code = kActionCodeDownloadHashMismatchError;
186 } else if (bytes_received_ != install_plan_.size) {
187 LOG(ERROR) << "Download of " << install_plan_.download_url
188 << " failed. Expected size " << install_plan_.size
189 << " but got size " << bytes_received_;
190 code = kActionCodeDownloadSizeMismatchError;
191 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192 }
193 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700194
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700195 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196
Darin Petkovc97435c2010-07-20 12:37:43 -0700197 // Write the path to the output pipe if we're successful.
198 if (code == kActionCodeSuccess && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000199 SetOutputObject(GetInputObject());
Darin Petkovc97435c2010-07-20 12:37:43 -0700200 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201}
202
Darin Petkov9ce452b2010-11-17 14:33:28 -0800203void DownloadAction::TransferTerminated(HttpFetcher *fetcher) {
204 if (code_ != kActionCodeSuccess) {
205 processor_->ActionComplete(this, code_);
206 }
207}
208
rspangler@google.com49fdf182009-10-10 00:57:34 +0000209}; // namespace {}