[email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #include "components/update_client/component_patcher_operation.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 6 | |
sorin | 5cb1f549 | 2014-09-23 04:07:44 | [diff] [blame] | 7 | #include <stdint.h> |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 10 | #include "base/bind.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 12 | #include "base/files/memory_mapped_file.h" |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 13 | #include "base/location.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 15 | #include "components/update_client/update_client.h" |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 16 | #include "components/update_client/utils.h" |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 17 | #include "courgette/courgette.h" |
altimin | 979ea2e1 | 2016-05-18 16:16:24 | [diff] [blame] | 18 | #include "courgette/third_party/bsdiff/bsdiff.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 19 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 20 | namespace update_client { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 21 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 22 | namespace { |
| 23 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 24 | const char kOutput[] = "output"; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 25 | const char kSha256[] = "sha256"; |
| 26 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 27 | // The integer offset disambiguates between overlapping error ranges. |
| 28 | const int kCourgetteErrorOffset = 300; |
| 29 | const int kBsdiffErrorOffset = 600; |
| 30 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 31 | } // namespace |
| 32 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 33 | const char kOp[] = "op"; |
| 34 | const char kBsdiff[] = "bsdiff"; |
| 35 | const char kCourgette[] = "courgette"; |
| 36 | const char kInput[] = "input"; |
| 37 | const char kPatch[] = "patch"; |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 38 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 39 | DeltaUpdateOp* CreateDeltaUpdateOp( |
| 40 | const std::string& operation, |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 41 | const scoped_refptr<OutOfProcessPatcher>& out_of_process_patcher) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 42 | if (operation == "copy") { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 43 | return new DeltaUpdateOpCopy(); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 44 | } else if (operation == "create") { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 45 | return new DeltaUpdateOpCreate(); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 46 | } else if (operation == "bsdiff" || operation == "courgette") { |
| 47 | return new DeltaUpdateOpPatch(operation, out_of_process_patcher); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 48 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 52 | DeltaUpdateOp::DeltaUpdateOp() { |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 53 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 54 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 55 | DeltaUpdateOp::~DeltaUpdateOp() { |
| 56 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 57 | |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 58 | void DeltaUpdateOp::Run( |
| 59 | const base::DictionaryValue* command_args, |
| 60 | const base::FilePath& input_dir, |
| 61 | const base::FilePath& unpack_dir, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 62 | const scoped_refptr<CrxInstaller>& installer, |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame^] | 63 | const ComponentPatcher::Callback& callback, |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 64 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 65 | callback_ = callback; |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 66 | task_runner_ = task_runner; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 67 | std::string output_rel_path; |
| 68 | if (!command_args->GetString(kOutput, &output_rel_path) || |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 69 | !command_args->GetString(kSha256, &output_sha256_)) { |
| 70 | DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0); |
| 71 | return; |
| 72 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 73 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 74 | output_abs_path_ = |
| 75 | unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); |
| 76 | ComponentUnpacker::Error parse_result = |
| 77 | DoParseArguments(command_args, input_dir, installer); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 78 | if (parse_result != ComponentUnpacker::kNone) { |
| 79 | DoneRunning(parse_result, 0); |
| 80 | return; |
| 81 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 82 | |
| 83 | const base::FilePath parent = output_abs_path_.DirName(); |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 84 | if (!base::DirectoryExists(parent)) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 85 | if (!base::CreateDirectory(parent)) { |
| 86 | DoneRunning(ComponentUnpacker::kIoError, 0); |
| 87 | return; |
| 88 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 91 | DoRun(base::Bind(&DeltaUpdateOp::DoneRunning, |
| 92 | scoped_refptr<DeltaUpdateOp>(this))); |
| 93 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 94 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 95 | void DeltaUpdateOp::DoneRunning(ComponentUnpacker::Error error, |
| 96 | int extended_error) { |
| 97 | if (error == ComponentUnpacker::kNone) |
| 98 | error = CheckHash(); |
| 99 | task_runner_->PostTask(FROM_HERE, |
| 100 | base::Bind(callback_, error, extended_error)); |
| 101 | callback_.Reset(); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 102 | } |
| 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. |
| 106 | ComponentUnpacker::Error DeltaUpdateOp::CheckHash() { |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 107 | return VerifyFileHash256(output_abs_path_, output_sha256_) |
| 108 | ? ComponentUnpacker::kNone |
| 109 | : ComponentUnpacker::kDeltaVerificationFailure; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 87da10b | 2014-04-02 04:13:14 | [diff] [blame] | 112 | scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() { |
| 113 | return task_runner_; |
| 114 | } |
| 115 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 116 | DeltaUpdateOpCopy::DeltaUpdateOpCopy() { |
| 117 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 118 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 119 | DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { |
| 120 | } |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 121 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 122 | ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 123 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 124 | const base::FilePath& input_dir, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 125 | const scoped_refptr<CrxInstaller>& installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 126 | std::string input_rel_path; |
| 127 | if (!command_args->GetString(kInput, &input_rel_path)) |
| 128 | return ComponentUnpacker::kDeltaBadCommands; |
| 129 | |
| 130 | if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 131 | return ComponentUnpacker::kDeltaMissingExistingFile; |
| 132 | |
| 133 | return ComponentUnpacker::kNone; |
| 134 | } |
| 135 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame^] | 136 | void DeltaUpdateOpCopy::DoRun(const ComponentPatcher::Callback& callback) { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 137 | if (!base::CopyFile(input_abs_path_, output_abs_path_)) |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 138 | callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); |
| 139 | else |
| 140 | callback.Run(ComponentUnpacker::kNone, 0); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 143 | DeltaUpdateOpCreate::DeltaUpdateOpCreate() { |
| 144 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 145 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 146 | DeltaUpdateOpCreate::~DeltaUpdateOpCreate() { |
| 147 | } |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 148 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 149 | ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 150 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 151 | const base::FilePath& input_dir, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 152 | const scoped_refptr<CrxInstaller>& installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 153 | std::string patch_rel_path; |
| 154 | if (!command_args->GetString(kPatch, &patch_rel_path)) |
| 155 | return ComponentUnpacker::kDeltaBadCommands; |
| 156 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 157 | patch_abs_path_ = |
| 158 | input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 159 | |
| 160 | return ComponentUnpacker::kNone; |
| 161 | } |
| 162 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame^] | 163 | void DeltaUpdateOpCreate::DoRun(const ComponentPatcher::Callback& callback) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 164 | if (!base::Move(patch_abs_path_, output_abs_path_)) |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 165 | callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); |
| 166 | else |
| 167 | callback.Run(ComponentUnpacker::kNone, 0); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 170 | DeltaUpdateOpPatch::DeltaUpdateOpPatch( |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 171 | const std::string& operation, |
| 172 | scoped_refptr<OutOfProcessPatcher> out_of_process_patcher) |
| 173 | : operation_(operation), out_of_process_patcher_(out_of_process_patcher) { |
| 174 | DCHECK(operation == kBsdiff || operation == kCourgette); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | DeltaUpdateOpPatch::~DeltaUpdateOpPatch() { |
| 178 | } |
| 179 | |
| 180 | ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments( |
| 181 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 182 | const base::FilePath& input_dir, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 183 | const scoped_refptr<CrxInstaller>& installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 184 | std::string patch_rel_path; |
| 185 | std::string input_rel_path; |
| 186 | if (!command_args->GetString(kPatch, &patch_rel_path) || |
| 187 | !command_args->GetString(kInput, &input_rel_path)) |
| 188 | return ComponentUnpacker::kDeltaBadCommands; |
| 189 | |
| 190 | if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 191 | return ComponentUnpacker::kDeltaMissingExistingFile; |
| 192 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 193 | patch_abs_path_ = |
| 194 | input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 195 | |
| 196 | return ComponentUnpacker::kNone; |
| 197 | } |
| 198 | |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame^] | 199 | void DeltaUpdateOpPatch::DoRun(const ComponentPatcher::Callback& callback) { |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 200 | if (out_of_process_patcher_.get()) { |
| 201 | out_of_process_patcher_->Patch( |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 202 | operation_, GetTaskRunner(), input_abs_path_, patch_abs_path_, |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 203 | output_abs_path_, |
| 204 | base::Bind(&DeltaUpdateOpPatch::DonePatching, this, callback)); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 205 | return; |
| 206 | } |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 207 | |
| 208 | if (operation_ == kBsdiff) { |
| 209 | DonePatching(callback, |
huangs | 7054b5a2 | 2016-07-26 21:46:13 | [diff] [blame] | 210 | bsdiff::ApplyBinaryPatch(input_abs_path_, patch_abs_path_, |
| 211 | output_abs_path_)); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 212 | } else if (operation_ == kCourgette) { |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 213 | DonePatching(callback, courgette::ApplyEnsemblePatch( |
| 214 | input_abs_path_.value().c_str(), |
| 215 | patch_abs_path_.value().c_str(), |
| 216 | output_abs_path_.value().c_str())); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 217 | } else { |
| 218 | NOTREACHED(); |
| 219 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 222 | void DeltaUpdateOpPatch::DonePatching( |
sorin | cbb7c09 | 2016-10-27 01:39:20 | [diff] [blame^] | 223 | const ComponentPatcher::Callback& callback, |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 224 | int result) { |
| 225 | if (operation_ == kBsdiff) { |
huangs | 7054b5a2 | 2016-07-26 21:46:13 | [diff] [blame] | 226 | if (result == bsdiff::OK) { |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 227 | callback.Run(ComponentUnpacker::kNone, 0); |
| 228 | } else { |
| 229 | callback.Run(ComponentUnpacker::kDeltaOperationFailure, |
| 230 | result + kBsdiffErrorOffset); |
| 231 | } |
| 232 | } else if (operation_ == kCourgette) { |
| 233 | if (result == courgette::C_OK) { |
| 234 | callback.Run(ComponentUnpacker::kNone, 0); |
| 235 | } else { |
| 236 | callback.Run(ComponentUnpacker::kDeltaOperationFailure, |
| 237 | result + kCourgetteErrorOffset); |
| 238 | } |
| 239 | } else { |
| 240 | NOTREACHED(); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 241 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 242 | } |
| 243 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 244 | } // namespace update_client |