| 1 | /* Copyright (c) 2003, 2010, 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 as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | #ifdef USE_PRAGMA_IMPLEMENTATION |
| 17 | #pragma implementation // gcc: Class implementation |
| 18 | #endif |
| 19 | |
| 20 | #include <my_global.h> |
| 21 | #include "sql_priv.h" |
| 22 | #include <mysql/psi/mysql_file.h> |
| 23 | #include "transparent_file.h" |
| 24 | #include "my_sys.h" // MY_WME, MY_ALLOW_ZERO_PTR, MY_SEEK_SET |
| 25 | |
| 26 | Transparent_file::Transparent_file() : lower_bound(0), buff_size(IO_SIZE) |
| 27 | { |
| 28 | buff= (uchar *) my_malloc(buff_size*sizeof(uchar), MYF(MY_WME)); |
| 29 | } |
| 30 | |
| 31 | Transparent_file::~Transparent_file() |
| 32 | { |
| 33 | my_free(buff); |
| 34 | } |
| 35 | |
| 36 | void Transparent_file::init_buff(File filedes_arg) |
| 37 | { |
| 38 | filedes= filedes_arg; |
| 39 | /* read the beginning of the file */ |
| 40 | lower_bound= 0; |
| 41 | mysql_file_seek(filedes, 0, MY_SEEK_SET, MYF(0)); |
| 42 | if (filedes && buff) |
| 43 | upper_bound= mysql_file_read(filedes, buff, buff_size, MYF(0)); |
| 44 | } |
| 45 | |
| 46 | uchar *Transparent_file::ptr() |
| 47 | { |
| 48 | return buff; |
| 49 | } |
| 50 | |
| 51 | my_off_t Transparent_file::start() |
| 52 | { |
| 53 | return lower_bound; |
| 54 | } |
| 55 | |
| 56 | my_off_t Transparent_file::end() |
| 57 | { |
| 58 | return upper_bound; |
| 59 | } |
| 60 | |
| 61 | my_off_t Transparent_file::read_next() |
| 62 | { |
| 63 | size_t bytes_read; |
| 64 | |
| 65 | /* |
| 66 | No need to seek here, as the file managed by Transparent_file class |
| 67 | always points to upper_bound byte |
| 68 | */ |
| 69 | if ((bytes_read= mysql_file_read(filedes, buff, buff_size, MYF(0))) |
| 70 | == MY_FILE_ERROR) |
| 71 | return (my_off_t) -1; |
| 72 | |
| 73 | /* end of file */ |
| 74 | if (!bytes_read) |
| 75 | return (my_off_t) -1; |
| 76 | |
| 77 | lower_bound= upper_bound; |
| 78 | upper_bound+= bytes_read; |
| 79 | |
| 80 | return lower_bound; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | char Transparent_file::get_value(my_off_t offset) |
| 85 | { |
| 86 | size_t bytes_read; |
| 87 | |
| 88 | /* check boundaries */ |
| 89 | if ((lower_bound <= offset) && (((my_off_t) offset) < upper_bound)) |
| 90 | return buff[offset - lower_bound]; |
| 91 | |
| 92 | mysql_file_seek(filedes, offset, MY_SEEK_SET, MYF(0)); |
| 93 | /* read appropriate portion of the file */ |
| 94 | if ((bytes_read= mysql_file_read(filedes, buff, buff_size, |
| 95 | MYF(0))) == MY_FILE_ERROR) |
| 96 | return 0; |
| 97 | |
| 98 | lower_bound= offset; |
| 99 | upper_bound= lower_bound + bytes_read; |
| 100 | |
| 101 | /* end of file */ |
| 102 | if (upper_bound == (my_off_t) offset) |
| 103 | return 0; |
| 104 | |
| 105 | return buff[0]; |
| 106 | } |
| 107 | |