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