MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mf_iocache2.c
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 /*
295  Simple printf version. Supports '%s', '%d', '%u', "%ld", "%lu" and "%llu"
296  Used for logging in MySQL
297  returns number of written character, or (size_t) -1 on error
298 */
299 
300 size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
301 {
302  size_t result;
303  va_list args;
304  va_start(args,fmt);
305  result=my_b_vprintf(info, fmt, args);
306  va_end(args);
307  return result;
308 }
309 
310 
311 size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
312 {
313  size_t out_length= 0;
314  uint minimum_width; /* as yet unimplemented */
315  uint minimum_width_sign;
316  uint precision; /* as yet unimplemented for anything but %b */
317  my_bool is_zero_padded;
318 
319  /*
320  Store the location of the beginning of a format directive, for the
321  case where we learn we shouldn't have been parsing a format string
322  at all, and we don't want to lose the flag/precision/width/size
323  information.
324  */
325  const char* backtrack;
326 
327  for (; *fmt != '\0'; fmt++)
328  {
329  /* Copy everything until '%' or end of string */
330  const char *start=fmt;
331  size_t length;
332 
333  for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
334 
335  length= (size_t) (fmt - start);
336  out_length+=length;
337  if (my_b_write(info, (const uchar*) start, length))
338  goto err;
339 
340  if (*fmt == '\0') /* End of format */
341  return out_length;
342 
343  /*
344  By this point, *fmt must be a percent; Keep track of this location and
345  skip over the percent character.
346  */
347  DBUG_ASSERT(*fmt == '%');
348  backtrack= fmt;
349  fmt++;
350 
351  is_zero_padded= FALSE;
352  minimum_width_sign= 1;
353  minimum_width= 0;
354  precision= 0;
355  /* Skip if max size is used (to be compatible with printf) */
356 
357 process_flags:
358  switch (*fmt)
359  {
360  case '-':
361  minimum_width_sign= -1; fmt++; goto process_flags;
362  case '0':
363  is_zero_padded= TRUE; fmt++; goto process_flags;
364  case '#': fmt++; goto process_flags;
366  case ' ': fmt++; goto process_flags;
368  case '+': fmt++; goto process_flags;
370  }
371 
372  if (*fmt == '*')
373  {
374  precision= (int) va_arg(args, int);
375  fmt++;
376  }
377  else
378  {
379  while (my_isdigit(&my_charset_latin1, *fmt)) {
380  minimum_width=(minimum_width * 10) + (*fmt - '0');
381  fmt++;
382  }
383  }
384  minimum_width*= minimum_width_sign;
385 
386  if (*fmt == '.')
387  {
388  fmt++;
389  if (*fmt == '*') {
390  precision= (int) va_arg(args, int);
391  fmt++;
392  }
393  else
394  {
395  while (my_isdigit(&my_charset_latin1, *fmt)) {
396  precision=(precision * 10) + (*fmt - '0');
397  fmt++;
398  }
399  }
400  }
401 
402  if (*fmt == 's') /* String parameter */
403  {
404  reg2 char *par = va_arg(args, char *);
405  size_t length2 = strlen(par);
406  /* TODO: implement precision */
407  out_length+= length2;
408  if (my_b_write(info, (uchar*) par, length2))
409  goto err;
410  }
411  else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
412  {
413  char *par = va_arg(args, char *);
414  out_length+= precision;
415  if (my_b_write(info, (uchar*) par, precision))
416  goto err;
417  }
418  else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
419  {
420  register int iarg;
421  size_t length2;
422  char buff[32];
423 
424  iarg = va_arg(args, int);
425  if (*fmt == 'd')
426  length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
427  else
428  length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
429 
430  /* minimum width padding */
431  if (minimum_width > length2)
432  {
433  char *buffz;
434 
435  buffz= my_alloca(minimum_width - length2);
436  if (is_zero_padded)
437  memset(buffz, '0', minimum_width - length2);
438  else
439  memset(buffz, ' ', minimum_width - length2);
440  if (my_b_write(info, buffz, minimum_width - length2))
441  {
442  my_afree(buffz);
443  goto err;
444  }
445  my_afree(buffz);
446  }
447 
448  out_length+= length2;
449  if (my_b_write(info, (uchar*) buff, length2))
450  goto err;
451  }
452  else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
453  /* long parameter */
454  {
455  register long iarg;
456  size_t length2;
457  char buff[32];
458 
459  iarg = va_arg(args, long);
460  if (*++fmt == 'd')
461  length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
462  else
463  length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
464  out_length+= length2;
465  if (my_b_write(info, (uchar*) buff, length2))
466  goto err;
467  }
468  else if (fmt[0] == 'l' && fmt[1] == 'l' && fmt[2] == 'u')
469  {
470  ulonglong iarg;
471  size_t length2;
472  char buff[32];
473 
474  iarg = va_arg(args, ulonglong);
475  length2= (size_t) (longlong10_to_str(iarg, buff, 10) - buff);
476  out_length+= length2;
477  fmt+= 2;
478  if (my_b_write(info, (uchar *) buff, length2))
479  goto err;
480  }
481  else
482  {
483  /* %% or unknown code */
484  if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
485  goto err;
486  out_length+= fmt-backtrack;
487  }
488  }
489  return out_length;
490 
491 err:
492  return (size_t) -1;
493 }