~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to mysys/mf_iocache2.c

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2000, 2013, 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
/*
 
17
  More functions to be used with IO_CACHE files
 
18
*/
 
19
 
 
20
#include "mysys_priv.h"
 
21
#include <m_string.h>
 
22
#include <stdarg.h>
 
23
#include <m_ctype.h>
 
24
 
 
25
/*
 
26
  Copy contents of an IO_CACHE to a file.
 
27
 
 
28
  SYNOPSIS
 
29
    my_b_copy_to_file()
 
30
    cache  IO_CACHE to copy from
 
31
    file   File to copy to
 
32
 
 
33
  DESCRIPTION
 
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
 
36
    cache.
 
37
 
 
38
    If a failure to write fully occurs, the cache is only copied
 
39
    partially.
 
40
 
 
41
  TODO
 
42
    Make this function solid by handling partial reads from the cache
 
43
    in a correct manner: it should be atomic.
 
44
 
 
45
  RETURN VALUE
 
46
    0  All OK
 
47
    1  An error occured
 
48
*/
 
49
int
 
50
my_b_copy_to_file(IO_CACHE *cache, FILE *file)
 
51
{
 
52
  size_t bytes_in_cache;
 
53
  DBUG_ENTER("my_b_copy_to_file");
 
54
 
 
55
  /* Reinit the cache to read from the beginning of the cache */
 
56
  if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
 
57
    DBUG_RETURN(1);
 
58
  bytes_in_cache= my_b_bytes_in_cache(cache);
 
59
  do
 
60
  {
 
61
    if (my_fwrite(file, cache->read_pos, bytes_in_cache,
 
62
                  MYF(MY_WME | MY_NABP)) == (size_t) -1)
 
63
      DBUG_RETURN(1);
 
64
    cache->read_pos= cache->read_end;
 
65
  } while ((bytes_in_cache= my_b_fill(cache)));
 
66
  if(cache->error == -1)
 
67
    DBUG_RETURN(1);
 
68
  DBUG_RETURN(0);
 
69
}
 
70
 
 
71
 
 
72
my_off_t my_b_append_tell(IO_CACHE* info)
 
73
{
 
74
  /*
 
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
 
77
  */
 
78
#ifndef DBUG_OFF
 
79
# define dbug_volatile volatile
 
80
#else
 
81
# define dbug_volatile
 
82
#endif
 
83
 
 
84
  /*
 
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
 
87
  */
 
88
  dbug_volatile my_off_t res; 
 
89
 
 
90
  /*
 
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.
 
94
  */
 
95
  mysql_mutex_lock(&info->append_buffer_lock);
 
96
 
 
97
#ifndef DBUG_OFF
 
98
  /*
 
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
 
102
  */
 
103
  {
 
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));
 
107
    /*
 
108
      Save the value of my_tell in res so we can see it when studying coredump
 
109
    */
 
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));
 
113
  }
 
114
#endif  
 
115
  res = info->end_of_file + (info->write_pos-info->append_read_pos);
 
116
  mysql_mutex_unlock(&info->append_buffer_lock);
 
117
  return res;
 
118
}
 
119
 
 
120
my_off_t my_b_safe_tell(IO_CACHE *info)
 
121
{
 
122
  if (unlikely(info->type == SEQ_READ_APPEND))
 
123
    return my_b_append_tell(info);
 
124
  return my_b_tell(info);
 
125
}
 
126
 
 
127
/*
 
128
  Make next read happen at the given position
 
129
  For write cache, make next write happen at the given position
 
130
*/
 
131
 
 
132
void my_b_seek(IO_CACHE *info,my_off_t pos)
 
133
{
 
134
  my_off_t offset;
 
135
  DBUG_ENTER("my_b_seek");
 
136
  DBUG_PRINT("enter",("pos: %lu", (ulong) pos));
 
137
 
 
138
  /*
 
139
    TODO:
 
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
 
144
  */
 
145
  if (info->type == SEQ_READ_APPEND)
 
146
    (void) flush_io_cache(info);
 
147
 
 
148
  offset=(pos - info->pos_in_file);
 
149
 
 
150
  if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
 
151
  {
 
152
    /* TODO: explain why this works if pos < info->pos_in_file */
 
153
    if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer))
 
