recvol.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "rar.hpp"
  2. // Buffer size for all volumes involved.
  3. static const size_t TotalBufferSize=0x4000000;
  4. RecVolumes::RecVolumes()
  5. {
  6. Buf.Alloc(TotalBufferSize);
  7. memset(SrcFile,0,sizeof(SrcFile));
  8. }
  9. RecVolumes::~RecVolumes()
  10. {
  11. for (int I=0;I<sizeof(SrcFile)/sizeof(SrcFile[0]);I++)
  12. delete SrcFile[I];
  13. }
  14. bool RecVolumes::Restore(RAROptions *Cmd,const char *Name,
  15. const wchar *NameW,bool Silent)
  16. {
  17. char ArcName[NM];
  18. wchar ArcNameW[NM];
  19. strcpy(ArcName,Name);
  20. wcscpy(ArcNameW,NameW);
  21. char *Ext=GetExt(ArcName);
  22. bool NewStyle=false;
  23. bool RevName=Ext!=NULL && stricomp(Ext,".rev")==0;
  24. if (RevName)
  25. {
  26. for (int DigitGroup=0;Ext>ArcName && DigitGroup<3;Ext--)
  27. if (!IsDigit(*Ext))
  28. if (IsDigit(*(Ext-1)) && (*Ext=='_' || DigitGroup<2))
  29. DigitGroup++;
  30. else
  31. if (DigitGroup<2)
  32. {
  33. NewStyle=true;
  34. break;
  35. }
  36. while (IsDigit(*Ext) && Ext>ArcName+1)
  37. Ext--;
  38. strcpy(Ext,"*.*");
  39. if (*ArcNameW!=0)
  40. {
  41. wchar *ExtW=GetExt(ArcNameW);
  42. for (int DigitGroup=0;ExtW>ArcNameW && DigitGroup<3;ExtW--)
  43. if (!IsDigit(*ExtW))
  44. if (IsDigit(*(ExtW-1)) && (*ExtW=='_' || DigitGroup<2))
  45. DigitGroup++;
  46. else
  47. if (DigitGroup<2)
  48. {
  49. NewStyle=true;
  50. break;
  51. }
  52. while (IsDigit(*ExtW) && ExtW>ArcNameW+1)
  53. ExtW--;
  54. wcscpy(ExtW,L"*.*");
  55. }
  56. FindFile Find;
  57. Find.SetMask(ArcName);
  58. Find.SetMaskW(ArcNameW);
  59. FindData fd;
  60. while (Find.Next(&fd))
  61. {
  62. Archive Arc(Cmd);
  63. if (Arc.WOpen(fd.Name,fd.NameW) && Arc.IsArchive(true))
  64. {
  65. strcpy(ArcName,fd.Name);
  66. wcscpy(ArcNameW,fd.NameW);
  67. break;
  68. }
  69. }
  70. }
  71. Archive Arc(Cmd);
  72. if (!Arc.WCheckOpen(ArcName,ArcNameW))
  73. return(false);
  74. if (!Arc.Volume)
  75. {
  76. #ifndef SILENT
  77. Log(ArcName,St(MNotVolume),ArcName);
  78. #endif
  79. return(false);
  80. }
  81. bool NewNumbering=(Arc.NewMhd.Flags & MHD_NEWNUMBERING)!=0;
  82. Arc.Close();
  83. char *VolNumStart=VolNameToFirstName(ArcName,ArcName,NewNumbering);
  84. char RecVolMask[NM];
  85. strcpy(RecVolMask,ArcName);
  86. size_t BaseNamePartLength=VolNumStart-ArcName;
  87. strcpy(RecVolMask+BaseNamePartLength,"*.rev");
  88. wchar RecVolMaskW[NM];
  89. size_t BaseNamePartLengthW=0;
  90. *RecVolMaskW=0;
  91. if (*ArcNameW!=0)
  92. {
  93. wchar *VolNumStartW=VolNameToFirstName(ArcNameW,ArcNameW,NewNumbering);
  94. wcscpy(RecVolMaskW,ArcNameW);
  95. BaseNamePartLengthW=VolNumStartW-ArcNameW;
  96. wcscpy(RecVolMaskW+BaseNamePartLengthW,L"*.rev");
  97. }
  98. #ifndef SILENT
  99. int64 RecFileSize=0;
  100. #endif
  101. // We cannot display "Calculating CRC..." message here, because we do not
  102. // know if we'll find any recovery volumes. We'll display it after finding
  103. // the first recovery volume.
  104. bool CalcCRCMessageDone=false;
  105. FindFile Find;
  106. Find.SetMask(RecVolMask);
  107. Find.SetMaskW(RecVolMaskW);
  108. FindData RecData;
  109. int FileNumber=0,RecVolNumber=0,FoundRecVolumes=0,MissingVolumes=0;
  110. char PrevName[NM];
  111. wchar PrevNameW[NM];
  112. while (Find.Next(&RecData))
  113. {
  114. char *CurName=RecData.Name;
  115. wchar *CurNameW=RecData.NameW;
  116. int P[3];
  117. if (!RevName && !NewStyle)
  118. {
  119. NewStyle=true;
  120. char *Dot=GetExt(CurName);
  121. if (Dot!=NULL)
  122. {
  123. int LineCount=0;
  124. Dot--;
  125. while (Dot>CurName && *Dot!='.')
  126. {
  127. if (*Dot=='_')
  128. LineCount++;
  129. Dot--;
  130. }
  131. if (LineCount==2)
  132. NewStyle=false;
  133. }
  134. wchar *DotW=GetExt(CurNameW);
  135. if (DotW!=NULL)
  136. {
  137. int LineCount=0;
  138. DotW--;
  139. while (DotW>CurNameW && *DotW!='.')
  140. {
  141. if (*DotW=='_')
  142. LineCount++;
  143. DotW--;
  144. }
  145. if (LineCount==2)
  146. NewStyle=false;
  147. }
  148. }
  149. if (NewStyle)
  150. {
  151. if (!CalcCRCMessageDone)
  152. {
  153. #ifndef SILENT
  154. mprintf(St(MCalcCRCAllVol));
  155. #endif
  156. CalcCRCMessageDone=true;
  157. }
  158. File CurFile;
  159. CurFile.TOpen(CurName,CurNameW);
  160. CurFile.Seek(0,SEEK_END);
  161. int64 Length=CurFile.Tell();
  162. CurFile.Seek(Length-7,SEEK_SET);
  163. for (int I=0;I<3;I++)
  164. P[2-I]=CurFile.GetByte()+1;
  165. uint FileCRC=0;
  166. for (int I=0;I<4;I++)
  167. FileCRC|=CurFile.GetByte()<<(I*8);
  168. if (FileCRC!=CalcFileCRC(&CurFile,Length-4))
  169. {
  170. #ifndef SILENT
  171. mprintf(St(MCRCFailed),CurName);
  172. #endif
  173. continue;
  174. }
  175. }
  176. else
  177. {
  178. char *Dot=GetExt(CurName);
  179. if (Dot==NULL)
  180. continue;
  181. bool WrongParam=false;
  182. for (int I=0;I<ASIZE(P);I++)
  183. {
  184. do
  185. {
  186. Dot--;
  187. } while (IsDigit(*Dot) && Dot>=CurName+BaseNamePartLength);
  188. P[I]=atoi(Dot+1);
  189. if (P[I]==0 || P[I]>255)
  190. WrongParam=true;
  191. }
  192. if (WrongParam)
  193. continue;
  194. }
  195. if (P[1]+P[2]>255)
  196. continue;
  197. if (RecVolNumber!=0 && RecVolNumber!=P[1] || FileNumber!=0 && FileNumber!=P[2])
  198. {
  199. #ifndef SILENT
  200. Log(NULL,St(MRecVolDiffSets),CurName,PrevName);
  201. #endif
  202. return(false);
  203. }
  204. RecVolNumber=P[1];
  205. FileNumber=P[2];
  206. strcpy(PrevName,CurName);
  207. wcscpy(PrevNameW,CurNameW);
  208. File *NewFile=new File;
  209. NewFile->TOpen(CurName,CurNameW);
  210. SrcFile[FileNumber+P[0]-1]=NewFile;
  211. FoundRecVolumes++;
  212. #ifndef SILENT
  213. if (RecFileSize==0)
  214. RecFileSize=NewFile->FileLength();
  215. #endif
  216. }
  217. #ifndef SILENT
  218. if (!Silent || FoundRecVolumes!=0)
  219. {
  220. mprintf(St(MRecVolFound),FoundRecVolumes);
  221. }
  222. #endif
  223. if (FoundRecVolumes==0)
  224. return(false);
  225. bool WriteFlags[256];
  226. memset(WriteFlags,0,sizeof(WriteFlags));
  227. char LastVolName[NM];
  228. *LastVolName=0;
  229. wchar LastVolNameW[NM];
  230. *LastVolNameW=0;
  231. for (int CurArcNum=0;CurArcNum<FileNumber;CurArcNum++)
  232. {
  233. Archive *NewFile=new Archive;
  234. bool ValidVolume=FileExist(ArcName,ArcNameW);
  235. if (ValidVolume)
  236. {
  237. NewFile->TOpen(ArcName,ArcNameW);
  238. ValidVolume=NewFile->IsArchive(false);
  239. if (ValidVolume)
  240. {
  241. while (NewFile->ReadHeader()!=0)
  242. {
  243. if (NewFile->GetHeaderType()==ENDARC_HEAD)
  244. {
  245. if ((NewFile->EndArcHead.Flags&EARC_DATACRC)!=0 &&
  246. NewFile->EndArcHead.ArcDataCRC!=CalcFileCRC(NewFile,NewFile->CurBlockPos))
  247. {
  248. ValidVolume=false;
  249. #ifndef SILENT
  250. mprintf(St(MCRCFailed),ArcName);
  251. #endif
  252. }
  253. break;
  254. }
  255. NewFile->SeekToNext();
  256. }
  257. }
  258. if (!ValidVolume)
  259. {
  260. NewFile->Close();
  261. char NewName[NM];
  262. strcpy(NewName,ArcName);
  263. strcat(NewName,".bad");
  264. wchar NewNameW[NM];
  265. wcscpy(NewNameW,ArcNameW);
  266. if (*NewNameW!=0)
  267. wcscat(NewNameW,L".bad");
  268. #ifndef SILENT
  269. mprintf(St(MBadArc),ArcName);
  270. mprintf(St(MRenaming),ArcName,NewName);
  271. #endif
  272. RenameFile(ArcName,ArcNameW,NewName,NewNameW);
  273. }
  274. NewFile->Seek(0,SEEK_SET);
  275. }
  276. if (!ValidVolume)
  277. {
  278. // It is important to return 'false' instead of aborting here,
  279. // so if we are called from extraction, we will be able to continue
  280. // extracting. It may happen if .rar and .rev are on read-only disks
  281. // like CDs.
  282. if (!NewFile->Create(ArcName,ArcNameW))
  283. {
  284. // We need to display the title of operation before the error message,
  285. // to make clear for user that create error is related to recovery
  286. // volumes. This is why we cannot use WCreate call here. Title must be
  287. // before create error, not after that.
  288. #ifndef SILENT
  289. mprintf(St(MReconstructing));
  290. #endif
  291. ErrHandler.CreateErrorMsg(ArcName,ArcNameW);
  292. return false;
  293. }
  294. WriteFlags[CurArcNum]=true;
  295. MissingVolumes++;
  296. if (CurArcNum==FileNumber-1)
  297. {
  298. strcpy(LastVolName,ArcName);
  299. wcscpy(LastVolNameW,ArcNameW);
  300. }
  301. #ifndef SILENT
  302. mprintf(St(MAbsNextVol),ArcName);
  303. #endif
  304. }
  305. SrcFile[CurArcNum]=(File*)NewFile;
  306. NextVolumeName(ArcName,ArcNameW,ASIZE(ArcName),!NewNumbering);
  307. }
  308. #ifndef SILENT
  309. mprintf(St(MRecVolMissing),MissingVolumes);
  310. #endif
  311. if (MissingVolumes==0)
  312. {
  313. #ifndef SILENT
  314. mprintf(St(MRecVolAllExist));
  315. #endif
  316. return(false);
  317. }
  318. if (MissingVolumes>FoundRecVolumes)
  319. {
  320. #ifndef SILENT
  321. mprintf(St(MRecVolCannotFix));
  322. #endif
  323. return(false);
  324. }
  325. #ifndef SILENT
  326. mprintf(St(MReconstructing));
  327. #endif
  328. RSCoder RSC(RecVolNumber);
  329. int TotalFiles=FileNumber+RecVolNumber;
  330. int Erasures[256],EraSize=0;
  331. for (int I=0;I<TotalFiles;I++)
  332. if (WriteFlags[I] || SrcFile[I]==NULL)
  333. Erasures[EraSize++]=I;
  334. #ifndef SILENT
  335. int64 ProcessedSize=0;
  336. #ifndef GUI
  337. int LastPercent=-1;
  338. mprintf(" ");
  339. #endif
  340. #endif
  341. // Size of per file buffer.
  342. size_t RecBufferSize=TotalBufferSize/TotalFiles;
  343. while (true)
  344. {
  345. Wait();
  346. int MaxRead=0;
  347. for (int I=0;I<TotalFiles;I++)
  348. if (WriteFlags[I] || SrcFile[I]==NULL)
  349. memset(&Buf[I*RecBufferSize],0,RecBufferSize);
  350. else
  351. {
  352. int ReadSize=SrcFile[I]->Read(&Buf[I*RecBufferSize],RecBufferSize);
  353. if (ReadSize!=RecBufferSize)
  354. memset(&Buf[I*RecBufferSize+ReadSize],0,RecBufferSize-ReadSize);
  355. if (ReadSize>MaxRead)
  356. MaxRead=ReadSize;
  357. }
  358. if (MaxRead==0)
  359. break;
  360. #ifndef SILENT
  361. int CurPercent=ToPercent(ProcessedSize,RecFileSize);
  362. if (!Cmd->DisablePercentage && CurPercent!=LastPercent)
  363. {
  364. mprintf("\b\b\b\b%3d%%",CurPercent);
  365. LastPercent=CurPercent;
  366. }
  367. ProcessedSize+=MaxRead;
  368. #endif
  369. for (int BufPos=0;BufPos<MaxRead;BufPos++)
  370. {
  371. if ((BufPos&0xffff)==0)
  372. Wait();
  373. byte Data[256];
  374. for (int I=0;I<TotalFiles;I++)
  375. Data[I]=Buf[I*RecBufferSize+BufPos];
  376. RSC.Decode(Data,TotalFiles,Erasures,EraSize);
  377. for (int I=0;I<EraSize;I++)
  378. Buf[Erasures[I]*RecBufferSize+BufPos]=Data[Erasures[I]];
  379. /*
  380. for (int I=0;I<FileNumber;I++)
  381. Buf[I*RecBufferSize+BufPos]=Data[I];
  382. */
  383. }
  384. for (int I=0;I<FileNumber;I++)
  385. if (WriteFlags[I])
  386. SrcFile[I]->Write(&Buf[I*RecBufferSize],MaxRead);
  387. }
  388. for (int I=0;I<RecVolNumber+FileNumber;I++)
  389. if (SrcFile[I]!=NULL)
  390. {
  391. File *CurFile=SrcFile[I];
  392. if (NewStyle && WriteFlags[I])
  393. {
  394. int64 Length=CurFile->Tell();
  395. CurFile->Seek(Length-7,SEEK_SET);
  396. for (int J=0;J<7;J++)
  397. CurFile->PutByte(0);
  398. }
  399. CurFile->Close();
  400. SrcFile[I]=NULL;
  401. }
  402. if (*LastVolName!=0 || *LastVolNameW!=0)
  403. {
  404. // Truncate the last volume to its real size.
  405. Archive Arc(Cmd);
  406. if (Arc.Open(LastVolName,LastVolNameW,false,true) && Arc.IsArchive(true) &&
  407. Arc.SearchBlock(ENDARC_HEAD))
  408. {
  409. Arc.Seek(Arc.NextBlockPos,SEEK_SET);
  410. char Buf[8192];
  411. int ReadSize=Arc.Read(Buf,sizeof(Buf));
  412. int ZeroCount=0;
  413. while (ZeroCount<ReadSize && Buf[ZeroCount]==0)
  414. ZeroCount++;
  415. if (ZeroCount==ReadSize)
  416. {
  417. Arc.Seek(Arc.NextBlockPos,SEEK_SET);
  418. Arc.Truncate();
  419. }
  420. }
  421. }
  422. #if !defined(GUI) && !defined(SILENT)
  423. if (!Cmd->DisablePercentage)
  424. mprintf("\b\b\b\b100%%");
  425. if (!Silent && !Cmd->DisableDone)
  426. mprintf(St(MDone));
  427. #endif
  428. return(true);
  429. }