huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 5 | #include "components/zucchini/zucchini_commands.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 6 | |
Samuel Huang | e8d07b75 | 2017-08-21 16:05:25 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 10 | #include <ostream> |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 11 | #include <utility> |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 12 | |
| 13 | #include "base/command_line.h" |
| 14 | #include "base/files/file.h" |
| 15 | #include "base/files/file_path.h" |
| 16 | #include "base/files/memory_mapped_file.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 17 | #include "base/logging.h" |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 18 | #include "base/macros.h" |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 19 | #include "components/zucchini/buffer_view.h" |
| 20 | #include "components/zucchini/crc32.h" |
| 21 | #include "components/zucchini/io_utils.h" |
| 22 | #include "components/zucchini/mapped_file.h" |
| 23 | #include "components/zucchini/patch_writer.h" |
| 24 | #include "components/zucchini/zucchini_integration.h" |
| 25 | #include "components/zucchini/zucchini_tools.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 29 | /******** Command-line Switches ********/ |
| 30 | |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 31 | constexpr char kSwitchDump[] = "dump"; |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame^] | 32 | constexpr char kSwitchKeep[] = "keep"; |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 33 | constexpr char kSwitchRaw[] = "raw"; |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 34 | |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 35 | } // namespace |
| 36 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 37 | zucchini::status::Code MainGen(MainParams params) { |
| 38 | CHECK_EQ(3U, params.file_paths.size()); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 39 | |
| 40 | // TODO(huangs): Move implementation to zucchini_integration.cc. |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 41 | using base::File; |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 42 | File old_file(params.file_paths[0], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 43 | zucchini::MappedFileReader old_image(std::move(old_file)); |
| 44 | if (old_image.HasError()) { |
| 45 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 46 | << old_image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 47 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 48 | } |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 49 | File new_file(params.file_paths[1], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 50 | zucchini::MappedFileReader new_image(std::move(new_file)); |
| 51 | if (new_image.HasError()) { |
| 52 | LOG(ERROR) << "Error with file " << params.file_paths[1].value() << ": " |
| 53 | << new_image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 54 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 55 | } |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 56 | zucchini::EnsemblePatchWriter patch_writer(old_image.region(), |
| 57 | new_image.region()); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 58 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 59 | auto generate = params.command_line.HasSwitch(kSwitchRaw) |
| 60 | ? zucchini::GenerateRaw |
| 61 | : zucchini::GenerateEnsemble; |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 62 | zucchini::status::Code result = |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 63 | generate(old_image.region(), new_image.region(), &patch_writer); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 64 | if (result != zucchini::status::kStatusSuccess) { |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 65 | params.out << "Fatal error encountered when generating patch." << std::endl; |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 66 | return result; |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 67 | } |
| 68 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 69 | // By default, delete patch on destruction, to avoid having lingering files in |
| 70 | // case of a failure. On Windows deletion can be done by the OS. |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 71 | File patch_file(params.file_paths[2], File::FLAG_CREATE_ALWAYS | |
| 72 | File::FLAG_READ | File::FLAG_WRITE | |
| 73 | File::FLAG_SHARE_DELETE | |
| 74 | File::FLAG_CAN_DELETE_ON_CLOSE); |
| 75 | zucchini::MappedFileWriter patch(params.file_paths[2], std::move(patch_file), |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 76 | patch_writer.SerializedSize()); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 77 | if (patch.HasError()) { |
| 78 | LOG(ERROR) << "Error with file " << params.file_paths[2].value() << ": " |
| 79 | << patch.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 80 | return zucchini::status::kStatusFileWriteError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 81 | } |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 82 | |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame^] | 83 | if (params.command_line.HasSwitch(kSwitchKeep)) |
| 84 | patch.Keep(); |
| 85 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 86 | if (!patch_writer.SerializeInto(patch.region())) |
| 87 | return zucchini::status::kStatusPatchWriteError; |
| 88 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 89 | // Successfully created patch. Explicitly request file to be kept. |
| 90 | if (!patch.Keep()) |
| 91 | return zucchini::status::kStatusFileWriteError; |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 92 | return zucchini::status::kStatusSuccess; |
| 93 | } |
| 94 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 95 | zucchini::status::Code MainApply(MainParams params) { |
| 96 | CHECK_EQ(3U, params.file_paths.size()); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 97 | return zucchini::Apply(params.file_paths[0], params.file_paths[1], |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame^] | 98 | params.file_paths[2], |
| 99 | params.command_line.HasSwitch(kSwitchKeep)); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | zucchini::status::Code MainRead(MainParams params) { |
| 103 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 104 | base::File input_file(params.file_paths[0], |
| 105 | base::File::FLAG_OPEN | base::File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 106 | zucchini::MappedFileReader input(std::move(input_file)); |
| 107 | if (input.HasError()) { |
| 108 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 109 | << input.error(); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 110 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 111 | } |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 112 | |
| 113 | bool do_dump = params.command_line.HasSwitch(kSwitchDump); |
| 114 | zucchini::status::Code status = zucchini::ReadReferences( |
| 115 | {input.data(), input.length()}, do_dump, params.out); |
| 116 | if (status != zucchini::status::kStatusSuccess) |
| 117 | params.err << "Fatal error found when dumping references." << std::endl; |
| 118 | return status; |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 119 | } |
| 120 | |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 121 | zucchini::status::Code MainDetect(MainParams params) { |
| 122 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 123 | base::File input_file(params.file_paths[0], |
| 124 | base::File::FLAG_OPEN | base::File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 125 | zucchini::MappedFileReader input(std::move(input_file)); |
| 126 | if (input.HasError()) { |
| 127 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 128 | << input.error(); |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 129 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 130 | } |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 131 | |
| 132 | std::vector<zucchini::ConstBufferView> sub_image_list; |
| 133 | zucchini::status::Code result = zucchini::DetectAll( |
| 134 | {input.data(), input.length()}, params.out, &sub_image_list); |
| 135 | if (result != zucchini::status::kStatusSuccess) |
| 136 | params.err << "Fatal error found when detecting executables." << std::endl; |
| 137 | return result; |
| 138 | } |
| 139 | |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 140 | zucchini::status::Code MainMatch(MainParams params) { |
| 141 | CHECK_EQ(2U, params.file_paths.size()); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 142 | using base::File; |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 143 | File old_file(params.file_paths[0], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [
|