util_mac.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2021 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. #include "chrome/updater/util.h"
  5. #include <string>
  6. #include "base/command_line.h"
  7. #include "base/files/file_enumerator.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/logging.h"
  11. #include "base/process/launch.h"
  12. #include "base/strings/strcat.h"
  13. #include "base/version.h"
  14. #include "chrome/updater/constants.h"
  15. #import "chrome/updater/mac/mac_util.h"
  16. #include "chrome/updater/updater_branding.h"
  17. #include "chrome/updater/updater_version.h"
  18. namespace updater {
  19. namespace {
  20. constexpr base::FilePath::CharType kZipExePath[] =
  21. FILE_PATH_LITERAL("/usr/bin/unzip");
  22. base::FilePath GetUpdaterFolderName() {
  23. return base::FilePath(COMPANY_SHORTNAME_STRING)
  24. .AppendASCII(PRODUCT_FULLNAME_STRING);
  25. }
  26. base::FilePath ExecutableFolderPath() {
  27. return base::FilePath(
  28. base::StrCat({PRODUCT_FULLNAME_STRING, kExecutableSuffix, ".app"}))
  29. .Append(FILE_PATH_LITERAL("Contents"))
  30. .Append(FILE_PATH_LITERAL("MacOS"));
  31. }
  32. } // namespace
  33. bool UnzipWithExe(const base::FilePath& src_path,
  34. const base::FilePath& dest_path) {
  35. base::FilePath file_path(kZipExePath);
  36. base::CommandLine command(file_path);
  37. command.AppendArg(src_path.value());
  38. command.AppendArg("-d");
  39. command.AppendArg(dest_path.value());
  40. std::string output;
  41. int exit_code = 0;
  42. if (!base::GetAppOutputWithExitCode(command, &output, &exit_code)) {
  43. VLOG(0) << "Something went wrong while running the unzipping with "
  44. << kZipExePath;
  45. return false;
  46. }
  47. // Unzip utility having 0 is success and 1 is a warning.
  48. if (exit_code > 1) {
  49. VLOG(0) << "Output from unzipping: " << output;
  50. VLOG(0) << "Exit code: " << exit_code;
  51. }
  52. return exit_code <= 1;
  53. }
  54. absl::optional<base::FilePath> GetBaseInstallDirectory(UpdaterScope scope) {
  55. absl::optional<base::FilePath> path = GetLibraryFolderPath(scope);
  56. if (!path)
  57. return absl::nullopt;
  58. return path->Append(GetUpdaterFolderName());
  59. }
  60. absl::optional<base::FilePath> GetExecutableFolderPathForVersion(
  61. UpdaterScope scope,
  62. const base::Version& version) {
  63. absl::optional<base::FilePath> path =
  64. GetVersionedInstallDirectory(scope, version);
  65. if (!path)
  66. return absl::nullopt;
  67. return path->Append(ExecutableFolderPath());
  68. }
  69. absl::optional<base::FilePath> GetUpdaterAppBundlePath(UpdaterScope scope) {
  70. absl::optional<base::FilePath> path = GetVersionedInstallDirectory(scope);
  71. if (!path)
  72. return absl::nullopt;
  73. return path->Append(
  74. base::StrCat({PRODUCT_FULLNAME_STRING, kExecutableSuffix, ".app"}));
  75. }
  76. absl::optional<base::FilePath> GetUpdaterExecutablePath(UpdaterScope scope) {
  77. absl::optional<base::FilePath> path = GetVersionedInstallDirectory(scope);
  78. if (!path)
  79. return absl::nullopt;
  80. return path->Append(ExecutableFolderPath())
  81. .AppendASCII(base::StrCat({PRODUCT_FULLNAME_STRING, kExecutableSuffix}));
  82. }
  83. base::FilePath GetExecutableRelativePath() {
  84. return ExecutableFolderPath().Append(
  85. base::StrCat({PRODUCT_FULLNAME_STRING, kExecutableSuffix}));
  86. }
  87. absl::optional<base::FilePath> GetKeystoneFolderPath(UpdaterScope scope) {
  88. absl::optional<base::FilePath> path = GetLibraryFolderPath(scope);
  89. if (!path)
  90. return absl::nullopt;
  91. return path->Append(FILE_PATH_LITERAL(COMPANY_SHORTNAME_STRING))
  92. .Append(FILE_PATH_LITERAL(KEYSTONE_NAME));
  93. }
  94. bool ConfirmFilePermissions(const base::FilePath& root_path,
  95. int kPermissionsMask) {
  96. base::FileEnumerator file_enumerator(
  97. root_path, false,
  98. base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES |
  99. base::FileEnumerator::SHOW_SYM_LINKS);
  100. for (base::FilePath path = file_enumerator.Next(); !path.empty();
  101. path = file_enumerator.Next()) {
  102. if (!SetPosixFilePermissions(path, kPermissionsMask)) {
  103. VLOG(0) << "Couldn't set file permissions for for: " << path.value();
  104. return false;
  105. }
  106. base::File::Info file_info;
  107. if (!base::GetFileInfo(path, &file_info)) {
  108. VLOG(0) << "Couldn't get file info for: " << path.value();
  109. return false;
  110. }
  111. // If file path is real directory and not a link, recurse into it.
  112. if (file_info.is_directory && !base::IsLink(path)) {
  113. if (!ConfirmFilePermissions(path, kPermissionsMask))
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. } // namespace updater