home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / sunrar.zip / extract.c < prev    next >
Text File  |  1998-07-01  |  9KB  |  342 lines

  1. void ExtrFile(void)
  2. {
  3.   struct FileStat FS;
  4.   char DestFileName[NM],*ChPtr;
  5.   long FileCount=0,TotalFileCount,ErrCount=0;
  6.   int SkipSolid,ExtrFile,MDCode,Size,AllArgsUsed;
  7.   int UserReject,TmpPassword=0,BrokenFile;
  8.   int FirstFile;
  9.  
  10.  
  11. GetNextArchive:
  12.   if (UnpMemory!=NULL)
  13.   {
  14.     free(UnpMemory);
  15.     UnpMemory=NULL;
  16.   }
  17.  
  18.   while (ReadArcName())
  19.   {
  20.  
  21. SkipToFirstVol:
  22.  
  23.     TotalFileCount=ArgUsed=AllArgsUsed=0;
  24.     FirstFile=1;
  25.     if ((ArcPtr=wopen(ArcName,READBINARY,M_DENYWRITE))==NULL)
  26.       continue;
  27.  
  28.     if (TmpPassword!=0)
  29.       TmpPassword=Password[0]=0;
  30.     if (!IsArchive())
  31.     {
  32.       mprintf(MNotRAR,ArcName);
  33.       tclose(ArcPtr);
  34.       continue;
  35.     }
  36.  
  37.     if (MainCommand[0]=='T')
  38.       mprintf(MExtrTest,ArcName);
  39.     else
  40.       mprintf(MExtracting,ArcName);
  41.  
  42.     ViewComment();
  43.  
  44.     if ((UnpMemory=malloc(UNP_MEMORY))==NULL)
  45.     {
  46.       mprintf(MExtrOutMem);
  47.       ErrExit(EEMPTY,MEMORY_ERROR);
  48.     }
  49.  
  50.     tseek(ArcPtr,NewMhd.HeadSize-MainHeadSize,SEEK_CUR);
  51.     UnpVolume=0;
  52.  
  53.     while (1)
  54.     {
  55.       Size=ReadBlock(FILE_HEAD | READSUBBLOCK);
  56.       if (Size<=0 && UnpVolume==0)
  57.         break;
  58.       if (BlockHead.HeadType==SUB_HEAD)
  59.       {
  60.         tseek(ArcPtr,NextBlockPos,SEEK_SET);
  61.         continue;
  62.       }
  63.  
  64.       if (AllArgsUsed)
  65.         break;
  66.  
  67.       ConvertPath(ArcFileName,ArcFileName);
  68.  
  69.       ConvertFlags();
  70.  
  71.       if ((NewLhd.Flags & LHD_SPLIT_BEFORE) && SolidType && FirstFile)
  72.       {
  73.         tclose(ArcPtr);
  74.         ChPtr=strrchr(ArcName,'.');
  75.         if (ChPtr==NULL || stricomp(ChPtr,".rar")!=0)
  76.         {
  77.           SetExt(ArcName,"rar");
  78.           if (FileExist(ArcName))
  79.             goto SkipToFirstVol;
  80.           SetExt(ArcName,"exe");
  81.           if (FileExist(ArcName))
  82.             goto SkipToFirstVol;
  83.         }
  84.         mprintf(MNeedFirstVol);
  85.         if (TotalArcCount>1)
  86.           goto GetNextArchive;
  87.         else
  88.           ErrExit(EEMPTY,FATAL_ERROR);
  89.       }
  90.       FirstFile=0;
  91.       if (UnpVolume && Size==0 && !MergeArchive(0))
  92.       {
  93.         ErrCount++;
  94.         ExitCode=WARNING;
  95.         goto GetNextArchive;
  96.       }
  97.       UnpVolume=(NewLhd.Flags & LHD_SPLIT_AFTER);
  98.  
  99.       tseek(ArcPtr,NextBlockPos-NewLhd.PackSize,SEEK_SET);
  100.  
  101.       TestMode=0;
  102.       ExtrFile=0;
  103.       SkipSolid=0;
  104.  
  105.       if (IsProcessFile(COMPARE_PATH) && (NewLhd.Flags & LHD_SPLIT_BEFORE)==0
  106.           || (SkipSolid=SolidType)!=0)
  107.       {
  108.  
  109.         if (NewLhd.Flags & LHD_PASSWORD)
  110.         {
  111.           if (*Password==0)
  112.           {
  113.             if (GetPassword(1)!=1)
  114.               ErrExit(EEMPTY,USER_BREAK);
  115.             TmpPassword=(SolidType) ? 2 : 1;
  116.           }
  117.           else
  118.             if (TmpPassword==1)
  119.             {
  120.               mprintf(MUseCurPsw,ArcFileName);
  121.               Ask(MYesNoAll);
  122.               switch(Choice)
  123.               {
  124.                 case -1:
  125.                   ErrExit(EEMPTY,USER_BREAK);
  126.                 case 2:
  127.                   if (GetPassword(1)!=1)
  128.                     ErrExit(EEMPTY,USER_BREAK);
  129.                   break;
  130.                 case 3:
  131.                   TmpPassword=2;
  132.                   break;
  133.               }
  134.             }
  135.         }
  136.  
  137.         strcpy(DestFileName,ExtrPath);
  138.  
  139.         if (MainCommand[0]=='E')
  140.           strcat(DestFileName,PointToName(ArcFileName));
  141.         else
  142.           strcat(DestFileName,ArcFileName);
  143.  
  144.         ExtrFile=!SkipSolid;
  145.         if ((Opt.FreshFiles || Opt.UpdateFiles) && (MainCommand[0]=='E' || MainCommand[0]=='X'))
  146.         {
  147.           if (GetFileStat(DestFileName,&FS))
  148.           {
  149.             if ( FS.FileTime >= NewLhd.FileTime)
  150.               ExtrFile=0;
  151.           }
  152.           else
  153.             if (Opt.FreshFiles)
  154.               ExtrFile=0;
  155.         }
  156.  
  157.         if (NewLhd.UnpVer<13 || NewLhd.UnpVer>UNP_VER)
  158.         {
  159.           mprintf(MExtrUnknMeth,ArcFileName);
  160.           ExtrFile=0;
  161.           ErrCount++;
  162.           ExitCode=WARNING;
  163.         }
  164.  
  165.         if (IsLabel(NewLhd.FileAttr))
  166.           continue;
  167.         if (IsDir(NewLhd.FileAttr))
  168.         {
  169.           if (MainCommand[0]=='P' || MainCommand[0]=='E')
  170.             continue;
  171.           if (SkipSolid)
  172.           {
  173.             mprintf(MExtrSkipDir,ArcFileName);
  174.             continue;
  175.           }
  176.           FileCount++;
  177.           if (MainCommand[0]=='T')
  178.           {
  179.             mprintf(MExtrTestDir,ArcFileName);
  180.             continue;
  181.           }
  182.           if ((MDCode=MakeDir(DestFileName,NewLhd.FileAttr))==-1 && errno==ENOENT)
  183.           {
  184.             CreatePath(DestFileName);
  185.             if ((MDCode=MakeDir(DestFileName,NewLhd.FileAttr))==-1 && errno!=EEXIST)
  186.             {
  187.               mprintf(MExtrErrMkDir,ArcFileName);
  188.               ExitCode=WARNING;
  189.             }
  190.           }
  191.           if (MDCode==0)
  192.             mprintf(MCreatDir,ArcFileName);
  193.           continue;
  194.         }
  195.         else
  196.         {
  197.           if (MainCommand[0]=='T' && ExtrFile)
  198.             TestMode=1;
  199.           if (MainCommand[0]=='P' && ExtrFile)
  200.             FilePtr=stdout;
  201.           if ((MainCommand[0]=='E' || MainCommand[0]=='X') && ExtrFile)
  202.           {
  203.             FilePtr=FileCreate(DestFileName,Opt.Overwrite,&UserReject);
  204.             if (FilePtr==NULL)
  205.             {
  206.               if (!UserReject)
  207.               {
  208.                 mprintf(MExtrErrCreat,DestFileName);
  209.                 ErrCount++;
  210.                 ExitCode=WARNING;
  211.               }
  212.               ExtrFile=0;
  213.               AllArgsUsed=IsAllArgsUsed();
  214.             }
  215.           }
  216.         }
  217.  
  218.         if (!ExtrFile && SolidType)
  219.         {
  220.           SkipSolid=1;
  221.           TestMode=1;
  222.           ExtrFile=1;
  223.         }
  224.         if (ExtrFile)
  225.         {
  226.           if (!SkipSolid)
  227.           {
  228.             if (!TestMode && CheckForDevice(FilePtr))
  229.               ErrExit(EWRITE,WRITE_ERROR);
  230.             FileCount++;
  231.           }
  232.           TotalFileCount++;
  233.           if (SkipSolid)
  234.             mprintf(MExtrSkipFile,ArcFileName);
  235.           else
  236.             switch(MainCommand[0])
  237.             {
  238.               case 'T':
  239.                 mprintf(MExtrTestFile,ArcFileName);
  240.                 break;
  241.               case 'P':
  242.                 mprintf(MExtrPrinting,ArcFileName);
  243.                 CheckWriteSize=0;
  244.                 break;
  245.               case 'X':
  246.               case 'E':
  247.                 mprintf(MExtrFile,DestFileName);
  248.                 break;
  249.             }
  250.           strcpy(CurExtrFile,SkipSolid ? "":DestFileName);
  251.           CurUnpRead=CurUnpWrite=0;
  252.           UnpFileCRC=(ArcFormat==OLD) ? 0 : 0xFFFFFFFFL;
  253.           if ((*Password!=0) && (NewLhd.Flags & LHD_PASSWORD))
  254.             Encryption=NewLhd.UnpVer;
  255.           else
  256.             Encryption=0;
  257.           if (Encryption)
  258.             SetCryptKeys(Password);
  259.           UnpPackedSize=NewLhd.PackSize;
  260.           DestUnpSize=NewLhd.UnpSize;
  261.           RdUnpPtr=ArcPtr;
  262.           WrUnpPtr=FilePtr;
  263.           Suspend=0;
  264.           Repack=0;
  265.           SkipUnpCRC=SkipSolid;
  266.           if (NewLhd.Method==0x30)
  267.             UnstoreFile();
  268.           else
  269.             if (NewLhd.UnpVer<=15)
  270.               tunpack(UnpMemory,TotalFileCount>1 && SolidType,OLD_UNPACK);
  271.             else
  272.               tunpack(UnpMemory,NewLhd.Flags & LHD_SOLID,NEW_UNPACK);
  273.           if (!SkipSolid)
  274.             AllArgsUsed=IsAllArgsUsed();
  275.  
  276.           if (MainCommand[0]=='P')
  277.             CheckWriteSize=1;
  278.           if (ArcPtr!=NULL)
  279.             tseek(ArcPtr,NextBlockPos,SEEK_SET);
  280.           if (!SkipSolid)
  281.             if ((ArcFormat==OLD && UnpFileCRC==NewLhd.FileCRC) || (ArcFormat==NEW && UnpFileCRC==~NewLhd.FileCRC))
  282.             {
  283.               if (MainCommand[0]!='P')
  284.                 mprintf(MExtrOk);
  285.               BrokenFile=0;
  286.             }
  287.             else
  288.             {
  289.               if (NewLhd.Flags & LHD_PASSWORD)
  290.                 mprintf(MEncrBadCRC,ArcFileName);
  291.               else
  292.                 mprintf(MExtrBadCRC,ArcFileName);
  293.               ExitCode=CRC_ERROR;
  294.               ErrCount++;
  295.               BrokenFile=1;
  296.             }
  297.           if (TestMode==1)
  298.             TestMode=0;
  299.           else
  300.           {
  301.             if (MainCommand[0]=='X' || MainCommand[0]=='E')
  302.             {
  303.               SetOpenFileStat(FilePtr);
  304.               tclose(FilePtr);
  305.               SetCloseFileStat(DestFileName);
  306.               if (BrokenFile && !Opt.KeepBroken)
  307.                 remove(DestFileName);
  308.             }
  309.           }
  310.           *CurExtrFile=0;
  311.         }
  312.       }
  313.       if (ArcPtr==NULL)
  314.         goto GetNextArchive;
  315.       if (!ExtrFile)
  316.         if (!SolidType)
  317.           tseek(ArcPtr,NextBlockPos,SEEK_SET);
  318.         else
  319.           if (!SkipSolid)
  320.             break;
  321.       if (AllArgsUsed)
  322.         break;
  323.     }
  324.     free(UnpMemory);
  325.     UnpMemory=NULL;
  326.     tclose(ArcPtr);
  327.   }
  328.   if (FileCount==0)
  329.   {
  330.     mprintf(MExtrNoFiles);
  331.     ExitCode=WARNING;
  332.   }
  333.   else
  334.     if (ErrCount==0)
  335.       mprintf(MExtrAllOk);
  336.     else
  337.       mprintf(MExtrTotalErr,ErrCount);
  338.   if (TmpPassword!=0)
  339.     memset(Password,0,sizeof(Password));
  340. }
  341.  
  342.