xref: /aosp_15_r20/external/lzma/CPP/7zip/UI/Common/CompressCall2.cpp (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // CompressCall2.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifndef Z7_EXTERNAL_CODECS
6 
7 #include "../../../Common/MyException.h"
8 
9 #include "../../UI/Common/EnumDirItems.h"
10 
11 #include "../../UI/FileManager/LangUtils.h"
12 
13 #include "../../UI/GUI/BenchmarkDialog.h"
14 #include "../../UI/GUI/ExtractGUI.h"
15 #include "../../UI/GUI/UpdateGUI.h"
16 #include "../../UI/GUI/HashGUI.h"
17 
18 #include "../../UI/GUI/ExtractRes.h"
19 
20 #include "CompressCall.h"
21 
22 extern HWND g_HWND;
23 
24 #define MY_TRY_BEGIN  HRESULT result; try {
25 #define MY_TRY_FINISH } \
26   catch(CSystemException &e) { result = e.ErrorCode; } \
27   catch(UString &s) { ErrorMessage(s); result = E_FAIL; } \
28   catch(...) { result = E_FAIL; } \
29   if (result != S_OK && result != E_ABORT) \
30     ErrorMessageHRESULT(result);
31 
ThrowException_if_Error(HRESULT res)32 static void ThrowException_if_Error(HRESULT res)
33 {
34   if (res != S_OK)
35     throw CSystemException(res);
36 }
37 
38 #ifdef Z7_EXTERNAL_CODECS
39 
40 #define CREATE_CODECS \
41   CCodecs *codecs = new CCodecs; \
42   CMyComPtr<ICompressCodecsInfo> compressCodecsInfo = codecs; \
43   ThrowException_if_Error(codecs->Load()); \
44   Codecs_AddHashArcHandler(codecs);
45 
46 #define LOAD_EXTERNAL_CODECS \
47     CExternalCodecs _externalCodecs; \
48     _externalCodecs.GetCodecs = codecs; \
49     _externalCodecs.GetHashers = codecs; \
50     ThrowException_if_Error(_externalCodecs.Load());
51 
52 #else
53 
54 #define CREATE_CODECS \
55   CCodecs *codecs = new CCodecs; \
56   CMyComPtr<IUnknown> compressCodecsInfo = codecs; \
57   ThrowException_if_Error(codecs->Load()); \
58   Codecs_AddHashArcHandler(codecs);
59 
60 #define LOAD_EXTERNAL_CODECS
61 
62 #endif
63 
64 
65 
66 
GetQuotedString(const UString & s)67 UString GetQuotedString(const UString &s)
68 {
69   UString s2 ('\"');
70   s2 += s;
71   s2.Add_Char('\"');
72   return s2;
73 }
74 
ErrorMessage(LPCWSTR message)75 static void ErrorMessage(LPCWSTR message)
76 {
77   MessageBoxW(g_HWND, message, L"7-Zip", MB_ICONERROR);
78 }
79 
ErrorMessageHRESULT(HRESULT res)80 static void ErrorMessageHRESULT(HRESULT res)
81 {
82   ErrorMessage(HResultToMessage(res));
83 }
84 
ErrorLangMessage(UINT resourceID)85 static void ErrorLangMessage(UINT resourceID)
86 {
87   ErrorMessage(LangString(resourceID));
88 }
89 
CompressFiles(const UString & arcPathPrefix,const UString & arcName,const UString & arcType,bool addExtension,const UStringVector & names,bool email,bool showDialog,bool)90 HRESULT CompressFiles(
91     const UString &arcPathPrefix,
92     const UString &arcName,
93     const UString &arcType,
94     bool addExtension,
95     const UStringVector &names,
96     bool email, bool showDialog, bool /* waitFinish */)
97 {
98   MY_TRY_BEGIN
99 
100   CREATE_CODECS
101 
102   CUpdateCallbackGUI callback;
103 
104   callback.Init();
105 
106   CUpdateOptions uo;
107   uo.EMailMode = email;
108   uo.SetActionCommand_Add();
109 
110   uo.ArcNameMode = (addExtension ? k_ArcNameMode_Add : k_ArcNameMode_Exact);
111 
112   CObjectVector<COpenType> formatIndices;
113   if (!ParseOpenTypes(*codecs, arcType, formatIndices))
114   {
115     ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE);
116     return E_FAIL;
117   }
118   const UString arcPath = arcPathPrefix + arcName;
119   if (!uo.InitFormatIndex(codecs, formatIndices, arcPath) ||
120       !uo.SetArcPath(codecs, arcPath))
121   {
122     ErrorLangMessage(IDS_UPDATE_NOT_SUPPORTED);
123     return E_FAIL;
124   }
125 
126   NWildcard::CCensor censor;
127   FOR_VECTOR (i, names)
128   {
129     censor.AddPreItem_NoWildcard(names[i]);
130   }
131 
132   bool messageWasDisplayed = false;
133 
134   result = UpdateGUI(codecs,
135       formatIndices, arcPath,
136       censor, uo, showDialog, messageWasDisplayed, &callback, g_HWND);
137 
138   if (result != S_OK)
139   {
140     if (result != E_ABORT && messageWasDisplayed)
141       return E_FAIL;
142     throw CSystemException(result);
143   }
144   if (callback.FailedFiles.Size() > 0)
145   {
146     if (!messageWasDisplayed)
147       throw CSystemException(E_FAIL);
148     return E_FAIL;
149   }
150 
151   MY_TRY_FINISH
152   return S_OK;
153 }
154 
155 
ExtractGroupCommand(const UStringVector & arcPaths,bool showDialog,CExtractOptions & eo,const char * kType=NULL)156 static HRESULT ExtractGroupCommand(const UStringVector &arcPaths,
157     bool showDialog, CExtractOptions &eo, const char *kType = NULL)
158 {
159   MY_TRY_BEGIN
160 
161   CREATE_CODECS
162 
163   CExtractCallbackImp *ecs = new CExtractCallbackImp;
164   CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
165 
166   ecs->Init();
167 
168   // eo.CalcCrc = options.CalcCrc;
169 
170   UStringVector arcPathsSorted;
171   UStringVector arcFullPathsSorted;
172   {
173     NWildcard::CCensor arcCensor;
174     FOR_VECTOR (i, arcPaths)
175     {
176       arcCensor.AddPreItem_NoWildcard(arcPaths[i]);
177     }
178     arcCensor.AddPathsToCensor(NWildcard::k_RelatPath);
179     CDirItemsStat st;
180     EnumerateDirItemsAndSort(arcCensor, NWildcard::k_RelatPath, UString(),
181         arcPathsSorted, arcFullPathsSorted,
182         st,
183         NULL // &scan: change it!!!!
184         );
185   }
186 
187   CObjectVector<COpenType> formatIndices;
188   if (kType)
189   {
190     if (!ParseOpenTypes(*codecs, UString(kType), formatIndices))
191     {
192       throw CSystemException(E_INVALIDARG);
193       // ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE);
194       // return E_INVALIDARG;
195     }
196   }
197 
198   NWildcard::CCensor censor;
199   {
200     censor.AddPreItem_Wildcard();
201   }
202 
203   censor.AddPathsToCensor(NWildcard::k_RelatPath);
204 
205   bool messageWasDisplayed = false;
206 
207   ecs->MultiArcMode = (arcPathsSorted.Size() > 1);
208 
209   result = ExtractGUI(codecs,
210       formatIndices, CIntVector(),
211       arcPathsSorted, arcFullPathsSorted,
212       censor.Pairs.Front().Head, eo, NULL, showDialog, messageWasDisplayed, ecs, g_HWND);
213 
214   if (result != S_OK)
215   {
216     if (result != E_ABORT && messageWasDisplayed)
217       return E_FAIL;
218     throw CSystemException(result);
219   }
220   return ecs->IsOK() ? S_OK : E_FAIL;
221 
222   MY_TRY_FINISH
223   return result;
224 }
225 
ExtractArchives(const UStringVector & arcPaths,const UString & outFolder,bool showDialog,bool elimDup,UInt32 writeZone)226 void ExtractArchives(const UStringVector &arcPaths, const UString &outFolder,
227     bool showDialog, bool elimDup, UInt32 writeZone)
228 {
229   CExtractOptions eo;
230   eo.OutputDir = us2fs(outFolder);
231   eo.TestMode = false;
232   eo.ElimDup.Val = elimDup;
233   eo.ElimDup.Def = elimDup;
234   if (writeZone != (UInt32)(Int32)-1)
235     eo.ZoneMode = (NExtract::NZoneIdMode::EEnum)writeZone;
236   ExtractGroupCommand(arcPaths, showDialog, eo);
237 }
238 
TestArchives(const UStringVector & arcPaths,bool hashMode)239 void TestArchives(const UStringVector &arcPaths, bool hashMode)
240 {
241   CExtractOptions eo;
242   eo.TestMode = true;
243   ExtractGroupCommand(arcPaths,
244       true, // showDialog
245       eo,
246       hashMode ? "hash" : NULL);
247 }
248 
CalcChecksum(const UStringVector & paths,const UString & methodName,const UString & arcPathPrefix,const UString & arcFileName)249 void CalcChecksum(const UStringVector &paths,
250     const UString &methodName,
251     const UString &arcPathPrefix,
252     const UString &arcFileName)
253 {
254   MY_TRY_BEGIN
255 
256   if (!arcFileName.IsEmpty())
257   {
258     CompressFiles(
259       arcPathPrefix,
260       arcFileName,
261       UString("hash"),
262       false, // addExtension,
263       paths,
264       false, // email,
265       false, // showDialog,
266       false  // waitFinish
267       );
268     return;
269   }
270 
271   CREATE_CODECS
272   LOAD_EXTERNAL_CODECS
273 
274   NWildcard::CCensor censor;
275   FOR_VECTOR (i, paths)
276   {
277     censor.AddPreItem_NoWildcard(paths[i]);
278   }
279 
280   censor.AddPathsToCensor(NWildcard::k_RelatPath);
281   bool messageWasDisplayed = false;
282 
283   CHashOptions options;
284   options.Methods.Add(methodName);
285 
286   /*
287   if (!arcFileName.IsEmpty())
288     options.HashFilePath = arcPathPrefix + arcFileName;
289   */
290 
291   result = HashCalcGUI(EXTERNAL_CODECS_VARS_L censor, options, messageWasDisplayed);
292   if (result != S_OK)
293   {
294     if (result != E_ABORT && messageWasDisplayed)
295       return; //  E_FAIL;
296     throw CSystemException(result);
297   }
298 
299   MY_TRY_FINISH
300   return; //  result;
301 }
302 
Benchmark(bool totalMode)303 void Benchmark(bool totalMode)
304 {
305   MY_TRY_BEGIN
306 
307   CREATE_CODECS
308   LOAD_EXTERNAL_CODECS
309 
310   CObjectVector<CProperty> props;
311   if (totalMode)
312   {
313     CProperty prop;
314     prop.Name = "m";
315     prop.Value = "*";
316     props.Add(prop);
317   }
318   result = Benchmark(
319       EXTERNAL_CODECS_VARS_L
320       props,
321       k_NumBenchIterations_Default,
322       g_HWND);
323 
324   MY_TRY_FINISH
325 }
326 
327 #endif
328