~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  More functions to be used with IO_CACHE files
18
*/
19
20
#define MAP_TO_USE_RAID
21
#include "mysys_priv.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
22
#include <mystrings/m_string.h>
1 by brian
clean slate
23
#include <stdarg.h>
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
24
#include <mystrings/m_ctype.h>
1 by brian
clean slate
25
26
/*
27
  Copy contents of an IO_CACHE to a file.
28
29
  SYNOPSIS
30
    my_b_copy_to_file()
31
    cache  IO_CACHE to copy from
32
    file   File to copy to
33
34
  DESCRIPTION
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
37
    cache.
38
39
    If a failure to write fully occurs, the cache is only copied
40
    partially.
41
42
  TODO
43
    Make this function solid by handling partial reads from the cache
44
    in a correct manner: it should be atomic.
45
46
  RETURN VALUE
47
    0  All OK
48
    1  An error occured
49
*/
50
int
51
my_b_copy_to_file(IO_CACHE *cache, FILE *file)
52
{
53
  size_t bytes_in_cache;
54
55
  /* Reinit the cache to read from the beginning of the cache */
163 by Brian Aker
Merge Monty's code.
56
  if (reinit_io_cache(cache, READ_CACHE, 0L, false, false))
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
57
    return(1);
1 by brian
clean slate
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)
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
63
      return(1);
1 by brian
clean slate
64
    cache->read_pos= cache->read_end;
65
  } while ((bytes_in_cache= my_b_fill(cache)));
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
66
  return(0);
1 by brian
clean slate
67
}
68
69
70
my_off_t my_b_append_tell(IO_CACHE* info)
71
{
72
  /*
73
    Prevent optimizer from putting res in a register when debugging
74
    we need this to be able to see the value of res when the assert fails
75
  */
51.3.28 by Jay Pipes
DBUG entirely removed from server and client
76
  volatile my_off_t res; 
1 by brian
clean slate
77
78
  /*
79
    We need to lock the append buffer mutex to keep flush_io_cache()
80
    from messing with the variables that we need in order to provide the
81
    answer to the question.
82
  */
83
  pthread_mutex_lock(&info->append_buffer_lock);
84
  res = info->end_of_file + (info->write_pos-info->append_read_pos);
85
  pthread_mutex_unlock(&info->append_buffer_lock);
86
  return res;
87
}
88
89
my_off_t my_b_safe_tell(IO_CACHE *info)
90
{
91
  if (unlikely(info->type == SEQ_READ_APPEND))
92
    return my_b_append_tell(info);
93
  return my_b_tell(info);
94
}
95
96
/*
97
  Make next read happen at the given position
98
  For write cache, make next write happen at the given position
99
*/
100
101
void my_b_seek(IO_CACHE *info,my_off_t pos)
102
{
103
  my_off_t offset;
104
105
  /*
106
    TODO:
107
       Verify that it is OK to do seek in the non-append
108
       area in SEQ_READ_APPEND cache
109
     a) see if this always works
110
     b) see if there is a better way to make it work
111
  */
112
  if (info->type == SEQ_READ_APPEND)
113
    VOID(flush_io_cache(info));
114
115
  offset=(pos - info->pos_in_file);
116
117
  if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
118
  {
119
    /* TODO: explain why this works if pos < info->pos_in_file */
151 by Brian Aker
Ulonglong to uint64_t
120
    if ((uint64_t) offset < (uint64_t) (info->read_end - info->buffer))
1 by brian
clean slate
121
    {
122
      /* The read is in the current buffer; Reuse it */
123
      info->read_pos = info->buffer + offset;
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
124
      return;
1 by brian
clean slate
125
    }
126
    else
127
    {
128
      /* Force a new read on next my_b_read */
129
      info->read_pos=info->read_end=info->buffer;
130
    }
131
  }
132
  else if (info->type == WRITE_CACHE)
133
  {
134
    /* If write is in current buffer, reuse it */
151 by Brian Aker
Ulonglong to uint64_t
135
    if ((uint64_t) offset <
136
	(uint64_t) (info->write_end - info->write_buffer))
1 by brian
clean slate
137
    {
138
      info->write_pos = info->write_buffer + offset;
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
139
      return;
1 by brian
clean slate
140
    }
141
    VOID(flush_io_cache(info));
142
    /* Correct buffer end so that we write in increments of IO_SIZE */
143
    info->write_end=(info->write_buffer+info->buffer_length-
144
		     (pos & (IO_SIZE-1)));
145
  }
146
  info->pos_in_file=pos;
147
  info->seek_not_done=1;
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
148
  return;
1 by brian
clean slate
149
}
150
151
152
/*
153
  Fill buffer of the cache.
154
155
  NOTES
156
    This assumes that you have already used all characters in the CACHE,
157
    independent of the read_pos value!
158
159
  RETURN
160
  0  On error or EOF (info->error = -1 on error)
161
  #  Number of characters
162
*/
163
164
165
size_t my_b_fill(IO_CACHE *info)
166
{
167
  my_off_t pos_in_file=(info->pos_in_file+
168
			(size_t) (info->read_end - info->buffer));
169
  size_t diff_length, length, max_length;
170
171
  if (info->seek_not_done)
172
  {					/* File touched, do seek */
173
    if (my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0)) ==
174
	MY_FILEPOS_ERROR)
