MySQL 9.1.0
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 // Event comes from new server and cannot neglect unknown event fields
77 };
78
79 Binlog_read_error() = default;
81
82 bool has_error() const { return m_type != SUCCESS; }
83 bool has_fatal_error() const { return m_type > READ_EOF; }
84
85 /**
86 Return the error encountered when reading events.
87 */
88 Error_type get_type() const { return m_type; }
89
90 /**
91 Return error message of the error type.
92
93 @return It will return nullptr if m_type is SUCCESS. In practice, it should
94 never be called if m_type is SUCCESS. So an assertion is added in
95 debug mode which predicts m_type is not SUCCESS.
96 */
97 const char *get_str() const;
98
99 /**
100 Set m_error to error.
101
102 @param[in] type The error type will be set
103 @retval false If error is SUCCESS
104 @retval true If error is not SUCCESS.
105 */
107 m_type = type;
108 return has_error();
109 }
110
111 private:
113};
114
115/**
116 Seekable_istream with decryption feature. It can be setup into an stream
117 pipeline. In the pipeline, it decrypts the data from down stream and then
118 feeds the decrypted data into up stream.
119*/
121 public:
123
124 /**
125 Initialize the context used in the decryption stream.
126
127 @param[in] down_istream The down stream where the encrypted data is stored.
128 @param[in] binlog_read_error Binlog_encryption_istream doesn't own a
129 Binlog_read_error. So the caller should provide
130 one to it. When error happens, the error type
131 will be set into 'binlog_read_error'.
132
133 @retval false Succeed.
134 @retval true Error.
135 */
136 bool open(std::unique_ptr<Basic_seekable_istream> down_istream,
137 Binlog_read_error *binlog_read_error);
138 /**
139 Closes the stream. It also closes the down stream and the decryptor.
140 */
141 void close();
142
143 ssize_t read(unsigned char *buffer, size_t length) override;
144 bool seek(my_off_t offset) override;
145 my_off_t length() override;
146
147 private:
148 /* The decryptor cypher to decrypt the content read from down stream */
149 std::unique_ptr<Stream_cipher> m_decryptor;
150 /* The down stream containing the encrypted content */
151 std::unique_ptr<Basic_seekable_istream> m_down_istream;
152};
153
154/**
155 Base class of binlog input files. It is a logical binlog file which wraps
156 and hides the detail of lower layer storage implementation. Binlog reader and
157 other binlog code just uses this class to control real storage.
158*/
160 public:
161 /**
162 @param[in] binlog_read_error Basic_binlog_ifile doesn't own an
163 Binlog_read_error. So the caller should
164 provide one to it. When error happens,
165 the error type will be set into 'error'.
166 */
167 Basic_binlog_ifile(Binlog_read_error *binlog_read_error);
170 ~Basic_binlog_ifile() override;
171 /**
172 Open a binlog file.
173
174 @param[in] file_name name of the binlog file which will be opened.
175 */
176 bool open(const char *file_name);
177 /**
178 Close the binlog file it is reading.
179 */
180 void close();
181
182 ssize_t read(unsigned char *buffer, size_t length) override;
183 bool seek(my_off_t position) override;
184
185 my_off_t position() const { return m_position; }
186 bool is_open() const { return m_istream != nullptr; }
187
188 const std::string &file_name() const { return m_file_name; }
189 /**
190 Get length of the binlog file. It is not os file length. The content maybe
191 encrypted or compressed. It is the total length of BINLOG_MAGIC and all
192 raw binlog events.
193 */
194 my_off_t length() override;
195
196 protected:
197 /**
198 Open the system layer file. It is the entry of the stream pipeline.
199 Implementation is delegated to sub-classes. Sub-classes opens system layer
200 files in different way.
201
202 @param[in] file_name name of the binlog file which will be opened.
203 */
204 virtual std::unique_ptr<Basic_seekable_istream> open_file(
205 const char *file_name) = 0;
206
207 /**
208 It is convenient for caller to share a Binlog_read_error object between
209 streams. So Binlog_read_error pointer is defined here. It should be
210 initialized in constructor by caller.
211 */
213
214 private:
215 /**
216 The binlog's position where it is reading. It is the position in logical
217 binlog file, but not the position of system file.
218 */
220 /** It is the entry of the low level stream pipeline. */
221 std::unique_ptr<Basic_seekable_istream> m_istream;
222 /** Name of the file opened */
223 std::string m_file_name;
224 /**
225 Read binlog magic from binlog file and check if it is valid binlog magic.
226
227 This function also takes care of setting up any other stream layer (i.e.
228 encryption) when needed.
229
230 @retval false The high level stream layer was recognized as a binary log.
231 @retval true Failure identifying the high level stream layer.
232 */
233 bool read_binlog_magic();
234};
235
236#ifdef MYSQL_SERVER
237/**
238 Binlog input file. It is responsible for opening binlog files generated by
239 the server itself, but not relaylog files.
240*/
242 public:
244
245 protected:
246 std::unique_ptr<Basic_seekable_istream> open_file(
247 const char *file_name) override;
248};
249
250/**
251 Relaylog input file. It is responsible for opening relay log files.
252*/
254 public:
256
257 protected:
258 std::unique_ptr<Basic_seekable_istream> open_file(
259 const char *file_name) override;
260};
261#endif
262
263#endif // BINLOG_ISTREAM_INCLUDED
Base class of binlog input files.
Definition: binlog_istream.h:159
bool open(const char *file_name)
Open a binlog file.
Definition: binlog_istream.cc:186
bool seek(my_off_t position) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:207
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:138
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:201
Basic_binlog_ifile & operator=(const Basic_binlog_ifile &)=delete
my_off_t length() override
Get length of the binlog file.
Definition: binlog_istream.cc:216
const std::string & file_name() const
Definition: binlog_istream.h:188
my_off_t position() const
Definition: binlog_istream.h:185
my_off_t m_position
The binlog's position where it is reading.
Definition: binlog_istream.h:219
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_istream.h:212
~Basic_binlog_ifile() override
Definition: binlog_istream.cc:181
bool is_open() const
Definition: binlog_istream.h:186
std::string m_file_name
Name of the file opened.
Definition: binlog_istream.h:223
Basic_binlog_ifile(Binlog_read_error *binlog_read_error)
Definition: binlog_istream.cc:176
std::unique_ptr< Basic_seekable_istream > m_istream
It is the entry of the low level stream pipeline.
Definition: binlog_istream.h:221
void close()
Close the binlog file it is reading.
Definition: binlog_istream.cc:194
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:60
Seekable_istream with decryption feature.
Definition: binlog_istream.h:120
bool seek(my_off_t offset) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:124
~Binlog_encryption_istream() override
Definition: binlog_istream.cc:136
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:81
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:117
void close()
Closes the stream.
Definition: binlog_istream.cc:111
std::unique_ptr< Basic_seekable_istream > m_down_istream
Definition: binlog_istream.h:151
my_off_t length() override
The total length of the stream.
Definition: binlog_istream.cc:132
std::unique_ptr< Stream_cipher > m_decryptor
Definition: binlog_istream.h:149
Binlog input file.
Definition: binlog_istream.h:241
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:220
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:82
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:88
Binlog_read_error(Error_type type)
Definition: binlog_istream.h:80
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
@ EVENT_UNSUPPORTED_NEW_VERSION
Definition: binlog_istream.h:76
@ 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:106
Error_type m_type
Definition: binlog_istream.h:112
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:83
Relaylog input file.
Definition: binlog_istream.h:253
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:231
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:418
required string type
Definition: replication_group_member_actions.proto:34
This file includes the major components for encrypting/decrypting binary log files.