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 | |
| 5 | #include "chrome/installer/zucchini/zucchini_commands.h" |
| 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 <iostream> |
| 11 | #include <ostream> |
| 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" |
| 19 | #include "chrome/installer/zucchini/buffer_view.h" |
| 20 | #include "chrome/installer/zucchini/crc32.h" |
| 21 | #include "chrome/installer/zucchini/io_utils.h" |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 22 | #include "chrome/installer/zucchini/patch_reader.h" |
| 23 | #include "chrome/installer/zucchini/patch_writer.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 27 | /******** MappedFileReader ********/ |
| 28 | |
| 29 | // A file reader wrapper. |
| 30 | class MappedFileReader { |
| 31 | public: |
| 32 | explicit MappedFileReader(const base::FilePath& file_name) { |
| 33 | is_ok_ = buffer_.Initialize(file_name); |
| 34 | if (!is_ok_) // This is also triggered if |file_name| is an empty file. |
| 35 | LOG(ERROR) << "Can't read file: " << file_name.value(); |
| 36 | } |
| 37 | bool is_ok() const { return is_ok_; } |
| 38 | const uint8_t* data() const { return buffer_.data(); } |
| 39 | size_t length() const { return buffer_.length(); } |
| 40 | zucchini::ConstBufferView region() const { return {data(), length()}; } |
| 41 | |
| 42 | private: |
| 43 | bool is_ok_; |
| 44 | base::MemoryMappedFile buffer_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(MappedFileReader); |
| 47 | }; |
| 48 | |
| 49 | /******** MappedFileWriter ********/ |
| 50 | |
| 51 | // A file writer wrapper. |
| 52 | class MappedFileWriter { |
| 53 | public: |
| 54 | MappedFileWriter(const base::FilePath& file_name, size_t length) { |
| 55 | using base::File; |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 56 | is_ok_ = buffer_.Initialize( |
| 57 | File(file_name, |
| 58 | File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE), |
| 59 | {0, static_cast<int64_t>(length)}, |
| 60 | base::MemoryMappedFile::READ_WRITE_EXTEND); |
| 61 | if (!is_ok_) |
| 62 | LOG(ERROR) << "Can't create file: " << file_name.value(); |
| 63 | } |
| 64 | bool is_ok() const { return is_ok_; } |
| 65 | uint8_t* data() { return buffer_.data(); } |
| 66 | size_t length() const { return buffer_.length(); } |
| 67 | zucchini::MutableBufferView region() { return {data(), length()}; } |
| 68 | |
| 69 | private: |
| 70 | bool is_ok_; |
| 71 | base::MemoryMappedFile buffer_; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(MappedFileWriter); |
| 74 | }; |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 75 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 76 | /******** Command-line Switches ********/ |
| 77 | |
| 78 | const char kSwitchRaw[] = "raw"; |
| 79 | |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 80 | } // namespace |
| 81 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 82 | zucchini::status::Code MainGen(MainParams params) { |
| 83 | CHECK_EQ(3U, params.file_paths.size()); |
| 84 | MappedFileReader old_image(params.file_paths[0]); |
| 85 | if (!old_image.is_ok()) |
| 86 | return zucchini::status::kStatusFileReadError; |
| 87 | MappedFileReader new_image(params.file_paths[1]); |
| 88 | if (!new_image.is_ok()) |
| 89 | return zucchini::status::kStatusFileReadError; |
| 90 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 91 | zucchini::EnsemblePatchWriter patch_writer(old_image.region(), |
| 92 | new_image.region()); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 93 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [
|