blob: 0cfc8aaad96aafe7fa3ee8904960226d91d7a12b [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]e3e696d32013-06-21 20:41:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
sorin52ac0882015-01-24 01:15:005#include "components/update_client/component_patcher_operation.h"
[email protected]e3e696d32013-06-21 20:41:366
sorin5cb1f5492014-09-23 04:07:447#include <stdint.h>
[email protected]e3e696d32013-06-21 20:41:368#include <vector>
9
[email protected]94a481b2014-03-28 19:41:5510#include "base/bind.h"
thestig819adcc82014-09-10 22:24:5311#include "base/files/file_util.h"
[email protected]e3e696d32013-06-21 20:41:3612#include "base/files/memory_mapped_file.h"
[email protected]e260af72014-08-05 07:52:3913#include "base/location.h"
[email protected]e3e696d32013-06-21 20:41:3614#include "base/strings/string_number_conversions.h"
Sorin Jianuebd652462017-07-23 02:00:5815#include "base/task_scheduler/post_task.h"
16#include "base/threading/sequenced_task_runner_handle.h"
sorincca1c122017-05-11 17:43:2217#include "components/update_client/out_of_process_patcher.h"
sorin52ac0882015-01-24 01:15:0018#include "components/update_client/update_client.h"
sorin7b8650522016-11-02 18:23:4119#include "components/update_client/update_client_errors.h"
sorin74e70672016-02-03 03:13:1020#include "components/update_client/utils.h"
[email protected]94a481b2014-03-28 19:41:5521#include "courgette/courgette.h"
altimin979ea2e12016-05-18 16:16:2422#include "courgette/third_party/bsdiff/bsdiff.h"
[email protected]e3e696d32013-06-21 20:41:3623
sorin52ac0882015-01-24 01:15:0024namespace update_client {
[email protected]055981f2014-01-17 20:22:3225
[email protected]e3e696d32013-06-21 20:41:3626namespace {
27
[email protected]e3e696d32013-06-21 20:41:3628const char kOutput[] = "output";
[email protected]e3e696d32013-06-21 20:41:3629const char kSha256[] = "sha256";
30
[email protected]94a481b2014-03-28 19:41:5531// The integer offset disambiguates between overlapping error ranges.
32const int kCourgetteErrorOffset = 300;
33const int kBsdiffErrorOffset = 600;
34
[email protected]e3e696d32013-06-21 20:41:3635} // namespace
36
[email protected]e260af72014-08-05 07:52:3937const char kOp[] = "op";
38const char kBsdiff[] = "bsdiff";
39const char kCourgette[] = "courgette";
40const char kInput[] = "input";
41const char kPatch[] = "patch";
[email protected]94a481b2014-03-28 19:41:5542
[email protected]e260af72014-08-05 07:52:3943DeltaUpdateOp* CreateDeltaUpdateOp(
44 const std::string& operation,
bauerb810e60f42015-02-05 01:09:1045 const scoped_refptr<OutOfProcessPatcher>& out_of_process_patcher) {
[email protected]94a481b2014-03-28 19:41:5546 if (operation == "copy") {
[email protected]e3e696d32013-06-21 20:41:3647 return new DeltaUpdateOpCopy();
[email protected]94a481b2014-03-28 19:41:5548 } else if (operation == "create") {
[email protected]e3e696d32013-06-21 20:41:3649 return new DeltaUpdateOpCreate();
[email protected]e260af72014-08-05 07:52:3950 } else if (operation == "bsdiff" || operation == "courgette") {
51 return new DeltaUpdateOpPatch(operation, out_of_process_patcher);
[email protected]94a481b2014-03-28 19:41:5552 }
Ivan Kotenkov75b1c3a2017-10-24 14:47:2453 return nullptr;
[email protected]e3e696d32013-06-21 20:41:3654}
55
[email protected]e260af72014-08-05 07:52:3956DeltaUpdateOp::DeltaUpdateOp() {
[email protected]d0c8b8b42014-05-06 05:11:4557}
[email protected]e3e696d32013-06-21 20:41:3658
[email protected]d0c8b8b42014-05-06 05:11:4559DeltaUpdateOp::~DeltaUpdateOp() {
60}
[email protected]e3e696d32013-06-21 20:41:3661
Sorin Jianuebd652462017-07-23 02:00:5862void DeltaUpdateOp::Run(const base::DictionaryValue* command_args,
63 const base::FilePath& input_dir,
64 const base::FilePath& unpack_dir,
65 const scoped_refptr<CrxInstaller>& installer,
66 const ComponentPatcher::Callback& callback) {
[email protected]94a481b2014-03-28 19:41:5567 callback_ = callback;
[email protected]e3e696d32013-06-21 20:41:3668 std::string output_rel_path;
69 if (!command_args->GetString(kOutput, &output_rel_path) ||
[email protected]94a481b2014-03-28 19:41:5570 !command_args->GetString(kSha256, &output_sha256_)) {
sorin7b8650522016-11-02 18:23:4171 DoneRunning(UnpackerError::kDeltaBadCommands, 0);
[email protected]94a481b2014-03-28 19:41:5572 return;
73 }
[email protected]e3e696d32013-06-21 20:41:3674
[email protected]d0c8b8b42014-05-06 05:11:4575 output_abs_path_ =
76 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path));
sorin7b8650522016-11-02 18:23:4177 UnpackerError parse_result =
[email protected]d0c8b8b42014-05-06 05:11:4578 DoParseArguments(command_args, input_dir, installer);
sorin7b8650522016-11-02 18:23:4179 if (parse_result != UnpackerError::kNone) {
[email protected]94a481b2014-03-28 19:41:5580 DoneRunning(parse_result, 0);
81 return;
82 }
[email protected]e3e696d32013-06-21 20:41:3683
84 const base::FilePath parent = output_abs_path_.DirName();
[email protected]dcd16612013-07-15 20:18:0985 if (!base::DirectoryExists(parent)) {
[email protected]94a481b2014-03-28 19:41:5586 if (!base::CreateDirectory(parent)) {
sorin7b8650522016-11-02 18:23:4187 DoneRunning(UnpackerError::kIoError, 0);
[email protected]94a481b2014-03-28 19:41:5588 return;
89 }
[email protected]e3e696d32013-06-21 20:41:3690 }
91
[email protected]94a481b2014-03-28 19:41:5592 DoRun(base::Bind(&DeltaUpdateOp::DoneRunning,
93 scoped_refptr<DeltaUpdateOp>(this)));
94}
[email protected]e3e696d32013-06-21 20:41:3695
sorin7b8650522016-11-02 18:23:4196void DeltaUpdateOp::DoneRunning(UnpackerError error, int extended_error) {
97 if (error == UnpackerError::kNone)
[email protected]94a481b2014-03-28 19:41:5598 error = CheckHash();
Sorin Jianuebd652462017-07-23 02:00:5899 base::SequencedTaskRunnerHandle::Get()->PostTask(
100 FROM_HERE, base::BindOnce(callback_, error, extended_error));
[email protected]94a481b2014-03-28 19:41:55101 callback_.Reset();
[email protected]e3e696d32013-06-21 20:41:36102}
103
104// Uses the hash as a checksum to confirm that the file now residing in the
105// output directory probably has the contents it should.
sorin7b8650522016-11-02 18:23:41106UnpackerError DeltaUpdateOp::CheckHash() {
sorin74e70672016-02-03 03:13:10107 return VerifyFileHash256(output_abs_path_, output_sha256_)
sorin7b8650522016-11-02 18:23:41108 ? UnpackerError::kNone
109 : UnpackerError::kDeltaVerificationFailure;
[email protected]e3e696d32013-06-21 20:41:36110}
111
[email protected]d0c8b8b42014-05-06 05:11:45112DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
113}
[email protected]e3e696d32013-06-21 20:41:36114
[email protected]d0c8b8b42014-05-06 05:11:45115DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
116}
[email protected]94a481b2014-03-28 19:41:55117
sorin7b8650522016-11-02 18:23:41118UnpackerError DeltaUpdateOpCopy::DoParseArguments(
[email protected]94a481b2014-03-28 19:41:55119 const base::DictionaryValue* command_args,
[email protected]e3e696d32013-06-21 20:41:36120 const base::FilePath& input_dir,
sorin9797aba2015-04-17 17:15:03121 const scoped_refptr<CrxInstaller>& installer) {
[email protected]e3e696d32013-06-21 20:41:36122 std::string input_rel_path;
123 if (!command_args->GetString(kInput, &input_rel_path))
sorin7b8650522016-11-02 18:23:41124 return UnpackerError::kDeltaBadCommands;
[email protected]e3e696d32013-06-21 20:41:36125
126 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
sorin7b8650522016-11-02 18:23:41127 return UnpackerError::kDeltaMissingExistingFile;
[email protected]e3e696d32013-06-21 20:41:36128
sorin7b8650522016-11-02 18:23:41129 return UnpackerError::kNone;
[email protected]e3e696d32013-06-21 20:41:36130}
131
sorincbb7c092016-10-27 01:39:20132void DeltaUpdateOpCopy::DoRun(const ComponentPatcher::Callback& callback) {
[email protected]f0ff2ad2013-07-09 17:42:26133 if (!base::CopyFile(input_abs_path_, output_abs_path_))
sorin7b8650522016-11-02 18:23:41134 callback.Run(UnpackerError::kDeltaOperationFailure, 0);
[email protected]94a481b2014-03-28 19:41:55135 else
sorin7b8650522016-11-02 18:23:41136 callback.Run(UnpackerError::kNone, 0);
[email protected]e3e696d32013-06-21 20:41:36137}
138
[email protected]d0c8b8b42014-05-06 05:11:45139DeltaUpdateOpCreate::DeltaUpdateOpCreate() {
140}
[email protected]e3e696d32013-06-21 20:41:36141
[email protected]d0c8b8b42014-05-06 05:11:45142DeltaUpdateOpCreate::~DeltaUpdateOpCreate() {
143}
[email protected]94a481b2014-03-28 19:41:55144
sorin7b8650522016-11-02 18:23:41145UnpackerError DeltaUpdateOpCreate::DoParseArguments(
[email protected]94a481b2014-03-28 19:41:55146 const base::DictionaryValue* command_args,
[email protected]e3e696d32013-06-21 20:41:36147 const base::FilePath& input_dir,
sorin9797aba2015-04-17 17:15:03148 const scoped_refptr<CrxInstaller>& installer) {
[email protected]e3e696d32013-06-21 20:41:36149 std::string patch_rel_path;
150 if (!command_args->GetString(kPatch, &patch_rel_path))
sorin7b8650522016-11-02 18:23:41151 return UnpackerError::kDeltaBadCommands;
[email protected]e3e696d32013-06-21 20:41:36152
[email protected]d0c8b8b42014-05-06 05:11:45153 patch_abs_path_ =
154 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
[email protected]e3e696d32013-06-21 20:41:36155
sorin7b8650522016-11-02 18:23:41156 return UnpackerError::kNone;
[email protected]e3e696d32013-06-21 20:41:36157}
158
sorincbb7c092016-10-27 01:39:20159void DeltaUpdateOpCreate::DoRun(const ComponentPatcher::Callback& callback) {
[email protected]5553d5b2013-07-01 23:07:36160 if (!base::Move(patch_abs_path_, output_abs_path_))
sorin7b8650522016-11-02 18:23:41161 callback.Run(UnpackerError::kDeltaOperationFailure, 0);
[email protected]94a481b2014-03-28 19:41:55162 else
sorin7b8650522016-11-02 18:23:41163 callback.Run(UnpackerError::kNone, 0);
[email protected]e3e696d32013-06-21 20:41:36164}
165
[email protected]94a481b2014-03-28 19:41:55166DeltaUpdateOpPatch::DeltaUpdateOpPatch(
[email protected]e260af72014-08-05 07:52:39167 const std::string& operation,
sorincca1c122017-05-11 17:43:22168 const scoped_refptr<OutOfProcessPatcher>& out_of_process_patcher)
[email protected]e260af72014-08-05 07:52:39169 : operation_(operation), out_of_process_patcher_(out_of_process_patcher) {
170 DCHECK(operation == kBsdiff || operation == kCourgette);
[email protected]94a481b2014-03-28 19:41:55171}
172
173DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
174}
175
sorin7b8650522016-11-02 18:23:41176UnpackerError DeltaUpdateOpPatch::DoParseArguments(
[email protected]94a481b2014-03-28 19:41:55177 const base::DictionaryValue* command_args,
[email protected]e3e696d32013-06-21 20:41:36178 const base::FilePath& input_dir,
sorin9797aba2015-04-17 17:15:03179 const scoped_refptr<CrxInstaller>& installer) {
[email protected]e3e696d32013-06-21 20:41:36180 std::string patch_rel_path;
181 std::string input_rel_path;
182 if (!command_args->GetString(kPatch, &patch_rel_path) ||
183 !command_args->GetString(kInput, &input_rel_path))
sorin7b8650522016-11-02 18:23:41184 return UnpackerError::kDeltaBadCommands;
[email protected]e3e696d32013-06-21 20:41:36185
186 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
sorin7b8650522016-11-02 18:23:41187 return UnpackerError::kDeltaMissingExistingFile;
[email protected]e3e696d32013-06-21 20:41:36188
[email protected]d0c8b8b42014-05-06 05:11:45189 patch_abs_path_ =
190 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
[email protected]e3e696d32013-06-21 20:41:36191
sorin7b8650522016-11-02 18:23:41192 return UnpackerError::kNone;
[email protected]e3e696d32013-06-21 20:41:36193}
194
sorincbb7c092016-10-27 01:39:20195void DeltaUpdateOpPatch::DoRun(const ComponentPatcher::Callback& callback) {
[email protected]e260af72014-08-05 07:52:39196 if (out_of_process_patcher_.get()) {
197 out_of_process_patcher_->Patch(
Sorin Jianuebd652462017-07-23 02:00:58198 operation_, input_abs_path_, patch_abs_path_, output_abs_path_,
[email protected]e260af72014-08-05 07:52:39199 base::Bind(&DeltaUpdateOpPatch::DonePatching, this, callback));
[email protected]94a481b2014-03-28 19:41:55200 return;
201 }
[email protected]e260af72014-08-05 07:52:39202
203 if (operation_ == kBsdiff) {
204 DonePatching(callback,
huangs7054b5a22016-07-26 21:46:13205 bsdiff::ApplyBinaryPatch(input_abs_path_, patch_abs_path_,
206 output_abs_path_));
[email protected]e260af72014-08-05 07:52:39207 } else if (operation_ == kCourgette) {
sorin52ac0882015-01-24 01:15:00208 DonePatching(callback, courgette::ApplyEnsemblePatch(
209 input_abs_path_.value().c_str(),
210 patch_abs_path_.value().c_str(),
211 output_abs_path_.value().c_str()));
[email protected]e260af72014-08-05 07:52:39212 } else {
213 NOTREACHED();
214 }
[email protected]e3e696d32013-06-21 20:41:36215}
216
[email protected]e260af72014-08-05 07:52:39217void DeltaUpdateOpPatch::DonePatching(
sorincbb7c092016-10-27 01:39:20218 const ComponentPatcher::Callback& callback,
[email protected]e260af72014-08-05 07:52:39219 int result) {
220 if (operation_ == kBsdiff) {
huangs7054b5a22016-07-26 21:46:13221 if (result == bsdiff::OK) {
sorin7b8650522016-11-02 18:23:41222 callback.Run(UnpackerError::kNone, 0);
[email protected]e260af72014-08-05 07:52:39223 } else {
sorin7b8650522016-11-02 18:23:41224 callback.Run(UnpackerError::kDeltaOperationFailure,
[email protected]e260af72014-08-05 07:52:39225 result + kBsdiffErrorOffset);
226 }
227 } else if (operation_ == kCourgette) {
228 if (result == courgette::C_OK) {
sorin7b8650522016-11-02 18:23:41229 callback.Run(UnpackerError::kNone, 0);
[email protected]e260af72014-08-05 07:52:39230 } else {
sorin7b8650522016-11-02 18:23:41231 callback.Run(UnpackerError::kDeltaOperationFailure,
[email protected]e260af72014-08-05 07:52:39232 result + kCourgetteErrorOffset);
233 }
234 } else {
235 NOTREACHED();
[email protected]94a481b2014-03-28 19:41:55236 }
[email protected]e3e696d32013-06-21 20:41:36237}
238
sorin52ac0882015-01-24 01:15:00239} // namespace update_client