blob: 9be81f0eb763a69c694d7ca0210729b2ea7d52b8 [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 Petkovc1a8b422010-07-19 11:34:49 -070064 processor_->ActionComplete(this, kActionCodeError);
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 Petkovc1a8b422010-07-19 11:34:49 -070073 processor_->ActionComplete(this, kActionCodeError);
Andrew de los Reyesf9185172010-05-03 11:07:05 -070074 return;
75 }
76 }
77 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078}
79
80void DownloadAction::TerminateProcessing() {
81 CHECK(writer_);
82 CHECK_EQ(writer_->Close(), 0);
83 writer_ = NULL;
84 http_fetcher_->TerminateTransfer();
85}
86
87void DownloadAction::ReceivedBytes(HttpFetcher *fetcher,
88 const char* bytes,
89 int length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070090 bytes_received_ += length;
91 if (delegate_)
92 delegate_->BytesReceived(bytes_received_, install_plan_.size);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000093 int rc = writer_->Write(bytes, length);
94 TEST_AND_RETURN(rc >= 0);
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 omaha_hash_calculator_.Update(bytes, length);
96}
97
Andrew de los Reyesf9714432010-05-04 10:21:23 -070098namespace {
99void FlushLinuxCaches() {
100 vector<string> command;
101 command.push_back("/bin/sync");
102 int rc;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700103 LOG(INFO) << "FlushLinuxCaches-sync...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700104 Subprocess::SynchronousExec(command, &rc);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700105 LOG(INFO) << "FlushLinuxCaches-drop_caches...";
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700106
107 const char* const drop_cmd = "3\n";
108 utils::WriteFile("/proc/sys/vm/drop_caches", drop_cmd, strlen(drop_cmd));
109
110 LOG(INFO) << "FlushLinuxCaches done.";
111}
112}
113
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114void DownloadAction::TransferComplete(HttpFetcher *fetcher, bool successful) {
115 if (writer_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000116 CHECK_EQ(writer_->Close(), 0) << errno;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 writer_ = NULL;
118 }
119 if (successful) {
120 // Make sure hash is correct
121 omaha_hash_calculator_.Finalize();
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700122 if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
123 LOG(ERROR) << "Download of " << install_plan_.download_url
124 << " failed. Expect hash " << install_plan_.download_hash
125 << " but got hash " << omaha_hash_calculator_.hash();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 successful = false;
127 }
128 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700129
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700130 FlushLinuxCaches();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131
132 // Write the path to the output pipe if we're successful
133 if (successful && HasOutputPipe())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000134 SetOutputObject(GetInputObject());
Darin Petkovc1a8b422010-07-19 11:34:49 -0700135 processor_->ActionComplete(
136 this,
137 successful ? kActionCodeSuccess : kActionCodeError);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138}
139
140}; // namespace {}