1 /* Copyright (c) 2000, 2018, Oracle and/or its affiliates.
2 Copyright (c) 2009, 2018, MariaDB Corporation
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
16
17 /*
18 More functions to be used with IO_CACHE files
19 */
20
21 #include "mysys_priv.h"
22 #include <m_string.h>
23 #include <stdarg.h>
24 #include <m_ctype.h>
25
26 /**
27 Copy the cache to the file. Copying can be constrained to @c count
28 number of bytes when the parameter is less than SIZE_T_MAX. The
29 cache will be optionally re-inited to a read cache and will read
30 from the beginning of the cache. If a failure to write fully
31 occurs, the cache is only copied partially.
32
33 TODO
34 Make this function solid by handling partial reads from the cache
35 in a correct manner: it should be atomic.
36
37 @param cache IO_CACHE to copy from
38 @param file File to copy to
39 @param count the copied size or the max of the type
40 when the whole cache is to be copied.
41 @return
42 0 All OK
43 1 An error occurred
44 */
45 int
my_b_copy_to_file(IO_CACHE * cache,FILE * file,size_t count)46 my_b_copy_to_file(IO_CACHE *cache, FILE *file,
47 size_t count)
48 {
49 size_t curr_write, bytes_in_cache;
50 DBUG_ENTER("my_b_copy_to_file");
51
52 bytes_in_cache= my_b_bytes_in_cache(cache);
53 do
54 {
55 curr_write= MY_MIN(bytes_in_cache, count);
56 if (my_fwrite(file, cache->read_pos, curr_write,
57 MYF(MY_WME | MY_NABP)) == (size_t) -1)
58 DBUG_RETURN(1);
59
60 cache->read_pos += curr_write;
61 count -= curr_write;
62 } while (count && (bytes_in_cache= my_b_fill(cache)));
63 if(cache->error == -1)
64 DBUG_RETURN(1);
65 DBUG_RETURN(0);
66 }
67
my_b_copy_all_to_file(IO_CACHE * cache,FILE * file)68 int my_b_copy_all_to_file(IO_CACHE *cache, FILE *file)
69 {
70 DBUG_ENTER("my_b_copy_all_to_file");
71 /* Reinit the cache to read from the beginning of the cache */
72 if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
73 DBUG_RETURN(1);
74 DBUG_RETURN(my_b_copy_to_file(cache, file, SIZE_T_MAX));
75 }
76
my_b_append_tell(IO_CACHE * info)77 my_off_t my_b_append_tell(IO_CACHE* info)
78 {
79 /*
80 Sometimes we want to make sure that the variable is not put into
81 a register in debugging mode so we can see its value in the core
82 */
83 #ifndef DBUG_OFF
84 # define dbug_volatile volatile
85 #else
86 # define dbug_volatile
87 #endif
88
89 /*
90 Prevent optimizer from putting res in a register when debugging
91 we need this to be able to see the value of res when the assert fails
92 */
93 dbug_volatile my_off_t res;
94
95 /*
96 We need to lock the append buffer mutex to keep flush_io_cache()
97 from messing with the variables that we need in order to provide the
98 answer to the question.
99 */
100 mysql_mutex_lock(&info->append_buffer_lock);
101
102 #ifndef DBUG_OFF
103 /*
104 Make sure EOF is where we think it is. Note that we cannot just use
105 my_tell() because we have a reader thread that could have left the
106 file offset in a non-EOF location
107 */
108 {
109 volatile my_off_t save_pos;
110 save_pos= mysql_file_tell(info->file, MYF(0));
111 mysql_file_seek(info->file, 0, MY_SEEK_END, MYF(0));
112 /*
113 Save the value of my_tell in res so we can see it when studying coredump
114 */
115 DBUG_ASSERT(info->end_of_file - (info->append_read_pos-info->write_buffer)
116 == (res= mysql_file_tell(info->file, MYF(0))));
117 mysql_file_seek(info->file, save_pos, MY_SEEK_SET, MYF(0));
118 }
119 #endif
120 res = info->end_of_file + (info->write_pos-info->append_read_pos);
121 mysql_mutex_unlock(&info->append_buffer_lock);
122 return res;
123 }
124
my_b_safe_tell(IO_CACHE * info)125 my_off_t my_b_safe_tell(IO_CACHE *info)
126 {
127 if (unlikely(info->type == SEQ_READ_APPEND))
128 return my_b_append_tell(info);
129 return my_b_tell(info);
130 }
131
132 /*
133 Make next read happen at the given position
134 For write cache, make next write happen at the given position
135 */
136
my_b_seek(IO_CACHE * info,my_off_t pos)137 void my_b_seek(IO_CACHE *info,my_off_t pos)
138 {
139 my_off_t offset;
140 DBUG_ENTER("my_b_seek");
141 DBUG_PRINT("enter",("pos: %lu", (ulong) pos));
142
143 /*
144 TODO:
145 Verify that it is OK to do seek in the non-append
146 area in SEQ_READ_APPEND cache
147 a) see if this always works
148 b) see if there is a better way to make it work
149 */
150 if (info->type == SEQ_READ_APPEND)
151 (void) flush_io_cache(info);
152
153 offset=(pos - info->pos_in_file);
154
155 if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
156 {
157 /* TODO: explain why this works if pos < info->pos_in_file */
158 if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer))
159 {
160 /* The read is in the current buffer; Reuse it */
161 info->read_pos = info->buffer + offset;
162 DBUG_VOID_RETURN;
163 }
164 else
165 {
166 /* Force a new read on next my_b_read */
167 info->read_pos=info->read_end=info->buffer;
168 }
169 }
170 else if (info->type == WRITE_CACHE)
171 {
172 /* If write is in current buffer, reuse it */
173 if ((ulonglong) offset <
174 (ulonglong) (info->write_end - info->write_buffer))
175 {
176 info->write_pos = info->write_buffer + offset;
177 DBUG_VOID_RETURN;
178 }
179 (void) flush_io_cache(info);
180 /* Correct buffer end so that we write in increments of IO_SIZE */
181 info->write_end=(info->write_buffer+info->buffer_length-
182 (pos & (IO_SIZE-1)));
183 }
184 info->pos_in_file=pos;
185 info->seek_not_done=1;
186 DBUG_VOID_RETURN;
187 }
188
my_b_pread(IO_CACHE * info,uchar * Buffer,size_t Count,my_off_t pos)189 int my_b_pread(IO_CACHE *info, uchar *Buffer, size_t Count, my_off_t pos)
190 {
191 if (info->myflags & MY_ENCRYPT)
192 {
193 my_b_seek(info, pos);
194 return my_b_read(info, Buffer, Count);
195 }
196
197 /* backward compatibility behavior. XXX remove it? */
198 if (mysql_file_pread(info->file, Buffer, Count, pos, info->myflags | MY_NABP))
199 return info->error= -1;
200 return 0;
201 }
202
203 /*
204 Read a string ended by '\n' into a buffer of 'max_length' size.
205 Returns number of characters read, 0 on error.
206 last byte is set to '\0'
207 If buffer is full then to[max_length-1] will be set to \0.
208 */
209
my_b_gets(IO_CACHE * info,char * to,size_t max_length)210 size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
211 {
212 char *start = to;
213 size_t length;
214 max_length--; /* Save place for end \0 */
215
216 /* Calculate number of characters in buffer */
217 if (!(length= my_b_bytes_in_cache(info)) &&
218 !(length= my_b_fill(info)))
219 return 0;
220
221 for (;;)
222 {
223 uchar *pos, *end;
224 if (length > max_length)
225 length=max_length;
226 for (pos=info->read_pos,end=pos+length ; pos < end ;)
227 {
228 if ((*to++ = *pos++) == '\n')
229 {
230 info->read_pos=pos;
231 *to='\0';
232 return (size_t) (to-start);
233 }
234 }
235 if (!(max_length-=length))
236 {
237 /* Found enough charcters; Return found string */
238 info->read_pos=pos;
239 *to='\0';
240 return (size_t) (to-start);
241 }
242 if (!(length=my_b_fill(info)))
243 return 0;
244 }
245 }
246
247
my_b_filelength(IO_CACHE * info)248 my_off_t my_b_filelength(IO_CACHE *info)
249 {
250 if (info->type == WRITE_CACHE)
251 return my_b_tell(info);
252
253 info->seek_not_done= 1;
254 return mysql_file_seek(info->file, 0, MY_SEEK_END, MYF(0));
255 }
256
257
258 my_bool
my_b_write_backtick_quote(IO_CACHE * info,const char * str,size_t len)259 my_b_write_backtick_quote(IO_CACHE *info, const char *str, size_t len)
260 {
261 const uchar *start;
262 const uchar *p= (const uchar *)str;
263 const uchar *end= p + len;
264 size_t count;
265
266 if (my_b_write(info, (uchar *)"`", 1))
267 return 1;
268 for (;;)
269 {
270 start= p;
271 while (p < end && *p != '`')
272 ++p;
273 count= p - start;
274 if (count && my_b_write(info, start, count))
275 return 1;
276 if (p >= end)
277 break;
278 if (my_b_write(info, (uchar *)"``", 2))
279 return 1;
280 ++p;
281 }
282 return (my_b_write(info, (uchar *)"`", 1));
283 }
284
285 /*
286 Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu"
287 Used for logging in MariaDB
288
289 @return 0 ok
290 1 error
291 */
292
my_b_printf(IO_CACHE * info,const char * fmt,...)293 my_bool my_b_printf(IO_CACHE *info, const char* fmt, ...)
294 {
295 size_t result;
296 va_list args;
297 va_start(args,fmt);
298 result=my_b_vprintf(info, fmt, args);
299 va_end(args);
300 return result == (size_t) -1;
301 }
302
303
my_b_vprintf(IO_CACHE * info,const char * fmt,va_list args)304 size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
305 {
306 size_t out_length= 0;
307 uint minimum_width; /* as yet unimplemented */
308 uint minimum_width_sign;
309 uint precision; /* as yet unimplemented for anything but %b */
310 my_bool is_zero_padded;
311 my_bool backtick_quoting;
312
313 /*
314 Store the location of the beginning of a format directive, for the
315 case where we learn we shouldn't have been parsing a format string
316 at all, and we don't want to lose the flag/precision/width/size
317 information.
318 */
319 const char* backtrack;
320
321 for (; *fmt != '\0'; fmt++)
322 {
323 /* Copy everything until '%' or end of string */
324 const char *start=fmt;
325 size_t length;
326
327 for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
328
329 length= (size_t) (fmt - start);
330 out_length+=length;
331 if (my_b_write(info, (const uchar*) start, length))
332 goto err;
333
334 if (*fmt == '\0') /* End of format */
335 return out_length;
336
337 /*
338 By this point, *fmt must be a percent; Keep track of this location and
339 skip over the percent character.
340 */
341 DBUG_ASSERT(*fmt == '%');
342 backtrack= fmt;
343 fmt++;
344
345 is_zero_padded= FALSE;
346 backtick_quoting= FALSE;
347 minimum_width_sign= 1;
348 minimum_width= 0;
349 precision= 0;
350 /* Skip if max size is used (to be compatible with printf) */
351
352 process_flags:
353 switch (*fmt)
354 {
355 case '-':
356 minimum_width_sign= -1; fmt++; goto process_flags;
357 case '0':
358 is_zero_padded= TRUE; fmt++; goto process_flags;
359 case '`':
360 backtick_quoting= TRUE; fmt++; goto process_flags;
361 case '#':
362 /** @todo Implement "#" conversion flag. */ fmt++; goto process_flags;
363 case ' ':
364 /** @todo Implement " " conversion flag. */ fmt++; goto process_flags;
365 case '+':
366 /** @todo Implement "+" conversion flag. */ fmt++; goto process_flags;
367 }
368
369 if (*fmt == '*')
370 {
371 precision= (int) va_arg(args, int);
372 fmt++;
373 }
374 else
375 {
376 while (my_isdigit(&my_charset_latin1, *fmt)) {
377 minimum_width=(minimum_width * 10) + (*fmt - '0');
378 fmt++;
379 }
380 }
381 minimum_width*= minimum_width_sign;
382
383 if (*fmt == '.')
384 {
385 fmt++;
386 if (*fmt == '*') {
387 precision= (int) va_arg(args, int);
388 fmt++;
389 }
390 else
391 {
392 while (my_isdigit(&my_charset_latin1, *fmt)) {
393 precision=(precision * 10) + (*fmt - '0');
394 fmt++;
395 }
396 }
397 }
398
399 if (*fmt == 's') /* String parameter */
400 {
401 reg2 char *par = va_arg(args, char *);
402 size_t length2 = strlen(par);
403 /* TODO: implement precision */
404 if (backtick_quoting)
405 {
406 size_t total= my_b_write_backtick_quote(info, par, length2);
407 if (total == (size_t)-1)
408 goto err;
409 out_length+= total;
410 }
411 else
412 {
413 out_length+= length2;
414 if (my_b_write(info, (uchar*) par, length2))
415 goto err;
416 }
417 }
418 else if (*fmt == 'c') /* char type parameter */
419 {
420 char par[2];
421 par[0] = va_arg(args, int);
422 if (my_b_write(info, (uchar*) par, 1))
423 goto err;
424 }
425 else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
426 {
427 char *par = va_arg(args, char *);
428 out_length+= precision;
429 if (my_b_write(info, (uchar*) par, precision))
430 goto err;
431 }
432 else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
433 {
434 register int iarg;
435 size_t length2;
436 char buff[32];
437
438 iarg = va_arg(args, int);
439 if (*fmt == 'd')
440 length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
441 else
442 length2= (uint) (int10_to_str((long) (uint) iarg,buff,10)- buff);
443
444 /* minimum width padding */
445 if (minimum_width > length2)
446 {
447 uchar *buffz;
448
449 buffz= (uchar*) my_alloca(minimum_width - length2);
450 if (is_zero_padded)
451 memset(buffz, '0', minimum_width - length2);
452 else
453 memset(buffz, ' ', minimum_width - length2);
454 if (my_b_write(info, buffz, minimum_width - length2))
455 {
456 my_afree(buffz);
457 goto err;
458 }
459 my_afree(buffz);
460 }
461
462 out_length+= length2;
463 if (my_b_write(info, (uchar*) buff, length2))
464 goto err;
465 }
466 else if ((*fmt == 'l' && (fmt[1] == 'd' || fmt[1] == 'u')))
467 /* long parameter */
468 {
469 register long iarg;
470 size_t length2;
471 char buff[32];
472
473 iarg = va_arg(args, long);
474 if (*++fmt == 'd')
475 length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
476 else
477 length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
478 out_length+= length2;
479 if (my_b_write(info, (uchar*) buff, length2))
480 goto err;
481 }
482 else
483 {
484 /* %% or unknown code */
485 if (my_b_write(info, (uchar*) backtrack, (size_t) (fmt-backtrack)))
486 goto err;
487 out_length+= fmt-backtrack;
488 }
489 }
490 return out_length;
491
492 err:
493 return (size_t) -1;
494 }
495