home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / isamlog.c < prev    next >
C/C++ Source or Header  |  2000-09-14  |  22KB  |  840 lines

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