1
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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.
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.
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 */
17
More functions to be used with IO_CACHE files
20
#include "mysys_priv.h"
26
Copy contents of an IO_CACHE to a file.
30
cache IO_CACHE to copy from
34
Copy the contents of the cache to the file. The cache will be
35
re-inited to a read cache and will read from the beginning of the
38
If a failure to write fully occurs, the cache is only copied
42
Make this function solid by handling partial reads from the cache
43
in a correct manner: it should be atomic.
50
my_b_copy_to_file(IO_CACHE *cache, FILE *file)
52
size_t bytes_in_cache;
53
DBUG_ENTER("my_b_copy_to_file");
55
/* Reinit the cache to read from the beginning of the cache */
56
if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
58
bytes_in_cache= my_b_bytes_in_cache(cache);
61
if (my_fwrite(file, cache->read_pos, bytes_in_cache,
62
MYF(MY_WME | MY_NABP)) == (size_t) -1)
64
cache->read_pos= cache->read_end;
65
} while ((bytes_in_cache= my_b_fill(cache)));
66
if(cache->error == -1)
72
my_off_t my_b_append_tell(IO_CACHE* info)
75
Sometimes we want to make sure that the variable is not put into
76
a register in debugging mode so we can see its value in the core
79
# define dbug_volatile volatile
81
# define dbug_volatile
85
Prevent optimizer from putting res in a register when debugging
86
we need this to be able to see the value of res when the assert fails
88
dbug_volatile my_off_t res;
91
We need to lock the append buffer mutex to keep flush_io_cache()
92
from messing with the variables that we need in order to provide the
93
answer to the question.
95
mysql_mutex_lock(&info->append_buffer_lock);
99
Make sure EOF is where we think it is. Note that we cannot just use
100
my_tell() because we have a reader thread that could have left the
101
file offset in a non-EOF location
104
volatile my_off_t save_pos;
105
save_pos = my_tell(info->file,MYF(0));
106
my_seek(info->file,(my_off_t)0,MY_SEEK_END,MYF(0));
108
Save the value of my_tell in res so we can see it when studying coredump
110
DBUG_ASSERT(info->end_of_file - (info->append_read_pos-info->write_buffer)
111
== (res=my_tell(info->file,MYF(0))));
112
my_seek(info->file,save_pos,MY_SEEK_SET,MYF(0));
115
res = info->end_of_file + (info->write_pos-info->append_read_pos);
116
mysql_mutex_unlock(&info->append_buffer_lock);
120
my_off_t my_b_safe_tell(IO_CACHE *info)
122
if (unlikely(info->type == SEQ_READ_APPEND))
123
return my_b_append_tell(info);
124
return my_b_tell(info);
128
Make next read happen at the given position
129
For write cache, make next write happen at the given position
132
void my_b_seek(IO_CACHE *info,my_off_t pos)
135
DBUG_ENTER("my_b_seek");
136
DBUG_PRINT("enter",("pos: %lu", (ulong) pos));
140
Verify that it is OK to do seek in the non-append
141
area in SEQ_READ_APPEND cache
142
a) see if this always works
143
b) see if there is a better way to make it work
145
if (info->type == SEQ_READ_APPEND)
146
(void) flush_io_cache(info);
148
offset=(pos - info->pos_in_file);
150
if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
152
/* TODO: explain why this works if pos < info->pos_in_file */
153
if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer))
155
/* The read is in the current buffer; Reuse it */
156
info->read_pos = info->buffer + offset;
161
/* Force a new read on next my_b_read */
162
info->read_pos=info->read_end=info->buffer;
165
else if (info->type == WRITE_CACHE)
167
/* If write is in current buffer, reuse it */
168
if ((ulonglong) offset <
169
(ulonglong) (info->write_end - info->write_buffer))
171
info->write_pos = info->write_buffer + offset;
174
(void) flush_io_cache(info);
175
/* Correct buffer end so that we write in increments of IO_SIZE */
176
info->write_end=(info->write_buffer+info->buffer_length-
177
(pos & (IO_SIZE-1)));
179
info->pos_in_file=pos;
180
info->seek_not_done=1;
186
Fill buffer of the cache.
189
This assumes that you have already used all characters in the CACHE,
190
independent of the read_pos value!
193
0 On error or EOF (info->error = -1 on error)
194
# Number of characters
198
size_t my_b_fill(IO_CACHE *info)
200
my_off_t pos_in_file=(info->pos_in_file+
201
(size_t) (info->read_end - info->buffer));
202
size_t diff_length, length, max_length;
204
if (info->seek_not_done)
205
{ /* File touched, do seek */
206
if (my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0)) ==
212
info->seek_not_done=0;
214
diff_length=(size_t) (pos_in_file & (IO_SIZE-1));
215
max_length=(info->read_length-diff_length);
216
if (max_length >= (info->end_of_file - pos_in_file))
217
max_length= (size_t) (info->end_of_file - pos_in_file);
224
DBUG_EXECUTE_IF ("simulate_my_b_fill_error",
225
{DBUG_SET("+d,simulate_file_read_error");});
226
if ((length= my_read(info->file,info->buffer,max_length,
227
info->myflags)) == (size_t) -1)
232
info->read_pos=info->buffer;
233
info->read_end=info->buffer+length;
234
info->pos_in_file=pos_in_file;
240
Read a string ended by '\n' into a buffer of 'max_length' size.
241
Returns number of characters read, 0 on error.
242
last byte is set to '\0'
243
If buffer is full then to[max_length-1] will be set to \0.
246
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
250
max_length--; /* Save place for end \0 */
252
/* Calculate number of characters in buffer */
253
if (!(length= my_b_bytes_in_cache(info)) &&
254
!(length= my_b_fill(info)))
260
if (length > max_length)
262
for (pos=info->read_pos,end=pos+length ; pos < end ;)
264
if ((*to++ = *pos++) == '\n')
268
return (size_t) (to-start);
271
if (!(max_length-=length))
273
/* Found enough charcters; Return found string */
276
return (size_t) (to-start);
278
if (!(length=my_b_fill(info)))
284
my_off_t my_b_filelength(IO_CACHE *info)
286
if (info->type == WRITE_CACHE)
287
return my_b_tell(info);
289
info->seek_not_done= 1;
290
return my_seek(info->file, 0L, MY_SEEK_END, MYF(0));
295
my_b_write_backtick_quote(IO_CACHE *info, const char *str, size_t len)
298
const uchar *p= (const uchar *)str;
299
const uchar *end= p + len;
303
if (my_b_write(info, (uchar *)"`", 1))
309
while (p < end && *p != '`')
312
if (count && my_b_write(info, start, count))
317
if (my_b_write(info, (uchar *)"``", 2))
322
if (my_b_write(info, (uchar *)"`", 1))
329
Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu"
330
Used for logging in MySQL
331
returns number of written character, or (size_t) -1 on error
334
size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
339
result=my_b_vprintf(info, fmt, args);
345
size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
347
size_t out_length= 0;
348
uint minimum_width; /* as yet unimplemented */
349
uint minimum_width_sign;
350
uint precision; /* as yet unimplemented for anything but %b */
351
my_bool is_zero_padded;
352
my_bool backtick_quoting;
355
Store the location of the beginning of a format directive, for the
356
case where we learn we shouldn't have been parsing a format string
357
at all, and we don't want to lose the flag/precision/width/size
360
const char* backtrack;
362
for (; *fmt != '\0'; fmt++)
364
/* Copy everything until '%' or end of string */
365
const char *start=fmt;
368
for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
370
length= (size_t) (fmt - start);
372
if (my_b_write(info, (const uchar*) start, length))
375
if (*fmt == '\0') /* End of format */
379
By this point, *fmt must be a percent; Keep track of this location and
380
skip over the percent character.
382
DBUG_ASSERT(*fmt == '%');
386
is_zero_padded= FALSE;
387
backtick_quoting= FALSE;
388
minimum_width_sign= 1;
391
/* Skip if max size is used (to be compatible with printf) */
397
minimum_width_sign= -1; fmt++; goto process_flags;
399
is_zero_padded= TRUE; fmt++; goto process_flags;
401
backtick_quoting= TRUE; fmt++; goto process_flags;
403
/** @todo Implement "#" conversion flag. */ fmt++; goto process_flags;
405
/** @todo Implement " " conversion flag. */ fmt++; goto process_flags;
407
/** @todo Implement "+" conversion flag. */ fmt++; goto process_flags;
412
precision= (int) va_arg(args, int);
417
while (my_isdigit(&my_charset_latin1, *fmt)) {
418
minimum_width=(minimum_width * 10) + (*fmt - '0');
422
minimum_width*= minimum_width_sign;
428
precision= (int) va_arg(args, int);
433
while (my_isdigit(&my_charset_latin1, *fmt)) {
434
precision=(precision * 10) + (*fmt - '0');
440
if (*fmt == 's') /* String parameter */
442
reg2 char *par = va_arg(args, char *);
443
size_t length2 = strlen(par);
444
/* TODO: implement precision */
445
if (backtick_quoting)
447
size_t total= my_b_write_backtick_quote(info, par, length2);
448
if (total == (size_t)-1)
454
out_length+= length2;
455
if (my_b_write(info, (uchar*) par, length2))
459
else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
461
char *par = va_arg(args, char *);
462
out_length+= precision;
463
if (my_b_write(info, (uchar*) par, precision))
466
else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
472
iarg = va_arg(args, int);
474
length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
476
length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
478
/* minimum width padding */
479
if (minimum_width > length2)
483
buffz= (uchar*) my_alloca(minimum_width - length2);
485
memset(buffz, '0', minimum_width - length2);
487
memset(buffz, ' ', minimum_width - length2);
488
if (my_b_write(info, buffz, minimum_width - length2))
496
out_length+= length2;
497
if (my_b_write(info, (uchar*) buff, length2))
500
else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
507
iarg = va_arg(args, long);
509
length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
511
length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
512
out_length+= length2;
513
if (my_b_write(info, (uchar*) buff, length2))
518
/* %% or unknown code */
519
if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
521
out_length+= fmt-backtrack;