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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/* Copyright (c) 2000, 2025, 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.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
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 */
/**
@file mysys/mf_iocache2.cc
More functions to be used with IO_CACHE files
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "m_ctype.h"
#include "m_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_sys.h"
#include "mysql/psi/mysql_file.h"
#include "template_utils.h"
/*
Copy contents of an IO_CACHE to a file.
SYNOPSIS
my_b_copy_to_file()
cache IO_CACHE to copy from
file File to copy to
DESCRIPTION
Copy the contents of the cache to the file. The cache will be
re-inited to a read cache and will read from the beginning of the
cache.
If a failure to write fully occurs, the cache is only copied
partially.
TODO
Make this function solid by handling partial reads from the cache
in a correct manner: it should be atomic.
RETURN VALUE
0 All OK
1 An error occurred
*/
int my_b_copy_to_file(IO_CACHE *cache, FILE *file) {
size_t bytes_in_cache;
DBUG_TRACE;
/* Reinit the cache to read from the beginning of the cache */
if (reinit_io_cache(cache, READ_CACHE, 0L, false, false)) return 1;
bytes_in_cache = my_b_bytes_in_cache(cache);
do {
if (my_fwrite(file, cache->read_pos, bytes_in_cache,
MYF(MY_WME | MY_NABP)) == (size_t)-1)
return 1;
cache->read_pos = cache->read_end;
} while ((bytes_in_cache = my_b_fill(cache)));
if (cache->error == -1) return 1;
return 0;
}
/*
Make next read happen at the given position
For write cache, make next write happen at the given position
*/
void my_b_seek(IO_CACHE *info, my_off_t pos) {
my_off_t offset;
DBUG_TRACE;
DBUG_PRINT("enter", ("pos: %lu", (ulong)pos));
/*
TODO:
Verify that it is OK to do seek in the non-append
area in SEQ_READ_APPEND cache
a) see if this always works
b) see if there is a better way to make it work
*/
if (info->type == SEQ_READ_APPEND) (void)flush_io_cache(info);
offset = (pos - info->pos_in_file);
if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND) {
/* TODO: explain why this works if pos < info->pos_in_file */
if ((ulonglong)offset < (ulonglong)(info->read_end - info->buffer)) {
/* The read is in the current buffer; Reuse it */
info->read_pos = info->buffer + offset;
return;
} else {
/* Force a new read on next my_b_read */
info->read_pos = info->read_end = info->buffer;
}
} else if (info->type == WRITE_CACHE) {
/* If write is in current buffer, reuse it */
if ((ulonglong)offset <=
(ulonglong)(info->write_end - info->write_buffer)) {
info->write_pos = info->write_buffer + offset;
return;
}
(void)flush_io_cache(info);
/* Correct buffer end so that we write in increments of IO_SIZE */
info->write_end =
(info->write_buffer + info->buffer_length - (pos & (IO_SIZE - 1)));
}
info->pos_in_file = pos;
info->seek_not_done = true;
}
/*
Fill buffer of the cache.
NOTES
This assumes that you have already used all characters in the CACHE,
independent of the read_pos value!
RETURN
0 On error or EOF (info->error = -1 on error)
# Number of characters
*/
size_t my_b_fill(IO_CACHE *info) {
my_off_t pos_in_file =
(info->pos_in_file + (size_t)(info->read_end - info->buffer));
size_t diff_length, length, max_length;
if (info->seek_not_done) { /* File touched, do seek */
if (mysql_encryption_file_seek(info, pos_in_file, MY_SEEK_SET, MYF(0)) ==
MY_FILEPOS_ERROR) {
info->error = 0;
return 0;
}
info->seek_not_done = false;
}
diff_length = (size_t)(pos_in_file & (IO_SIZE - 1));
max_length = (info->read_length - diff_length);
if (max_length >= (info->end_of_file - pos_in_file))
max_length = (size_t)(info->end_of_file - pos_in_file);
if (!max_length) {
info->error = 0;
return 0; /* EOF */
}
DBUG_EXECUTE_IF("simulate_my_b_fill_error",
{ DBUG_SET("+d,simulate_file_read_error"); });
if ((length = mysql_encryption_file_read(info, info->buffer, max_length,
info->myflags)) == (size_t)-1) {
info->error = -1;
return 0;
}
info->read_pos = info->buffer;
info->read_end = info->buffer + length;
info->pos_in_file = pos_in_file;
return length;
}
/*
Read a string ended by '\n' into a buffer of 'max_length' size.
Returns number of characters read, 0 on error.
last byte is set to '\0'
If buffer is full then to[max_length-1] will be set to \0.
*/
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length) {
char *start = to;
size_t length;
max_length--; /* Save place for end \0 */
/* Calculate number of characters in buffer */
if (!(length = my_b_bytes_in_cache(info)) && !(length = my_b_fill(info)))
return 0;
for (;;) {
uchar *pos, *end;
if (length > max_length) length = max_length;
for (pos = info->read_pos, end = pos + length; pos < end;) {
if ((*to++ = *pos++) == '\n') {
info->read_pos = pos;
*to = '\0';
return (size_t)(to - start);
}
}
if (!(max_length -= length)) {
/* Found enough characters; Return found string */
info->read_pos = pos;
*to = '\0';
return (size_t)(to - start);
}
if (!(length = my_b_fill(info))) return 0;
}
}
my_off_t my_b_filelength(IO_CACHE *info) {
if (info->type == WRITE_CACHE) return my_b_tell(info);
info->seek_not_done = true;
return mysql_file_seek(info->file, 0L, MY_SEEK_END, MYF(0));
}
/**
Simple printf version. Used for logging in MySQL.
Supports '%c', '%s', '%b', '%d', '%u', '%ld', '%lu' and '%llu'.
Returns number of written characters, or (size_t) -1 on error.
@param info The IO_CACHE to write to
@param fmt format string
@param ... variable list of arguments
@return number of bytes written, -1 if an error occurred
*/
size_t my_b_printf(IO_CACHE *info, const char *fmt, ...) {
size_t result;
va_list args;
va_start(args, fmt);
result = my_b_vprintf(info, fmt, args);
va_end(args);
return result;
}
/**
Implementation of my_b_printf.
@param info The IO_CACHE to write to
@param fmt format string
@param args variable list of arguments
@return number of bytes written, -1 if an error occurred
*/
size_t my_b_vprintf(IO_CACHE *info, const char *fmt, va_list args) {
size_t out_length = 0;
uint minimum_width; /* as yet unimplemented */
uint minimum_width_sign;
uint precision; /* as yet unimplemented for anything but %b */
bool is_zero_padded;
/*
Store the location of the beginning of a format directive, for the
case where we learn we shouldn't have been parsing a format string
at all, and we don't want to lose the flag/precision/width/size
information.
*/
const char *backtrack;
for (; *fmt != '\0'; fmt++) {
/* Copy everything until '%' or end of string */
const char *start = fmt;
size_t length;
for (; (*fmt != '\0') && (*fmt != '%'); fmt++)
;
length = (size_t)(fmt - start);
out_length += length;
if (my_b_write(info, (const uchar *)start, length)) goto err;
if (*fmt == '\0') /* End of format */
return out_length;
/*
By this point, *fmt must be a percent; Keep track of this location and
skip over the percent character.
*/
assert(*fmt == '%');
backtrack = fmt;
fmt++;
is_zero_padded = false;
minimum_width_sign = 1;
minimum_width = 0;
precision = 0;
/* Skip if max size is used (to be compatible with printf) */
process_flags:
switch (*fmt) {
case '-':
minimum_width_sign = -1;
fmt++;
goto process_flags;
case '0':
is_zero_padded = true;
fmt++;
goto process_flags;
case '#':
/** @todo Implement "#" conversion flag. */ fmt++;
goto process_flags;
case ' ':
/** @todo Implement " " conversion flag. */ fmt++;
goto process_flags;
case '+':
/** @todo Implement "+" conversion flag. */ fmt++;
goto process_flags;
}
if (*fmt == '*') {
minimum_width = (int)va_arg(args, int);
fmt++;
} else {
while (my_isdigit(&my_charset_latin1, *fmt)) {
minimum_width = (minimum_width * 10) + (*fmt - '0');
fmt++;
}
}
minimum_width *= minimum_width_sign;
if (*fmt == '.') {
fmt++;
if (*fmt == '*') {
precision = (int)va_arg(args, int);
fmt++;
} else {
while (my_isdigit(&my_charset_latin1, *fmt)) {
precision = (precision * 10) + (*fmt - '0');
fmt++;
}
}
}
if (*fmt == 's') /* String parameter */
{
char *par = va_arg(args, char *);
size_t length2 = strlen(par);
/* TODO: implement precision */
out_length += length2;
if (my_b_write(info, (uchar *)par, length2)) goto err;
} else if (*fmt == 'c') /* char type parameter */
{
char par[2];
par[0] = va_arg(args, int);
out_length++;
if (my_b_write(info, (uchar *)par, 1)) goto err;
} else if (*fmt ==
'b') /* Sized buffer parameter, only precision makes sense */
{
char *par = va_arg(args, char *);
out_length += precision;
if (my_b_write(info, (uchar *)par, precision)) goto err;
} else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
{
size_t length2;
char buff[32];
if (*fmt == 'd')
length2 = longlong10_to_str(va_arg(args, int), buff, -10) - buff;
else
length2 = longlong10_to_str(va_arg(args, unsigned), buff, 10) - buff;
/* minimum width padding */
if (minimum_width > length2) {
char *buffz;
buffz = static_cast<char *>(my_alloca(minimum_width - length2));
if (is_zero_padded)
memset(buffz, '0', minimum_width - length2);
else
memset(buffz, ' ', minimum_width - length2);
if (my_b_write(info, pointer_cast<uchar *>(buffz),
minimum_width - length2)) {
goto err;
}
}
out_length += length2;
if (my_b_write(info, (uchar *)buff, length2)) goto err;
} else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
/* long parameter */
{
size_t length2;
char buff[32];
if (*++fmt == 'd')
length2 = longlong10_to_str(va_arg(args, long), buff, -10) - buff;
else
length2 =
longlong10_to_str(va_arg(args, unsigned long), buff, 10) - buff;
out_length += length2;
if (my_b_write(info, (uchar *)buff, length2)) goto err;
} else if (fmt[0] == 'l' && fmt[1] == 'l' && fmt[2] == 'u') {
ulonglong iarg;
size_t length2;
char buff[32];
iarg = va_arg(args, ulonglong);
length2 = (size_t)(longlong10_to_str(iarg, buff, 10) - buff);
out_length += length2;
fmt += 2;
if (my_b_write(info, (uchar *)buff, length2)) goto err;
} else {
/* %% or unknown code */
if (my_b_write(info, pointer_cast<const uchar *>(backtrack),
fmt - backtrack))
goto err;
out_length += fmt - backtrack;
}
}
return out_length;
err:
return (size_t)-1;
}
|