blob: 2b6287b389e11aee09e0951a3ed6b3d076606cc4 [file] [log] [blame]
Samuel Huang06f1ae92018-03-13 18:19:34 +00001// 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
Samuel Huangf35146e2018-06-21 15:50:22 +00008#include <string>
9
Samuel Huang06f1ae92018-03-13 18:19:34 +000010#include "base/files/file.h"
11#include "base/files/file_path.h"
12#include "components/zucchini/zucchini.h"
13
Samuel Huangf35146e2018-06-21 15:50:22 +000014// Zucchini integration interface to wrap core Zucchini library with file I/O.
15
Samuel Huang06f1ae92018-03-13 18:19:34 +000016namespace zucchini {
17
Samuel Huangf35146e2018-06-21 15:50:22 +000018// 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 Huang1bb3b652018-03-21 18:54:03 +000021// 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 Huangf35146e2018-06-21 15:50:22 +000023// 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"].
31status::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.
40status::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 Huang1bb3b652018-03-21 18:54:03 +000053// delete.
Samuel Huangf35146e2018-06-21 15:50:22 +000054status::Code Apply(base::File old_file,
55 base::File patch_file,
56 base::File new_file,
Samuel Huang1bb3b652018-03-21 18:54:03 +000057 bool force_keep = false);
Samuel Huang06f1ae92018-03-13 18:19:34 +000058
Samuel Huangf35146e2018-06-21 15:50:22 +000059// Alternative Apply() interface that takes base::FilePath as arguments.
60// Performs proper cleanup in Windows and UNIX if failure occurs.
Samuel Huang06f1ae92018-03-13 18:19:34 +000061status::Code Apply(const base::FilePath& old_path,
62 const base::FilePath& patch_path,
Samuel Huang1bb3b652018-03-21 18:54:03 +000063 const base::FilePath& new_path,
64 bool force_keep = false);
Samuel Huang06f1ae92018-03-13 18:19:34 +000065
Etienne Pierre-dorayb90a9472021-10-28 21:16:04 +000066// 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.
70status::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.
74status::Code VerifyPatch(const base::FilePath& patch_path);
75
Samuel Huang06f1ae92018-03-13 18:19:34 +000076} // namespace zucchini
77
78#endif // COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_