[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> |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 8 | #include <utility> |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 11 | #include "base/bind.h" |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 13 | #include "base/files/memory_mapped_file.h" |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 14 | #include "base/location.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 16 | #include "base/task/post_task.h" |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 17 | #include "base/threading/sequenced_task_runner_handle.h" |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 18 | #include "components/update_client/patcher.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 19 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 20 | #include "components/update_client/update_client_errors.h" |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 21 | #include "components/update_client/utils.h" |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 22 | #include "courgette/courgette.h" |
altimin | 979ea2e1 | 2016-05-18 16:16:24 | [diff] [blame] | 23 | #include "courgette/third_party/bsdiff/bsdiff.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 24 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 25 | namespace update_client { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 26 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 29 | const char kOutput[] = "output"; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 30 | const char kSha256[] = "sha256"; |
| 31 | |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 32 | // The integer offset disambiguates between overlapping error ranges. |
| 33 | const int kCourgetteErrorOffset = 300; |
| 34 | const int kBsdiffErrorOffset = 600; |
| 35 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 36 | } // namespace |
| 37 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 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"; |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 43 | |
Jay Civelli | 29f9b94 | 2017-11-08 20:07:26 | [diff] [blame] | 44 | DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation, |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 45 | scoped_refptr<Patcher> patcher) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 46 | if (operation == "copy") { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 47 | return new DeltaUpdateOpCopy(); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 48 | } else if (operation == "create") { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 49 | return new DeltaUpdateOpCreate(); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 50 | } else if (operation == "bsdiff" || operation == "courgette") { |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 51 | return new DeltaUpdateOpPatch(operation, patcher); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 52 | } |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 53 | return nullptr; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 56 | DeltaUpdateOp::DeltaUpdateOp() { |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 57 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 58 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 59 | DeltaUpdateOp::~DeltaUpdateOp() { |
| 60 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 61 | |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 62 | void DeltaUpdateOp::Run(const base::DictionaryValue* command_args, |
| 63 | const base::FilePath& input_dir, |
| 64 | const base::FilePath& unpack_dir, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 65 | scoped_refptr<CrxInstaller> installer, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 66 | ComponentPatcher::Callback callback) { |
| 67 | callback_ = std::move(callback); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 68 | std::string output_rel_path; |
| 69 | if (!command_args->GetString(kOutput, &output_rel_path) || |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 70 | !command_args->GetString(kSha256, &output_sha256_)) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 71 | DoneRunning(UnpackerError::kDeltaBadCommands, 0); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 72 | return; |
| 73 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 74 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 75 | output_abs_path_ = |
| 76 | unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 77 | UnpackerError parse_result = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 78 | DoParseArguments(command_args, input_dir, installer); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 79 | if (parse_result != UnpackerError::kNone) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 80 | DoneRunning(parse_result, 0); |
| 81 | return; |
| 82 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 83 | |
| 84 | const base::FilePath parent = output_abs_path_.DirName(); |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 85 | if (!base::DirectoryExists(parent)) { |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 86 | if (!base::CreateDirectory(parent)) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 87 | DoneRunning(UnpackerError::kIoError, 0); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 88 | return; |
| 89 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 90 | } |
| 91 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 92 | DoRun(base::BindOnce(&DeltaUpdateOp::DoneRunning, |
| 93 | scoped_refptr<DeltaUpdateOp>(this))); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 94 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 95 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 96 | void DeltaUpdateOp::DoneRunning(UnpackerError error, int extended_error) { |
| 97 | if (error == UnpackerError::kNone) |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 98 | error = CheckHash(); |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 99 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 100 | FROM_HERE, base::BindOnce(std::move(callback_), error, extended_error)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Uses the hash as a checksum to confirm that the file now residing in the |
| 104 | // output directory probably has the contents it should. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 105 | UnpackerError DeltaUpdateOp::CheckHash() { |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 106 | return VerifyFileHash256(output_abs_path_, output_sha256_) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 107 | ? UnpackerError::kNone |
| 108 | : UnpackerError::kDeltaVerificationFailure; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 111 | DeltaUpdateOpCopy::DeltaUpdateOpCopy() { |
| 112 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 113 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 114 | DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { |
| 115 | } |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 116 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 117 | UnpackerError DeltaUpdateOpCopy::DoParseArguments( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 118 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 119 | const base::FilePath& input_dir, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 120 | scoped_refptr<CrxInstaller> installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 121 | std::string input_rel_path; |
| 122 | if (!command_args->GetString(kInput, &input_rel_path)) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 123 | return UnpackerError::kDeltaBadCommands; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 124 | |
| 125 | if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 126 | return UnpackerError::kDeltaMissingExistingFile; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 127 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 128 | return UnpackerError::kNone; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 129 | } |
| 130 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 131 | void DeltaUpdateOpCopy::DoRun(ComponentPatcher::Callback callback) { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 132 | if (!base::CopyFile(input_abs_path_, output_abs_path_)) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 133 | std::move(callback).Run(UnpackerError::kDeltaOperationFailure, 0); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 134 | else |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 135 | std::move(callback).Run(UnpackerError::kNone, 0); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 138 | DeltaUpdateOpCreate::DeltaUpdateOpCreate() { |
| 139 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 140 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 141 | DeltaUpdateOpCreate::~DeltaUpdateOpCreate() { |
| 142 | } |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 143 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 144 | UnpackerError DeltaUpdateOpCreate::DoParseArguments( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 145 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 146 | const base::FilePath& input_dir, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 147 | scoped_refptr<CrxInstaller> installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 148 | std::string patch_rel_path; |
| 149 | if (!command_args->GetString(kPatch, &patch_rel_path)) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 150 | return UnpackerError::kDeltaBadCommands; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 151 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 152 | patch_abs_path_ = |
| 153 | input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 154 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 155 | return UnpackerError::kNone; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 156 | } |
| 157 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 158 | void DeltaUpdateOpCreate::DoRun(ComponentPatcher::Callback callback) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 159 | if (!base::Move(patch_abs_path_, output_abs_path_)) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 160 | std::move(callback).Run(UnpackerError::kDeltaOperationFailure, 0); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 161 | else |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 162 | std::move(callback).Run(UnpackerError::kNone, 0); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 163 | } |
| 164 | |
Jay Civelli | 29f9b94 | 2017-11-08 20:07:26 | [diff] [blame] | 165 | DeltaUpdateOpPatch::DeltaUpdateOpPatch(const std::string& operation, |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 166 | scoped_refptr<Patcher> patcher) |
| 167 | : operation_(operation), patcher_(patcher) { |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 168 | DCHECK(operation == kBsdiff || operation == kCourgette); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | DeltaUpdateOpPatch::~DeltaUpdateOpPatch() { |
| 172 | } |
| 173 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 174 | UnpackerError DeltaUpdateOpPatch::DoParseArguments( |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 175 | const base::DictionaryValue* command_args, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 176 | const base::FilePath& input_dir, |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 177 | scoped_refptr<CrxInstaller> installer) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 178 | std::string patch_rel_path; |
| 179 | std::string input_rel_path; |
| 180 | if (!command_args->GetString(kPatch, &patch_rel_path) || |
| 181 | !command_args->GetString(kInput, &input_rel_path)) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 182 | return UnpackerError::kDeltaBadCommands; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 183 | |
| 184 | if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 185 | return UnpackerError::kDeltaMissingExistingFile; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 186 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 187 | patch_abs_path_ = |
| 188 | input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 189 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 190 | return UnpackerError::kNone; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 191 | } |
| 192 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 193 | void DeltaUpdateOpPatch::DoRun(ComponentPatcher::Callback callback) { |
Joshua Pawlicki | d5409e1 | 2019-04-06 00:23:11 | [diff] [blame] | 194 | if (operation_ == kBsdiff) { |
| 195 | patcher_->PatchBsdiff(input_abs_path_, patch_abs_path_, output_abs_path_, |
| 196 | base::BindOnce(&DeltaUpdateOpPatch::DonePatching, |
| 197 | this, std::move(callback))); |
| 198 | } else { |
| 199 | patcher_->PatchCourgette(input_abs_path_, patch_abs_path_, output_abs_path_, |
| 200 | base::BindOnce(&DeltaUpdateOpPatch::DonePatching, |
| 201 | this, std::move(callback))); |
| 202 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 203 | } |
| 204 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 205 | void DeltaUpdateOpPatch::DonePatching(ComponentPatcher::Callback callback, |
| 206 | int result) { |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 207 | if (operation_ == kBsdiff) { |
huangs | 7054b5a2 | 2016-07-26 21:46:13 | [diff] [blame] | 208 | if (result == bsdiff::OK) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 209 | std::move(callback).Run(UnpackerError::kNone, 0); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 210 | } else { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 211 | std::move(callback).Run(UnpackerError::kDeltaOperationFailure, |
| 212 | result + kBsdiffErrorOffset); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 213 | } |
| 214 | } else if (operation_ == kCourgette) { |
| 215 | if (result == courgette::C_OK) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 216 | std::move(callback).Run(UnpackerError::kNone, 0); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 217 | } else { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 218 | std::move(callback).Run(UnpackerError::kDeltaOperationFailure, |
| 219 | result + kCourgetteErrorOffset); |
[email protected] | e260af7 | 2014-08-05 07:52:39 | [diff] [blame] | 220 | } |
| 221 | } else { |
| 222 | NOTREACHED(); |
[email protected] | 94a481b | 2014-03-28 19:41:55 | [diff] [blame] | 223 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 224 | } |
| 225 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 226 | } // namespace update_client |