blob: b6f7f81f14d17b8c036cab14ad68c2eeaa0ed992 [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
110void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
111 const char* bytes,
112 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700113 bytes_received_ += length;
114 if (delegate_)
115 delegate_->BytesReceived(bytes_received_, install_plan_.size);
Darin Petkov698d0412010-10-13 10:59:44 -0700116 if (writer_ && writer_->Write(bytes, length) < 0) {
117 LOG(ERROR) << "Write error -- terminating processing.";
118 TerminateProcessing();
119 processor_->ActionComplete(this, kActionCodeDownloadWriteError);
120 return;
121 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 omaha_hash_calculator_.Update(bytes, length);
123}
124
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700125namespace {
126void FlushLinuxCaches() {
127 vector<string> command;
128 command.push_back("/bin/sync");
129 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700130 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700131 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700132 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700133
134 const char* const drop_cmd = "3\n";
135 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
136
137 LOG(INFO) << "FlushLinuxCaches done.";
138}
139}
140
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
142 if (writer_) {
Darin Petkov698d0412010-10-13 10:59:44 -0700143 LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144 writer_ = NULL;
145 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700146 if (delegate_) {
147 delegate_->SetDownloadStatus(false); // Set to inactive.
148 }
Darin Petkovc97435c2010-07-20 12:37:43 -0700149 ActionExitCode code =
150 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
151 if (code == kActionCodeSuccess) {
Darin Petkov437adc42010-10-07 13:12:24 -0700152 if (!install_plan_.is_full_update) {
153 if (!delta_performer_->VerifyPayload("",
154 install_plan_.download_hash,
155 install_plan_.size)) {
156 LOG(ERROR) << "Download of " << install_plan_.download_url
157 << " failed due to payload verification error.";
158 code = kActionCodeDownloadPayloadVerificationError;
Darin Petkov2dd01092010-10-08 15:43:05 -0700159 } else if (!delta_performer_->VerifyAppliedUpdate(
160 install_plan_.install_path, install_plan_.kernel_install_path)) {
161 LOG(ERROR) << "Download of " << install_plan_.download_url
162 << " failed due to applied update verification error.";
163 code = kActionCodeDownloadAppliedUpdateVerificationError;
Darin Petkov437adc42010-10-07 13:12:24 -0700164 }
165 } else {
166 // Makes sure the hash and size are correct for an old-style full update.
167 omaha_hash_calculator_.Finalize();
168 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
169 LOG(ERROR) << "Download of " << install_plan_.download_url
170 << " failed. Expected hash " << install_plan_.download_hash
171 << " but got hash " << omaha_hash_calculator_.hash();
172 code = kActionCodeDownloadHashMismatchError;
173 } else if (bytes_received_ != install_plan_.size) {
174 LOG(ERROR) << "Download of " << install_plan_.download_url
175 << " failed. Expected size " << install_plan_.size
176 << " but got size " << bytes_received_;
177 code = kActionCodeDownloadSizeMismatchError;
178 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 }
180 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700181
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700182 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183
Darin Petkovc97435c2010-07-20 12:37:43 -0700184 // Write the path to the output pipe if we're successful.
185 if (code == kActionCodeSuccess && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000186 SetOutputObject(GetInputObject());
Darin Petkovc97435c2010-07-20 12:37:43 -0700187 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188}
189
190}; // namespace {}