blob: b88e44825a3644d4b593838c387d128947a40d8a [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_));
Andrew de los Reyesf9185172010-05-03 11:07:05 -070067 writer_ = delta_performer_.get();
68 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070070 int rc = writer_->Open(install_plan_.install_path.c_str(),
71 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
72 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070074 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 // report error to processor
Darin Petkovc97435c2010-07-20 12:37:43 -070076 processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000077 return;
78 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070079 if (!install_plan_.is_full_update) {
80 if (!delta_performer_->OpenKernel(
81 install_plan_.kernel_install_path.c_str())) {
82 LOG(ERROR) << "Unable to open kernel file "
83 << install_plan_.kernel_install_path.c_str();
84 writer_->Close();
Darin Petkovc97435c2010-07-20 12:37:43 -070085 processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070086 return;
87 }
88 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070089 if (delegate_) {
90 delegate_->SetDownloadStatus(true); // Set to active.
91 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070092 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000093}
94
95void DownloadAction::TerminateProcessing() {
96 CHECK(writer_);
97 CHECK_EQ(writer_->Close(), 0);
98 writer_ = NULL;
99 http_fetcher_->TerminateTransfer();
Darin Petkov9d911fa2010-08-19 09:36:08 -0700100 if (delegate_) {
101 delegate_->SetDownloadStatus(false); // Set to inactive.
102 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103}
104
105void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
106 const char* bytes,
107 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700108 bytes_received_ += length;
109 if (delegate_)
110 delegate_->BytesReceived(bytes_received_, install_plan_.size);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000111 int rc = writer_->Write(bytes, length);
112 TEST_AND_RETURN(rc >= 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113 omaha_hash_calculator_.Update(bytes, length);
114}
115
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700116namespace {
117void FlushLinuxCaches() {
118 vector<string> command;
119 command.push_back("/bin/sync");
120 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700121 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700122 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700123 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700124
125 const char* const drop_cmd = "3\n";
126 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
127
128 LOG(INFO) << "FlushLinuxCaches done.";
129}
130}
131
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
133 if (writer_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000134 CHECK_EQ(writer_->Close(), 0) << errno;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000135 writer_ = NULL;
136 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700137 if (delegate_) {
138 delegate_->SetDownloadStatus(false); // Set to inactive.
139 }
Darin Petkovc97435c2010-07-20 12:37:43 -0700140 ActionExitCode code =
141 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
142 if (code == kActionCodeSuccess) {
Darin Petkov437adc42010-10-07 13:12:24 -0700143 if (!install_plan_.is_full_update) {
144 if (!delta_performer_->VerifyPayload("",
145 install_plan_.download_hash,
146 install_plan_.size)) {
147 LOG(ERROR) << "Download of " << install_plan_.download_url
148 << " failed due to payload verification error.";
149 code = kActionCodeDownloadPayloadVerificationError;
150 }
151 } else {
152 // Makes sure the hash and size are correct for an old-style full update.
153 omaha_hash_calculator_.Finalize();
154 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
155 LOG(ERROR) << "Download of " << install_plan_.download_url
156 << " failed. Expected hash " << install_plan_.download_hash
157 << " but got hash " << omaha_hash_calculator_.hash();
158 code = kActionCodeDownloadHashMismatchError;
159 } else if (bytes_received_ != install_plan_.size) {
160 LOG(ERROR) << "Download of " << install_plan_.download_url
161 << " failed. Expected size " << install_plan_.size
162 << " but got size " << bytes_received_;
163 code = kActionCodeDownloadSizeMismatchError;
164 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000165 }
166 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700167
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700168 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169
Darin Petkovc97435c2010-07-20 12:37:43 -0700170 // Write the path to the output pipe if we're successful.
171 if (code == kActionCodeSuccess && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000172 SetOutputObject(GetInputObject());
Darin Petkovc97435c2010-07-20 12:37:43 -0700173 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000174}
175
176}; // namespace {}