154
    {
 
155
      /* The read is in the current buffer; Reuse it */
 
156
      info->read_pos = info->buffer + offset;
 
157
      DBUG_VOID_RETURN;
 
158
    }
 
159
    else
 
160
    {
 
161
      /* Force a new read on next my_b_read */
 
162
      info->read_pos=info->read_end=info->buffer;
 
163
    }
 
164
  }
 
165
  else if (info->type == WRITE_CACHE)
 
166
  {
 
167
    /* If write is in current buffer, reuse it */
 
168
    if ((ulonglong) offset <
 
169
        (ulonglong) (info->write_end - info->write_buffer))
 
170
    {
 
171
      info->write_pos = info->write_buffer + offset;
 
172
      DBUG_VOID_RETURN;
 
173
    }
 
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)));
 
178
  }
 
179
  info->pos_in_file=pos;
 
180
  info->seek_not_done=1;
 
181
  DBUG_VOID_RETURN;
 
182
}
 
183
 
 
184
 
 
185
/*
 
186
  Fill buffer of the cache.
 
187
 
 
188
  NOTES
 
189
    This assumes that you have already used all characters in the CACHE,
 
190
    independent of the read_pos value!
 
191
 
 
192
  RETURN
 
193
  0  On error or EOF (info->error = -1 on error)
 
194
  #  Number of characters
 
195
*/
 
196
 
 
197
 
 
198
size_t my_b_fill(IO_CACHE *info)
 
199
{
 
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;
 
203
 
 
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)) ==
 
207
        MY_FILEPOS_ERROR)
 
208
    {
 
209
      info->error= 0;
 
210
      return 0;
 
211
    }
 
212
    info->seek_not_done=0;
 
213
  }
 
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);
 
218
 
 
219
  if (!max_length)
 
220
  {
 
221
    info->error= 0;
 
222
    return 0;                                   /* EOF */
 
223
  }
 
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)
 
228
  {
 
229
    info->error= -1;
 
230
    return 0;
 
231
  }
 
232
  info->read_pos=info->buffer;
 
233
  info->read_end=info->buffer+length;
 
234
  info->pos_in_file=pos_in_file;
 
235
  return length;
 
236
}
 
237
 
 
238
 
 
239
/*
 
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.
 
244
*/
 
245
 
 
246
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
 
247
{
 
248
  char *start = to;
 
249
  size_t length;
 
250
  max_length--;                                 /* Save place for end \0 */
 
251
 
 
252
  /* Calculate number of characters in buffer */
 
253
  if (!(length= my_b_bytes_in_cache(info)) &&
 
254
      !(length= my_b_fill(info)))
 
255
    return 0;
 
256
 
 
257
  for (;;)
 
258
  {
 
259
    uchar *pos, *end;
 
260
    if (length > max_length)
 
261
      length=max_length;
 
262
    for (pos=info->read_pos,end=pos+length ; pos < end ;)
 
263
    {
 
264
      if ((*to++ = *pos++) == '\n')
 
265
      {
 
266
        info->read_pos=pos;
 
267
        *to='\0';
 
268
        return (size_t) (to-start);
 
269
      }
 
270
    }
 
271
    if (!(max_length-=length))
 
272
    {
 
273
     /* Found enough charcters;  Return found string */
 
274
      info->read_pos=pos;
 
275
      *to='\0';
 
276
      return (size_t) (to-start);
 
277
    }
 
278
    if (!(length=my_b_fill(info)))
 
279
      return 0;
 
280
  }
 
281
}
 
282
 
 
283
 
 
284
my_off_t my_b_filelength(IO_CACHE *info)
 
