1
/* Copyright (C) 2000 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
More functions to be used with IO_CACHE files
20
#define MAP_TO_USE_RAID
21
#include "mysys_priv.h"
27
Copy contents of an IO_CACHE to a file.
31
cache IO_CACHE to copy from
35
Copy the contents of the cache to the file. The cache will be
36
re-inited to a read cache and will read from the beginning of the
39
If a failure to write fully occurs, the cache is only copied
43
Make this function solid by handling partial reads from the cache
44
in a correct manner: it should be atomic.
51
my_b_copy_to_file(IO_CACHE *cache, FILE *file)
53
size_t bytes_in_cache;
54
DBUG_ENTER("my_b_copy_to_file");
56
/* Reinit the cache to read from the beginning of the cache */
57
if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
59
bytes_in_cache= my_b_bytes_in_cache(cache);
62
if (my_fwrite(file, cache->read_pos, bytes_in_cache,
63
MYF(MY_WME | MY_NABP)) == (size_t) -1)
65
cache->read_pos= cache->read_end;
66
} while ((bytes_in_cache= my_b_fill(cache)));
71
my_off_t my_b_append_tell(IO_CACHE* info)
74
Prevent optimizer from putting res in a register when debugging
75
we need this to be able to see the value of res when the assert fails
77
dbug_volatile my_off_t res;
80
We need to lock the append buffer mutex to keep flush_io_cache()
81
from messing with the variables that we need in order to provide the
82
answer to the question.
85
pthread_mutex_lock(&info->append_buffer_lock);
89
Make sure EOF is where we think it is. Note that we cannot just use
90
my_tell() because we have a reader thread that could have left the
91
file offset in a non-EOF location
94
volatile my_off_t save_pos;
95
save_pos = my_tell(info->file,MYF(0));
96
my_seek(info->file,(my_off_t)0,MY_SEEK_END,MYF(0));
98
Save the value of my_tell in res so we can see it when studying coredump
100
DBUG_ASSERT(info->end_of_file - (info->append_read_pos-info->write_buffer)
101
== (res=my_tell(info->file,MYF(0))));
102
my_seek(info->file,save_pos,MY_SEEK_SET,MYF(0));
105
res = info->end_of_file + (info->write_pos-info->append_read_pos);
107
pthread_mutex_unlock(&info->append_buffer_lock);
112
my_off_t my_b_safe_tell(IO_CACHE *info)
114
if (unlikely(info->type == SEQ_READ_APPEND))
115
return my_b_append_tell(info);
116
return my_b_tell(info);
120
Make next read happen at the given position
121
For write cache, make next write happen at the given position
124
void my_b_seek(IO_CACHE *info,my_off_t pos)
127
DBUG_ENTER("my_b_seek");
128
DBUG_PRINT("enter",("pos: %lu", (ulong) pos));
132
Verify that it is OK to do seek in the non-append
133
area in SEQ_READ_APPEND cache
134
a) see if this always works
135
b) see if there is a better way to make it work
137
if (info->type == SEQ_READ_APPEND)
138
VOID(flush_io_cache(info));
140
offset=(pos - info->pos_in_file);
142
if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
144
/* TODO: explain why this works if pos < info->pos_in_file */
145
if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer))
147
/* The read is in the current buffer; Reuse it */
148
info->read_pos = info->buffer + offset;
153
/* Force a new read on next my_b_read */
154
info->read_pos=info->read_end=info->buffer;
157
else if (info->type == WRITE_CACHE)
159
/* If write is in current buffer, reuse it */
160
if ((ulonglong) offset <
161
(ulonglong) (info->write_end - info->write_buffer))
163
info->write_pos = info->write_buffer + offset;
166
VOID(flush_io_cache(info));
167
/* Correct buffer end so that we write in increments of IO_SIZE */
168
info->write_end=(info->write_buffer+info->buffer_length-
169
(pos & (IO_SIZE-1)));
171
info->pos_in_file=pos;
172
info->seek_not_done=1;
178
Fill buffer of the cache.
181
This assumes that you have already used all characters in the CACHE,
182
independent of the read_pos value!
185
0 On error or EOF (info->error = -1 on error)
186
# Number of characters
190
size_t my_b_fill(IO_CACHE *info)
192
my_off_t pos_in_file=(info->pos_in_file+
193
(size_t) (info->read_end - info->buffer));
194
size_t diff_length, length, max_length;
196
if (info->seek_not_done)
197
{ /* File touched, do seek */
198
if (my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0)) ==
204
info->seek_not_done=0;
206
diff_length=(size_t) (pos_in_file & (IO_SIZE-1));
207
max_length=(info->read_length-diff_length);
208
if (max_length >= (info->end_of_file - pos_in_file))
209
max_length= (size_t) (info->end_of_file - pos_in_file);
216
if ((length= my_read(info->file,info->buffer,max_length,
217
info->myflags)) == (size_t) -1)
222
info->read_pos=info->buffer;
223
info->read_end=info->buffer+length;
224
info->pos_in_file=pos_in_file;
230
Read a string ended by '\n' into a buffer of 'max_length' size.
231
Returns number of characters read, 0 on error.
232
last byte is set to '\0'
233
If buffer is full then to[max_length-1] will be set to \0.
236
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
240
max_length--; /* Save place for end \0 */
242
/* Calculate number of characters in buffer */
243
if (!(length= my_b_bytes_in_cache(info)) &&
244
!(length= my_b_fill(info)))
250
if (length > max_length)
252
for (pos=info->read_pos,end=pos+length ; pos < end ;)
254
if ((*to++ = *pos++) == '\n')
258
return (size_t) (to-start);
261
if (!(max_length-=length))
263
/* Found enough charcters; Return found string */
266
return (size_t) (to-start);
268
if (!(length=my_b_fill(info)))
274
my_off_t my_b_filelength(IO_CACHE *info)
276
if (info->type == WRITE_CACHE)
277
return my_b_tell(info);
279
info->seek_not_done= 1;
280
return my_seek(info->file, 0L, MY_SEEK_END, MYF(0));
285
Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu"
286
Used for logging in MySQL
287
returns number of written character, or (size_t) -1 on error
290
size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
295
result=my_b_vprintf(info, fmt, args);
301
size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
303
size_t out_length= 0;
304
uint minimum_width; /* as yet unimplemented */
305
uint minimum_width_sign;
306
uint precision; /* as yet unimplemented for anything but %b */
307
my_bool is_zero_padded;
310
Store the location of the beginning of a format directive, for the
311
case where we learn we shouldn't have been parsing a format string
312
at all, and we don't want to lose the flag/precision/width/size
315
const char* backtrack;
317
for (; *fmt != '\0'; fmt++)
319
/* Copy everything until '%' or end of string */
320
const char *start=fmt;
323
for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
325
length= (size_t) (fmt - start);
327
if (my_b_write(info, (const uchar*) start, length))
330
if (*fmt == '\0') /* End of format */
334
By this point, *fmt must be a percent; Keep track of this location and
335
skip over the percent character.
337
DBUG_ASSERT(*fmt == '%');
341
is_zero_padded= FALSE;
342
minimum_width_sign= 1;
345
/* Skip if max size is used (to be compatible with printf) */
351
minimum_width_sign= -1; fmt++; goto process_flags;
353
is_zero_padded= TRUE; fmt++; goto process_flags;
355
/** @todo Implement "#" conversion flag. */ fmt++; goto process_flags;
357
/** @todo Implement " " conversion flag. */ fmt++; goto process_flags;
359
/** @todo Implement "+" conversion flag. */ fmt++; goto process_flags;
364
precision= (int) va_arg(args, int);
369
while (my_isdigit(&my_charset_latin1, *fmt)) {
370
minimum_width=(minimum_width * 10) + (*fmt - '0');
374
minimum_width*= minimum_width_sign;
380
precision= (int) va_arg(args, int);
385
while (my_isdigit(&my_charset_latin1, *fmt)) {
386
precision=(precision * 10) + (*fmt - '0');
392
if (*fmt == 's') /* String parameter */
394
register char *par = va_arg(args, char *);
395
size_t length2 = strlen(par);
396
/* TODO: implement precision */
397
out_length+= length2;
398
if (my_b_write(info, (uchar*) par, length2))
401
else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
403
char *par = va_arg(args, char *);
404
out_length+= precision;
405
if (my_b_write(info, (uchar*) par, precision))
408
else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
414
iarg = va_arg(args, int);
416
length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
418
length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
420
/* minimum width padding */
421
if (minimum_width > length2)
425
buffz= my_alloca(minimum_width - length2);
427
memset(buffz, '0', minimum_width - length2);
429
memset(buffz, ' ', minimum_width - length2);
430
my_b_write(info, buffz, minimum_width - length2);
434
out_length+= length2;
435
if (my_b_write(info, (uchar*) buff, length2))
438
else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
445
iarg = va_arg(args, long);
447
length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
449
length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
450
out_length+= length2;
451
if (my_b_write(info, (uchar*) buff, length2))
456
/* %% or unknown code */
457
if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
459
out_length+= fmt-backtrack;