michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 14 | #include <functional> |
| 15 | #include <memory> |
| 16 | #include <string> |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 19 | #include "src/main/cpp/archive_utils.h" |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 20 | #include "src/main/cpp/blaze_util_platform.h" |
| 21 | #include "src/main/cpp/util/errors.h" |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 22 | #include "src/main/cpp/util/exit_code.h" |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 23 | #include "src/main/cpp/util/file.h" |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 24 | #include "src/main/cpp/util/logging.h" |
| 25 | #include "src/main/cpp/util/path.h" |
| 26 | #include "src/main/cpp/util/strings.h" |
| 27 | #include "third_party/ijar/zip.h" |
| 28 | |
| 29 | namespace blaze { |
| 30 | |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 31 | using std::string; |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 32 | using std::vector; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 33 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 34 | struct PartialZipExtractor : public devtools_ijar::ZipExtractorProcessor { |
| 35 | using CallbackType = |
| 36 | std::function<void(const char *name, const char *data, size_t size)>; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 37 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 38 | // Scan the zip file "archive_path" until a file named "stop_entry" is seen, |
| 39 | // then stop. |
| 40 | // If entry_names is not null, it receives a list of all file members |
Googler | f805054 | 2019-11-09 13:25:35 -0800 | [diff] [blame] | 41 | // up to and including "stop_entry". |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 42 | // If a callback is given, it is run with the name and contents of |
| 43 | // each such member. |
| 44 | // Returns the contents of the "stop_entry" member. |
| 45 | string UnzipUntil(const string &archive_path, const string &stop_entry, |
| 46 | vector<string> *entry_names = nullptr, |
| 47 | CallbackType &&callback = {}) { |
| 48 | std::unique_ptr<devtools_ijar::ZipExtractor> extractor( |
| 49 | devtools_ijar::ZipExtractor::Create(archive_path.c_str(), this)); |
| 50 | if (!extractor) { |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 51 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 52 | << "Failed to open '" << archive_path |
| 53 | << "' as a zip file: " << blaze_util::GetLastErrorString(); |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 54 | } |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 55 | stop_name_ = stop_entry; |
| 56 | seen_names_.clear(); |
| 57 | callback_ = callback; |
| 58 | done_ = false; |
| 59 | while (!done_ && extractor->ProcessNext()) { |
| 60 | // Scan zip until EOF, an error, or Accept() has seen stop_entry. |
| 61 | } |
| 62 | if (const char *err = extractor->GetError()) { |
| 63 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
| 64 | << "Error reading zip file '" << archive_path << "': " << err; |
| 65 | } |
| 66 | if (!done_) { |
| 67 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
| 68 | << "Failed to find member '" << stop_entry << "' in zip file '" |
| 69 | << archive_path << "'"; |
| 70 | } |
| 71 | if (entry_names) *entry_names = std::move(seen_names_); |
| 72 | return stop_value_; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 75 | bool Accept(const char *filename, devtools_ijar::u4 attr) override { |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 76 | if (devtools_ijar::zipattr_is_dir(attr)) return false; |
| 77 | // Sometimes that fails to detect directories. Check the name too. |
Googler | f805054 | 2019-11-09 13:25:35 -0800 | [diff] [blame] | 78 | string fn = filename; |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 79 | if (fn.empty() || fn.back() == '/') return false; |
Googler | f805054 | 2019-11-09 13:25:35 -0800 | [diff] [blame] | 80 | if (stop_name_ == fn) done_ = true; |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 81 | seen_names_.push_back(std::move(fn)); |
Googler | f805054 | 2019-11-09 13:25:35 -0800 | [diff] [blame] | 82 | return done_ || !!callback_; // true if a callback was supplied |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Process(const char *filename, devtools_ijar::u4 attr, |
| 86 | const devtools_ijar::u1 *data, size_t size) override { |
| 87 | if (done_) { |
| 88 | stop_value_.assign(reinterpret_cast<const char *>(data), size); |
Googler | f805054 | 2019-11-09 13:25:35 -0800 | [diff] [blame] | 89 | } |
| 90 | if (callback_) { |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 91 | callback_(filename, reinterpret_cast<const char *>(data), size); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | string stop_name_; |
| 96 | string stop_value_; |
| 97 | vector<string> seen_names_; |
| 98 | CallbackType callback_; |
| 99 | bool done_ = false; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 102 | void DetermineArchiveContents(const string &archive_path, vector<string> *files, |
| 103 | string *install_md5) { |
| 104 | PartialZipExtractor pze; |
| 105 | *install_md5 = pze.UnzipUntil(archive_path, "install_base_key", files); |
michajlo | 371a2e3 | 2019-05-23 13:14:39 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 108 | void ExtractArchiveOrDie(const string &archive_path, const string &product_name, |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 109 | const string &expected_install_md5, |
| 110 | const string &output_dir) { |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 111 | string error; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 112 | std::unique_ptr<blaze::embedded_binaries::Dumper> dumper( |
| 113 | blaze::embedded_binaries::Create(&error)); |
| 114 | if (dumper == nullptr) { |
| 115 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) << error; |
| 116 | } |
michajlo | aeae59a | 2020-03-27 12:21:25 -0700 | [diff] [blame] | 117 | |
| 118 | if (!blaze_util::PathExists(output_dir)) { |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 119 | BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR) |
michajlo | aeae59a | 2020-03-27 12:21:25 -0700 | [diff] [blame] | 120 | << "Archive output directory didn't exist: " << output_dir; |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 121 | } |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 122 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 123 | BAZEL_LOG(USER) << "Extracting " << product_name << " installation..."; |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 124 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 125 | PartialZipExtractor pze; |
| 126 | string install_md5 = pze.UnzipUntil( |
| 127 | archive_path, "install_base_key", nullptr, |
| 128 | [&](const char *name, const char *data, size_t size) { |
| 129 | dumper->Dump(data, size, blaze_util::JoinPath(output_dir, name)); |
| 130 | }); |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 131 | |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 132 | if (!dumper->Finish(&error)) { |
| 133 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
| 134 | << "Failed to extract embedded binaries: " << error; |
| 135 | } |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 136 | |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 137 | if (install_md5 != expected_install_md5) { |
| 138 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
| 139 | << "The " << product_name << " binary at " << archive_path |
| 140 | << " was replaced during the client's self-extraction (old md5: " |
| 141 | << expected_install_md5 << " new md5: " << install_md5 |
| 142 | << "). If you expected this then you should simply re-run " |
| 143 | << product_name |
| 144 | << " in order to pick up the different version. If you didn't expect " |
| 145 | "this then you should investigate what happened."; |
| 146 | } |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 149 | void ExtractBuildLabel(const string &archive_path, string *build_label) { |
| 150 | PartialZipExtractor pze; |
| 151 | *build_label = pze.UnzipUntil(archive_path, "build-label.txt"); |
michajlo | 371a2e3 | 2019-05-23 13:14:39 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Googler | 9cc0346 | 2019-11-05 00:22:26 -0800 | [diff] [blame] | 154 | string GetServerJarPath(const vector<string> &archive_contents) { |
michajlo | 97559ba | 2019-06-03 14:14:22 -0700 | [diff] [blame] | 155 | if (archive_contents.empty()) { |
| 156 | BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) |
| 157 | << "Couldn't find server jar in archive"; |
| 158 | } |
| 159 | return archive_contents[0]; |
| 160 | } |
| 161 | |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 162 | } // namespace blaze |