blob: 5096703419fe06ce56da445fdd2307b6fade614c [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +000020DownloadAction::DownloadAction(HttpFetcher* http_fetcher)
Andrew de los Reyesf9185172010-05-03 11:07:05 -070021 : writer_(NULL),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070022 http_fetcher_(http_fetcher),
23 delegate_(NULL),
24 bytes_received_(0) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000025
26DownloadAction::~DownloadAction() {}
27
28void DownloadAction::PerformAction() {
29 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000030
adlr@google.comc98a7ed2009-12-04 18:54:03 +000031 // Get the InstallPlan and read it
32 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070033 install_plan_ = GetInputObject();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070034 bytes_received_ = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000035
Andrew de los Reyesf9185172010-05-03 11:07:05 -070036 install_plan_.Dump();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037
Andrew de los Reyesf9185172010-05-03 11:07:05 -070038 if (writer_) {
39 LOG(INFO) << "Using writer for test.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 } else {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070041 if (install_plan_.is_full_update) {
42 kernel_file_writer_.reset(new DirectFileWriter);
43 rootfs_file_writer_.reset(new DirectFileWriter);
44 split_file_writer_.reset(new SplitFileWriter(kernel_file_writer_.get(),
45 rootfs_file_writer_.get()));
46 split_file_writer_->SetFirstOpenArgs(
47 install_plan_.kernel_install_path.c_str(),
48 O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
49 0644);
50 decompressing_file_writer_.reset(
51 new GzipDecompressingFileWriter(split_file_writer_.get()));
52 writer_ = decompressing_file_writer_.get();
53 } else {
54 delta_performer_.reset(new DeltaPerformer);
55 writer_ = delta_performer_.get();
56 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070058 int rc = writer_->Open(install_plan_.install_path.c_str(),
59 O_TRUNC | O_WRONLY | O_CREAT | O_LARGEFILE,
60 0644);
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 if (rc < 0) {
Andrew de los Reyesf9185172010-05-03 11:07:05 -070062 LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
rspangler@google.com49fdf182009-10-10 00:57:34 +000063 // report error to processor
Darin Petkovc97435c2010-07-20 12:37:43 -070064 processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 return;
66 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070067 if (!install_plan_.is_full_update) {
68 if (!delta_performer_->OpenKernel(
69 install_plan_.kernel_install_path.c_str())) {
70 LOG(ERROR) << "Unable to open kernel file "
71 << install_plan_.kernel_install_path.c_str();
72 writer_->Close();
Darin Petkovc97435c2010-07-20 12:37:43 -070073 processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070074 return;
75 }
76 }
Darin Petkov9d911fa2010-08-19 09:36:08 -070077 if (delegate_) {
78 delegate_->SetDownloadStatus(true); // Set to active.
79 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070080 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000081}
82
83void DownloadAction::TerminateProcessing() {
84 CHECK(writer_);
85 CHECK_EQ(writer_->Close(), 0);
86 writer_ = NULL;
87 http_fetcher_->TerminateTransfer();
Darin Petkov9d911fa2010-08-19 09:36:08 -070088 if (delegate_) {
89 delegate_->SetDownloadStatus(false); // Set to inactive.
90 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000091}
92
93void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
94 const char* bytes,
95 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070096 bytes_received_ += length;
97 if (delegate_)
98 delegate_->BytesReceived(bytes_received_, install_plan_.size);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000099 int rc = writer_->Write(bytes, length);
100 TEST_AND_RETURN(rc >= 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 omaha_hash_calculator_.Update(bytes, length);
102}
103
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700104namespace {
105void FlushLinuxCaches() {
106 vector<string> command;
107 command.push_back("/bin/sync");
108 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700109 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700110 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700111 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700112
113 const char* const drop_cmd = "3\n";
114 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
115
116 LOG(INFO) << "FlushLinuxCaches done.";
117}
118}
119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
121 if (writer_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000122 CHECK_EQ(writer_->Close(), 0) << errno;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 writer_ = NULL;
124 }
Darin Petkov9d911fa2010-08-19 09:36:08 -0700125 if (delegate_) {
126 delegate_->SetDownloadStatus(false); // Set to inactive.
127 }
Darin Petkovc97435c2010-07-20 12:37:43 -0700128 ActionExitCode code =
129 successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
130 if (code == kActionCodeSuccess) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131 // Make sure hash is correct
132 omaha_hash_calculator_.Finalize();
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700133 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
134 LOG(ERROR) << "Download of " << install_plan_.download_url
135 << " failed. Expect hash " << install_plan_.download_hash
136 << " but got hash " << omaha_hash_calculator_.hash();
Darin Petkovc97435c2010-07-20 12:37:43 -0700137 code = kActionCodeDownloadHashMismatchError;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138 }
139 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700140
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700141 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142
Darin Petkovc97435c2010-07-20 12:37:43 -0700143 // Write the path to the output pipe if we're successful.
144 if (code == kActionCodeSuccess && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000145 SetOutputObject(GetInputObject());
Darin Petkovc97435c2010-07-20 12:37:43 -0700146 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000147}
148
149}; // namespace {}