MySQL 8.0.37
Source Code Documentation
manifest.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MANIFEST_INCLUDED
25#define MANIFEST_INCLUDED
26
27#include <fstream> /* std::ifstream */
28#include <memory>
29#include <string>
30
31#include "scope_guard.h"
32
33#include "my_rapidjson_size_t.h"
34
35#include <rapidjson/document.h>
36#include <rapidjson/schema.h>
37
38namespace manifest {
39
41 "{"
42 " \"title\": \"Manifest validator version 1.0\","
43 " \"description\": \"Expected schema for version 1.0\","
44 " \"type\": \"object\","
45 " \"properties\": {"
46 " \"read_local_manifest\": {"
47 " \"description\": \"Flag to indicate that manifest information is in "
48 "data directory\","
49 " \"type\": \"boolean\""
50 " },"
51 " \"components\": {"
52 " \"description\": \"The list of components to be loaded at "
53 "bootstrap\","
54 " \"type\": \"string\""
55 " }"
56 " }"
57 "}";
58
59class Manifest_reader final {
60 public:
61 /*
62 Constructor
63
64 Reads manifest file if present.
65 Expected format: JSON.
66
67 @param [in] executable_path Executable location
68 @param [in] instance_path Location of specific instance
69 Must have separator character at the end
70 */
71
72 explicit Manifest_reader(const std::string executable_path,
73 const std::string instance_path,
74 std::string json_schema = manifest_version_1_0)
76 schema_(),
77 data_(),
78 file_present_(false),
79 valid_(false),
80 empty_(true),
81 ro_(true) {
82 std::string exe_path(executable_path);
83 std::size_t last_separator = exe_path.find_last_of("/\\");
84 std::string executable = exe_path.substr(last_separator + 1);
85 std::string path = exe_path.erase(last_separator + 1);
86#ifdef _WIN32
87 std::size_t ext = executable.find(".exe");
88 executable = executable.substr(0, ext);
89#endif // _WIN32
90 executable.append(".my");
91 if (instance_path.length() == 0)
92 config_file_path_ = path + executable;
93 else
94 config_file_path_ = instance_path + executable;
95 std::ifstream file_stream(config_file_path_,
96 std::ios::in | std::ios::ate | std::ios::binary);
97 if (!file_stream.is_open()) return;
98 file_present_ = true;
99 {
100 /* Check if files read-only or not */
101 std::ofstream out_stream(config_file_path_, std::ios_base::app);
102 ro_ = !out_stream.is_open();
103 out_stream.close();
104 }
105 auto clean_up = create_scope_guard([&] { file_stream.close(); });
106 auto file_length = file_stream.tellg();
107 if (file_length > 0) {
108 empty_ = false;
109 file_stream.seekg(std::ios::beg);
110 std::unique_ptr<char[]> read_data(new (std::nothrow) char[file_length]);
111 if (!read_data) return;
112 if (file_stream.read(read_data.get(), file_length).fail() == true) return;
113 std::string data(read_data.get(), file_length);
114 if (data_.Parse(data).HasParseError()) return;
115 if (schema_.Parse(json_schema).HasParseError()) return;
116 {
117 rapidjson::Document document;
118 if (document.Parse(data).HasParseError()) return;
119
120 rapidjson::SchemaDocument sd(schema_);
121 rapidjson::SchemaValidator validator(sd);
122 if (!document.Accept(validator)) return;
123 }
124 }
125 valid_ = true;
126 }
127
128 ~Manifest_reader() = default;
129
130 bool file_present() const { return file_present_; }
131 bool empty() const { return !file_present_ || empty_; }
132 bool ro() const { return ro_; }
133 std::string manifest_file() const { return config_file_path_; }
134
135 bool read_local_manifest() const {
136 bool read_local_manifest = false;
137 if (get_element<bool>("read_local_manifest", read_local_manifest) == false)
138 return false;
139 return read_local_manifest;
140 }
141
142 bool components(std::string &components_string) const {
143 return get_element<std::string>("components", components_string);
144 }
145
146 private:
147 /**
148 Get an element value from JSON document.
149 Assumption: Type is compatible with Get() function and
150 type of element is matching with template argument.
151
152 @param [in] element_name Name of the element being searched
153 @param [out] element_value Value of the element
154
155 @returns status of search operation
156 @retval true Element found. Refer to element_value
157 @retval false Element missing.
158 */
159
160 template <typename T>
161 bool get_element(const std::string element_name, T &element_value) const {
162 if (!valid_ || !data_.HasMember(element_name)) return false;
163 element_value = data_[element_name].Get<T>();
164 return true;
165 }
166
167 private:
168 /** Configuration file path */
169 std::string config_file_path_;
170 /** Schema Document */
171 rapidjson::Document schema_;
172 /** Configuration data in JSON */
173 rapidjson::Document data_;
174 /** File status */
176 /** Validity of configuration data */
177 bool valid_;
178 /** content */
179 bool empty_;
180 /** RO flag */
181 bool ro_;
182};
183
184} // namespace manifest
185
186#endif // !MANIFEST_INCLUDED
Definition: manifest.h:59
bool ro() const
Definition: manifest.h:132
bool file_present() const
Definition: manifest.h:130
bool components(std::string &components_string) const
Definition: manifest.h:142
Manifest_reader(const std::string executable_path, const std::string instance_path, std::string json_schema=manifest_version_1_0)
Definition: manifest.h:72
bool empty() const
Definition: manifest.h:131
bool get_element(const std::string element_name, T &element_value) const
Get an element value from JSON document.
Definition: manifest.h:161
bool empty_
content
Definition: manifest.h:179
bool read_local_manifest() const
Definition: manifest.h:135
bool ro_
RO flag.
Definition: manifest.h:181
std::string config_file_path_
Configuration file path.
Definition: manifest.h:169
rapidjson::Document data_
Configuration data in JSON.
Definition: manifest.h:173
bool valid_
Validity of configuration data.
Definition: manifest.h:177
std::string manifest_file() const
Definition: manifest.h:133
rapidjson::Document schema_
Schema Document.
Definition: manifest.h:171
bool file_present_
File status.
Definition: manifest.h:175
Define rapidjson::SizeType to be std::size_t.
static void clean_up(bool print_message)
Definition: mysqld.cc:2600
static char * path
Definition: mysqldump.cc:137
constexpr value_type binary
Definition: classic_protocol_constants.h:275
Json_data_extension ext
Definition: backend.cc:51
Definition: manifest.h:38
std::string manifest_version_1_0
Definition: manifest.h:40
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Definition: scope_guard.h:61