-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbasic_istream.cc
118 lines (98 loc) · 3.74 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
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "sql/basic_istream.h"
#include <my_io.h>
#include <my_sys.h>
#include <mysql/psi/mysql_file.h>
IO_CACHE_istream::IO_CACHE_istream() {}
IO_CACHE_istream::~IO_CACHE_istream() { close(); }
bool IO_CACHE_istream::open(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key MY_ATTRIBUTE((unused)),
PSI_file_key log_cache_key,
#endif
const char *file_name, myf flags MY_ATTRIBUTE((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) {
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_EXECUTE_IF("simulate_seek_failure", return true;);
my_b_seek(&m_io_cache, offset);
return false;
}
Stdin_istream::Stdin_istream() {}
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 interpeted 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, 0,
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;
}