File: op_puffin.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (160 lines) | stat: -rw-r--r-- 5,897 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/op_puffin.h"

#include <string>
#include <utility>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "components/update_client/configurator.h"
#include "components/update_client/crx_cache.h"
#include "components/update_client/patcher.h"
#include "components/update_client/pipeline_util.h"
#include "components/update_client/protocol_definition.h"
#include "components/update_client/task_traits.h"
#include "components/update_client/update_client_errors.h"
#include "components/update_client/utils.h"
#include "third_party/puffin/src/include/puffin/puffpatch.h"

namespace update_client {

namespace {

// The sequence of calls is:
//
// [Original Sequence]    [Blocking Pool]
//
// PuffOperation
// CacheLookupDone
//                        Patch
//                        VerifyAndCleanUp
// PatchDone
// [original callback]
//
// All errors shortcut to PatchDone.

// Runs on the original sequence. Adds events and calls the original callback.
void PatchDone(
    base::OnceCallback<void(base::expected<base::FilePath, CategorizedError>)>
        callback,
    base::RepeatingCallback<void(base::Value::Dict)> event_adder,
    base::expected<base::FilePath, CategorizedError> result) {
  event_adder.Run(
      MakeSimpleOperationEvent(result, protocol_request::kEventPuff));
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), result));
}

// Runs in the blocking pool. Deletes any files that are no longer needed.
void VerifyAndCleanUp(
    base::OnceCallback<void(base::expected<base::FilePath, CategorizedError>)>
        callback,
    const base::FilePath& patch_file,
    const base::FilePath& new_file,
    const std::string& output_hash,
    int result) {
  base::DeleteFile(patch_file);
  if (result != puffin::P_OK) {
    base::DeleteFile(new_file);
    std::move(callback).Run(base::unexpected<CategorizedError>(
        {.category = ErrorCategory::kUnpack,
         .code = static_cast<int>(UnpackerError::kDeltaOperationFailure),
         .extra = result}));
    return;
  }

  if (!VerifyFileHash256(new_file, output_hash)) {
    base::DeleteFile(new_file);
    std::move(callback).Run(base::unexpected<CategorizedError>(
        {.category = ErrorCategory::kUnpack,
         .code = static_cast<int>(UnpackerError::kPatchOutHashMismatch)}));
    return;
  }

  std::move(callback).Run(new_file);
}

// Runs in the blocking pool. Opens file handles and applies the patch.
void Patch(
    scoped_refptr<Patcher> patcher,
    const base::FilePath& old_file,
    const base::FilePath& patch_file,
    const base::FilePath& temp_dir,
    const std::string& output_hash,
    base::OnceCallback<void(base::expected<base::FilePath, CategorizedError>)>
        callback) {
  base::FilePath new_file = temp_dir.Append(FILE_PATH_LITERAL("puffpatch_out"));
  patcher->PatchPuffPatch(
      base::File(old_file, base::File::FLAG_OPEN | base::File::FLAG_READ),
      base::File(patch_file, base::File::FLAG_OPEN | base::File::FLAG_READ),
      base::File(new_file, base::File::FLAG_CREATE_ALWAYS |
                               base::File::FLAG_WRITE |
                               base::File::FLAG_WIN_EXCLUSIVE_WRITE),
      base::BindOnce(&VerifyAndCleanUp, std::move(callback), patch_file,
                     new_file, output_hash));
}

// Runs on the original sequence.
void CacheLookupDone(
    base::RepeatingCallback<void(base::Value::Dict)> event_adder,
    scoped_refptr<Patcher> patcher,
    const base::FilePath& patch_file,
    const base::FilePath& temp_dir,
    const std::string& output_hash,
    base::OnceCallback<void(base::expected<base::FilePath, CategorizedError>)>
        callback,
    base::expected<base::FilePath, UnpackerError> cache_result) {
  if (!cache_result.has_value()) {
    base::ThreadPool::PostTaskAndReply(
        FROM_HERE, kTaskTraits,
        base::BindOnce(IgnoreResult(&base::DeleteFile), patch_file),
        base::BindOnce(&PatchDone, std::move(callback), event_adder,
                       base::unexpected<CategorizedError>(
                           {.category = ErrorCategory::kUnpack,
                            .code = static_cast<int>(cache_result.error())})));
    return;
  }
  base::ThreadPool::CreateSequencedTaskRunner(kTaskTraits)
      ->PostTask(
          FROM_HERE,
          base::BindOnce(&Patch, patcher, cache_result.value(), patch_file,
                         temp_dir, output_hash,
                         base::BindPostTaskToCurrentDefault(base::BindOnce(
                             &PatchDone, std::move(callback), event_adder))));
}

}  // namespace

base::OnceClosure PuffOperation(
    scoped_refptr<CrxCache> crx_cache,
    scoped_refptr<Patcher> patcher,
    base::RepeatingCallback<void(base::Value::Dict)> event_adder,
    const std::string& old_hash,
    const std::string& output_hash,
    const base::FilePath& patch_file,
    base::OnceCallback<void(base::expected<base::FilePath, CategorizedError>)>
        callback) {
  crx_cache->GetByHash(
      old_hash,
      base::BindOnce(&CacheLookupDone, event_adder, patcher, patch_file,
                     patch_file.DirName(), output_hash, std::move(callback)));
  return base::DoNothing();
}

}  // namespace update_client