SHARE
TWEET

sbester

a guest Aug 25th, 2007 1,385 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <errno.h>
  4.  
  5. int main(int argc, char * argv[])
  6. {
  7.         FILE *myd_file;
  8.         FILE *del_file;
  9.         unsigned char *record=NULL;
  10.         size_t reclength=0;
  11.         unsigned long long offset=0;
  12.         unsigned long long recnum=0;
  13.         unsigned long long numdeleted=0;
  14.         unsigned long long numpresent=0;
  15.        
  16.         if(argc != 4)
  17.         {
  18.                 printf("usage: ./undeleter /path/to/original.MYD /path/to/recovered.MYD record_length\n");
  19.                 printf("example: to recover table history and place deleted records into a table called history_recovered, having record length of 42 (as shown in show table status:\n");
  20.                 printf("./undeleter /data/test/history.MYD /data/test/history_recovered.MYD 42\n\n");
  21.                 return 1;
  22.         }                      
  23.  
  24.         myd_file=fopen(argv[1],"rb");
  25.         if(NULL == myd_file)
  26.         {
  27.                 printf("failed to open input table from file '%s', errno=%d\n",argv[1],errno);
  28.                 return 1;
  29.         }
  30.        
  31.         del_file=fopen(argv[2],"wb");
  32.         if(NULL == del_file)
  33.         {
  34.                 printf("failed to open output table from file '%s', errno=%d\n",argv[2],errno);
  35.                 fclose(myd_file);
  36.                 return 1;
  37.         }
  38.        
  39.         reclength=atoi(argv[3]);
  40.         printf("allocating a block of memory for records, %lu bytes\n",reclength);
  41.         record=(unsigned char*)calloc(reclength,1);
  42.         if(NULL == record)
  43.         {
  44.                 printf("failed to allocate memory for a single record block\n");
  45.                 fclose(myd_file);
  46.                 fclose(del_file);
  47.                 return 1;                
  48.         }
  49.        
  50.         printf("starting to read records\n");
  51.         while(!feof(myd_file))
  52.         {
  53.                 if(fread(record,reclength,1,myd_file) < 1)
  54.                         continue;
  55.                
  56.                 if(record[0] == 0)
  57.                 {
  58.                         numdeleted++;
  59.                         record[0]=0xc0; //reset the delete flag
  60.                         printf("found deleted record at offset %lu\n",offset);      
  61.                         if(fwrite(record,reclength,1,del_file) < 1)
  62.                         {
  63.                                 printf("error trying to write record to '%s', errno=%d\n",argv[2],errno);        
  64.                         }
  65.                 }
  66.                 else
  67.                 {
  68.                         numpresent++;      
  69.                 }
  70.                 offset+=reclength;
  71.         }
  72.        
  73.         fclose(myd_file);
  74.         fclose(del_file);
  75.         free(record);
  76.        
  77.         printf("-----------------\n");
  78.         printf("    completed    \n");
  79.         printf("-----------------\n");
  80.         printf("records copied into '%s': %lu\n",argv[2],numdeleted);
  81.         printf("records present in '%s': %lu\n\n",argv[1],numpresent);
  82.         printf("next steps:\n");
  83.         printf("1. remove the MYI for '%s'\n",argv[2]);
  84.         printf("2. make sure '%s' definition .frm is created without any unique or PK keys\n",argv[2]);
  85.         printf("3. repair table <recovered table> use_frm\n");
  86.         printf("\n\n");
  87.        
  88.         return 0;
  89. }
RAW Paste Data
Top