blob: ab6445f57dd577fff05619c14828d17726dacfff [file] [log] [blame]
michajlo3d8925d2019-05-20 16:10:41 -07001// 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.
Googler9cc03462019-11-05 00:22:26 -080014#include <functional>
15#include <memory>
16#include <string>
michajlo3d8925d2019-05-20 16:10:41 -070017#include <vector>
18
Googler9cc03462019-11-05 00:22:26 -080019#include "src/main/cpp/archive_utils.h"
michajlo6f38f342019-05-22 11:53:22 -070020#include "src/main/cpp/blaze_util_platform.h"
21#include "src/main/cpp/util/errors.h"
michajlo3d8925d2019-05-20 16:10:41 -070022#include "src/main/cpp/util/exit_code.h"
Googler9cc03462019-11-05 00:22:26 -080023#include "src/main/cpp/util/file.h"
michajlo3d8925d2019-05-20 16:10:41 -070024#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
29namespace blaze {
30
michajlo6f38f342019-05-22 11:53:22 -070031using std::string;
Googler9cc03462019-11-05 00:22:26 -080032using std::vector;
michajlo6f38f342019-05-22 11:53:22 -070033
Googler9cc03462019-11-05 00:22:26 -080034struct PartialZipExtractor : public devtools_ijar::ZipExtractorProcessor {
35 using CallbackType =
36 std::function<void(const char *name, const char *data, size_t size)>;
michajlo6f38f342019-05-22 11:53:22 -070037
Googler9cc03462019-11-05 00:22:26 -080038 // 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
Googlerf8050542019-11-09 13:25:35 -080041 // up to and including "stop_entry".
Googler9cc03462019-11-05 00:22:26 -080042 // 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) {
michajlo6f38f342019-05-22 11:53:22 -070051 BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
Googler9cc03462019-11-05 00:22:26 -080052 << "Failed to open '" << archive_path
53 << "' as a zip file: " << blaze_util::GetLastErrorString();
michajlo6f38f342019-05-22 11:53:22 -070054 }
Googler9cc03462019-11-05 00:22:26 -080055 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_;
michajlo6f38f342019-05-22 11:53:22 -070073 }
74
Googler9cc03462019-11-05 00:22:26 -080075 bool Accept(const char *filename, devtools_ijar::u4 attr) override {
Googler9cc03462019-11-05 00:22:26 -080076 if (devtools_ijar::zipattr_is_dir(attr)) return false;
77 // Sometimes that fails to detect directories. Check the name too.
Googlerf8050542019-11-09 13:25:35 -080078 string fn = filename;
Googler9cc03462019-11-05 00:22:26 -080079 if (fn.empty() || fn.back() == '/') return false;
Googlerf8050542019-11-09 13:25:35 -080080 if (stop_name_ == fn) done_ = true;
Googler9cc03462019-11-05 00:22:26 -080081 seen_names_.push_back(std::move(fn));
Googlerf8050542019-11-09 13:25:35 -080082 return done_ || !!callback_; // true if a callback was supplied
Googler9cc03462019-11-05 00:22:26 -080083 }
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);
Googlerf8050542019-11-09 13:25:35 -080089 }
90 if (callback_) {
Googler9cc03462019-11-05 00:22:26 -080091 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;
michajlo6f38f342019-05-22 11:53:22 -0700100};
101
Googler9cc03462019-11-05 00:22:26 -0800102void 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);
michajlo371a2e32019-05-23 13:14:39 -0700106}
107
Googler9cc03462019-11-05 00:22:26 -0800108void ExtractArchiveOrDie(const string &archive_path, const string &product_name,
michajlo6f38f342019-05-22 11:53:22 -0700109 const string &expected_install_md5,
110 const string &output_dir) {
Googler9cc03462019-11-05 00:22:26 -0800111 string error;
michajlo6f38f342019-05-22 11:53:22 -0700112 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 }
michajloaeae59a2020-03-27 12:21:25 -0700117
118 if (!blaze_util::PathExists(output_dir)) {
michajlo6f38f342019-05-22 11:53:22 -0700119 BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR)
michajloaeae59a2020-03-27 12:21:25 -0700120 << "Archive output directory didn't exist: " << output_dir;
michajlo6f38f342019-05-22 11:53:22 -0700121 }
michajlo3d8925d2019-05-20 16:10:41 -0700122
Googler9cc03462019-11-05 00:22:26 -0800123 BAZEL_LOG(USER) << "Extracting " << product_name << " installation...";
michajlo3d8925d2019-05-20 16:10:41 -0700124
Googler9cc03462019-11-05 00:22:26 -0800125 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 });
michajlo3d8925d2019-05-20 16:10:41 -0700131
michajlo6f38f342019-05-22 11:53:22 -0700132 if (!dumper->Finish(&error)) {
133 BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
134 << "Failed to extract embedded binaries: " << error;
135 }
michajlo3d8925d2019-05-20 16:10:41 -0700136
michajlo6f38f342019-05-22 11:53:22 -0700137 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 }
michajlo3d8925d2019-05-20 16:10:41 -0700147}
148
Googler9cc03462019-11-05 00:22:26 -0800149void ExtractBuildLabel(const string &archive_path, string *build_label) {
150 PartialZipExtractor pze;
151 *build_label = pze.UnzipUntil(archive_path, "build-label.txt");
michajlo371a2e32019-05-23 13:14:39 -0700152}
153
Googler9cc03462019-11-05 00:22:26 -0800154string GetServerJarPath(const vector<string> &archive_contents) {
michajlo97559ba2019-06-03 14:14:22 -0700155 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
michajlo3d8925d2019-05-20 16:10:41 -0700162} // namespace blaze