1 /* Copyright (c) 2000, 2018, 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
my_b_copy_to_file(IO_CACHE * cache,FILE * file)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
my_b_append_tell(IO_CACHE * info)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 = mysql_file_tell(info->file,MYF(0));
106 mysql_file_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=mysql_file_tell(info->file,MYF(0))));
112 mysql_file_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
my_b_safe_tell(IO_CACHE * info)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
my_b_seek(IO_CACHE * info,my_off_t pos)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
my_b_fill(IO_CACHE * info)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 (mysql_file_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= mysql_file_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
my_b_gets(IO_CACHE * info,char * to,size_t max_length)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
my_b_filelength(IO_CACHE * info)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 mysql_file_seek(info->file, 0L, MY_SEEK_END, MYF(0));
291 }
292
293
294 /*
295 Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu"
296 Used for logging in MySQL
297 returns number of written character, or (size_t) -1 on error
298 */
299
my_b_printf(IO_CACHE * info,const char * fmt,...)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
my_b_vprintf(IO_CACHE * info,const char * fmt,va_list args)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 '#':
365 /** @todo Implement "#" conversion flag. */ fmt++; goto process_flags;
366 case ' ':
367 /** @todo Implement " " conversion flag. */ fmt++; goto process_flags;
368 case '+':
369 /** @todo Implement "+" conversion flag. */ 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 == 'c') /* char type parameter */
412 {
413 char par[2];
414 par[0] = va_arg(args, int);
415 if (my_b_write(info, (uchar*) par, 1))
416 goto err;
417 }
418 else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
419 {
420 char *par = va_arg(args, char *);
421 out_length+= precision;
422 if (my_b_write(info, (uchar*) par, precision))
423 goto err;
424 }
425 else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
426 {
427 register int iarg;
428 size_t length2;
429 char buff[32];
430
431 iarg = va_arg(args, int);
432 if (*fmt == 'd')
433 length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
434 else
435 length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
436
437 /* minimum width padding */
438 if (minimum_width > length2)
439 {
440 char *buffz;
441
442 buffz= my_alloca(minimum_width - length2);
443 if (is_zero_padded)
444 memset(buffz, '0', minimum_width - length2);
445 else
446 memset(buffz, ' ', minimum_width - length2);
447 if (my_b_write(info, buffz, minimum_width - length2))
448 {
449 my_afree(buffz);
450 goto err;
451 }
452 my_afree(buffz);
453 }
454
455 out_length+= length2;
456 if (my_b_write(info, (uchar*) buff, length2))
457 goto err;
458 }
459 else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
460 /* long parameter */
461 {
462 register long iarg;
463 size_t length2;
464 char buff[32];
465
466 iarg = va_arg(args, long);
467 if (*++fmt == 'd')
468 length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
469 else
470 length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
471 out_length+= length2;
472 if (my_b_write(info, (uchar*) buff, length2))
473 goto err;
474 }
475 else
476 {
477 /* %% or unknown code */
478 if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
479 goto err;
480 out_length+= fmt-backtrack;
481 }
482 }
483 return out_length;
484
485 err:
486 return (size_t) -1;
487 }
488