home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 568a.lha / RexxRMF_v1.1 / reloadrmfdata.c < prev    next >
C/C++ Source or Header  |  1991-09-11  |  6KB  |  203 lines

  1.  
  2. /*-------------------------------------------------------------------------*
  3.  *                                                                         *
  4.  * Attempts to rebuild both the Data and Index file, eliminating deleted   *
  5.  * blocks.  The current Index file MUST reflect the contents of the data   *
  6.  * data file.  All index information is retained, including alternates.    *
  7.  *                                                                         *
  8.  * Compiles under Manx C 5.0d                                              *
  9.  *   cc -hi allprecomp -pp -wnuq -o reloadrmfdata.o reloadrmfdata.c        *
  10.  *   ln -o reloadrmfdata reloadrmfdata.o -lc                               *
  11.  *                                                                         *
  12.  * Compile/link for Lattice 5.10                                           *
  13.  *     LC -L -Hinclude:allprecomp.q reloadrmfdata                          *
  14.  *           (pre-compiled header file)                                    *
  15.  *                                                                         *
  16.  *-------------------------------------------------------------------------*/
  17.  
  18.  
  19. #include "avl.structs.h"
  20.  
  21. int reload_data_index( char *ifile, char *dfile, char *nindex, char *ndata);
  22.  
  23. main( int argc, char *argv[] )
  24. {
  25.     char *oldi,*oldd,*newi,*newd;
  26.     long oldnamelen;
  27.     int  noerr;
  28.     
  29.     if( argc < 2 ) {
  30.         printf("\n Syntax is %s 'name of rmf file'\n",argv[0]);
  31.         exit( 10 );
  32.     }
  33.         
  34.     oldnamelen = strlen(argv[1]) + 50;
  35.     
  36.     oldi = AllocMem( oldnamelen, MEMF_CLEAR );
  37.     oldd = AllocMem( oldnamelen, MEMF_CLEAR );
  38.     newi = AllocMem( oldnamelen, MEMF_CLEAR );
  39.     newd = AllocMem( oldnamelen, MEMF_CLEAR );
  40.     
  41.     strcpy(oldd,argv[1]);
  42.     strcpy(oldi,argv[1]);
  43.     strcpy(newi,argv[1]);
  44.     strcpy(newd,argv[1]);
  45.     
  46.     strcat(oldi,".rmfindex");
  47.     strcat(newi,".NEW.RMFINDEX");
  48.     strcat(newd,".NEW");
  49.     
  50.     noerr = reload_data_index( oldi, oldd, newi, newd );
  51.     if( noerr ) {
  52.         printf("\n\n   DataFile %s rebuilt as %s\n",oldd,newd);
  53.         printf("\n   IndexFile %s rebuilt as %s\n\n",oldi,newi);
  54.     }
  55.     else 
  56.        printf("\n\n  Error Occured during rebuild \n\n");
  57.     
  58.     FreeMem( oldi, oldnamelen);
  59.     FreeMem( oldd, oldnamelen);
  60.     FreeMem( newi, oldnamelen);
  61.     FreeMem( newd, oldnamelen);
  62.     
  63. }
  64.  
  65.  
  66. int reload_data_index( char *indexfile, char *datafile, char *newindex, char *newdata)
  67. {
  68.  
  69.  
  70.    struct IndexFile      *ix;
  71.    struct DiskIndex      *dtnode; 
  72.    struct DataFileHeader *dfh;
  73.    struct RecordHeader   *rh;
  74.    char                  *datablock; 
  75.    ULONG                 blocklen;
  76.    ULONG                 recaddr,rba;
  77.    ULONG                 ifd,dfd,nifd,ndfd;
  78.    long                  err,total_keylen;
  79.    int                   ok,i;
  80.  
  81.    
  82.    ifd = Open(indexfile,MODE_OLDFILE);
  83.    if( !ifd ) {
  84.        printf(" COULD NOT OPEN INDEX file %s\n",indexfile);
  85.        return( FALSE );
  86.    }
  87.    
  88.    dfd = Open(datafile,MODE_OLDFILE);
  89.    if( !ifd ) {
  90.        printf(" COULD NOT OPEN DATA file %s\n",datafile);
  91.        Close( ifd );
  92.        return( FALSE );
  93.    }
  94.    
  95.    ndfd = Open(newdata,MODE_NEWFILE);
  96.    if( !ifd ) {
  97.        printf(" COULD NOT CREATE DATA file %s\n",newdata);
  98.        Close( dfd );
  99.        Close( ifd );
  100.        return( FALSE );
  101.    }
  102.    
  103.    nifd = Open(newindex,MODE_NEWFILE);
  104.    if( !nifd ) {
  105.        printf(" COULD NOT CREATE file %s\n",newindex);
  106.        Close( ndfd );
  107.        Close( dfd );
  108.        Close( ifd );
  109.        return( FALSE );
  110.    }
  111.    
  112.  
  113.    ix     = AllocMem( sizeof(struct IndexFile), MEMF_CLEAR );
  114.    dtnode = AllocMem( sizeof(struct DiskIndex), MEMF_CLEAR );
  115.    dfh    = AllocMem( sizeof(struct DataFileHeader), MEMF_CLEAR );
  116.    rh     = AllocMem( sizeof(struct RecordHeader), MEMF_CLEAR );
  117.    if( !rh || !ix || !dtnode || !dfh ) {
  118.        printf("\nUnable to allocate memory\n\n");
  119.        if( ix )
  120.            FreeMem( ix, sizeof(struct IndexFile) );
  121.        if( dtnode ) 
  122.            FreeMem( dtnode, sizeof(struct DiskIndex) );
  123.        if( dfh ) 
  124.            FreeMem( dfh, sizeof(struct DataFileHeader) );
  125.        if( rh ) 
  126.            FreeMem( rh, sizeof(struct RecordHeader) );
  127.        Close( ndfd );
  128.        Close( nifd );
  129.        Close( dfd );
  130.        Close( ifd );
  131.        return( FALSE );    
  132.    }
  133.    
  134.    ok = Read(ifd,ix,sizeof(struct IndexFile));
  135.    ok = Write(nifd,ix,sizeof(struct IndexFile));
  136.    
  137.    ok = Read(dfd,dfh,sizeof(struct DataFileHeader));
  138.    ok = Write(ndfd,dfh,sizeof(struct DataFileHeader));
  139.    
  140.    ok = TRUE;
  141.    while( ok ) {
  142.    
  143.       /* fixed portion */
  144.       err = Read(ifd,dtnode,sizeof(struct DI_Info));
  145.       if( err <= 0 ) 
  146.            break;
  147.       
  148.       total_keylen = 0;
  149.       for( i=0; i < MAXINDICES; i++ )
  150.            total_keylen = total_keylen + dtnode->di_info.keylen[i]; 
  151.            
  152.       /* read variable key portion */
  153.       err = Read(ifd,&dtnode->keydata[0],total_keylen);
  154.       
  155.       if( err <= 0 )
  156.           break;
  157.  
  158.       if( dtnode->di_info.recstatus == 'D' )  /* a deleted block */
  159.           continue;                           /* causing it to be removed */
  160.  
  161.       recaddr = dtnode->di_info.recaddr;      /* else gonna assume its 'A' */
  162.       
  163.       Seek(dfd,recaddr,OFFSET_BEGINNING);
  164.       err = Read( dfd, rh, sizeof(struct RecordHeader));
  165.       if( err <= 0 )
  166.           break;
  167.           
  168.       blocklen  = rh->blocklength;
  169.       datablock = AllocMem( blocklen, MEMF_CLEAR );    
  170.       
  171.       err = Read( dfd, datablock, (blocklen));
  172.       if( err <= 0 )
  173.           break;
  174.           
  175.       rba = Seek( ndfd, 0L, OFFSET_END);    
  176.       rba = Seek( ndfd, 0L, OFFSET_CURRENT);    
  177.       
  178.       err = Write( ndfd, rh, sizeof(struct RecordHeader));
  179.       err = Write( ndfd, datablock, (blocklen));
  180.       
  181.       dtnode->di_info.recaddr = rba; /* index record gets a new RBA here */
  182.       
  183.       err = Write( nifd, dtnode, sizeof(struct DI_Info)+total_keylen);
  184.       
  185.       FreeMem( datablock,blocklen);
  186.    }   
  187.    
  188.    
  189.    Close( ndfd );
  190.    Close( nifd );
  191.    Close( dfd );
  192.    Close( ifd );
  193.    
  194.    FreeMem( ix, sizeof(struct IndexFile) );
  195.    FreeMem( dtnode, sizeof(struct DiskIndex) );
  196.    FreeMem( dfh, sizeof(struct DataFileHeader) );
  197.    FreeMem( rh, sizeof(struct RecordHeader) );
  198.    
  199.    return( !err );
  200.    
  201. }
  202.  
  203.