1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/component_updater/component_patcher_operation.h"
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/files/memory_mapped_file.h"
13 #include "base/location.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "components/component_updater/component_patcher.h"
16 #include "components/component_updater/component_updater_service.h"
17 #include "courgette/courgette.h"
18 #include "courgette/third_party/bsdiff.h"
19 #include "crypto/secure_hash.h"
20 #include "crypto/sha2.h"
21 #include "crypto/signature_verifier.h"
23 using crypto::SecureHash
;
25 namespace component_updater
{
29 const char kOutput
[] = "output";
30 const char kSha256
[] = "sha256";
32 // The integer offset disambiguates between overlapping error ranges.
33 const int kCourgetteErrorOffset
= 300;
34 const int kBsdiffErrorOffset
= 600;
38 const char kOp
[] = "op";
39 const char kBsdiff
[] = "bsdiff";
40 const char kCourgette
[] = "courgette";
41 const char kInput
[] = "input";
42 const char kPatch
[] = "patch";
44 DeltaUpdateOp
* CreateDeltaUpdateOp(
45 const std::string
& operation
,
46 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
) {
47 if (operation
== "copy") {
48 return new DeltaUpdateOpCopy();
49 } else if (operation
== "create") {
50 return new DeltaUpdateOpCreate();
51 } else if (operation
== "bsdiff" || operation
== "courgette") {
52 return new DeltaUpdateOpPatch(operation
, out_of_process_patcher
);
57 DeltaUpdateOp::DeltaUpdateOp() {
60 DeltaUpdateOp::~DeltaUpdateOp() {
63 void DeltaUpdateOp::Run(const base::DictionaryValue
* command_args
,
64 const base::FilePath
& input_dir
,
65 const base::FilePath
& unpack_dir
,
66 ComponentInstaller
* installer
,
67 const ComponentUnpacker::Callback
& callback
,
68 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
70 task_runner_
= task_runner
;
71 std::string output_rel_path
;
72 if (!command_args
->GetString(kOutput
, &output_rel_path
) ||
73 !command_args
->GetString(kSha256
, &output_sha256_
)) {
74 DoneRunning(ComponentUnpacker::kDeltaBadCommands
, 0);
79 unpack_dir
.Append(base::FilePath::FromUTF8Unsafe(output_rel_path
));
80 ComponentUnpacker::Error parse_result
=
81 DoParseArguments(command_args
, input_dir
, installer
);
82 if (parse_result
!= ComponentUnpacker::kNone
) {
83 DoneRunning(parse_result
, 0);
87 const base::FilePath parent
= output_abs_path_
.DirName();
88 if (!base::DirectoryExists(parent
)) {
89 if (!base::CreateDirectory(parent
)) {
90 DoneRunning(ComponentUnpacker::kIoError
, 0);
95 DoRun(base::Bind(&DeltaUpdateOp::DoneRunning
,
96 scoped_refptr
<DeltaUpdateOp
>(this)));
99 void DeltaUpdateOp::DoneRunning(ComponentUnpacker::Error error
,
100 int extended_error
) {
101 if (error
== ComponentUnpacker::kNone
)
103 task_runner_
->PostTask(FROM_HERE
,
104 base::Bind(callback_
, error
, extended_error
));
108 // Uses the hash as a checksum to confirm that the file now residing in the
109 // output directory probably has the contents it should.
110 ComponentUnpacker::Error
DeltaUpdateOp::CheckHash() {
111 std::vector
<uint8_t> expected_hash
;
112 if (!base::HexStringToBytes(output_sha256_
, &expected_hash
) ||
113 expected_hash
.size() != crypto::kSHA256Length
)
114 return ComponentUnpacker::kDeltaVerificationFailure
;
116 base::MemoryMappedFile output_file_mmapped
;
117 if (!output_file_mmapped
.Initialize(output_abs_path_
))
118 return ComponentUnpacker::kDeltaVerificationFailure
;
120 uint8_t actual_hash
[crypto::kSHA256Length
] = {0};
121 const scoped_ptr
<SecureHash
> hasher(SecureHash::Create(SecureHash::SHA256
));
122 hasher
->Update(output_file_mmapped
.data(), output_file_mmapped
.length());
123 hasher
->Finish(actual_hash
, sizeof(actual_hash
));
124 if (memcmp(actual_hash
, &expected_hash
[0], sizeof(actual_hash
)))
125 return ComponentUnpacker::kDeltaVerificationFailure
;
127 return ComponentUnpacker::kNone
;
130 scoped_refptr
<base::SequencedTaskRunner
> DeltaUpdateOp::GetTaskRunner() {
134 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
137 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
140 ComponentUnpacker::Error
DeltaUpdateOpCopy::DoParseArguments(
141 const base::DictionaryValue
* command_args
,
142 const base::FilePath
& input_dir
,
143 ComponentInstaller
* installer
) {
144 std::string input_rel_path
;
145 if (!command_args
->GetString(kInput
, &input_rel_path
))
146 return ComponentUnpacker::kDeltaBadCommands
;
148 if (!installer
->GetInstalledFile(input_rel_path
, &input_abs_path_
))
149 return ComponentUnpacker::kDeltaMissingExistingFile
;
151 return ComponentUnpacker::kNone
;
154 void DeltaUpdateOpCopy::DoRun(const ComponentUnpacker::Callback
& callback
) {
155 if (!base::CopyFile(input_abs_path_
, output_abs_path_
))
156 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
, 0);
158 callback
.Run(ComponentUnpacker::kNone
, 0);
161 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {
164 DeltaUpdateOpCreate::~DeltaUpdateOpCreate() {
167 ComponentUnpacker::Error
DeltaUpdateOpCreate::DoParseArguments(
168 const base::DictionaryValue
* command_args
,
169 const base::FilePath
& input_dir
,
170 ComponentInstaller
* installer
) {
171 std::string patch_rel_path
;
172 if (!command_args
->GetString(kPatch
, &patch_rel_path
))
173 return ComponentUnpacker::kDeltaBadCommands
;
176 input_dir
.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path
));
178 return ComponentUnpacker::kNone
;
181 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback
& callback
) {
182 if (!base::Move(patch_abs_path_
, output_abs_path_
))
183 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
, 0);
185 callback
.Run(ComponentUnpacker::kNone
, 0);
188 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
189 const std::string
& operation
,
190 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
)
191 : operation_(operation
), out_of_process_patcher_(out_of_process_patcher
) {
192 DCHECK(operation
== kBsdiff
|| operation
== kCourgette
);
195 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
198 ComponentUnpacker::Error
DeltaUpdateOpPatch::DoParseArguments(
199 const base::DictionaryValue
* command_args
,
200 const base::FilePath
& input_dir
,
201 ComponentInstaller
* installer
) {
202 std::string patch_rel_path
;
203 std::string input_rel_path
;
204 if (!command_args
->GetString(kPatch
, &patch_rel_path
) ||
205 !command_args
->GetString(kInput
, &input_rel_path
))
206 return ComponentUnpacker::kDeltaBadCommands
;
208 if (!installer
->GetInstalledFile(input_rel_path
, &input_abs_path_
))
209 return ComponentUnpacker::kDeltaMissingExistingFile
;
212 input_dir
.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path
));
214 return ComponentUnpacker::kNone
;
217 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback
& callback
) {
218 if (out_of_process_patcher_
.get()) {
219 out_of_process_patcher_
->Patch(
225 base::Bind(&DeltaUpdateOpPatch::DonePatching
, this, callback
));
229 if (operation_
== kBsdiff
) {
230 DonePatching(callback
,
231 courgette::ApplyBinaryPatch(
232 input_abs_path_
, patch_abs_path_
, output_abs_path_
));
233 } else if (operation_
== kCourgette
) {
236 courgette::ApplyEnsemblePatch(input_abs_path_
.value().c_str(),
237 patch_abs_path_
.value().c_str(),
238 output_abs_path_
.value().c_str()));
244 void DeltaUpdateOpPatch::DonePatching(
245 const ComponentUnpacker::Callback
& callback
,
247 if (operation_
== kBsdiff
) {
248 if (result
== courgette::OK
) {
249 callback
.Run(ComponentUnpacker::kNone
, 0);
251 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
,
252 result
+ kBsdiffErrorOffset
);
254 } else if (operation_
== kCourgette
) {
255 if (result
== courgette::C_OK
) {
256 callback
.Run(ComponentUnpacker::kNone
, 0);
258 callback
.Run(ComponentUnpacker::kDeltaOperationFailure
,
259 result
+ kCourgetteErrorOffset
);
266 } // namespace component_updater