-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbasic_istream.cc
129 lines (108 loc) · 4.31 KB
/
basic_istream.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/basic_istream.h"
#include <my_io.h>
#include <my_sys.h>
#include <mysql/psi/mysql_file.h>
#include "my_dbug.h"
#include "my_inttypes.h"
IO_CACHE_istream::IO_CACHE_istream() = default;
IO_CACHE_istream::~IO_CACHE_istream() { close(); }
bool IO_CACHE_istream::open(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key [[maybe_unused]], PSI_file_key log_cache_key,
#endif
const char *file_name, myf flags [[maybe_unused]], size_t cache_size) {
File file = -1;
file = mysql_file_open(log_file_key, file_name, O_RDONLY, MYF(MY_WME));
if (file < 0) return true;
#ifdef HAVE_PSI_INTERFACE
if (init_io_cache_ext(&m_io_cache, file, cache_size, READ_CACHE, 0, false,
flags, log_cache_key))
#else
if (init_io_cache(&m_io_cache, file, cache_size, READ_CACHE, 0, false,
MYF(MY_WME | MY_DONT_CHECK_FILESIZE)))
#endif
{
mysql_file_close(file, MYF(0));
return true;
}
return false;
}
void IO_CACHE_istream::close() {
if (my_b_inited(&m_io_cache)) {
end_io_cache(&m_io_cache);
mysql_file_close(m_io_cache.file, MYF(MY_WME));
}
}
my_off_t IO_CACHE_istream::length() { return my_b_filelength(&m_io_cache); }
ssize_t IO_CACHE_istream::read(unsigned char *buffer, size_t length) {
DBUG_TRACE;
if (my_b_read(&m_io_cache, buffer, length) ||
DBUG_EVALUATE_IF("simulate_magic_header_io_failure", 1, 0))
return m_io_cache.error;
return static_cast<longlong>(length);
}
bool IO_CACHE_istream::seek(my_off_t offset) {
DBUG_TRACE;
bool res = false;
my_b_seek(&m_io_cache, offset);
DBUG_EXECUTE_IF("simulate_seek_failure", res = true;);
return res;
}
Stdin_istream::Stdin_istream() = default;
Stdin_istream::~Stdin_istream() { close(); }
bool Stdin_istream::open(std::string *errmsg) {
/* read from stdin */
/*
Windows opens stdin in text mode by default. Certain characters
such as CTRL-Z are interpreted as events and the read() method
will stop. CTRL-Z is the EOF marker in Windows. to get past this
you have to open stdin in binary mode. Setmode() is used to set
stdin in binary mode. Errors on setting this mode result in
halting the function and printing an error message to stderr.
*/
#if defined(_WIN32)
if (_setmode(fileno(stdin), _O_BINARY) == -1) {
*errmsg = "Could not set binary mode on stdin.";
return true;
}
#endif
if (init_io_cache(
&m_io_cache, my_fileno(stdin), 0, READ_CACHE, 0, false,
MYF(MY_WME | MY_NABP | MY_DONT_CHECK_FILESIZE | MY_FULL_IO))) {
*errmsg = "Failed to init IO cache.";
return true;
}
return false;
}
void Stdin_istream::close() { end_io_cache(&m_io_cache); }
ssize_t Stdin_istream::read(unsigned char *buffer, size_t length) {
if (my_b_read(&m_io_cache, buffer, length)) return m_io_cache.error;
return static_cast<longlong>(length);
}
bool Stdin_istream::skip(my_off_t bytes) {
/* Just refill the cache If all data in it should be skipped. */
while (bytes > my_b_bytes_in_cache(&m_io_cache)) {
bytes -= my_b_bytes_in_cache(&m_io_cache);
if (my_b_fill(&m_io_cache) == 0) return m_io_cache.error == -1;
}
m_io_cache.read_pos += bytes;
return false;
}