175
    {
176
      info->error= 0;
177
      return 0;
178
    }
179
    info->seek_not_done=0;
180
  }
181
  diff_length=(size_t) (pos_in_file & (IO_SIZE-1));
182
  max_length=(info->read_length-diff_length);
183
  if (max_length >= (info->end_of_file - pos_in_file))
184
    max_length= (size_t) (info->end_of_file - pos_in_file);
185
186
  if (!max_length)
187
  {
188
    info->error= 0;
189
    return 0;					/* EOF */
190
  }
191
  if ((length= my_read(info->file,info->buffer,max_length,
192
                       info->myflags)) == (size_t) -1)
193
  {
194
    info->error= -1;
195
    return 0;
196
  }
197
  info->read_pos=info->buffer;
198
  info->read_end=info->buffer+length;
199
  info->pos_in_file=pos_in_file;
200
  return length;
201
}
202
203
204
/*
205
  Read a string ended by '\n' into a buffer of 'max_length' size.
206
  Returns number of characters read, 0 on error.
207
  last byte is set to '\0'
208
  If buffer is full then to[max_length-1] will be set to \0.
209
*/
210
211
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
212
{
213
  char *start = to;
214
  size_t length;
215
  max_length--;					/* Save place for end \0 */
216
217
  /* Calculate number of characters in buffer */
218
  if (!(length= my_b_bytes_in_cache(info)) &&
219
      !(length= my_b_fill(info)))
220
    return 0;
221
222
  for (;;)
223
  {
224
    uchar *pos, *end;
225
    if (length > max_length)
226
      length=max_length;
227
    for (pos=info->read_pos,end=pos+length ; pos < end ;)
228
    {
229
      if ((*to++ = *pos++) == '\n')
230
      {
231
	info->read_pos=pos;
232
	*to='\0';
233
	return (size_t) (to-start);
234
      }
235
    }
236
    if (!(max_length-=length))
237
    {
238
     /* Found enough charcters;  Return found string */
239
      info->read_pos=pos;
240
      *to='\0';
241
      return (size_t) (to-start);
242
    }
243
    if (!(length=my_b_fill(info)))
244
      return 0;
245
  }
246
}
247
248
249
my_off_t my_b_filelength(IO_CACHE *info)
250
{
251
  if (info->type == WRITE_CACHE)
252
    return my_b_tell(info);
253
254
  info->seek_not_done= 1;
255
  return my_seek(info->file, 0L, MY_SEEK_END, MYF(0));
256
}
257
258
259
/*
260
  Simple printf version.  Supports '%s', '%d', '%u', "%ld" and "%lu"
261
  Used for logging in MySQL
262
  returns number of written character, or (size_t) -1 on error
263
*/
264
265
size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
266
{
267
  size_t result;
268
  va_list args;
269
  va_start(args,fmt);
270
  result=my_b_vprintf(info, fmt, args);
271
  va_end(args);
272
  return result;
273
}
274
275
276
size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
277
{
278
  size_t out_length= 0;
279
  uint minimum_width; /* as yet unimplemented */
280
  uint minimum_width_sign;
281
  uint precision; /* as yet unimplemented for anything but %b */
146 by Brian Aker
my_bool cleanup.
282
  bool is_zero_padded;
1 by brian
clean slate
283
284
  /*
285
    Store the location of the beginning of a format directive, for the
286
    case where we learn we shouldn't have been parsing a format string
287
    at all, and we don't want to lose the flag/precision/width/size
288
    information.
289
   */
290
  const char* backtrack;
291
292
  for (; *fmt != '\0'; fmt++)
293
  {
294
    /* Copy everything until '%' or end of string */
295
    const char *start=fmt;
296
    size_t length;
297
    
298
    for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
299
300
    length= (size_t) (fmt - start);
301
    out_length+=length;
302
    if (my_b_write(info, (const uchar*) start, length))
303
      goto err;
304
305
    if (*fmt == '\0')				/* End of format */
306
      return out_length;
307
308
    /* 
309
      By this point, *fmt must be a percent;  Keep track of this location and
310
      skip over the percent character. 
311
    */
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
312
    assert(*fmt == '%');
1 by brian
clean slate
313
    backtrack= fmt;
314
    fmt++;
315
163 by Brian Aker
Merge Monty's code.
316
    is_zero_padded= false;
1 by brian
clean slate
317
    minimum_width_sign= 1;
318
    minimum_width= 0;
319
    precision= 0;
320
    /* Skip if max size is used (to be compatible with printf) */
321
322
process_flags:
323
    switch (*fmt)
324
    {
325
      case '-': 
326
        minimum_width_sign= -1; fmt++; goto process_flags;
327
      case '0':
163 by Brian Aker
Merge Monty's code.
328
        is_zero_padded= true; fmt++; goto process_flags;
1 by brian
clean slate
329
      case '#':
330
        /** @todo Implement "#" conversion flag. */  fmt++; goto process_flags;
331
      case ' ':
332
        /** @todo Implement " " conversion flag. */  fmt++; goto process_flags;
333
      case '+':
334
        /** @todo Implement "+" conversion flag. */  fmt++; goto process_flags;
335
    }
336
337
    if (*fmt == '*')
338
    {
339
      precision= (int) va_arg(args, int);
340
      fmt++;
341
    }
342
    else
343
    {
344
      while (my_isdigit(&my_charset_latin1, *fmt)) {
345
        minimum_width=(minimum_width * 10) + (*fmt - '0');
346
        fmt++;
347
      }
348
    }
349
    minimum_width*= minimum_width_sign;
350
351
    if (*fmt == '.')
352
    {
353
      fmt++;
354
      if (*fmt == '*') {
355
        precision= (int) va_arg(args, int);
356
        fmt++;
357
      }
358
      else
359
      {
360
        while (my_isdigit(&my_charset_latin1, *fmt)) {
361
          precision=(precision * 10) + (*fmt - '0');
362
          fmt++;
363
        }
364
      }
365
    }
366
367
    if (*fmt == 's')				/* String parameter */
368
    {
369
      register char *par = va_arg(args, char *);
370
      size_t length2 = strlen(par);
371
      /* TODO: implement precision */
372
      out_length+= length2;
373
      if (my_b_write(info, (uchar*) par, length2))
374
	goto err;
375
    }
376
    else if (*fmt == 'b')                       /* Sized buffer parameter, only precision makes sense */
377
    {
378
      char *par = va_arg(args, char *);
379
      out_length+= precision;
380
      if (my_b_write(info, (uchar*) par, precision))
381
        goto err;
382
    }
383
    else if (*fmt == 'd' || *fmt == 'u')	/* Integer parameter */
384
    {
385
      register int iarg;
386
      size_t length2;
387
      char buff[17];
388
389
      iarg = va_arg(args, int);
390
      if (*fmt == 'd')
391
	length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
392
      else
393
        length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
394
395
      /* minimum width padding */
396
      if (minimum_width > length2) 
397
      {
53.2.15 by Monty Taylor
Fixed simple signdedness problem.
398
        uchar *buffz;
1 by brian
clean slate
399
                    
400
        buffz= my_alloca(minimum_width - length2);
401
        if (is_zero_padded)
402
          memset(buffz, '0', minimum_width - length2);
403
        else
404
          memset(buffz, ' ', minimum_width - length2);
405
        my_b_write(info, buffz, minimum_width - length2);
406
        my_afree(buffz);
407
      }
408
409
      out_length+= length2;
410
      if (my_b_write(info, (uchar*) buff, length2))
411
	goto err;
412
    }
413
    else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
414
      /* long parameter */
415
    {
416
      register long iarg;
417
      size_t length2;
418
      char buff[17];
419
420
      iarg = va_arg(args, long);
421
      if (*++fmt == 'd')
422
	length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
423
      else
424
	length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
425
      out_length+= length2;
426
      if (my_b_write(info, (uchar*) buff, length2))
427
	goto err;
428
    }
429
    else
430
    {
431
      /* %% or unknown code */
432
      if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
433
        goto err;
434
      out_length+= fmt-backtrack;
435
    }
436
  }
437
  return out_length;
438
439
err:
440
  return (size_t) -1;
441
}