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. |
| 14 | |
| 15 | #ifndef BAZEL_SRC_MAIN_CPP_ARCHIVE_UTILS_H_ |
| 16 | #define BAZEL_SRC_MAIN_CPP_ARCHIVE_UTILS_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Googler | 057b6b7 | 2023-05-25 11:57:59 -0700 | [diff] [blame] | 21 | #include "src/main/cpp/startup_options.h" |
| 22 | #include "src/main/cpp/util/logging.h" |
| 23 | |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 24 | namespace blaze { |
| 25 | |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 26 | // Determines the contents of the archive, storing the names of the contained |
| 27 | // files into `files` and the install md5 key into `install_md5`. |
| 28 | void DetermineArchiveContents(const std::string &archive_path, |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 29 | std::vector<std::string> *files, |
| 30 | std::string *install_md5); |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 31 | |
Googler | 057b6b7 | 2023-05-25 11:57:59 -0700 | [diff] [blame] | 32 | struct DurationMillis { |
| 33 | public: |
| 34 | const uint64_t millis; |
| 35 | |
| 36 | DurationMillis() : millis(kUnknownDuration) {} |
| 37 | DurationMillis(const uint64_t ms) : millis(ms) {} |
| 38 | |
| 39 | bool IsUnknown() const { return millis == kUnknownDuration; } |
| 40 | |
| 41 | private: |
| 42 | // Value representing that a timing event never occurred or is unknown. |
| 43 | static constexpr uint64_t kUnknownDuration = 0; |
| 44 | }; |
| 45 | |
| 46 | // DurationMillis that tracks if an archive was extracted. |
| 47 | struct ExtractionDurationMillis : DurationMillis { |
| 48 | const bool archive_extracted; |
| 49 | ExtractionDurationMillis() : DurationMillis(), archive_extracted(false) {} |
| 50 | ExtractionDurationMillis(const uint64_t ms, const bool archive_extracted) |
| 51 | : DurationMillis(ms), archive_extracted(archive_extracted) {} |
| 52 | }; |
| 53 | |
| 54 | // The reason for a blaze server restart. |
| 55 | // Keep in sync with logging.proto. |
| 56 | enum RestartReason { |
| 57 | NO_RESTART = 0, |
| 58 | NO_DAEMON, |
| 59 | NEW_VERSION, |
| 60 | NEW_OPTIONS, |
| 61 | PID_FILE_BUT_NO_SERVER, |
| 62 | SERVER_VANISHED, |
| 63 | SERVER_UNRESPONSIVE |
| 64 | }; |
| 65 | |
| 66 | // Encapsulates miscellaneous information reported to the server for logging and |
| 67 | // profiling purposes. |
| 68 | struct LoggingInfo { |
| 69 | public: |
| 70 | explicit LoggingInfo(const std::string &binary_path_, |
| 71 | const uint64_t start_time_ms_) |
| 72 | : binary_path(binary_path_), |
| 73 | start_time_ms(start_time_ms_), |
| 74 | restart_reason(NO_RESTART) {} |
| 75 | |
| 76 | void SetRestartReasonIfNotSet(const RestartReason restart_reason_) { |
| 77 | if (restart_reason == NO_RESTART) { |
| 78 | restart_reason = restart_reason_; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Path of this binary. |
| 83 | const std::string binary_path; |
| 84 | |
| 85 | // The time in ms the binary started up, measured from approximately the time |
| 86 | // that "main" was called. |
| 87 | const uint64_t start_time_ms; |
| 88 | |
| 89 | // The reason the server was restarted. |
| 90 | RestartReason restart_reason; |
| 91 | }; |
| 92 | |
| 93 | // Extracts the archive and ensures success via calls to ExtractArchiveOrDie and |
| 94 | // BlessFiles. If the install base, the location the archive is unpacked, |
| 95 | // already exists, extraction is skipped. Kills the client if an error is |
| 96 | // encountered. |
| 97 | ExtractionDurationMillis ExtractData( |
| 98 | const std::string &self_path, |
| 99 | const std::vector<std::string> &archive_contents, |
| 100 | const std::string &expected_install_md5, |
| 101 | const StartupOptions &startup_options, LoggingInfo *logging_info); |
| 102 | |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 103 | // Extracts the embedded data files in `archive_path` into `output_dir`. |
michajlo | aeae59a | 2020-03-27 12:21:25 -0700 | [diff] [blame] | 104 | // It's expected that `output_dir` already exists and that it's a directory. |
michajlo | 6f38f34 | 2019-05-22 11:53:22 -0700 | [diff] [blame] | 105 | // Fails if `expected_install_md5` doesn't match that contained in the archive, |
| 106 | // as this could indicate that the contents has unexpectedly changed. |
| 107 | void ExtractArchiveOrDie(const std::string &archive_path, |
| 108 | const std::string &product_name, |
| 109 | const std::string &expected_install_md5, |
| 110 | const std::string &output_dir); |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 111 | |
Googler | 9b7b4cc | 2023-05-22 17:18:04 -0700 | [diff] [blame] | 112 | // Sets the timestamps of the extracted files to the future via |
| 113 | // blaze_util::IFileMtime::SetToDistanceFuture and ensures that the files we |
| 114 | // have written are actually on the disk. Later, the blaze client calls |
| 115 | // blaze_util::IFileMtime::IsUntampered to ensure the files were "blessed" with |
| 116 | // these distant mtimes. |
| 117 | void BlessFiles(const std::string &embedded_binaries); |
| 118 | |
michajlo | 371a2e3 | 2019-05-23 13:14:39 -0700 | [diff] [blame] | 119 | // Retrieves the build label (version string) from `archive_path` into |
| 120 | // `build_label`. |
| 121 | void ExtractBuildLabel(const std::string &archive_path, |
michajlo | 371a2e3 | 2019-05-23 13:14:39 -0700 | [diff] [blame] | 122 | std::string *build_label); |
michajlo | 97559ba | 2019-06-03 14:14:22 -0700 | [diff] [blame] | 123 | |
| 124 | // Returns the server jar path from the archive contents. |
| 125 | std::string GetServerJarPath(const std::vector<std::string> &archive_contents); |
| 126 | |
michajlo | 3d8925d | 2019-05-20 16:10:41 -0700 | [diff] [blame] | 127 | } // namespace blaze |
| 128 | |
| 129 | #endif // BAZEL_SRC_MAIN_CPP_ARCHIVE_UTILS_H_ |