File: main.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 2,627 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <mach-o/getsect.h>
#include <mach-o/ldsyms.h>
#include <mach-o/loader.h>

#include <cstdint>
#include <memory>
#include <string>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/process/launch.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/util/util.h"
#include "third_party/zlib/google/zip_reader.h"

namespace updater {

namespace {

int Unzip(const std::string& archive, const base::FilePath& dest) {
  zip::ZipReader reader;
  if (!reader.OpenFromString(archive)) {
    VLOG(0) << "Error opening updater resource.";
    return kErrorUnpackingResource;
  }

  while (const zip::ZipReader::Entry* const entry = reader.Next()) {
    if (entry->is_directory) {
      if (!base::CreateDirectory(dest.Append(entry->path))) {
        VLOG(0) << "Failed to mkdir " << entry->path;
        return kErrorUnpackingResource;
      }
      continue;
    }

    auto writer =
        std::make_unique<zip::FilePathWriterDelegate>(dest.Append(entry->path));
    if (!writer || !reader.ExtractCurrentEntry(writer.get())) {
      VLOG(0) << "Cannot extract " << entry->path;
      return kErrorUnpackingResource;
    }
  }

  if (!reader.ok()) {
    return kErrorUnpackingResource;
  }

  return kErrorOk;
}

int Run(const base::FilePath& extract_path) {
  base::CommandLine command_line(*base::CommandLine::ForCurrentProcess());
  command_line.SetProgram(extract_path.Append(GetExecutableRelativePath()));
  int exit_code = 0;
  std::string out;
  base::GetAppOutputWithExitCode(command_line, &out, &exit_code);
  VLOG(1) << out;
  return exit_code;
}

int Main() {
  unsigned long size = 0;
  uint8_t* data =
      getsectiondata(&_mh_execute_header, SEG_DATA, "__updater_zip", &size);
  VLOG(1) << "The zip resource is " << size << " bytes long.";

  base::ScopedTempDir extract_path;
  if (!extract_path.CreateUniqueTempDir()) {
    VLOG(0) << "Failed to create temp dir.";
    return kErrorCreatingTempDir;
  }

  int result = Unzip({data, data + size}, extract_path.GetPath());
  if (result != kErrorOk) {
    return result;
  }

  return Run(extract_path.GetPath());
}

}  // namespace
}  // namespace updater

int main(int argc, const char* argv[]) {
  base::CommandLine::Init(argc, argv);
  return updater::Main();
}