1
0

extract.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. #include "rar.hpp"
  2. CmdExtract::CmdExtract(CommandData *Cmd)
  3. {
  4. CmdExtract::Cmd=Cmd;
  5. *ArcName=0;
  6. *DestFileName=0;
  7. TotalFileCount=0;
  8. Unp=new Unpack(&DataIO);
  9. #ifdef RAR_SMP
  10. Unp->SetThreads(Cmd->Threads);
  11. #endif
  12. }
  13. CmdExtract::~CmdExtract()
  14. {
  15. delete Unp;
  16. }
  17. void CmdExtract::DoExtract()
  18. {
  19. #if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT)
  20. Fat32=NotFat32=false;
  21. #endif
  22. PasswordCancelled=false;
  23. DataIO.SetCurrentCommand(Cmd->Command[0]);
  24. FindData FD;
  25. while (Cmd->GetArcName(ArcName,ASIZE(ArcName)))
  26. if (FindFile::FastFind(ArcName,&FD))
  27. DataIO.TotalArcSize+=FD.Size;
  28. Cmd->ArcNames.Rewind();
  29. while (Cmd->GetArcName(ArcName,ASIZE(ArcName)))
  30. {
  31. if (Cmd->ManualPassword)
  32. Cmd->Password.Clean(); // Clean user entered password before processing next archive.
  33. ReconstructDone=false; // Must be reset here, not in ExtractArchiveInit().
  34. UseExactVolName=false; // Must be reset here, not in ExtractArchiveInit().
  35. while (true)
  36. {
  37. EXTRACT_ARC_CODE Code=ExtractArchive();
  38. if (Code!=EXTRACT_ARC_REPEAT)
  39. break;
  40. }
  41. DataIO.ProcessedArcSize+=DataIO.LastArcSize;
  42. }
  43. // Clean user entered password. Not really required, just for extra safety.
  44. if (Cmd->ManualPassword)
  45. Cmd->Password.Clean();
  46. if (TotalFileCount==0 && Cmd->Command[0]!='I' &&
  47. ErrHandler.GetErrorCode()!=RARX_BADPWD) // Not in case of wrong archive password.
  48. {
  49. if (!PasswordCancelled)
  50. uiMsg(UIERROR_NOFILESTOEXTRACT,ArcName);
  51. // Other error codes may explain a reason of "no files extracted" clearer,
  52. // so set it only if no other errors found (wrong mask set by user).
  53. if (ErrHandler.GetErrorCode()==RARX_SUCCESS)
  54. ErrHandler.SetErrorCode(RARX_NOFILES);
  55. }
  56. else
  57. if (!Cmd->DisableDone)
  58. if (Cmd->Command[0]=='I')
  59. mprintf(St(MDone));
  60. else
  61. if (ErrHandler.GetErrorCount()==0)
  62. mprintf(St(MExtrAllOk));
  63. else
  64. mprintf(St(MExtrTotalErr),ErrHandler.GetErrorCount());
  65. }
  66. void CmdExtract::ExtractArchiveInit(Archive &Arc)
  67. {
  68. DataIO.AdjustTotalArcSize(&Arc);
  69. FileCount=0;
  70. MatchedArgs=0;
  71. #ifndef SFX_MODULE
  72. FirstFile=true;
  73. #endif
  74. GlobalPassword=Cmd->Password.IsSet() || uiIsGlobalPasswordSet();
  75. DataIO.UnpVolume=false;
  76. PrevProcessed=false;
  77. AllMatchesExact=true;
  78. AnySolidDataUnpackedWell=false;
  79. StartTime.SetCurrentTime();
  80. }
  81. EXTRACT_ARC_CODE CmdExtract::ExtractArchive()
  82. {
  83. Archive Arc(Cmd);
  84. if (*Cmd->UseStdin!=0)
  85. {
  86. Arc.SetHandleType(FILE_HANDLESTD);
  87. #ifdef USE_QOPEN
  88. Arc.SetProhibitQOpen(true);
  89. #endif
  90. }
  91. else
  92. {
  93. #if defined(_WIN_ALL) && !defined(SFX_MODULE) // WinRAR GUI code also resets the cache.
  94. if (*Cmd->Command=='T' || Cmd->Test)
  95. ResetFileCache(ArcName); // Reset the file cache when testing an archive.
  96. #endif
  97. if (!Arc.WOpen(ArcName))
  98. return EXTRACT_ARC_NEXT;
  99. }
  100. if (!Arc.IsArchive(true))
  101. {
  102. #if !defined(SFX_MODULE) && !defined(RARDLL)
  103. if (CmpExt(ArcName,L"rev"))
  104. {
  105. wchar FirstVolName[NM];
  106. VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),true);
  107. // If several volume names from same volume set are specified
  108. // and current volume is not first in set and first volume is present
  109. // and specified too, let's skip the current volume.
  110. if (wcsicomp(ArcName,FirstVolName)!=0 && FileExist(FirstVolName) &&
  111. Cmd->ArcNames.Search(FirstVolName,false))
  112. return EXTRACT_ARC_NEXT;
  113. RecVolumesTest(Cmd,NULL,ArcName);
  114. TotalFileCount++; // Suppress "No files to extract" message.
  115. return EXTRACT_ARC_NEXT;
  116. }
  117. #endif
  118. mprintf(St(MNotRAR),ArcName);
  119. #ifndef SFX_MODULE
  120. if (CmpExt(ArcName,L"rar"))
  121. #endif
  122. ErrHandler.SetErrorCode(RARX_WARNING);
  123. return EXTRACT_ARC_NEXT;
  124. }
  125. if (Arc.FailedHeaderDecryption) // Bad archive password.
  126. return EXTRACT_ARC_NEXT;
  127. #ifndef SFX_MODULE
  128. if (Arc.Volume && !Arc.FirstVolume && !UseExactVolName)
  129. {
  130. wchar FirstVolName[NM];
  131. VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),Arc.NewNumbering);
  132. // If several volume names from same volume set are specified
  133. // and current volume is not first in set and first volume is present
  134. // and specified too, let's skip the current volume.
  135. if (wcsicomp(ArcName,FirstVolName)!=0 && FileExist(FirstVolName) &&
  136. Cmd->ArcNames.Search(FirstVolName,false))
  137. return EXTRACT_ARC_NEXT;
  138. }
  139. #endif
  140. int64 VolumeSetSize=0; // Total size of volumes after the current volume.
  141. if (Arc.Volume)
  142. {
  143. #ifndef SFX_MODULE
  144. // Try to speed up extraction for independent solid volumes by starting
  145. // extraction from non-first volume if we can.
  146. if (!UseExactVolName && Arc.Solid && DetectStartVolume(Arc.FileName,Arc.NewNumbering))
  147. {
  148. UseExactVolName=true;
  149. return EXTRACT_ARC_REPEAT;
  150. }
  151. #endif
  152. // Calculate the total size of all accessible volumes.
  153. // This size is necessary to display the correct total progress indicator.
  154. wchar NextName[NM];
  155. wcsncpyz(NextName,Arc.FileName,ASIZE(NextName));
  156. while (true)
  157. {
  158. // First volume is already added to DataIO.TotalArcSize
  159. // in initial TotalArcSize calculation in DoExtract.
  160. // So we skip it and start from second volume.
  161. NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering);
  162. FindData FD;
  163. if (FindFile::FastFind(NextName,&FD))
  164. VolumeSetSize+=FD.Size;
  165. else
  166. break;
  167. }
  168. DataIO.TotalArcSize+=VolumeSetSize;
  169. }
  170. ExtractArchiveInit(Arc);
  171. if (*Cmd->Command=='T' || *Cmd->Command=='I')
  172. Cmd->Test=true;
  173. if (*Cmd->Command=='I')
  174. {
  175. Cmd->DisablePercentage=true;
  176. }
  177. else
  178. uiStartArchiveExtract(!Cmd->Test,ArcName);
  179. Arc.ViewComment();
  180. while (1)
  181. {
  182. size_t Size=Arc.ReadHeader();
  183. bool Repeat=false;
  184. if (!ExtractCurrentFile(Arc,Size,Repeat))
  185. if (Repeat)
  186. {
  187. // If we started extraction from not first volume and need to
  188. // restart it from first, we must set DataIO.TotalArcSize to size
  189. // of new first volume to display the total progress correctly.
  190. FindData NewArc;
  191. if (FindFile::FastFind(ArcName,&NewArc))
  192. DataIO.TotalArcSize=NewArc.Size;
  193. return EXTRACT_ARC_REPEAT;
  194. }
  195. else
  196. break;
  197. }
  198. #if !defined(SFX_MODULE) && !defined(RARDLL)
  199. if (Cmd->Test && Arc.Volume)
  200. RecVolumesTest(Cmd,&Arc,ArcName);
  201. #endif
  202. return EXTRACT_ARC_NEXT;
  203. }
  204. bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat)
  205. {
  206. wchar Command=Cmd->Command[0];
  207. if (HeaderSize==0)
  208. if (DataIO.UnpVolume)
  209. {
  210. #ifdef NOVOLUME
  211. return false;
  212. #else
  213. // Supposing we unpack an old RAR volume without the end of archive
  214. // record and last file is not split between volumes.
  215. if (!MergeArchive(Arc,&DataIO,false,Command))
  216. {
  217. ErrHandler.SetErrorCode(RARX_WARNING);
  218. return false;
  219. }
  220. #endif
  221. }
  222. else
  223. return false;
  224. HEADER_TYPE HeaderType=Arc.GetHeaderType();
  225. if (HeaderType!=HEAD_FILE)
  226. {
  227. #ifndef SFX_MODULE
  228. if (Arc.Format==RARFMT15 && HeaderType==HEAD3_OLDSERVICE && PrevProcessed)
  229. SetExtraInfo20(Cmd,Arc,DestFileName);
  230. #endif
  231. if (HeaderType==HEAD_SERVICE && PrevProcessed)
  232. SetExtraInfo(Cmd,Arc,DestFileName);
  233. if (HeaderType==HEAD_ENDARC)
  234. if (Arc.EndArcHead.NextVolume)
  235. {
  236. #ifdef NOVOLUME
  237. return false;
  238. #else
  239. if (!MergeArchive(Arc,&DataIO,false,Command))
  240. {
  241. ErrHandler.SetErrorCode(RARX_WARNING);
  242. return false;
  243. }
  244. Arc.Seek(Arc.CurBlockPos,SEEK_SET);
  245. return true;
  246. #endif
  247. }
  248. else
  249. return false;
  250. Arc.SeekToNext();
  251. return true;
  252. }
  253. PrevProcessed=false;
  254. // We can get negative sizes in corrupt archive and it is unacceptable
  255. // for size comparisons in ComprDataIO::UnpRead, where we cast sizes
  256. // to size_t and can exceed another read or available size. We could fix it
  257. // when reading an archive. But we prefer to do it here, because this
  258. // function is called directly in unrar.dll, so we fix bad parameters
  259. // passed to dll. Also we want to see real negative sizes in the listing
  260. // of corrupt archive. To prevent uninitialized data access perform
  261. // these checks after rejecting zero length and non-file headers above.
  262. if (Arc.FileHead.PackSize<0)
  263. Arc.FileHead.PackSize=0;
  264. if (Arc.FileHead.UnpSize<0)
  265. Arc.FileHead.UnpSize=0;
  266. if (!Cmd->Recurse && MatchedArgs>=Cmd->FileArgs.ItemsCount() && AllMatchesExact)
  267. return false;
  268. int MatchType=MATCH_WILDSUBPATH;
  269. bool EqualNames=false;
  270. wchar MatchedArg[NM];
  271. int MatchNumber=Cmd->IsProcessFile(Arc.FileHead,&EqualNames,MatchType,0,MatchedArg,ASIZE(MatchedArg));
  272. bool MatchFound=MatchNumber!=0;
  273. #ifndef SFX_MODULE
  274. if (Cmd->ExclPath==EXCL_BASEPATH)
  275. {
  276. wcsncpyz(Cmd->ArcPath,MatchedArg,ASIZE(Cmd->ArcPath));
  277. *PointToName(Cmd->ArcPath)=0;
  278. if (IsWildcard(Cmd->ArcPath)) // Cannot correctly process path*\* masks here.
  279. *Cmd->ArcPath=0;
  280. }
  281. #endif
  282. if (MatchFound && !EqualNames)
  283. AllMatchesExact=false;
  284. Arc.ConvertAttributes();
  285. #if !defined(SFX_MODULE) && !defined(RARDLL)
  286. if (Arc.FileHead.SplitBefore && FirstFile && !UseExactVolName)
  287. {
  288. wchar CurVolName[NM];
  289. wcsncpyz(CurVolName,ArcName,ASIZE(CurVolName));
  290. GetFirstVolIfFullSet(ArcName,Arc.NewNumbering,ArcName,ASIZE(ArcName));
  291. if (wcsicomp(ArcName,CurVolName)!=0 && FileExist(ArcName))
  292. {
  293. wcsncpyz(Cmd->ArcName,ArcName,ASIZE(ArcName)); // For GUI "Delete archive after extraction".
  294. // If first volume name does not match the current name and if such
  295. // volume name really exists, let's unpack from this first volume.
  296. Repeat=true;
  297. return false;
  298. }
  299. #ifndef RARDLL
  300. if (!ReconstructDone)
  301. {
  302. ReconstructDone=true;
  303. if (RecVolumesRestore(Cmd,Arc.FileName,true))
  304. {
  305. Repeat=true;
  306. return false;
  307. }
  308. }
  309. #endif
  310. wcsncpyz(ArcName,CurVolName,ASIZE(ArcName));
  311. }
  312. #endif
  313. wchar ArcFileName[NM];
  314. ConvertPath(Arc.FileHead.FileName,ArcFileName,ASIZE(ArcFileName));
  315. if (Arc.FileHead.Version)
  316. {
  317. if (Cmd->VersionControl!=1 && !EqualNames)
  318. {
  319. if (Cmd->VersionControl==0)
  320. MatchFound=false;
  321. int Version=ParseVersionFileName(ArcFileName,false);
  322. if (Cmd->VersionControl-1==Version)
  323. ParseVersionFileName(ArcFileName,true);
  324. else
  325. MatchFound=false;
  326. }
  327. }
  328. else
  329. if (!Arc.IsArcDir() && Cmd->VersionControl>1)
  330. MatchFound=false;
  331. DataIO.UnpVolume=Arc.FileHead.SplitAfter;
  332. DataIO.NextVolumeMissing=false;
  333. Arc.Seek(Arc.NextBlockPos-Arc.FileHead.PackSize,SEEK_SET);
  334. bool ExtrFile=false;
  335. bool SkipSolid=false;
  336. #ifndef SFX_MODULE
  337. if (FirstFile && (MatchFound || Arc.Solid) && Arc.FileHead.SplitBefore)
  338. {
  339. if (MatchFound)
  340. {
  341. uiMsg(UIERROR_NEEDPREVVOL,Arc.FileName,ArcFileName);
  342. #ifdef RARDLL
  343. Cmd->DllError=ERAR_BAD_DATA;
  344. #endif
  345. ErrHandler.SetErrorCode(RARX_OPEN);
  346. }
  347. MatchFound=false;
  348. }
  349. FirstFile=false;
  350. #endif
  351. if (Arc.FileHead.Encrypted && Cmd->SkipEncrypted)
  352. if (Arc.Solid)
  353. return false; // Abort the entire extraction for solid archive.
  354. else
  355. MatchFound=false; // Skip only the current file for non-solid archive.
  356. if (MatchFound || (SkipSolid=Arc.Solid)!=0)
  357. {
  358. // First common call of uiStartFileExtract. It is done before overwrite
  359. // prompts, so if SkipSolid state is changed below, we'll need to make
  360. // additional uiStartFileExtract calls with updated parameters.
  361. if (!uiStartFileExtract(ArcFileName,!Cmd->Test,Cmd->Test && Command!='I',SkipSolid))
  362. return false;
  363. ExtrPrepareName(Arc,ArcFileName,DestFileName,ASIZE(DestFileName));
  364. // DestFileName can be set empty in case of excessive -ap switch.
  365. ExtrFile=!SkipSolid && *DestFileName!=0 && !Arc.FileHead.SplitBefore;
  366. /* // OPENMPT ADDITION
  367. if ((Cmd->FreshFiles || Cmd->UpdateFiles) && (Command=='E' || Command=='X'))
  368. {
  369. FindData FD;
  370. if (FindFile::FastFind(DestFileName,&FD))
  371. {
  372. if (FD.mtime >= Arc.FileHead.mtime)
  373. {
  374. // If directory already exists and its modification time is newer
  375. // than start of extraction, it is likely it was created
  376. // when creating a path to one of already extracted items.
  377. // In such case we'll better update its time even if archived
  378. // directory is older.
  379. if (!FD.IsDir || FD.mtime<StartTime)
  380. ExtrFile=false;
  381. }
  382. }
  383. else
  384. if (Cmd->FreshFiles)
  385. ExtrFile=false;
  386. }
  387. */ // OPENMPT ADDITION
  388. if (!CheckUnpVer(Arc,ArcFileName))
  389. {
  390. ErrHandler.SetErrorCode(RARX_FATAL);
  391. #ifdef RARDLL
  392. Cmd->DllError=ERAR_UNKNOWN_FORMAT;
  393. #endif
  394. Arc.SeekToNext();
  395. return !Arc.Solid; // Can try extracting next file only in non-solid archive.
  396. }
  397. while (true) // Repeat the password prompt for wrong and empty passwords.
  398. {
  399. if (Arc.FileHead.Encrypted)
  400. {
  401. // Stop archive extracting if user cancelled a password prompt.
  402. #ifdef RARDLL
  403. if (!ExtrDllGetPassword())
  404. {
  405. Cmd->DllError=ERAR_MISSING_PASSWORD;
  406. return false;
  407. }
  408. #else
  409. if (!ExtrGetPassword(Arc,ArcFileName))
  410. {
  411. PasswordCancelled=true;
  412. return false;
  413. }
  414. #endif
  415. }
  416. // Set a password before creating the file, so we can skip creating
  417. // in case of wrong password.
  418. SecPassword FilePassword=Cmd->Password;
  419. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  420. ConvertDosPassword(Arc,FilePassword);
  421. #endif
  422. byte PswCheck[SIZE_PSWCHECK];
  423. DataIO.SetEncryption(false,Arc.FileHead.CryptMethod,&FilePassword,
  424. Arc.FileHead.SaltSet ? Arc.FileHead.Salt:NULL,
  425. Arc.FileHead.InitV,Arc.FileHead.Lg2Count,
  426. Arc.FileHead.HashKey,PswCheck);
  427. // If header is damaged, we cannot rely on password check value,
  428. // because it can be damaged too.
  429. if (Arc.FileHead.Encrypted && Arc.FileHead.UsePswCheck &&
  430. memcmp(Arc.FileHead.PswCheck,PswCheck,SIZE_PSWCHECK)!=0 &&
  431. !Arc.BrokenHeader)
  432. {
  433. if (GlobalPassword) // For -p<pwd> or Ctrl+P to avoid the infinite loop.
  434. {
  435. // This message is used by Android GUI to reset cached passwords.
  436. // Update appropriate code if changed.
  437. uiMsg(UIERROR_BADPSW,Arc.FileName,ArcFileName);
  438. }
  439. else // For passwords entered manually.
  440. {
  441. // This message is used by Android GUI and Windows GUI and SFX to
  442. // reset cached passwords. Update appropriate code if changed.
  443. uiMsg(UIWAIT_BADPSW,Arc.FileName,ArcFileName);
  444. Cmd->Password.Clean();
  445. // Avoid new requests for unrar.dll to prevent the infinite loop
  446. // if app always returns the same password.
  447. #ifndef RARDLL
  448. continue; // Request a password again.
  449. #endif
  450. }
  451. #ifdef RARDLL
  452. // If we already have ERAR_EOPEN as result of missing volume,
  453. // we should not replace it with less precise ERAR_BAD_PASSWORD.
  454. if (Cmd->DllError!=ERAR_EOPEN)
  455. Cmd->DllError=ERAR_BAD_PASSWORD;
  456. #endif
  457. ErrHandler.SetErrorCode(RARX_BADPWD);
  458. ExtrFile=false;
  459. }
  460. break;
  461. }
  462. #ifdef RARDLL
  463. if (*Cmd->DllDestName!=0)
  464. wcsncpyz(DestFileName,Cmd->DllDestName,ASIZE(DestFileName));
  465. #endif
  466. File CurFile;
  467. bool LinkEntry=Arc.FileHead.RedirType!=FSREDIR_NONE;
  468. if (LinkEntry && Arc.FileHead.RedirType!=FSREDIR_FILECOPY)
  469. {
  470. if (ExtrFile && Command!='P' && !Cmd->Test)
  471. {
  472. // Overwrite prompt for symbolic and hard links.
  473. bool UserReject=false;
  474. if (FileExist(DestFileName) && !UserReject)
  475. FileCreate(Cmd,NULL,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime);
  476. if (UserReject)
  477. ExtrFile=false;
  478. }
  479. }
  480. else
  481. if (Arc.IsArcDir())
  482. {
  483. if (!ExtrFile || Command=='P' || Command=='I' || Command=='E' || Cmd->ExclPath==EXCL_SKIPWHOLEPATH)
  484. return true;
  485. TotalFileCount++;
  486. ExtrCreateDir(Arc,ArcFileName);
  487. // It is important to not increment MatchedArgs here, so we extract
  488. // dir with its entire contents and not dir record only even if
  489. // dir record precedes files.
  490. return true;
  491. }
  492. else
  493. if (ExtrFile) // Create files and file copies (FSREDIR_FILECOPY).
  494. ExtrFile=ExtrCreateFile(Arc,CurFile);
  495. if (!ExtrFile && Arc.Solid)
  496. {
  497. SkipSolid=true;
  498. ExtrFile=true;
  499. // We changed SkipSolid, so we need to call uiStartFileExtract
  500. // with "Skip" parameter to change the operation status
  501. // from "extracting" to "skipping". For example, it can be necessary
  502. // if user answered "No" to overwrite prompt when unpacking
  503. // a solid archive.
  504. if (!uiStartFileExtract(ArcFileName,false,false,true))
  505. return false;
  506. }
  507. if (ExtrFile)
  508. {
  509. // Set it in test mode, so we also test subheaders such as NTFS streams
  510. // after tested file.
  511. if (Cmd->Test)
  512. PrevProcessed=true;
  513. bool TestMode=Cmd->Test || SkipSolid; // Unpack to memory, not to disk.
  514. if (!SkipSolid)
  515. {
  516. if (!TestMode && Command!='P' && CurFile.IsDevice())
  517. {
  518. uiMsg(UIERROR_INVALIDNAME,Arc.FileName,DestFileName);
  519. ErrHandler.WriteError(Arc.FileName,DestFileName);
  520. }
  521. TotalFileCount++;
  522. }
  523. FileCount++;
  524. if (Command!='I' && !Cmd->DisableNames)
  525. if (SkipSolid)
  526. mprintf(St(MExtrSkipFile),ArcFileName);
  527. else
  528. switch(Cmd->Test ? 'T':Command) // "Test" can be also enabled by -t switch.
  529. {
  530. case 'T':
  531. mprintf(St(MExtrTestFile),ArcFileName);
  532. break;
  533. #ifndef SFX_MODULE
  534. case 'P':
  535. mprintf(St(MExtrPrinting),ArcFileName);
  536. break;
  537. #endif
  538. case 'X':
  539. case 'E':
  540. mprintf(St(MExtrFile),DestFileName);
  541. break;
  542. }
  543. if (!Cmd->DisablePercentage && !Cmd->DisableNames)
  544. mprintf(L" ");
  545. if (Cmd->DisableNames)
  546. uiEolAfterMsg(); // Avoid erasing preceding messages by percentage indicator in -idn mode.
  547. DataIO.CurUnpRead=0;
  548. DataIO.CurUnpWrite=0;
  549. DataIO.UnpHash.Init(Arc.FileHead.FileHash.Type,Cmd->Threads);
  550. DataIO.PackedDataHash.Init(Arc.FileHead.FileHash.Type,Cmd->Threads);
  551. DataIO.SetPackedSizeToRead(Arc.FileHead.PackSize);
  552. DataIO.SetFiles(&Arc,&CurFile);
  553. DataIO.SetTestMode(TestMode);
  554. DataIO.SetSkipUnpCRC(SkipSolid);
  555. #if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT)
  556. if (!TestMode && !Arc.BrokenHeader &&
  557. Arc.FileHead.UnpSize>0xffffffff && (Fat32 || !NotFat32))
  558. {
  559. if (!Fat32) // Not detected yet.
  560. NotFat32=!(Fat32=IsFAT(Cmd->ExtrPath));
  561. if (Fat32)
  562. uiMsg(UIMSG_FAT32SIZE); // Inform user about FAT32 size limit.
  563. }
  564. #endif
  565. uint64 Preallocated=0;
  566. if (!TestMode && !Arc.BrokenHeader && Arc.FileHead.UnpSize>1000000 &&
  567. Arc.FileHead.PackSize*1024>Arc.FileHead.UnpSize && Arc.IsSeekable() &&
  568. (Arc.FileHead.UnpSize<100000000 || Arc.FileLength()>Arc.FileHead.PackSize))
  569. {
  570. CurFile.Prealloc(Arc.FileHead.UnpSize);
  571. Preallocated=Arc.FileHead.UnpSize;
  572. }
  573. CurFile.SetAllowDelete(!Cmd->KeepBroken);
  574. bool FileCreateMode=!TestMode && !SkipSolid && Command!='P';
  575. bool ShowChecksum=true; // Display checksum verification result.
  576. bool LinkSuccess=true; // Assume success for test mode.
  577. if (LinkEntry)
  578. {
  579. /* // OPENMPT ADDITION
  580. FILE_SYSTEM_REDIRECT Type=Arc.FileHead.RedirType;
  581. if (Type==FSREDIR_HARDLINK || Type==FSREDIR_FILECOPY)
  582. {
  583. wchar RedirName[NM];
  584. ConvertPath(Arc.FileHead.RedirName,RedirName,ASIZE(RedirName));
  585. wchar NameExisting[NM];
  586. ExtrPrepareName(Arc,RedirName,NameExisting,ASIZE(NameExisting));
  587. if (FileCreateMode && *NameExisting!=0) // *NameExisting can be 0 in case of excessive -ap switch.
  588. if (Type==FSREDIR_HARDLINK)
  589. LinkSuccess=ExtractHardlink(Cmd,DestFileName,NameExisting,ASIZE(NameExisting));
  590. else
  591. LinkSuccess=ExtractFileCopy(CurFile,Arc.FileName,DestFileName,NameExisting,ASIZE(NameExisting));
  592. }
  593. else
  594. if (Type==FSREDIR_UNIXSYMLINK || Type==FSREDIR_WINSYMLINK || Type==FSREDIR_JUNCTION)
  595. {
  596. if (FileCreateMode)
  597. LinkSuccess=ExtractSymlink(Cmd,DataIO,Arc,DestFileName);
  598. }
  599. else
  600. {
  601. uiMsg(UIERROR_UNKNOWNEXTRA,Arc.FileName,DestFileName);
  602. LinkSuccess=false;
  603. }
  604. if (!LinkSuccess || Arc.Format==RARFMT15 && !FileCreateMode)
  605. {
  606. // RAR 5.x links have a valid data checksum even in case of
  607. // failure, because they do not store any data.
  608. // We do not want to display "OK" in this case.
  609. // For 4.x symlinks we verify the checksum only when extracting,
  610. // but not when testing an archive.
  611. ShowChecksum=false;
  612. }
  613. PrevProcessed=FileCreateMode && LinkSuccess;
  614. */ // OPENMPT ADDITION
  615. }
  616. else
  617. if (!Arc.FileHead.SplitBefore)
  618. if (Arc.FileHead.Method==0)
  619. UnstoreFile(DataIO,Arc.FileHead.UnpSize);
  620. else
  621. {
  622. Unp->Init(Arc.FileHead.WinSize,Arc.FileHead.Solid);
  623. Unp->SetDestSize(Arc.FileHead.UnpSize);
  624. #ifndef SFX_MODULE
  625. if (Arc.Format!=RARFMT50 && Arc.FileHead.UnpVer<=15)
  626. Unp->DoUnpack(15,FileCount>1 && Arc.Solid);
  627. else
  628. #endif
  629. Unp->DoUnpack(Arc.FileHead.UnpVer,Arc.FileHead.Solid);
  630. }
  631. Arc.SeekToNext();
  632. // We check for "split after" flag to detect partially extracted files
  633. // from incomplete volume sets. For them file header contains packed
  634. // data hash, which must not be compared against unpacked data hash
  635. // to prevent accidental match. Moreover, for -m0 volumes packed data
  636. // hash would match truncated unpacked data hash and lead to fake "OK"
  637. // in incomplete volume set.
  638. bool ValidCRC=!Arc.FileHead.SplitAfter && DataIO.UnpHash.Cmp(&Arc.FileHead.FileHash,Arc.FileHead.UseHashKey ? Arc.FileHead.HashKey:NULL);
  639. // We set AnySolidDataUnpackedWell to true if we found at least one
  640. // valid non-zero solid file in preceding solid stream. If it is true
  641. // and if current encrypted file is broken, we do not need to hint
  642. // about a wrong password and can report CRC error only.
  643. if (!Arc.FileHead.Solid)
  644. AnySolidDataUnpackedWell=false; // Reset the flag, because non-solid file is found.
  645. else
  646. if (Arc.FileHead.Method!=0 && Arc.FileHead.UnpSize>0 && ValidCRC)
  647. AnySolidDataUnpackedWell=true;
  648. bool BrokenFile=false;
  649. // Checksum is not calculated in skip solid mode for performance reason.
  650. if (!SkipSolid && ShowChecksum)
  651. {
  652. if (ValidCRC)
  653. {
  654. if (Command!='P' && Command!='I' && !Cmd->DisableNames)
  655. mprintf(L"%s%s ",Cmd->DisablePercentage ? L" ":L"\b\b\b\b\b ",
  656. Arc.FileHead.FileHash.Type==HASH_NONE ? L" ?":St(MOk));
  657. }
  658. else
  659. {
  660. if (Arc.FileHead.Encrypted && (!Arc.FileHead.UsePswCheck ||
  661. Arc.BrokenHeader) && !AnySolidDataUnpackedWell)
  662. uiMsg(UIERROR_CHECKSUMENC,Arc.FileName,ArcFileName);
  663. else
  664. uiMsg(UIERROR_CHECKSUM,Arc.FileName,ArcFileName);
  665. BrokenFile=true;
  666. ErrHandler.SetErrorCode(RARX_CRC);
  667. #ifdef RARDLL
  668. // If we already have ERAR_EOPEN as result of missing volume
  669. // or ERAR_BAD_PASSWORD for RAR5 wrong password,
  670. // we should not replace it with less precise ERAR_BAD_DATA.
  671. if (Cmd->DllError!=ERAR_EOPEN && Cmd->DllError!=ERAR_BAD_PASSWORD)
  672. Cmd->DllError=ERAR_BAD_DATA;
  673. #endif
  674. }
  675. }
  676. else
  677. {
  678. // We check SkipSolid to remove percent for skipped solid files only.
  679. // We must not apply these \b to links with ShowChecksum==false
  680. // and their possible error messages.
  681. if (SkipSolid)
  682. mprintf(L"\b\b\b\b\b ");
  683. }
  684. /* // OPENMPT ADDITION
  685. // If we successfully unpacked a hard link, we wish to set its file
  686. // attributes. Hard link shares file metadata with link target,
  687. // so we do not need to set link time or owner. But when we overwrite
  688. // an existing link, we can call PrepareToDelete(), which affects
  689. // link target attributes as well. So we set link attributes to restore
  690. // both target and link attributes if PrepareToDelete() changed them.
  691. bool SetAttrOnly=LinkEntry && Arc.FileHead.RedirType==FSREDIR_HARDLINK && LinkSuccess;
  692. if (!TestMode && (Command=='X' || Command=='E') &&
  693. (!LinkEntry || SetAttrOnly || Arc.FileHead.RedirType==FSREDIR_FILECOPY && LinkSuccess) &&
  694. (!BrokenFile || Cmd->KeepBroken))
  695. {
  696. // Below we use DestFileName instead of CurFile.FileName,
  697. // so we can set file attributes also for hard links, which do not
  698. // have the open CurFile. These strings are the same for other items.
  699. if (!SetAttrOnly)
  700. {
  701. // We could preallocate more space that really written to broken file
  702. // or file with crafted header.
  703. if (Preallocated>0 && (BrokenFile || DataIO.CurUnpWrite!=Preallocated))
  704. CurFile.Truncate();
  705. CurFile.SetOpenFileTime(
  706. Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime,
  707. Cmd->xctime==EXTTIME_NONE ? NULL:&Arc.FileHead.ctime,
  708. Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime);
  709. CurFile.Close();
  710. SetFileHeaderExtra(Cmd,Arc,DestFileName);
  711. CurFile.SetCloseFileTime(
  712. Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime,
  713. Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime);
  714. }
  715. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  716. if (Cmd->SetCompressedAttr &&
  717. (Arc.FileHead.FileAttr & FILE_ATTRIBUTE_COMPRESSED)!=0)
  718. SetFileCompression(DestFileName,true);
  719. if (Cmd->ClearArc)
  720. Arc.FileHead.FileAttr&=~FILE_ATTRIBUTE_ARCHIVE;
  721. #endif
  722. if (!Cmd->IgnoreGeneralAttr && !SetFileAttr(DestFileName,Arc.FileHead.FileAttr))
  723. {
  724. uiMsg(UIERROR_FILEATTR,Arc.FileName,DestFileName);
  725. // Android cannot set file attributes and while UIERROR_FILEATTR
  726. // above is handled by Android RAR silently, this call would cause
  727. // "Operation not permitted" message for every unpacked file.
  728. ErrHandler.SysErrMsg();
  729. }
  730. PrevProcessed=true;
  731. }
  732. */ // OPENMPT ADDITION
  733. }
  734. }
  735. // It is important to increment it for files, but not dirs. So we extract
  736. // dir with its entire contents, not just dir record only even if dir
  737. // record precedes files.
  738. if (MatchFound)
  739. MatchedArgs++;
  740. if (DataIO.NextVolumeMissing)
  741. return false;
  742. if (!ExtrFile)
  743. if (!Arc.Solid)
  744. Arc.SeekToNext();
  745. else
  746. if (!SkipSolid)
  747. return false;
  748. return true;
  749. }
  750. void CmdExtract::UnstoreFile(ComprDataIO &DataIO,int64 DestUnpSize)
  751. {
  752. Array<byte> Buffer(File::CopyBufferSize());
  753. while (true)
  754. {
  755. int ReadSize=DataIO.UnpRead(&Buffer[0],Buffer.Size());
  756. if (ReadSize<=0)
  757. break;
  758. int WriteSize=ReadSize<DestUnpSize ? ReadSize:(int)DestUnpSize;
  759. if (WriteSize>0)
  760. {
  761. DataIO.UnpWrite(&Buffer[0],WriteSize);
  762. DestUnpSize-=WriteSize;
  763. }
  764. }
  765. }
  766. bool CmdExtract::ExtractFileCopy(File &New,wchar *ArcName,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize)
  767. {
  768. SlashToNative(NameExisting,NameExisting,NameExistingSize); // Not needed for RAR 5.1+ archives.
  769. File Existing;
  770. if (!Existing.WOpen(NameExisting))
  771. {
  772. uiMsg(UIERROR_FILECOPY,ArcName,NameExisting,NameNew);
  773. uiMsg(UIERROR_FILECOPYHINT,ArcName);
  774. #ifdef RARDLL
  775. Cmd->DllError=ERAR_EREFERENCE;
  776. #endif
  777. return false;
  778. }
  779. Array<char> Buffer(0x100000);
  780. int64 CopySize=0;
  781. while (true)
  782. {
  783. Wait();
  784. int ReadSize=Existing.Read(&Buffer[0],Buffer.Size());
  785. if (ReadSize==0)
  786. break;
  787. New.Write(&Buffer[0],ReadSize);
  788. CopySize+=ReadSize;
  789. }
  790. return true;
  791. }
  792. void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *DestName,size_t DestSize)
  793. {
  794. *DestName = L'*'; // OPENMPT ADDITION
  795. return; // OPENMPT ADDITION
  796. wcsncpyz(DestName,Cmd->ExtrPath,DestSize);
  797. if (*Cmd->ExtrPath!=0)
  798. {
  799. wchar LastChar=*PointToLastChar(Cmd->ExtrPath);
  800. // We need IsPathDiv check here to correctly handle Unix forward slash
  801. // in the end of destination path in Windows: rar x arc dest/
  802. // so we call IsPathDiv first instead of just calling AddEndSlash,
  803. // which checks for only one type of path separator.
  804. // IsDriveDiv is needed for current drive dir: rar x arc d:
  805. if (!IsPathDiv(LastChar) && !IsDriveDiv(LastChar))
  806. {
  807. // Destination path can be without trailing slash if it come from GUI shell.
  808. AddEndSlash(DestName,DestSize);
  809. }
  810. }
  811. #ifndef SFX_MODULE
  812. if (Cmd->AppendArcNameToPath!=APPENDARCNAME_NONE)
  813. {
  814. switch(Cmd->AppendArcNameToPath)
  815. {
  816. case APPENDARCNAME_DESTPATH: // To subdir of destination path.
  817. wcsncatz(DestName,PointToName(Arc.FirstVolumeName),DestSize);
  818. SetExt(DestName,NULL,DestSize);
  819. break;
  820. case APPENDARCNAME_OWNSUBDIR: // To subdir of archive own dir.
  821. wcsncpyz(DestName,Arc.FirstVolumeName,DestSize);
  822. SetExt(DestName,NULL,DestSize);
  823. break;
  824. case APPENDARCNAME_OWNDIR: // To archive own dir.
  825. wcsncpyz(DestName,Arc.FirstVolumeName,DestSize);
  826. RemoveNameFromPath(DestName);
  827. break;
  828. }
  829. AddEndSlash(DestName,DestSize);
  830. }
  831. #endif
  832. #ifndef SFX_MODULE
  833. wchar *ArcPath=*Cmd->ExclArcPath!=0 ? Cmd->ExclArcPath:Cmd->ArcPath;
  834. size_t ArcPathLength=wcslen(ArcPath);
  835. if (ArcPathLength>0)
  836. {
  837. size_t NameLength=wcslen(ArcFileName);
  838. if (NameLength>=ArcPathLength && wcsnicompc(ArcPath,ArcFileName,ArcPathLength)==0 &&
  839. (IsPathDiv(ArcPath[ArcPathLength-1]) ||
  840. IsPathDiv(ArcFileName[ArcPathLength]) || ArcFileName[ArcPathLength]==0))
  841. {
  842. ArcFileName+=Min(ArcPathLength,NameLength);
  843. while (IsPathDiv(*ArcFileName))
  844. ArcFileName++;
  845. if (*ArcFileName==0) // Excessive -ap switch.
  846. {
  847. *DestName=0;
  848. return;
  849. }
  850. }
  851. }
  852. #endif
  853. wchar Command=Cmd->Command[0];
  854. // Use -ep3 only in systems, where disk letters are exist, not in Unix.
  855. bool AbsPaths=Cmd->ExclPath==EXCL_ABSPATH && Command=='X' && IsDriveDiv(':');
  856. // We do not use any user specified destination paths when extracting
  857. // absolute paths in -ep3 mode.
  858. if (AbsPaths)
  859. *DestName=0;
  860. if (Command=='E' || Cmd->ExclPath==EXCL_SKIPWHOLEPATH)
  861. wcsncatz(DestName,PointToName(ArcFileName),DestSize);
  862. else
  863. wcsncatz(DestName,ArcFileName,DestSize);
  864. #ifdef _WIN_ALL
  865. // Must do after Cmd->ArcPath processing above, so file name and arc path
  866. // trailing spaces are in sync.
  867. if (!Cmd->AllowIncompatNames)
  868. MakeNameCompatible(DestName,DestSize);
  869. #endif
  870. wchar DiskLetter=toupperw(DestName[0]);
  871. if (AbsPaths)
  872. {
  873. if (DestName[1]=='_' && IsPathDiv(DestName[2]) &&
  874. DiskLetter>='A' && DiskLetter<='Z')
  875. DestName[1]=':';
  876. else
  877. if (DestName[0]=='_' && DestName[1]=='_')
  878. {
  879. // Convert __server\share to \\server\share.
  880. DestName[0]=CPATHDIVIDER;
  881. DestName[1]=CPATHDIVIDER;
  882. }
  883. }
  884. }
  885. #ifdef RARDLL
  886. bool CmdExtract::ExtrDllGetPassword()
  887. {
  888. return false; // OPENMPT ADDITION
  889. if (!Cmd->Password.IsSet())
  890. {
  891. if (Cmd->Callback!=NULL)
  892. {
  893. wchar PasswordW[MAXPASSWORD];
  894. *PasswordW=0;
  895. if (Cmd->Callback(UCM_NEEDPASSWORDW,Cmd->UserData,(LPARAM)PasswordW,ASIZE(PasswordW))==-1)
  896. *PasswordW=0;
  897. if (*PasswordW==0)
  898. {
  899. char PasswordA[MAXPASSWORD];
  900. *PasswordA=0;
  901. if (Cmd->Callback(UCM_NEEDPASSWORD,Cmd->UserData,(LPARAM)PasswordA,ASIZE(PasswordA))==-1)
  902. *PasswordA=0;
  903. GetWideName(PasswordA,NULL,PasswordW,ASIZE(PasswordW));
  904. cleandata(PasswordA,sizeof(PasswordA));
  905. }
  906. Cmd->Password.Set(PasswordW);
  907. cleandata(PasswordW,sizeof(PasswordW));
  908. Cmd->ManualPassword=true;
  909. }
  910. if (!Cmd->Password.IsSet())
  911. return false;
  912. }
  913. return true;
  914. }
  915. #endif
  916. #ifndef RARDLL
  917. bool CmdExtract::ExtrGetPassword(Archive &Arc,const wchar *ArcFileName)
  918. {
  919. if (!Cmd->Password.IsSet())
  920. {
  921. if (!uiGetPassword(UIPASSWORD_FILE,ArcFileName,&Cmd->Password)/* || !Cmd->Password.IsSet()*/)
  922. {
  923. // Suppress "test is ok" message if user cancelled the password prompt.
  924. uiMsg(UIERROR_INCERRCOUNT);
  925. return false;
  926. }
  927. Cmd->ManualPassword=true;
  928. }
  929. #if !defined(SILENT)
  930. else
  931. if (!GlobalPassword && !Arc.FileHead.Solid)
  932. {
  933. eprintf(St(MUseCurPsw),ArcFileName);
  934. switch(Cmd->AllYes ? 1 : Ask(St(MYesNoAll)))
  935. {
  936. case -1:
  937. ErrHandler.Exit(RARX_USERBREAK);
  938. case 2:
  939. if (!uiGetPassword(UIPASSWORD_FILE,ArcFileName,&Cmd->Password))
  940. return false;
  941. break;
  942. case 3:
  943. GlobalPassword=true;
  944. break;
  945. }
  946. }
  947. #endif
  948. return true;
  949. }
  950. #endif
  951. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  952. void CmdExtract::ConvertDosPassword(Archive &Arc,SecPassword &DestPwd)
  953. {
  954. if (Arc.Format==RARFMT15 && Arc.FileHead.HostOS==HOST_MSDOS)
  955. {
  956. // We need the password in OEM encoding if file was encrypted by
  957. // native RAR/DOS (not extender based). Let's make the conversion.
  958. wchar PlainPsw[MAXPASSWORD];
  959. Cmd->Password.Get(PlainPsw,ASIZE(PlainPsw));
  960. char PswA[MAXPASSWORD];
  961. CharToOemBuffW(PlainPsw,PswA,ASIZE(PswA));
  962. PswA[ASIZE(PswA)-1]=0;
  963. CharToWide(PswA,PlainPsw,ASIZE(PlainPsw));
  964. DestPwd.Set(PlainPsw);
  965. cleandata(PlainPsw,sizeof(PlainPsw));
  966. cleandata(PswA,sizeof(PswA));
  967. }
  968. }
  969. #endif
  970. void CmdExtract::ExtrCreateDir(Archive &Arc,const wchar *ArcFileName)
  971. {
  972. return; // OPENMPT ADDITION
  973. if (Cmd->Test)
  974. {
  975. if (!Cmd->DisableNames)
  976. {
  977. mprintf(St(MExtrTestFile),ArcFileName);
  978. mprintf(L" %s",St(MOk));
  979. }
  980. return;
  981. }
  982. MKDIR_CODE MDCode=MakeDir(DestFileName,!Cmd->IgnoreGeneralAttr,Arc.FileHead.FileAttr);
  983. bool DirExist=false;
  984. if (MDCode!=MKDIR_SUCCESS)
  985. {
  986. DirExist=FileExist(DestFileName);
  987. if (DirExist && !IsDir(GetFileAttr(DestFileName)))
  988. {
  989. // File with name same as this directory exists. Propose user
  990. // to overwrite it.
  991. bool UserReject;
  992. FileCreate(Cmd,NULL,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime);
  993. DirExist=false;
  994. }
  995. if (!DirExist)
  996. {
  997. CreatePath(DestFileName,true,Cmd->DisableNames);
  998. MDCode=MakeDir(DestFileName,!Cmd->IgnoreGeneralAttr,Arc.FileHead.FileAttr);
  999. if (MDCode!=MKDIR_SUCCESS && !IsNameUsable(DestFileName))
  1000. {
  1001. uiMsg(UIMSG_CORRECTINGNAME,Arc.FileName);
  1002. wchar OrigName[ASIZE(DestFileName)];
  1003. wcsncpyz(OrigName,DestFileName,ASIZE(OrigName));
  1004. MakeNameUsable(DestFileName,true);
  1005. #ifndef SFX_MODULE
  1006. uiMsg(UIERROR_RENAMING,Arc.FileName,OrigName,DestFileName);
  1007. #endif
  1008. DirExist=FileExist(DestFileName) && IsDir(GetFileAttr(DestFileName));
  1009. if (!DirExist)
  1010. {
  1011. CreatePath(DestFileName,true,Cmd->DisableNames);
  1012. MDCode=MakeDir(DestFileName,!Cmd->IgnoreGeneralAttr,Arc.FileHead.FileAttr);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. if (MDCode==MKDIR_SUCCESS)
  1018. {
  1019. if (!Cmd->DisableNames)
  1020. {
  1021. mprintf(St(MCreatDir),DestFileName);
  1022. mprintf(L" %s",St(MOk));
  1023. }
  1024. PrevProcessed=true;
  1025. }
  1026. else
  1027. if (DirExist)
  1028. {
  1029. if (!Cmd->IgnoreGeneralAttr)
  1030. SetFileAttr(DestFileName,Arc.FileHead.FileAttr);
  1031. PrevProcessed=true;
  1032. }
  1033. else
  1034. {
  1035. uiMsg(UIERROR_DIRCREATE,Arc.FileName,DestFileName);
  1036. ErrHandler.SysErrMsg();
  1037. #ifdef RARDLL
  1038. Cmd->DllError=ERAR_ECREATE;
  1039. #endif
  1040. ErrHandler.SetErrorCode(RARX_CREATE);
  1041. }
  1042. if (PrevProcessed)
  1043. {
  1044. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  1045. if (Cmd->SetCompressedAttr &&
  1046. (Arc.FileHead.FileAttr & FILE_ATTRIBUTE_COMPRESSED)!=0 && WinNT()!=WNT_NONE)
  1047. SetFileCompression(DestFileName,true);
  1048. #endif
  1049. SetFileHeaderExtra(Cmd,Arc,DestFileName);
  1050. SetDirTime(DestFileName,
  1051. Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime,
  1052. Cmd->xctime==EXTTIME_NONE ? NULL:&Arc.FileHead.ctime,
  1053. Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime);
  1054. }
  1055. }
  1056. bool CmdExtract::ExtrCreateFile(Archive &Arc,File &CurFile)
  1057. {
  1058. return true; // OPENMPT ADDITION
  1059. bool Success=true;
  1060. wchar Command=Cmd->Command[0];
  1061. #if !defined(SFX_MODULE)
  1062. if (Command=='P')
  1063. CurFile.SetHandleType(FILE_HANDLESTD);
  1064. #endif
  1065. if ((Command=='E' || Command=='X') && !Cmd->Test)
  1066. {
  1067. bool UserReject;
  1068. // Specify "write only" mode to avoid OpenIndiana NAS problems
  1069. // with SetFileTime and read+write files.
  1070. if (!FileCreate(Cmd,&CurFile,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true))
  1071. {
  1072. Success=false;
  1073. if (!UserReject)
  1074. {
  1075. ErrHandler.CreateErrorMsg(Arc.FileName,DestFileName);
  1076. if (FileExist(DestFileName) && IsDir(GetFileAttr(DestFileName)))
  1077. uiMsg(UIERROR_DIRNAMEEXISTS);
  1078. #ifdef RARDLL
  1079. Cmd->DllError=ERAR_ECREATE;
  1080. #endif
  1081. if (!IsNameUsable(DestFileName))
  1082. {
  1083. uiMsg(UIMSG_CORRECTINGNAME,Arc.FileName);
  1084. wchar OrigName[ASIZE(DestFileName)];
  1085. wcsncpyz(OrigName,DestFileName,ASIZE(OrigName));
  1086. MakeNameUsable(DestFileName,true);
  1087. CreatePath(DestFileName,true,Cmd->DisableNames);
  1088. if (FileCreate(Cmd,&CurFile,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true))
  1089. {
  1090. #ifndef SFX_MODULE
  1091. uiMsg(UIERROR_RENAMING,Arc.FileName,OrigName,DestFileName);
  1092. #endif
  1093. Success=true;
  1094. }
  1095. else
  1096. ErrHandler.CreateErrorMsg(Arc.FileName,DestFileName);
  1097. }
  1098. }
  1099. }
  1100. }
  1101. return Success;
  1102. }
  1103. bool CmdExtract::CheckUnpVer(Archive &Arc,const wchar *ArcFileName)
  1104. {
  1105. bool WrongVer;
  1106. if (Arc.Format==RARFMT50) // Both SFX and RAR can unpack RAR 5.0 archives.
  1107. WrongVer=Arc.FileHead.UnpVer>VER_UNPACK5;
  1108. else
  1109. {
  1110. #ifdef SFX_MODULE // SFX can unpack only RAR 2.9 archives.
  1111. WrongVer=Arc.FileHead.UnpVer!=VER_UNPACK;
  1112. #else // All formats since 1.3 for RAR.
  1113. WrongVer=Arc.FileHead.UnpVer<13 || Arc.FileHead.UnpVer>VER_UNPACK;
  1114. #endif
  1115. }
  1116. // We can unpack stored files regardless of compression version field.
  1117. if (Arc.FileHead.Method==0)
  1118. WrongVer=false;
  1119. if (WrongVer)
  1120. {
  1121. ErrHandler.UnknownMethodMsg(Arc.FileName,ArcFileName);
  1122. uiMsg(UIERROR_NEWERRAR,Arc.FileName);
  1123. }
  1124. return !WrongVer;
  1125. }
  1126. #ifndef SFX_MODULE
  1127. // To speed up solid volumes extraction, try to find a non-first start volume,
  1128. // which still allows to unpack all files. It is possible for independent
  1129. // solid volumes with solid statistics reset in the beginning.
  1130. bool CmdExtract::DetectStartVolume(const wchar *VolName,bool NewNumbering)
  1131. {
  1132. wchar *ArgName=Cmd->FileArgs.GetString();
  1133. Cmd->FileArgs.Rewind();
  1134. if (ArgName!=NULL && (wcscmp(ArgName,L"*")==0 || wcscmp(ArgName,L"*.*")==0))
  1135. return false; // No need to check further for * and *.* masks.
  1136. wchar StartName[NM];
  1137. *StartName=0;
  1138. // Start search from first volume if all volumes preceding current are available.
  1139. wchar NextName[NM];
  1140. GetFirstVolIfFullSet(VolName,NewNumbering,NextName,ASIZE(NextName));
  1141. bool Matched=false;
  1142. while (!Matched)
  1143. {
  1144. Archive Arc(Cmd);
  1145. if (!Arc.Open(NextName) || !Arc.IsArchive(false) || !Arc.Volume)
  1146. break;
  1147. bool OpenNext=false;
  1148. while (Arc.ReadHeader()>0)
  1149. {
  1150. Wait();
  1151. HEADER_TYPE HeaderType=Arc.GetHeaderType();
  1152. if (HeaderType==HEAD_ENDARC)
  1153. {
  1154. OpenNext|=Arc.EndArcHead.NextVolume; // Allow open next volume.
  1155. break;
  1156. }
  1157. if (HeaderType==HEAD_FILE)
  1158. {
  1159. if (!Arc.FileHead.SplitBefore)
  1160. {
  1161. if (!Arc.FileHead.Solid) // Can start extraction from here.
  1162. wcsncpyz(StartName,NextName,ASIZE(StartName));
  1163. if (Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL,0)!=0)
  1164. {
  1165. Matched=true; // First matched file found, must stop further scan.
  1166. break;
  1167. }
  1168. }
  1169. if (Arc.FileHead.SplitAfter)
  1170. {
  1171. OpenNext=true; // Allow open next volume.
  1172. break;
  1173. }
  1174. }
  1175. Arc.SeekToNext();
  1176. }
  1177. Arc.Close();
  1178. if (!OpenNext)
  1179. break;
  1180. NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering);
  1181. }
  1182. bool NewStartFound=wcscmp(VolName,StartName)!=0;
  1183. if (NewStartFound) // Found a new volume to start extraction.
  1184. wcsncpyz(ArcName,StartName,ASIZE(ArcName));
  1185. return NewStartFound;
  1186. }
  1187. #endif
  1188. #ifndef SFX_MODULE
  1189. // Return the first volume name if all volumes preceding the specified
  1190. // are available. Otherwise return the specified volume name.
  1191. void CmdExtract::GetFirstVolIfFullSet(const wchar *SrcName,bool NewNumbering,wchar *DestName,size_t DestSize)
  1192. {
  1193. wchar FirstVolName[NM];
  1194. VolNameToFirstName(SrcName,FirstVolName,ASIZE(FirstVolName),NewNumbering);
  1195. wchar NextName[NM];
  1196. wcsncpyz(NextName,FirstVolName,ASIZE(NextName));
  1197. wchar ResultName[NM];
  1198. wcsncpyz(ResultName,SrcName,ASIZE(ResultName));
  1199. while (true)
  1200. {
  1201. if (wcscmp(SrcName,NextName)==0)
  1202. {
  1203. wcsncpyz(ResultName,FirstVolName,DestSize);
  1204. break;
  1205. }
  1206. if (!FileExist(NextName))
  1207. break;
  1208. NextVolumeName(NextName,ASIZE(NextName),!NewNumbering);
  1209. }
  1210. wcsncpyz(DestName,ResultName,DestSize);
  1211. }
  1212. #endif