1 // UpdateCallbackConsole.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "UpdateCallbackConsole.h"
6 
7 #include "Windows/Error.h"
8 #ifndef _7ZIP_ST
9 #include "Windows/Synchronization.h"
10 #endif
11 
12 #include "ConsoleClose.h"
13 #include "UserInputUtils.h"
14 
15 using namespace NWindows;
16 
17 #ifndef _7ZIP_ST
18 static NSynchronization::CCriticalSection g_CriticalSection;
19 #define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
20 #else
21 #define MT_LOCK
22 #endif
23 
24 static const wchar_t *kEmptyFileAlias = L"[Content]";
25 
26 static const char *kCreatingArchiveMessage = "Creating archive ";
27 static const char *kUpdatingArchiveMessage = "Updating archive ";
28 static const char *kScanningMessage = "Scanning";
29 
30 
OpenResult(const wchar_t * name,HRESULT result)31 HRESULT CUpdateCallbackConsole::OpenResult(const wchar_t *name, HRESULT result)
32 {
33   (*OutStream) << endl;
34   if (result != S_OK)
35     (*OutStream) << "Error: " << name << " is not supported archive" << endl;
36   return S_OK;
37 }
38 
StartScanning()39 HRESULT CUpdateCallbackConsole::StartScanning()
40 {
41   (*OutStream) << kScanningMessage;
42   return S_OK;
43 }
44 
ScanProgress(UInt64,UInt64,const wchar_t *)45 HRESULT CUpdateCallbackConsole::ScanProgress(UInt64 /* numFolders */, UInt64 /* numFiles */, const wchar_t * /* path */)
46 {
47   return CheckBreak();
48 }
49 
CanNotFindError(const wchar_t * name,DWORD systemError)50 HRESULT CUpdateCallbackConsole::CanNotFindError(const wchar_t *name, DWORD systemError)
51 {
52   CantFindFiles.Add(name);
53   CantFindCodes.Add(systemError);
54   // m_PercentPrinter.ClosePrint();
55   if (!m_WarningsMode)
56   {
57     (*OutStream) << endl << endl;
58     m_PercentPrinter.PrintNewLine();
59     m_WarningsMode = true;
60   }
61   m_PercentPrinter.PrintString(name);
62   m_PercentPrinter.PrintString(":  WARNING: ");
63   m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
64   m_PercentPrinter.PrintNewLine();
65   return S_OK;
66 }
67 
FinishScanning()68 HRESULT CUpdateCallbackConsole::FinishScanning()
69 {
70   (*OutStream) << endl << endl;
71   return S_OK;
72 }
73 
StartArchive(const wchar_t * name,bool updating)74 HRESULT CUpdateCallbackConsole::StartArchive(const wchar_t *name, bool updating)
75 {
76   if(updating)
77     (*OutStream) << kUpdatingArchiveMessage;
78   else
79     (*OutStream) << kCreatingArchiveMessage;
80   if (name != 0)
81     (*OutStream) << name;
82   else
83     (*OutStream) << "StdOut";
84   (*OutStream) << endl << endl;
85   return S_OK;
86 }
87 
FinishArchive()88 HRESULT CUpdateCallbackConsole::FinishArchive()
89 {
90   (*OutStream) << endl;
91   return S_OK;
92 }
93 
CheckBreak()94 HRESULT CUpdateCallbackConsole::CheckBreak()
95 {
96   if (NConsoleClose::TestBreakSignal())
97     return E_ABORT;
98   return S_OK;
99 }
100 
Finilize()101 HRESULT CUpdateCallbackConsole::Finilize()
102 {
103   MT_LOCK
104   if (m_NeedBeClosed)
105   {
106     if (EnablePercents)
107     {
108       m_PercentPrinter.ClosePrint();
109     }
110     if (!StdOutMode && m_NeedNewLine)
111     {
112       m_PercentPrinter.PrintNewLine();
113       m_NeedNewLine = false;
114     }
115     m_NeedBeClosed = false;
116   }
117   return S_OK;
118 }
119 
SetNumFiles(UInt64)120 HRESULT CUpdateCallbackConsole::SetNumFiles(UInt64 /* numFiles */)
121 {
122   return S_OK;
123 }
124 
SetTotal(UInt64 size)125 HRESULT CUpdateCallbackConsole::SetTotal(UInt64 size)
126 {
127   MT_LOCK
128   if (EnablePercents)
129     m_PercentPrinter.SetTotal(size);
130   return S_OK;
131 }
132 
SetCompleted(const UInt64 * completeValue)133 HRESULT CUpdateCallbackConsole::SetCompleted(const UInt64 *completeValue)
134 {
135   MT_LOCK
136   if (completeValue != NULL)
137   {
138     if (EnablePercents)
139     {
140       m_PercentPrinter.SetRatio(*completeValue);
141       m_PercentPrinter.PrintRatio();
142       m_NeedBeClosed = true;
143     }
144   }
145   if (NConsoleClose::TestBreakSignal())
146     return E_ABORT;
147   return S_OK;
148 }
149 
SetRatioInfo(const UInt64 *,const UInt64 *)150 HRESULT CUpdateCallbackConsole::SetRatioInfo(const UInt64 * /* inSize */, const UInt64 * /* outSize */)
151 {
152   if (NConsoleClose::TestBreakSignal())
153     return E_ABORT;
154   return S_OK;
155 }
156 
GetStream(const wchar_t * name,bool isAnti)157 HRESULT CUpdateCallbackConsole::GetStream(const wchar_t *name, bool isAnti)
158 {
159   MT_LOCK
160   if (StdOutMode)
161     return S_OK;
162   if(isAnti)
163     m_PercentPrinter.PrintString("Anti item    ");
164   else
165     m_PercentPrinter.PrintString("Compressing  ");
166   if (name[0] == 0)
167     name = kEmptyFileAlias;
168   m_PercentPrinter.PrintString(name);
169   if (EnablePercents)
170     m_PercentPrinter.RePrintRatio();
171   return S_OK;
172 }
173 
OpenFileError(const wchar_t * name,DWORD systemError)174 HRESULT CUpdateCallbackConsole::OpenFileError(const wchar_t *name, DWORD systemError)
175 {
176   MT_LOCK
177   FailedCodes.Add(systemError);
178   FailedFiles.Add(name);
179   // if (systemError == ERROR_SHARING_VIOLATION)
180   {
181     m_PercentPrinter.ClosePrint();
182     m_PercentPrinter.PrintNewLine();
183     m_PercentPrinter.PrintString("WARNING: ");
184     m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
185     return S_FALSE;
186   }
187   // return systemError;
188 }
189 
SetOperationResult(Int32)190 HRESULT CUpdateCallbackConsole::SetOperationResult(Int32 )
191 {
192   m_NeedBeClosed = true;
193   m_NeedNewLine = true;
194   return S_OK;
195 }
196 
CryptoGetTextPassword2(Int32 * passwordIsDefined,BSTR * password)197 HRESULT CUpdateCallbackConsole::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password)
198 {
199   *password = NULL;
200 
201   #ifdef _NO_CRYPTO
202 
203   *passwordIsDefined = false;
204   return S_OK;
205 
206   #else
207 
208   if (!PasswordIsDefined)
209   {
210     if (AskPassword)
211     {
212       Password = GetPassword(OutStream);
213       PasswordIsDefined = true;
214     }
215   }
216   *passwordIsDefined = BoolToInt(PasswordIsDefined);
217   return StringToBstr(Password, password);
218 
219   #endif
220 }
221 
CryptoGetTextPassword(BSTR * password)222 HRESULT CUpdateCallbackConsole::CryptoGetTextPassword(BSTR *password)
223 {
224   *password = NULL;
225 
226   #ifdef _NO_CRYPTO
227 
228   return E_NOTIMPL;
229 
230   #else
231 
232   if (!PasswordIsDefined)
233   {
234     {
235       Password = GetPassword(OutStream);
236       PasswordIsDefined = true;
237     }
238   }
239   return StringToBstr(Password, password);
240 
241   #endif
242 }
243 
244 /*
245 HRESULT CUpdateCallbackConsole::ShowDeleteFile(const wchar_t *name)
246 {
247   // MT_LOCK
248   if (StdOutMode)
249     return S_OK;
250   RINOK(Finilize());
251   m_PercentPrinter.PrintString("Deleting  ");
252   if (name[0] == 0)
253     name = kEmptyFileAlias;
254   m_PercentPrinter.PrintString(name);
255   if (EnablePercents)
256     m_PercentPrinter.RePrintRatio();
257   m_NeedBeClosed = true;
258   m_NeedNewLine = true;
259   return S_OK;
260 }
261 */
262