Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 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 | #ifndef COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ |
| 6 | #define COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 7 | |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 10 | #include "base/files/file.h" |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 12 | #include "components/zucchini/zucchini.h" |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 13 | |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 14 | // Zucchini integration interface to wrap core Zucchini library with file I/O. |
| 15 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 16 | namespace zucchini { |
| 17 | |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 18 | // Generates a patch to transform |old_file| to |new_file|, and writes the |
| 19 | // result to |patch_file|. Since this uses memory mapped files, crashes are |
| 20 | // expected in case of I/O errors. On Windows, |patch_file| is kept iff returned |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 21 | // code is kStatusSuccess or if |force_keep == true|, and is deleted otherwise. |
| 22 | // For UNIX systems the caller needs to do cleanup since it has ownership of the |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 23 | // base::File params, and Zucchini has no knowledge of which base::FilePath to |
| 24 | // delete. If |is_raw == true| then uses Raw Zucchini. If |imposed_matches| is |
| 25 | // non-empty, then overrides default element detection and matching heuristics |
| 26 | // with custom element matching encoded in |imposed_matches|, which should be |
| 27 | // formatted as: |
| 28 | // "#+#=#+#,#+#=#+#,..." (e.g., "1+2=3+4", "1+2=3+4,5+6=7+8"), |
| 29 | // where "#+#=#+#" encodes a match as 4 unsigned integers: |
| 30 | // [offset in "old", size in "old", offset in "new", size in "new"]. |
| 31 | status::Code Generate(base::File old_file, |
| 32 | base::File new_file, |
| 33 | base::File patch_file, |
| 34 | bool force_keep = false, |
| 35 | bool is_raw = false, |
| 36 | std::string imposed_matches = ""); |
| 37 | |
| 38 | // Alternative Generate() interface that takes base::FilePath as arguments. |
| 39 | // Performs proper cleanup in Windows and UNIX if failure occurs. |
| 40 | status::Code Generate(const base::FilePath& old_path, |
| 41 | const base::FilePath& new_path, |
| 42 | const base::FilePath& patch_path, |
| 43 | bool force_keep = false, |
| 44 | bool is_raw = false, |
| 45 | std::string imposed_matches = ""); |
| 46 | |
| 47 | // Applies the patch in |patch_file| to |old_file|, and writes the result to |
| 48 | // |new_file|. Since this uses memory mapped files, crashes are expected in case |
| 49 | // of I/O errors. On Windows, |new_file| is kept iff returned code is |
| 50 | // kStatusSuccess or if |force_keep == true|, and is deleted otherwise. For UNIX |
| 51 | // systems the caller needs to do cleanup since it has ownership of the |
| 52 | // base::File params, and Zucchini has no knowledge of which base::FilePath to |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 53 | // delete. |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 54 | status::Code Apply(base::File old_file, |
| 55 | base::File patch_file, |
| 56 | base::File new_file, |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 57 | bool force_keep = false); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 58 | |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 59 | // Alternative Apply() interface that takes base::FilePath as arguments. |
| 60 | // Performs proper cleanup in Windows and UNIX if failure occurs. |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 61 | status::Code Apply(const base::FilePath& old_path, |
| 62 | const base::FilePath& patch_path, |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 63 | const base::FilePath& new_path, |
| 64 | bool force_keep = false); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 65 | |
Etienne Pierre-doray | 559d77a | 2021-10-28 21:16:04 | [diff] [blame] | 66 | // Verifies the patch format in |patch_file| and returns |
| 67 | // Code::kStatusPatchReadError if the patch is malformed or version is |
| 68 | // unsupported. Since this uses memory mapped files, crashes are expected in |
| 69 | // case of I/O errors. |
| 70 | status::Code VerifyPatch(base::File patch_file); |
| 71 | |
| 72 | // Alternative VerifyPatch() interface that takes base::FilePath as arguments. |
| 73 | // Performs proper cleanup in Windows and UNIX if failure occurs. |
| 74 | status::Code VerifyPatch(const base::FilePath& patch_path); |
| 75 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 76 | } // namespace zucchini |
| 77 | |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 78 | #endif // COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ |