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