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
|
// ExtractCallbackConsole.cpp
#include "StdAfx.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/Wildcard.h"
#include "../../../Windows/FileDir.h"
#include "../../../Windows/FileFind.h"
#include "../../../Windows/TimeUtils.h"
#include "../../../Windows/ErrorMsg.h"
#include "../../../Windows/PropVariantConv.h"
#ifndef _7ZIP_ST
#include "../../../Windows/Synchronization.h"
#endif
#include "../../Common/FilePathAutoRename.h"
#include "../Common/ExtractingFilePath.h"
#include "ConsoleClose.h"
#include "ExtractCallbackConsole.h"
#include "UserInputUtils.h"
using namespace NWindows;
using namespace NFile;
using namespace NDir;
static HRESULT CheckBreak2()
{
return NConsoleClose::TestBreakSignal() ? E_ABORT : S_OK;
}
static const char * const kError = "ERROR: ";
void CExtractScanConsole::StartScanning()
{
if (NeedPercents())
_percent.Command = "Scan";
}
HRESULT CExtractScanConsole::ScanProgress(const CDirItemsStat &st, const FString &path, bool /* isDir */)
{
if (NeedPercents())
{
_percent.Files = st.NumDirs + st.NumFiles;
_percent.Completed = st.GetTotalBytes();
_percent.FileName = fs2us(path);
_percent.Print();
}
return CheckBreak2();
}
HRESULT CExtractScanConsole::ScanError(const FString &path, DWORD systemError)
{
ClosePercentsAndFlush();
if (_se)
{
*_se << endl << kError << NError::MyFormatMessage(systemError) << endl;
_se->NormalizePrint_UString(fs2us(path));
*_se << endl << endl;
_se->Flush();
}
return HRESULT_FROM_WIN32(systemError);
}
void Print_UInt64_and_String(AString &s, UInt64 val, const char *name)
{
char temp[32];
ConvertUInt64ToString(val, temp);
s += temp;
s.Add_Space();
s += name;
}
void PrintSize_bytes_Smart(AString &s, UInt64 val)
{
Print_UInt64_and_String(s, val, "bytes");
if (val == 0)
return;
unsigned numBits = 10;
char c = 'K';
char temp[4] = { 'K', 'i', 'B', 0 };
if (val >= ((UInt64)10 << 30)) { numBits = 30; c = 'G'; }
else if (val >= ((UInt64)10 << 20)) { numBits = 20; c = 'M'; }
temp[0] = c;
s += " (";
Print_UInt64_and_String(s, ((val + ((UInt64)1 << numBits) - 1) >> numBits), temp);
s += ')';
}
void PrintSize_bytes_Smart_comma(AString &s, UInt64 val)
{
if (val == (UInt64)(Int64)-1)
return;
s += ", ";
PrintSize_bytes_Smart(s, val);
}
void Print_DirItemsStat(AString &s, const CDirItemsStat &st)
{
if (st.NumDirs != 0)
{
Print_UInt64_and_String(s, st.NumDirs, st.NumDirs == 1 ? "folder" : "folders");
s += ", ";
}
Print_UInt64_and_String(s, st.NumFiles, st.NumFiles == 1 ? "file" : "files");
PrintSize_bytes_Smart_comma(s, st.FilesSize);
if (st.NumAltStreams != 0)
{
s.Add_LF();
Print_UInt64_and_String(s, st.NumAltStreams, "alternate streams");
PrintSize_bytes_Smart_comma(s, st.AltStreamsSize);
}
}
void Print_DirItemsStat2(AString &s, const CDirItemsStat2 &st)
{
Print_DirItemsStat(s, (CDirItemsStat &)st);
bool needLF = true;
if (st.Anti_NumDirs != 0)
{
if (needLF)
s.Add_LF();
needLF = false;
Print_UInt64_and_String(s, st.Anti_NumDirs, st.Anti_NumDirs == 1 ? "anti-folder" : "anti-folders");
}
if (st.Anti_NumFiles != 0)
{
if (needLF)
s.Add_LF();
else
s += ", ";
needLF = false;
Print_UInt64_and_String(s, st.Anti_NumFiles, st.Anti_NumFiles == 1 ? "anti-file" : "anti-files");
}
if (st.Anti_NumAltStreams != 0)
{
if (needLF)
s.Add_LF();
else
s += ", ";
needLF = false;
Print_UInt64_and_String(s, st.Anti_NumAltStreams, "anti-alternate-streams");
}
}
void CExtractScanConsole::PrintStat(const CDirItemsStat &st)
{
if (_so)
{
AString s;
Print_DirItemsStat(s, st);
*_so << s << endl;
}
}
#ifndef _7ZIP_ST
static NSynchronization::CCriticalSection g_CriticalSection;
#define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
#else
#define MT_LOCK
#endif
static const char * const kTestString = "T";
static const char * const kExtractString = "-";
static const char * const kSkipString = ".";
// static const char * const kCantAutoRename = "can not create file with auto name\n";
// static const char * const kCantRenameFile = "can not rename existing file\n";
// static const char * const kCantDeleteOutputFile = "can not delete output file ";
static const char * const kMemoryExceptionMessage = "Can't allocate required memory!";
static const char * const kExtracting = "Extracting archive: ";
static const char * const kTesting = "Testing archive: ";
static const char * const kEverythingIsOk = "Everything is Ok";
static const char * const kNoFiles = "No files to process";
static const char * const kUnsupportedMethod = "Unsupported Method";
static const char * const kCrcFailed = "CRC Failed";
static const char * const kCrcFailedEncrypted = "CRC Failed in encrypted file. Wrong password?";
static const char * const kDataError = "Data Error";
static const char * const kDataErrorEncrypted = "Data Error in encrypted file. Wrong password?";
static const char * const kUnavailableData = "Unavailable data";
static const char * const kUnexpectedEnd = "Unexpected end of data";
static const char * const kDataAfterEnd = "There are some data after the end of the payload data";
static const char * const kIsNotArc = "Is not archive";
static const char * const kHeadersError = "Headers Error";
static const char * const kWrongPassword = "Wrong password";
static const char * const k_ErrorFlagsMessages[] =
{
"Is not archive"
, "Headers Error"
, "Headers Error in encrypted archive. Wrong password?"
, "Unavailable start of archive"
, "Unconfirmed start of archive"
, "Unexpected end of archive"
, "There are data after the end of archive"
, "Unsupported method"
, "Unsupported feature"
, "Data Error"
, "CRC Error"
};
STDMETHODIMP CExtractCallbackConsole::SetTotal(UInt64 size)
{
MT_LOCK
if (NeedPercents())
{
_percent.Total = size;
_percent.Print();
}
return CheckBreak2();
}
STDMETHODIMP CExtractCallbackConsole::SetCompleted(const UInt64 *completeValue)
{
MT_LOCK
if (NeedPercents())
{
if (completeValue)
_percent.Completed = *completeValue;
_percent.Print();
}
return CheckBreak2();
}
static const char * const kTab = " ";
static void PrintFileInfo(CStdOutStream *_so, const wchar_t *path, const FILETIME *ft, const UInt64 *size)
{
*_so << kTab << "Path: ";
_so->NormalizePrint_wstr(path);
*_so << endl;
if (size && *size != (UInt64)(Int64)-1)
{
AString s;
PrintSize_bytes_Smart(s, *size);
*_so << kTab << "Size: " << s << endl;
}
if (ft)
{
char temp[64];
if (ConvertUtcFileTimeToString(*ft, temp, kTimestampPrintLevel_SEC))
*_so << kTab << "Modified: " << temp << endl;
}
}
STDMETHODIMP CExtractCallbackConsole::AskOverwrite(
const wchar_t *existName, const FILETIME *existTime, const UInt64 *existSize,
const wchar_t *newName, const FILETIME *newTime, const UInt64 *newSize,
Int32 *answer)
{
MT_LOCK
RINOK(CheckBreak2());
ClosePercentsAndFlush();
if (_so)
{
*_so << endl << "Would you like to replace the existing file:\n";
PrintFileInfo(_so, existName, existTime, existSize);
*_so << "with the file from archive:\n";
PrintFileInfo(_so, newName, newTime, newSize);
}
NUserAnswerMode::EEnum overwriteAnswer = ScanUserYesNoAllQuit(_so);
switch (overwriteAnswer)
{
case NUserAnswerMode::kQuit: return E_ABORT;
case NUserAnswerMode::kNo: *answer = NOverwriteAnswer::kNo; break;
case NUserAnswerMode::kNoAll: *answer = NOverwriteAnswer::kNoToAll; break;
case NUserAnswerMode::kYesAll: *answer = NOverwriteAnswer::kYesToAll; break;
case NUserAnswerMode::kYes: *answer = NOverwriteAnswer::kYes; break;
case NUserAnswerMode::kAutoRenameAll: *answer = NOverwriteAnswer::kAutoRename; break;
case NUserAnswerMode::kEof: return E_ABORT;
case NUserAnswerMode::kError: return E_FAIL;
default: return E_FAIL;
}
if (_so)
{
*_so << endl;
if (NeedFlush)
_so->Flush();
}
return CheckBreak2();
}
STDMETHODIMP CExtractCallbackConsole::PrepareOperation(const wchar_t *name, Int32 /* isFolder */, Int32 askExtractMode, const UInt64 *position)
{
MT_LOCK
_currentName = name;
const char *s;
unsigned requiredLevel = 1;
switch (askExtractMode)
{
case NArchive::NExtract::NAskMode::kExtract: s = kExtractString; break;
case NArchive::NExtract::NAskMode::kTest: s = kTestString; break;
case NArchive::NExtract::NAskMode::kSkip: s = kSkipString; requiredLevel = 2; break;
default: s = "???"; requiredLevel = 2;
};
bool show2 = (LogLevel >= requiredLevel && _so);
if (show2)
{
ClosePercents_for_so();
_tempA = s;
if (name)
_tempA.Add_Space();
*_so << _tempA;
_tempU.Empty();
if (name)
{
_tempU = name;
_so->Normalize_UString(_tempU);
}
_so->PrintUString(_tempU, _tempA);
if (position)
*_so << " <" << *position << ">";
*_so << endl;
if (NeedFlush)
_so->Flush();
}
if (NeedPercents())
{
if (PercentsNameLevel >= 1)
{
_percent.FileName.Empty();
_percent.Command.Empty();
if (PercentsNameLevel > 1 || !show2)
{
_percent.Command = s;
if (name)
_percent.FileName = name;
}
}
_percent.Print();
}
return CheckBreak2();
}
STDMETHODIMP CExtractCallbackConsole::MessageError(const wchar_t *message)
{
MT_LOCK
RINOK(CheckBreak2());
NumFileErrors_in_Current++;
NumFileErrors++;
ClosePercentsAndFlush();
if (_se)
{
*_se << kError << message << endl;
_se->Flush();
}
return CheckBreak2();
}
void SetExtractErrorMessage(Int32 opRes, Int32 encrypted, AString &dest)
{
dest.Empty();
const char *s = NULL;
switch (opRes)
{
case NArchive::NExtract::NOperationResult::kUnsupportedMethod:
s = kUnsupportedMethod;
break;
case NArchive::NExtract::NOperationResult::kCRCError:
s = (encrypted ? kCrcFailedEncrypted : kCrcFailed);
break;
case NArchive::NExtract::NOperationResult::kDataError:
s = (encrypted ? kDataErrorEncrypted : kDataError);
break;
case NArchive::NExtract::NOperationResult::kUnavailable:
s = kUnavailableData;
break;
case NArchive::NExtract::NOperationResult::kUnexpectedEnd:
s = kUnexpectedEnd;
break;
case NArchive::NExtract::NOperationResult::kDataAfterEnd:
s = kDataAfterEnd;
break;
case NArchive::NExtract::NOperationResult::kIsNotArc:
s = kIsNotArc;
break;
case NArchive::NExtract::NOperationResult::kHeadersError:
s = kHeadersError;
break;
case NArchive::NExtract::NOperationResult::kWrongPassword:
s = kWrongPassword;
break;
}
dest += kError;
if (s)
dest += s;
else
{
dest += "Error #";
dest.Add_UInt32(opRes);
}
}
STDMETHODIMP CExtractCallbackConsole::SetOperationResult(Int32 opRes, Int32 encrypted)
{
MT_LOCK
if (opRes == NArchive::NExtract::NOperationResult::kOK)
{
if (NeedPercents())
{
_percent.Command.Empty();
_percent.FileName.Empty();
_percent.Files++;
}
}
else
{
NumFileErrors_in_Current++;
NumFileErrors++;
if (_se)
{
ClosePercentsAndFlush();
AString s;
SetExtractErrorMessage(opRes, encrypted, s);
*_se << s;
if (!_currentName.IsEmpty())
{
*_se << " : ";
_se->NormalizePrint_UString(_currentName);
}
*_se << endl;
_se->Flush();
}
}
return CheckBreak2();
}
STDMETHODIMP CExtractCallbackConsole::ReportExtractResult(Int32 opRes, Int32 encrypted, const wchar_t *name)
{
if (opRes != NArchive::NExtract::NOperationResult::kOK)
{
_currentName = name;
return SetOperationResult(opRes, encrypted);
}
return CheckBreak2();
}
#ifndef _NO_CRYPTO
HRESULT CExtractCallbackConsole::SetPassword(const UString &password)
{
PasswordIsDefined = true;
Password = password;
return S_OK;
}
STDMETHODIMP CExtractCallbackConsole::CryptoGetTextPassword(BSTR *password)
{
COM_TRY_BEGIN
MT_LOCK
return Open_CryptoGetTextPassword(password);
COM_TRY_END
}
#endif
HRESULT CExtractCallbackConsole::BeforeOpen(const wchar_t *name, bool testMode)
{
RINOK(CheckBreak2());
NumTryArcs++;
ThereIsError_in_Current = false;
ThereIsWarning_in_Current = false;
NumFileErrors_in_Current = 0;
ClosePercents_for_so();
if (_so)
{
*_so << endl << (testMode ? kTesting : kExtracting);
_so->NormalizePrint_wstr(name);
*_so << endl;
}
if (NeedPercents())
_percent.Command = "Open";
return S_OK;
}
HRESULT Print_OpenArchive_Props(CStdOutStream &so, const CCodecs *codecs, const CArchiveLink &arcLink);
HRESULT Print_OpenArchive_Error(CStdOutStream &so, const CCodecs *codecs, const CArchiveLink &arcLink);
static AString GetOpenArcErrorMessage(UInt32 errorFlags)
{
AString s;
for (unsigned i = 0; i < ARRAY_SIZE(k_ErrorFlagsMessages); i++)
{
UInt32 f = (1 << i);
if ((errorFlags & f) == 0)
continue;
const char *m = k_ErrorFlagsMessages[i];
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 += sz;
}
return s;
}
void PrintErrorFlags(CStdOutStream &so, const char *s, UInt32 errorFlags)
{
if (errorFlags == 0)
return;
so << s << endl << GetOpenArcErrorMessage(errorFlags) << endl;
}
void Add_Messsage_Pre_ArcType(UString &s, const char *pre, const wchar_t *arcType)
{
s.Add_LF();
s += pre;
s += " as [";
s += arcType;
s += "] archive";
}
void Print_ErrorFormatIndex_Warning(CStdOutStream *_so, const CCodecs *codecs, const CArc &arc)
{
const CArcErrorInfo &er = arc.ErrorInfo;
*_so << "WARNING:\n";
_so->NormalizePrint_UString(arc.Path);
UString s;
if (arc.FormatIndex == er.ErrorFormatIndex)
{
s.Add_LF();
s += "The archive is open with offset";
}
else
{
Add_Messsage_Pre_ArcType(s, "Can not open the file", codecs->GetFormatNamePtr(er.ErrorFormatIndex));
Add_Messsage_Pre_ArcType(s, "The file is open", codecs->GetFormatNamePtr(arc.FormatIndex));
}
*_so << s << endl << endl;
}
HRESULT CExtractCallbackConsole::OpenResult(
const CCodecs *codecs, const CArchiveLink &arcLink,
const wchar_t *name, HRESULT result)
{
ClosePercents();
if (NeedPercents())
{
_percent.Files = 0;
_percent.Command.Empty();
_percent.FileName.Empty();
}
ClosePercentsAndFlush();
FOR_VECTOR (level, arcLink.Arcs)
{
const CArc &arc = arcLink.Arcs[level];
const CArcErrorInfo &er = arc.ErrorInfo;
UInt32 errorFlags = er.GetErrorFlags();
if (errorFlags != 0 || !er.ErrorMessage.IsEmpty())
{
if (_se)
{
*_se << endl;
if (level != 0)
{
_se->NormalizePrint_UString(arc.Path);
*_se << endl;
}
}
if (errorFlags != 0)
{
if (_se)
PrintErrorFlags(*_se, "ERRORS:", errorFlags);
NumOpenArcErrors++;
ThereIsError_in_Current = true;
}
if (!er.ErrorMessage.IsEmpty())
{
if (_se)
*_se << "ERRORS:" << endl << er.ErrorMessage << endl;
NumOpenArcErrors++;
ThereIsError_in_Current = true;
}
if (_se)
{
*_se << endl;
_se->Flush();
}
}
UInt32 warningFlags = er.GetWarningFlags();
if (warningFlags != 0 || !er.WarningMessage.IsEmpty())
{
if (_so)
{
*_so << endl;
if (level != 0)
{
_so->NormalizePrint_UString(arc.Path);
*_so << endl;
}
}
if (warningFlags != 0)
{
if (_so)
PrintErrorFlags(*_so, "WARNINGS:", warningFlags);
NumOpenArcWarnings++;
ThereIsWarning_in_Current = true;
}
if (!er.WarningMessage.IsEmpty())
{
if (_so)
*_so << "WARNINGS:" << endl << er.WarningMessage << endl;
NumOpenArcWarnings++;
ThereIsWarning_in_Current = true;
}
if (_so)
{
*_so << endl;
if (NeedFlush)
_so->Flush();
}
}
if (er.ErrorFormatIndex >= 0)
{
if (_so)
{
Print_ErrorFormatIndex_Warning(_so, codecs, arc);
if (NeedFlush)
_so->Flush();
}
ThereIsWarning_in_Current = true;
}
}
if (result == S_OK)
{
if (_so)
{
RINOK(Print_OpenArchive_Props(*_so, codecs, arcLink));
*_so << endl;
}
}
else
{
NumCantOpenArcs++;
if (_so)
_so->Flush();
if (_se)
{
*_se << kError;
_se->NormalizePrint_wstr(name);
*_se << endl;
HRESULT res = Print_OpenArchive_Error(*_se, codecs, arcLink);
RINOK(res);
if (result == S_FALSE)
{
}
else
{
if (result == E_OUTOFMEMORY)
*_se << "Can't allocate required memory";
else
*_se << NError::MyFormatMessage(result);
*_se << endl;
}
_se->Flush();
}
}
return CheckBreak2();
}
HRESULT CExtractCallbackConsole::ThereAreNoFiles()
{
ClosePercents_for_so();
if (_so)
{
*_so << endl << kNoFiles << endl;
if (NeedFlush)
_so->Flush();
}
return CheckBreak2();
}
HRESULT CExtractCallbackConsole::ExtractResult(HRESULT result)
{
MT_LOCK
if (NeedPercents())
{
_percent.ClosePrint(true);
_percent.Command.Empty();
_percent.FileName.Empty();
}
if (_so)
_so->Flush();
if (result == S_OK)
{
if (NumFileErrors_in_Current == 0 && !ThereIsError_in_Current)
{
if (ThereIsWarning_in_Current)
NumArcsWithWarnings++;
else
NumOkArcs++;
if (_so)
*_so << kEverythingIsOk << endl;
}
else
{
NumArcsWithError++;
if (_so)
{
*_so << endl;
if (NumFileErrors_in_Current != 0)
*_so << "Sub items Errors: " << NumFileErrors_in_Current << endl;
}
}
if (_so && NeedFlush)
_so->Flush();
}
else
{
NumArcsWithError++;
if (result == E_ABORT || result == ERROR_DISK_FULL)
return result;
if (_se)
{
*_se << endl << kError;
if (result == E_OUTOFMEMORY)
*_se << kMemoryExceptionMessage;
else
*_se << NError::MyFormatMessage(result);
*_se << endl;
_se->Flush();
}
}
return CheckBreak2();
}
|