| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/component_updater/component_patcher_operation.h" | 5 #include "chrome/browser/component_updater/component_patcher_operation.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 return new DeltaUpdateOpPatchBsdiff(); | 46 return new DeltaUpdateOpPatchBsdiff(); |
| 47 else if (operation == "courgette") | 47 else if (operation == "courgette") |
| 48 return new DeltaUpdateOpPatchCourgette(); | 48 return new DeltaUpdateOpPatchCourgette(); |
| 49 return NULL; | 49 return NULL; |
| 50 } | 50 } |
| 51 | 51 |
| 52 DeltaUpdateOp::DeltaUpdateOp() {} | 52 DeltaUpdateOp::DeltaUpdateOp() {} |
| 53 | 53 |
| 54 DeltaUpdateOp::~DeltaUpdateOp() {} | 54 DeltaUpdateOp::~DeltaUpdateOp() {} |
| 55 | 55 |
| 56 ComponentUnpacker::Error DeltaUpdateOp::Run(base::DictionaryValue* command_args, | 56 component_updater::Error DeltaUpdateOp::Run(base::DictionaryValue* command_args, |
| 57 const base::FilePath& input_dir, | 57 const base::FilePath& input_dir, |
| 58 const base::FilePath& unpack_dir, | 58 const base::FilePath& unpack_dir, |
| 59 ComponentPatcher* patcher, | 59 ComponentPatcher* patcher, |
| 60 ComponentInstaller* installer, | 60 ComponentInstaller* installer, |
| 61 int* error) { | 61 int* error) { |
| 62 std::string output_rel_path; | 62 std::string output_rel_path; |
| 63 if (!command_args->GetString(kOutput, &output_rel_path) || | 63 if (!command_args->GetString(kOutput, &output_rel_path) || |
| 64 !command_args->GetString(kSha256, &output_sha256_)) | 64 !command_args->GetString(kSha256, &output_sha256_)) |
| 65 return ComponentUnpacker::kDeltaBadCommands; | 65 return component_updater::kDeltaBadCommands; |
| 66 | 66 |
| 67 output_abs_path_ = unpack_dir.Append( | 67 output_abs_path_ = unpack_dir.Append( |
| 68 base::FilePath::FromUTF8Unsafe(output_rel_path)); | 68 base::FilePath::FromUTF8Unsafe(output_rel_path)); |
| 69 ComponentUnpacker::Error parse_result = DoParseArguments( | 69 component_updater::Error parse_result = DoParseArguments( |
| 70 command_args, input_dir, installer); | 70 command_args, input_dir, installer); |
| 71 if (parse_result != ComponentUnpacker::kNone) | 71 if (parse_result != component_updater::kNone) |
| 72 return parse_result; | 72 return parse_result; |
| 73 | 73 |
| 74 const base::FilePath parent = output_abs_path_.DirName(); | 74 const base::FilePath parent = output_abs_path_.DirName(); |
| 75 if (!base::DirectoryExists(parent)) { | 75 if (!base::DirectoryExists(parent)) { |
| 76 if (!file_util::CreateDirectory(parent)) | 76 if (!file_util::CreateDirectory(parent)) |
| 77 return ComponentUnpacker::kIoError; | 77 return component_updater::kIoError; |
| 78 } | 78 } |
| 79 | 79 |
| 80 ComponentUnpacker::Error run_result = DoRun(patcher, error); | 80 component_updater::Error run_result = DoRun(patcher, error); |
| 81 if (run_result != ComponentUnpacker::kNone) | 81 if (run_result != component_updater::kNone) |
| 82 return run_result; | 82 return run_result; |
| 83 | 83 |
| 84 return CheckHash(); | 84 return CheckHash(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Uses the hash as a checksum to confirm that the file now residing in the | 87 // Uses the hash as a checksum to confirm that the file now residing in the |
| 88 // output directory probably has the contents it should. | 88 // output directory probably has the contents it should. |
| 89 ComponentUnpacker::Error DeltaUpdateOp::CheckHash() { | 89 component_updater::Error DeltaUpdateOp::CheckHash() { |
| 90 std::vector<uint8> expected_hash; | 90 std::vector<uint8> expected_hash; |
| 91 if (!base::HexStringToBytes(output_sha256_, &expected_hash) || | 91 if (!base::HexStringToBytes(output_sha256_, &expected_hash) || |
| 92 expected_hash.size() != crypto::kSHA256Length) | 92 expected_hash.size() != crypto::kSHA256Length) |
| 93 return ComponentUnpacker::kDeltaVerificationFailure; | 93 return component_updater::kDeltaVerificationFailure; |
| 94 | 94 |
| 95 base::MemoryMappedFile output_file_mmapped; | 95 base::MemoryMappedFile output_file_mmapped; |
| 96 if (!output_file_mmapped.Initialize(output_abs_path_)) | 96 if (!output_file_mmapped.Initialize(output_abs_path_)) |
| 97 return ComponentUnpacker::kDeltaVerificationFailure; | 97 return component_updater::kDeltaVerificationFailure; |
| 98 | 98 |
| 99 uint8 actual_hash[crypto::kSHA256Length] = {0}; | 99 uint8 actual_hash[crypto::kSHA256Length] = {0}; |
| 100 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); | 100 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); |
| 101 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); | 101 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); |
| 102 hasher->Finish(actual_hash, sizeof(actual_hash)); | 102 hasher->Finish(actual_hash, sizeof(actual_hash)); |
| 103 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) | 103 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) |
| 104 return ComponentUnpacker::kDeltaVerificationFailure; | 104 return component_updater::kDeltaVerificationFailure; |
| 105 | 105 |
| 106 return ComponentUnpacker::kNone; | 106 return component_updater::kNone; |
| 107 } | 107 } |
| 108 | 108 |
| 109 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {} | 109 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {} |
| 110 | 110 |
| 111 ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments( | 111 component_updater::Error DeltaUpdateOpCopy::DoParseArguments( |
| 112 base::DictionaryValue* command_args, | 112 base::DictionaryValue* command_args, |
| 113 const base::FilePath& input_dir, | 113 const base::FilePath& input_dir, |
| 114 ComponentInstaller* installer) { | 114 ComponentInstaller* installer) { |
| 115 std::string input_rel_path; | 115 std::string input_rel_path; |
| 116 if (!command_args->GetString(kInput, &input_rel_path)) | 116 if (!command_args->GetString(kInput, &input_rel_path)) |
| 117 return ComponentUnpacker::kDeltaBadCommands; | 117 return component_updater::kDeltaBadCommands; |
| 118 | 118 |
| 119 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | 119 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 120 return ComponentUnpacker::kDeltaMissingExistingFile; | 120 return component_updater::kDeltaMissingExistingFile; |
| 121 | 121 |
| 122 return ComponentUnpacker::kNone; | 122 return component_updater::kNone; |
| 123 } | 123 } |
| 124 | 124 |
| 125 ComponentUnpacker::Error DeltaUpdateOpCopy::DoRun(ComponentPatcher*, | 125 component_updater::Error DeltaUpdateOpCopy::DoRun(ComponentPatcher*, |
| 126 int* error) { | 126 int* error) { |
| 127 *error = 0; | 127 *error = 0; |
| 128 if (!base::CopyFile(input_abs_path_, output_abs_path_)) | 128 if (!base::CopyFile(input_abs_path_, output_abs_path_)) |
| 129 return ComponentUnpacker::kDeltaOperationFailure; | 129 return component_updater::kDeltaOperationFailure; |
| 130 | 130 |
| 131 return ComponentUnpacker::kNone; | 131 return component_updater::kNone; |
| 132 } | 132 } |
| 133 | 133 |
| 134 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {} | 134 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {} |
| 135 | 135 |
| 136 ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments( | 136 component_updater::Error DeltaUpdateOpCreate::DoParseArguments( |
| 137 base::DictionaryValue* command_args, | 137 base::DictionaryValue* command_args, |
| 138 const base::FilePath& input_dir, | 138 const base::FilePath& input_dir, |
| 139 ComponentInstaller* installer) { | 139 ComponentInstaller* installer) { |
| 140 std::string patch_rel_path; | 140 std::string patch_rel_path; |
| 141 if (!command_args->GetString(kPatch, &patch_rel_path)) | 141 if (!command_args->GetString(kPatch, &patch_rel_path)) |
| 142 return ComponentUnpacker::kDeltaBadCommands; | 142 return component_updater::kDeltaBadCommands; |
| 143 | 143 |
| 144 patch_abs_path_ = input_dir.Append( | 144 patch_abs_path_ = input_dir.Append( |
| 145 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | 145 base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
| 146 | 146 |
| 147 return ComponentUnpacker::kNone; | 147 return component_updater::kNone; |
| 148 } | 148 } |
| 149 | 149 |
| 150 ComponentUnpacker::Error DeltaUpdateOpCreate::DoRun(ComponentPatcher*, | 150 component_updater::Error DeltaUpdateOpCreate::DoRun(ComponentPatcher*, |
| 151 int* error) { | 151 int* error) { |
| 152 *error = 0; | 152 *error = 0; |
| 153 if (!base::Move(patch_abs_path_, output_abs_path_)) | 153 if (!base::Move(patch_abs_path_, output_abs_path_)) |
| 154 return ComponentUnpacker::kDeltaOperationFailure; | 154 return component_updater::kDeltaOperationFailure; |
| 155 | 155 |
| 156 return ComponentUnpacker::kNone; | 156 return component_updater::kNone; |
| 157 } | 157 } |
| 158 | 158 |
| 159 DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {} | 159 DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {} |
| 160 | 160 |
| 161 ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoParseArguments( | 161 component_updater::Error DeltaUpdateOpPatchBsdiff::DoParseArguments( |
| 162 base::DictionaryValue* command_args, | 162 base::DictionaryValue* command_args, |
| 163 const base::FilePath& input_dir, | 163 const base::FilePath& input_dir, |
| 164 ComponentInstaller* installer) { | 164 ComponentInstaller* installer) { |
| 165 std::string patch_rel_path; | 165 std::string patch_rel_path; |
| 166 std::string input_rel_path; | 166 std::string input_rel_path; |
| 167 if (!command_args->GetString(kPatch, &patch_rel_path) || | 167 if (!command_args->GetString(kPatch, &patch_rel_path) || |
| 168 !command_args->GetString(kInput, &input_rel_path)) | 168 !command_args->GetString(kInput, &input_rel_path)) |
| 169 return ComponentUnpacker::kDeltaBadCommands; | 169 return component_updater::kDeltaBadCommands; |
| 170 | 170 |
| 171 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | 171 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 172 return ComponentUnpacker::kDeltaMissingExistingFile; | 172 return component_updater::kDeltaMissingExistingFile; |
| 173 | 173 |
| 174 patch_abs_path_ = input_dir.Append( | 174 patch_abs_path_ = input_dir.Append( |
| 175 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | 175 base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
| 176 | 176 |
| 177 return ComponentUnpacker::kNone; | 177 return component_updater::kNone; |
| 178 } | 178 } |
| 179 | 179 |
| 180 ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoRun( | 180 component_updater::Error DeltaUpdateOpPatchBsdiff::DoRun( |
| 181 ComponentPatcher* patcher, | 181 ComponentPatcher* patcher, |
| 182 int* error) { | 182 int* error) { |
| 183 *error = 0; | 183 *error = 0; |
| 184 return patcher->Patch(ComponentPatcher::kPatchTypeBsdiff, | 184 return patcher->Patch(ComponentPatcher::kPatchTypeBsdiff, |
| 185 input_abs_path_, | 185 input_abs_path_, |
| 186 patch_abs_path_, | 186 patch_abs_path_, |
| 187 output_abs_path_, | 187 output_abs_path_, |
| 188 error); | 188 error); |
| 189 } | 189 } |
| 190 | 190 |
| 191 DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {} | 191 DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {} |
| 192 | 192 |
| 193 ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoParseArguments( | 193 component_updater::Error DeltaUpdateOpPatchCourgette::DoParseArguments( |
| 194 base::DictionaryValue* command_args, | 194 base::DictionaryValue* command_args, |
| 195 const base::FilePath& input_dir, | 195 const base::FilePath& input_dir, |
| 196 ComponentInstaller* installer) { | 196 ComponentInstaller* installer) { |
| 197 std::string patch_rel_path; | 197 std::string patch_rel_path; |
| 198 std::string input_rel_path; | 198 std::string input_rel_path; |
| 199 if (!command_args->GetString(kPatch, &patch_rel_path) || | 199 if (!command_args->GetString(kPatch, &patch_rel_path) || |
| 200 !command_args->GetString(kInput, &input_rel_path)) | 200 !command_args->GetString(kInput, &input_rel_path)) |
| 201 return ComponentUnpacker::kDeltaBadCommands; | 201 return component_updater::kDeltaBadCommands; |
| 202 | 202 |
| 203 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | 203 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
| 204 return ComponentUnpacker::kDeltaMissingExistingFile; | 204 return component_updater::kDeltaMissingExistingFile; |
| 205 | 205 |
| 206 patch_abs_path_ = input_dir.Append( | 206 patch_abs_path_ = input_dir.Append( |
| 207 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | 207 base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
| 208 | 208 |
| 209 return ComponentUnpacker::kNone; | 209 return component_updater::kNone; |
| 210 } | 210 } |
| 211 | 211 |
| 212 ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoRun( | 212 component_updater::Error DeltaUpdateOpPatchCourgette::DoRun( |
| 213 ComponentPatcher* patcher, | 213 ComponentPatcher* patcher, |
| 214 int* error) { | 214 int* error) { |
| 215 *error = 0; | 215 *error = 0; |
| 216 return patcher->Patch(ComponentPatcher::kPatchTypeCourgette, | 216 return patcher->Patch(ComponentPatcher::kPatchTypeCourgette, |
| 217 input_abs_path_, | 217 input_abs_path_, |
| 218 patch_abs_path_, | 218 patch_abs_path_, |
| 219 output_abs_path_, | 219 output_abs_path_, |
| 220 error); | 220 error); |
| 221 } | 221 } |
| 222 | 222 |
| OLD | NEW |