| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <vector> | 5 #include <vector> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 va_end(args); | 45 va_end(args); |
| 46 exit(1); | 46 exit(1); |
| 47 } | 47 } |
| 48 | 48 |
| 49 std::string ReadOrFail(const std::wstring& file_name, const char* kind) { | 49 std::string ReadOrFail(const std::wstring& file_name, const char* kind) { |
| 50 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
| 51 FilePath file_path(file_name); | 51 FilePath file_path(file_name); |
| 52 #else | 52 #else |
| 53 FilePath file_path(WideToASCII(file_name)); | 53 FilePath file_path(WideToASCII(file_name)); |
| 54 #endif | 54 #endif |
| 55 int64 file_size = 0; |
| 56 if (!file_util::GetFileSize(file_path, &file_size)) |
| 57 Problem("Can't read %s file.", kind); |
| 55 std::string buffer; | 58 std::string buffer; |
| 59 buffer.reserve(static_cast<size_t>(file_size)); |
| 56 if (!file_util::ReadFileToString(file_path, &buffer)) | 60 if (!file_util::ReadFileToString(file_path, &buffer)) |
| 57 Problem("Can't read %s file.", kind); | 61 Problem("Can't read %s file.", kind); |
| 58 return buffer; | 62 return buffer; |
| 59 } | 63 } |
| 60 | 64 |
| 61 void WriteSinkToFile(const courgette::SinkStream *sink, | 65 void WriteSinkToFile(const courgette::SinkStream *sink, |
| 62 const std::wstring& output_file) { | 66 const std::wstring& output_file) { |
| 63 #if defined(OS_WIN) | 67 #if defined(OS_WIN) |
| 64 FilePath output_path(output_file); | 68 FilePath output_path(output_file); |
| 65 #else | 69 #else |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 courgette::GenerateEnsemblePatch(&old_stream, &new_stream, &patch_stream); | 282 courgette::GenerateEnsemblePatch(&old_stream, &new_stream, &patch_stream); |
| 279 | 283 |
| 280 if (status != courgette::C_OK) Problem("-gen failed."); | 284 if (status != courgette::C_OK) Problem("-gen failed."); |
| 281 | 285 |
| 282 WriteSinkToFile(&patch_stream, patch_file); | 286 WriteSinkToFile(&patch_stream, patch_file); |
| 283 } | 287 } |
| 284 | 288 |
| 285 void ApplyEnsemblePatch(const std::wstring& old_file, | 289 void ApplyEnsemblePatch(const std::wstring& old_file, |
| 286 const std::wstring& patch_file, | 290 const std::wstring& patch_file, |
| 287 const std::wstring& new_file) { | 291 const std::wstring& new_file) { |
| 292 // We do things a little differently here in order to call the same Courgette |
| 293 // entry point as the installer. That entry point point takes file names and |
| 294 // returns an status code but does not output any diagnostics. |
| 295 #if defined(OS_WIN) |
| 296 FilePath old_path(old_file); |
| 297 FilePath patch_path(patch_file); |
| 298 FilePath new_path(new_file); |
| 299 #else |
| 300 FilePath old_path(WideToASCII(old_file)); |
| 301 FilePath patch_path(WideToASCII(patch_file)); |
| 302 FilePath new_path(WideToASCII(new_file)); |
| 303 #endif |
| 304 |
| 305 courgette::Status status = |
| 306 courgette::ApplyEnsemblePatch(old_path.value().c_str(), |
| 307 patch_path.value().c_str(), |
| 308 new_path.value().c_str()); |
| 309 |
| 310 if (status == courgette::C_OK) |
| 311 return; |
| 312 |
| 313 // Diagnose the error. |
| 314 if (status == courgette::C_BAD_ENSEMBLE_MAGIC) |
| 315 Problem("Not a courgette patch"); |
| 316 if (status == courgette::C_BAD_ENSEMBLE_VERSION) |
| 317 Problem("Wrong version patch"); |
| 318 if (status == courgette::C_BAD_ENSEMBLE_HEADER) |
| 319 Problem("Corrupt patch"); |
| 320 // If we failed due to a missing input file, this will |
| 321 // print the message. |
| 288 std::string old_buffer = ReadOrFail(old_file, "'old' input"); | 322 std::string old_buffer = ReadOrFail(old_file, "'old' input"); |
| 323 old_buffer.clear(); |
| 289 std::string patch_buffer = ReadOrFail(patch_file, "'patch' input"); | 324 std::string patch_buffer = ReadOrFail(patch_file, "'patch' input"); |
| 325 patch_buffer.clear(); |
| 290 | 326 |
| 291 courgette::SourceStream old_stream; | 327 // Non-input related errors: |
| 292 courgette::SourceStream patch_stream; | 328 if (status == courgette::C_WRITE_OPEN_ERROR) |
| 293 old_stream.Init(old_buffer); | 329 Problem("Can't open output"); |
| 294 patch_stream.Init(patch_buffer); | 330 if (status == courgette::C_WRITE_ERROR) |
| 295 courgette::SinkStream new_stream; | 331 Problem("Can't write output"); |
| 296 courgette::Status status = | |
| 297 courgette::ApplyEnsemblePatch(&old_stream, &patch_stream, &new_stream); | |
| 298 | 332 |
| 299 if (status != courgette::C_OK) Problem("-apply failed."); | 333 Problem("-apply failed."); |
| 300 | |
| 301 WriteSinkToFile(&new_stream, new_file); | |
| 302 } | 334 } |
| 303 | 335 |
| 304 void GenerateBSDiffPatch(const std::wstring& old_file, | 336 void GenerateBSDiffPatch(const std::wstring& old_file, |
| 305 const std::wstring& new_file, | 337 const std::wstring& new_file, |
| 306 const std::wstring& patch_file) { | 338 const std::wstring& patch_file) { |
| 307 std::string old_buffer = ReadOrFail(old_file, "'old' input"); | 339 std::string old_buffer = ReadOrFail(old_file, "'old' input"); |
| 308 std::string new_buffer = ReadOrFail(new_file, "'new' input"); | 340 std::string new_buffer = ReadOrFail(new_file, "'new' input"); |
| 309 | 341 |
| 310 courgette::SourceStream old_stream; | 342 courgette::SourceStream old_stream; |
| 311 courgette::SourceStream new_stream; | 343 courgette::SourceStream new_stream; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } else if (cmd_spread_1_adjusted || cmd_spread_1_unadjusted) { | 454 } else if (cmd_spread_1_adjusted || cmd_spread_1_unadjusted) { |
| 423 if (values.size() != 3) | 455 if (values.size() != 3) |
| 424 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); | 456 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); |
| 425 DisassembleAdjustDiff(values[0], values[1], values[2], | 457 DisassembleAdjustDiff(values[0], values[1], values[2], |
| 426 cmd_spread_1_adjusted); | 458 cmd_spread_1_adjusted); |
| 427 } else { | 459 } else { |
| 428 UsageProblem("No operation specified"); | 460 UsageProblem("No operation specified"); |
| 429 } | 461 } |
| 430 } | 462 } |
| 431 } | 463 } |
| OLD | NEW |