ExtractCallbackConsole.cpp 6.3 KB

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