list.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "rar.hpp"
  2. static void ListFileHeader(FileHeader &hd,bool Verbose,bool Technical,bool &TitleShown,bool Bare);
  3. static void ListSymLink(Archive &Arc);
  4. static void ListFileAttr(uint A,int HostOS);
  5. static void ListOldSubHeader(Archive &Arc);
  6. static void ListNewSubHeader(CommandData *Cmd,Archive &Arc,bool Technical);
  7. void ListArchive(CommandData *Cmd)
  8. {
  9. int64 SumPackSize=0,SumUnpSize=0;
  10. uint ArcCount=0,SumFileCount=0;
  11. bool Technical=(Cmd->Command[1]=='T');
  12. bool Bare=(Cmd->Command[1]=='B');
  13. bool Verbose=(*Cmd->Command=='V');
  14. char ArcName[NM];
  15. wchar ArcNameW[NM];
  16. while (Cmd->GetArcName(ArcName,ArcNameW,sizeof(ArcName)))
  17. {
  18. Archive Arc(Cmd);
  19. #ifdef _WIN_ALL
  20. Arc.RemoveSequentialFlag();
  21. #endif
  22. if (!Arc.WOpen(ArcName,ArcNameW))
  23. continue;
  24. bool FileMatched=true;
  25. while (1)
  26. {
  27. int64 TotalPackSize=0,TotalUnpSize=0;
  28. uint FileCount=0;
  29. if (Arc.IsArchive(true))
  30. {
  31. // if (!Arc.IsOpened())
  32. // break;
  33. bool TitleShown=false;
  34. if (!Bare)
  35. {
  36. Arc.ViewComment();
  37. mprintf("\n");
  38. if (Arc.Solid)
  39. mprintf(St(MListSolid));
  40. if (Arc.SFXSize>0)
  41. mprintf(St(MListSFX));
  42. if (Arc.Volume)
  43. if (Arc.Solid)
  44. mprintf(St(MListVol1));
  45. else
  46. mprintf(St(MListVol2));
  47. else
  48. if (Arc.Solid)
  49. mprintf(St(MListArc1));
  50. else
  51. mprintf(St(MListArc2));
  52. mprintf(" %s\n",Arc.FileName);
  53. if (Technical)
  54. {
  55. if (Arc.Protected)
  56. mprintf(St(MListRecRec));
  57. if (Arc.Locked)
  58. mprintf(St(MListLock));
  59. }
  60. }
  61. while(Arc.ReadHeader()>0)
  62. {
  63. int HeaderType=Arc.GetHeaderType();
  64. if (HeaderType==ENDARC_HEAD)
  65. break;
  66. switch(HeaderType)
  67. {
  68. case FILE_HEAD:
  69. IntToExt(Arc.NewLhd.FileName,Arc.NewLhd.FileName);
  70. FileMatched=Cmd->IsProcessFile(Arc.NewLhd)!=0;
  71. if (FileMatched)
  72. {
  73. ListFileHeader(Arc.NewLhd,Verbose,Technical,TitleShown,Bare);
  74. if (!(Arc.NewLhd.Flags & LHD_SPLIT_BEFORE))
  75. {
  76. TotalUnpSize+=Arc.NewLhd.FullUnpSize;
  77. FileCount++;
  78. }
  79. TotalPackSize+=Arc.NewLhd.FullPackSize;
  80. if (Technical)
  81. ListSymLink(Arc);
  82. #ifndef SFX_MODULE
  83. if (Verbose)
  84. Arc.ViewFileComment();
  85. #endif
  86. }
  87. break;
  88. #ifndef SFX_MODULE
  89. case SUB_HEAD:
  90. if (Technical && FileMatched && !Bare)
  91. ListOldSubHeader(Arc);
  92. break;
  93. #endif
  94. case NEWSUB_HEAD:
  95. if (FileMatched && !Bare)
  96. {
  97. if (Technical)
  98. ListFileHeader(Arc.SubHead,Verbose,true,TitleShown,false);
  99. ListNewSubHeader(Cmd,Arc,Technical);
  100. }
  101. break;
  102. }
  103. Arc.SeekToNext();
  104. }
  105. if (!Bare)
  106. if (TitleShown)
  107. {
  108. mprintf("\n");
  109. for (int I=0;I<79;I++)
  110. mprintf("-");
  111. char UnpSizeText[20];
  112. itoa(TotalUnpSize,UnpSizeText);
  113. char PackSizeText[20];
  114. itoa(TotalPackSize,PackSizeText);
  115. mprintf("\n%5lu %16s %8s %3d%%",FileCount,UnpSizeText,
  116. PackSizeText,ToPercentUnlim(TotalPackSize,TotalUnpSize));
  117. SumFileCount+=FileCount;
  118. SumUnpSize+=TotalUnpSize;
  119. SumPackSize+=TotalPackSize;
  120. #ifndef SFX_MODULE
  121. if (Arc.EndArcHead.Flags & EARC_VOLNUMBER)
  122. {
  123. mprintf(" ");
  124. mprintf(St(MVolumeNumber),Arc.EndArcHead.VolNumber+1);
  125. }
  126. #endif
  127. mprintf("\n");
  128. }
  129. else
  130. mprintf(St(MListNoFiles));
  131. ArcCount++;
  132. #ifndef NOVOLUME
  133. if (Cmd->VolSize!=0 && ((Arc.NewLhd.Flags & LHD_SPLIT_AFTER) ||
  134. Arc.GetHeaderType()==ENDARC_HEAD &&
  135. (Arc.EndArcHead.Flags & EARC_NEXT_VOLUME)!=0) &&
  136. MergeArchive(Arc,NULL,false,*Cmd->Command))
  137. {
  138. Arc.Seek(0,SEEK_SET);
  139. }
  140. else
  141. #endif
  142. break;
  143. }
  144. else
  145. {
  146. if (Cmd->ArcNames->ItemsCount()<2 && !Bare)
  147. mprintf(St(MNotRAR),Arc.FileName);
  148. break;
  149. }
  150. }
  151. }
  152. if (ArcCount>1 && !Bare)
  153. {
  154. char UnpSizeText[20],PackSizeText[20];
  155. itoa(SumUnpSize,UnpSizeText);
  156. itoa(SumPackSize,PackSizeText);
  157. mprintf("\n%5lu %16s %8s %3d%%\n",SumFileCount,UnpSizeText,
  158. PackSizeText,ToPercentUnlim(SumPackSize,SumUnpSize));
  159. }
  160. }
  161. void ListFileHeader(FileHeader &hd,bool Verbose,bool Technical,bool &TitleShown,bool Bare)
  162. {
  163. if (!Bare)
  164. {
  165. if (!TitleShown)
  166. {
  167. if (Verbose)
  168. mprintf(St(MListPathComm));
  169. else
  170. mprintf(St(MListName));
  171. mprintf(St(MListTitle));
  172. if (Technical)
  173. mprintf(St(MListTechTitle));
  174. for (int I=0;I<79;I++)
  175. mprintf("-");
  176. TitleShown=true;
  177. }
  178. if (hd.HeadType==NEWSUB_HEAD)
  179. mprintf(St(MSubHeadType),hd.FileName);
  180. mprintf("\n%c",(hd.Flags & LHD_PASSWORD) ? '*' : ' ');
  181. }
  182. char *Name=hd.FileName;
  183. #ifdef UNICODE_SUPPORTED
  184. char ConvertedName[NM];
  185. if ((hd.Flags & LHD_UNICODE)!=0 && *hd.FileNameW!=0 && UnicodeEnabled())
  186. {
  187. if (WideToChar(hd.FileNameW,ConvertedName) && *ConvertedName!=0)
  188. Name=ConvertedName;
  189. }
  190. #endif
  191. if (Bare)
  192. {
  193. mprintf("%s\n",Verbose ? Name:PointToName(Name));
  194. return;
  195. }
  196. if (Verbose)
  197. mprintf("%s\n%12s ",Name,"");
  198. else
  199. mprintf("%-12s",PointToName(Name));
  200. char UnpSizeText[20],PackSizeText[20];
  201. if (hd.FullUnpSize==INT64NDF)
  202. strcpy(UnpSizeText,"?");
  203. else
  204. itoa(hd.FullUnpSize,UnpSizeText);
  205. itoa(hd.FullPackSize,PackSizeText);
  206. mprintf(" %8s %8s ",UnpSizeText,PackSizeText);
  207. if ((hd.Flags & LHD_SPLIT_BEFORE) && (hd.Flags & LHD_SPLIT_AFTER))
  208. mprintf(" <->");
  209. else
  210. if (hd.Flags & LHD_SPLIT_BEFORE)
  211. mprintf(" <--");
  212. else
  213. if (hd.Flags & LHD_SPLIT_AFTER)
  214. mprintf(" -->");
  215. else
  216. mprintf("%3d%%",ToPercentUnlim(hd.FullPackSize,hd.FullUnpSize));
  217. char DateStr[50];
  218. hd.mtime.GetText(DateStr,false);
  219. mprintf(" %s ",DateStr);
  220. if (hd.HeadType==NEWSUB_HEAD)
  221. mprintf(" %c....B ",(hd.SubFlags & SUBHEAD_FLAGS_INHERITED) ? 'I' : '.');
  222. else
  223. ListFileAttr(hd.FileAttr,hd.HostOS);
  224. mprintf(" %8.8X",hd.FileCRC);
  225. mprintf(" m%d",hd.Method-0x30);
  226. if ((hd.Flags & LHD_WINDOWMASK)<=6*32)
  227. mprintf("%c",((hd.Flags&LHD_WINDOWMASK)>>5)+'a');
  228. else
  229. mprintf(" ");
  230. mprintf(" %d.%d",hd.UnpVer/10,hd.UnpVer%10);
  231. static const char *RarOS[]={
  232. "DOS","OS/2","Windows","Unix","Mac OS","BeOS","WinCE","","",""
  233. };
  234. if (Technical)
  235. mprintf("\n%22s %8s %4s",
  236. (hd.HostOS<ASIZE(RarOS) ? RarOS[hd.HostOS]:""),
  237. (hd.Flags & LHD_SOLID) ? St(MYes):St(MNo),
  238. (hd.Flags & LHD_VERSION) ? St(MYes):St(MNo));
  239. }
  240. void ListSymLink(Archive &Arc)
  241. {
  242. if (Arc.NewLhd.HostOS==HOST_UNIX && (Arc.NewLhd.FileAttr & 0xF000)==0xA000)
  243. if ((Arc.NewLhd.Flags & LHD_PASSWORD)==0)
  244. {
  245. char FileName[NM];
  246. int DataSize=Min(Arc.NewLhd.PackSize,sizeof(FileName)-1);
  247. Arc.Read(FileName,DataSize);
  248. FileName[DataSize]=0;
  249. mprintf("\n%22s %s","-->",FileName);
  250. }
  251. else
  252. {
  253. // Link data are encrypted. We would need to ask for password
  254. // and initialize decryption routine to display the link target.
  255. mprintf("\n%22s %s","-->","*<-?->");
  256. }
  257. }
  258. void ListFileAttr(uint A,int HostOS)
  259. {
  260. switch(HostOS)
  261. {
  262. case HOST_MSDOS:
  263. case HOST_OS2:
  264. case HOST_WIN32:
  265. case HOST_MACOS:
  266. mprintf(" %c%c%c%c%c%c%c ",
  267. (A & 0x08) ? 'V' : '.',
  268. (A & 0x10) ? 'D' : '.',
  269. (A & 0x01) ? 'R' : '.',
  270. (A & 0x02) ? 'H' : '.',
  271. (A & 0x04) ? 'S' : '.',
  272. (A & 0x20) ? 'A' : '.',
  273. (A & 0x800) ? 'C' : '.');
  274. break;
  275. case HOST_UNIX:
  276. case HOST_BEOS:
  277. switch (A & 0xF000)
  278. {
  279. case 0x4000:
  280. mprintf("d");
  281. break;
  282. case 0xA000:
  283. mprintf("l");
  284. break;
  285. default:
  286. mprintf("-");
  287. break;
  288. }
  289. mprintf("%c%c%c%c%c%c%c%c%c",
  290. (A & 0x0100) ? 'r' : '-',
  291. (A & 0x0080) ? 'w' : '-',
  292. (A & 0x0040) ? ((A & 0x0800) ? 's':'x'):((A & 0x0800) ? 'S':'-'),
  293. (A & 0x0020) ? 'r' : '-',
  294. (A & 0x0010) ? 'w' : '-',
  295. (A & 0x0008) ? ((A & 0x0400) ? 's':'x'):((A & 0x0400) ? 'S':'-'),
  296. (A & 0x0004) ? 'r' : '-',
  297. (A & 0x0002) ? 'w' : '-',
  298. (A & 0x0001) ? 'x' : '-');
  299. break;
  300. }
  301. }
  302. #ifndef SFX_MODULE
  303. void ListOldSubHeader(Archive &Arc)
  304. {
  305. switch(Arc.SubBlockHead.SubType)
  306. {
  307. case EA_HEAD:
  308. mprintf(St(MListEAHead));
  309. break;
  310. case UO_HEAD:
  311. mprintf(St(MListUOHead),Arc.UOHead.OwnerName,Arc.UOHead.GroupName);
  312. break;
  313. case MAC_HEAD:
  314. mprintf(St(MListMACHead1),Arc.MACHead.fileType>>24,Arc.MACHead.fileType>>16,Arc.MACHead.fileType>>8,Arc.MACHead.fileType);
  315. mprintf(St(MListMACHead2),Arc.MACHead.fileCreator>>24,Arc.MACHead.fileCreator>>16,Arc.MACHead.fileCreator>>8,Arc.MACHead.fileCreator);
  316. break;
  317. case BEEA_HEAD:
  318. mprintf(St(MListBeEAHead));
  319. break;
  320. case NTACL_HEAD:
  321. mprintf(St(MListNTACLHead));
  322. break;
  323. case STREAM_HEAD:
  324. mprintf(St(MListStrmHead),Arc.StreamHead.StreamName);
  325. break;
  326. default:
  327. mprintf(St(MListUnkHead),Arc.SubBlockHead.SubType);
  328. break;
  329. }
  330. }
  331. #endif
  332. void ListNewSubHeader(CommandData *Cmd,Archive &Arc,bool Technical)
  333. {
  334. if (Arc.SubHead.CmpName(SUBHEAD_TYPE_CMT) &&
  335. (Arc.SubHead.Flags & LHD_SPLIT_BEFORE)==0 && !Cmd->DisableComment)
  336. {
  337. Array<byte> CmtData;
  338. size_t ReadSize=Arc.ReadCommentData(&CmtData,NULL);
  339. if (ReadSize!=0)
  340. {
  341. mprintf(St(MFileComment));
  342. OutComment((char *)&CmtData[0],ReadSize);
  343. }
  344. }
  345. if (Arc.SubHead.CmpName(SUBHEAD_TYPE_STREAM) &&
  346. (Arc.SubHead.Flags & LHD_SPLIT_BEFORE)==0)
  347. {
  348. size_t DestSize=Arc.SubHead.SubData.Size()/2;
  349. wchar DestNameW[NM];
  350. char DestName[NM];
  351. if (DestSize<sizeof(DestName))
  352. {
  353. RawToWide(&Arc.SubHead.SubData[0],DestNameW,DestSize);
  354. DestNameW[DestSize]=0;
  355. WideToChar(DestNameW,DestName);
  356. mprintf("\n %s",DestName);
  357. }
  358. }
  359. }