1 /* Copyright (c) 2000, 2010, 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 /* write whats in isam.log */
17
18 #ifndef USE_MY_FUNC
19 #define USE_MY_FUNC
20 #endif
21
22 #include "myisamdef.h"
23 #include <my_tree.h>
24 #include <stdarg.h>
25 #ifdef HAVE_GETRUSAGE
26 #include <sys/resource.h>
27 #endif
28
29 #define FILENAME(A) (A ? A->show_name : "Unknown")
30
31 struct file_info {
32 long process;
33 int filenr,id;
34 uint rnd;
35 char *name, *show_name;
36 uchar *record;
37 MI_INFO *isam;
38 my_bool closed, used;
39 ulong accessed;
40 };
41
42 struct test_if_open_param {
43 char * name;
44 int max_id;
45 };
46
47 struct st_access_param
48 {
49 ulong min_accessed;
50 struct file_info *found;
51 };
52
53 #define NO_FILEPOS (ulong) ~0L
54
55 extern int main(int argc,char * *argv);
56 static void get_options(int *argc,char ***argv);
57 static int examine_log(char * file_name,char **table_names);
58 static int read_string(IO_CACHE *file,uchar* *to,uint length);
59 static int file_info_compare(void *cmp_arg, void *a,void *b);
60 static int test_if_open(struct file_info *key,element_count count,
61 struct test_if_open_param *param);
62 static void fix_blob_pointers(MI_INFO *isam,uchar *record);
63 static int test_when_accessed(struct file_info *key,element_count count,
64 struct st_access_param *access_param);
65 static void file_info_free(struct file_info *info);
66 static int close_some_file(TREE *tree);
67 static int reopen_closed_file(TREE *tree,struct file_info *file_info);
68 static int find_record_with_key(struct file_info *file_info,uchar *record);
69 static void printf_log(const char *str,...);
70 static my_bool cmp_filename(struct file_info *file_info,char * name);
71
72 static uint verbose=0,update=0,test_info=0,max_files=0,re_open_count=0,
73 recover=0,prefix_remove=0,opt_processes=0;
74 static char *log_filename=0, *filepath=0, *write_filename=0;
75 static char *record_pos_file= 0;
76 static ulong com_count[10][3],number_of_commands=(ulong) ~0L,
77 isamlog_process;
78 static my_off_t isamlog_filepos,start_offset=0,record_pos= HA_OFFSET_ERROR;
79 static const char *command_name[]=
80 {"open","write","update","delete","close","extra","lock","re-open",
81 "delete-all", NullS};
82
83
main(int argc,char ** argv)84 int main(int argc, char **argv)
85 {
86 int error,i,first;
87 ulong total_count,total_error,total_recover;
88 MY_INIT(argv[0]);
89
90 log_filename=myisam_log_filename;
91 get_options(&argc,&argv);
92 /* Number of MyISAM files we can have open at one time */
93 max_files= (my_set_max_open_files(min(max_files,8))-6)/2;
94 if (update)
95 printf("Trying to %s MyISAM files according to log '%s'\n",
96 (recover ? "recover" : "update"),log_filename);
97 error= examine_log(log_filename,argv);
98 if (update && ! error)
99 puts("Tables updated successfully");
100 total_count=total_error=total_recover=0;
101 for (i=first=0 ; command_name[i] ; i++)
102 {
103 if (com_count[i][0])
104 {
105 if (!first++)
106 {
107 if (verbose || update)
108 puts("");
109 puts("Commands Used count Errors Recover errors");
110 }
111 printf("%-12s%9ld%10ld%17ld\n",command_name[i],com_count[i][0],
112 com_count[i][1],com_count[i][2]);
113 total_count+=com_count[i][0];
114 total_error+=com_count[i][1];
115 total_recover+=com_count[i][2];
116 }
117 }
118 if (total_count)
119 printf("%-12s%9ld%10ld%17ld\n","Total",total_count,total_error,
120 total_recover);
121 if (re_open_count)
122 printf("Had to do %d re-open because of too few possibly open files\n",
123 re_open_count);
124 (void) mi_panic(HA_PANIC_CLOSE);
125 my_free_open_file_info();
126 my_end(test_info ? MY_CHECK_ERROR | MY_GIVE_INFO : MY_CHECK_ERROR);
127 exit(error);
128 return 0; /* No compiler warning */
129 } /* main */
130
131
get_options(register int * argc,register char *** argv)132 static void get_options(register int *argc, register char ***argv)
133 {
134 int help,version;
135 const char *pos,*usage;
136 char option;
137
138 help=0;
139 usage="Usage: %s [-?iruvDIV] [-c #] [-f #] [-F filepath/] [-o #] [-R file recordpos] [-w write_file] [log-filename [table ...]] \n";
140 pos="";
141
142 while (--*argc > 0 && *(pos = *(++*argv)) == '-' ) {
143 while (*++pos)
144 {
145 version=0;
146 switch((option=*pos)) {
147 case '#':
148 DBUG_PUSH (++pos);
149 pos=" "; /* Skip rest of arg */
150 break;
151 case 'c':
152 if (! *++pos)
153 {
154 if (!--*argc)
155 goto err;
156 else
157 pos= *(++*argv);
158 }
159 number_of_commands=(ulong) atol(pos);
160 pos=" ";
161 break;
162 case 'u':
163 update=1;
164 break;
165 case 'f':
166 if (! *++pos)
167 {
168 if (!--*argc)
169 goto err;
170 else
171 pos= *(++*argv);
172 }
173 max_files=(uint) atoi(pos);
174 pos=" ";
175 break;
176 case 'i':
177 test_info=1;
178 break;
179 case 'o':
180 if (! *++pos)
181 {
182 if (!--*argc)
183 goto err;
184 else
185 pos= *(++*argv);
186 }
187 start_offset=(my_off_t) strtoll(pos,NULL,10);
188 pos=" ";
189 break;
190 case 'p':
191 if (! *++pos)
192 {
193 if (!--*argc)
194 goto err;
195 else
196 pos= *(++*argv);
197 }
198 prefix_remove=atoi(pos);
199 break;
200 case 'r':
201 update=1;
202 recover++;
203 break;
204 case 'P':
205 opt_processes=1;
206 break;
207 case 'R':
208 if (! *++pos)
209 {
210 if (!--*argc)
211 goto err;
212 else
213 pos= *(++*argv);
214 }
215 record_pos_file=(char*) pos;
216 if (!--*argc)
217 goto err;
218 record_pos=(my_off_t) strtoll(*(++*argv),NULL,10);
219 pos=" ";
220 break;
221 case 'v':
222 verbose++;
223 break;
224 case 'w':
225 if (! *++pos)
226 {
227 if (!--*argc)
228 goto err;
229 else
230 pos= *(++*argv);
231 }
232 write_filename=(char*) pos;
233 pos=" ";
234 break;
235 case 'F':
236 if (! *++pos)
237 {
238 if (!--*argc)
239 goto err;
240 else
241 pos= *(++*argv);
242 }
243 filepath= (char*) pos;
244 pos=" ";
245 break;
246 case 'V':
247 version=1;
248 /* Fall through */
249 case 'I':
250 case '?':
251 printf("%s Ver 1.4 for %s at %s\n",my_progname,SYSTEM_TYPE,
252 MACHINE_TYPE);
253 puts("By Monty, for your professional use\n");
254 if (version)
255 break;
256 puts("Write info about whats in a MyISAM log file.");
257 printf("If no file name is given %s is used\n",log_filename);
258 puts("");
259 printf(usage,my_progname);
260 puts("");
261 puts("Options: -? or -I \"Info\" -V \"version\" -c \"do only # commands\"");
262 puts(" -f \"max open files\" -F \"filepath\" -i \"extra info\"");
263 puts(" -o \"offset\" -p # \"remove # components from path\"");
264 puts(" -r \"recover\" -R \"file recordposition\"");
265 puts(" -u \"update\" -v \"verbose\" -w \"write file\"");
266 puts(" -D \"myisam compiled with DBUG\" -P \"processes\"");
267 puts("\nOne can give a second and a third '-v' for more verbose.");
268 puts("Normaly one does a update (-u).");
269 puts("If a recover is done all writes and all possibly updates and deletes is done\nand errors are only counted.");
270 puts("If one gives table names as arguments only these tables will be updated\n");
271 help=1;
272 break;
273 default:
274 printf("illegal option: \"-%c\"\n",*pos);
275 break;
276 }
277 }
278 }
279 if (! *argc)
280 {
281 if (help)
282 exit(0);
283 (*argv)++;
284 }
285 if (*argc >= 1)
286 {
287 log_filename=(char*) pos;
288 (*argc)--;
289 (*argv)++;
290 }
291 return;
292 err:
293 (void) fprintf(stderr,"option \"%c\" used without or with wrong argument\n",
294 option);
295 exit(1);
296 }
297
298
examine_log(char * file_name,char ** table_names)299 static int examine_log(char * file_name, char **table_names)
300 {
301 uint command,result,files_open;
302 ulong access_time,length;
303 my_off_t filepos;
304 int lock_command,mi_result;
305 char isam_file_name[FN_REFLEN],llbuff[21],llbuff2[21];
306 uchar head[20];
307 uchar* buff;
308 struct test_if_open_param open_param;
309 IO_CACHE cache;
310 File file;
311 FILE *write_file;
312 enum ha_extra_function extra_command;
313 TREE tree;
314 struct file_info file_info,*curr_file_info;
315 DBUG_ENTER("examine_log");
316
317 if ((file=my_open(file_name,O_RDONLY,MYF(MY_WME))) < 0)
318 DBUG_RETURN(1);
319 write_file=0;
320 if (write_filename)
321 {
322 if (!(write_file=my_fopen(write_filename,O_WRONLY,MYF(MY_WME))))
323 {
324 my_close(file,MYF(0));
325 DBUG_RETURN(1);
326 }
327 }
328
329 init_io_cache(&cache,file,0,READ_CACHE,start_offset,0,MYF(0));
330 bzero((uchar*) com_count,sizeof(com_count));
331 init_tree(&tree,0,0,sizeof(file_info),(qsort_cmp2) file_info_compare,1,
332 (tree_element_free) file_info_free, NULL);
333 (void) init_key_cache(dflt_key_cache,KEY_CACHE_BLOCK_SIZE,KEY_CACHE_SIZE,
334 0, 0);
335
336 files_open=0; access_time=0;
337 while (access_time++ != number_of_commands &&
338 !my_b_read(&cache,(uchar*) head,9))
339 {
340 isamlog_filepos=my_b_tell(&cache)-9L;
341 file_info.filenr= mi_uint2korr(head+1);
342 isamlog_process=file_info.process=(long) mi_uint4korr(head+3);
343 if (!opt_processes)
344 file_info.process=0;
345 result= mi_uint2korr(head+7);
346 if ((curr_file_info=(struct file_info*) tree_search(&tree, &file_info,
347 tree.custom_arg)))
348 {
349 curr_file_info->accessed=access_time;
350 if (update && curr_file_info->used && curr_file_info->closed)
351 {
352 if (reopen_closed_file(&tree,curr_file_info))
353 {
354 command=sizeof(com_count)/sizeof(com_count[0][0])/3;
355 result=0;
356 goto com_err;
357 }
358 }
359 }
360 command=(uint) head[0];
361 if (command < sizeof(com_count)/sizeof(com_count[0][0])/3 &&
362 (!table_names[0] || (curr_file_info && curr_file_info->used)))
363 {
364 com_count[command][0]++;
365 if (result)
366 com_count[command][1]++;
367 }
368 switch ((enum myisam_log_commands) command) {
369 case MI_LOG_OPEN:
370 if (!table_names[0])
371 {
372 com_count[command][0]--; /* Must be counted explicite */
373 if (result)
374 com_count[command][1]--;
375 }
376
377 if (curr_file_info)
378 printf("\nWarning: %s is opened with same process and filenumber\n"
379 "Maybe you should use the -P option ?\n",
380 curr_file_info->show_name);
381 if (my_b_read(&cache,(uchar*) head,2))
382 goto err;
383 buff= 0;
384 file_info.name=0;
385 file_info.show_name=0;
386 file_info.record=0;
387 if (read_string(&cache, &buff, (uint) mi_uint2korr(head)))
388 goto err;
389 {
390 uint i;
391 char *pos,*to;
392
393 /* Fix if old DOS files to new format */
394 for (pos=file_info.name=(char*)buff; (pos=strchr(pos,'\\')) ; pos++)
395 *pos= '/';
396
397 pos=file_info.name;
398 for (i=0 ; i < prefix_remove ; i++)
399 {
400 char *next;
401 if (!(next=strchr(pos,'/')))
402 break;
403 pos=next+1;
404 }
405 to=isam_file_name;
406 if (filepath)
407 to=convert_dirname(isam_file_name,filepath,NullS);
408 strmov(to,pos);
409 fn_ext(isam_file_name)[0]=0; /* Remove extension */
410 }
411 open_param.name=file_info.name;
412 open_param.max_id=0;
413 (void) tree_walk(&tree,(tree_walk_action) test_if_open,(void*) &open_param,
414 left_root_right);
415 file_info.id=open_param.max_id+1;
416 /*
417 * In the line below +10 is added to accomodate '<' and '>' chars
418 * plus '\0' at the end, so that there is place for 7 digits.
419 * It is improbable that same table can have that many entries in
420 * the table cache.
421 * The additional space is needed for the sprintf commands two lines
422 * below.
423 */
424 file_info.show_name=my_memdup(isam_file_name,
425 (uint) strlen(isam_file_name)+10,
426 MYF(MY_WME));
427 if (file_info.id > 1)
428 sprintf(strend(file_info.show_name),"<%d>",file_info.id);
429 file_info.closed=1;
430 file_info.accessed=access_time;
431 file_info.used=1;
432 if (table_names[0])
433 {
434 char **name;
435 file_info.used=0;
436 for (name=table_names ; *name ; name++)
437 {
438 if (!strcmp(*name,isam_file_name))
439 file_info.used=1; /* Update/log only this */
440 }
441 }
442 if (update && file_info.used)
443 {
444 if (files_open >= max_files)
445 {
446 if (close_some_file(&tree))
447 goto com_err;
448 files_open--;
449 }
450 if (!(file_info.isam= mi_open(isam_file_name,O_RDWR,
451 HA_OPEN_WAIT_IF_LOCKED)))
452 goto com_err;
453 if (!(file_info.record=my_malloc(file_info.isam->s->base.reclength,
454 MYF(MY_WME))))
455 goto end;
456 files_open++;
457 file_info.closed=0;
458 }
459 (void) tree_insert(&tree, (uchar*) &file_info, 0, tree.custom_arg);
460 if (file_info.used)
461 {
462 if (verbose && !record_pos_file)
463 printf_log("%s: open -> %d",file_info.show_name, file_info.filenr);
464 com_count[command][0]++;
465 if (result)
466 com_count[command][1]++;
467 }
468 break;
469 case MI_LOG_CLOSE:
470 if (verbose && !record_pos_file &&
471 (!table_names[0] || (curr_file_info && curr_file_info->used)))
472 printf_log("%s: %s -> %d",FILENAME(curr_file_info),
473 command_name[command],result);
474 if (curr_file_info)
475 {
476 if (!curr_file_info->closed)
477 files_open--;
478 (void) tree_delete(&tree, (uchar*) curr_file_info, 0, tree.custom_arg);
479 }
480 break;
481 case MI_LOG_EXTRA:
482 if (my_b_read(&cache,(uchar*) head,1))
483 goto err;
484 extra_command=(enum ha_extra_function) head[0];
485 if (verbose && !record_pos_file &&
486 (!table_names[0] || (curr_file_info && curr_file_info->used)))
487 printf_log("%s: %s(%d) -> %d",FILENAME(curr_file_info),
488 command_name[command], (int) extra_command,result);
489 if (update && curr_file_info && !curr_file_info->closed)
490 {
491 if (mi_extra(curr_file_info->isam, extra_command, 0) != (int) result)
492 {
493 fflush(stdout);
494 (void) fprintf(stderr,
495 "Warning: error %d, expected %d on command %s at %s\n",
496 my_errno,result,command_name[command],
497 llstr(isamlog_filepos,llbuff));
498 fflush(stderr);
499 }
500 }
501 break;
502 case MI_LOG_DELETE:
503 if (my_b_read(&cache,(uchar*) head,8))
504 goto err;
505 filepos=mi_sizekorr(head);
506 if (verbose && (!record_pos_file ||
507 ((record_pos == filepos || record_pos == NO_FILEPOS) &&
508 !cmp_filename(curr_file_info,record_pos_file))) &&
509 (!table_names[0] || (curr_file_info && curr_file_info->used)))
510 printf_log("%s: %s at %ld -> %d",FILENAME(curr_file_info),
511 command_name[command],(long) filepos,result);
512 if (update && curr_file_info && !curr_file_info->closed)
513 {
514 if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos))
515 {
516 if (!recover)
517 goto com_err;
518 if (verbose)
519 printf_log("error: Didn't find row to delete with mi_rrnd");
520 com_count[command][2]++; /* Mark error */
521 }
522 mi_result=mi_delete(curr_file_info->isam,curr_file_info->record);
523 if ((mi_result == 0 && result) ||
524 (mi_result && (uint) my_errno != result))
525 {
526 if (!recover)
527 goto com_err;
528 if (mi_result)
529 com_count[command][2]++; /* Mark error */
530 if (verbose)
531 printf_log("error: Got result %d from mi_delete instead of %d",
532 mi_result, result);
533 }
534 }
535 break;
536 case MI_LOG_WRITE:
537 case MI_LOG_UPDATE:
538 if (my_b_read(&cache,(uchar*) head,12))
539 goto err;
540 filepos=mi_sizekorr(head);
541 length=mi_uint4korr(head+8);
542 buff=0;
543 if (read_string(&cache,&buff,(uint) length))
544 goto err;
545 if ((!record_pos_file ||
546 ((record_pos == filepos || record_pos == NO_FILEPOS) &&
547 !cmp_filename(curr_file_info,record_pos_file))) &&
548 (!table_names[0] || (curr_file_info && curr_file_info->used)))
549 {
550 if (write_file &&
551 (my_fwrite(write_file,buff,length,MYF(MY_WAIT_IF_FULL | MY_NABP))))
552 goto end;
553 if (verbose)
554 printf_log("%s: %s at %ld, length=%ld -> %d",
555 FILENAME(curr_file_info),
556 command_name[command], filepos,length,result);
557 }
558 if (update && curr_file_info && !curr_file_info->closed)
559 {
560 if (curr_file_info->isam->s->base.blobs)
561 fix_blob_pointers(curr_file_info->isam,buff);
562 if ((enum myisam_log_commands) command == MI_LOG_UPDATE)
563 {
564 if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos))
565 {
566 if (!recover)
567 {
568 result=0;
569 goto com_err;
570 }
571 if (verbose)
572 printf_log("error: Didn't find row to update with mi_rrnd");
573 if (recover == 1 || result ||
574 find_record_with_key(curr_file_info,buff))
575 {
576 com_count[command][2]++; /* Mark error */
577 break;
578 }
579 }
580 mi_result=mi_update(curr_file_info->isam,curr_file_info->record,
581 buff);
582 if ((mi_result == 0 && result) ||
583 (mi_result && (uint) my_errno != result))
584 {
585 if (!recover)
586 goto com_err;
587 if (verbose)
588 printf_log("error: Got result %d from mi_update instead of %d",
589 mi_result, result);
590 if (mi_result)
591 com_count[command][2]++; /* Mark error */
592 }
593 }
594 else
595 {
596 mi_result=mi_write(curr_file_info->isam,buff);
597 if ((mi_result == 0 && result) ||
598 (mi_result && (uint) my_errno != result))
599 {
600 if (!recover)
601 goto com_err;
602 if (verbose)
603 printf_log("error: Got result %d from mi_write instead of %d",
604 mi_result, result);
605 if (mi_result)
606 com_count[command][2]++; /* Mark error */
607 }
608 if (!recover && filepos != curr_file_info->isam->lastpos)
609 {
610 printf("error: Wrote at position: %s, should have been %s",
611 llstr(curr_file_info->isam->lastpos,llbuff),
612 llstr(filepos,llbuff2));
613 goto end;
614 }
615 }
616 }
617 my_free(buff);
618 break;
619 case MI_LOG_LOCK:
620 if (my_b_read(&cache,(uchar*) head,sizeof(lock_command)))
621 goto err;
622 memcpy(&lock_command, head, sizeof(lock_command));
623 if (verbose && !record_pos_file &&
624 (!table_names[0] || (curr_file_info && curr_file_info->used)))
625 printf_log("%s: %s(%d) -> %d\n",FILENAME(curr_file_info),
626 command_name[command],lock_command,result);
627 if (update && curr_file_info && !curr_file_info->closed)
628 {
629 if (mi_lock_database(curr_file_info->isam,lock_command) !=
630 (int) result)
631 goto com_err;
632 }
633 break;
634 case MI_LOG_DELETE_ALL:
635 if (verbose && !record_pos_file &&
636 (!table_names[0] || (curr_file_info && curr_file_info->used)))
637 printf_log("%s: %s -> %d\n",FILENAME(curr_file_info),
638 command_name[command],result);
639 break;
640 default:
641 fflush(stdout);
642 (void) fprintf(stderr,
643 "Error: found unknown command %d in logfile, aborted\n",
644 command);
645 fflush(stderr);
646 goto end;
647 }
648 }
649 end_key_cache(dflt_key_cache,1);
650 delete_tree(&tree);
651 (void) end_io_cache(&cache);
652 (void) my_close(file,MYF(0));
653 if (write_file && my_fclose(write_file,MYF(MY_WME)))
654 DBUG_RETURN(1);
655 DBUG_RETURN(0);
656
657 err:
658 fflush(stdout);
659 (void) fprintf(stderr,"Got error %d when reading from logfile\n",my_errno);
660 fflush(stderr);
661 goto end;
662 com_err:
663 fflush(stdout);
664 (void) fprintf(stderr,"Got error %d, expected %d on command %s at %s\n",
665 my_errno,result,command_name[command],
666 llstr(isamlog_filepos,llbuff));
667 fflush(stderr);
668 end:
669 end_key_cache(dflt_key_cache, 1);
670 delete_tree(&tree);
671 (void) end_io_cache(&cache);
672 (void) my_close(file,MYF(0));
673 if (write_file)
674 (void) my_fclose(write_file,MYF(MY_WME));
675 DBUG_RETURN(1);
676 }
677
678
read_string(IO_CACHE * file,register uchar ** to,register uint length)679 static int read_string(IO_CACHE *file, register uchar* *to, register uint length)
680 {
681 DBUG_ENTER("read_string");
682
683 if (*to)
684 my_free(*to);
685 if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) ||
686 my_b_read(file,(uchar*) *to,length))
687 {
688 if (*to)
689 my_free(*to);
690 *to= 0;
691 DBUG_RETURN(1);
692 }
693 *((uchar*) *to+length)= '\0';
694 DBUG_RETURN (0);
695 } /* read_string */
696
697
file_info_compare(void * cmp_arg,void * a,void * b)698 static int file_info_compare(void* cmp_arg __attribute__((unused)),
699 void *a, void *b)
700 {
701 long lint;
702
703 if ((lint=((struct file_info*) a)->process -
704 ((struct file_info*) b)->process))
705 return lint < 0L ? -1 : 1;
706 return ((struct file_info*) a)->filenr - ((struct file_info*) b)->filenr;
707 }
708
709 /* ARGSUSED */
710
test_if_open(struct file_info * key,element_count count,struct test_if_open_param * param)711 static int test_if_open (struct file_info *key,
712 element_count count __attribute__((unused)),
713 struct test_if_open_param *param)
714 {
715 if (!strcmp(key->name,param->name) && key->id > param->max_id)
716 param->max_id=key->id;
717 return 0;
718 }
719
720
fix_blob_pointers(MI_INFO * info,uchar * record)721 static void fix_blob_pointers(MI_INFO *info, uchar *record)
722 {
723 uchar *pos;
724 MI_BLOB *blob,*end;
725
726 pos=record+info->s->base.reclength;
727 for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
728 blob != end ;
729 blob++)
730 {
731 memcpy(record+blob->offset+blob->pack_length, &pos, sizeof(char*));
732 pos+=_mi_calc_blob_length(blob->pack_length,record+blob->offset);
733 }
734 }
735
736 /* close the file with hasn't been accessed for the longest time */
737 /* ARGSUSED */
738
test_when_accessed(struct file_info * key,element_count count,struct st_access_param * access_param)739 static int test_when_accessed (struct file_info *key,
740 element_count count __attribute__((unused)),
741 struct st_access_param *access_param)
742 {
743 if (key->accessed < access_param->min_accessed && ! key->closed)
744 {
745 access_param->min_accessed=key->accessed;
746 access_param->found=key;
747 }
748 return 0;
749 }
750
751
file_info_free(struct file_info * fileinfo)752 static void file_info_free(struct file_info *fileinfo)
753 {
754 DBUG_ENTER("file_info_free");
755 if (update)
756 {
757 if (!fileinfo->closed)
758 (void) mi_close(fileinfo->isam);
759 if (fileinfo->record)
760 my_free(fileinfo->record);
761 }
762 my_free(fileinfo->name);
763 my_free(fileinfo->show_name);
764 DBUG_VOID_RETURN;
765 }
766
767
768
close_some_file(TREE * tree)769 static int close_some_file(TREE *tree)
770 {
771 struct st_access_param access_param;
772
773 access_param.min_accessed=LONG_MAX;
774 access_param.found=0;
775
776 (void) tree_walk(tree,(tree_walk_action) test_when_accessed,
777 (void*) &access_param,left_root_right);
778 if (!access_param.found)
779 return 1; /* No open file that is possibly to close */
780 if (mi_close(access_param.found->isam))
781 return 1;
782 access_param.found->closed=1;
783 return 0;
784 }
785
786
reopen_closed_file(TREE * tree,struct file_info * fileinfo)787 static int reopen_closed_file(TREE *tree, struct file_info *fileinfo)
788 {
789 char name[FN_REFLEN];
790 if (close_some_file(tree))
791 return 1; /* No file to close */
792 strmov(name,fileinfo->show_name);
793 if (fileinfo->id > 1)
794 *strrchr(name,'<')='\0'; /* Remove "<id>" */
795
796 if (!(fileinfo->isam= mi_open(name,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)))
797 return 1;
798 fileinfo->closed=0;
799 re_open_count++;
800 return 0;
801 }
802
803 /* Try to find record with uniq key */
804
find_record_with_key(struct file_info * file_info,uchar * record)805 static int find_record_with_key(struct file_info *file_info, uchar *record)
806 {
807 uint key;
808 MI_INFO *info=file_info->isam;
809 uchar tmp_key[MI_MAX_KEY_BUFF];
810
811 for (key=0 ; key < info->s->base.keys ; key++)
812 {
813 if (mi_is_key_active(info->s->state.key_map, key) &&
814 info->s->keyinfo[key].flag & HA_NOSAME)
815 {
816 (void) _mi_make_key(info,key,tmp_key,record,0L);
817 return mi_rkey(info,file_info->record,(int) key,tmp_key,0,
818 HA_READ_KEY_EXACT);
819 }
820 }
821 return 1;
822 }
823
824
printf_log(const char * format,...)825 static void printf_log(const char *format,...)
826 {
827 char llbuff[21];
828 va_list args;
829 va_start(args,format);
830 if (verbose > 2)
831 printf("%9s:",llstr(isamlog_filepos,llbuff));
832 if (verbose > 1)
833 printf("%5ld ",isamlog_process); /* Write process number */
834 (void) vprintf((char*) format,args);
835 putchar('\n');
836 va_end(args);
837 }
838
839
cmp_filename(struct file_info * file_info,char * name)840 static my_bool cmp_filename(struct file_info *file_info, char * name)
841 {
842 if (!file_info)
843 return 1;
844 return strcmp(file_info->name,name) ? 1 : 0;
845 }
846
847 #include "mi_extrafunc.h"
848