| 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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(const base::FilePath::CharType* old_file_name, |
| 375 const base::FilePath::CharType* patch_file_name, | 375 const base::FilePath::CharType* patch_file_name, |
| 376 const base::FilePath::CharType* new_file_name) { | 376 const base::FilePath::CharType* new_file_name) { |
| 377 base::FilePath patch_file_path(patch_file_name); | 377 return ApplyEnsemblePatch( |
| 378 base::MemoryMappedFile patch_file; | 378 base::File( |
| 379 if (!patch_file.Initialize(patch_file_path)) | 379 base::FilePath(old_file_name), |
| 380 base::File::FLAG_OPEN | base::File::FLAG_READ), |
| 381 base::File( |
| 382 base::FilePath(patch_file_name), |
| 383 base::File::FLAG_OPEN | base::File::FLAG_READ), |
| 384 base::File( |
| 385 base::FilePath(new_file_name), |
| 386 base::File::FLAG_CREATE_ALWAYS | |
| 387 base::File::FLAG_WRITE | |
| 388 base::File::FLAG_EXCLUSIVE_WRITE)); |
| 389 } |
| 390 |
| 391 Status ApplyEnsemblePatch(base::File old_file, |
| 392 base::File patch_file, |
| 393 base::File new_file) { |
| 394 base::MemoryMappedFile patch_file_mem; |
| 395 if (!patch_file_mem.Initialize(std::move(patch_file))) |
| 380 return C_READ_OPEN_ERROR; | 396 return C_READ_OPEN_ERROR; |
| 381 | 397 |
| 382 // 'Dry-run' the first step of the patch process to validate format of header. | 398 // 'Dry-run' the first step of the patch process to validate format of header. |
| 383 SourceStream patch_header_stream; | 399 SourceStream patch_header_stream; |
| 384 patch_header_stream.Init(patch_file.data(), patch_file.length()); | 400 patch_header_stream.Init(patch_file_mem.data(), patch_file_mem.length()); |
| 385 EnsemblePatchApplication patch_process; | 401 EnsemblePatchApplication patch_process; |
| 386 Status status = patch_process.ReadHeader(&patch_header_stream); | 402 Status status = patch_process.ReadHeader(&patch_header_stream); |
| 387 if (status != C_OK) | 403 if (status != C_OK) |
| 388 return status; | 404 return status; |
| 389 | 405 |
| 390 // Read the old_file. | 406 // Read the old_file. |
| 391 base::FilePath old_file_path(old_file_name); | 407 base::MemoryMappedFile old_file_mem; |
| 392 base::MemoryMappedFile old_file; | 408 if (!old_file_mem.Initialize(std::move(old_file))) |
| 393 if (!old_file.Initialize(old_file_path)) | |
| 394 return C_READ_ERROR; | 409 return C_READ_ERROR; |
| 395 | 410 |
| 396 // Apply patch on streams. | 411 // Apply patch on streams. |
| 397 SourceStream old_source_stream; | 412 SourceStream old_source_stream; |
| 398 SourceStream patch_source_stream; | 413 SourceStream patch_source_stream; |
| 399 old_source_stream.Init(old_file.data(), old_file.length()); | 414 old_source_stream.Init(old_file_mem.data(), old_file_mem.length()); |
| 400 patch_source_stream.Init(patch_file.data(), patch_file.length()); | 415 patch_source_stream.Init(patch_file_mem.data(), patch_file_mem.length()); |
| 401 SinkStream new_sink_stream; | 416 SinkStream new_sink_stream; |
| 402 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream, | 417 status = ApplyEnsemblePatch(&old_source_stream, &patch_source_stream, |
| 403 &new_sink_stream); | 418 &new_sink_stream); |
| 404 if (status != C_OK) | 419 if (status != C_OK) |
| 405 return status; | 420 return status; |
| 406 | 421 |
| 407 // Write the patched data to |new_file_name|. | 422 // Write the patched data to |new_file_name|. |
| 408 base::FilePath new_file_path(new_file_name); | 423 int written = new_file.Write( |
| 409 int written = | 424 0, |
| 410 base::WriteFile( | 425 reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
| 411 new_file_path, | 426 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) | 427 if (written == -1) |
| 415 return C_WRITE_OPEN_ERROR; | 428 return C_WRITE_OPEN_ERROR; |
| 416 if (static_cast<size_t>(written) != new_sink_stream.Length()) | 429 if (static_cast<size_t>(written) != new_sink_stream.Length()) |
| 417 return C_WRITE_ERROR; | 430 return C_WRITE_ERROR; |
| 418 | 431 |
| 419 return C_OK; | 432 return C_OK; |
| 420 } | 433 } |
| 421 | 434 |
| 422 } // namespace | 435 } // namespace courgette |
| OLD | NEW |