285
{
 
286
  if (info->type == WRITE_CACHE)
 
287
    return my_b_tell(info);
 
288
 
 
289
  info->seek_not_done= 1;
 
290
  return my_seek(info->file, 0L, MY_SEEK_END, MYF(0));
 
291
}
 
292
 
 
293
 
 
294
size_t
 
295
my_b_write_backtick_quote(IO_CACHE *info, const char *str, size_t len)
 
296
{
 
297
  const uchar *start;
 
298
  const uchar *p= (const uchar *)str;
 
299
  const uchar *end= p + len;
 
300
  size_t count;
 
301
  size_t total= 0;
 
302
 
 
303
  if (my_b_write(info, (uchar *)"`", 1))
 
304
    return (size_t)-1;
 
305
  ++total;
 
306
  for (;;)
 
307
  {
 
308
    start= p;
 
309
    while (p < end && *p != '`')
 
310
      ++p;
 
311
    count= p - start;
 
312
    if (count && my_b_write(info, start, count))
 
313
      return (size_t)-1;
 
314
    total+= count;
 
315
    if (p >= end)
 
316
      break;
 
317
    if (my_b_write(info, (uchar *)"``", 2))
 
318
      return (size_t)-1;
 
319
    total+= 2;
 
320
    ++p;
 
321
  }
 
322
  if (my_b_write(info, (uchar *)"`", 1))
 
323
    return (size_t)-1;
 
324
  ++total;
 
325
  return total;
 
326
}
 
327
 
 
328
/*
 
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
 
332
*/
 
333
 
 
334
size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
 
335
{
 
336
  size_t result;
 
337
  va_list args;
 
338
  va_start(args,fmt);
 
339
  result=my_b_vprintf(info, fmt, args);
 
340
  va_end(args);
 
341
  return result;
 
342
}
 
343
 
 
344
 
 
345
size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
 
