blob: e952a2900a4df9d711287e752dadf0d981b7a515 [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_response_handler_action.h"
Darin Petkov73058b42010-10-06 16:32:19 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -07008
9#include <base/logging.h>
10
Darin Petkov0406e402010-10-06 21:33:11 -070011#include "update_engine/delta_performer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070012#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include "update_engine/utils.h"
14
15using std::string;
16
17namespace chromeos_update_engine {
18
Darin Petkov6c118642010-10-21 12:06:30 -070019const char OmahaResponseHandlerAction::kDeadlineFile[] =
20 "/tmp/update-check-response-deadline";
21
Darin Petkovabc7bc02011-02-23 14:39:43 -080022OmahaResponseHandlerAction::OmahaResponseHandlerAction(PrefsInterface* prefs)
23 : prefs_(prefs),
24 got_no_update_response_(false),
25 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath) {}
26
adlr@google.com3defe6a2009-12-04 20:57:17 +000027void OmahaResponseHandlerAction::PerformAction() {
28 CHECK(HasInputObject());
29 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070030 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000031 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080032 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000033 LOG(INFO) << "There are no updates. Aborting.";
34 return;
35 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070036 install_plan_.download_url = response.codebase;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070037 install_plan_.payload_size = response.size;
38 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070039 install_plan_.metadata_size = response.metadata_size;
40 install_plan_.metadata_signature = response.metadata_signature;
Darin Petkov0406e402010-10-06 21:33:11 -070041
42 install_plan_.is_resume =
43 DeltaPerformer::CanResumeUpdate(prefs_, response.hash);
44 if (!install_plan_.is_resume) {
Darin Petkov9b230572010-10-08 10:20:09 -070045 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(prefs_, false))
Darin Petkov0406e402010-10-06 21:33:11 -070046 << "Unable to reset the update progress.";
47 LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateCheckResponseHash,
48 response.hash))
49 << "Unable to save the update check response hash.";
50 }
51
adlr@google.com3defe6a2009-12-04 20:57:17 +000052 TEST_AND_RETURN(GetInstallDev(
53 (!boot_device_.empty() ? boot_device_ : utils::BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070054 &install_plan_.install_path));
55 install_plan_.kernel_install_path =
56 utils::BootKernelDevice(install_plan_.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +000057
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070058 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070060 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +000061 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070062 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -070063
Darin Petkov6c118642010-10-21 12:06:30 -070064 // Send the deadline data (if any) to Chrome through a file. This is a pretty
65 // hacky solution but should be OK for now.
66 //
67 // TODO(petkov): Rearchitect this to avoid communication through a
68 // file. Ideallly, we would include this information in D-Bus's GetStatus
69 // method and UpdateStatus signal. A potential issue is that update_engine may
70 // be unresponsive during an update download.
71 utils::WriteFile(kDeadlineFile,
72 response.deadline.data(),
73 response.deadline.size());
74 chmod(kDeadlineFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
75
Darin Petkovc1a8b422010-07-19 11:34:49 -070076 completer.set_code(kActionCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +000077}
78
79bool OmahaResponseHandlerAction::GetInstallDev(const std::string& boot_dev,
80 std::string* install_dev) {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070081 TEST_AND_RETURN_FALSE(utils::StringHasPrefix(boot_dev, "/dev/"));
adlr@google.com3defe6a2009-12-04 20:57:17 +000082 string ret(boot_dev);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070083 string::reverse_iterator it = ret.rbegin(); // last character in string
84 // Right now, we just switch '3' and '5' partition numbers.
85 TEST_AND_RETURN_FALSE((*it == '3') || (*it == '5'));
86 *it = (*it == '3') ? '5' : '3';
adlr@google.com3defe6a2009-12-04 20:57:17 +000087 *install_dev = ret;
88 return true;
89}
90
91} // namespace chromeos_update_engine