MySQL 9.1.0
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 const std::size_t last_separator = exe_path.find_last_of("/\\");
84 std::string executable = exe_path.substr(last_separator + 1);
85 const std::string path = exe_path.erase(last_separator + 1);
86#ifdef _WIN32
87 const 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 const std::unique_ptr<char[]> read_data(
111 new (std::nothrow) char[file_length]);
112 if (!read_data) return;
113 if (file_stream.read(read_data.get(), file_length).fail() == true) return;
114 const std::string data(read_data.get(), file_length);
115 if (data_.Parse(data).HasParseError()) return;
116 if (schema_.Parse(json_schema).HasParseError()) return;
117 {
118 rapidjson::Document document;
119 if (document.Parse(data).HasParseError()) return;
120
121 const rapidjson::SchemaDocument sd(schema_);
122 rapidjson::SchemaValidator validator(sd);
123 if (!document.Accept(validator)) return;
124 }
125 }
126 valid_ = true;
127 }
128
129 ~Manifest_reader() = default;
130
131 bool file_present() const { return file_present_; }
132 bool empty() const { return !file_present_ || empty_; }
133 bool ro() const { return ro_; }
134 std::string manifest_file() const { return config_file_path_; }
135
136 bool read_local_manifest() const {
137 bool read_local_manifest = false;
138 if (get_element<bool>("read_local_manifest", read_local_manifest) == false)
139 return false;
140 return read_local_manifest;
141 }
142
143 bool components(std::string &components_string) const {
144 return get_element<std::string>("components", components_string);
145 }
146
147 private:
148 /**
149 Get an element value from JSON document.
150 Assumption: Type is compatible with Get() function and
151 type of element is matching with template argument.
152
153 @param [in] element_name Name of the element being searched
154 @param [out] element_value Value of the element
155
156 @returns status of search operation
157 @retval true Element found. Refer to element_value
158 @retval false Element missing.
159 */
160
161 template <typename T>
162 bool get_element(const std::string element_name, T &element_value) const {
163 if (!valid_ || !data_.HasMember(element_name)) return false;
164 element_value = data_[element_name].Get<T>();
165 return true;
166 }
167
168 private:
169 /** Configuration file path */
170 std::string config_file_path_;
171 /** Schema Document */
172 rapidjson::Document schema_;
173 /** Configuration data in JSON */
174 rapidjson::Document data_;
175 /** File status */
177 /** Validity of configuration data */
178 bool valid_;
179 /** content */
180 bool empty_;
181 /** RO flag */
182 bool ro_;
183};
184
185} // namespace manifest
186
187#endif // !MANIFEST_INCLUDED
Definition: manifest.h:59
bool ro() const
Definition: manifest.h:133
bool file_present() const
Definition: manifest.h:131
bool components(std::string &components_string) const
Definition: manifest.h:143
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:132
bool get_element(const std::string element_name, T &element_value) const
Get an element value from JSON document.
Definition: manifest.h:162
bool empty_
content
Definition: manifest.h:180
bool read_local_manifest() const
Definition: manifest.h:136
bool ro_
RO flag.
Definition: manifest.h:182
std::string config_file_path_
Configuration file path.
Definition: manifest.h:170
rapidjson::Document data_
Configuration data in JSON.
Definition: manifest.h:174
bool valid_
Validity of configuration data.
Definition: manifest.h:178
std::string manifest_file() const
Definition: manifest.h:134
rapidjson::Document schema_
Schema Document.
Definition: manifest.h:172
bool file_present_
File status.
Definition: manifest.h:176
Define rapidjson::SizeType to be std::uint64_t.
static void clean_up(bool print_message)
Definition: mysqld.cc:2746
static char * path
Definition: mysqldump.cc:149
constexpr value_type binary
Definition: classic_protocol_constants.h:275
Json_data_extension ext
Definition: backend.cc:50
Definition: manifest.h:38
std::string manifest_version_1_0
Definition: manifest.h:40
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Create a scope guard object.
Definition: scope_guard.h:113