346
{
 
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;
 
353
 
 
354
  /*
 
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
 
358
    information.
 
359
   */
 
360
  const char* backtrack;
 
361
 
 
362
  for (; *fmt != '\0'; fmt++)
 
363
  {
 
364
    /* Copy everything until '%' or end of string */
 
365
    const char *start=fmt;
 
366
    size_t length;
 
367
    
 
368
    for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
 
369
 
 
370
    length= (size_t) (fmt - start);
 
371
    out_length+=length;
 
372
    if (my_b_write(info, (const uchar*) start, length))
 
373
      goto err;
 
374
 
 
375
    if (*fmt == '\0')                           /* End of format */
 
376
      return out_length;
 
377
 
 
378
    /* 
 
379
      By this point, *fmt must be a percent;  Keep track of this location and
 
380
      skip over the percent character. 
 
381
    */
 
382
    DBUG_ASSERT(*fmt == '%');
 
383
    backtrack= fmt;
 
384
    fmt++;
 
385
 
 
386
    is_zero_padded= FALSE;
 
387
    backtick_quoting= FALSE;
 
388
    minimum_width_sign= 1;
 
389
    minimum_width= 0;
 
390
    precision= 0;
 
391
    /* Skip if max size is used (to be compatible with printf) */
 
392
 
 
393
process_flags:
 
394
    switch (*fmt)
 
395
    {
 
396
      case '-': 
 
397
        minimum_width_sign= -1; fmt++; goto process_flags;
 
398
      case '0':
 
399
        is_zero_padded= TRUE; fmt++; goto process_flags;
 
400
      case '`':
 
401
        backtick_quoting= TRUE; fmt++; goto process_flags;
 
402
      case '#':
 
403
        /** @todo Implement "#" conversion flag. */  fmt++; goto process_flags;
 
404
      case ' ':
 
405
        /** @todo Implement " " conversion flag. */  fmt++; goto process_flags;
 
406
      case '+':
 
407
        /** @todo Implement "+" conversion flag. */  fmt++; goto process_flags;
 
408
    }
 
409
 
 
410
    if (*fmt == '*')
 
411
    {
 
412
      precision= (int) va_arg(args, int);
 
413
      fmt++;
 
414
    }
 
415
    else
 
416
    {
 
417
      while (my_isdigit(&my_charset_latin1, *fmt)) {
 
418
        minimum_width=(minimum_width * 10) + (*fmt - '0');
 
419
        fmt++;
 
420
      }
 
421
    }
 
422
    minimum_width*= minimum_width_sign;
 
423
 
 
424
    if (*fmt == '.')
 
425
    {
 
426
      fmt++;
 
427
      if (*fmt == '*') {
 
428
        precision= (int) va_arg(args, int);
 
429
        fmt++;
 
430
      }
 
431
      else
 
432
      {
 
433
        while (my_isdigit(&my_charset_latin1, *fmt)) {
 
434
          precision=(precision * 10) + (*fmt - '0');
 
435
          fmt++;
 
436
        }
 
437
      }
 
438
    }
 
439
 
 
440
    if (*fmt == 's')                            /* String parameter */
 
441
    {
 
442
      reg2 char *par = va_arg(args, char *);
 
443
      size_t length2 = strlen(par);
 
444
      /* TODO: implement precision */
 
445
      if (backtick_quoting)
 
446
      {
 
447
        size_t total= my_b_write_backtick_quote(info, par, length2);
 
448
        if (total == (size_t)-1)
 
449
          goto err;
 
450
        out_length+= total;
 
451
      }
 
452
      else
 
453
      {
 
454
        out_length+= length2;
 
455
        if (my_b_write(info, (uchar*) par, length2))
 
456
          goto err;
 
457
      }
 
458
    }
 
459
    else if (*fmt == 'b')                       /* Sized buffer parameter, only precision makes sense */
 
460
    {
 
461
      char *par = va_arg(args, char *);
 
462
      out_length+= precision;
 
463
      if (my_b_write(info, (uchar*) par, precision))
 
464
        goto err;
 
465
    }
 
466
    else if (*fmt == 'd' || *fmt == 'u')        /* Integer parameter */
 
467
    {
 
468
      register int iarg;
 
469
      size_t length2;
 
470
      char buff[17];
 
471
 
 
472
      iarg = va_arg(args, int);
 
473
      if (*fmt == 'd')
 
474
        length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
 
475
      else
 
476
        length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
 
477
 
 
478
      /* minimum width padding */
 
479
      if (minimum_width > length2) 
 
480
      {
 
481
        uchar *buffz;
 
482
                    
 
483
        buffz= (uchar*) my_alloca(minimum_width - length2);
 
484
        if (is_zero_padded)
 
485
          memset(buffz, '0', minimum_width - length2);
 
486
        else
 
487
          memset(buffz, ' ', minimum_width - length2);
 
488
        if (my_b_write(info, buffz, minimum_width - length2))
 
489
        {
 
490
          my_afree(buffz);
 
491
          goto err;
 
492
        }
 
493
        my_afree(buffz);
 
494
      }
 
495
 
 
496
      out_length+= length2;
 
497
      if (my_b_write(info, (uchar*) buff, length2))
 
498
        goto err;
 
499
    }
 
500
    else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
 
501
      /* long parameter */
 
502
    {
 
503
      register long iarg;
 
504
      size_t length2;
 
505
      char buff[17];
 
506
 
 
507
      iarg = va_arg(args, long);
 
508
      if (*++fmt == 'd')
 
509
        length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
 
510
      else
 
511
        length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
 
512
      out_length+= length2;
 
513
      if (my_b_write(info, (uchar*) buff, length2))
 
514
        goto err;
 
515
    }
 
516
    else
 
517
    {
 
518
      /* %% or unknown code */
 
519
      if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
 
520
        goto err;
 
521
      out_length+= fmt-backtrack;
 
522
    }
 
523
  }
 
524
  return out_length;
 
525
 
 
526
err:
 
527
  return (size_t) -1;
 
528
}
 
529