1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
|
// List.cpp
#include "StdAfx.h"
#include "Common/IntToString.h"
#include "Common/MyCom.h"
#include "Common/StdOutStream.h"
#include "Common/StringConvert.h"
#include "Windows/Error.h"
#include "Windows/FileDir.h"
#include "Windows/PropVariant.h"
#include "Windows/PropVariantConversions.h"
#include "../../Archive/IArchive.h"
#include "../Common/OpenArchive.h"
#include "../Common/PropIDUtils.h"
#include "ConsoleClose.h"
#include "List.h"
#include "OpenCallbackConsole.h"
using namespace NWindows;
struct CPropIdToName
{
PROPID PropID;
const wchar_t *Name;
};
static const CPropIdToName kPropIdToName[] =
{
{ kpidPath, L"Path" },
{ kpidName, L"Name" },
{ kpidIsDir, L"Folder" },
{ kpidSize, L"Size" },
{ kpidPackSize, L"Packed Size" },
{ kpidAttrib, L"Attributes" },
{ kpidCTime, L"Created" },
{ kpidATime, L"Accessed" },
{ kpidMTime, L"Modified" },
{ kpidSolid, L"Solid" },
{ kpidCommented, L"Commented" },
{ kpidEncrypted, L"Encrypted" },
{ kpidSplitBefore, L"Split Before" },
{ kpidSplitAfter, L"Split After" },
{ kpidDictionarySize, L"Dictionary Size" },
{ kpidCRC, L"CRC" },
{ kpidType, L"Type" },
{ kpidIsAnti, L"Anti" },
{ kpidMethod, L"Method" },
{ kpidHostOS, L"Host OS" },
{ kpidFileSystem, L"File System" },
{ kpidUser, L"User" },
{ kpidGroup, L"Group" },
{ kpidBlock, L"Block" },
{ kpidComment, L"Comment" },
{ kpidPosition, L"Position" },
{ kpidPrefix, L"Prefix" },
{ kpidNumSubDirs, L"Folders" },
{ kpidNumSubFiles, L"Files" },
{ kpidUnpackVer, L"Version" },
{ kpidVolume, L"Volume" },
{ kpidIsVolume, L"Multivolume" },
{ kpidOffset, L"Offset" },
{ kpidLinks, L"Links" },
{ kpidNumBlocks, L"Blocks" },
{ kpidNumVolumes, L"Volumes" },
{ kpidBit64, L"64-bit" },
{ kpidBigEndian, L"Big-endian" },
{ kpidCpu, L"CPU" },
{ kpidPhySize, L"Physical Size" },
{ kpidHeadersSize, L"Headers Size" },
{ kpidChecksum, L"Checksum" },
{ kpidCharacts, L"Characteristics" },
{ kpidVa, L"Virtual Address" },
{ kpidId, L"ID" },
{ kpidShortName, L"Short Name" },
{ kpidCreatorApp, L"Creator Application"},
{ kpidSectorSize, L"Sector Size" },
{ kpidPosixAttrib, L"Mode" },
{ kpidLink, L"Link" },
{ kpidError, L"Error" },
{ kpidTotalSize, L"Total Size" },
{ kpidFreeSpace, L"Free Space" },
{ kpidClusterSize, L"Cluster Size" },
{ kpidVolumeName, L"Label" }
};
static const char kEmptyAttribChar = '.';
static const char *kListing = "Listing archive: ";
static const wchar_t *kFilesMessage = L"files";
static const wchar_t *kDirsMessage = L"folders";
static void GetAttribString(DWORD wa, bool isDir, char *s)
{
s[0] = ((wa & FILE_ATTRIBUTE_DIRECTORY) != 0 || isDir) ? 'D' : kEmptyAttribChar;
s[1] = ((wa & FILE_ATTRIBUTE_READONLY) != 0) ? 'R': kEmptyAttribChar;
s[2] = ((wa & FILE_ATTRIBUTE_HIDDEN) != 0) ? 'H': kEmptyAttribChar;
s[3] = ((wa & FILE_ATTRIBUTE_SYSTEM) != 0) ? 'S': kEmptyAttribChar;
s[4] = ((wa & FILE_ATTRIBUTE_ARCHIVE) != 0) ? 'A': kEmptyAttribChar;
s[5] = '\0';
}
enum EAdjustment
{
kLeft,
kCenter,
kRight
};
struct CFieldInfo
{
PROPID PropID;
UString Name;
EAdjustment TitleAdjustment;
EAdjustment TextAdjustment;
int PrefixSpacesWidth;
int Width;
};
struct CFieldInfoInit
{
PROPID PropID;
const wchar_t *Name;
EAdjustment TitleAdjustment;
EAdjustment TextAdjustment;
int PrefixSpacesWidth;
int Width;
};
static CFieldInfoInit kStandardFieldTable[] =
{
{ kpidMTime, L" Date Time", kLeft, kLeft, 0, 19 },
{ kpidAttrib, L"Attr", kRight, kCenter, 1, 5 },
{ kpidSize, L"Size", kRight, kRight, 1, 12 },
{ kpidPackSize, L"Compressed", kRight, kRight, 1, 12 },
{ kpidPath, L"Name", kLeft, kLeft, 2, 24 }
};
static void PrintSpaces(int numSpaces)
{
for (int i = 0; i < numSpaces; i++)
g_StdOut << ' ';
}
static void PrintString(EAdjustment adjustment, int width, const UString &textString)
{
const int numSpaces = width - textString.Length();
int numLeftSpaces = 0;
switch (adjustment)
{
case kLeft:
numLeftSpaces = 0;
break;
case kCenter:
numLeftSpaces = numSpaces / 2;
break;
case kRight:
numLeftSpaces = numSpaces;
break;
}
PrintSpaces(numLeftSpaces);
g_StdOut << textString;
PrintSpaces(numSpaces - numLeftSpaces);
}
class CFieldPrinter
{
CObjectVector<CFieldInfo> _fields;
public:
void Clear() { _fields.Clear(); }
void Init(const CFieldInfoInit *standardFieldTable, int numItems);
HRESULT Init(IInArchive *archive);
void PrintTitle();
void PrintTitleLines();
HRESULT PrintItemInfo(const CArc &arc, UInt32 index, bool techMode);
HRESULT PrintSummaryInfo(UInt64 numFiles, UInt64 numDirs,
const UInt64 *size, const UInt64 *compressedSize);
};
void CFieldPrinter::Init(const CFieldInfoInit *standardFieldTable, int numItems)
{
Clear();
for (int i = 0; i < numItems; i++)
{
CFieldInfo fieldInfo;
const CFieldInfoInit &fieldInfoInit = standardFieldTable[i];
fieldInfo.PropID = fieldInfoInit.PropID;
fieldInfo.Name = fieldInfoInit.Name;
fieldInfo.TitleAdjustment = fieldInfoInit.TitleAdjustment;
fieldInfo.TextAdjustment = fieldInfoInit.TextAdjustment;
fieldInfo.PrefixSpacesWidth = fieldInfoInit.PrefixSpacesWidth;
fieldInfo.Width = fieldInfoInit.Width;
_fields.Add(fieldInfo);
}
}
static UString GetPropName(PROPID propID, BSTR name)
{
for (int i = 0; i < sizeof(kPropIdToName) / sizeof(kPropIdToName[0]); i++)
{
const CPropIdToName &propIdToName = kPropIdToName[i];
if (propIdToName.PropID == propID)
return propIdToName.Name;
}
if (name)
return name;
wchar_t s[16];
ConvertUInt32ToString(propID, s);
return s;
}
HRESULT CFieldPrinter::Init(IInArchive *archive)
{
Clear();
UInt32 numProps;
RINOK(archive->GetNumberOfProperties(&numProps));
for (UInt32 i = 0; i < numProps; i++)
{
CMyComBSTR name;
PROPID propID;
VARTYPE vt;
RINOK(archive->GetPropertyInfo(i, &name, &propID, &vt));
CFieldInfo fieldInfo;
fieldInfo.PropID = propID;
fieldInfo.Name = GetPropName(propID, name);
_fields.Add(fieldInfo);
}
return S_OK;
}
void CFieldPrinter::PrintTitle()
{
for (int i = 0; i < _fields.Size(); i++)
{
const CFieldInfo &fieldInfo = _fields[i];
PrintSpaces(fieldInfo.PrefixSpacesWidth);
PrintString(fieldInfo.TitleAdjustment,
((fieldInfo.PropID == kpidPath) ? 0: fieldInfo.Width), fieldInfo.Name);
}
}
void CFieldPrinter::PrintTitleLines()
{
for (int i = 0; i < _fields.Size(); i++)
{
const CFieldInfo &fieldInfo = _fields[i];
PrintSpaces(fieldInfo.PrefixSpacesWidth);
for (int i = 0; i < fieldInfo.Width; i++)
g_StdOut << '-';
}
}
static BOOL IsFileTimeZero(CONST FILETIME *lpFileTime)
{
return (lpFileTime->dwLowDateTime == 0) && (lpFileTime->dwHighDateTime == 0);
}
static const char *kEmptyTimeString = " ";
static void PrintTime(const NCOM::CPropVariant &prop)
{
if (prop.vt != VT_FILETIME)
throw "incorrect item";
if (IsFileTimeZero(&prop.filetime))
g_StdOut << kEmptyTimeString;
else
{
FILETIME localFileTime;
if (!FileTimeToLocalFileTime(&prop.filetime, &localFileTime))
throw "FileTimeToLocalFileTime error";
char s[32];
if (ConvertFileTimeToString(localFileTime, s, true, true))
g_StdOut << s;
else
g_StdOut << kEmptyTimeString;
}
}
HRESULT CFieldPrinter::PrintItemInfo(const CArc &arc, UInt32 index, bool techMode)
{
/*
if (techMode)
{
g_StdOut << "Index = ";
g_StdOut << (UInt64)index;
g_StdOut << endl;
}
*/
for (int i = 0; i < _fields.Size(); i++)
{
const CFieldInfo &fieldInfo = _fields[i];
if (!techMode)
PrintSpaces(fieldInfo.PrefixSpacesWidth);
NCOM::CPropVariant prop;
if (fieldInfo.PropID == kpidPath)
{
UString s;
RINOK(arc.GetItemPath(index, s));
prop = s;
}
else
{
RINOK(arc.Archive->GetProperty(index, fieldInfo.PropID, &prop));
}
if (techMode)
{
g_StdOut << fieldInfo.Name << " = ";
}
int width = (fieldInfo.PropID == kpidPath) ? 0: fieldInfo.Width;
if (fieldInfo.PropID == kpidAttrib && (prop.vt == VT_EMPTY || prop.vt == VT_UI4))
{
UInt32 attrib = (prop.vt == VT_EMPTY) ? 0 : prop.ulVal;
bool isFolder;
RINOK(IsArchiveItemFolder(arc.Archive, index, isFolder));
char s[8];
GetAttribString(attrib, isFolder, s);
g_StdOut << s;
}
else if (prop.vt == VT_EMPTY)
{
if (!techMode)
PrintSpaces(width);
}
else if (fieldInfo.PropID == kpidMTime)
{
PrintTime(prop);
}
else if (prop.vt == VT_BSTR)
{
if (techMode)
g_StdOut << prop.bstrVal;
else
PrintString(fieldInfo.TextAdjustment, width, prop.bstrVal);
}
else
{
UString s = ConvertPropertyToString(prop, fieldInfo.PropID);
s.Replace(wchar_t(0xA), L' ');
s.Replace(wchar_t(0xD), L' ');
if (techMode)
g_StdOut << s;
else
PrintString(fieldInfo.TextAdjustment, width, s);
}
if (techMode)
g_StdOut << endl;
}
return S_OK;
}
static void PrintNumberString(EAdjustment adjustment, int width, const UInt64 *value)
{
wchar_t textString[32] = { 0 };
if (value != NULL)
ConvertUInt64ToString(*value, textString);
PrintString(adjustment, width, textString);
}
HRESULT CFieldPrinter::PrintSummaryInfo(UInt64 numFiles, UInt64 numDirs,
const UInt64 *size, const UInt64 *compressedSize)
{
for (int i = 0; i < _fields.Size(); i++)
{
const CFieldInfo &fieldInfo = _fields[i];
PrintSpaces(fieldInfo.PrefixSpacesWidth);
NCOM::CPropVariant prop;
if (fieldInfo.PropID == kpidSize)
PrintNumberString(fieldInfo.TextAdjustment, fieldInfo.Width, size);
else if (fieldInfo.PropID == kpidPackSize)
PrintNumberString(fieldInfo.TextAdjustment, fieldInfo.Width, compressedSize);
else if (fieldInfo.PropID == kpidPath)
{
wchar_t textString[32];
ConvertUInt64ToString(numFiles, textString);
UString temp = textString;
temp += L" ";
temp += kFilesMessage;
temp += L", ";
ConvertUInt64ToString(numDirs, textString);
temp += textString;
temp += L" ";
temp += kDirsMessage;
PrintString(fieldInfo.TextAdjustment, 0, temp);
}
else
PrintString(fieldInfo.TextAdjustment, fieldInfo.Width, L"");
}
return S_OK;
}
bool GetUInt64Value(IInArchive *archive, UInt32 index, PROPID propID, UInt64 &value)
{
NCOM::CPropVariant prop;
if (archive->GetProperty(index, propID, &prop) != S_OK)
throw "GetPropertyValue error";
if (prop.vt == VT_EMPTY)
return false;
value = ConvertPropVariantToUInt64(prop);
return true;
}
static void PrintPropPair(const wchar_t *name, const wchar_t *value)
{
g_StdOut << name << " = " << value << endl;
}
HRESULT ListArchives(CCodecs *codecs, const CIntVector &formatIndices,
bool stdInMode,
UStringVector &arcPaths, UStringVector &arcPathsFull,
const NWildcard::CCensorNode &wildcardCensor,
bool enableHeaders, bool techMode,
#ifndef _NO_CRYPTO
bool &passwordEnabled, UString &password,
#endif
UInt64 &numErrors)
{
numErrors = 0;
CFieldPrinter fieldPrinter;
if (!techMode)
fieldPrinter.Init(kStandardFieldTable, sizeof(kStandardFieldTable) / sizeof(kStandardFieldTable[0]));
UInt64 numFiles2 = 0, numDirs2 = 0, totalPackSize2 = 0, totalUnPackSize2 = 0;
UInt64 *totalPackSizePointer2 = 0, *totalUnPackSizePointer2 = 0;
int numArcs = /* stdInMode ? 1 : */ arcPaths.Size();
for (int i = 0; i < numArcs; i++)
{
const UString &archiveName = arcPaths[i];
UInt64 arcPackSize = 0;
if (!stdInMode)
{
NFile::NFind::CFileInfoW fi;
if (!fi.Find(archiveName) || fi.IsDir())
{
g_StdOut << endl << "Error: " << archiveName << " is not file" << endl;
numErrors++;
continue;
}
arcPackSize = fi.Size;
}
CArchiveLink archiveLink;
COpenCallbackConsole openCallback;
openCallback.OutStream = &g_StdOut;
#ifndef _NO_CRYPTO
openCallback.PasswordIsDefined = passwordEnabled;
openCallback.Password = password;
#endif
HRESULT result = archiveLink.Open2(codecs, formatIndices, stdInMode, NULL, archiveName, &openCallback);
if (result != S_OK)
{
if (result == E_ABORT)
return result;
g_StdOut << endl << "Error: " << archiveName << ": ";
if (result == S_FALSE)
{
#ifndef _NO_CRYPTO
if (openCallback.Open_WasPasswordAsked())
g_StdOut << "Can not open encrypted archive. Wrong password?";
else
#endif
g_StdOut << "Can not open file as archive";
}
else if (result == E_OUTOFMEMORY)
g_StdOut << "Can't allocate required memory";
else
g_StdOut << NError::MyFormatMessage(result);
g_StdOut << endl;
numErrors++;
continue;
}
if (!stdInMode)
for (int v = 0; v < archiveLink.VolumePaths.Size(); v++)
{
int index = arcPathsFull.FindInSorted(archiveLink.VolumePaths[v]);
if (index >= 0 && index > i)
{
arcPaths.Delete(index);
arcPathsFull.Delete(index);
numArcs = arcPaths.Size();
}
}
if (enableHeaders)
{
g_StdOut << endl << kListing << archiveName << endl << endl;
for (int i = 0; i < archiveLink.Arcs.Size(); i++)
{
const CArc &arc = archiveLink.Arcs[i];
g_StdOut << "--\n";
PrintPropPair(L"Path", arc.Path);
PrintPropPair(L"Type", codecs->Formats[arc.FormatIndex].Name);
if (!arc.ErrorMessage.IsEmpty())
PrintPropPair(L"Error", arc.ErrorMessage);
UInt32 numProps;
IInArchive *archive = arc.Archive;
if (archive->GetNumberOfArchiveProperties(&numProps) == S_OK)
{
for (UInt32 j = 0; j < numProps; j++)
{
CMyComBSTR name;
PROPID propID;
VARTYPE vt;
RINOK(archive->GetArchivePropertyInfo(j, &name, &propID, &vt));
NCOM::CPropVariant prop;
RINOK(archive->GetArchiveProperty(propID, &prop));
UString s = ConvertPropertyToString(prop, propID);
if (!s.IsEmpty())
PrintPropPair(GetPropName(propID, name), s);
}
}
if (i != archiveLink.Arcs.Size() - 1)
{
UInt32 numProps;
g_StdOut << "----\n";
if (archive->GetNumberOfProperties(&numProps) == S_OK)
{
UInt32 mainIndex = archiveLink.Arcs[i + 1].SubfileIndex;
for (UInt32 j = 0; j < numProps; j++)
{
CMyComBSTR name;
PROPID propID;
VARTYPE vt;
RINOK(archive->GetPropertyInfo(j, &name, &propID, &vt));
NCOM::CPropVariant prop;
RINOK(archive->GetProperty(mainIndex, propID, &prop));
UString s = ConvertPropertyToString(prop, propID);
if (!s.IsEmpty())
PrintPropPair(GetPropName(propID, name), s);
}
}
}
}
g_StdOut << endl;
if (techMode)
g_StdOut << "----------\n";
}
if (enableHeaders && !techMode)
{
fieldPrinter.PrintTitle();
g_StdOut << endl;
fieldPrinter.PrintTitleLines();
g_StdOut << endl;
}
const CArc &arc = archiveLink.Arcs.Back();
IInArchive *archive = arc.Archive;
if (techMode)
{
RINOK(fieldPrinter.Init(archive));
}
UInt64 numFiles = 0, numDirs = 0, totalPackSize = 0, totalUnPackSize = 0;
UInt64 *totalPackSizePointer = 0, *totalUnPackSizePointer = 0;
UInt32 numItems;
RINOK(archive->GetNumberOfItems(&numItems));
for (UInt32 i = 0; i < numItems; i++)
{
if (NConsoleClose::TestBreakSignal())
return E_ABORT;
UString filePath;
HRESULT res = arc.GetItemPath(i, filePath);
if (stdInMode && res == E_INVALIDARG)
break;
RINOK(res);
bool isFolder;
RINOK(IsArchiveItemFolder(archive, i, isFolder));
if (!wildcardCensor.CheckPath(filePath, !isFolder))
continue;
fieldPrinter.PrintItemInfo(arc, i, techMode);
UInt64 packSize, unpackSize;
if (!GetUInt64Value(archive, i, kpidSize, unpackSize))
unpackSize = 0;
else
totalUnPackSizePointer = &totalUnPackSize;
if (!GetUInt64Value(archive, i, kpidPackSize, packSize))
packSize = 0;
else
totalPackSizePointer = &totalPackSize;
g_StdOut << endl;
if (isFolder)
numDirs++;
else
numFiles++;
totalPackSize += packSize;
totalUnPackSize += unpackSize;
}
if (!stdInMode && totalPackSizePointer == 0)
{
if (archiveLink.VolumePaths.Size() != 0)
arcPackSize += archiveLink.VolumesSize;
totalPackSize = (numFiles == 0) ? 0 : arcPackSize;
totalPackSizePointer = &totalPackSize;
}
if (totalUnPackSizePointer == 0 && numFiles == 0)
{
totalUnPackSize = 0;
totalUnPackSizePointer = &totalUnPackSize;
}
if (enableHeaders && !techMode)
{
fieldPrinter.PrintTitleLines();
g_StdOut << endl;
fieldPrinter.PrintSummaryInfo(numFiles, numDirs, totalUnPackSizePointer, totalPackSizePointer);
g_StdOut << endl;
}
if (totalPackSizePointer != 0)
{
totalPackSizePointer2 = &totalPackSize2;
totalPackSize2 += totalPackSize;
}
if (totalUnPackSizePointer != 0)
{
totalUnPackSizePointer2 = &totalUnPackSize2;
totalUnPackSize2 += totalUnPackSize;
}
numFiles2 += numFiles;
numDirs2 += numDirs;
}
if (enableHeaders && !techMode && numArcs > 1)
{
g_StdOut << endl;
fieldPrinter.PrintTitleLines();
g_StdOut << endl;
fieldPrinter.PrintSummaryInfo(numFiles2, numDirs2, totalUnPackSizePointer2, totalPackSizePointer2);
g_StdOut << endl;
g_StdOut << "Archives: " << numArcs << endl;
}
return S_OK;
}
|