|
1 // ExtractCallbackConsole.h |
|
2 |
|
3 #include "StdAfx.h" |
|
4 |
|
5 #include "ExtractCallbackConsole.h" |
|
6 #include "UserInputUtils.h" |
|
7 #include "ConsoleClose.h" |
|
8 |
|
9 #include "Common/Wildcard.h" |
|
10 |
|
11 #include "Windows/FileDir.h" |
|
12 #include "Windows/FileFind.h" |
|
13 #include "Windows/Time.h" |
|
14 #include "Windows/Defs.h" |
|
15 #include "Windows/PropVariant.h" |
|
16 #include "Windows/Error.h" |
|
17 #include "Windows/PropVariantConversions.h" |
|
18 |
|
19 #include "../../Common/FilePathAutoRename.h" |
|
20 |
|
21 #include "../Common/ExtractingFilePath.h" |
|
22 |
|
23 using namespace NWindows; |
|
24 using namespace NFile; |
|
25 using namespace NDirectory; |
|
26 |
|
27 static const char *kTestingString = "Testing "; |
|
28 static const char *kExtractingString = "Extracting "; |
|
29 static const char *kSkippingString = "Skipping "; |
|
30 |
|
31 // static const char *kCantAutoRename = "can not create file with auto name\n"; |
|
32 // static const char *kCantRenameFile = "can not rename existing file\n"; |
|
33 // static const char *kCantDeleteOutputFile = "can not delete output file "; |
|
34 static const char *kError = "ERROR: "; |
|
35 static const char *kMemoryExceptionMessage = "Can't allocate required memory!"; |
|
36 |
|
37 static const char *kProcessing = "Processing archive: "; |
|
38 static const char *kEverythingIsOk = "Everything is Ok"; |
|
39 static const char *kNoFiles = "No files to process"; |
|
40 |
|
41 static const char *kUnsupportedMethod = "Unsupported Method"; |
|
42 static const char *kCrcFailed = "CRC Failed"; |
|
43 static const char *kCrcFailedEncrypted = "CRC Failed in encrypted file. Wrong password?"; |
|
44 static const char *kDataError = "Data Error"; |
|
45 static const char *kDataErrorEncrypted = "Data Error in encrypted file. Wrong password?"; |
|
46 static const char *kUnknownError = "Unknown Error"; |
|
47 |
|
48 STDMETHODIMP CExtractCallbackConsole::SetTotal(UInt64) |
|
49 { |
|
50 if (NConsoleClose::TestBreakSignal()) |
|
51 return E_ABORT; |
|
52 return S_OK; |
|
53 } |
|
54 |
|
55 STDMETHODIMP CExtractCallbackConsole::SetCompleted(const UInt64 *) |
|
56 { |
|
57 if (NConsoleClose::TestBreakSignal()) |
|
58 return E_ABORT; |
|
59 return S_OK; |
|
60 } |
|
61 |
|
62 STDMETHODIMP CExtractCallbackConsole::AskOverwrite( |
|
63 const wchar_t *existName, const FILETIME *, const UInt64 *, |
|
64 const wchar_t *newName, const FILETIME *, const UInt64 *, |
|
65 Int32 *answer) |
|
66 { |
|
67 (*OutStream) << "file " << existName << |
|
68 "\nalready exists. Overwrite with " << endl; |
|
69 (*OutStream) << newName; |
|
70 |
|
71 NUserAnswerMode::EEnum overwriteAnswer = ScanUserYesNoAllQuit(OutStream); |
|
72 |
|
73 switch(overwriteAnswer) |
|
74 { |
|
75 case NUserAnswerMode::kQuit: |
|
76 return E_ABORT; |
|
77 case NUserAnswerMode::kNo: |
|
78 *answer = NOverwriteAnswer::kNo; |
|
79 break; |
|
80 case NUserAnswerMode::kNoAll: |
|
81 *answer = NOverwriteAnswer::kNoToAll; |
|
82 break; |
|
83 case NUserAnswerMode::kYesAll: |
|
84 *answer = NOverwriteAnswer::kYesToAll; |
|
85 break; |
|
86 case NUserAnswerMode::kYes: |
|
87 *answer = NOverwriteAnswer::kYes; |
|
88 break; |
|
89 case NUserAnswerMode::kAutoRename: |
|
90 *answer = NOverwriteAnswer::kAutoRename; |
|
91 break; |
|
92 default: |
|
93 return E_FAIL; |
|
94 } |
|
95 return S_OK; |
|
96 } |
|
97 |
|
98 STDMETHODIMP CExtractCallbackConsole::PrepareOperation(const wchar_t *name, bool /* isFolder */, Int32 askExtractMode, const UInt64 *position) |
|
99 { |
|
100 switch (askExtractMode) |
|
101 { |
|
102 case NArchive::NExtract::NAskMode::kExtract: |
|
103 (*OutStream) << kExtractingString; |
|
104 break; |
|
105 case NArchive::NExtract::NAskMode::kTest: |
|
106 (*OutStream) << kTestingString; |
|
107 break; |
|
108 case NArchive::NExtract::NAskMode::kSkip: |
|
109 (*OutStream) << kSkippingString; |
|
110 break; |
|
111 }; |
|
112 (*OutStream) << name; |
|
113 if (position != 0) |
|
114 (*OutStream) << " <" << *position << ">"; |
|
115 return S_OK; |
|
116 } |
|
117 |
|
118 STDMETHODIMP CExtractCallbackConsole::MessageError(const wchar_t *message) |
|
119 { |
|
120 (*OutStream) << message << endl; |
|
121 NumFileErrorsInCurrentArchive++; |
|
122 NumFileErrors++; |
|
123 return S_OK; |
|
124 } |
|
125 |
|
126 STDMETHODIMP CExtractCallbackConsole::SetOperationResult(Int32 operationResult, bool encrypted) |
|
127 { |
|
128 switch(operationResult) |
|
129 { |
|
130 case NArchive::NExtract::NOperationResult::kOK: |
|
131 break; |
|
132 default: |
|
133 { |
|
134 NumFileErrorsInCurrentArchive++; |
|
135 NumFileErrors++; |
|
136 (*OutStream) << " "; |
|
137 switch(operationResult) |
|
138 { |
|
139 case NArchive::NExtract::NOperationResult::kUnSupportedMethod: |
|
140 (*OutStream) << kUnsupportedMethod; |
|
141 break; |
|
142 case NArchive::NExtract::NOperationResult::kCRCError: |
|
143 (*OutStream) << (encrypted ? kCrcFailedEncrypted: kCrcFailed); |
|
144 break; |
|
145 case NArchive::NExtract::NOperationResult::kDataError: |
|
146 (*OutStream) << (encrypted ? kDataErrorEncrypted : kDataError); |
|
147 break; |
|
148 default: |
|
149 (*OutStream) << kUnknownError; |
|
150 } |
|
151 } |
|
152 } |
|
153 (*OutStream) << endl; |
|
154 return S_OK; |
|
155 } |
|
156 |
|
157 STDMETHODIMP CExtractCallbackConsole::CryptoGetTextPassword(BSTR *password) |
|
158 { |
|
159 if (!PasswordIsDefined) |
|
160 { |
|
161 Password = GetPassword(OutStream); |
|
162 PasswordIsDefined = true; |
|
163 } |
|
164 CMyComBSTR tempName(Password); |
|
165 *password = tempName.Detach(); |
|
166 return S_OK; |
|
167 } |
|
168 |
|
169 HRESULT CExtractCallbackConsole::BeforeOpen(const wchar_t *name) |
|
170 { |
|
171 NumArchives++; |
|
172 NumFileErrorsInCurrentArchive = 0; |
|
173 (*OutStream) << endl << kProcessing << name << endl; |
|
174 return S_OK; |
|
175 } |
|
176 |
|
177 HRESULT CExtractCallbackConsole::OpenResult(const wchar_t * /* name */, HRESULT result, bool encrypted) |
|
178 { |
|
179 (*OutStream) << endl; |
|
180 if (result != S_OK) |
|
181 { |
|
182 (*OutStream) << "Error: "; |
|
183 if (encrypted) |
|
184 (*OutStream) << "Can not open encrypted archive. Wrong password?"; |
|
185 else |
|
186 (*OutStream) << "Can not open file as archive"; |
|
187 (*OutStream) << endl; |
|
188 NumArchiveErrors++; |
|
189 } |
|
190 return S_OK; |
|
191 } |
|
192 |
|
193 HRESULT CExtractCallbackConsole::ThereAreNoFiles() |
|
194 { |
|
195 (*OutStream) << endl << kNoFiles << endl; |
|
196 return S_OK; |
|
197 } |
|
198 |
|
199 HRESULT CExtractCallbackConsole::ExtractResult(HRESULT result) |
|
200 { |
|
201 if (result == S_OK) |
|
202 { |
|
203 (*OutStream) << endl; |
|
204 if (NumFileErrorsInCurrentArchive == 0) |
|
205 (*OutStream) << kEverythingIsOk << endl; |
|
206 else |
|
207 { |
|
208 NumArchiveErrors++; |
|
209 (*OutStream) << "Sub items Errors: " << NumFileErrorsInCurrentArchive << endl; |
|
210 } |
|
211 } |
|
212 if (result == S_OK) |
|
213 return result; |
|
214 NumArchiveErrors++; |
|
215 if (result == E_ABORT || result == ERROR_DISK_FULL) |
|
216 return result; |
|
217 (*OutStream) << endl << kError; |
|
218 if (result == E_OUTOFMEMORY) |
|
219 (*OutStream) << kMemoryExceptionMessage; |
|
220 else |
|
221 { |
|
222 UString message; |
|
223 NError::MyFormatMessage(result, message); |
|
224 (*OutStream) << message; |
|
225 } |
|
226 (*OutStream) << endl; |
|
227 return S_OK; |
|
228 } |
|
229 |
|
230 HRESULT CExtractCallbackConsole::SetPassword(const UString &password) |
|
231 { |
|
232 PasswordIsDefined = true; |
|
233 Password = password; |
|
234 return S_OK; |
|
235 } |