1 /* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
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 #include "sql/basic_istream.h"
24 #include <my_io.h>
25 #include <my_sys.h>
26 #include <mysql/psi/mysql_file.h>
27 
IO_CACHE_istream()28 IO_CACHE_istream::IO_CACHE_istream() {}
29 
~IO_CACHE_istream()30 IO_CACHE_istream::~IO_CACHE_istream() { close(); }
31 
open(PSI_file_key log_file_key MY_ATTRIBUTE ((unused)),PSI_file_key log_cache_key,const char * file_name,myf flags MY_ATTRIBUTE ((unused)),size_t cache_size)32 bool IO_CACHE_istream::open(
33 #ifdef HAVE_PSI_INTERFACE
34     PSI_file_key log_file_key MY_ATTRIBUTE((unused)),
35     PSI_file_key log_cache_key,
36 #endif
37     const char *file_name, myf flags MY_ATTRIBUTE((unused)),
38     size_t cache_size) {
39   File file = -1;
40 
41   file = mysql_file_open(log_file_key, file_name, O_RDONLY, MYF(MY_WME));
42   if (file < 0) return true;
43 
44 #ifdef HAVE_PSI_INTERFACE
45   if (init_io_cache_ext(&m_io_cache, file, cache_size, READ_CACHE, 0, false,
46                         flags, log_cache_key))
47 #else
48   if (init_io_cache(&m_io_cache, file, cache_size, READ_CACHE, 0, false,
49                     MYF(MY_WME | MY_DONT_CHECK_FILESIZE)))
50 #endif
51   {
52     mysql_file_close(file, MYF(0));
53     return true;
54   }
55   return false;
56 }
57 
close()58 void IO_CACHE_istream::close() {
59   if (my_b_inited(&m_io_cache)) {
60     end_io_cache(&m_io_cache);
61     mysql_file_close(m_io_cache.file, MYF(MY_WME));
62   }
63 }
64 
length()65 my_off_t IO_CACHE_istream::length() { return my_b_filelength(&m_io_cache); }
66 
read(unsigned char * buffer,size_t length)67 ssize_t IO_CACHE_istream::read(unsigned char *buffer, size_t length) {
68   DBUG_TRACE;
69   if (my_b_read(&m_io_cache, buffer, length) ||
70       DBUG_EVALUATE_IF("simulate_magic_header_io_failure", 1, 0))
71     return m_io_cache.error;
72   return static_cast<longlong>(length);
73 }
74 
seek(my_off_t offset)75 bool IO_CACHE_istream::seek(my_off_t offset) {
76   DBUG_TRACE;
77   bool res = false;
78   my_b_seek(&m_io_cache, offset);
79   DBUG_EXECUTE_IF("simulate_seek_failure", res = true;);
80   return res;
81 }
82 
Stdin_istream()83 Stdin_istream::Stdin_istream() {}
84 
~Stdin_istream()85 Stdin_istream::~Stdin_istream() { close(); }
86 
open(std::string * errmsg)87 bool Stdin_istream::open(std::string *errmsg) {
88 /* read from stdin */
89 /*
90   Windows opens stdin in text mode by default. Certain characters
91   such as CTRL-Z are interpeted as events and the read() method
92   will stop. CTRL-Z is the EOF marker in Windows. to get past this
93   you have to open stdin in binary mode. Setmode() is used to set
94   stdin in binary mode. Errors on setting this mode result in
95   halting the function and printing an error message to stderr.
96 */
97 #if defined(_WIN32)
98   if (_setmode(fileno(stdin), _O_BINARY) == -1) {
99     *errmsg = "Could not set binary mode on stdin.";
100     return true;
101   }
102 #endif
103   if (init_io_cache(
104           &m_io_cache, my_fileno(stdin), 0, READ_CACHE, 0, false,
105           MYF(MY_WME | MY_NABP | MY_DONT_CHECK_FILESIZE | MY_FULL_IO))) {
106     *errmsg = "Failed to init IO cache.";
107     return true;
108   }
109   return false;
110 }
111 
close()112 void Stdin_istream::close() { end_io_cache(&m_io_cache); }
113 
read(unsigned char * buffer,size_t length)114 ssize_t Stdin_istream::read(unsigned char *buffer, size_t length) {
115   if (my_b_read(&m_io_cache, buffer, length)) return m_io_cache.error;
116   return static_cast<longlong>(length);
117 }
118 
skip(my_off_t bytes)119 bool Stdin_istream::skip(my_off_t bytes) {
120   /* Just refill the cache If all data in it should be skipped. */
121   while (bytes > my_b_bytes_in_cache(&m_io_cache)) {
122     bytes -= my_b_bytes_in_cache(&m_io_cache);
123     if (my_b_fill(&m_io_cache) == 0) return m_io_cache.error == -1;
124   }
125 
126   m_io_cache.read_pos += bytes;
127   return false;
128 }
129