MySQL 8.0.37
Source Code Documentation
binlog_istream.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 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 BINLOG_ISTREAM_INCLUDED
25#define BINLOG_ISTREAM_INCLUDED
26#include "my_sys.h"
27#include "sql/basic_istream.h"
29
30/**
31 It defines the error types which could happen when reading binlog files or
32 deserializing binlog events. String error message of the error types are
33 defined as well. It has a member variable to store an error type and
34 provides a few functions to check the error type stored in the member
35 variable.
36 */
38 public:
39 /**
40 Possible errors which happens when reading an event.
41 */
43 // No error happens
45 /*
46 Arrive at the end of the stream. Nothing was read. It is smaller than any
47 other errors. Because READ_EOF is often not an error, and others are
48 usually errors.
49 */
51 // malformed event
53 // IO error while reading
55 // Failed to allocate memory
57 // Only a partial event could be read
59 // Only a partial Format_description_log_event could be read
63 // Event's is_valid returned false
65 // Cannot open the binlog file
67 // System IO error happened while reading the binlog magic
69 // The binlog magic is incorrect
75 };
76
77 Binlog_read_error() = default;
79
80 bool has_error() const { return m_type != SUCCESS; }
81 bool has_fatal_error() const { return m_type > READ_EOF; }
82
83 /**
84 Return the error encountered when reading events.
85 */
86 Error_type get_type() const { return m_type; }
87
88 /**
89 Return error message of the error type.
90
91 @return It will return nullptr if m_type is SUCCESS. In practice, it should
92 never be called if m_type is SUCCESS. So an assertion is added in
93 debug mode which predicts m_type is not SUCCESS.
94 */
95 const char *get_str() const;
96
97 /**
98 Set m_error to error.
99
100 @param[in] type The error type will be set
101 @retval false If error is SUCCESS
102 @retval true If error is not SUCCESS.
103 */
105 m_type = type;
106 return has_error();
107 }
108
109 private:
111};
112
113/**
114 Seekable_istream with decryption feature. It can be setup into an stream
115 pipeline. In the pipeline, it decrypts the data from down stream and then
116 feeds the decrypted data into up stream.
117*/
119 public:
121
122 /**
123 Initialize the context used in the decryption stream.
124
125 @param[in] down_istream The down stream where the encrypted data is stored.
126 @param[in] binlog_read_error Binlog_encryption_istream doesn't own a
127 Binlog_read_error. So the caller should provide
128 one to it. When error happens, the error type
129 will be set into 'binlog_read_error'.
130
131 @retval false Succeed.
132 @retval true Error.
133 */
134 bool open(std::unique_ptr<Basic_seekable_istream> down_istream,
135 Binlog_read_error *binlog_read_error);
136 /**
137 Closes the stream. It also closes the down stream and the decryptor.
138 */
139 void close();
140
141 ssize_t read(unsigned char *buffer, size_t length) override;
142 bool seek(my_off_t offset) override;
143 my_off_t length() override;
144
145 private:
146 /* The decryptor cypher to decrypt the content read from down stream */
147 std::unique_ptr<Stream_cipher> m_decryptor;
148 /* The down stream containing the encrypted content */
149 std::unique_ptr<Basic_seekable_istream> m_down_istream;
150};
151
152/**
153 Base class of binlog input files. It is a logical binlog file which wraps
154 and hides the detail of lower layer storage implementation. Binlog reader and
155 other binlog code just uses this class to control real storage.
156*/
158 public:
159 /**
160 @param[in] binlog_read_error Basic_binlog_ifile doesn't own an
161 Binlog_read_error. So the caller should
162 provide one to it. When error happens,
163 the error type will be set into 'error'.
164 */
165 Basic_binlog_ifile(Binlog_read_error *binlog_read_error);
168 ~Basic_binlog_ifile() override;
169 /**
170 Open a binlog file.
171
172 @param[in] file_name name of the binlog file which will be opened.
173 */
174 bool open(const char *file_name);
175 /**
176 Close the binlog file it is reading.
177 */
178 void close();
179
180 ssize_t read(unsigned char *buffer, size_t length) override;
181 bool seek(my_off_t position) override;
182
183 my_off_t position() const { return m_position; }
184 bool is_open() const { return m_istream != nullptr; }
185
186 const std::string &file_name() const { return m_file_name; }
187 /**
188 Get length of the binlog file. It is not os file length. The content maybe
189 encrypted or compressed. It is the total length of BINLOG_MAGIC and all
190 raw binlog events.
191 */
192 my_off_t length() override;
193
194 protected:
195 /**
196 Open the system layer file. It is the entry of the stream pipeline.
197 Implementation is delegated to sub-classes. Sub-classes opens system layer
198 files in different way.
199
200 @param[in] file_name name of the binlog file which will be opened.
201 */
202 virtual std::unique_ptr<Basic_seekable_istream> open_file(
203 const char *file_name) = 0;
204
205 /**
206 It is convenient for caller to share a Binlog_read_error object between
207 streams. So Binlog_read_error pointer is defined here. It should be
208 initialized in constructor by caller.
209 */
211
212 private:
213 /**
214 The binlog's position where it is reading. It is the position in logical
215 binlog file, but not the position of system file.
216 */
218 /** It is the entry of the low level stream pipeline. */
219 std::unique_ptr<Basic_seekable_istream> m_istream;
220 /** Name of the file opened */
221 std::string m_file_name;
222 /**
223 Read binlog magic from binlog file and check if it is valid binlog magic.
224
225 This function also takes care of setting up any other stream layer (i.e.
226 encryption) when needed.
227
228 @retval false The high level stream layer was recognized as a binary log.
229 @retval true Failure identifying the high level stream layer.
230 */
231 bool read_binlog_magic();
232};
233
234#ifdef MYSQL_SERVER
235/**
236 Binlog input file. It is responsible for opening binlog files generated by
237 the server itself, but not relaylog files.
238*/
240 public:
242
243 protected:
244 std::unique_ptr<Basic_seekable_istream> open_file(
245 const char *file_name) override;
246};
247
248/**
249 Relaylog input file. It is responsible for opening relay log files.
250*/
252 public:
254
255 protected:
256 std::unique_ptr<Basic_seekable_istream> open_file(
257 const char *file_name) override;
258};
259#endif
260
261#endif // BINLOG_ISTREAM_INCLUDED
Base class of binlog input files.
Definition: binlog_istream.h:157
bool open(const char *file_name)
Open a binlog file.
Definition: binlog_istream.cc:183
bool seek(my_off_t position) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:204
virtual std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name)=0
Open the system layer file.
bool read_binlog_magic()
Read binlog magic from binlog file and check if it is valid binlog magic.
Definition: binlog_istream.cc:135
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:198
Basic_binlog_ifile & operator=(const Basic_binlog_ifile &)=delete
my_off_t length() override
Get length of the binlog file.
Definition: binlog_istream.cc:213
const std::string & file_name() const
Definition: binlog_istream.h:186
my_off_t position() const
Definition: binlog_istream.h:183
my_off_t m_position
The binlog's position where it is reading.
Definition: binlog_istream.h:217
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_istream.h:210
~Basic_binlog_ifile() override
Definition: binlog_istream.cc:178
bool is_open() const
Definition: binlog_istream.h:184
std::string m_file_name
Name of the file opened.
Definition: binlog_istream.h:221
Basic_binlog_ifile(Binlog_read_error *binlog_read_error)
Definition: binlog_istream.cc:173
std::unique_ptr< Basic_seekable_istream > m_istream
It is the entry of the low level stream pipeline.
Definition: binlog_istream.h:219
void close()
Close the binlog file it is reading.
Definition: binlog_istream.cc:191
Basic_binlog_ifile(const Basic_binlog_ifile &)=delete
The abstract class for seekable input streams which have fixed length and provide seek operation.
Definition: basic_istream.h:58
Seekable_istream with decryption feature.
Definition: binlog_istream.h:118
bool seek(my_off_t offset) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:121
~Binlog_encryption_istream() override
Definition: binlog_istream.cc:133
bool open(std::unique_ptr< Basic_seekable_istream > down_istream, Binlog_read_error *binlog_read_error)
Initialize the context used in the decryption stream.
Definition: binlog_istream.cc:78
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:114
void close()
Closes the stream.
Definition: binlog_istream.cc:108
std::unique_ptr< Basic_seekable_istream > m_down_istream
Definition: binlog_istream.h:149
my_off_t length() override
The total length of the stream.
Definition: binlog_istream.cc:129
std::unique_ptr< Stream_cipher > m_decryptor
Definition: binlog_istream.h:147
Binlog input file.
Definition: binlog_istream.h:239
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:217
It defines the error types which could happen when reading binlog files or deserializing binlog event...
Definition: binlog_istream.h:37
bool has_error() const
Definition: binlog_istream.h:80
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:86
Binlog_read_error(Error_type type)
Definition: binlog_istream.h:78
Error_type
Possible errors which happens when reading an event.
Definition: binlog_istream.h:42
@ EVENT_TOO_LARGE
Definition: binlog_istream.h:61
@ MEM_ALLOCATE
Definition: binlog_istream.h:56
@ BOGUS
Definition: binlog_istream.h:52
@ SUCCESS
Definition: binlog_istream.h:44
@ SYSTEM_IO
Definition: binlog_istream.h:54
@ READ_ENCRYPTED_LOG_FILE_IS_NOT_SUPPORTED
Definition: binlog_istream.h:73
@ CANNOT_GET_FILE_PASSWORD
Definition: binlog_istream.h:72
@ ERROR_DECRYPTING_FILE
Definition: binlog_istream.h:74
@ READ_EOF
Definition: binlog_istream.h:50
@ HEADER_IO_FAILURE
Definition: binlog_istream.h:68
@ TRUNC_EVENT
Definition: binlog_istream.h:58
@ CANNOT_OPEN
Definition: binlog_istream.h:66
@ BAD_BINLOG_MAGIC
Definition: binlog_istream.h:70
@ INVALID_EVENT
Definition: binlog_istream.h:64
@ TRUNC_FD_EVENT
Definition: binlog_istream.h:60
@ CHECKSUM_FAILURE
Definition: binlog_istream.h:62
@ INVALID_ENCRYPTION_HEADER
Definition: binlog_istream.h:71
Binlog_read_error()=default
bool set_type(Error_type type)
Set m_error to error.
Definition: binlog_istream.h:104
Error_type m_type
Definition: binlog_istream.h:110
const char * get_str() const
Return error message of the error type.
Definition: binlog_istream.cc:34
bool has_fatal_error() const
Definition: binlog_istream.h:81
Relaylog input file.
Definition: binlog_istream.h:251
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:228
ulonglong my_off_t
Definition: my_inttypes.h:72
Common header for many mysys elements.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
required string type
Definition: replication_group_member_actions.proto:34
This file includes the major components for encrypting/decrypting binary log files.