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