forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.cc
76 lines (63 loc) · 2.68 KB
/
patcher.cc
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
// Copyright 2019 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 "chrome/updater/patcher.h"
#include <utility>
#include "base/callback.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "courgette/courgette.h"
#include "courgette/third_party/bsdiff/bsdiff.h"
namespace updater {
namespace {
class PatcherImpl : public update_client::Patcher {
public:
PatcherImpl() = default;
void PatchBsdiff(const base::FilePath& input_path,
const base::FilePath& patch_path,
const base::FilePath& output_path,
PatchCompleteCallback callback) const override {
base::File input_file(input_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
base::File patch_file(patch_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
base::File output_file(output_path, base::File::FLAG_CREATE |
base::File::FLAG_WRITE |
base::File::FLAG_EXCLUSIVE_WRITE);
if (!input_file.IsValid() || !patch_file.IsValid() ||
!output_file.IsValid()) {
std::move(callback).Run(-1);
return;
}
std::move(callback).Run(bsdiff::ApplyBinaryPatch(
std::move(input_file), std::move(patch_file), std::move(output_file)));
}
void PatchCourgette(const base::FilePath& input_path,
const base::FilePath& patch_path,
const base::FilePath& output_path,
PatchCompleteCallback callback) const override {
base::File input_file(input_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
base::File patch_file(patch_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
base::File output_file(output_path, base::File::FLAG_CREATE |
base::File::FLAG_WRITE |
base::File::FLAG_EXCLUSIVE_WRITE);
if (!input_file.IsValid() || !patch_file.IsValid() ||
!output_file.IsValid()) {
std::move(callback).Run(-1);
return;
}
std::move(callback).Run(courgette::ApplyEnsemblePatch(
std::move(input_file), std::move(patch_file), std::move(output_file)));
}
protected:
~PatcherImpl() override = default;
};
} // namespace
PatcherFactory::PatcherFactory() = default;
scoped_refptr<update_client::Patcher> PatcherFactory::Create() const {
return base::MakeRefCounted<PatcherImpl>();
}
PatcherFactory::~PatcherFactory() = default;
} // namespace updater