aboutsummaryrefslogtreecommitdiff
path: root/zucchini_integration.cc
blob: c654a08735a4f83474d590bbb3b2e53d86aec7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/zucchini/zucchini_integration.h"

#include <utility>

#include "base/logging.h"
#include "components/zucchini/buffer_view.h"
#include "components/zucchini/mapped_file.h"
#include "components/zucchini/patch_reader.h"

namespace zucchini {

namespace {

struct FileNames {
  FileNames() : is_dummy(true) {
    // Use fake names.
    old_name = old_name.AppendASCII("old_name");
    new_name = new_name.AppendASCII("new_name");
    patch_name = patch_name.AppendASCII("patch_name");
  }

  FileNames(const base::FilePath& old_name,
            const base::FilePath& new_name,
            const base::FilePath& patch_name)
      : old_name(old_name),
        new_name(new_name),
        patch_name(patch_name),
        is_dummy(false) {}

  base::FilePath old_name;
  base::FilePath new_name;
  base::FilePath patch_name;

  // A flag to decide whether the filenames are only for error output.
  const bool is_dummy;
};

status::Code GenerateCommon(base::File old_file,
                            base::File new_file,
                            base::File patch_file,
                            const FileNames& names,
                            bool force_keep,
                            bool is_raw,
                            std::string imposed_matches) {
  MappedFileReader mapped_old(std::move(old_file));
  if (mapped_old.HasError()) {
    LOG(ERROR) << "Error with file " << names.old_name.value() << ": "
               << mapped_old.error();
    return status::kStatusFileReadError;
  }

  MappedFileReader mapped_new(std::move(new_file));
  if (mapped_new.HasError()) {
    LOG(ERROR) << "Error with file " << names.new_name.value() << ": "
               << mapped_new.error();
    return status::kStatusFileReadError;
  }

  status::Code result = status::kStatusSuccess;
  EnsemblePatchWriter patch_writer(mapped_old.region(), mapped_new.region());
  if (is_raw) {
    result = GenerateBufferRaw(mapped_old.region(), mapped_new.region(),
                               &patch_writer);
  } else {
    result = GenerateBufferImposed(mapped_old.region(), mapped_new.region(),
                                   std::move(imposed_matches), &patch_writer);
  }
  if (result != status::kStatusSuccess) {
    LOG(ERROR) << "Fatal error encountered when generating patch.";
    return result;
  }

  // By default, delete patch on destruction, to avoid having lingering files in
  // case of a failure. On Windows deletion can be done by the OS.
  MappedFileWriter mapped_patch(names.patch_name, std::move(patch_file),
                                patch_writer.SerializedSize());
  if (mapped_patch.HasError()) {
    LOG(ERROR) << "Error with file " << names.patch_name.value() << ": "
               << mapped_patch.error();
    return status::kStatusFileWriteError;
  }
  if (force_keep)
    mapped_patch.Keep();

  if (!patch_writer.SerializeInto(mapped_patch.region()))
    return status::kStatusPatchWriteError;

  // Successfully created patch. Explicitly request file to be kept.
  if (!mapped_patch.Keep())
    return status::kStatusFileWriteError;
  return status::kStatusSuccess;
}

status::Code ApplyCommon(base::File old_file,
                         base::File patch_file,
                         base::File new_file,
                         const FileNames& names,
                         bool force_keep) {
  MappedFileReader mapped_patch(std::move(patch_file));
  if (mapped_patch.HasError()) {
    LOG(ERROR) << "Error with file " << names.patch_name.value() << ": "
               << mapped_patch.error();
    return status::kStatusFileReadError;
  }

  auto patch_reader = EnsemblePatchReader::Create(mapped_patch.region());
  if (!patch_reader.has_value()) {
    LOG(ERROR) << "Error reading patch header.";
    return status::kStatusPatchReadError;
  }

  MappedFileReader mapped_old(std::move(old_file));
  if (mapped_old.HasError()) {
    LOG(ERROR) << "Error with file " << names.old_name.value() << ": "
               << mapped_old.error();
    return status::kStatusFileReadError;
  }

  PatchHeader header = patch_reader->header();
  // By default, delete output on destruction, to avoid having lingering files
  // in case of a failure. On Windows deletion can be done by the OS.
  MappedFileWriter mapped_new(names.new_name, std::move(new_file),
                              header.new_size);
  if (mapped_new.HasError()) {
    LOG(ERROR) << "Error with file " << names.new_name.value() << ": "
               << mapped_new.error();
    return status::kStatusFileWriteError;
  }
  if (force_keep)
    mapped_new.Keep();

  status::Code result =
      ApplyBuffer(mapped_old.region(), *patch_reader, mapped_new.region());
  if (result != status::kStatusSuccess) {
    LOG(ERROR) << "Fatal error encountered while applying patch.";
    return result;
  }

  // Successfully patch |mapped_new|. Explicitly request file to be kept.
  if (!mapped_new.Keep())
    return status::kStatusFileWriteError;
  return status::kStatusSuccess;
}

status::Code VerifyPatchCommon(base::File patch_file,
                               base::FilePath patch_name) {
  MappedFileReader mapped_patch(std::move(patch_file));
  if (mapped_patch.HasError()) {
    LOG(ERROR) << "Error with file " << patch_name.value() << ": "
               << mapped_patch.error();
    return status::kStatusFileReadError;
  }
  auto patch_reader = EnsemblePatchReader::Create(mapped_patch.region());
  if (!patch_reader.has_value()) {
    LOG(ERROR) << "Error reading patch header.";
    return status::kStatusPatchReadError;
  }
  return status::kStatusSuccess;
}

}  // namespace

status::Code Generate(base::File old_file,
                      base::File new_file,
                      base::File patch_file,
                      bool force_keep,
                      bool is_raw,
                      std::string imposed_matches) {
  const FileNames file_names;
  return GenerateCommon(std::move(old_file), std::move(new_file),
                        std::move(patch_file), file_names, force_keep, is_raw,
                        std::move(imposed_matches));
}

status::Code Generate(const base::FilePath& old_path,
                      const base::FilePath& new_path,
                      const base::FilePath& patch_path,
                      bool force_keep,
                      bool is_raw,
                      std::string imposed_matches) {
  using base::File;
  File old_file(old_path, File::FLAG_OPEN | File::FLAG_READ |
                              base::File::FLAG_WIN_SHARE_DELETE);
  File new_file(new_path, File::FLAG_OPEN | File::FLAG_READ |
                              base::File::FLAG_WIN_SHARE_DELETE);
  File patch_file(patch_path, File::FLAG_CREATE_ALWAYS | File::FLAG_READ |
                                  File::FLAG_WRITE |
                                  File::FLAG_WIN_SHARE_DELETE |
                                  File::FLAG_CAN_DELETE_ON_CLOSE);
  const FileNames file_names(old_path, new_path, patch_path);
  return GenerateCommon(std::move(old_file), std::move(new_file),
                        std::move(patch_file), file_names, force_keep, is_raw,
                        std::move(imposed_matches));
}

status::Code Apply(base::File old_file,
                   base::File patch_file,
                   base::File new_file,
                   bool force_keep) {
  const FileNames file_names;
  return ApplyCommon(std::move(old_file), std::move(patch_file),
                     std::move(new_file), file_names, force_keep);
}

status::Code Apply(const base::FilePath& old_path,
                   const base::FilePath& patch_path,
                   const base::FilePath& new_path,
                   bool force_keep) {
  using base::File;
  File old_file(old_path, File::FLAG_OPEN | File::FLAG_READ |
                              base::File::FLAG_WIN_SHARE_DELETE);
  File patch_file(patch_path, File::FLAG_OPEN | File::FLAG_READ |
                                  base::File::FLAG_WIN_SHARE_DELETE);
  File new_file(new_path, File::FLAG_CREATE_ALWAYS | File::FLAG_READ |
                              File::FLAG_WRITE | File::FLAG_WIN_SHARE_DELETE |
                              File::FLAG_CAN_DELETE_ON_CLOSE);
  const FileNames file_names(old_path, new_path, patch_path);
  return ApplyCommon(std::move(old_file), std::move(patch_file),
                     std::move(new_file), file_names, force_keep);
}

status::Code VerifyPatch(base::File patch_file) {
  return VerifyPatchCommon(std::move(patch_file), base::FilePath());
}

status::Code VerifyPatch(const base::FilePath& patch_path) {
  using base::File;
  File patch_file(patch_path, File::FLAG_OPEN | File::FLAG_READ |
                                  base::File::FLAG_SHARE_DELETE);
  return VerifyPatchCommon(std::move(patch_file), patch_path);
}

}  // namespace zucchini