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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
|
// ExtractCallback.cpp
#include "StdAfx.h"
#include "../../../Common/ComTry.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/Lang.h"
#include "../../../Common/StringConvert.h"
#include "../../../Windows/ErrorMsg.h"
#include "../../../Windows/FileDir.h"
#include "../../../Windows/FileFind.h"
#include "../../../Windows/PropVariantConv.h"
#include "../../Common/FilePathAutoRename.h"
#include "../../Common/StreamUtils.h"
#include "../Common/ExtractingFilePath.h"
#ifndef _SFX
#include "../Common/ZipRegistry.h"
#endif
#include "../GUI/ExtractRes.h"
#include "resourceGui.h"
#include "ExtractCallback.h"
#include "FormatUtils.h"
#include "LangUtils.h"
#include "OverwriteDialog.h"
#ifndef _NO_CRYPTO
#include "PasswordDialog.h"
#endif
#include "PropertyName.h"
using namespace NWindows;
using namespace NFile;
using namespace NFind;
CExtractCallbackImp::~CExtractCallbackImp() {}
void CExtractCallbackImp::Init()
{
_lang_Extracting = LangString(IDS_PROGRESS_EXTRACTING);
_lang_Testing = LangString(IDS_PROGRESS_TESTING);
_lang_Skipping = LangString(IDS_PROGRESS_SKIPPING);
NumArchiveErrors = 0;
ThereAreMessageErrors = false;
#ifndef _SFX
NumFolders = NumFiles = 0;
NeedAddFile = false;
#endif
}
void CExtractCallbackImp::AddError_Message(LPCWSTR s)
{
ThereAreMessageErrors = true;
ProgressDialog->Sync.AddError_Message(s);
}
#ifndef _SFX
STDMETHODIMP CExtractCallbackImp::SetNumFiles(UInt64
#ifndef _SFX
numFiles
#endif
)
{
#ifndef _SFX
ProgressDialog->Sync.Set_NumFilesTotal(numFiles);
#endif
return S_OK;
}
#endif
STDMETHODIMP CExtractCallbackImp::SetTotal(UInt64 total)
{
ProgressDialog->Sync.Set_NumBytesTotal(total);
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::SetCompleted(const UInt64 *value)
{
return ProgressDialog->Sync.Set_NumBytesCur(value);
}
HRESULT CExtractCallbackImp::Open_CheckBreak()
{
return ProgressDialog->Sync.CheckStop();
}
HRESULT CExtractCallbackImp::Open_SetTotal(const UInt64 *files, const UInt64 *bytes)
{
HRESULT res = S_OK;
if (!MultiArcMode)
{
if (files)
{
_totalFilesDefined = true;
// res = ProgressDialog->Sync.Set_NumFilesTotal(*files);
}
else
_totalFilesDefined = false;
if (bytes)
{
_totalBytesDefined = true;
ProgressDialog->Sync.Set_NumBytesTotal(*bytes);
}
else
_totalBytesDefined = false;
}
return res;
}
HRESULT CExtractCallbackImp::Open_SetCompleted(const UInt64 *files, const UInt64 *bytes)
{
if (!MultiArcMode)
{
if (files)
{
ProgressDialog->Sync.Set_NumFilesCur(*files);
}
if (bytes)
{
}
}
return ProgressDialog->Sync.CheckStop();
}
HRESULT CExtractCallbackImp::Open_Finished()
{
return ProgressDialog->Sync.CheckStop();
}
#ifndef _NO_CRYPTO
HRESULT CExtractCallbackImp::Open_CryptoGetTextPassword(BSTR *password)
{
return CryptoGetTextPassword(password);
}
/*
HRESULT CExtractCallbackImp::Open_GetPasswordIfAny(bool &passwordIsDefined, UString &password)
{
passwordIsDefined = PasswordIsDefined;
password = Password;
return S_OK;
}
bool CExtractCallbackImp::Open_WasPasswordAsked()
{
return PasswordWasAsked;
}
void CExtractCallbackImp::Open_Clear_PasswordWasAsked_Flag()
{
PasswordWasAsked = false;
}
*/
#endif
#ifndef _SFX
STDMETHODIMP CExtractCallbackImp::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
{
ProgressDialog->Sync.Set_Ratio(inSize, outSize);
return S_OK;
}
#endif
/*
STDMETHODIMP CExtractCallbackImp::SetTotalFiles(UInt64 total)
{
ProgressDialog->Sync.SetNumFilesTotal(total);
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::SetCompletedFiles(const UInt64 *value)
{
if (value != NULL)
ProgressDialog->Sync.SetNumFilesCur(*value);
return S_OK;
}
*/
STDMETHODIMP CExtractCallbackImp::AskOverwrite(
const wchar_t *existName, const FILETIME *existTime, const UInt64 *existSize,
const wchar_t *newName, const FILETIME *newTime, const UInt64 *newSize,
Int32 *answer)
{
COverwriteDialog dialog;
dialog.OldFileInfo.SetTime(existTime);
dialog.OldFileInfo.SetSize(existSize);
dialog.OldFileInfo.Name = existName;
dialog.NewFileInfo.SetTime(newTime);
dialog.NewFileInfo.SetSize(newSize);
dialog.NewFileInfo.Name = newName;
ProgressDialog->WaitCreating();
INT_PTR writeAnswer = dialog.Create(*ProgressDialog);
switch (writeAnswer)
{
case IDCANCEL: *answer = NOverwriteAnswer::kCancel; return E_ABORT;
case IDYES: *answer = NOverwriteAnswer::kYes; break;
case IDNO: *answer = NOverwriteAnswer::kNo; break;
case IDB_YES_TO_ALL: *answer = NOverwriteAnswer::kYesToAll; break;
case IDB_NO_TO_ALL: *answer = NOverwriteAnswer::kNoToAll; break;
case IDB_AUTO_RENAME: *answer = NOverwriteAnswer::kAutoRename; break;
default: return E_FAIL;
}
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::PrepareOperation(const wchar_t *name, Int32 isFolder, Int32 askExtractMode, const UInt64 * /* position */)
{
_isFolder = IntToBool(isFolder);
_currentFilePath = name;
const UString *msg = &_lang_Empty;
switch (askExtractMode)
{
case NArchive::NExtract::NAskMode::kExtract: msg = &_lang_Extracting; break;
case NArchive::NExtract::NAskMode::kTest: msg = &_lang_Testing; break;
case NArchive::NExtract::NAskMode::kSkip: msg = &_lang_Skipping; break;
// default: s = "Unknown operation";
}
return ProgressDialog->Sync.Set_Status2(*msg, name, IntToBool(isFolder));
}
STDMETHODIMP CExtractCallbackImp::MessageError(const wchar_t *s)
{
AddError_Message(s);
return S_OK;
}
HRESULT CExtractCallbackImp::MessageError(const char *message, const FString &path)
{
ThereAreMessageErrors = true;
ProgressDialog->Sync.AddError_Message_Name(GetUnicodeString(message), fs2us(path));
return S_OK;
}
#ifndef _SFX
STDMETHODIMP CExtractCallbackImp::ShowMessage(const wchar_t *s)
{
AddError_Message(s);
return S_OK;
}
#endif
void SetExtractErrorMessage(Int32 opRes, Int32 encrypted, const wchar_t *fileName, UString &s)
{
s.Empty();
if (opRes == NArchive::NExtract::NOperationResult::kOK)
return;
UINT messageID = 0;
UINT id = 0;
switch (opRes)
{
case NArchive::NExtract::NOperationResult::kUnsupportedMethod:
messageID = IDS_EXTRACT_MESSAGE_UNSUPPORTED_METHOD;
id = IDS_EXTRACT_MSG_UNSUPPORTED_METHOD;
break;
case NArchive::NExtract::NOperationResult::kDataError:
messageID = encrypted ?
IDS_EXTRACT_MESSAGE_DATA_ERROR_ENCRYPTED:
IDS_EXTRACT_MESSAGE_DATA_ERROR;
id = IDS_EXTRACT_MSG_DATA_ERROR;
break;
case NArchive::NExtract::NOperationResult::kCRCError:
messageID = encrypted ?
IDS_EXTRACT_MESSAGE_CRC_ERROR_ENCRYPTED:
IDS_EXTRACT_MESSAGE_CRC_ERROR;
id = IDS_EXTRACT_MSG_CRC_ERROR;
break;
case NArchive::NExtract::NOperationResult::kUnavailable:
id = IDS_EXTRACT_MSG_UNAVAILABLE_DATA;
break;
case NArchive::NExtract::NOperationResult::kUnexpectedEnd:
id = IDS_EXTRACT_MSG_UEXPECTED_END;
break;
case NArchive::NExtract::NOperationResult::kDataAfterEnd:
id = IDS_EXTRACT_MSG_DATA_AFTER_END;
break;
case NArchive::NExtract::NOperationResult::kIsNotArc:
id = IDS_EXTRACT_MSG_IS_NOT_ARC;
break;
case NArchive::NExtract::NOperationResult::kHeadersError:
id = IDS_EXTRACT_MSG_HEADERS_ERROR;
break;
case NArchive::NExtract::NOperationResult::kWrongPassword:
id = IDS_EXTRACT_MSG_WRONG_PSW_CLAIM;
break;
/*
default:
messageID = IDS_EXTRACT_MESSAGE_UNKNOWN_ERROR;
break;
*/
}
UString msg;
UString msgOld;
#ifndef _SFX
if (id != 0)
LangString_OnlyFromLangFile(id, msg);
if (messageID != 0 && msg.IsEmpty())
LangString_OnlyFromLangFile(messageID, msgOld);
#endif
if (msg.IsEmpty() && !msgOld.IsEmpty())
s = MyFormatNew(msgOld, fileName);
else
{
if (msg.IsEmpty() && id != 0)
LangString(id, msg);
if (!msg.IsEmpty())
s += msg;
else
{
char temp[16];
ConvertUInt32ToString(opRes, temp);
s.AddAscii("Error #");
s.AddAscii(temp);
}
if (encrypted && opRes != NArchive::NExtract::NOperationResult::kWrongPassword)
{
// s.AddAscii(" : ");
// AddLangString(s, IDS_EXTRACT_MSG_ENCRYPTED);
s.AddAscii(" : ");
AddLangString(s, IDS_EXTRACT_MSG_WRONG_PSW_GUESS);
}
s.AddAscii(" : ");
s += fileName;
}
}
STDMETHODIMP CExtractCallbackImp::SetOperationResult(Int32 opRes, Int32 encrypted)
{
switch (opRes)
{
case NArchive::NExtract::NOperationResult::kOK:
break;
default:
{
UString s;
SetExtractErrorMessage(opRes, encrypted, _currentFilePath, s);
Add_ArchiveName_Error();
AddError_Message(s);
}
}
#ifndef _SFX
if (_isFolder)
NumFolders++;
else
NumFiles++;
ProgressDialog->Sync.Set_NumFilesCur(NumFiles);
#endif
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::ReportExtractResult(Int32 opRes, Int32 encrypted, const wchar_t *name)
{
if (opRes != NArchive::NExtract::NOperationResult::kOK)
{
UString s;
SetExtractErrorMessage(opRes, encrypted, name, s);
Add_ArchiveName_Error();
AddError_Message(s);
}
return S_OK;
}
////////////////////////////////////////
// IExtractCallbackUI
HRESULT CExtractCallbackImp::BeforeOpen(const wchar_t *name, bool /* testMode */)
{
#ifndef _SFX
RINOK(ProgressDialog->Sync.CheckStop());
ProgressDialog->Sync.Set_TitleFileName(name);
#endif
_currentArchivePath = name;
return S_OK;
}
HRESULT CExtractCallbackImp::SetCurrentFilePath2(const wchar_t *path)
{
_currentFilePath = path;
#ifndef _SFX
ProgressDialog->Sync.Set_FilePath(path);
#endif
return S_OK;
}
#ifndef _SFX
HRESULT CExtractCallbackImp::SetCurrentFilePath(const wchar_t *path)
{
#ifndef _SFX
if (NeedAddFile)
NumFiles++;
NeedAddFile = true;
ProgressDialog->Sync.Set_NumFilesCur(NumFiles);
#endif
return SetCurrentFilePath2(path);
}
#endif
UString HResultToMessage(HRESULT errorCode);
static const UInt32 k_ErrorFlagsIds[] =
{
IDS_EXTRACT_MSG_IS_NOT_ARC,
IDS_EXTRACT_MSG_HEADERS_ERROR,
IDS_EXTRACT_MSG_HEADERS_ERROR,
IDS_OPEN_MSG_UNAVAILABLE_START,
IDS_OPEN_MSG_UNCONFIRMED_START,
IDS_EXTRACT_MSG_UEXPECTED_END,
IDS_EXTRACT_MSG_DATA_AFTER_END,
IDS_EXTRACT_MSG_UNSUPPORTED_METHOD,
IDS_OPEN_MSG_UNSUPPORTED_FEATURE,
IDS_EXTRACT_MSG_DATA_ERROR,
IDS_EXTRACT_MSG_CRC_ERROR
};
static void AddNewLineString(UString &s, const UString &m)
{
s += m;
s.Add_LF();
}
UString GetOpenArcErrorMessage(UInt32 errorFlags)
{
UString s;
for (unsigned i = 0; i < ARRAY_SIZE(k_ErrorFlagsIds); i++)
{
UInt32 f = ((UInt32)1 << i);
if ((errorFlags & f) == 0)
continue;
UInt32 id = k_ErrorFlagsIds[i];
UString m = LangString(id);
if (m.IsEmpty())
continue;
if (f == kpv_ErrorFlags_EncryptedHeadersError)
{
m.AddAscii(" : ");
AddLangString(m, IDS_EXTRACT_MSG_WRONG_PSW_GUESS);
}
if (!s.IsEmpty())
s.Add_LF();
s += m;
errorFlags &= ~f;
}
if (errorFlags != 0)
{
char sz[16];
sz[0] = '0';
sz[1] = 'x';
ConvertUInt32ToHex(errorFlags, sz + 2);
if (!s.IsEmpty())
s.Add_LF();
s.AddAscii(sz);
}
return s;
}
static void ErrorInfo_Print(UString &s, const CArcErrorInfo &er)
{
UInt32 errorFlags = er.GetErrorFlags();
UInt32 warningFlags = er.GetWarningFlags();
if (errorFlags != 0)
AddNewLineString(s, GetOpenArcErrorMessage(errorFlags));
if (!er.ErrorMessage.IsEmpty())
AddNewLineString(s, er.ErrorMessage);
if (warningFlags != 0)
{
s += GetNameOfProperty(kpidWarningFlags, L"Warnings");
s.AddAscii(":");
s.Add_LF();
AddNewLineString(s, GetOpenArcErrorMessage(warningFlags));
}
if (!er.WarningMessage.IsEmpty())
{
s += GetNameOfProperty(kpidWarning, L"Warning");
s.AddAscii(": ");
s += er.WarningMessage;
s.Add_LF();
}
}
static UString GetBracedType(const wchar_t *type)
{
UString s = L'[';
s += type;
s += L']';
return s;
}
void OpenResult_GUI(UString &s, const CCodecs *codecs, const CArchiveLink &arcLink, const wchar_t *name, HRESULT result)
{
FOR_VECTOR (level, arcLink.Arcs)
{
const CArc &arc = arcLink.Arcs[level];
const CArcErrorInfo &er = arc.ErrorInfo;
if (!er.IsThereErrorOrWarning() && er.ErrorFormatIndex < 0)
continue;
if (s.IsEmpty())
{
s += name;
s.Add_LF();
}
if (level != 0)
{
AddNewLineString(s, arc.Path);
}
ErrorInfo_Print(s, er);
if (er.ErrorFormatIndex >= 0)
{
AddNewLineString(s, GetNameOfProperty(kpidWarning, L"Warning"));
if (arc.FormatIndex == er.ErrorFormatIndex)
{
AddNewLineString(s, LangString(IDS_IS_OPEN_WITH_OFFSET));
}
else
{
AddNewLineString(s, MyFormatNew(IDS_CANT_OPEN_AS_TYPE, GetBracedType(codecs->GetFormatNamePtr(er.ErrorFormatIndex))));
AddNewLineString(s, MyFormatNew(IDS_IS_OPEN_AS_TYPE, GetBracedType(codecs->GetFormatNamePtr(arc.FormatIndex))));
}
}
}
if (arcLink.NonOpen_ErrorInfo.ErrorFormatIndex >= 0 || result != S_OK)
{
s += name;
s.Add_LF();
if (!arcLink.Arcs.IsEmpty())
AddNewLineString(s, arcLink.NonOpen_ArcPath);
if (arcLink.NonOpen_ErrorInfo.ErrorFormatIndex >= 0 || result == S_FALSE)
{
UINT id = IDS_CANT_OPEN_ARCHIVE;
UString param;
if (arcLink.PasswordWasAsked)
id = IDS_CANT_OPEN_ENCRYPTED_ARCHIVE;
else if (arcLink.NonOpen_ErrorInfo.ErrorFormatIndex >= 0)
{
id = IDS_CANT_OPEN_AS_TYPE;
param = GetBracedType(codecs->GetFormatNamePtr(arcLink.NonOpen_ErrorInfo.ErrorFormatIndex));
}
UString s2 = MyFormatNew(id, param);
s2.Replace(L" ''", L"");
s2.Replace(L"''", L"");
s += s2;
}
else
s += HResultToMessage(result);
s.Add_LF();
ErrorInfo_Print(s, arcLink.NonOpen_ErrorInfo);
}
if (!s.IsEmpty() && s.Back() == '\n')
s.DeleteBack();
}
HRESULT CExtractCallbackImp::OpenResult(const CCodecs *codecs, const CArchiveLink &arcLink, const wchar_t *name, HRESULT result)
{
_currentArchivePath = name;
_needWriteArchivePath = true;
UString s;
OpenResult_GUI(s, codecs, arcLink, name, result);
if (!s.IsEmpty())
{
NumArchiveErrors++;
AddError_Message(s);
_needWriteArchivePath = false;
}
return S_OK;
}
HRESULT CExtractCallbackImp::ThereAreNoFiles()
{
return S_OK;
}
void CExtractCallbackImp::Add_ArchiveName_Error()
{
if (_needWriteArchivePath)
{
if (!_currentArchivePath.IsEmpty())
AddError_Message(_currentArchivePath);
_needWriteArchivePath = false;
}
}
HRESULT CExtractCallbackImp::ExtractResult(HRESULT result)
{
if (result == S_OK)
return result;
NumArchiveErrors++;
if (result == E_ABORT || result == ERROR_DISK_FULL)
return result;
Add_ArchiveName_Error();
if (!_currentFilePath.IsEmpty())
MessageError(_currentFilePath);
MessageError(NError::MyFormatMessage(result));
return S_OK;
}
#ifndef _NO_CRYPTO
HRESULT CExtractCallbackImp::SetPassword(const UString &password)
{
PasswordIsDefined = true;
Password = password;
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::CryptoGetTextPassword(BSTR *password)
{
PasswordWasAsked = true;
if (!PasswordIsDefined)
{
CPasswordDialog dialog;
#ifndef _SFX
bool showPassword = NExtract::Read_ShowPassword();
dialog.ShowPassword = showPassword;
#endif
ProgressDialog->WaitCreating();
if (dialog.Create(*ProgressDialog) != IDOK)
return E_ABORT;
Password = dialog.Password;
PasswordIsDefined = true;
#ifndef _SFX
if (dialog.ShowPassword != showPassword)
NExtract::Save_ShowPassword(dialog.ShowPassword);
#endif
}
return StringToBstr(Password, password);
}
#endif
#ifndef _SFX
STDMETHODIMP CExtractCallbackImp::AskWrite(
const wchar_t *srcPath, Int32 srcIsFolder,
const FILETIME *srcTime, const UInt64 *srcSize,
const wchar_t *destPath,
BSTR *destPathResult,
Int32 *writeAnswer)
{
UString destPathResultTemp = destPath;
// RINOK(StringToBstr(destPath, destPathResult));
*destPathResult = 0;
*writeAnswer = BoolToInt(false);
FString destPathSys = us2fs(destPath);
bool srcIsFolderSpec = IntToBool(srcIsFolder);
CFileInfo destFileInfo;
if (destFileInfo.Find(destPathSys))
{
if (srcIsFolderSpec)
{
if (!destFileInfo.IsDir())
{
RINOK(MessageError("can not replace file with folder with same name", destPathSys));
return E_ABORT;
}
*writeAnswer = BoolToInt(false);
return S_OK;
}
if (destFileInfo.IsDir())
{
RINOK(MessageError("can not replace folder with file with same name", destPathSys));
*writeAnswer = BoolToInt(false);
return S_OK;
}
switch (OverwriteMode)
{
case NExtract::NOverwriteMode::kSkip:
return S_OK;
case NExtract::NOverwriteMode::kAsk:
{
Int32 overwriteResult;
UString destPathSpec = destPath;
int slashPos = destPathSpec.ReverseFind_PathSepar();
destPathSpec.DeleteFrom(slashPos + 1);
destPathSpec += fs2us(destFileInfo.Name);
RINOK(AskOverwrite(
destPathSpec,
&destFileInfo.MTime, &destFileInfo.Size,
srcPath,
srcTime, srcSize,
&overwriteResult));
switch (overwriteResult)
{
case NOverwriteAnswer::kCancel: return E_ABORT;
case NOverwriteAnswer::kNo: return S_OK;
case NOverwriteAnswer::kNoToAll: OverwriteMode = NExtract::NOverwriteMode::kSkip; return S_OK;
case NOverwriteAnswer::kYes: break;
case NOverwriteAnswer::kYesToAll: OverwriteMode = NExtract::NOverwriteMode::kOverwrite; break;
case NOverwriteAnswer::kAutoRename: OverwriteMode = NExtract::NOverwriteMode::kRename; break;
default:
return E_FAIL;
}
}
}
if (OverwriteMode == NExtract::NOverwriteMode::kRename)
{
if (!AutoRenamePath(destPathSys))
{
RINOK(MessageError("can not create name for file", destPathSys));
return E_ABORT;
}
destPathResultTemp = fs2us(destPathSys);
}
else
if (!NDir::DeleteFileAlways(destPathSys))
{
RINOK(MessageError("can not delete output file", destPathSys));
return E_ABORT;
}
}
*writeAnswer = BoolToInt(true);
return StringToBstr(destPathResultTemp, destPathResult);
}
STDMETHODIMP CExtractCallbackImp::UseExtractToStream(Int32 *res)
{
*res = BoolToInt(StreamMode);
return S_OK;
}
static HRESULT GetTime(IGetProp *getProp, PROPID propID, FILETIME &ft, bool &ftDefined)
{
ftDefined = false;
NCOM::CPropVariant prop;
RINOK(getProp->GetProp(propID, &prop));
if (prop.vt == VT_FILETIME)
{
ft = prop.filetime;
ftDefined = (ft.dwHighDateTime != 0 || ft.dwLowDateTime != 0);
}
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
static HRESULT GetItemBoolProp(IGetProp *getProp, PROPID propID, bool &result)
{
NCOM::CPropVariant prop;
result = false;
RINOK(getProp->GetProp(propID, &prop));
if (prop.vt == VT_BOOL)
result = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
STDMETHODIMP CExtractCallbackImp::GetStream7(const wchar_t *name,
Int32 isDir,
ISequentialOutStream **outStream, Int32 askExtractMode,
IGetProp *getProp)
{
COM_TRY_BEGIN
*outStream = 0;
_newVirtFileWasAdded = false;
_hashStreamWasUsed = false;
_needUpdateStat = false;
if (_hashStream)
_hashStreamSpec->ReleaseStream();
GetItemBoolProp(getProp, kpidIsAltStream, _isAltStream);
if (!ProcessAltStreams && _isAltStream)
return S_OK;
_filePath = name;
_isFolder = IntToBool(isDir);
_curSize = 0;
_curSizeDefined = false;
UInt64 size = 0;
bool sizeDefined;
{
NCOM::CPropVariant prop;
RINOK(getProp->GetProp(kpidSize, &prop));
sizeDefined = ConvertPropVariantToUInt64(prop, size);
}
if (sizeDefined)
{
_curSize = size;
_curSizeDefined = true;
}
if (askExtractMode != NArchive::NExtract::NAskMode::kExtract &&
askExtractMode != NArchive::NExtract::NAskMode::kTest)
return S_OK;
_needUpdateStat = true;
CMyComPtr<ISequentialOutStream> outStreamLoc;
if (VirtFileSystem && askExtractMode == NArchive::NExtract::NAskMode::kExtract)
{
CVirtFile &file = VirtFileSystemSpec->AddNewFile();
_newVirtFileWasAdded = true;
file.Name = name;
file.IsDir = IntToBool(isDir);
file.IsAltStream = _isAltStream;
file.Size = 0;
RINOK(GetTime(getProp, kpidCTime, file.CTime, file.CTimeDefined));
RINOK(GetTime(getProp, kpidATime, file.ATime, file.ATimeDefined));
RINOK(GetTime(getProp, kpidMTime, file.MTime, file.MTimeDefined));
NCOM::CPropVariant prop;
RINOK(getProp->GetProp(kpidAttrib, &prop));
if (prop.vt == VT_UI4)
{
file.Attrib = prop.ulVal;
file.AttribDefined = true;
}
// else if (isDir) file.Attrib = FILE_ATTRIBUTE_DIRECTORY;
file.ExpectedSize = 0;
if (sizeDefined)
file.ExpectedSize = size;
outStreamLoc = VirtFileSystem;
}
if (_hashStream)
{
{
_hashStreamSpec->SetStream(outStreamLoc);
outStreamLoc = _hashStream;
_hashStreamSpec->Init(true);
_hashStreamWasUsed = true;
}
}
if (outStreamLoc)
*outStream = outStreamLoc.Detach();
return S_OK;
COM_TRY_END
}
STDMETHODIMP CExtractCallbackImp::PrepareOperation7(Int32 askExtractMode)
{
COM_TRY_BEGIN
_needUpdateStat = (
askExtractMode == NArchive::NExtract::NAskMode::kExtract ||
askExtractMode == NArchive::NExtract::NAskMode::kTest);
/*
_extractMode = false;
switch (askExtractMode)
{
case NArchive::NExtract::NAskMode::kExtract:
if (_testMode)
askExtractMode = NArchive::NExtract::NAskMode::kTest;
else
_extractMode = true;
break;
};
*/
return SetCurrentFilePath2(_filePath);
COM_TRY_END
}
STDMETHODIMP CExtractCallbackImp::SetOperationResult7(Int32 opRes, Int32 encrypted)
{
COM_TRY_BEGIN
if (VirtFileSystem && _newVirtFileWasAdded)
{
// FIXME: probably we must request file size from VirtFileSystem
// _curSize = VirtFileSystem->GetLastFileSize()
// _curSizeDefined = true;
RINOK(VirtFileSystemSpec->CloseMemFile());
}
if (_hashStream && _hashStreamWasUsed)
{
_hashStreamSpec->_hash->Final(_isFolder, _isAltStream, _filePath);
_curSize = _hashStreamSpec->GetSize();
_curSizeDefined = true;
_hashStreamSpec->ReleaseStream();
_hashStreamWasUsed = false;
}
else if (_hashCalc && _needUpdateStat)
{
_hashCalc->SetSize(_curSize);
_hashCalc->Final(_isFolder, _isAltStream, _filePath);
}
return SetOperationResult(opRes, encrypted);
COM_TRY_END
}
static const size_t k_SizeT_MAX = (size_t)((size_t)0 - 1);
static const UInt32 kBlockSize = ((UInt32)1 << 31);
STDMETHODIMP CVirtFileSystem::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
if (processedSize)
*processedSize = 0;
if (size == 0)
return S_OK;
if (!_fileMode)
{
CVirtFile &file = Files.Back();
size_t rem = file.Data.Size() - (size_t)file.Size;
bool useMem = true;
if (rem < size)
{
UInt64 b = 0;
if (file.Data.Size() == 0)
b = file.ExpectedSize;
UInt64 a = file.Size + size;
if (b < a)
b = a;
a = (UInt64)file.Data.Size() * 2;
if (b < a)
b = a;
useMem = false;
if (b <= k_SizeT_MAX && b <= MaxTotalAllocSize)
useMem = file.Data.ReAlloc_KeepData((size_t)b, (size_t)file.Size);
}
if (useMem)
{
memcpy(file.Data + file.Size, data, size);
file.Size += size;
if (processedSize)
*processedSize = (UInt32)size;
return S_OK;
}
_fileMode = true;
}
RINOK(FlushToDisk(false));
return _outFileStream->Write(data, size, processedSize);
}
HRESULT CVirtFileSystem::FlushToDisk(bool closeLast)
{
if (!_outFileStream)
{
_outFileStreamSpec = new COutFileStream;
_outFileStream = _outFileStreamSpec;
}
while (_numFlushed < Files.Size())
{
const CVirtFile &file = Files[_numFlushed];
const FString path = DirPrefix + us2fs(Get_Correct_FsFile_Name(file.Name));
if (!_fileIsOpen)
{
if (!_outFileStreamSpec->Create(path, false))
{
_outFileStream.Release();
return E_FAIL;
// MessageBoxMyError(UString(L"Can't create file ") + fs2us(tempFilePath));
}
_fileIsOpen = true;
RINOK(WriteStream(_outFileStream, file.Data, (size_t)file.Size));
}
if (_numFlushed == Files.Size() - 1 && !closeLast)
break;
if (file.CTimeDefined ||
file.ATimeDefined ||
file.MTimeDefined)
_outFileStreamSpec->SetTime(
file.CTimeDefined ? &file.CTime : NULL,
file.ATimeDefined ? &file.ATime : NULL,
file.MTimeDefined ? &file.MTime : NULL);
_outFileStreamSpec->Close();
_numFlushed++;
_fileIsOpen = false;
if (file.AttribDefined)
NDir::SetFileAttrib(path, file.Attrib);
}
return S_OK;
}
#endif
|