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 #ifndef COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 6 #define COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 7 8 #include <string> 9 10 #include "base/files/file.h" 11 #include "base/files/file_path.h" 12 #include "components/zucchini/zucchini.h" 13 14 // Zucchini integration interface to wrap core Zucchini library with file I/O. 15 16 namespace zucchini { 17 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 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 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 53 // delete. 54 status::Code Apply(base::File old_file, 55 base::File patch_file, 56 base::File new_file, 57 bool force_keep = false); 58 59 // Alternative Apply() interface that takes base::FilePath as arguments. 60 // Performs proper cleanup in Windows and UNIX if failure occurs. 61 status::Code Apply(const base::FilePath& old_path, 62 const base::FilePath& patch_path, 63 const base::FilePath& new_path, 64 bool force_keep = false); 65 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 76 } // namespace zucchini 77 78 #endif // COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 79