blob: bb5d18938c7d302a0c062455ab40a9b0fd3d8102 [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),
28 delegate_(NULL),
29 bytes_received_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000030
31DownloadAction::~DownloadAction() {}
32
33void DownloadAction::PerformAction() {
34 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000035
adlr@google.comc98a7ed2009-12-04 18:54:03 +000036 // Get the InstallPlan and read it
37 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070038 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070039 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000040
Andrew de los Reyesf9185172010-05-03 11:07:05 -070041 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000042
Andrew de los Reyesf9185172010-05-03 11:07:05 -070043 if (writer_) {
44 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 } else {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070046 if (install_plan_.is_full_update) {
47 kernel_file_writer_.reset(new DirectFileWriter);
48 rootfs_file_writer_.reset(new DirectFileWriter);
Darin Petkove971f332010-09-22 16:57:25 -070049 kernel_buffered_file_writer_.reset(
50 new BufferedFileWriter(kernel_file_writer_.get(),
51 kFileWriterBufferSize));
52 rootfs_buffered_file_writer_.reset(
53 new BufferedFileWriter(rootfs_file_writer_.get(),
54 kFileWriterBufferSize));
55 split_file_writer_.reset(
56 new SplitFileWriter(kernel_buffered_file_writer_.get(),
57 rootfs_buffered_file_writer_.get()));
Andrew de los Reyesf9185172010-05-03 11:07:05 -070058 split_file_writer_->SetFirstOpenArgs(
59 install_plan_.kernel_install_path.c_str(),
60 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
61 0644);
62 decompressing_file_writer_.reset(
63 new GzipDecompressingFileWriter(split_file_writer_.get()));
64 writer_ = decompressing_file_writer_.get();
65 } else {
Darin Petkov73058b42010-10-06 16:32:19 -070066 delta_performer_.reset(new DeltaPerformer(prefs_));
Darin Petkov698d0412010-10-13 10:59:44 -070067 delta_performer_->set_current_kernel_hash(
68 &install_plan_.current_kernel_hash);
69 delta_performer_->set_current_rootfs_hash(
70 &install_plan_.current_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 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 http_fetcher_->TerminateTransfer();
Darin Petkov9d911fa2010-08-19 09:36:08 -0700105 if (delegate_) {
106 delegate_->SetDownloadStatus(false); // Set to inactive.
107 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108}
109
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700110void DownloadAction::SeekToOffset(off_t offset) {
111 bytes_received_ = offset;
112}
113
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
115 const char* bytes,
116 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700117 bytes_received_ += length;
118 if (delegate_)
119 delegate_->BytesReceived(bytes_received_, install_plan_.size);
Darin Petkov698d0412010-10-13 10:59:44 -0700120 if (writer_ && writer_->Write(bytes, length) < 0) {
121 LOG(ERROR) << "Write error -- terminating processing.";
122 TerminateProcessing();
123 processor_->ActionComplete(this, kActionCodeDownloadWriteError);
124 return;
125 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 omaha_hash_calculator_.Update(bytes, length);
127}
128
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700129namespace {
130void FlushLinuxCaches() {
131 vector<string> command;
132 command.push_back("/bin/sync");
133 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700134 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700135 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700136 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700137
138 const char* const drop_cmd = "3\n";
139 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
140
141 LOG(INFO) << "FlushLinuxCaches done.";
142}
143}
144
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
146 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700147 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000148 writer_ = NULL;
149 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700150 if (delegate_) {
151 delegate_->SetDownloadStatus(false); // Set to inactive.
152 }
Darin Petkovc97435c2010-07-20 12:37:43 -0700153 ActionExitCode code =
154 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
155 if (code == kActionCodeSuccess) {
Darin Petkov437adc42010-10-07 13:12:24 -0700156 if (!install_plan_.is_full_update) {
157 if (!delta_performer_->VerifyPayload("",
158 install_plan_.download_hash,
159 install_plan_.size)) {
160 LOG(ERROR) << "Download of " << install_plan_.download_url
161 << " failed due to payload verification error.";
162 code = kActionCodeDownloadPayloadVerificationError;
Darin Petkov2dd01092010-10-08 15:43:05 -0700163 } else if (!delta_performer_->VerifyAppliedUpdate(
164 install_plan_.install_path, install_plan_.kernel_install_path)) {
165 LOG(ERROR) << "Download of " << install_plan_.download_url
166 << " failed due to applied update verification error.";
167 code = kActionCodeDownloadAppliedUpdateVerificationError;
Darin Petkov437adc42010-10-07 13:12:24 -0700168 }
169 } else {
170 // Makes sure the hash and size are correct for an old-style full update.
171 omaha_hash_calculator_.Finalize();
172 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
173 LOG(ERROR) << "Download of " << install_plan_.download_url
174 << " failed. Expected hash " << install_plan_.download_hash
175 << " but got hash " << omaha_hash_calculator_.hash();
176 code = kActionCodeDownloadHashMismatchError;
177 } else if (bytes_received_ != install_plan_.size) {
178 LOG(ERROR) << "Download of " << install_plan_.download_url
179 << " failed. Expected size " << install_plan_.size
180 << " but got size " << bytes_received_;
181 code = kActionCodeDownloadSizeMismatchError;
182 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183 }
184 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700185
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700186 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187
Darin Petkovc97435c2010-07-20 12:37:43 -0700188 // Write the path to the output pipe if we're successful.
189 if (code == kActionCodeSuccess && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000190 SetOutputObject(GetInputObject());
Darin Petkovc97435c2010-07-20 12:37:43 -0700191 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192}
193
194}; // namespace {}