1 // UpdateCallbackFar.cpp
2
3 #include "StdAfx.h"
4
5 #ifndef Z7_ST
6 #include "../../../Windows/Synchronization.h"
7 #endif
8
9 #include "../../../Common/StringConvert.h"
10
11 #include "FarUtils.h"
12 #include "UpdateCallbackFar.h"
13
14 using namespace NWindows;
15 using namespace NFar;
16
17 #ifndef Z7_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
CheckBreak2()24 static HRESULT CheckBreak2()
25 {
26 return WasEscPressed() ? E_ABORT : S_OK;
27 }
28
29
Z7_COM7F_IMF(CUpdateCallback100Imp::ScanProgress (UInt64 numFolders,UInt64 numFiles,UInt64 totalSize,const wchar_t * path,Int32))30 Z7_COM7F_IMF(CUpdateCallback100Imp::ScanProgress(UInt64 numFolders, UInt64 numFiles, UInt64 totalSize, const wchar_t *path, Int32 /* isDir */))
31 {
32 MT_LOCK
33
34 if (_percent)
35 {
36 _percent->FilesTotal = numFolders + numFiles;
37 _percent->Total = totalSize;
38 _percent->Command = "Scanning";
39 _percent->FileName = path;
40 _percent->Print();
41 _percent->Print();
42 }
43 return CheckBreak2();
44 }
45
Z7_COM7F_IMF(CUpdateCallback100Imp::ScanError (const wchar_t * path,HRESULT errorCode))46 Z7_COM7F_IMF(CUpdateCallback100Imp::ScanError(const wchar_t *path, HRESULT errorCode))
47 {
48 if (ShowSysErrorMessage(errorCode, path) == -1)
49 return E_ABORT;
50 return CheckBreak2();
51 }
52
Z7_COM7F_IMF(CUpdateCallback100Imp::SetNumFiles (UInt64 numFiles))53 Z7_COM7F_IMF(CUpdateCallback100Imp::SetNumFiles(UInt64 numFiles))
54 {
55 MT_LOCK
56
57 if (_percent)
58 {
59 _percent->FilesTotal = numFiles;
60 _percent->Print();
61 }
62 return CheckBreak2();
63 }
64
65
Z7_COM7F_IMF(CUpdateCallback100Imp::SetTotal (const UInt64 *,const UInt64 *))66 Z7_COM7F_IMF(CUpdateCallback100Imp::SetTotal(const UInt64 * /* files */, const UInt64 * /* bytes */))
67 {
68 return S_OK;
69 }
70
Z7_COM7F_IMF(CUpdateCallback100Imp::SetCompleted (const UInt64 *,const UInt64 *))71 Z7_COM7F_IMF(CUpdateCallback100Imp::SetCompleted(const UInt64 * /* files */, const UInt64 * /* bytes */))
72 {
73 MT_LOCK
74 return CheckBreak2();
75 }
76
77
78
Z7_COM7F_IMF(CUpdateCallback100Imp::SetTotal (UInt64 size))79 Z7_COM7F_IMF(CUpdateCallback100Imp::SetTotal(UInt64 size))
80 {
81 MT_LOCK
82
83 if (_percent)
84 {
85 _percent->Total = size;
86 _percent->Print();
87 }
88 return CheckBreak2();
89 }
90
Z7_COM7F_IMF(CUpdateCallback100Imp::SetCompleted (const UInt64 * completeValue))91 Z7_COM7F_IMF(CUpdateCallback100Imp::SetCompleted(const UInt64 *completeValue))
92 {
93 MT_LOCK
94
95 if (_percent)
96 {
97 if (completeValue)
98 _percent->Completed = *completeValue;
99 _percent->Print();
100 }
101 return CheckBreak2();
102 }
103
Z7_COM7F_IMF(CUpdateCallback100Imp::CompressOperation (const wchar_t * name))104 Z7_COM7F_IMF(CUpdateCallback100Imp::CompressOperation(const wchar_t *name))
105 {
106 MT_LOCK
107
108 if (_percent)
109 {
110 _percent->Command = "Adding";
111 _percent->FileName = name;
112 _percent->Print();
113 }
114 return CheckBreak2();
115 }
116
Z7_COM7F_IMF(CUpdateCallback100Imp::DeleteOperation (const wchar_t * name))117 Z7_COM7F_IMF(CUpdateCallback100Imp::DeleteOperation(const wchar_t *name))
118 {
119 MT_LOCK
120
121 if (_percent)
122 {
123 _percent->Command = "Deleting";
124 _percent->FileName = name;
125 _percent->Print();
126 }
127 return CheckBreak2();
128 }
129
Z7_COM7F_IMF(CUpdateCallback100Imp::OperationResult (Int32))130 Z7_COM7F_IMF(CUpdateCallback100Imp::OperationResult(Int32 /* opRes */))
131 {
132 MT_LOCK
133
134 if (_percent)
135 {
136 _percent->Files++;
137 }
138 return CheckBreak2();
139 }
140
Z7_COM7F_IMF(CUpdateCallback100Imp::UpdateErrorMessage (const wchar_t * message))141 Z7_COM7F_IMF(CUpdateCallback100Imp::UpdateErrorMessage(const wchar_t *message))
142 {
143 MT_LOCK
144
145 if (g_StartupInfo.ShowErrorMessage(UnicodeStringToMultiByte(message, CP_OEMCP)) == -1)
146 return E_ABORT;
147 return CheckBreak2();
148 }
149
Z7_COM7F_IMF(CUpdateCallback100Imp::OpenFileError (const wchar_t * path,HRESULT errorCode))150 Z7_COM7F_IMF(CUpdateCallback100Imp::OpenFileError(const wchar_t *path, HRESULT errorCode))
151 {
152 if (ShowSysErrorMessage(errorCode, path) == -1)
153 return E_ABORT;
154 return CheckBreak2();
155 }
156
Z7_COM7F_IMF(CUpdateCallback100Imp::ReadingFileError (const wchar_t * path,HRESULT errorCode))157 Z7_COM7F_IMF(CUpdateCallback100Imp::ReadingFileError(const wchar_t *path, HRESULT errorCode))
158 {
159 if (ShowSysErrorMessage(errorCode, path) == -1)
160 return E_ABORT;
161 return CheckBreak2();
162 }
163
164 void SetExtractErrorMessage(Int32 opRes, Int32 encrypted, AString &s);
165
Z7_COM7F_IMF(CUpdateCallback100Imp::ReportExtractResult (Int32 opRes,Int32 isEncrypted,const wchar_t * name))166 Z7_COM7F_IMF(CUpdateCallback100Imp::ReportExtractResult(Int32 opRes, Int32 isEncrypted, const wchar_t *name))
167 {
168 MT_LOCK
169
170 if (opRes != NArchive::NExtract::NOperationResult::kOK)
171 {
172 AString s;
173 SetExtractErrorMessage(opRes, isEncrypted, s);
174 if (PrintErrorMessage(s, name) == -1)
175 return E_ABORT;
176 }
177
178 return CheckBreak2();
179 }
180
181
Z7_COM7F_IMF(CUpdateCallback100Imp::ReportUpdateOperation (UInt32 op,const wchar_t * name,Int32))182 Z7_COM7F_IMF(CUpdateCallback100Imp::ReportUpdateOperation(UInt32 op, const wchar_t *name, Int32 /* isDir */))
183 {
184 const char *s;
185 switch (op)
186 {
187 case NUpdateNotifyOp::kAdd: s = "Adding"; break;
188 case NUpdateNotifyOp::kUpdate: s = "Updating"; break;
189 case NUpdateNotifyOp::kAnalyze: s = "Analyzing"; break;
190 case NUpdateNotifyOp::kReplicate: s = "Replicating"; break;
191 case NUpdateNotifyOp::kRepack: s = "Repacking"; break;
192 case NUpdateNotifyOp::kSkip: s = "Skipping"; break;
193 case NUpdateNotifyOp::kHeader: s = "Header creating"; break;
194 case NUpdateNotifyOp::kDelete: s = "Deleting"; break;
195 default: s = "Unknown operation";
196 }
197
198 MT_LOCK
199
200 if (_percent)
201 {
202 _percent->Command = s;
203 _percent->FileName.Empty();
204 if (name)
205 _percent->FileName = name;
206 _percent->Print();
207 }
208
209 return CheckBreak2();
210 }
211
212
MoveArc_UpdateStatus()213 HRESULT CUpdateCallback100Imp::MoveArc_UpdateStatus()
214 {
215 MT_LOCK
216
217 if (_percent)
218 {
219 AString s;
220 s.Add_UInt64(_arcMoving_percents);
221 // status.Add_Space();
222 s.Add_Char('%');
223 const bool totalDefined = (_arcMoving_total != 0 && _arcMoving_total != (UInt64)(Int64)-1);
224 if (_arcMoving_current != 0 || totalDefined)
225 {
226 s += " : ";
227 s.Add_UInt64(_arcMoving_current >> 20);
228 s += " MiB";
229 }
230 if (totalDefined)
231 {
232 s += " / ";
233 s.Add_UInt64((_arcMoving_total + ((1 << 20) - 1)) >> 20);
234 s += " MiB";
235 }
236 s += " : temporary archive moving ...";
237 _percent->Command = s;
238 _percent->Print();
239 }
240
241 return CheckBreak2();
242 }
243
244
Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Start (const wchar_t * srcTempPath,const wchar_t *,UInt64 size,Int32))245 Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Start(const wchar_t *srcTempPath, const wchar_t * /* destFinalPath */ , UInt64 size, Int32 /* updateMode */))
246 {
247 MT_LOCK
248
249 _arcMoving_total = size;
250 _arcMoving_current = 0;
251 _arcMoving_percents = 0;
252 // _arcMoving_updateMode = updateMode;
253 // _name2 = fs2us(destFinalPath);
254 if (_percent)
255 _percent->FileName = srcTempPath;
256 return MoveArc_UpdateStatus();
257 }
258
Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Progress (UInt64 totalSize,UInt64 currentSize))259 Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Progress(UInt64 totalSize, UInt64 currentSize))
260 {
261 UInt64 percents = 0;
262 if (totalSize != 0)
263 {
264 if (totalSize < ((UInt64)1 << 57))
265 percents = currentSize * 100 / totalSize;
266 else
267 percents = currentSize / (totalSize / 100);
268 }
269
270 #ifdef _WIN32
271 // Sleep(300); // for debug
272 #endif
273 if (percents == _arcMoving_percents)
274 return CheckBreak2();
275 _arcMoving_total = totalSize;
276 _arcMoving_current = currentSize;
277 _arcMoving_percents = percents;
278 // if (_arcMoving_percents > 100) return E_FAIL;
279 return MoveArc_UpdateStatus();
280 }
281
282
Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Finish ())283 Z7_COM7F_IMF(CUpdateCallback100Imp::MoveArc_Finish())
284 {
285 // _arcMoving_percents = 0;
286 if (_percent)
287 {
288 _percent->Command.Empty();
289 _percent->FileName.Empty();
290 _percent->Print();
291 }
292 return CheckBreak2();
293 }
294
295
Z7_COM7F_IMF(CUpdateCallback100Imp::Before_ArcReopen ())296 Z7_COM7F_IMF(CUpdateCallback100Imp::Before_ArcReopen())
297 {
298 // fixme: we can use Clear_Stop_Status() here
299 return CheckBreak2();
300 }
301
302
303 extern HRESULT GetPassword(UString &password);
304
Z7_COM7F_IMF(CUpdateCallback100Imp::CryptoGetTextPassword (BSTR * password))305 Z7_COM7F_IMF(CUpdateCallback100Imp::CryptoGetTextPassword(BSTR *password))
306 {
307 MT_LOCK
308
309 *password = NULL;
310 if (!PasswordIsDefined)
311 {
312 RINOK(GetPassword(Password))
313 PasswordIsDefined = true;
314 }
315 return StringToBstr(Password, password);
316 }
317
Z7_COM7F_IMF(CUpdateCallback100Imp::CryptoGetTextPassword2 (Int32 * passwordIsDefined,BSTR * password))318 Z7_COM7F_IMF(CUpdateCallback100Imp::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password))
319 {
320 MT_LOCK
321
322 *password = NULL;
323 *passwordIsDefined = BoolToInt(PasswordIsDefined);
324 if (!PasswordIsDefined)
325 return S_OK;
326 return StringToBstr(Password, password);
327 }
328