UpdateCallbackConsole.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // UpdateCallbackConsole.cpp
  2. #include "StdAfx.h"
  3. #include "UpdateCallbackConsole.h"
  4. #include "Windows/Error.h"
  5. #ifdef COMPRESS_MT
  6. #include "Windows/Synchronization.h"
  7. #endif
  8. #include "ConsoleClose.h"
  9. #include "UserInputUtils.h"
  10. using namespace NWindows;
  11. #ifdef COMPRESS_MT
  12. static NSynchronization::CCriticalSection g_CriticalSection;
  13. #define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
  14. #else
  15. #define MT_LOCK
  16. #endif
  17. static const wchar_t *kEmptyFileAlias = L"[Content]";
  18. static const char *kCreatingArchiveMessage = "Creating archive ";
  19. static const char *kUpdatingArchiveMessage = "Updating archive ";
  20. static const char *kScanningMessage = "Scanning";
  21. HRESULT CUpdateCallbackConsole::OpenResult(const wchar_t *name, HRESULT result)
  22. {
  23. (*OutStream) << endl;
  24. if (result != S_OK)
  25. (*OutStream) << "Error: " << name << " is not supported archive" << endl;
  26. return S_OK;
  27. }
  28. HRESULT CUpdateCallbackConsole::StartScanning()
  29. {
  30. (*OutStream) << kScanningMessage;
  31. return S_OK;
  32. }
  33. HRESULT CUpdateCallbackConsole::CanNotFindError(const wchar_t *name, DWORD systemError)
  34. {
  35. CantFindFiles.Add(name);
  36. CantFindCodes.Add(systemError);
  37. // m_PercentPrinter.ClosePrint();
  38. if (!m_WarningsMode)
  39. {
  40. (*OutStream) << endl << endl;
  41. m_PercentPrinter.PrintNewLine();
  42. m_WarningsMode = true;
  43. }
  44. m_PercentPrinter.PrintString(name);
  45. m_PercentPrinter.PrintString(": WARNING: ");
  46. m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
  47. m_PercentPrinter.PrintNewLine();
  48. return S_OK;
  49. }
  50. HRESULT CUpdateCallbackConsole::FinishScanning()
  51. {
  52. (*OutStream) << endl << endl;
  53. return S_OK;
  54. }
  55. HRESULT CUpdateCallbackConsole::StartArchive(const wchar_t *name, bool updating)
  56. {
  57. if(updating)
  58. (*OutStream) << kUpdatingArchiveMessage;
  59. else
  60. (*OutStream) << kCreatingArchiveMessage;
  61. if (name != 0)
  62. (*OutStream) << name;
  63. else
  64. (*OutStream) << "StdOut";
  65. (*OutStream) << endl << endl;
  66. return S_OK;
  67. }
  68. HRESULT CUpdateCallbackConsole::FinishArchive()
  69. {
  70. (*OutStream) << endl;
  71. return S_OK;
  72. }
  73. HRESULT CUpdateCallbackConsole::CheckBreak()
  74. {
  75. if (NConsoleClose::TestBreakSignal())
  76. return E_ABORT;
  77. return S_OK;
  78. }
  79. HRESULT CUpdateCallbackConsole::Finilize()
  80. {
  81. MT_LOCK
  82. if (m_NeedBeClosed)
  83. {
  84. if (EnablePercents)
  85. {
  86. m_PercentPrinter.ClosePrint();
  87. }
  88. if (!StdOutMode && m_NeedNewLine)
  89. {
  90. m_PercentPrinter.PrintNewLine();
  91. m_NeedNewLine = false;
  92. }
  93. m_NeedBeClosed = false;
  94. }
  95. return S_OK;
  96. }
  97. HRESULT CUpdateCallbackConsole::SetNumFiles(UInt64 /* numFiles */)
  98. {
  99. return S_OK;
  100. }
  101. HRESULT CUpdateCallbackConsole::SetTotal(UInt64 size)
  102. {
  103. MT_LOCK
  104. if (EnablePercents)
  105. m_PercentPrinter.SetTotal(size);
  106. return S_OK;
  107. }
  108. HRESULT CUpdateCallbackConsole::SetCompleted(const UInt64 *completeValue)
  109. {
  110. MT_LOCK
  111. if (completeValue != NULL)
  112. {
  113. if (EnablePercents)
  114. {
  115. m_PercentPrinter.SetRatio(*completeValue);
  116. m_PercentPrinter.PrintRatio();
  117. m_NeedBeClosed = true;
  118. }
  119. }
  120. if (NConsoleClose::TestBreakSignal())
  121. return E_ABORT;
  122. return S_OK;
  123. }
  124. HRESULT CUpdateCallbackConsole::SetRatioInfo(const UInt64 * /* inSize */, const UInt64 * /* outSize */)
  125. {
  126. /*
  127. if (NConsoleClose::TestBreakSignal())
  128. return E_ABORT;
  129. */
  130. return S_OK;
  131. }
  132. HRESULT CUpdateCallbackConsole::GetStream(const wchar_t *name, bool isAnti)
  133. {
  134. MT_LOCK
  135. if (StdOutMode)
  136. return S_OK;
  137. if(isAnti)
  138. m_PercentPrinter.PrintString("Anti item ");
  139. else
  140. m_PercentPrinter.PrintString("Compressing ");
  141. if (name[0] == 0)
  142. name = kEmptyFileAlias;
  143. m_PercentPrinter.PrintString(name);
  144. if (EnablePercents)
  145. m_PercentPrinter.RePrintRatio();
  146. return S_OK;
  147. }
  148. HRESULT CUpdateCallbackConsole::OpenFileError(const wchar_t *name, DWORD systemError)
  149. {
  150. MT_LOCK
  151. FailedCodes.Add(systemError);
  152. FailedFiles.Add(name);
  153. // if (systemError == ERROR_SHARING_VIOLATION)
  154. {
  155. m_PercentPrinter.ClosePrint();
  156. m_PercentPrinter.PrintNewLine();
  157. m_PercentPrinter.PrintString("WARNING: ");
  158. m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
  159. return S_FALSE;
  160. }
  161. // return systemError;
  162. }
  163. HRESULT CUpdateCallbackConsole::SetOperationResult(Int32 )
  164. {
  165. m_NeedBeClosed = true;
  166. m_NeedNewLine = true;
  167. return S_OK;
  168. }
  169. HRESULT CUpdateCallbackConsole::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password)
  170. {
  171. if (!PasswordIsDefined)
  172. {
  173. if (AskPassword)
  174. {
  175. Password = GetPassword(OutStream);
  176. PasswordIsDefined = true;
  177. }
  178. }
  179. *passwordIsDefined = BoolToInt(PasswordIsDefined);
  180. CMyComBSTR tempName(Password);
  181. *password = tempName.Detach();
  182. return S_OK;
  183. }