Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This file contains the code to apply a Courgette patch. | 5 // This file contains the code to apply a Courgette patch. |
| 6 | 6 |
| 7 #include "courgette/ensemble.h" | 7 #include "courgette/ensemble.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file.h" |
| 16 #include "base/files/memory_mapped_file.h" | 16 #include "base/files/memory_mapped_file.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "courgette/crc.h" | 19 #include "courgette/crc.h" |
| 20 #include "courgette/patcher_x86_32.h" | 20 #include "courgette/patcher_x86_32.h" |
| 21 #include "courgette/region.h" | 21 #include "courgette/region.h" |
| 22 #include "courgette/simple_delta.h" | 22 #include "courgette/simple_delta.h" |
| 23 #include "courgette/streams.h" | 23 #include "courgette/streams.h" |
| 24 | 24 |
| 25 namespace courgette { | 25 namespace courgette { |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 SourceStream final_patch_prediction; | 364 SourceStream final_patch_prediction; |
| 365 final_patch_prediction.Init(original_ensemble_and_corrected_base_elements); | 365 final_patch_prediction.Init(original_ensemble_and_corrected_base_elements); |
| 366 status = patch_process.SubpatchFinalOutput(&final_patch_prediction, | 366 status = patch_process.SubpatchFinalOutput(&final_patch_prediction, |
| 367 ensemble_correction, output); | 367 ensemble_correction, output); |
| 368 if (status != C_OK) | 368 if (status != C_OK) |
| 369 return status; | 369 return status; |
| 370 | 370 |
| 371 return C_OK; | 371 return C_OK; |
| 372 } | 372 } |
| 373 | 373 |
| 374 Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name, | 374 Status ApplyEnsemblePatch(base::File old_file, |
| 375 const base::FilePath::CharType* patch_file_name, | 375 base::File patch_file, |
| 376 const base::FilePath::CharType* new_file_name) { | 376 base::File new_file) { |
| 377 base::FilePath patch_file_path(patch_file_name); | 377 base::MemoryMappedFile patch_file_mem; |
| 378 base::MemoryMappedFile patch_file; | 378 if (!patch_file_mem.Initialize(std::move(patch_file))) |
| 379 if (!patch_file.Initialize(patch_file_path)) | |
| 380 return C_READ_OPEN_ERROR; | 379 return C_READ_OPEN_ERROR; |
| 381 | 380 |
| 382 // 'Dry-run' the first step of the patch process to validate format of header. | 381 // 'Dry-run' the first step of the patch process to validate format of header. |
| 383 SourceStream patch_header_stream; | 382 SourceStream patch_header_stream; |
| 384 patch_header_stream.Init(patch_file.data(), patch_file.length()); | 383 patch_header_stream.Init(patch_file_mem.data(), patch_file_mem.length()); |
| 385 EnsemblePatchApplication patch_process; | 384 EnsemblePatchApplication patch_process; |
| 386 Status status = patch_process.ReadHeader(&patch_header_stream); | 385 Status status = patch_process.ReadHeader(&patch_header_stream); |
| 387 if (status != C_OK) | 386 if (status != C_OK) |
| 388 return status; | 387 return status; |
| 389 | 388 |
| 390 // Read the old_file. | 389 // Read the old_file. |
| 391 base::FilePath old_file_path(old_file_name); | 390 base::MemoryMappedFile old_file_mem; |
| 392 base::MemoryMappedFile old_file; | 391 if (!old_file_mem.Initialize(std::move(old_file))) |
| 393 if (!old_file.Initialize(old_file_path)) | |
| 394 return C_READ_ERROR; | 392 return C_READ_ERROR; |
| 395 | 393 |
| 396 // Apply patch on streams. | 394 // Apply patch on streams. |
| 397 SourceStream old_source_stream; | 395 SourceStream old_source_stream; |
| 398 SourceStream patch_source_stream; | 396 SourceStream patch_source_stream; |
| 399 old_source_stream.Init(old_file.data(), old_file.length()); | 397 old_source_stream.Init(old_file_mem.data(), old_file_mem.length()); |
| 400 patch_source_stream.Init(patch_file.data(), patch_file.length()); | 398 patch_source_stream.Init(patch_file_mem.data(), patch_file_mem.length()); |
| 401 SinkStream new_sink_stream; | 399 SinkStream new_sink_stream; |
| 402 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream, | 400 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream, |
| 403 &new_sink_stream); | 401 &new_sink_stream); |
| 404 if (status != C_OK) | 402 if (status != C_OK) |
| 405 return status; | 403 return status; |
| 406 | 404 |
| 407 // Write the patched data to |new_file_name|. | 405 // Write the patched data to |new_file_name|. |
| 408 base::FilePath new_file_path(new_file_name); | 406 int written = new_file.Write( |
| 409 int written = | 407 0, |
| 410 base::WriteFile( | 408 reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
| 411 new_file_path, | 409 static_cast<int>(new_sink_stream.Length())); |
| 412 reinterpret_cast<const char*>(new_sink_stream.Buffer()), | |
| 413 static_cast<int>(new_sink_stream.Length())); | |
| 414 if (written == -1) | 410 if (written == -1) |
| 415 return C_WRITE_OPEN_ERROR; | 411 return C_WRITE_OPEN_ERROR; |
| 416 if (static_cast<size_t>(written) != new_sink_stream.Length()) | 412 if (static_cast<size_t>(written) != new_sink_stream.Length()) |
| 417 return C_WRITE_ERROR; | 413 return C_WRITE_ERROR; |
| 418 | 414 |
| 419 return C_OK; | 415 return C_OK; |
| 420 } | 416 } |
| 421 | 417 |
| 422 } // namespace | 418 Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name, |
| 419 const base::FilePath::CharType* patch_file_name, | |
| 420 const base::FilePath::CharType* new_file_name) { | |
| 421 return ApplyEnsemblePatch( | |
| 422 base::File( | |
| 423 base::FilePath(old_file_name), | |
| 424 base::File::FLAG_OPEN | base::File::FLAG_READ), | |
| 425 base::File( | |
| 426 base::FilePath(patch_file_name), | |
| 427 base::File::FLAG_OPEN | base::File::FLAG_READ), | |
| 428 base::File( | |
| 429 base::FilePath(new_file_name), | |
| 430 base::File::FLAG_CREATE_ALWAYS | | |
| 431 base::File::FLAG_WRITE | | |
| 432 base::File::FLAG_EXCLUSIVE_WRITE)); | |
| 433 } | |
|
grt (UTC plus 2)
2016/12/07 10:14:42
is there a reason not to delete the new file in ca
waffles
2016/12/09 18:27:46
You're so smart, Greg. (Done.)
| |
| 434 | |
| 435 } // namespace courgette | |
| OLD | NEW |